3 * $Id: xgetline.c,v 1.13 2004/04/08 01:36:29 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 /*----- Header files ------------------------------------------------------*/
39 #include <gdk/gdkkeysyms.h>
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>
47 #include <mgLib/cancel.h>
48 #include <mgLib/mdwfocus.h>
50 /*----- Main code ---------------------------------------------------------*/
54 * Arguments: @GtkWidget *w@ = widget raising the signal
55 * @gpointer *p@ = pointer to integer result code
59 * Use: Sets the result code to zero (failure) and ends the loop.
62 static void quit(GtkWidget
*w
, gpointer
*p
)
71 * Arguments: @GtkWidget *w@ = widget raising the signal
72 * @gpointer *p@ = pointer to integer result code
76 * Use: Sets the result code nonzero (success) and ends the loop.
79 static void done(GtkWidget
*w
, gpointer
*p
)
86 /* --- @version@ --- *
88 * Arguments: @FILE *fp@ = output stream to print the message on
92 * Use: Spits out a version message.
95 static void version(FILE *fp
)
97 fprintf(fp
, "%s (xtoys version " VERSION
")\n", QUIS
);
102 * Arguments: @FILE *fp@ = output stream to print the message on
106 * Use: Spits out a usage message.
109 static void usage(FILE *fp
)
112 "Usage: %s [-in] [-t title] [-p prompt] [-d default]\n"
113 "\t[-l|-H file] [-m max]\n",
119 * Arguments: @int argc@ = number of command line arguments
120 * @char *argv[]@ = addresses of arguments
122 * Returns: Zero if OK, and we read a string; nonzero if the user
125 * Use: Reads a string from the user, and returns it on standard
129 int main(int argc
, char *argv
[])
131 /* --- Configuration variables --- */
135 char *title
= "Input request";
140 const char *list
= 0;
147 #define f_nochoice 8u
149 /* --- User interface bits --- */
156 /* --- Crank up the toolkit --- *
158 * Have to do this here: GTK snarfs some command line options which my
159 * parser would barf about.
163 gtk_init(&argc
, &argv
);
165 /* --- Parse options from command line --- */
169 /* --- Long options structure --- */
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' },
188 /* --- Fetch an option --- */
190 i
= getopt_long(argc
, argv
, "huv t:p:d:i H:l:m:n", opt
, 0);
194 /* --- Work out what to do with it --- */
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\
206 Options available are:\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\
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\
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",
257 histmax
= atoi(optarg
);
271 if ((f
& f_invis
) && list
) {
273 "invisible entry is dumb if you provide a list of alternatives!");
276 if ((f
& f_nochoice
) && !list
)
277 die(EXIT_FAILURE
, "nothing to restrict choice to!");
279 /* --- Create the main window --- */
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
);
287 /* --- Create the box for laying out the widgets inside --- */
289 left
= (prompt ?
1 : 0);
290 box
= gtk_table_new(left
+ 2, 1, 0);
292 /* --- Maybe create a prompt widget --- */
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);
301 /* --- Create the entry widget --- */
304 FILE *fp
= fopen(list
, "r");
307 /* --- Read the items in from the file --- *
309 * Inability to open the file is not a disaster.
315 while (dstr_putline(&d
, fp
) != EOF
) {
316 hist
= g_list_append(hist
, xstrdup(d
.buf
));
323 /* --- Now create a combo box --- */
325 combo
= gtk_combo_new();
326 entry
= GTK_COMBO(combo
)->entry
;
328 gtk_combo_set_popdown_strings(GTK_COMBO(combo
), hist
);
330 /* --- Do other configuring --- */
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);
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
: "");
342 gtk_entry_set_text(GTK_ENTRY(entry
), dfl
);
344 /* --- Set the widget in the right place and show it --- */
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
);
351 entry
= gtk_entry_new();
352 gtk_entry_set_text(GTK_ENTRY(entry
), dfl
);
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
);
361 /* --- Create the default action widget --- */
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
);
369 /* --- Add the box into the main window --- */
371 gtk_container_add(GTK_CONTAINER(win
), box
);
372 gtk_widget_show(box
);
374 /* --- Last minute configuration things --- */
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
),
382 cancel(GTK_WINDOW(win
), 0);
384 /* --- Go go go --- */
386 gtk_widget_realize(win
);
388 gtk_widget_grab_focus(entry
);
389 gtk_widget_show(win
);
392 /* --- Output the result --- */
395 char *p
= gtk_entry_get_text(GTK_ENTRY(entry
));
397 /* --- If history is enabled, output a new history file --- *
399 * If the first entry was accepted verbatim, or if the entry is a blank
400 * line, don't bother.
403 if (f
& f_history
&& *p
&& !(hist
&& strcmp(p
, hist
->data
) == 0)) {
409 if ((fd
= open(list
, O_WRONLY
| O_CREAT
| O_TRUNC
, 0600)) < 0)
411 if ((fp
= fdopen(fd
, "w")) == 0) {
419 for (i
= 1, g
= hist
; (histmax
< 1 || i
< histmax
) && g
; g
= g
->next
) {
420 if (*(char *)g
->data
&& strcmp(g
->data
, p
) != 0) {
430 /* --- Print the result and go away --- */
435 return (ok ? EXIT_SUCCESS
: EXIT_FAILURE
);
438 /*----- That's all, folks -------------------------------------------------*/