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