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