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