3 * $Id: xgetline.c,v 1.9 1999/05/05 18:54:37 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.9 1999/05/05 18:54:37 mdw
33 * Keep blank lines out of the history list.
35 * Revision 1.8 1998/12/16 19:58:53 mdw
36 * Stop the dropdown list from dropping down when you press enter.
38 * Revision 1.7 1998/12/11 09:53:02 mdw
39 * Updates for mLib/mgLib. Support history files for recalling past
40 * entries, using a drop-down list.
42 * Revision 1.6 1998/12/03 00:56:29 mdw
43 * Set focus on the entry field, rather than leaving things to luck.
45 * Revision 1.5 1998/12/03 00:39:44 mdw
46 * Force focus when starting up.
48 * Revision 1.4 1998/11/30 22:36:47 mdw
49 * Tidy up tabbing in help texts very slightly.
51 * Revision 1.3 1998/11/21 22:30:20 mdw
52 * Support GNU-style long options throughout, and introduce proper help
53 * text to all programs. Update manual pages to match.
55 * Revision 1.2 1998/11/18 21:25:30 mdw
56 * Remove bogus `-h' option from the options list.
58 * Revision 1.1 1998/11/16 23:00:49 mdw
63 /*----- Header files ------------------------------------------------------*/
73 #include <gdk/gdkkeysyms.h>
75 #include <mLib/alloc.h>
76 #include <mLib/dstr.h>
77 #include <mLib/mdwopt.h>
78 #include <mLib/report.h>
79 #include <mLib/quis.h>
81 #include <mgLib/cancel.h>
82 #include <mgLib/mdwfocus.h>
84 /*----- Main code ---------------------------------------------------------*/
88 * Arguments: @GtkWidget *w@ = widget raising the signal
89 * @gpointer *p@ = pointer to integer result code
93 * Use: Sets the result code to zero (failure) and ends the loop.
96 static void quit(GtkWidget
*w
, gpointer
*p
)
105 * Arguments: @GtkWidget *w@ = widget raising the signal
106 * @gpointer *p@ = pointer to integer result code
110 * Use: Sets the result code nonzero (success) and ends the loop.
113 static void done(GtkWidget
*w
, gpointer
*p
)
120 /* --- @version@ --- *
122 * Arguments: @FILE *fp@ = output stream to print the message on
126 * Use: Spits out a version message.
129 static void version(FILE *fp
)
131 fprintf(fp
, "%s (xtoys version " VERSION
")\n", QUIS
);
136 * Arguments: @FILE *fp@ = output stream to print the message on
140 * Use: Spits out a usage message.
143 static void usage(FILE *fp
)
146 "Usage: %s [-in] [-t title] [-p prompt] [-d default]\n"
147 "\t[-l|-H file] [-m max]\n",
153 * Arguments: @int argc@ = number of command line arguments
154 * @char *argv[]@ = addresses of arguments
156 * Returns: Zero if OK, and we read a string; nonzero if the user
159 * Use: Reads a string from the user, and returns it on standard
163 int main(int argc
, char *argv
[])
165 /* --- Configuration variables --- */
169 char *title
= "Input request";
174 const char *list
= 0;
185 /* --- User interface bits --- */
192 /* --- Crank up the toolkit --- *
194 * Have to do this here: GTK snarfs some command line options which my
195 * parser would barf about.
199 gtk_init(&argc
, &argv
);
201 /* --- Parse options from command line --- */
205 /* --- Long options structure --- */
207 static struct option opt
[] = {
208 { "help", 0, 0, 'h' },
209 { "usage", 0, 0, 'u' },
210 { "version", 0, 0, 'v' },
211 { "title", required_argument
, 0, 't' },
212 { "prompt", required_argument
, 0, 'p' },
213 { "default", required_argument
, 0, 'd' },
214 { "password", 0, 0, 'i' },
215 { "invisible", 0, 0, 'i' },
216 { "history", required_argument
, 0, 'H' },
217 { "list", required_argument
, 0, 'l' },
218 { "histmax", required_argument
, 0, 'm' },
219 { "no-choice", 0, 0, 'n' },
224 /* --- Fetch an option --- */
226 i
= getopt_long(argc
, argv
, "huv t:p:d:i H:l:m:n", opt
, 0);
230 /* --- Work out what to do with it --- */
239 "Pops up a small window requesting input from a user, and echoes the\n"
240 "response to stdout, where it can be collected by a shell script.\n"
242 "Options available are:\n"
244 "-h, --help Display this help text\n"
245 "-u, --usage Display a short usage summary\n"
246 "-v, --version Display the program's version number\n"
248 "-i, --invisible\t Don't show the user's string as it's typed\n"
249 "-t, --title=TITLE Set the window's title string\n"
250 "-p, --prompt=PROMPT Set the window's prompt string\n"
251 "-d, --default=DEFAULT Set the default string already in the window\n"
253 "-l, --list=FILE Read FILE into a drop-down list\n"
254 "-n, --no-choice No free text input: must choose item from list\n"
255 "-H, --history=FILE As for `--list', but update with new string\n"
256 "-m, --histmax=MAX Maximum number of items written back to file\n",
293 histmax
= atoi(optarg
);
307 if ((f
& f_invis
) && list
) {
309 "invisible entry is dumb if you provide a list of alternatives!");
312 if ((f
& f_nochoice
) && !list
)
313 die(EXIT_FAILURE
, "nothing to restrict choice to!");
315 /* --- Create the main window --- */
317 win
= gtk_window_new(GTK_WINDOW_TOPLEVEL
);
318 gtk_window_set_title(GTK_WINDOW(win
), title
);
319 gtk_window_position(GTK_WINDOW(win
), GTK_WIN_POS_MOUSE
);
320 gtk_signal_connect(GTK_OBJECT(win
), "destroy",
321 GTK_SIGNAL_FUNC(quit
), &ok
);
323 /* --- Create the box for laying out the widgets inside --- */
325 left
= (prompt ?
1 : 0);
326 box
= gtk_table_new(left
+ 2, 1, 0);
328 /* --- Maybe create a prompt widget --- */
331 GtkWidget
*w
= gtk_label_new(prompt
);
332 gtk_table_attach(GTK_TABLE(box
), w
,
333 0, 1, 0, 1, 0, GTK_EXPAND
, 4, 2);
337 /* --- Create the entry widget --- */
340 FILE *fp
= fopen(list
, "r");
343 /* --- Read the items in from the file --- *
345 * Inability to open the file is not a disaster.
353 while (dstr_putline(&d
, fp
) != EOF
) {
354 hist
= g_list_append(hist
, xstrdup(d
.buf
));
360 /* --- Now create a combo box --- */
362 combo
= gtk_combo_new();
363 entry
= GTK_COMBO(combo
)->entry
;
365 gtk_combo_set_popdown_strings(GTK_COMBO(combo
), hist
);
367 /* --- Do other configuring --- */
369 if (f
& f_nochoice
) {
370 gtk_combo_set_value_in_list(GTK_COMBO(combo
), 1, 0);
371 gtk_entry_set_editable(GTK_ENTRY(entry
), 0);
373 gtk_combo_set_case_sensitive(GTK_COMBO(combo
), 1);
374 gtk_combo_set_use_arrows_always(GTK_COMBO(combo
), 1);
375 gtk_combo_disable_activate(GTK_COMBO(combo
));
376 if (strcmp(dfl
, "@") == 0)
377 gtk_entry_set_text(GTK_ENTRY(entry
), hist ?
(char *)hist
->data
: "");
379 gtk_entry_set_text(GTK_ENTRY(entry
), dfl
);
381 /* --- Set the widget in the right place and show it --- */
383 gtk_table_attach(GTK_TABLE(box
), combo
,
384 left
, left
+ 1, 0, 1,
385 GTK_EXPAND
| GTK_FILL
, GTK_EXPAND
, 4, 2);
386 gtk_widget_show(combo
);
388 entry
= gtk_entry_new();
389 gtk_entry_set_text(GTK_ENTRY(entry
), dfl
);
391 gtk_entry_set_visibility(GTK_ENTRY(entry
), FALSE
);
392 gtk_table_attach(GTK_TABLE(box
), entry
,
393 left
, left
+ 1, 0, 1,
394 GTK_EXPAND
| GTK_FILL
, GTK_EXPAND
, 4, 2);
395 gtk_widget_show(entry
);
398 /* --- Create the default action widget --- */
400 btn
= gtk_button_new_with_label("OK");
401 gtk_table_attach(GTK_TABLE(box
), btn
,
402 left
+ 1, left
+ 2, 0, 1, 0, GTK_EXPAND
, 2, 2);
403 GTK_WIDGET_SET_FLAGS(btn
, GTK_CAN_DEFAULT
);
404 gtk_widget_show(btn
);
406 /* --- Add the box into the main window --- */
408 gtk_container_add(GTK_CONTAINER(win
), box
);
409 gtk_widget_show(box
);
411 /* --- Last minute configuration things --- */
413 gtk_widget_grab_default(btn
);
414 gtk_signal_connect(GTK_OBJECT(btn
), "clicked",
415 GTK_SIGNAL_FUNC(done
), &ok
);
416 gtk_signal_connect_object(GTK_OBJECT(entry
), "activate",
417 GTK_SIGNAL_FUNC(gtk_widget_activate
),
419 cancel(GTK_WINDOW(win
), 0);
421 /* --- Go go go --- */
423 gtk_widget_realize(win
);
425 gtk_widget_grab_focus(entry
);
426 gtk_widget_show(win
);
429 /* --- Output the result --- */
432 char *p
= gtk_entry_get_text(GTK_ENTRY(entry
));
434 /* --- If history is enabled, output a new history file --- *
436 * If the first entry was accepted verbatim, or if the entry is a blank
437 * line, don't bother.
440 if (f
& f_history
&& *p
&& !(hist
&& strcmp(p
, hist
->data
) == 0)) {
446 if ((fd
= open(list
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600)) < 0)
448 if ((fp
= fdopen(fd
, "w")) == 0) {
456 for (i
= 1, g
= hist
; (histmax
< 1 || i
< histmax
) && g
; g
= g
->next
) {
457 if (*(char *)g
->data
&& strcmp(g
->data
, p
) != 0) {
467 /* --- Print the result and go away --- */
472 return (ok ? EXIT_SUCCESS
: EXIT_FAILURE
);
475 /*----- That's all, folks -------------------------------------------------*/