chiark / gitweb /
xwait, xtell: Add short help options!
[xtoys] / xgetline.c
1 /* -*-c-*-
2  *
3  * $Id: xgetline.c,v 1.13 2004/04/08 01:36:29 mdw Exp $
4  *
5  * Fetch a line of text from the user
6  *
7  * (c) 1998 Straylight/Edgeware
8  */
9
10 /*----- Licensing notice --------------------------------------------------* 
11  *
12  * This file is part of the Edgeware X tools collection.
13  *
14  * X tools is free software; you can redistribute it and/or modify
15  * it under the terms of the GNU General Public License as published by
16  * the Free Software Foundation; either version 2 of the License, or
17  * (at your option) any later version.
18  * 
19  * X tools is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  * 
24  * You should have received a copy of the GNU General Public License
25  * along with X tools; if not, write to the Free Software Foundation,
26  * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
27  */
28
29 /*----- Header files ------------------------------------------------------*/
30
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34
35 #include <fcntl.h>
36 #include <unistd.h>
37
38 #include <gtk/gtk.h>
39 #include <gdk/gdkkeysyms.h>
40
41 #include <mLib/alloc.h>
42 #include <mLib/dstr.h>
43 #include <mLib/mdwopt.h>
44 #include <mLib/report.h>
45 #include <mLib/quis.h>
46
47 #include <mgLib/cancel.h>
48 #include <mgLib/mdwfocus.h>
49
50 /*----- Main code ---------------------------------------------------------*/
51
52 /* --- @quit@ --- *
53  *
54  * Arguments:   @GtkWidget *w@ = widget raising the signal
55  *              @gpointer *p@ = pointer to integer result code
56  *
57  * Returns:     ---
58  *
59  * Use:         Sets the result code to zero (failure) and ends the loop.
60  */
61
62 static void quit(GtkWidget *w, gpointer *p)
63 {
64   int *ip = (int *)p;
65   *ip = 0;
66   gtk_main_quit();
67 }
68
69 /* --- @done@ --- *
70  *
71  * Arguments:   @GtkWidget *w@ = widget raising the signal
72  *              @gpointer *p@ = pointer to integer result code
73  *
74  * Returns:     ---
75  *
76  * Use:         Sets the result code nonzero (success) and ends the loop.
77  */
78
79 static void done(GtkWidget *w, gpointer *p)
80 {
81   int *ip = (int *)p;
82   *ip = 1;
83   gtk_main_quit();
84 }
85
86 /* --- @version@ --- *
87  *
88  * Arguments:   @FILE *fp@ = output stream to print the message on
89  *
90  * Returns:     ---
91  *
92  * Use:         Spits out a version message.
93  */
94
95 static void version(FILE *fp)
96 {
97   fprintf(fp, "%s (xtoys version " VERSION ")\n", QUIS);
98 }
99
100 /* --- @usage@ --- *
101  *
102  * Arguments:   @FILE *fp@ = output stream to print the message on
103  *
104  * Returns:     ---
105  *
106  * Use:         Spits out a usage message.
107  */
108
109 static void usage(FILE *fp)
110 {
111   fprintf(fp,
112           "Usage: %s [-in] [-t title] [-p prompt] [-d default]\n"
113           "\t[-l|-H file] [-m max]\n",
114           QUIS);
115 }
116
117 /* --- @main@ --- *
118  *
119  * Arguments:   @int argc@ = number of command line arguments
120  *              @char *argv[]@ = addresses of arguments
121  *
122  * Returns:     Zero if OK, and we read a string; nonzero if the user
123  *              cancelled.
124  *
125  * Use:         Reads a string from the user, and returns it on standard
126  *              output.
127  */
128
129 int main(int argc, char *argv[])
130 {
131   /* --- Configuration variables --- */
132
133   char *prompt = 0;
134   char *dfl = "";
135   char *title = "Input request";
136   int left;
137   unsigned f = 0;
138   int ok = 0;
139
140   const char *list = 0;
141   int histmax = 20;
142   GList *hist = 0;
143
144 #define f_invis 1u
145 #define f_duff 2u
146 #define f_history 4u
147 #define f_nochoice 8u
148
149   /* --- User interface bits --- */
150
151   GtkWidget *win;
152   GtkWidget *box;
153   GtkWidget *entry;
154   GtkWidget *btn;
155
156   /* --- Crank up the toolkit --- *
157    *
158    * Have to do this here: GTK snarfs some command line options which my
159    * parser would barf about.
160    */   
161
162   ego(argv[0]);
163   gtk_init(&argc, &argv);
164
165   /* --- Parse options from command line --- */
166
167   for (;;) {
168
169     /* --- Long options structure --- */
170
171     static struct option opt[] = {
172       { "help",         0,              0,      'h' },
173       { "usage",        0,              0,      'u' },
174       { "version",      0,              0,      'v' },
175       { "title",        OPTF_ARGREQ,    0,      't' },
176       { "prompt",       OPTF_ARGREQ,    0,      'p' },
177       { "default",      OPTF_ARGREQ,    0,      'd' },
178       { "password",     0,              0,      'i' },
179       { "invisible",    0,              0,      'i' },
180       { "history",      OPTF_ARGREQ,    0,      'H' },
181       { "list",         OPTF_ARGREQ,    0,      'l' },
182       { "histmax",      OPTF_ARGREQ,    0,      'm' },
183       { "no-choice",    0,              0,      'n' },
184       { 0,              0,              0,      0 }
185     };
186     int i;
187
188     /* --- Fetch an option --- */
189
190     i = getopt_long(argc, argv, "huv t:p:d:i H:l:m:n", opt, 0);
191     if (i < 0)
192       break;
193
194     /* --- Work out what to do with it --- */
195
196     switch (i) {
197       case 'h':
198         version(stdout);
199         fputs("\n", stdout);
200         usage(stdout);
201         fputs("\
202 \n\
203 Pops up a small window requesting input from a user, and echoes the\n\
204 response to stdout, where it can be collected by a shell script.\n\
205 \n\
206 Options available are:\n\
207 \n\
208 -h, --help              Display this help text\n\
209 -u, --usage             Display a short usage summary\n\
210 -v, --version           Display the program's version number\n\
211 \n\
212 -i, --invisible         Don't show the user's string as it's typed\n\
213 -t, --title=TITLE       Set the window's title string\n\
214 -p, --prompt=PROMPT     Set the window's prompt string\n\
215 -d, --default=DEFAULT   Set the default string already in the window\n\
216 \n\
217 -l, --list=FILE         Read FILE into a drop-down list\n\
218 -n, --no-choice         No free text input: must choose item from list\n\
219 -H, --history=FILE      As for `--list', but update with new string\n\
220 -m, --histmax=MAX       Maximum number of items written back to file\n",
221           stdout);
222         exit(0);
223         break;
224       case 'u':
225         usage(stdout);
226         exit(0);
227         break;
228       case 'v':
229         version(stdout);
230         exit(0);
231         break;
232
233       case 't':
234         title = optarg;
235         break;
236       case 'p':
237         prompt = optarg;
238         break;
239       case 'd':
240         dfl = optarg;
241         break;
242       case 'i':
243         f |= f_invis;
244         break;
245
246       case 'l':
247         list = optarg;
248         break;
249       case 'n':
250         f |= f_nochoice;
251         break;
252       case 'H':
253         f |= f_history;
254         list = optarg;
255         break;
256       case 'm':
257         histmax = atoi(optarg);
258         break;
259
260       default:
261         f |= f_duff;
262         break;
263     }
264   }
265
266   if (f & f_duff) {
267     usage(stderr);
268     exit(EXIT_FAILURE);
269   }
270
271   if ((f & f_invis) && list) {
272     die(EXIT_FAILURE,
273         "invisible entry is dumb if you provide a list of alternatives!");
274   }
275
276   if ((f & f_nochoice) && !list)
277     die(EXIT_FAILURE, "nothing to restrict choice to!");
278
279   /* --- Create the main window --- */
280
281   win = gtk_window_new(GTK_WINDOW_TOPLEVEL);
282   gtk_window_set_title(GTK_WINDOW(win), title);
283   gtk_window_position(GTK_WINDOW(win), GTK_WIN_POS_MOUSE);
284   gtk_signal_connect(GTK_OBJECT(win), "destroy",
285                      GTK_SIGNAL_FUNC(quit), &ok);
286
287   /* --- Create the box for laying out the widgets inside --- */
288
289   left = (prompt ? 1 : 0);
290   box = gtk_table_new(left + 2, 1, 0);
291
292   /* --- Maybe create a prompt widget --- */
293
294   if (prompt) {
295     GtkWidget *w = gtk_label_new(prompt);
296     gtk_table_attach(GTK_TABLE(box), w,
297                      0, 1, 0, 1, 0, GTK_EXPAND, 4, 2);
298     gtk_widget_show(w);
299   }
300
301   /* --- Create the entry widget --- */
302
303   if (list) {
304     FILE *fp = fopen(list, "r");
305     GtkWidget *combo;
306
307     /* --- Read the items in from the file --- *
308      *
309      * Inability to open the file is not a disaster.
310      */
311
312     if (fp) {
313       dstr d = DSTR_INIT;
314
315       while (dstr_putline(&d, fp) != EOF) {
316         hist = g_list_append(hist, xstrdup(d.buf));
317         DRESET(&d);
318       }
319       dstr_destroy(&d);
320       fclose(fp);
321     }
322
323     /* --- Now create a combo box --- */
324
325     combo = gtk_combo_new();
326     entry = GTK_COMBO(combo)->entry;
327     if (hist)
328       gtk_combo_set_popdown_strings(GTK_COMBO(combo), hist);
329
330     /* --- Do other configuring --- */
331
332     if (f & f_nochoice) {
333       gtk_combo_set_value_in_list(GTK_COMBO(combo), 1, 0);
334       gtk_entry_set_editable(GTK_ENTRY(entry), 0);
335     }
336     gtk_combo_set_case_sensitive(GTK_COMBO(combo), 1);
337     gtk_combo_set_use_arrows_always(GTK_COMBO(combo), 1);
338     gtk_combo_disable_activate(GTK_COMBO(combo));
339     if (strcmp(dfl, "@") == 0)
340       gtk_entry_set_text(GTK_ENTRY(entry), hist ? (char *)hist->data : "");
341     else
342       gtk_entry_set_text(GTK_ENTRY(entry), dfl);
343
344     /* --- Set the widget in the right place and show it --- */
345
346     gtk_table_attach(GTK_TABLE(box), combo,
347                      left, left + 1, 0, 1,
348                      GTK_EXPAND | GTK_FILL, GTK_EXPAND, 4, 2);
349     gtk_widget_show(combo);
350   } else {
351     entry = gtk_entry_new();
352     gtk_entry_set_text(GTK_ENTRY(entry), dfl);
353     if (f & f_invis)
354       gtk_entry_set_visibility(GTK_ENTRY(entry), FALSE);
355     gtk_table_attach(GTK_TABLE(box), entry,
356                      left, left + 1, 0, 1,
357                      GTK_EXPAND | GTK_FILL, GTK_EXPAND, 4, 2);
358     gtk_widget_show(entry);
359   }
360
361   /* --- Create the default action widget --- */
362
363   btn = gtk_button_new_with_label("OK");
364   gtk_table_attach(GTK_TABLE(box), btn,
365                    left + 1, left + 2, 0, 1, 0, GTK_EXPAND, 2, 2);
366   GTK_WIDGET_SET_FLAGS(btn, GTK_CAN_DEFAULT);
367   gtk_widget_show(btn);
368
369   /* --- Add the box into the main window --- */
370
371   gtk_container_add(GTK_CONTAINER(win), box);
372   gtk_widget_show(box);
373
374   /* --- Last minute configuration things --- */
375
376   gtk_widget_grab_default(btn);
377   gtk_signal_connect(GTK_OBJECT(btn), "clicked",
378                      GTK_SIGNAL_FUNC(done), &ok);
379   gtk_signal_connect_object(GTK_OBJECT(entry), "activate",
380                             GTK_SIGNAL_FUNC(gtk_widget_activate),
381                             GTK_OBJECT(btn));
382   cancel(GTK_WINDOW(win), 0);
383
384   /* --- Go go go --- */
385
386   gtk_widget_realize(win);
387   mdwfocus(win);
388   gtk_widget_grab_focus(entry);
389   gtk_widget_show(win);
390   gtk_main();
391
392   /* --- Output the result --- */
393
394   if (ok) {
395     char *p = gtk_entry_get_text(GTK_ENTRY(entry));
396
397     /* --- If history is enabled, output a new history file --- *
398      *
399      * If the first entry was accepted verbatim, or if the entry is a blank
400      * line, don't bother.
401      */
402
403     if (f & f_history && *p && !(hist && strcmp(p, hist->data) == 0)) {
404       int fd;
405       FILE *fp;
406       int i;
407       GList *g;
408
409       if ((fd = open(list, O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0)
410         goto fail;
411       if ((fp = fdopen(fd, "w")) == 0) {
412         close(fd);
413         goto fail;
414       }
415
416       fputs(p, fp);
417       fputc('\n', fp);
418
419       for (i = 1, g = hist; (histmax < 1 || i < histmax) && g; g = g->next) {
420         if (*(char *)g->data && strcmp(g->data, p) != 0) {
421           fputs(g->data, fp);
422           fputc('\n', fp);
423           i++;
424         }
425       }
426       fclose(fp);
427     fail:;
428     }
429
430     /* --- Print the result and go away --- */
431       
432     puts(p);
433   }
434
435   return (ok ? EXIT_SUCCESS : EXIT_FAILURE);
436 }
437
438 /*----- That's all, folks -------------------------------------------------*/