90b2c5d4 |
1 | /* -*-c-*- |
2 | * |
d0639ab5 |
3 | * $Id: xgetline.c,v 1.11 1999/08/20 07:30:33 mdw Exp $ |
90b2c5d4 |
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 | /*----- Revision history --------------------------------------------------* |
30 | * |
31 | * $Log: xgetline.c,v $ |
d0639ab5 |
32 | * Revision 1.11 1999/08/20 07:30:33 mdw |
33 | * Miscellaneous changes, mostly concerning options parsing. |
34 | * |
e34817d8 |
35 | * Revision 1.10 1999/05/21 22:09:19 mdw |
36 | * Take advantage of new dynamic string macros. |
37 | * |
b907fbe4 |
38 | * Revision 1.9 1999/05/05 18:54:37 mdw |
39 | * Keep blank lines out of the history list. |
40 | * |
cb171cd0 |
41 | * Revision 1.8 1998/12/16 19:58:53 mdw |
42 | * Stop the dropdown list from dropping down when you press enter. |
43 | * |
715c9c39 |
44 | * Revision 1.7 1998/12/11 09:53:02 mdw |
45 | * Updates for mLib/mgLib. Support history files for recalling past |
46 | * entries, using a drop-down list. |
47 | * |
208b901e |
48 | * Revision 1.6 1998/12/03 00:56:29 mdw |
49 | * Set focus on the entry field, rather than leaving things to luck. |
50 | * |
7347b0d9 |
51 | * Revision 1.5 1998/12/03 00:39:44 mdw |
52 | * Force focus when starting up. |
53 | * |
d6130abd |
54 | * Revision 1.4 1998/11/30 22:36:47 mdw |
55 | * Tidy up tabbing in help texts very slightly. |
56 | * |
f3b35b6b |
57 | * Revision 1.3 1998/11/21 22:30:20 mdw |
58 | * Support GNU-style long options throughout, and introduce proper help |
59 | * text to all programs. Update manual pages to match. |
60 | * |
881bb211 |
61 | * Revision 1.2 1998/11/18 21:25:30 mdw |
62 | * Remove bogus `-h' option from the options list. |
63 | * |
90b2c5d4 |
64 | * Revision 1.1 1998/11/16 23:00:49 mdw |
65 | * Initial versions. |
66 | * |
67 | */ |
68 | |
69 | /*----- Header files ------------------------------------------------------*/ |
70 | |
71 | #include <stdio.h> |
72 | #include <stdlib.h> |
73 | #include <string.h> |
74 | |
715c9c39 |
75 | #include <fcntl.h> |
76 | #include <unistd.h> |
77 | |
90b2c5d4 |
78 | #include <gtk/gtk.h> |
79 | #include <gdk/gdkkeysyms.h> |
80 | |
715c9c39 |
81 | #include <mLib/alloc.h> |
82 | #include <mLib/dstr.h> |
83 | #include <mLib/mdwopt.h> |
84 | #include <mLib/report.h> |
85 | #include <mLib/quis.h> |
86 | |
87 | #include <mgLib/cancel.h> |
88 | #include <mgLib/mdwfocus.h> |
90b2c5d4 |
89 | |
90 | /*----- Main code ---------------------------------------------------------*/ |
91 | |
715c9c39 |
92 | /* --- @quit@ --- * |
90b2c5d4 |
93 | * |
94 | * Arguments: @GtkWidget *w@ = widget raising the signal |
95 | * @gpointer *p@ = pointer to integer result code |
96 | * |
97 | * Returns: --- |
98 | * |
99 | * Use: Sets the result code to zero (failure) and ends the loop. |
100 | */ |
101 | |
715c9c39 |
102 | static void quit(GtkWidget *w, gpointer *p) |
90b2c5d4 |
103 | { |
104 | int *ip = (int *)p; |
105 | *ip = 0; |
106 | gtk_main_quit(); |
107 | } |
108 | |
109 | /* --- @done@ --- * |
110 | * |
111 | * Arguments: @GtkWidget *w@ = widget raising the signal |
112 | * @gpointer *p@ = pointer to integer result code |
113 | * |
114 | * Returns: --- |
115 | * |
116 | * Use: Sets the result code nonzero (success) and ends the loop. |
117 | */ |
118 | |
119 | static void done(GtkWidget *w, gpointer *p) |
120 | { |
121 | int *ip = (int *)p; |
122 | *ip = 1; |
123 | gtk_main_quit(); |
124 | } |
125 | |
f3b35b6b |
126 | /* --- @version@ --- * |
127 | * |
128 | * Arguments: @FILE *fp@ = output stream to print the message on |
129 | * |
130 | * Returns: --- |
131 | * |
132 | * Use: Spits out a version message. |
133 | */ |
134 | |
135 | static void version(FILE *fp) |
136 | { |
137 | fprintf(fp, "%s (xtoys version " VERSION ")\n", QUIS); |
138 | } |
139 | |
140 | /* --- @usage@ --- * |
141 | * |
142 | * Arguments: @FILE *fp@ = output stream to print the message on |
143 | * |
144 | * Returns: --- |
145 | * |
146 | * Use: Spits out a usage message. |
147 | */ |
148 | |
149 | static void usage(FILE *fp) |
150 | { |
715c9c39 |
151 | fprintf(fp, |
152 | "Usage: %s [-in] [-t title] [-p prompt] [-d default]\n" |
153 | "\t[-l|-H file] [-m max]\n", |
154 | QUIS); |
f3b35b6b |
155 | } |
156 | |
90b2c5d4 |
157 | /* --- @main@ --- * |
158 | * |
159 | * Arguments: @int argc@ = number of command line arguments |
160 | * @char *argv[]@ = addresses of arguments |
161 | * |
162 | * Returns: Zero if OK, and we read a string; nonzero if the user |
163 | * cancelled. |
164 | * |
165 | * Use: Reads a string from the user, and returns it on standard |
166 | * output. |
167 | */ |
168 | |
169 | int main(int argc, char *argv[]) |
170 | { |
171 | /* --- Configuration variables --- */ |
172 | |
173 | char *prompt = 0; |
174 | char *dfl = ""; |
175 | char *title = "Input request"; |
176 | int left; |
177 | unsigned f = 0; |
178 | int ok = 0; |
179 | |
715c9c39 |
180 | const char *list = 0; |
181 | int histmax = 20; |
182 | GList *hist; |
183 | |
90b2c5d4 |
184 | enum { |
185 | f_invis = 1, |
715c9c39 |
186 | f_duff = 2, |
187 | f_history = 4, |
188 | f_nochoice = 8 |
90b2c5d4 |
189 | }; |
190 | |
191 | /* --- User interface bits --- */ |
192 | |
193 | GtkWidget *win; |
194 | GtkWidget *box; |
195 | GtkWidget *entry; |
196 | GtkWidget *btn; |
197 | |
198 | /* --- Crank up the toolkit --- * |
199 | * |
200 | * Have to do this here: GTK snarfs some command line options which my |
201 | * parser would barf about. |
202 | */ |
203 | |
f3b35b6b |
204 | ego(argv[0]); |
90b2c5d4 |
205 | gtk_init(&argc, &argv); |
206 | |
207 | /* --- Parse options from command line --- */ |
208 | |
209 | for (;;) { |
210 | |
211 | /* --- Long options structure --- */ |
212 | |
213 | static struct option opt[] = { |
d0639ab5 |
214 | { "help", 0, 0, 'h' }, |
215 | { "usage", 0, 0, 'u' }, |
216 | { "version", 0, 0, 'v' }, |
217 | { "title", OPTF_ARGREQ, 0, 't' }, |
218 | { "prompt", OPTF_ARGREQ, 0, 'p' }, |
219 | { "default", OPTF_ARGREQ, 0, 'd' }, |
220 | { "password", 0, 0, 'i' }, |
221 | { "invisible", 0, 0, 'i' }, |
222 | { "history", OPTF_ARGREQ, 0, 'H' }, |
223 | { "list", OPTF_ARGREQ, 0, 'l' }, |
224 | { "histmax", OPTF_ARGREQ, 0, 'm' }, |
225 | { "no-choice", 0, 0, 'n' }, |
226 | { 0, 0, 0, 0 } |
90b2c5d4 |
227 | }; |
228 | int i; |
229 | |
230 | /* --- Fetch an option --- */ |
231 | |
715c9c39 |
232 | i = getopt_long(argc, argv, "huv t:p:d:i H:l:m:n", opt, 0); |
90b2c5d4 |
233 | if (i < 0) |
234 | break; |
235 | |
236 | /* --- Work out what to do with it --- */ |
237 | |
238 | switch (i) { |
f3b35b6b |
239 | case 'h': |
240 | version(stdout); |
241 | fputs("\n", stdout); |
242 | usage(stdout); |
d0639ab5 |
243 | fputs("\ |
244 | \n\ |
245 | Pops up a small window requesting input from a user, and echoes the\n\ |
246 | response to stdout, where it can be collected by a shell script.\n\ |
247 | \n\ |
248 | Options available are:\n\ |
249 | \n\ |
250 | -h, --help Display this help text\n\ |
251 | -u, --usage Display a short usage summary\n\ |
252 | -v, --version Display the program's version number\n\ |
253 | \n\ |
254 | -i, --invisible Don't show the user's string as it's typed\n\ |
255 | -t, --title=TITLE Set the window's title string\n\ |
256 | -p, --prompt=PROMPT Set the window's prompt string\n\ |
257 | -d, --default=DEFAULT Set the default string already in the window\n\ |
258 | \n\ |
259 | -l, --list=FILE Read FILE into a drop-down list\n\ |
260 | -n, --no-choice No free text input: must choose item from list\n\ |
261 | -H, --history=FILE As for `--list', but update with new string\n\ |
262 | -m, --histmax=MAX Maximum number of items written back to file\n", |
f3b35b6b |
263 | stdout); |
264 | exit(0); |
265 | break; |
266 | case 'u': |
267 | usage(stdout); |
268 | exit(0); |
269 | break; |
270 | case 'v': |
271 | version(stdout); |
272 | exit(0); |
273 | break; |
715c9c39 |
274 | |
90b2c5d4 |
275 | case 't': |
276 | title = optarg; |
277 | break; |
278 | case 'p': |
279 | prompt = optarg; |
280 | break; |
281 | case 'd': |
282 | dfl = optarg; |
283 | break; |
284 | case 'i': |
285 | f |= f_invis; |
286 | break; |
715c9c39 |
287 | |
288 | case 'l': |
289 | list = optarg; |
290 | break; |
291 | case 'n': |
292 | f |= f_nochoice; |
293 | break; |
294 | case 'H': |
295 | f |= f_history; |
296 | list = optarg; |
297 | break; |
298 | case 'm': |
299 | histmax = atoi(optarg); |
300 | break; |
301 | |
90b2c5d4 |
302 | default: |
303 | f |= f_duff; |
304 | break; |
305 | } |
306 | } |
307 | |
308 | if (f & f_duff) { |
f3b35b6b |
309 | usage(stderr); |
90b2c5d4 |
310 | exit(EXIT_FAILURE); |
311 | } |
312 | |
715c9c39 |
313 | if ((f & f_invis) && list) { |
314 | die(EXIT_FAILURE, |
315 | "invisible entry is dumb if you provide a list of alternatives!"); |
316 | } |
317 | |
318 | if ((f & f_nochoice) && !list) |
319 | die(EXIT_FAILURE, "nothing to restrict choice to!"); |
320 | |
90b2c5d4 |
321 | /* --- Create the main window --- */ |
322 | |
323 | win = gtk_window_new(GTK_WINDOW_TOPLEVEL); |
324 | gtk_window_set_title(GTK_WINDOW(win), title); |
325 | gtk_window_position(GTK_WINDOW(win), GTK_WIN_POS_MOUSE); |
326 | gtk_signal_connect(GTK_OBJECT(win), "destroy", |
715c9c39 |
327 | GTK_SIGNAL_FUNC(quit), &ok); |
90b2c5d4 |
328 | |
329 | /* --- Create the box for laying out the widgets inside --- */ |
330 | |
331 | left = (prompt ? 1 : 0); |
332 | box = gtk_table_new(left + 2, 1, 0); |
333 | |
334 | /* --- Maybe create a prompt widget --- */ |
335 | |
336 | if (prompt) { |
337 | GtkWidget *w = gtk_label_new(prompt); |
338 | gtk_table_attach(GTK_TABLE(box), w, |
339 | 0, 1, 0, 1, 0, GTK_EXPAND, 4, 2); |
340 | gtk_widget_show(w); |
341 | } |
342 | |
343 | /* --- Create the entry widget --- */ |
344 | |
715c9c39 |
345 | if (list) { |
346 | FILE *fp = fopen(list, "r"); |
347 | GtkWidget *combo; |
348 | |
349 | /* --- Read the items in from the file --- * |
350 | * |
351 | * Inability to open the file is not a disaster. |
352 | */ |
353 | |
354 | hist = 0; |
355 | if (fp) { |
e34817d8 |
356 | dstr d = DSTR_INIT; |
715c9c39 |
357 | |
715c9c39 |
358 | while (dstr_putline(&d, fp) != EOF) { |
359 | hist = g_list_append(hist, xstrdup(d.buf)); |
d0639ab5 |
360 | DRESET(&d); |
715c9c39 |
361 | } |
d0639ab5 |
362 | dstr_destroy(&d); |
715c9c39 |
363 | fclose(fp); |
364 | } |
365 | |
366 | /* --- Now create a combo box --- */ |
367 | |
368 | combo = gtk_combo_new(); |
369 | entry = GTK_COMBO(combo)->entry; |
370 | if (hist) |
371 | gtk_combo_set_popdown_strings(GTK_COMBO(combo), hist); |
372 | |
373 | /* --- Do other configuring --- */ |
374 | |
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); |
378 | } |
379 | gtk_combo_set_case_sensitive(GTK_COMBO(combo), 1); |
380 | gtk_combo_set_use_arrows_always(GTK_COMBO(combo), 1); |
cb171cd0 |
381 | gtk_combo_disable_activate(GTK_COMBO(combo)); |
715c9c39 |
382 | if (strcmp(dfl, "@") == 0) |
383 | gtk_entry_set_text(GTK_ENTRY(entry), hist ? (char *)hist->data : ""); |
384 | else |
385 | gtk_entry_set_text(GTK_ENTRY(entry), dfl); |
386 | |
387 | /* --- Set the widget in the right place and show it --- */ |
388 | |
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); |
393 | } else { |
394 | entry = gtk_entry_new(); |
395 | gtk_entry_set_text(GTK_ENTRY(entry), dfl); |
396 | if (f & f_invis) |
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); |
402 | } |
90b2c5d4 |
403 | |
404 | /* --- Create the default action widget --- */ |
405 | |
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); |
411 | |
412 | /* --- Add the box into the main window --- */ |
413 | |
414 | gtk_container_add(GTK_CONTAINER(win), box); |
415 | gtk_widget_show(box); |
416 | |
417 | /* --- Last minute configuration things --- */ |
418 | |
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), |
424 | GTK_OBJECT(btn)); |
715c9c39 |
425 | cancel(GTK_WINDOW(win), 0); |
90b2c5d4 |
426 | |
427 | /* --- Go go go --- */ |
428 | |
7347b0d9 |
429 | gtk_widget_realize(win); |
430 | mdwfocus(win); |
208b901e |
431 | gtk_widget_grab_focus(entry); |
90b2c5d4 |
432 | gtk_widget_show(win); |
433 | gtk_main(); |
434 | |
435 | /* --- Output the result --- */ |
436 | |
437 | if (ok) { |
438 | char *p = gtk_entry_get_text(GTK_ENTRY(entry)); |
715c9c39 |
439 | |
440 | /* --- If history is enabled, output a new history file --- * |
441 | * |
b907fbe4 |
442 | * If the first entry was accepted verbatim, or if the entry is a blank |
443 | * line, don't bother. |
715c9c39 |
444 | */ |
445 | |
b907fbe4 |
446 | if (f & f_history && *p && !(hist && strcmp(p, hist->data) == 0)) { |
715c9c39 |
447 | int fd; |
448 | FILE *fp; |
449 | int i; |
450 | GList *g; |
451 | |
452 | if ((fd = open(list, O_WRONLY | O_CREAT | O_TRUNC, 0600)) < 0) |
453 | goto fail; |
454 | if ((fp = fdopen(fd, "w")) == 0) { |
455 | close(fd); |
456 | goto fail; |
457 | } |
458 | |
459 | fputs(p, fp); |
460 | fputc('\n', fp); |
461 | |
462 | for (i = 1, g = hist; (histmax < 1 || i < histmax) && g; g = g->next) { |
b907fbe4 |
463 | if (*(char *)g->data && strcmp(g->data, p) != 0) { |
715c9c39 |
464 | fputs(g->data, fp); |
465 | fputc('\n', fp); |
466 | i++; |
467 | } |
468 | } |
469 | fclose(fp); |
470 | fail:; |
471 | } |
472 | |
473 | /* --- Print the result and go away --- */ |
474 | |
90b2c5d4 |
475 | puts(p); |
476 | } |
477 | |
478 | return (ok ? EXIT_SUCCESS : EXIT_FAILURE); |
479 | } |
480 | |
481 | /*----- That's all, folks -------------------------------------------------*/ |