xref: /dragonfly/contrib/dialog/dialog.c (revision f9993810)
1 /*
2  * $Id: dialog.c,v 1.289 2022/07/28 21:11:36 tom Exp $
3  *
4  *  cdialog - Display simple dialog boxes from shell scripts
5  *
6  *  Copyright 2000-2021,2022	Thomas E. Dickey
7  *
8  *  This program is free software; you can redistribute it and/or modify
9  *  it under the terms of the GNU Lesser General Public License, version 2.1
10  *  as published by the Free Software Foundation.
11  *
12  *  This program is distributed in the hope that it will be useful, but
13  *  WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  *  Lesser General Public License for more details.
16  *
17  *  You should have received a copy of the GNU Lesser General Public
18  *  License along with this program; if not, write to
19  *	Free Software Foundation, Inc.
20  *	51 Franklin St., Fifth Floor
21  *	Boston, MA 02110, USA.
22  *
23  *  An earlier version of this program lists as authors
24  *	Savio Lam (lam836@cs.cuhk.hk)
25  */
26 
27 #include <dlg_internals.h>
28 
29 #define USEMAX_ARGS	20
30 #define USEMAX_FILE	50
31 
32 #define PASSARGS             t,       av,        offset_add
33 #define CALLARGS const char *t, char *av[], int *offset_add
34 typedef int (callerFn) (CALLARGS);
35 
36 typedef enum {
37     o_unknown = 0
38     ,o_allow_close
39     ,o_and_widget
40     ,o_ascii_lines
41     ,o_aspect_ratio
42     ,o_auto_placement
43     ,o_backtitle
44     ,o_beep_signal
45     ,o_beep_after_signal
46     ,o_begin_set
47     ,o_cancel_label
48     ,o_checklist
49     ,o_dlg_clear_screen
50     ,o_colors
51     ,o_column_separator
52     ,o_cr_wrap
53     ,o_create_rc
54     ,o_cursor_off_label
55     ,o_date_format
56     ,o_default_button
57     ,o_default_item
58     ,o_defaultno
59     ,o_erase_on_exit
60     ,o_exit_label
61     ,o_extra_button
62     ,o_extra_label
63     ,o_fixed_font
64     ,o_form
65     ,o_gauge
66     ,o_help
67     ,o_help_button
68     ,o_help_file
69     ,o_help_label
70     ,o_help_line
71     ,o_help_status
72     ,o_help_tags
73     ,o_icon
74     ,o_ignore
75     ,o_infobox
76     ,o_input_fd
77     ,o_inputbox
78     ,o_inputmenu
79     ,o_insecure
80     ,o_item_help
81     ,o_keep_colors
82     ,o_keep_tite
83     ,o_keep_window
84     ,o_last_key
85     ,o_max_input
86     ,o_menu
87     ,o_mixedform
88     ,o_mixedgauge
89     ,o_msgbox
90     ,o_no_close
91     ,o_nocollapse
92     ,o_no_cr_wrap
93     ,o_cant_kill
94     ,o_no_hot_list
95     ,o_no_label
96     ,o_no_lines
97     ,o_no_mouse
98     ,o_no_nl_expand
99     ,o_use_shadow
100     ,o_nocancel
101     ,o_nook
102     ,o_ok_label
103     ,o_output_fd
104     ,o_output_separator
105     ,o_passwordbox
106     ,o_passwordform
107     ,o_pause
108     ,o_prgbox
109     ,o_print_maxsize
110     ,o_print_siz
111     ,o_text_only
112     ,o_print_text_size
113     ,o_print_version
114     ,o_programbox
115     ,o_progressbox
116     ,o_quoted
117     ,o_radiolist
118     ,o_screen_center
119     ,o_use_scrollbar
120     ,o_separate_output
121     ,o_separate_str
122     ,o_single_quoted
123     ,o_size_err
124     ,o_sleep_secs
125     ,o_smooth
126     ,o_output_stderr
127     ,o_output_stdout
128     ,o_tab_correct
129     ,o_tab_len
130     ,o_tailbox
131     ,o_tailboxbg
132     ,o_textbox
133     ,o_time_format
134     ,o_timeout_secs
135     ,o_title
136     ,o_trim_whitespace
137     ,o_under_mouse
138     ,o_version
139     ,o_visit_items
140     ,o_wmclass
141     ,o_yes_label
142     ,o_yesno
143 #ifdef HAVE_WHIPTAIL
144     ,o_fullbutton
145     ,o_topleft
146 #endif
147 #ifdef HAVE_XDIALOG
148     ,o_calendar
149     ,o_dselect
150     ,o_editbox
151     ,o_fselect
152     ,o_timebox
153     ,o_week_start
154 #endif
155 #ifdef HAVE_XDIALOG2
156     ,o_buildlist
157     ,o_rangebox
158     ,o_reorder
159     ,o_treeview
160 #endif
161 #if defined(HAVE_XDIALOG2) || defined(HAVE_WHIPTAIL)
162     ,o_no_items
163     ,o_no_tags
164 #endif
165 #ifdef HAVE_DLG_TRACE
166     ,o_trace
167 #endif
168     ,o_iso_week
169 } eOptions;
170 
171 typedef enum {
172     tUnknown
173     ,tFalse
174     ,tTrue
175     ,tNumber
176     ,tString
177 } tOptions;
178 
179 /*
180  * The bits in 'pass' are used to decide which options are applicable at
181  * different stages in the program:
182  *	1 flags before widgets
183  *	2 widgets
184  *	4 non-widget options
185  */
186 typedef struct {
187     const char *name;
188     eOptions code;
189     int vars;			/* 0=none, 1=state, 2=vars */
190     tOptions type;		/* type for bool(true/false), number, string */
191     unsigned offset;
192     int pass;			/* 1,2,4 or combination */
193     const char *help;		/* NULL to suppress, non-empty to display params */
194 } Options;
195 
196 typedef struct {
197     eOptions code;
198     int argmin, argmax;
199     callerFn *jumper;
200 } Mode;
201 
202 /* use these macros for simple options in DIALOG_STATE */
203 #define ssF(name) o_##name, 1, tFalse,  offsetof(DIALOG_STATE,name)
204 #define ssT(name) o_##name, 1, tTrue,   offsetof(DIALOG_STATE,name)
205 #define ssN(name) o_##name, 1, tNumber, offsetof(DIALOG_STATE,name)
206 #define ssS(name) o_##name, 1, tString, offsetof(DIALOG_STATE,name)
207 
208 /* use these macros for simple options in DIALOG_VARS */
209 #define svF(name) o_##name, 2, tFalse,  offsetof(DIALOG_VARS,name)
210 #define svT(name) o_##name, 2, tTrue,   offsetof(DIALOG_VARS,name)
211 #define svN(name) o_##name, 2, tNumber, offsetof(DIALOG_VARS,name)
212 #define svS(name) o_##name, 2, tString, offsetof(DIALOG_VARS,name)
213 
214 /* use these macros for ignored options */
215 #define xxF(name) o_##name, 0, tFalse,  0
216 #define xxT(name) o_##name, 0, tTrue,   0
217 #define xxN(name) o_##name, 0, tNumber, 0
218 #define xxS(name) o_##name, 0, tString, 0
219 
220 /* use this macro for widget options */
221 #define opW(name) o_##name, 0, 0, 0
222 
223 /* use this macro for other options */
224 #define opO(name) o_##name, 0, 0, 0
225 
226 static int known_opts = 0;
227 static const char **dialog_opts;
228 static char **dialog_argv;
229 
230 static char **special_argv = 0;
231 static int special_argc = 0;
232 
233 static bool ignore_unknown = FALSE;
234 
235 static const char *program = "dialog";
236 
237 #ifdef NO_LEAKS
238 typedef struct _all_blobs {
239     struct _all_blobs *next;
240     void *blob;
241 } AllBlobs;
242 
243 static AllBlobs *all_blobs;
244 #endif
245 
246 /*
247  * The options[] table is organized this way to make it simple to maintain
248  * a sorted list of options for the help-message.
249  *
250  * Because Boolean options correspond to "true", --shadow is listed here while
251  * --no-shadow is not.  The findOption and optionBool functions handle the
252  * cases where "no" is added or removed from the option name to derive an
253  * opposite setting.
254  */
255 /* *INDENT-OFF* */
256 static const Options options[] = {
257     { "allow-close",	xxT(allow_close),	1, NULL },
258     { "and-widget",	opO(and_widget),	4, NULL },
259     { "ascii-lines",	svT(ascii_lines), 	1, "" },
260     { "aspect",		ssN(aspect_ratio),	1, "<ratio>" },
261     { "auto-placement", xxT(auto_placement),	1, NULL },
262     { "backtitle",	svS(backtitle),		1, "<backtitle>" },
263     { "beep",		svT(beep_signal),	1, "" },
264     { "beep-after",	svT(beep_after_signal),	1, "" },
265     { "begin",		svT(begin_set),		1, "<y> <x>" },
266     { "cancel-label",	svS(cancel_label),	1, "<str>" },
267     { "checklist",	opW(checklist),		2, "<text> <height> <width> <list height> <tag1> <item1> <status1>..." },
268     { "clear",		svT(dlg_clear_screen),	1, "" },
269     { "colors",		svT(colors),		1, "" },
270     { "column-separator",svS(column_separator),	1, "<str>" },
271     { "cr-wrap",	svT(cr_wrap),		1, "" },
272     { "create-rc",	opO(create_rc),		1, NULL },
273     { "cursor-off-label",svT(cursor_off_label),	1, "" },
274     { "date-format",	svS(date_format),	1, "<str>" },
275     { "default-button",	xxS(default_button),	1, "<str>" },
276     { "default-item",	svS(default_item),	1, "<str>" },
277     { "defaultno",	svT(defaultno),		1, "" },
278     { "erase-on-exit",	svT(erase_on_exit),	1, "" },
279     { "exit-label",	svS(exit_label),	1, "<str>" },
280     { "extra-button",	svT(extra_button),	1, "" },
281     { "extra-label",	svS(extra_label),	1, "<str>" },
282     { "fixed-font",	xxT(fixed_font),	1, NULL },
283     { "form",		opW(form),		2, "<text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1>..." },
284     { "gauge",		opW(gauge),		2, "<text> <height> <width> [<percent>]" },
285     { "guage",		opW(gauge),		2, NULL },
286     { "help",		opO(help),		4, "" },
287     { "help-button",	svT(help_button),	1, "" },
288     { "help-label",	svS(help_label),	1, "<str>" },
289     { "help-status",	svT(help_status),	1, "" },
290     { "help-tags",	svT(help_tags),		1, "" },
291     { "hfile",		svS(help_file),		1, "<str>" },
292     { "hline",		svS(help_line),		1, "<str>" },
293     { "icon",		xxS(icon),		1, NULL },
294     { "ignore",		opO(ignore),		1, "" },
295     { "infobox",	opW(infobox),		2, "<text> <height> <width>" },
296     { "input-fd",	opO(input_fd),		1, "<fd>" },
297     { "inputbox",	opW(inputbox),		2, "<text> <height> <width> [<init>]" },
298     { "inputmenu",	opW(inputmenu),		2, "<text> <height> <width> <menu height> <tag1> <item1>..." },
299     { "insecure",	svT(insecure),		1, "" },
300     { "item-help",	svT(item_help),		1, "" },
301     { "keep-colors",	xxT(keep_colors),	1, NULL },
302     { "keep-tite",	svT(keep_tite),		1, "" },
303     { "keep-window",	svT(keep_window),	1, "" },
304     { "last-key",	svT(last_key),		1, "" },
305     { "max-input",	svN(max_input),		1, "<n>" },
306     { "menu",		opW(menu),		2, "<text> <height> <width> <menu height> <tag1> <item1>..." },
307     { "mixedform",	opW(mixedform),		2, "<text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1> <itype>..." },
308     { "mixedgauge",	opW(mixedgauge),	2, "<text> <height> <width> <percent> <tag1> <item1>..." },
309     { "msgbox",		opW(msgbox),		2, "<text> <height> <width>" },
310     { "no-cancel",	svT(nocancel),		1, "" },
311     { "no-close",	xxT(no_close),		1, NULL },
312     { "no-collapse",	svT(nocollapse),	1, "" },
313     { "no-hot-list",	svT(no_hot_list),	1, "" },
314     { "no-kill",	svT(cant_kill),		1, "" },
315     { "no-label",	svS(no_label),		1, "<str>" },
316     { "no-lines",	svT(no_lines), 		1, "" },
317     { "no-mouse",	ssT(no_mouse),		1, "" },
318     { "no-nl-expand",	svT(no_nl_expand),	1, "" },
319     { "no-ok",		svT(nook),		1, "" },
320     { "no-shadow",	ssF(use_shadow),	1, "" },
321     { "ok-label",	svS(ok_label),		1, "<str>" },
322     { "output-fd",	opO(output_fd),		1, "<fd>" },
323     { "output-separator",svS(output_separator),	1, "<str>" },
324     { "passwordbox",	opW(passwordbox),	2, "<text> <height> <width> [<init>]" },
325     { "passwordform",	opW(passwordform),	2, "<text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1>..." },
326     { "pause",		opW(pause),		2, "<text> <height> <width> <seconds>" },
327     { "prgbox",		opW(prgbox),		2, "<text> <command> <height> <width>" },
328     { "print-maxsize",	opO(print_maxsize),	1, "" },
329     { "print-size",	svT(print_siz),		1, "" },
330     { "print-text-only",ssT(text_only),		5, "<text> <height> <width>" },
331     { "print-text-size",opO(print_text_size),	5, "<text> <height> <width>" },
332     { "print-version",	opO(print_version),	5, "" },
333     { "programbox",	opW(programbox),	2, "<text> <height> <width>" },
334     { "progressbox",	opW(progressbox),	2, "<text> <height> <width>" },
335     { "quoted",		svT(quoted),		1, "" },
336     { "radiolist",	opW(radiolist),		2, "<text> <height> <width> <list height> <tag1> <item1> <status1>..." },
337     { "screen-center",	xxT(screen_center),	1, NULL },
338     { "scrollbar",	ssT(use_scrollbar),	1, "" },
339     { "separate-output",svT(separate_output),	1, "" },
340     { "separate-widget",ssS(separate_str),	1, "<str>" },
341     { "separator",	svS(output_separator),	1, NULL },
342     { "single-quoted",	svT(single_quoted),	1, "" },
343     { "size-err",	svT(size_err),		1, "" },
344     { "sleep",		svN(sleep_secs),	1, "<secs>" },
345     { "smooth",		xxT(smooth),		1, NULL },
346     { "stderr",		opO(output_stderr),	1, "" },
347     { "stdout",		opO(output_stdout),	1, "" },
348     { "tab-correct",	svT(tab_correct),	1, "" },
349     { "tab-len",	ssN(tab_len),		1, "<n>" },
350     { "tailbox",	opW(tailbox),		2, "<file> <height> <width>" },
351     { "tailboxbg",	opW(tailboxbg),		2, "<file> <height> <width>" },
352     { "textbox",	opW(textbox),		2, "<file> <height> <width>" },
353     { "time-format",	svS(time_format),	1, "<str>" },
354     { "timeout",	svN(timeout_secs),	1, "<secs>" },
355     { "title",		svS(title),		1, "<title>" },
356     { "trim",		svT(trim_whitespace),	1, "" },
357     { "under-mouse", 	xxT(under_mouse),	1, NULL },
358     { "version",	opO(version),		5, "" },
359     { "visit-items", 	ssT(visit_items),	1, "" },
360     { "wmclass",	xxS(wmclass),		1, NULL },
361     { "yes-label",	svS(yes_label),		1, "<str>" },
362     { "yesno",		opW(yesno),		2, "<text> <height> <width>" },
363 #ifdef HAVE_WHIPTAIL
364     { "cancel-button",	svS(cancel_label),	1, NULL },
365     { "fb",		xxT(fullbutton),	1, NULL },
366     { "fullbutton",	xxT(fullbutton),	1, NULL },
367     { "no-button",	svS(no_label),		1, NULL },
368     { "ok-button",	svS(ok_label),		1, NULL },
369     { "scrolltext",	ssT(use_scrollbar),	1, NULL },
370     { "topleft",	svT(begin_set),		1, NULL },
371     { "yes-button",	svS(yes_label),		1, NULL },
372 #endif
373 #ifdef HAVE_XDIALOG
374     { "calendar",	opW(calendar),		2, "<text> <height> <width> <day> <month> <year>" },
375     { "dselect",	opW(dselect),		2, "<directory> <height> <width>" },
376     { "editbox",	opW(editbox),		2, "<file> <height> <width>" },
377     { "fselect",	opW(fselect),		2, "<filepath> <height> <width>" },
378     { "timebox",	opW(timebox),		2, "<text> <height> <width> <hour> <minute> <second>" },
379     { "week-start",	svS(week_start),	1, "<str>" },
380     { "iso-week",	svT(iso_week),		1, NULL },
381 #endif
382 #ifdef HAVE_XDIALOG2
383     { "buildlist",	opW(buildlist),		2, "<text> <height> <width> <list-height> <tag1> <item1> <status1>..." },
384     { "no-items", 	svT(no_items),		1, "" },
385     { "no-tags", 	svT(no_tags),		1, "" },
386     { "rangebox",	opW(rangebox),		2, "<text> <height> <width> <min-value> <max-value> <default-value>" },
387     { "reorder", 	svT(reorder),		1, "" },
388     { "treeview",	opW(treeview),		2, "<text> <height> <width> <list-height> <tag1> <item1> <status1> <depth1>..." },
389 #endif
390 #if defined(HAVE_XDIALOG2) || defined(HAVE_WHIPTAIL)
391     { "noitem", 	svT(no_items),		1, NULL },
392     { "notags", 	svT(no_tags),		1, NULL },
393 #endif
394 #ifdef HAVE_DLG_TRACE
395     { "trace",		opO(trace),		1, "<file>" },
396 #endif
397 };
398 /* *INDENT-ON* */
399 
400 #ifdef NO_LEAKS
401 static void
402 ignore_leak(void *value)
403 {
404     AllBlobs *next = dlg_calloc(AllBlobs, (size_t) 1);
405     if (next != 0) {
406 	next->blob = value;
407 	next->next = all_blobs;
408 	all_blobs = next;
409     }
410 }
411 
412 static void
413 handle_leaks(void)
414 {
415     while (all_blobs != 0) {
416 	char *blob = all_blobs->blob;
417 	AllBlobs *next = all_blobs->next;
418 	free(blob);
419 	free(all_blobs);
420 	all_blobs = next;
421     }
422     free(dialog_opts);
423     if (special_argv != 0) {
424 	free(special_argv[0]);
425 	free(special_argv);
426 	special_argv = 0;
427 	special_argc = 0;
428     }
429 }
430 #else
431 #define handle_leaks()		/* nothing */
432 #define ignore_leak(n)		/* nothing */
433 #endif
434 
435 #define OptionChars "\
436 0123456789\
437 -\
438 abcdefghijklmnopqrstuvwxyz\
439 "
440 
441 /*
442  * Check if the given string from main's argv is an option.
443  */
444 static bool
445 isOption(const char *arg)
446 {
447     bool result = FALSE;
448 
449     if (arg != 0) {
450 	if (dialog_opts != 0) {
451 	    int n;
452 	    for (n = 0; dialog_opts[n] != 0; ++n) {
453 		if (dialog_opts[n] == arg) {
454 		    result = TRUE;
455 		    break;
456 		}
457 	    }
458 	} else if (!strncmp(arg, "--", (size_t) 2) && isalpha(UCH(arg[2]))) {
459 	    if (strlen(arg) == (strspn) (arg, OptionChars)) {
460 		result = TRUE;
461 	    } else {
462 		handle_leaks();
463 		dlg_exiterr(_("Invalid option \"%s\""), arg);
464 	    }
465 	}
466     }
467     return result;
468 }
469 
470 /*
471  * Make an array showing which argv[] entries are options.  Use "--" as a
472  * special token to escape the next argument, allowing it to begin with "--".
473  * When we find a "--" argument, also remove it from argv[] and adjust argc.
474  * That appears to be an undocumented feature of the popt library.
475  *
476  * Also, if we see a "--file", expand it into the parameter list by reading the
477  * text from the given file and stripping quotes, treating whitespace outside
478  * quotes as a parameter delimiter.
479  *
480  * Finally, if we see a "--args", dump the current list of arguments to the
481  * standard error.  This is used for debugging complex --file combinations.
482  */
483 static void
484 unescape_argv(int *argcp, char ***argvp)
485 {
486     int j, k;
487     int limit_includes = 20 + *argcp;
488     int count_includes = 0;
489     bool doalloc = FALSE;
490     char *filename;
491     const char **my_argv = 0;
492     int my_argc;
493 
494     DLG_TRACE(("# unescape_argv\n"));
495     for (k = 0; k < 2; ++k) {
496 
497 	my_argc = 0;
498 	if (special_argv != 0) {
499 	    for (j = 0; special_argv[j] != 0; ++j) {
500 		if (!strcmp(special_argv[j], "--")) {
501 		    break;
502 		} else if (isOption(special_argv[j])) {
503 		    if (k != 0)
504 			my_argv[my_argc] = special_argv[j];
505 		    my_argc++;
506 		}
507 	    }
508 	}
509 
510 	if (k == 0) {
511 	    my_argc += (*argcp + 1);
512 	    my_argv = dlg_calloc(const char *, (size_t) my_argc);
513 	    assert_ptr(my_argv, "unescape_argv");
514 	}
515     }
516 
517     for (j = 1; j < *argcp; j++) {
518 	bool escaped = FALSE;
519 	if (!strcmp((*argvp)[j], "--")) {
520 	    escaped = TRUE;
521 	    dlg_eat_argv(argcp, argvp, j, 1);
522 	} else if (!strcmp((*argvp)[j], "--args")) {
523 	    fprintf(stderr, _("Showing arguments at arg%d\n"), j);
524 	    for (k = 0; k < *argcp; ++k) {
525 		fprintf(stderr, " arg%d:%s\n", k, (*argvp)[k]);
526 	    }
527 	    dlg_eat_argv(argcp, argvp, j, 1);
528 	    --j;
529 	} else if (!strcmp((*argvp)[j], "--file")) {
530 	    if (++count_includes > limit_includes) {
531 		handle_leaks();
532 		dlg_exiterr(_("Too many --file options"));
533 	    }
534 
535 	    if ((filename = (*argvp)[j + 1]) != 0) {
536 		FILE *fp;
537 		char **list;
538 
539 		if (*filename == '&') {
540 		    fp = fdopen(atoi(filename + sizeof(char)), "r");
541 		} else {
542 		    fp = fopen(filename, "r");
543 		}
544 
545 		if (fp) {
546 		    char *blob;
547 		    int added;
548 		    size_t bytes_read;
549 		    size_t length;
550 		    int n;
551 
552 		    DLG_TRACE(("# opened --file %s ..\n", filename));
553 		    blob = NULL;
554 		    length = 0;
555 		    do {
556 			blob = dlg_realloc(char, length + BUFSIZ + 1, blob);
557 			assert_ptr(blob, "unescape_argv");
558 			bytes_read = fread(blob + length,
559 					   sizeof(char),
560 					     (size_t) BUFSIZ,
561 					   fp);
562 			length += bytes_read;
563 			if (ferror(fp)) {
564 			    handle_leaks();
565 			    dlg_exiterr(_("error on filehandle in unescape_argv"));
566 			}
567 		    } while (bytes_read == BUFSIZ);
568 		    fclose(fp);
569 
570 		    blob[length] = '\0';
571 		    ignore_leak(blob);
572 
573 		    list = dlg_string_to_argv(blob);
574 		    added = dlg_count_argv(list);
575 		    if (added > 2) {
576 			/* *argcp arguments before the expansion of --file
577 			   - 2 for the removal of '--file <filepath>'
578 			   + added for the arguments contained in <filepath>
579 			   + 1 for the terminating NULL pointer */
580 			size_t need = (size_t) (*argcp + added - 1);
581 			if (doalloc) {
582 			    *argvp = dlg_realloc(char *, need, *argvp);
583 			    assert_ptr(*argvp, "unescape_argv");
584 			} else {
585 			    char **newp = dlg_malloc(char *, need);
586 			    ignore_leak(newp);
587 			    assert_ptr(newp, "unescape_argv");
588 			    for (n = 0; n < *argcp; ++n) {
589 				newp[n] = (*argvp)[n];
590 			    }
591 			    /* The new array is not NULL-terminated yet. */
592 			    *argvp = newp;
593 			    doalloc = TRUE;
594 			}
595 			my_argv = dlg_realloc(const char *, need, my_argv);
596 			assert_ptr(my_argv, "unescape_argv");
597 
598 			/* Shift the arguments after '--file <filepath>'
599 			   right by (added - 2) positions */
600 			for (n = *argcp - 1; n >= j + 2; --n) {
601 			    (*argvp)[n + added - 2] = (*argvp)[n];
602 			}
603 		    } else if (added < 2) {
604 			/* 0 or 1 argument read from the included file
605 			   -> shift the arguments after '--file <filepath>'
606 			   left by (2 - added) positions */
607 			for (n = j + added; n + 2 - added < *argcp; ++n) {
608 			    (*argvp)[n] = (*argvp)[n + 2 - added];
609 			}
610 		    }
611 		    /* Copy the inserted arguments to *argvp */
612 		    for (n = 0; n < added; ++n) {
613 			(*argvp)[n + j] = list[n];
614 		    }
615 		    *argcp += added - 2;
616 		    (*argvp)[*argcp] = 0;	/* Write the NULL terminator */
617 		    free(list);	/* No-op if 'list' is NULL */
618 		    /* Force rescan starting from the first inserted argument */
619 		    --j;
620 		    DLG_TRACE(("# finished --file\n"));
621 		    continue;
622 		} else {
623 		    handle_leaks();
624 		    dlg_exiterr(_("Cannot open --file %s"), filename);
625 		}
626 	    } else {
627 		handle_leaks();
628 		dlg_exiterr(_("No value given for --file"));
629 	    }
630 	}
631 	if (!escaped
632 	    && (*argvp)[j] != 0
633 	    && !strncmp((*argvp)[j], "--", (size_t) 2)
634 	    && isalpha(UCH((*argvp)[j][2]))) {
635 	    my_argv[my_argc++] = (*argvp)[j];
636 	    DLG_TRACE(("#\toption argv[%d]=%s\n", j, (*argvp)[j]));
637 	}
638     }
639 
640     my_argv[my_argc] = 0;
641 
642     known_opts = my_argc;
643     dialog_opts = my_argv;
644 
645     DLG_TRACE(("#\t%d options vs %d arguments\n", known_opts, *argcp));
646     dialog_argv = (*argvp);
647 }
648 
649 static const Options *
650 findOption(const char *name, int pass, bool recur)
651 {
652     const Options *result = NULL;
653 
654     if (!strncmp(name, "--", 2) && isalpha(UCH(name[2]))) {
655 	unsigned n;
656 
657 	name += 2;		/* skip the "--" */
658 	for (n = 0; n < TableSize(options); n++) {
659 	    if ((pass & options[n].pass) != 0
660 		&& !strcmp(name, options[n].name)) {
661 		result = &options[n];
662 		break;
663 	    }
664 	}
665 	if (result == NULL && !recur) {
666 	    char *temp = malloc(8 + strlen(name));
667 	    if (temp != NULL) {
668 		if (!strncmp(name, "no", 2)) {
669 		    int skip = !strncmp(name, "no-", 3) ? 3 : 2;
670 		    sprintf(temp, "--no-%s", name + skip);
671 		    result = findOption(temp, pass, TRUE);
672 		    if (result == NULL) {
673 			sprintf(temp, "--%s", name + skip);
674 			result = findOption(temp, pass, TRUE);
675 		    }
676 		}
677 		if (result == NULL && strncmp(name, "no", 2)) {
678 		    sprintf(temp, "--no-%s", name);
679 		    result = findOption(temp, pass, TRUE);
680 		}
681 		free(temp);
682 	    }
683 	}
684     }
685     return result;
686 }
687 
688 static eOptions
689 lookupOption(const char *name, int pass)
690 {
691     eOptions result = o_unknown;
692     const Options *data = findOption(name, pass, FALSE);
693     if (data != NULL) {
694 	result = data->code;
695     }
696     return result;
697 }
698 
699 static void
700 Usage(const char *msg)
701 {
702     handle_leaks();
703     dlg_exiterr(_("%s.\nUse --help to list options.\n\n"), msg);
704 }
705 
706 /*
707  * Count arguments, stopping at the end of the argument list, or on any of our
708  * "--" tokens.
709  */
710 static int
711 arg_rest(char *argv[])
712 {
713     int i = 1;			/* argv[0] points to a "--" token */
714 
715     while (argv[i] != 0
716 	   && (!isOption(argv[i]) || lookupOption(argv[i], 7) == o_unknown))
717 	i++;
718     return i;
719 }
720 
721 /*
722  * In MultiWidget this function is needed to count how many tags
723  * a widget (menu, checklist, radiolist) has
724  */
725 static int
726 howmany_tags(char *argv[], int group)
727 {
728     int result = 0;
729     char temp[80];
730 
731     while (argv[0] != 0) {
732 	int have;
733 
734 	if (isOption(argv[0]))
735 	    break;
736 	if ((have = arg_rest(argv)) < group) {
737 	    const char *format = _("Expected %d arguments, found only %d");
738 	    sprintf(temp, format, group, have);
739 	    Usage(temp);
740 	} else if ((have % group) != 0) {
741 	    const char *format = _("Expected %d arguments, found extra %d");
742 	    sprintf(temp, format, group, (have % group));
743 	    Usage(temp);
744 	}
745 
746 	argv += have;
747 	result += (have / group);
748     }
749 
750     return result;
751 }
752 
753 static int
754 numeric_arg(char **av, int n)
755 {
756     int result = 0;
757 
758     if (n < dlg_count_argv(av)) {
759 	char *last = 0;
760 	result = (int) strtol(av[n], &last, 10);
761 
762 	if (last == 0 || *last != 0) {
763 	    char msg[80];
764 
765 	    sprintf(msg, _("Expected a number for token %d of %.*s"), n,
766 		    USEMAX_ARGS, av[0]);
767 	    Usage(msg);
768 	}
769     }
770     return result;
771 }
772 
773 static char *
774 optional_str(char **av, int n, char *dft)
775 {
776     char *ret = dft;
777     if (arg_rest(av) > n)
778 	ret = av[n];
779     return ret;
780 }
781 
782 #if defined(HAVE_DLG_GAUGE) || defined(HAVE_XDIALOG)
783 static int
784 optional_num(char **av, int n, int dft)
785 {
786     int ret = dft;
787     if (arg_rest(av) > n)
788 	ret = numeric_arg(av, n);
789     return ret;
790 }
791 #endif
792 
793 /*
794  * On AIX 4.x, we have to flush the output right away since there is a bug in
795  * the curses package which discards stdout even when we've used newterm to
796  * redirect output to /dev/tty.
797  */
798 static int
799 show_result(int ret)
800 {
801     bool either = FALSE;
802 
803     switch (ret) {
804     case DLG_EXIT_OK:
805     case DLG_EXIT_EXTRA:
806     case DLG_EXIT_HELP:
807     case DLG_EXIT_ITEM_HELP:
808 	if ((dialog_state.output_count > 1) && !dialog_vars.separate_output) {
809 	    fputs((dialog_state.separate_str
810 		   ? dialog_state.separate_str
811 		   : DEFAULT_SEPARATE_STR),
812 		  dialog_state.output);
813 	    either = TRUE;
814 	}
815 	if (dialog_vars.input_result != 0
816 	    && dialog_vars.input_result[0] != '\0') {
817 	    fputs(dialog_vars.input_result, dialog_state.output);
818 	    DLG_TRACE(("# input_result:\n%s\n", dialog_vars.input_result));
819 	    either = TRUE;
820 	}
821 	if (either) {
822 	    fflush(dialog_state.output);
823 	}
824 	break;
825     }
826     return ret;
827 }
828 
829 /*
830  * These are the widget callers.
831  */
832 
833 static int
834 call_yesno(CALLARGS)
835 {
836     *offset_add = 4;
837     return dialog_yesno(t,
838 			av[1],
839 			numeric_arg(av, 2),
840 			numeric_arg(av, 3));
841 }
842 
843 static int
844 call_msgbox(CALLARGS)
845 {
846     *offset_add = 4;
847     return dialog_msgbox(t,
848 			 av[1],
849 			 numeric_arg(av, 2),
850 			 numeric_arg(av, 3), 1);
851 }
852 
853 static int
854 call_infobox(CALLARGS)
855 {
856     *offset_add = 4;
857     return dialog_msgbox(t,
858 			 av[1],
859 			 numeric_arg(av, 2),
860 			 numeric_arg(av, 3), 0);
861 }
862 
863 static int
864 call_textbox(CALLARGS)
865 {
866     *offset_add = 4;
867     return dialog_textbox(t,
868 			  av[1],
869 			  numeric_arg(av, 2),
870 			  numeric_arg(av, 3));
871 }
872 
873 static int
874 call_menu(CALLARGS)
875 {
876     int tags = howmany_tags(av + 5, MENUBOX_TAGS);
877     *offset_add = 5 + tags * MENUBOX_TAGS;
878 
879     return dialog_menu(t,
880 		       av[1],
881 		       numeric_arg(av, 2),
882 		       numeric_arg(av, 3),
883 		       numeric_arg(av, 4),
884 		       tags, av + 5);
885 }
886 
887 static int
888 call_inputmenu(CALLARGS)
889 {
890     int tags = howmany_tags(av + 5, MENUBOX_TAGS);
891     bool free_extra_label = FALSE;
892     int result;
893 
894     dialog_vars.input_menu = TRUE;
895 
896     if (dialog_vars.extra_label == 0) {
897 	free_extra_label = TRUE;
898 	dialog_vars.extra_label = dlg_strclone(_("Rename"));
899     }
900 
901     dialog_vars.extra_button = TRUE;
902 
903     *offset_add = 5 + tags * MENUBOX_TAGS;
904     result = dialog_menu(t,
905 			 av[1],
906 			 numeric_arg(av, 2),
907 			 numeric_arg(av, 3),
908 			 numeric_arg(av, 4),
909 			 tags, av + 5);
910     if (free_extra_label) {
911 	free(dialog_vars.extra_label);
912 	dialog_vars.extra_label = 0;
913     }
914     return result;
915 }
916 
917 static int
918 call_checklist(CALLARGS)
919 {
920     int tags = howmany_tags(av + 5, CHECKBOX_TAGS);
921     int code;
922 
923     *offset_add = 5 + tags * CHECKBOX_TAGS;
924     code = dialog_checklist(t,
925 			    av[1],
926 			    numeric_arg(av, 2),
927 			    numeric_arg(av, 3),
928 			    numeric_arg(av, 4),
929 			    tags, av + 5, FLAG_CHECK);
930     return code;
931 }
932 
933 static int
934 call_radiolist(CALLARGS)
935 {
936     int tags = howmany_tags(av + 5, CHECKBOX_TAGS);
937     *offset_add = 5 + tags * CHECKBOX_TAGS;
938     return dialog_checklist(t,
939 			    av[1],
940 			    numeric_arg(av, 2),
941 			    numeric_arg(av, 3),
942 			    numeric_arg(av, 4),
943 			    tags, av + 5, FLAG_RADIO);
944 }
945 
946 static int
947 call_inputbox(CALLARGS)
948 {
949     *offset_add = arg_rest(av);
950     return dialog_inputbox(t,
951 			   av[1],
952 			   numeric_arg(av, 2),
953 			   numeric_arg(av, 3),
954 			   optional_str(av, 4, 0), 0);
955 }
956 
957 static int
958 call_passwordbox(CALLARGS)
959 {
960     *offset_add = arg_rest(av);
961     return dialog_inputbox(t,
962 			   av[1],
963 			   numeric_arg(av, 2),
964 			   numeric_arg(av, 3),
965 			   optional_str(av, 4, 0), 1);
966 }
967 
968 #ifdef HAVE_XDIALOG
969 static int
970 call_calendar(CALLARGS)
971 {
972     *offset_add = arg_rest(av);
973     return dialog_calendar(t,
974 			   av[1],
975 			   numeric_arg(av, 2),
976 			   numeric_arg(av, 3),
977 			   optional_num(av, 4, -1),
978 			   optional_num(av, 5, -1),
979 			   optional_num(av, 6, -1));
980 }
981 
982 static int
983 call_dselect(CALLARGS)
984 {
985     *offset_add = arg_rest(av);
986     return dialog_dselect(t,
987 			  av[1],
988 			  numeric_arg(av, 2),
989 			  numeric_arg(av, 3));
990 }
991 
992 static int
993 call_editbox(CALLARGS)
994 {
995     *offset_add = 4;
996     return dialog_editbox(t,
997 			  av[1],
998 			  numeric_arg(av, 2),
999 			  numeric_arg(av, 3));
1000 }
1001 
1002 static int
1003 call_fselect(CALLARGS)
1004 {
1005     *offset_add = arg_rest(av);
1006     return dialog_fselect(t,
1007 			  av[1],
1008 			  numeric_arg(av, 2),
1009 			  numeric_arg(av, 3));
1010 }
1011 
1012 static int
1013 call_timebox(CALLARGS)
1014 {
1015     *offset_add = arg_rest(av);
1016     return dialog_timebox(t,
1017 			  av[1],
1018 			  numeric_arg(av, 2),
1019 			  numeric_arg(av, 3),
1020 			  optional_num(av, 4, -1),
1021 			  optional_num(av, 5, -1),
1022 			  optional_num(av, 6, -1));
1023 }
1024 #endif /* HAVE_XDIALOG */
1025 
1026 /* dialog 1.2 widgets */
1027 #ifdef HAVE_XDIALOG2
1028 
1029 #define DisableNoTags() \
1030 	bool save_no_tags = dialog_vars.no_tags; \
1031 	bool save_no_items = dialog_vars.no_items; \
1032 	dialog_vars.no_tags = TRUE; \
1033 	dialog_vars.no_items = FALSE
1034 
1035 #define RestoreNoTags() \
1036 	dialog_vars.no_tags = save_no_tags; \
1037 	dialog_vars.no_items = save_no_items
1038 
1039 static int
1040 call_buildlist(CALLARGS)
1041 {
1042     int tags = howmany_tags(av + 5, CHECKBOX_TAGS);
1043     int result;
1044 
1045     DisableNoTags();
1046 
1047     *offset_add = 5 + tags * CHECKBOX_TAGS;
1048     result = dialog_buildlist(t,
1049 			      av[1],
1050 			      numeric_arg(av, 2),
1051 			      numeric_arg(av, 3),
1052 			      numeric_arg(av, 4),
1053 			      tags, av + 5,
1054 			      dialog_vars.reorder);
1055     RestoreNoTags();
1056     return result;
1057 }
1058 
1059 static int
1060 call_rangebox(CALLARGS)
1061 {
1062     int min_value;
1063 
1064     *offset_add = arg_rest(av);
1065     min_value = numeric_arg(av, 4);
1066     return dialog_rangebox(t,
1067 			   av[1],
1068 			   numeric_arg(av, 2),
1069 			   numeric_arg(av, 3),
1070 			   min_value,
1071 			   numeric_arg(av, 5),
1072 			   (*offset_add > 6) ? numeric_arg(av, 6) : min_value);
1073 }
1074 
1075 static int
1076 call_treeview(CALLARGS)
1077 {
1078     int tags = howmany_tags(av + 5, TREEVIEW_TAGS);
1079     int result;
1080 
1081     DisableNoTags();
1082 
1083     *offset_add = arg_rest(av);
1084     result = dialog_treeview(t,
1085 			     av[1],
1086 			     numeric_arg(av, 2),
1087 			     numeric_arg(av, 3),
1088 			     numeric_arg(av, 4),
1089 			     tags, av + 5, FLAG_RADIO);
1090     RestoreNoTags();
1091     return result;
1092 }
1093 #endif /* HAVE_XDIALOG */
1094 
1095 #ifdef HAVE_DLG_FORMBOX
1096 static int
1097 call_form(CALLARGS)
1098 {
1099     int group = FORMBOX_TAGS;
1100     int tags = howmany_tags(av + 5, group);
1101     *offset_add = 5 + tags * group;
1102 
1103     return dialog_form(t,
1104 		       av[1],
1105 		       numeric_arg(av, 2),
1106 		       numeric_arg(av, 3),
1107 		       numeric_arg(av, 4),
1108 		       tags, av + 5);
1109 }
1110 
1111 static int
1112 call_password_form(CALLARGS)
1113 {
1114     unsigned save = dialog_vars.formitem_type;
1115     int result;
1116 
1117     dialog_vars.formitem_type = 1;
1118     result = call_form(PASSARGS);
1119     dialog_vars.formitem_type = save;
1120 
1121     return result;
1122 }
1123 #endif /* HAVE_DLG_FORMBOX */
1124 
1125 #ifdef HAVE_DLG_MIXEDFORM
1126 static int
1127 call_mixed_form(CALLARGS)
1128 {
1129     int group = MIXEDFORM_TAGS;
1130     int tags = howmany_tags(av + 5, group);
1131     *offset_add = 5 + tags * group;
1132 
1133     return dialog_mixedform(t,
1134 			    av[1],
1135 			    numeric_arg(av, 2),
1136 			    numeric_arg(av, 3),
1137 			    numeric_arg(av, 4),
1138 			    tags, av + 5);
1139 }
1140 #endif /* HAVE_DLG_MIXEDFORM */
1141 
1142 #ifdef HAVE_DLG_GAUGE
1143 static int
1144 call_gauge(CALLARGS)
1145 {
1146     *offset_add = arg_rest(av);
1147     return dialog_gauge(t,
1148 			av[1],
1149 			numeric_arg(av, 2),
1150 			numeric_arg(av, 3),
1151 			optional_num(av, 4, 0));
1152 }
1153 
1154 static int
1155 call_pause(CALLARGS)
1156 {
1157     *offset_add = arg_rest(av);
1158     return dialog_pause(t,
1159 			av[1],
1160 			numeric_arg(av, 2),
1161 			numeric_arg(av, 3),
1162 			numeric_arg(av, 4));
1163 }
1164 #endif
1165 
1166 #ifdef HAVE_MIXEDGAUGE
1167 static int
1168 call_mixed_gauge(CALLARGS)
1169 {
1170 #define MIXEDGAUGE_BASE 5
1171     int tags = howmany_tags(av + MIXEDGAUGE_BASE, MIXEDGAUGE_TAGS);
1172     *offset_add = MIXEDGAUGE_BASE + tags * MIXEDGAUGE_TAGS;
1173     return dialog_mixedgauge(t,
1174 			     av[1],
1175 			     numeric_arg(av, 2),
1176 			     numeric_arg(av, 3),
1177 			     numeric_arg(av, 4),
1178 			     tags, av + MIXEDGAUGE_BASE);
1179 }
1180 #endif
1181 
1182 #ifdef HAVE_DLG_GAUGE
1183 static int
1184 call_prgbox(CALLARGS)
1185 {
1186     *offset_add = arg_rest(av);
1187     /* the original version does not accept a prompt string, but for
1188      * consistency we allow it.
1189      */
1190     return ((*offset_add == 5)
1191 	    ? dialog_prgbox(t,
1192 			    av[1],
1193 			    av[2],
1194 			    numeric_arg(av, 3),
1195 			    numeric_arg(av, 4), TRUE)
1196 	    : dialog_prgbox(t,
1197 			    "",
1198 			    av[1],
1199 			    numeric_arg(av, 2),
1200 			    numeric_arg(av, 3), TRUE));
1201 }
1202 #endif
1203 
1204 #ifdef HAVE_DLG_GAUGE
1205 static int
1206 call_programbox(CALLARGS)
1207 {
1208     int result;
1209 
1210     *offset_add = arg_rest(av);
1211     /* this function is a compromise between --prgbox and --progressbox.
1212      */
1213     result = ((*offset_add == 4)
1214 	      ? dlg_progressbox(t,
1215 				av[1],
1216 				numeric_arg(av, 2),
1217 				numeric_arg(av, 3),
1218 				TRUE,
1219 				dialog_state.pipe_input)
1220 	      : dlg_progressbox(t,
1221 				"",
1222 				numeric_arg(av, 1),
1223 				numeric_arg(av, 2),
1224 				TRUE,
1225 				dialog_state.pipe_input));
1226     dialog_state.pipe_input = 0;
1227     return result;
1228 }
1229 #endif
1230 
1231 #ifdef HAVE_DLG_GAUGE
1232 static int
1233 call_progressbox(CALLARGS)
1234 {
1235     *offset_add = arg_rest(av);
1236     /* the original version does not accept a prompt string, but for
1237      * consistency we allow it.
1238      */
1239     return ((*offset_add == 4)
1240 	    ? dialog_progressbox(t,
1241 				 av[1],
1242 				 numeric_arg(av, 2),
1243 				 numeric_arg(av, 3))
1244 	    : dialog_progressbox(t,
1245 				 "",
1246 				 numeric_arg(av, 1),
1247 				 numeric_arg(av, 2)));
1248 }
1249 #endif
1250 
1251 #ifdef HAVE_DLG_TAILBOX
1252 static int
1253 call_tailbox(CALLARGS)
1254 {
1255     *offset_add = 4;
1256     return dialog_tailbox(t,
1257 			  av[1],
1258 			  numeric_arg(av, 2),
1259 			  numeric_arg(av, 3),
1260 			  FALSE);
1261 }
1262 
1263 static int
1264 call_tailboxbg(CALLARGS)
1265 {
1266     *offset_add = 4;
1267     return dialog_tailbox(t,
1268 			  av[1],
1269 			  numeric_arg(av, 2),
1270 			  numeric_arg(av, 3),
1271 			  TRUE);
1272 }
1273 #endif
1274 /* *INDENT-OFF* */
1275 static const Mode modes[] =
1276 {
1277     {o_yesno,           4, 4, call_yesno},
1278     {o_msgbox,          4, 4, call_msgbox},
1279     {o_infobox,         4, 4, call_infobox},
1280     {o_textbox,         4, 4, call_textbox},
1281     {o_menu,            6, 0, call_menu},
1282     {o_inputmenu,       6, 0, call_inputmenu},
1283     {o_checklist,       7, 0, call_checklist},
1284     {o_radiolist,       7, 0, call_radiolist},
1285     {o_inputbox,        4, 5, call_inputbox},
1286     {o_passwordbox,     4, 5, call_passwordbox},
1287 #ifdef HAVE_DLG_GAUGE
1288     {o_gauge,           4, 5, call_gauge},
1289     {o_pause,           5, 5, call_pause},
1290     {o_prgbox,          4, 5, call_prgbox},
1291     {o_programbox,      3, 4, call_programbox},
1292     {o_progressbox,     3, 4, call_progressbox},
1293 #endif
1294 #ifdef HAVE_DLG_FORMBOX
1295     {o_passwordform,   13, 0, call_password_form},
1296     {o_form,           13, 0, call_form},
1297 #endif
1298 #ifdef HAVE_MIXEDGAUGE
1299     {o_mixedgauge,      MIXEDGAUGE_BASE, 0, call_mixed_gauge},
1300 #endif
1301 #ifdef HAVE_DLG_MIXEDFORM
1302     {o_mixedform,      13, 0, call_mixed_form},
1303 #endif
1304 #ifdef HAVE_DLG_TAILBOX
1305     {o_tailbox,         4, 4, call_tailbox},
1306     {o_tailboxbg,       4, 4, call_tailboxbg},
1307 #endif
1308 #ifdef HAVE_XDIALOG
1309     {o_calendar,        4, 7, call_calendar},
1310     {o_dselect,         4, 5, call_dselect},
1311     {o_editbox,         4, 4, call_editbox},
1312     {o_fselect,         4, 5, call_fselect},
1313     {o_timebox,         4, 7, call_timebox},
1314 #endif
1315 #ifdef HAVE_XDIALOG2
1316     {o_buildlist,       4, 0, call_buildlist},
1317     {o_rangebox,        5, 7, call_rangebox},
1318     {o_treeview,        4, 0, call_treeview},
1319 #endif
1320 };
1321 /* *INDENT-ON* */
1322 
1323 static char *
1324 optionString(char **argv, int *num)
1325 {
1326     int next = *num + 1;
1327     char *result = argv[next];
1328     if (result == 0) {
1329 	char temp[80];
1330 	sprintf(temp, ("Expected a string-parameter for %.*s"), USEMAX_ARGS,
1331 		argv[*num]);
1332 	Usage(temp);
1333     }
1334     *num = next;
1335     return result;
1336 }
1337 
1338 static int
1339 optionValue(char **argv, int *num)
1340 {
1341     int next = *num + 1;
1342     char *src = argv[next];
1343     char *tmp = 0;
1344     int result = 0;
1345 
1346     if (src != 0) {
1347 	result = (int) strtol(src, &tmp, 0);
1348 	if (tmp == 0 || *tmp != 0)
1349 	    src = 0;
1350     }
1351 
1352     if (src == 0) {
1353 	char temp[80];
1354 	sprintf(temp, _("Expected a numeric-parameter for %.*s"),
1355 		USEMAX_ARGS, argv[*num]);
1356 	Usage(temp);
1357     }
1358     *num = next;
1359     return result;
1360 }
1361 
1362 /*
1363  * In findOption, we made provision for adding/removing a "no" prefix from
1364  * any boolean option.  This function determines the actual true/false result.
1365  */
1366 static bool
1367 optionBool(const char *actual, const Options * data)
1368 {
1369     bool normal = (data->type == tTrue) ? TRUE : FALSE;
1370     bool result = !strcmp(actual + 2, data->name) ? normal : !normal;
1371     if (result != normal) {
1372 	int want_no = (strncmp(actual + 2, "no", 2) == 0);
1373 	int have_no = (strncmp(data->name, "no", 2) == 0);
1374 	if (have_no == want_no)
1375 	    result = normal;
1376     }
1377     return result;
1378 }
1379 
1380 /* Return exit-code for a named button */
1381 static int
1382 button_code(const char *name)
1383 {
1384     /* *INDENT-OFF* */
1385     static struct {
1386 	const char *name;
1387 	int code;
1388     } table[] = {
1389 	{ "ok",	    DLG_EXIT_OK },
1390 	{ "yes",    DLG_EXIT_OK },
1391 	{ "cancel", DLG_EXIT_CANCEL },
1392 	{ "no",	    DLG_EXIT_CANCEL },
1393 	{ "help",   DLG_EXIT_HELP },
1394 	{ "extra",  DLG_EXIT_EXTRA },
1395     };
1396     /* *INDENT-ON* */
1397 
1398     int code = DLG_EXIT_ERROR;
1399     size_t i;
1400 
1401     for (i = 0; i < TableSize(table); i++) {
1402 	if (!dlg_strcmp(name, table[i].name)) {
1403 	    code = table[i].code;
1404 	    break;
1405 	}
1406     }
1407 
1408     if (code == DLG_EXIT_ERROR) {
1409 	char temp[80];
1410 	sprintf(temp, _("Button name \"%.*s\" unknown"), USEMAX_ARGS, name);
1411 	Usage(temp);
1412     }
1413 
1414     return code;
1415 }
1416 
1417 /*
1418  * If this is the last option, we do not want any error messages - just our
1419  * output.  Calling end_dialog() cancels the refresh() at the end of the
1420  * program as well.
1421  */
1422 static void
1423 IgnoreNonScreen(char **argv, int offset)
1424 {
1425     if (argv[offset + 1] == 0) {
1426 	ignore_unknown = TRUE;
1427 	end_dialog();
1428     }
1429 }
1430 
1431 static void
1432 PrintTextOnly(char **argv, int *offset, eOptions code)
1433 {
1434     /* TODO - handle two optional numeric params */
1435     char *text;
1436     int height = 0;
1437     int width = 0;
1438     int height2 = 0;
1439     int width2 = 0;
1440     int next = arg_rest(argv + *offset);
1441 
1442     if (LINES <= 0 && COLS <= 0) {
1443 	dlg_ttysize(fileno(dialog_state.input),
1444 		    &dialog_state.screen_height,
1445 		    &dialog_state.screen_width);
1446     }
1447 
1448     text = strdup(optionString(argv, offset));
1449     IgnoreNonScreen(argv, *offset);
1450 
1451     if (next >= 1) {
1452 	next = MIN(next, 3);
1453 	height = numeric_arg(argv, *offset + 1);
1454 	if (next >= 2)
1455 	    width = numeric_arg(argv, *offset + 2);
1456 	*offset += next - 1;
1457     }
1458 
1459     dlg_trim_string(text);
1460     dlg_auto_size(NULL, text, &height2, &width2, height, width);
1461 
1462     switch (code) {
1463     case o_text_only:
1464 	dialog_state.text_only = TRUE;
1465 	dlg_print_autowrap(stdscr, text, height2, width2);
1466 	dialog_state.text_only = FALSE;
1467 	break;
1468     case o_print_text_size:
1469 	fprintf(dialog_state.output, "%d %d\n",
1470 		dialog_state.text_height,
1471 		dialog_state.text_width);
1472 	break;
1473     default:
1474 	break;
1475     }
1476 }
1477 
1478 /*
1479  * Print parts of a message
1480  */
1481 static void
1482 PrintList(const char *const *list)
1483 {
1484     const char *leaf = strrchr(program, '/');
1485     unsigned n = 0;
1486 
1487     if (leaf != 0)
1488 	leaf++;
1489     else
1490 	leaf = program;
1491 
1492     while (*list != 0) {
1493 	fprintf(dialog_state.output, *list, n ? leaf : dialog_version());
1494 	(void) fputc('\n', dialog_state.output);
1495 	n = 1;
1496 	list++;
1497     }
1498 }
1499 
1500 static const Mode *
1501 lookupMode(eOptions code)
1502 {
1503     const Mode *modePtr = 0;
1504     unsigned n;
1505 
1506     for (n = 0; n < TableSize(modes); n++) {
1507 	if (modes[n].code == code) {
1508 	    modePtr = &modes[n];
1509 	    break;
1510 	}
1511     }
1512     return modePtr;
1513 }
1514 
1515 static int
1516 compare_opts(const void *a, const void *b)
1517 {
1518     Options *const *p = (Options * const *) a;
1519     Options *const *q = (Options * const *) b;
1520     return strcmp((*p)->name, (*q)->name);
1521 }
1522 
1523 /*
1524  * Print program's version.
1525  */
1526 static void
1527 PrintVersion(FILE *fp)
1528 {
1529     fprintf(fp, "Version: %s\n", dialog_version());
1530 }
1531 
1532 /*
1533  * Print program help-message
1534  */
1535 static void
1536 Help(void)
1537 {
1538     static const char *const tbl_1[] =
1539     {
1540 	"cdialog (ComeOn Dialog!) version %s",
1541 	"Copyright 2000-2021,2022 Thomas E. Dickey",
1542 	"This is free software; see the source for copying conditions.  There is NO",
1543 	"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.",
1544 	"",
1545 	"* Display dialog boxes from shell scripts *",
1546 	"",
1547 	"Usage: %s <options> { --and-widget <options> }",
1548 	"where options are \"common\" options, followed by \"box\" options",
1549 	"",
1550 #ifdef HAVE_RC_FILE
1551 	"Special options:",
1552 	"  [--create-rc \"file\"]",
1553 #endif
1554 	0
1555     }, *const tbl_3[] =
1556     {
1557 	"",
1558 	"Auto-size with height and width = 0. Maximize with height and width = -1.",
1559 	"Global-auto-size if also menu_height/list_height = 0.",
1560 	0
1561     };
1562     size_t limit = TableSize(options);
1563     size_t j, k;
1564     const Options **opts;
1565 
1566     end_dialog();
1567     dialog_state.output = stdout;
1568 
1569     opts = dlg_calloc(const Options *, limit);
1570     assert_ptr(opts, "Help");
1571     for (j = 0; j < limit; ++j) {
1572 	opts[j] = &(options[j]);
1573     }
1574     qsort(opts, limit, sizeof(Options *), compare_opts);
1575 
1576     PrintList(tbl_1);
1577     fprintf(dialog_state.output, "Common options:\n ");
1578     for (j = k = 0; j < limit; j++) {
1579 	if ((opts[j]->pass & 1)
1580 	    && opts[j]->help != 0) {
1581 	    size_t len = 6 + strlen(opts[j]->name) + strlen(opts[j]->help);
1582 	    k += len;
1583 	    if (k > 75) {
1584 		fprintf(dialog_state.output, "\n ");
1585 		k = len;
1586 	    }
1587 	    fprintf(dialog_state.output, " [--%s%s%s]", opts[j]->name,
1588 		    *(opts[j]->help) ? " " : "", opts[j]->help);
1589 	}
1590     }
1591     fprintf(dialog_state.output, "\nBox options:\n");
1592     for (j = 0; j < limit; j++) {
1593 	if ((opts[j]->pass & 2) != 0
1594 	    && opts[j]->help != 0
1595 	    && lookupMode(opts[j]->code)) {
1596 	    fprintf(dialog_state.output, "  --%-12s %s\n", opts[j]->name,
1597 		    opts[j]->help);
1598 	}
1599     }
1600     PrintList(tbl_3);
1601 
1602     free(opts);
1603     handle_leaks();
1604     dlg_exit(DLG_EXIT_OK);
1605 }
1606 
1607 #ifdef HAVE_DLG_TRACE
1608 /*
1609  * Only the first call to dlg_trace will open a trace file.  But each time
1610  * --trace is parsed, we show the whole parameter list as it is at that moment,
1611  * counting discarded parameters.  The only way to capture the whole parameter
1612  * list is if --trace is the first option.
1613  */
1614 static void
1615 process_trace_option(char **argv, int *offset)
1616 {
1617     int j;
1618 
1619     if (dialog_state.trace_output == 0) {
1620 	dlg_trace(optionString(argv, offset));
1621     } else {
1622 	DLG_TRACE(("# ignore extra --trace option\n"));
1623 	*offset += 1;
1624     }
1625 
1626     DLG_TRACE(("# Parameters:\n"));
1627     for (j = 0; argv[j] != 0; ++j) {
1628 	DLG_TRACE(("#\targv[%d] = %s\n", j, argv[j]));
1629     }
1630 }
1631 #endif
1632 
1633 /*
1634  * "Common" options apply to all widgets more/less.  Most of the common options
1635  * set values in dialog_vars, a few set dialog_state and a couple write to the
1636  * output stream.
1637  */
1638 static int
1639 process_common_options(int argc, char **argv, int offset, int output)
1640 {
1641     const Options *data = NULL;
1642     bool done = FALSE;
1643 
1644     DLG_TRACE(("# process_common_options, offset %d:%d\n", offset, argc));
1645 
1646     while (!done && offset < argc) {
1647 	static char empty[] = "";
1648 	eOptions code = o_unknown;
1649 	char *sval = empty;
1650 	bool bval;
1651 	int nval;
1652 	char *target;
1653 
1654 	DLG_TRACE(("#\targv[%d] = %s\n", offset, argv[offset]));
1655 	if ((data = findOption(argv[offset], 1, FALSE)) == NULL) {
1656 	    done = TRUE;
1657 	    continue;
1658 	}
1659 
1660 	switch (data->vars) {
1661 	default:
1662 	    target = NULL;
1663 	    break;
1664 	case 1:
1665 	    target = (char *) &dialog_state;
1666 	    break;
1667 	case 2:
1668 	    target = (char *) &dialog_vars;
1669 	    break;
1670 	}
1671 
1672 #define TraceTarget \
1673 	    ((target == (char *) &dialog_state) \
1674 	     ? "dialog_state" \
1675 	     : "dialog_vars")
1676 #define TraceBVal (bval ? "TRUE" : "FALSE")
1677 #define TraceDone(fmt,value) \
1678 	DLG_TRACE(("#\t.. set %s.%s = %"fmt"\n", TraceTarget, data->name, value))
1679 #define TraceLate(fmt,value) \
1680 	DLG_TRACE(("#\t.. defer setting %s = %"fmt"\n", data->name, value))
1681 
1682 	code = data->code;
1683 	switch (data->type) {
1684 	default:
1685 	    break;
1686 	case tFalse:
1687 	case tTrue:
1688 	    bval = optionBool(argv[offset], data);
1689 	    if (target != NULL) {
1690 		*(bool *) (target + data->offset) = bval;
1691 		TraceDone("s", TraceBVal);
1692 	    } else {
1693 		TraceLate("s", TraceBVal);
1694 	    }
1695 	    break;
1696 	case tNumber:
1697 	    nval = optionValue(argv, &offset);
1698 	    if (target != NULL) {
1699 		*(int *) (void *) (target + data->offset) = nval;
1700 		TraceDone("d", nval);
1701 	    } else {
1702 		TraceLate("d", nval);
1703 	    }
1704 	    break;
1705 	case tString:
1706 	    sval = optionString(argv, &offset);
1707 	    if (target != NULL) {
1708 		*(char **) (void *) (target + data->offset) = sval;
1709 		TraceDone("s", sval);
1710 	    } else {
1711 		TraceLate("s", sval);
1712 	    }
1713 	    break;
1714 	}
1715 
1716 	switch (code) {
1717 	case o_defaultno:
1718 	    dialog_vars.default_button = DLG_EXIT_CANCEL;
1719 	    break;
1720 	case o_default_button:
1721 	    dialog_vars.default_button = button_code(sval);
1722 	    dialog_vars.defaultno = dialog_vars.default_button == DLG_EXIT_CANCEL;
1723 	    break;
1724 	case o_text_only:
1725 	case o_print_text_size:
1726 	    PrintTextOnly(argv, &offset, code);
1727 	    break;
1728 	case o_print_maxsize:
1729 	    if (output) {
1730 		IgnoreNonScreen(argv, offset);
1731 		fflush(dialog_state.output);
1732 		fprintf(dialog_state.output, "MaxSize: %d, %d\n", SLINES, SCOLS);
1733 	    }
1734 	    break;
1735 	case o_print_version:
1736 	    if (output) {
1737 		PrintVersion(dialog_state.output);
1738 	    }
1739 	    break;
1740 	case o_visit_items:
1741 	    dialog_state.visit_cols = 1;
1742 	    break;
1743 	case o_begin_set:
1744 #ifdef HAVE_WHIPTAIL
1745 	    if (!strcmp(argv[offset], "--topleft")) {
1746 		dialog_vars.begin_y = 0;
1747 		dialog_vars.begin_x = 0;
1748 	    } else
1749 #endif
1750 	    {
1751 		dialog_vars.begin_y = optionValue(argv, &offset);
1752 		dialog_vars.begin_x = optionValue(argv, &offset);
1753 	    }
1754 	    break;
1755 	case o_ascii_lines:
1756 	    dialog_vars.no_lines = FALSE;
1757 	    break;
1758 	case o_no_lines:
1759 	    dialog_vars.ascii_lines = FALSE;
1760 	    break;
1761 	case o_no_mouse:
1762 	    mouse_close();
1763 	    break;
1764 	case o_unknown:
1765 	    done = !ignore_unknown;
1766 	default:
1767 	    break;
1768 #ifdef HAVE_DLG_TRACE
1769 	case o_trace:
1770 	    process_trace_option(argv, &offset);
1771 	    break;
1772 #endif
1773 	case o_iso_week:
1774 	    if (dialog_vars.week_start == 0) {	/* Monday is implied */
1775 		static char default_1st[] = "1";
1776 		dialog_vars.week_start = default_1st;
1777 	    }
1778 	    break;
1779 	}
1780 	if (!done)
1781 	    offset++;
1782     }
1783 
1784     if (dialog_state.aspect_ratio == 0)
1785 	dialog_state.aspect_ratio = DEFAULT_ASPECT_RATIO;
1786 
1787     return offset;
1788 }
1789 
1790 /*
1791  * Initialize options at the start of a series of common options culminating
1792  * in a widget.
1793  */
1794 static void
1795 init_result(char *buffer)
1796 {
1797     static bool first = TRUE;
1798 
1799     DLG_TRACE(("# init_result\n"));
1800 
1801     /* clear everything we do not save for the next widget */
1802     memset(&dialog_vars, 0, sizeof(dialog_vars));
1803 
1804     dialog_vars.input_result = buffer;
1805     dialog_vars.input_result[0] = '\0';
1806 
1807     dialog_vars.default_button = -1;
1808 
1809     /*
1810      * The first time this is called, check for common options given by an
1811      * environment variable.
1812      */
1813     if (first) {
1814 	char *env = dlg_getenv_str("DIALOGOPTS");
1815 	if (env != 0)
1816 	    env = dlg_strclone(env);
1817 	if (env != 0) {
1818 	    special_argv = dlg_string_to_argv(env);
1819 	    special_argc = dlg_count_argv(special_argv);
1820 	}
1821 	first = FALSE;
1822     }
1823 
1824     if (special_argv != 0) {
1825 	process_common_options(special_argc, special_argv, 0, FALSE);
1826     }
1827 }
1828 
1829 int
1830 main(int argc, char *argv[])
1831 {
1832     char temp[256];
1833     bool esc_pressed = FALSE;
1834     bool keep_tite = FALSE;
1835     int initial = 1;
1836     int offset = 1;
1837     int offset_add = 0;
1838     int retval = DLG_EXIT_OK;
1839     int j;
1840     eOptions code;
1841     char my_buffer[MAX_LEN + 1];
1842 
1843     memset(&dialog_state, 0, sizeof(dialog_state));
1844     memset(&dialog_vars, 0, sizeof(dialog_vars));
1845 
1846 #if defined(ENABLE_NLS)
1847     /* initialize locale support */
1848     setlocale(LC_ALL, "");
1849     bindtextdomain(NLS_TEXTDOMAIN, LOCALEDIR);
1850     textdomain(NLS_TEXTDOMAIN);
1851 #elif defined(HAVE_SETLOCALE)
1852     (void) setlocale(LC_ALL, "");
1853 #endif
1854 
1855     init_result(my_buffer);	/* honor $DIALOGOPTS */
1856     unescape_argv(&argc, &argv);
1857     program = argv[0];
1858     dialog_state.output = stderr;
1859     dialog_state.input = stdin;
1860 
1861     /*
1862      * Look for the last --stdout, --stderr or --output-fd option, and use
1863      * that.  We can only write to one of them.  If --stdout is used, that
1864      * can interfere with initializing the curses library, so we want to
1865      * know explicitly if it is used.
1866      *
1867      * Also, look for any --version or --help message, processing those
1868      * immediately.
1869      */
1870     while (offset < argc) {
1871 	int base = offset;
1872 	switch (lookupOption(argv[offset], 7)) {
1873 	case o_output_stdout:
1874 	    dialog_state.output = stdout;
1875 	    break;
1876 	case o_output_stderr:
1877 	    dialog_state.output = stderr;
1878 	    break;
1879 	case o_input_fd:
1880 	    if ((j = optionValue(argv, &offset)) < 0
1881 		|| (dialog_state.input = fdopen(j, "r")) == 0) {
1882 		handle_leaks();
1883 		dlg_exiterr(_("Cannot open input-fd\n"));
1884 	    }
1885 	    break;
1886 	case o_output_fd:
1887 	    if ((j = optionValue(argv, &offset)) < 0
1888 		|| (dialog_state.output = fdopen(j, "w")) == 0) {
1889 		handle_leaks();
1890 		dlg_exiterr(_("Cannot open output-fd\n"));
1891 	    }
1892 	    break;
1893 	case o_keep_tite:
1894 	    keep_tite = TRUE;
1895 	    break;
1896 	case o_version:
1897 	    dialog_state.output = stdout;
1898 	    PrintVersion(dialog_state.output);
1899 	    dlg_exit(DLG_EXIT_OK);
1900 	    break;
1901 	case o_help:
1902 	    Help();
1903 	    break;
1904 #ifdef HAVE_DLG_TRACE
1905 	case o_trace:
1906 	    /*
1907 	     * Process/remove the --trace option if it is the first option.
1908 	     * Otherwise, process it in more/less expected order as a
1909 	     * "common" option.
1910 	     */
1911 	    if (base == 1) {
1912 		process_trace_option(argv, &offset);
1913 		break;
1914 	    } else {
1915 		++offset;
1916 		continue;
1917 	    }
1918 #endif
1919 	default:
1920 	    ++offset;
1921 	    continue;
1922 	}
1923 	DLG_TRACE(("# discarding %d parameters starting with argv[%d] (%s)\n",
1924 		   1 + offset - base, base,
1925 		   argv[base]));
1926 	for (j = base; j < argc; ++j) {
1927 	    dialog_argv[j] = dialog_argv[j + 1 + (offset - base)];
1928 	}
1929 	argc -= (1 + offset - base);
1930 	offset = base;
1931     }
1932     offset = 1;
1933     init_result(my_buffer);
1934     dialog_vars.keep_tite |= keep_tite;		/* init_result() cleared global */
1935 
1936     /*
1937      * Dialog's output may be redirected (see above).  Handle the special
1938      * case of options that only report information without interaction.
1939      */
1940     if (argc == 2) {
1941 	switch (lookupOption(argv[1], 7)) {
1942 	case o_print_maxsize:
1943 	    (void) initscr();
1944 	    endwin();
1945 	    fflush(dialog_state.output);
1946 	    fprintf(dialog_state.output, "MaxSize: %d, %d\n", SLINES, SCOLS);
1947 	    break;
1948 	case o_print_version:
1949 	    PrintVersion(dialog_state.output);
1950 	    break;
1951 	case o_dlg_clear_screen:
1952 	    initscr();
1953 	    refresh();
1954 	    dlg_keep_tite((dialog_state.output == stdout) ? stderr : stdout);
1955 	    endwin();
1956 	    break;
1957 	case o_ignore:
1958 	    break;
1959 	default:
1960 	    Help();
1961 	    break;
1962 	}
1963 	dlg_exit(DLG_EXIT_OK);
1964     } else if (argc < 2) {
1965 	Help();
1966     }
1967 #ifdef HAVE_RC_FILE
1968     else if (lookupOption(argv[1], 7) == o_create_rc) {
1969 	if (argc != 3) {
1970 	    sprintf(temp, _("Expected a filename for %.*s"), USEMAX_FILE,
1971 		    argv[1]);
1972 	    Usage(temp);
1973 	}
1974 #ifdef HAVE_COLOR
1975 	dialog_state.use_colors = USE_COLORS;	/* use colors by default? */
1976 	dialog_state.use_shadow = USE_SHADOW;	/* shadow dialog boxes by default? */
1977 #endif
1978 	if (dlg_parse_rc() == -1) {	/* Read the configuration file */
1979 	    handle_leaks();
1980 	    dlg_exiterr("dialog: dlg_parse_rc");
1981 	}
1982 	dlg_create_rc(argv[2]);
1983 	dlg_exit(DLG_EXIT_OK);
1984     }
1985 #endif /* HAVE_RC_FILE */
1986     else {
1987 	/*
1988 	 * Handle combinations of common options including --print-text-only
1989 	 * which can be done before involving curses, in case we can exit
1990 	 * without initializing curses (and writing to the terminal).
1991 	 */
1992 	initial = process_common_options(argc, argv, offset, TRUE);
1993 	if (initial >= argc)
1994 	    dlg_exit(DLG_EXIT_OK);
1995     }
1996 
1997     init_dialog(dialog_state.input, dialog_state.output);
1998 
1999     while (offset < argc && !esc_pressed) {
2000 	int have;
2001 	const Mode *modePtr;
2002 
2003 	init_result(my_buffer);
2004 	offset = process_common_options(argc, argv, offset, offset > initial);
2005 
2006 	if (argv[offset] == NULL) {
2007 	    if (ignore_unknown)
2008 		break;
2009 	    Usage(_("Expected a box option"));
2010 	}
2011 
2012 	if (dialog_vars.separate_output) {
2013 	    switch (lookupOption(argv[offset], 2)) {
2014 #ifdef HAVE_XDIALOG2
2015 	    case o_buildlist:
2016 	    case o_treeview:
2017 #endif
2018 	    case o_checklist:
2019 		break;
2020 	    default:
2021 		sprintf(temp,
2022 			_("Unexpected widget with --separate-output %.*s"),
2023 			USEMAX_ARGS, argv[offset]);
2024 		Usage(temp);
2025 	    }
2026 	}
2027 
2028 	dlg_put_backtitle();
2029 
2030 	/* use a table to look for the requested mode, to avoid code duplication */
2031 
2032 	modePtr = 0;
2033 	if ((code = lookupOption(argv[offset], 2)) != o_unknown)
2034 	    modePtr = lookupMode(code);
2035 	if (modePtr == 0) {
2036 	    sprintf(temp,
2037 		    (lookupOption(argv[offset], 7) != o_unknown
2038 		     ? _("Unexpected option %.*s")
2039 		     : _("Unknown option %.*s")),
2040 		    USEMAX_ARGS,
2041 		    argv[offset]);
2042 	    Usage(temp);
2043 	}
2044 
2045 	have = arg_rest(&argv[offset]);
2046 	if (have < modePtr->argmin) {
2047 	    sprintf(temp, _("Expected at least %d tokens for %.*s, have %d"),
2048 		    USEMAX_ARGS,
2049 		    modePtr->argmin - 1, argv[offset],
2050 		    have - 1);
2051 	    Usage(temp);
2052 	}
2053 	if (modePtr->argmax && have > modePtr->argmax) {
2054 	    sprintf(temp,
2055 		    _("Expected no more than %d tokens for %.*s, have %d"),
2056 		    USEMAX_ARGS,
2057 		    modePtr->argmax - 1, argv[offset],
2058 		    have - 1);
2059 	    Usage(temp);
2060 	}
2061 
2062 	/*
2063 	 * Trim whitespace from non-title option values, e.g., the ones that
2064 	 * will be used as captions or prompts.   Do that only for the widget
2065 	 * we are about to process, since the "--trim" option is reset before
2066 	 * accumulating options for each widget.
2067 	 */
2068 	for (j = offset + 1; j <= offset + have; j++) {
2069 	    switch (lookupOption(argv[j - 1], 7)) {
2070 	    case o_unknown:
2071 	    case o_title:
2072 	    case o_backtitle:
2073 	    case o_help_line:
2074 	    case o_help_file:
2075 		break;
2076 	    default:
2077 		if (argv[j] != 0) {
2078 		    char *argv_j = strdup(argv[j]);
2079 		    if (argv_j != 0) {
2080 			dlg_trim_string(argv_j);
2081 			argv[j] = argv_j;
2082 		    } else {
2083 			argv[j] = strdup("?");
2084 		    }
2085 		    ignore_leak(argv[j]);
2086 		}
2087 		break;
2088 	    }
2089 	}
2090 
2091 	DLG_TRACE(("# execute %s\n", argv[offset]));
2092 	retval = show_result((*(modePtr->jumper)) (dialog_vars.title,
2093 						   argv + offset,
2094 						   &offset_add));
2095 	DLG_TRACE(("# widget returns %d\n", retval));
2096 	offset += offset_add;
2097 
2098 	if (dialog_vars.input_result != my_buffer) {
2099 	    free(dialog_vars.input_result);
2100 	    dialog_vars.input_result = 0;
2101 	}
2102 
2103 	if (retval == DLG_EXIT_ESC) {
2104 	    esc_pressed = TRUE;
2105 	} else {
2106 
2107 	    if (dialog_vars.beep_after_signal)
2108 		(void) beep();
2109 
2110 	    if (dialog_vars.sleep_secs)
2111 		(void) napms(dialog_vars.sleep_secs * 1000);
2112 
2113 	    if (offset < argc) {
2114 		switch (lookupOption(argv[offset], 7)) {
2115 		case o_and_widget:
2116 		    offset++;
2117 		    break;
2118 		case o_unknown:
2119 		    sprintf(temp, _("Expected --and-widget, not %.*s"),
2120 			    USEMAX_ARGS, argv[offset]);
2121 		    Usage(temp);
2122 		    break;
2123 		default:
2124 		    /* if we got a cancel, etc., stop chaining */
2125 		    if (retval != DLG_EXIT_OK)
2126 			esc_pressed = TRUE;
2127 		    else
2128 			dialog_vars.dlg_clear_screen = TRUE;
2129 		    break;
2130 		}
2131 	    }
2132 	    if (dialog_vars.dlg_clear_screen)
2133 		dlg_clear();
2134 	}
2135     }
2136 
2137     dlg_killall_bg(&retval);
2138     if (dialog_state.screen_initialized) {
2139 	(void) refresh();
2140 	end_dialog();
2141     }
2142     handle_leaks();
2143     dlg_exit(retval);
2144 }
2145