3 * $Id: xgetline.c,v 1.12 2002/01/13 14:43:41 mdw Exp $
5 * Fetch a line of text from the user
7 * (c) 1998 Straylight/Edgeware
10 /*----- Licensing notice --------------------------------------------------*
12 * This file is part of the Edgeware X tools collection.
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.
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.
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.
29 /*----- Revision history --------------------------------------------------*
31 * $Log: xgetline.c,v $
32 * Revision 1.12 2002/01/13 14:43:41 mdw
33 * Kill some spurious warnings.
35 * Revision 1.11 1999/08/20 07:30:33 mdw
36 * Miscellaneous changes, mostly concerning options parsing.
38 * Revision 1.10 1999/05/21 22:09:19 mdw
39 * Take advantage of new dynamic string macros.
41 * Revision 1.9 1999/05/05 18:54:37 mdw
42 * Keep blank lines out of the history list.
44 * Revision 1.8 1998/12/16 19:58:53 mdw
45 * Stop the dropdown list from dropping down when you press enter.
47 * Revision 1.7 1998/12/11 09:53:02 mdw
48 * Updates for mLib/mgLib. Support history files for recalling past
49 * entries, using a drop-down list.
51 * Revision 1.6 1998/12/03 00:56:29 mdw
52 * Set focus on the entry field, rather than leaving things to luck.
54 * Revision 1.5 1998/12/03 00:39:44 mdw
55 * Force focus when starting up.
57 * Revision 1.4 1998/11/30 22:36:47 mdw
58 * Tidy up tabbing in help texts very slightly.
60 * Revision 1.3 1998/11/21 22:30:20 mdw
61 * Support GNU-style long options throughout, and introduce proper help
62 * text to all programs. Update manual pages to match.
64 * Revision 1.2 1998/11/18 21:25:30 mdw
65 * Remove bogus `-h' option from the options list.
67 * Revision 1.1 1998/11/16 23:00:49 mdw
72 /*----- Header files ------------------------------------------------------*/
82 #include <gdk/gdkkeysyms.h>
84 #include <mLib/alloc.h>
85 #include <mLib/dstr.h>
86 #include <mLib/mdwopt.h>
87 #include <mLib/report.h>
88 #include <mLib/quis.h>
90 #include <mgLib/cancel.h>
91 #include <mgLib/mdwfocus.h>
93 /*----- Main code ---------------------------------------------------------*/
97 * Arguments: @GtkWidget *w@ = widget raising the signal
98 * @gpointer *p@ = pointer to integer result code
102 * Use: Sets the result code to zero (failure) and ends the loop.
105 static void quit(GtkWidget
*w
, gpointer
*p
)
114 * Arguments: @GtkWidget *w@ = widget raising the signal
115 * @gpointer *p@ = pointer to integer result code
119 * Use: Sets the result code nonzero (success) and ends the loop.
122 static void done(GtkWidget
*w
, gpointer
*p
)
129 /* --- @version@ --- *
131 * Arguments: @FILE *fp@ = output stream to print the message on
135 * Use: Spits out a version message.
138 static void version(FILE *fp
)
140 fprintf(fp
, "%s (xtoys version " VERSION
")\n", QUIS
);
145 * Arguments: @FILE *fp@ = output stream to print the message on
149 * Use: Spits out a usage message.
152 static void usage(FILE *fp
)
155 "Usage: %s [-in] [-t title] [-p prompt] [-d default]\n"
156 "\t[-l|-H file] [-m max]\n",
162 * Arguments: @int argc@ = number of command line arguments
163 * @char *argv[]@ = addresses of arguments
165 * Returns: Zero if OK, and we read a string; nonzero if the user
168 * Use: Reads a string from the user, and returns it on standard
172 int main(int argc
, char *argv
[])
174 /* --- Configuration variables --- */
178 char *title
= "Input request";
183 const char *list
= 0;
190 #define f_nochoice 8u
192 /* --- User interface bits --- */
199 /* --- Crank up the toolkit --- *
201 * Have to do this here: GTK snarfs some command line options which my
202 * parser would barf about.
206 gtk_init(&argc
, &argv
);
208 /* --- Parse options from command line --- */
212 /* --- Long options structure --- */
214 static struct option opt
[] = {
215 { "help", 0, 0, 'h' },
216 { "usage", 0, 0, 'u' },
217 { "version", 0, 0, 'v' },
218 { "title", OPTF_ARGREQ
, 0, 't' },
219 { "prompt", OPTF_ARGREQ
, 0, 'p' },
220 { "default", OPTF_ARGREQ
, 0, 'd' },
221 { "password", 0, 0, 'i' },
222 { "invisible", 0, 0, 'i' },
223 { "history", OPTF_ARGREQ
, 0, 'H' },
224 { "list", OPTF_ARGREQ
, 0, 'l' },
225 { "histmax", OPTF_ARGREQ
, 0, 'm' },
226 { "no-choice", 0, 0, 'n' },
231 /* --- Fetch an option --- */
233 i
= getopt_long(argc
, argv
, "huv t:p:d:i H:l:m:n", opt
, 0);
237 /* --- Work out what to do with it --- */
246 Pops up a small window requesting input from a user, and echoes the\n\
247 response to stdout, where it can be collected by a shell script.\n\
249 Options available are:\n\
251 -h, --help Display this help text\n\
252 -u, --usage Display a short usage summary\n\
253 -v, --version Display the program's version number\n\
255 -i, --invisible Don't show the user's string as it's typed\n\
256 -t, --title=TITLE Set the window's title string\n\
257 -p, --prompt=PROMPT Set the window's prompt string\n\
258 -d, --default=DEFAULT Set the default string already in the window\n\
260 -l, --list=FILE Read FILE into a drop-down list\n\
261 -n, --no-choice No free text input: must choose item from list\n\
262 -H, --history=FILE As for `--list', but update with new string\n\
263 -m, --histmax=MAX Maximum number of items written back to file\n",
300 histmax
= atoi(optarg
);
314 if ((f
& f_invis
) && list
) {
316 "invisible entry is dumb if you provide a list of alternatives!");
319 if ((f
& f_nochoice
) && !list
)
320 die(EXIT_FAILURE
, "nothing to restrict choice to!");
322 /* --- Create the main window --- */
324 win
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
325 gtk_window_set_title(GTK_WINDOW(win
), title
);
326 gtk_window_position(GTK_WINDOW(win
), GTK_WIN_POS_MOUSE
);
327 gtk_signal_connect(GTK_OBJECT(win
), "destroy",
328 GTK_SIGNAL_FUNC(quit
), &ok
);
330 /* --- Create the box for laying out the widgets inside --- */
332 left
= (prompt ?
1 : 0);
333 box
= gtk_table_new(left
+ 2, 1, 0);
335 /* --- Maybe create a prompt widget --- */
338 GtkWidget
*w
= gtk_label_new(prompt
);
339 gtk_table_attach(GTK_TABLE(box
), w
,
340 0, 1, 0, 1, 0, GTK_EXPAND
, 4, 2);
344 /* --- Create the entry widget --- */
347 FILE *fp
= fopen(list
, "r");
350 /* --- Read the items in from the file --- *
352 * Inability to open the file is not a disaster.
358 while (dstr_putline(&d
, fp
) != EOF
) {
359 hist
= g_list_append(hist
, xstrdup(d
.buf
));
366 /* --- Now create a combo box --- */
368 combo
= gtk_combo_new();
369 entry
= GTK_COMBO(combo
)->entry
;
371 gtk_combo_set_popdown_strings(GTK_COMBO(combo
), hist
);
373 /* --- Do other configuring --- */
375 if (f
& f_nochoice
) {
376 gtk_combo_set_value_in_list(GTK_COMBO(combo
), 1, 0);
377 gtk_entry_set_editable(GTK_ENTRY(entry
), 0);
379 gtk_combo_set_case_sensitive(GTK_COMBO(combo
), 1);
380 gtk_combo_set_use_arrows_always(GTK_COMBO(combo
), 1);
381 gtk_combo_disable_activate(GTK_COMBO(combo
));
382 if (strcmp(dfl
, "@") == 0)
383 gtk_entry_set_text(GTK_ENTRY(entry
), hist ?
(char *)hist
->data
: "");
385 gtk_entry_set_text(GTK_ENTRY(entry
), dfl
);
387 /* --- Set the widget in the right place and show it --- */
389 gtk_table_attach(GTK_TABLE(box
), combo
,
390 left
, left
+ 1, 0, 1,
391 GTK_EXPAND
| GTK_FILL
, GTK_EXPAND
, 4, 2);
392 gtk_widget_show(combo
);
394 entry
= gtk_entry_new();
395 gtk_entry_set_text(GTK_ENTRY(entry
), dfl
);
397 gtk_entry_set_visibility(GTK_ENTRY(entry
), FALSE
);
398 gtk_table_attach(GTK_TABLE(box
), entry
,
399 left
, left
+ 1, 0, 1,
400 GTK_EXPAND
| GTK_FILL
, GTK_EXPAND
, 4, 2);
401 gtk_widget_show(entry
);
404 /* --- Create the default action widget --- */
406 btn
= gtk_button_new_with_label("OK");
407 gtk_table_attach(GTK_TABLE(box
), btn
,
408 left
+ 1, left
+ 2, 0, 1, 0, GTK_EXPAND
, 2, 2);
409 GTK_WIDGET_SET_FLAGS(btn
, GTK_CAN_DEFAULT
);
410 gtk_widget_show(btn
);
412 /* --- Add the box into the main window --- */
414 gtk_container_add(GTK_CONTAINER(win
), box
);
415 gtk_widget_show(box
);
417 /* --- Last minute configuration things --- */
419 gtk_widget_grab_default(btn
);
420 gtk_signal_connect(GTK_OBJECT(btn
), "clicked",
421 GTK_SIGNAL_FUNC(done
), &ok
);
422 gtk_signal_connect_object(GTK_OBJECT(entry
), "activate",
423 GTK_SIGNAL_FUNC(gtk_widget_activate
),
425 cancel(GTK_WINDOW(win
), 0);
427 /* --- Go go go --- */
429 gtk_widget_realize(win
);
431 gtk_widget_grab_focus(entry
);
432 gtk_widget_show(win
);
435 /* --- Output the result --- */
438 char *p
= gtk_entry_get_text(GTK_ENTRY(entry
));
440 /* --- If history is enabled, output a new history file --- *
442 * If the first entry was accepted verbatim, or if the entry is a blank
443 * line, don't bother.
446 if (f
& f_history
&& *p
&& !(hist
&& strcmp(p
, hist
->data
) == 0)) {
452 if ((fd
= open(list
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600)) < 0)
454 if ((fp
= fdopen(fd
, "w")) == 0) {
462 for (i
= 1, g
= hist
; (histmax
< 1 || i
< histmax
) && g
; g
= g
->next
) {
463 if (*(char *)g
->data
&& strcmp(g
->data
, p
) != 0) {
473 /* --- Print the result and go away --- */
478 return (ok ? EXIT_SUCCESS
: EXIT_FAILURE
);
481 /*----- That's all, folks -------------------------------------------------*/