xref: /freebsd/contrib/dialog/dialog.c (revision ec74116a)
14c8945a0SNathan Whitehorn /*
2a96ef450SBaptiste Daroussin  * $Id: dialog.c,v 1.281 2021/01/17 16:52:18 tom Exp $
34c8945a0SNathan Whitehorn  *
44c8945a0SNathan Whitehorn  *  cdialog - Display simple dialog boxes from shell scripts
54c8945a0SNathan Whitehorn  *
6a96ef450SBaptiste Daroussin  *  Copyright 2000-2020,2021	Thomas E. Dickey
74c8945a0SNathan Whitehorn  *
84c8945a0SNathan Whitehorn  *  This program is free software; you can redistribute it and/or modify
94c8945a0SNathan Whitehorn  *  it under the terms of the GNU Lesser General Public License, version 2.1
104c8945a0SNathan Whitehorn  *  as published by the Free Software Foundation.
114c8945a0SNathan Whitehorn  *
124c8945a0SNathan Whitehorn  *  This program is distributed in the hope that it will be useful, but
134c8945a0SNathan Whitehorn  *  WITHOUT ANY WARRANTY; without even the implied warranty of
144c8945a0SNathan Whitehorn  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
154c8945a0SNathan Whitehorn  *  Lesser General Public License for more details.
164c8945a0SNathan Whitehorn  *
174c8945a0SNathan Whitehorn  *  You should have received a copy of the GNU Lesser General Public
184c8945a0SNathan Whitehorn  *  License along with this program; if not, write to
194c8945a0SNathan Whitehorn  *	Free Software Foundation, Inc.
204c8945a0SNathan Whitehorn  *	51 Franklin St., Fifth Floor
214c8945a0SNathan Whitehorn  *	Boston, MA 02110, USA.
224c8945a0SNathan Whitehorn  *
234c8945a0SNathan Whitehorn  *  An earlier version of this program lists as authors
244c8945a0SNathan Whitehorn  *	Savio Lam (lam836@cs.cuhk.hk)
254c8945a0SNathan Whitehorn  */
264c8945a0SNathan Whitehorn 
274c8945a0SNathan Whitehorn #include <dialog.h>
28a96ef450SBaptiste Daroussin 
29ec74116aSBaptiste Daroussin #include <stddef.h>
304c8945a0SNathan Whitehorn #include <string.h>
314c8945a0SNathan Whitehorn #include <sys/types.h>
324c8945a0SNathan Whitehorn #include <sys/stat.h>
334c8945a0SNathan Whitehorn 
344c8945a0SNathan Whitehorn #ifdef HAVE_SETLOCALE
354c8945a0SNathan Whitehorn #include <locale.h>
364c8945a0SNathan Whitehorn #endif
374c8945a0SNathan Whitehorn 
38a96ef450SBaptiste Daroussin #include <dlg_internals.h>
39a96ef450SBaptiste Daroussin 
404c8945a0SNathan Whitehorn #define PASSARGS             t,       av,        offset_add
414c8945a0SNathan Whitehorn #define CALLARGS const char *t, char *av[], int *offset_add
424c8945a0SNathan Whitehorn typedef int (callerFn) (CALLARGS);
434c8945a0SNathan Whitehorn 
444c8945a0SNathan Whitehorn typedef enum {
454c8945a0SNathan Whitehorn     o_unknown = 0
464c8945a0SNathan Whitehorn     ,o_allow_close
474c8945a0SNathan Whitehorn     ,o_and_widget
484c8945a0SNathan Whitehorn     ,o_ascii_lines
49a96ef450SBaptiste Daroussin     ,o_aspect_ratio
504c8945a0SNathan Whitehorn     ,o_auto_placement
514c8945a0SNathan Whitehorn     ,o_backtitle
52a96ef450SBaptiste Daroussin     ,o_beep_signal
53a96ef450SBaptiste Daroussin     ,o_beep_after_signal
54a96ef450SBaptiste Daroussin     ,o_begin_set
554c8945a0SNathan Whitehorn     ,o_cancel_label
564c8945a0SNathan Whitehorn     ,o_checklist
57a96ef450SBaptiste Daroussin     ,o_dlg_clear_screen
584c8945a0SNathan Whitehorn     ,o_colors
594c8945a0SNathan Whitehorn     ,o_column_separator
604c8945a0SNathan Whitehorn     ,o_cr_wrap
614c8945a0SNathan Whitehorn     ,o_create_rc
62a96ef450SBaptiste Daroussin     ,o_cursor_off_label
634c8945a0SNathan Whitehorn     ,o_date_format
642a3e3873SBaptiste Daroussin     ,o_default_button
654c8945a0SNathan Whitehorn     ,o_default_item
664c8945a0SNathan Whitehorn     ,o_defaultno
67a96ef450SBaptiste Daroussin     ,o_erase_on_exit
684c8945a0SNathan Whitehorn     ,o_exit_label
694c8945a0SNathan Whitehorn     ,o_extra_button
704c8945a0SNathan Whitehorn     ,o_extra_label
714c8945a0SNathan Whitehorn     ,o_fixed_font
724c8945a0SNathan Whitehorn     ,o_form
734c8945a0SNathan Whitehorn     ,o_gauge
744c8945a0SNathan Whitehorn     ,o_help
754c8945a0SNathan Whitehorn     ,o_help_button
76682c9e0fSNathan Whitehorn     ,o_help_file
774c8945a0SNathan Whitehorn     ,o_help_label
78682c9e0fSNathan Whitehorn     ,o_help_line
794c8945a0SNathan Whitehorn     ,o_help_status
80febdb468SDevin Teske     ,o_help_tags
814c8945a0SNathan Whitehorn     ,o_icon
824c8945a0SNathan Whitehorn     ,o_ignore
834c8945a0SNathan Whitehorn     ,o_infobox
844c8945a0SNathan Whitehorn     ,o_input_fd
854c8945a0SNathan Whitehorn     ,o_inputbox
864c8945a0SNathan Whitehorn     ,o_inputmenu
874c8945a0SNathan Whitehorn     ,o_insecure
884c8945a0SNathan Whitehorn     ,o_item_help
894c8945a0SNathan Whitehorn     ,o_keep_colors
904c8945a0SNathan Whitehorn     ,o_keep_tite
914c8945a0SNathan Whitehorn     ,o_keep_window
922a3e3873SBaptiste Daroussin     ,o_last_key
934c8945a0SNathan Whitehorn     ,o_max_input
944c8945a0SNathan Whitehorn     ,o_menu
954c8945a0SNathan Whitehorn     ,o_mixedform
964c8945a0SNathan Whitehorn     ,o_mixedgauge
974c8945a0SNathan Whitehorn     ,o_msgbox
984c8945a0SNathan Whitehorn     ,o_no_close
99a96ef450SBaptiste Daroussin     ,o_nocollapse
1004c8945a0SNathan Whitehorn     ,o_no_cr_wrap
101a96ef450SBaptiste Daroussin     ,o_cant_kill
102a96ef450SBaptiste Daroussin     ,o_no_hot_list
1034c8945a0SNathan Whitehorn     ,o_no_label
1044c8945a0SNathan Whitehorn     ,o_no_lines
1057a1c0d96SNathan Whitehorn     ,o_no_mouse
106682c9e0fSNathan Whitehorn     ,o_no_nl_expand
107a96ef450SBaptiste Daroussin     ,o_use_shadow
1084c8945a0SNathan Whitehorn     ,o_nocancel
1094c8945a0SNathan Whitehorn     ,o_nook
1104c8945a0SNathan Whitehorn     ,o_ok_label
1114c8945a0SNathan Whitehorn     ,o_output_fd
1124c8945a0SNathan Whitehorn     ,o_output_separator
1134c8945a0SNathan Whitehorn     ,o_passwordbox
1144c8945a0SNathan Whitehorn     ,o_passwordform
1154c8945a0SNathan Whitehorn     ,o_pause
1167a1c0d96SNathan Whitehorn     ,o_prgbox
1174c8945a0SNathan Whitehorn     ,o_print_maxsize
118a96ef450SBaptiste Daroussin     ,o_print_siz
119a96ef450SBaptiste Daroussin     ,o_text_only
120f4f33ea0SBaptiste Daroussin     ,o_print_text_size
1214c8945a0SNathan Whitehorn     ,o_print_version
1227a1c0d96SNathan Whitehorn     ,o_programbox
1234c8945a0SNathan Whitehorn     ,o_progressbox
1244c8945a0SNathan Whitehorn     ,o_quoted
1254c8945a0SNathan Whitehorn     ,o_radiolist
1264c8945a0SNathan Whitehorn     ,o_screen_center
127a96ef450SBaptiste Daroussin     ,o_use_scrollbar
1284c8945a0SNathan Whitehorn     ,o_separate_output
129a96ef450SBaptiste Daroussin     ,o_separate_str
1304c8945a0SNathan Whitehorn     ,o_single_quoted
1314c8945a0SNathan Whitehorn     ,o_size_err
132a96ef450SBaptiste Daroussin     ,o_sleep_secs
1334c8945a0SNathan Whitehorn     ,o_smooth
134a96ef450SBaptiste Daroussin     ,o_output_stderr
135a96ef450SBaptiste Daroussin     ,o_output_stdout
1364c8945a0SNathan Whitehorn     ,o_tab_correct
1374c8945a0SNathan Whitehorn     ,o_tab_len
1384c8945a0SNathan Whitehorn     ,o_tailbox
1394c8945a0SNathan Whitehorn     ,o_tailboxbg
1404c8945a0SNathan Whitehorn     ,o_textbox
1414c8945a0SNathan Whitehorn     ,o_time_format
142a96ef450SBaptiste Daroussin     ,o_timeout_secs
1434c8945a0SNathan Whitehorn     ,o_title
144a96ef450SBaptiste Daroussin     ,o_trim_whitespace
1454c8945a0SNathan Whitehorn     ,o_under_mouse
146682c9e0fSNathan Whitehorn     ,o_version
1474c8945a0SNathan Whitehorn     ,o_visit_items
1484c8945a0SNathan Whitehorn     ,o_wmclass
1494c8945a0SNathan Whitehorn     ,o_yes_label
1504c8945a0SNathan Whitehorn     ,o_yesno
1512a3e3873SBaptiste Daroussin #ifdef HAVE_WHIPTAIL
1522a3e3873SBaptiste Daroussin     ,o_fullbutton
1532a3e3873SBaptiste Daroussin     ,o_topleft
1542a3e3873SBaptiste Daroussin #endif
1552a3e3873SBaptiste Daroussin #ifdef HAVE_XDIALOG
1562a3e3873SBaptiste Daroussin     ,o_calendar
1572a3e3873SBaptiste Daroussin     ,o_dselect
1582a3e3873SBaptiste Daroussin     ,o_editbox
1592a3e3873SBaptiste Daroussin     ,o_fselect
1602a3e3873SBaptiste Daroussin     ,o_timebox
161f4f33ea0SBaptiste Daroussin     ,o_week_start
1622a3e3873SBaptiste Daroussin #endif
1632a3e3873SBaptiste Daroussin #ifdef HAVE_XDIALOG2
1642a3e3873SBaptiste Daroussin     ,o_buildlist
1652a3e3873SBaptiste Daroussin     ,o_rangebox
166f4f33ea0SBaptiste Daroussin     ,o_reorder
1672a3e3873SBaptiste Daroussin     ,o_treeview
1682a3e3873SBaptiste Daroussin #endif
1692a3e3873SBaptiste Daroussin #if defined(HAVE_XDIALOG2) || defined(HAVE_WHIPTAIL)
1702a3e3873SBaptiste Daroussin     ,o_no_items
1712a3e3873SBaptiste Daroussin     ,o_no_tags
1722a3e3873SBaptiste Daroussin #endif
1734c8945a0SNathan Whitehorn #ifdef HAVE_DLG_TRACE
1744c8945a0SNathan Whitehorn     ,o_trace
1754c8945a0SNathan Whitehorn #endif
176f4f33ea0SBaptiste Daroussin     ,o_iso_week
1774c8945a0SNathan Whitehorn } eOptions;
1784c8945a0SNathan Whitehorn 
179a96ef450SBaptiste Daroussin typedef enum {
180a96ef450SBaptiste Daroussin     tUnknown
181a96ef450SBaptiste Daroussin     ,tFalse
182a96ef450SBaptiste Daroussin     ,tTrue
183a96ef450SBaptiste Daroussin     ,tNumber
184a96ef450SBaptiste Daroussin     ,tString
185a96ef450SBaptiste Daroussin } tOptions;
186a96ef450SBaptiste Daroussin 
1874c8945a0SNathan Whitehorn /*
1884c8945a0SNathan Whitehorn  * The bits in 'pass' are used to decide which options are applicable at
1894c8945a0SNathan Whitehorn  * different stages in the program:
1904c8945a0SNathan Whitehorn  *	1 flags before widgets
1914c8945a0SNathan Whitehorn  *	2 widgets
1924c8945a0SNathan Whitehorn  *	4 non-widget options
1934c8945a0SNathan Whitehorn  */
1944c8945a0SNathan Whitehorn typedef struct {
1954c8945a0SNathan Whitehorn     const char *name;
1964c8945a0SNathan Whitehorn     eOptions code;
197a96ef450SBaptiste Daroussin     int vars;			/* 0=none, 1=state, 2=vars */
198a96ef450SBaptiste Daroussin     tOptions type;		/* type for bool(true/false), number, string */
199a96ef450SBaptiste Daroussin     unsigned offset;
2004c8945a0SNathan Whitehorn     int pass;			/* 1,2,4 or combination */
2014c8945a0SNathan Whitehorn     const char *help;		/* NULL to suppress, non-empty to display params */
2024c8945a0SNathan Whitehorn } Options;
2034c8945a0SNathan Whitehorn 
2044c8945a0SNathan Whitehorn typedef struct {
2054c8945a0SNathan Whitehorn     eOptions code;
2064c8945a0SNathan Whitehorn     int argmin, argmax;
2074c8945a0SNathan Whitehorn     callerFn *jumper;
2084c8945a0SNathan Whitehorn } Mode;
2094c8945a0SNathan Whitehorn 
210a96ef450SBaptiste Daroussin /* use these macros for simple options in DIALOG_STATE */
211a96ef450SBaptiste Daroussin #define ssF(name) o_##name, 1, tFalse,  offsetof(DIALOG_STATE,name)
212a96ef450SBaptiste Daroussin #define ssT(name) o_##name, 1, tTrue,   offsetof(DIALOG_STATE,name)
213a96ef450SBaptiste Daroussin #define ssN(name) o_##name, 1, tNumber, offsetof(DIALOG_STATE,name)
214a96ef450SBaptiste Daroussin #define ssS(name) o_##name, 1, tString, offsetof(DIALOG_STATE,name)
215a96ef450SBaptiste Daroussin 
216a96ef450SBaptiste Daroussin /* use these macros for simple options in DIALOG_VARS */
217a96ef450SBaptiste Daroussin #define svF(name) o_##name, 2, tFalse,  offsetof(DIALOG_VARS,name)
218a96ef450SBaptiste Daroussin #define svT(name) o_##name, 2, tTrue,   offsetof(DIALOG_VARS,name)
219a96ef450SBaptiste Daroussin #define svN(name) o_##name, 2, tNumber, offsetof(DIALOG_VARS,name)
220a96ef450SBaptiste Daroussin #define svS(name) o_##name, 2, tString, offsetof(DIALOG_VARS,name)
221a96ef450SBaptiste Daroussin 
222a96ef450SBaptiste Daroussin /* use these macros for ignored options */
223a96ef450SBaptiste Daroussin #define xxF(name) o_##name, 0, tFalse,  0
224a96ef450SBaptiste Daroussin #define xxT(name) o_##name, 0, tTrue,   0
225a96ef450SBaptiste Daroussin #define xxN(name) o_##name, 0, tNumber, 0
226a96ef450SBaptiste Daroussin #define xxS(name) o_##name, 0, tString, 0
227a96ef450SBaptiste Daroussin 
228a96ef450SBaptiste Daroussin /* use this macro for widget options */
229a96ef450SBaptiste Daroussin #define opW(name) o_##name, 0, 0, 0
230a96ef450SBaptiste Daroussin 
231a96ef450SBaptiste Daroussin /* use this macro for other options */
232a96ef450SBaptiste Daroussin #define opO(name) o_##name, 0, 0, 0
233a96ef450SBaptiste Daroussin 
234f4f33ea0SBaptiste Daroussin static int known_opts = 0;
235f4f33ea0SBaptiste Daroussin static const char **dialog_opts;
2364c8945a0SNathan Whitehorn static char **dialog_argv;
2374c8945a0SNathan Whitehorn 
238f4f33ea0SBaptiste Daroussin static char **special_argv = 0;
239f4f33ea0SBaptiste Daroussin static int special_argc = 0;
240f4f33ea0SBaptiste Daroussin 
2414c8945a0SNathan Whitehorn static bool ignore_unknown = FALSE;
2424c8945a0SNathan Whitehorn 
2434c8945a0SNathan Whitehorn static const char *program = "dialog";
2444c8945a0SNathan Whitehorn 
245f4f33ea0SBaptiste Daroussin #ifdef NO_LEAKS
246f4f33ea0SBaptiste Daroussin typedef struct _all_blobs {
247f4f33ea0SBaptiste Daroussin     struct _all_blobs *next;
248f4f33ea0SBaptiste Daroussin     void *blob;
249f4f33ea0SBaptiste Daroussin } AllBlobs;
250f4f33ea0SBaptiste Daroussin 
251f4f33ea0SBaptiste Daroussin static AllBlobs *all_blobs;
252f4f33ea0SBaptiste Daroussin #endif
253f4f33ea0SBaptiste Daroussin 
2544c8945a0SNathan Whitehorn /*
2554c8945a0SNathan Whitehorn  * The options[] table is organized this way to make it simple to maintain
2564c8945a0SNathan Whitehorn  * a sorted list of options for the help-message.
257a96ef450SBaptiste Daroussin  *
258a96ef450SBaptiste Daroussin  * Because Boolean options correspond to "true", --shadow is listed here while
259a96ef450SBaptiste Daroussin  * --no-shadow is not.  The findOption and optionBool functions handle the
260a96ef450SBaptiste Daroussin  * cases where "no" is added or removed from the option name to derive an
261a96ef450SBaptiste Daroussin  * opposite setting.
2624c8945a0SNathan Whitehorn  */
2634c8945a0SNathan Whitehorn /* *INDENT-OFF* */
2644c8945a0SNathan Whitehorn static const Options options[] = {
265a96ef450SBaptiste Daroussin     { "allow-close",	xxT(allow_close),	1, NULL },
266a96ef450SBaptiste Daroussin     { "and-widget",	opO(and_widget),	4, NULL },
267a96ef450SBaptiste Daroussin     { "ascii-lines",	svT(ascii_lines), 	1, "" },
268a96ef450SBaptiste Daroussin     { "aspect",		ssN(aspect_ratio),	1, "<ratio>" },
269a96ef450SBaptiste Daroussin     { "auto-placement", xxT(auto_placement),	1, NULL },
270a96ef450SBaptiste Daroussin     { "backtitle",	svS(backtitle),		1, "<backtitle>" },
271a96ef450SBaptiste Daroussin     { "beep",		svT(beep_signal),	1, "" },
272a96ef450SBaptiste Daroussin     { "beep-after",	svT(beep_after_signal),	1, "" },
273a96ef450SBaptiste Daroussin     { "begin",		svT(begin_set),		1, "<y> <x>" },
274a96ef450SBaptiste Daroussin     { "cancel-label",	svS(cancel_label),	1, "<str>" },
275a96ef450SBaptiste Daroussin     { "checklist",	opW(checklist),		2, "<text> <height> <width> <list height> <tag1> <item1> <status1>..." },
276a96ef450SBaptiste Daroussin     { "clear",		svT(dlg_clear_screen),	1, "" },
277a96ef450SBaptiste Daroussin     { "colors",		svT(colors),		1, "" },
278a96ef450SBaptiste Daroussin     { "column-separator",svS(column_separator),	1, "<str>" },
279a96ef450SBaptiste Daroussin     { "cr-wrap",	svT(cr_wrap),		1, "" },
280a96ef450SBaptiste Daroussin     { "create-rc",	opO(create_rc),		1, NULL },
281a96ef450SBaptiste Daroussin     { "cursor-off-label",svT(cursor_off_label),	1, "" },
282a96ef450SBaptiste Daroussin     { "date-format",	svS(date_format),	1, "<str>" },
283a96ef450SBaptiste Daroussin     { "default-button",	xxS(default_button),	1, "<str>" },
284a96ef450SBaptiste Daroussin     { "default-item",	svS(default_item),	1, "<str>" },
285a96ef450SBaptiste Daroussin     { "defaultno",	svT(defaultno),		1, "" },
286a96ef450SBaptiste Daroussin     { "erase-on-exit",	svT(erase_on_exit),	1, "" },
287a96ef450SBaptiste Daroussin     { "exit-label",	svS(exit_label),	1, "<str>" },
288a96ef450SBaptiste Daroussin     { "extra-button",	svT(extra_button),	1, "" },
289a96ef450SBaptiste Daroussin     { "extra-label",	svS(extra_label),	1, "<str>" },
290a96ef450SBaptiste Daroussin     { "fixed-font",	xxT(fixed_font),	1, NULL },
291a96ef450SBaptiste Daroussin     { "form",		opW(form),		2, "<text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1>..." },
292a96ef450SBaptiste Daroussin     { "gauge",		opW(gauge),		2, "<text> <height> <width> [<percent>]" },
293a96ef450SBaptiste Daroussin     { "guage",		opW(gauge),		2, NULL },
294a96ef450SBaptiste Daroussin     { "help",		opO(help),		4, "" },
295a96ef450SBaptiste Daroussin     { "help-button",	svT(help_button),	1, "" },
296a96ef450SBaptiste Daroussin     { "help-label",	svS(help_label),	1, "<str>" },
297a96ef450SBaptiste Daroussin     { "help-status",	svT(help_status),	1, "" },
298a96ef450SBaptiste Daroussin     { "help-tags",	svT(help_tags),		1, "" },
299a96ef450SBaptiste Daroussin     { "hfile",		svS(help_file),		1, "<str>" },
300a96ef450SBaptiste Daroussin     { "hline",		svS(help_line),		1, "<str>" },
301a96ef450SBaptiste Daroussin     { "icon",		xxS(icon),		1, NULL },
302a96ef450SBaptiste Daroussin     { "ignore",		opO(ignore),		1, "" },
303a96ef450SBaptiste Daroussin     { "infobox",	opW(infobox),		2, "<text> <height> <width>" },
304a96ef450SBaptiste Daroussin     { "input-fd",	opO(input_fd),		1, "<fd>" },
305a96ef450SBaptiste Daroussin     { "inputbox",	opW(inputbox),		2, "<text> <height> <width> [<init>]" },
306a96ef450SBaptiste Daroussin     { "inputmenu",	opW(inputmenu),		2, "<text> <height> <width> <menu height> <tag1> <item1>..." },
307a96ef450SBaptiste Daroussin     { "insecure",	svT(insecure),		1, "" },
308a96ef450SBaptiste Daroussin     { "item-help",	svT(item_help),		1, "" },
309a96ef450SBaptiste Daroussin     { "keep-colors",	xxT(keep_colors),	1, NULL },
310a96ef450SBaptiste Daroussin     { "keep-tite",	svT(keep_tite),		1, "" },
311a96ef450SBaptiste Daroussin     { "keep-window",	svT(keep_window),	1, "" },
312a96ef450SBaptiste Daroussin     { "last-key",	svT(last_key),		1, "" },
313a96ef450SBaptiste Daroussin     { "max-input",	svN(max_input),		1, "<n>" },
314a96ef450SBaptiste Daroussin     { "menu",		opW(menu),		2, "<text> <height> <width> <menu height> <tag1> <item1>..." },
315a96ef450SBaptiste Daroussin     { "mixedform",	opW(mixedform),		2, "<text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1> <itype>..." },
316a96ef450SBaptiste Daroussin     { "mixedgauge",	opW(mixedgauge),	2, "<text> <height> <width> <percent> <tag1> <item1>..." },
317a96ef450SBaptiste Daroussin     { "msgbox",		opW(msgbox),		2, "<text> <height> <width>" },
318a96ef450SBaptiste Daroussin     { "no-cancel",	svT(nocancel),		1, "" },
319a96ef450SBaptiste Daroussin     { "no-close",	xxT(no_close),		1, NULL },
320a96ef450SBaptiste Daroussin     { "no-collapse",	svT(nocollapse),	1, "" },
321a96ef450SBaptiste Daroussin     { "no-hot-list",	svT(no_hot_list),	1, "" },
322a96ef450SBaptiste Daroussin     { "no-kill",	svT(cant_kill),		1, "" },
323a96ef450SBaptiste Daroussin     { "no-label",	svS(no_label),		1, "<str>" },
324a96ef450SBaptiste Daroussin     { "no-lines",	svT(no_lines), 		1, "" },
325a96ef450SBaptiste Daroussin     { "no-mouse",	ssT(no_mouse),		1, "" },
326a96ef450SBaptiste Daroussin     { "no-nl-expand",	svT(no_nl_expand),	1, "" },
327a96ef450SBaptiste Daroussin     { "no-ok",		svT(nook),		1, "" },
328a96ef450SBaptiste Daroussin     { "no-shadow",	ssF(use_shadow),	1, "" },
329a96ef450SBaptiste Daroussin     { "ok-label",	svS(ok_label),		1, "<str>" },
330a96ef450SBaptiste Daroussin     { "output-fd",	opO(output_fd),		1, "<fd>" },
331a96ef450SBaptiste Daroussin     { "output-separator",svS(output_separator),	1, "<str>" },
332a96ef450SBaptiste Daroussin     { "passwordbox",	opW(passwordbox),	2, "<text> <height> <width> [<init>]" },
333a96ef450SBaptiste Daroussin     { "passwordform",	opW(passwordform),	2, "<text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1>..." },
334a96ef450SBaptiste Daroussin     { "pause",		opW(pause),		2, "<text> <height> <width> <seconds>" },
335a96ef450SBaptiste Daroussin     { "prgbox",		opW(prgbox),		2, "<text> <command> <height> <width>" },
336a96ef450SBaptiste Daroussin     { "print-maxsize",	opO(print_maxsize),	1, "" },
337a96ef450SBaptiste Daroussin     { "print-size",	svT(print_siz),		1, "" },
338a96ef450SBaptiste Daroussin     { "print-text-only",ssT(text_only),		5, "<text> <height> <width>" },
339a96ef450SBaptiste Daroussin     { "print-text-size",opO(print_text_size),	5, "<text> <height> <width>" },
340a96ef450SBaptiste Daroussin     { "print-version",	opO(print_version),	5, "" },
341a96ef450SBaptiste Daroussin     { "programbox",	opW(programbox),	2, "<text> <height> <width>" },
342a96ef450SBaptiste Daroussin     { "progressbox",	opW(progressbox),	2, "<text> <height> <width>" },
343a96ef450SBaptiste Daroussin     { "quoted",		svT(quoted),		1, "" },
344a96ef450SBaptiste Daroussin     { "radiolist",	opW(radiolist),		2, "<text> <height> <width> <list height> <tag1> <item1> <status1>..." },
345a96ef450SBaptiste Daroussin     { "screen-center",	xxT(screen_center),	1, NULL },
346a96ef450SBaptiste Daroussin     { "scrollbar",	ssT(use_scrollbar),	1, "" },
347a96ef450SBaptiste Daroussin     { "separate-output",svT(separate_output),	1, "" },
348a96ef450SBaptiste Daroussin     { "separate-widget",ssS(separate_str),	1, "<str>" },
349a96ef450SBaptiste Daroussin     { "separator",	svS(output_separator),	1, NULL },
350a96ef450SBaptiste Daroussin     { "single-quoted",	svT(single_quoted),	1, "" },
351a96ef450SBaptiste Daroussin     { "size-err",	svT(size_err),		1, "" },
352a96ef450SBaptiste Daroussin     { "sleep",		svN(sleep_secs),	1, "<secs>" },
353a96ef450SBaptiste Daroussin     { "smooth",		xxT(smooth),		1, NULL },
354a96ef450SBaptiste Daroussin     { "stderr",		opO(output_stderr),	1, "" },
355a96ef450SBaptiste Daroussin     { "stdout",		opO(output_stdout),	1, "" },
356a96ef450SBaptiste Daroussin     { "tab-correct",	svT(tab_correct),	1, "" },
357a96ef450SBaptiste Daroussin     { "tab-len",	ssN(tab_len),		1, "<n>" },
358a96ef450SBaptiste Daroussin     { "tailbox",	opW(tailbox),		2, "<file> <height> <width>" },
359a96ef450SBaptiste Daroussin     { "tailboxbg",	opW(tailboxbg),		2, "<file> <height> <width>" },
360a96ef450SBaptiste Daroussin     { "textbox",	opW(textbox),		2, "<file> <height> <width>" },
361a96ef450SBaptiste Daroussin     { "time-format",	svS(time_format),	1, "<str>" },
362a96ef450SBaptiste Daroussin     { "timeout",	svN(timeout_secs),	1, "<secs>" },
363a96ef450SBaptiste Daroussin     { "title",		svS(title),		1, "<title>" },
364a96ef450SBaptiste Daroussin     { "trim",		svT(trim_whitespace),	1, "" },
365a96ef450SBaptiste Daroussin     { "under-mouse", 	xxT(under_mouse),	1, NULL },
366a96ef450SBaptiste Daroussin     { "version",	opO(version),		5, "" },
367a96ef450SBaptiste Daroussin     { "visit-items", 	ssT(visit_items),	1, "" },
368a96ef450SBaptiste Daroussin     { "wmclass",	xxS(wmclass),		1, NULL },
369a96ef450SBaptiste Daroussin     { "yes-label",	svS(yes_label),		1, "<str>" },
370a96ef450SBaptiste Daroussin     { "yesno",		opW(yesno),		2, "<text> <height> <width>" },
3712a3e3873SBaptiste Daroussin #ifdef HAVE_WHIPTAIL
372a96ef450SBaptiste Daroussin     { "cancel-button",	svS(cancel_label),	1, NULL },
373a96ef450SBaptiste Daroussin     { "fb",		xxT(fullbutton),	1, NULL },
374a96ef450SBaptiste Daroussin     { "fullbutton",	xxT(fullbutton),	1, NULL },
375a96ef450SBaptiste Daroussin     { "no-button",	svS(no_label),		1, NULL },
376a96ef450SBaptiste Daroussin     { "ok-button",	svS(ok_label),		1, NULL },
377a96ef450SBaptiste Daroussin     { "scrolltext",	ssT(use_scrollbar),	1, NULL },
378a96ef450SBaptiste Daroussin     { "topleft",	svT(begin_set),		1, NULL },
379a96ef450SBaptiste Daroussin     { "yes-button",	svS(yes_label),		1, NULL },
3802a3e3873SBaptiste Daroussin #endif
3812a3e3873SBaptiste Daroussin #ifdef HAVE_XDIALOG
382a96ef450SBaptiste Daroussin     { "calendar",	opW(calendar),		2, "<text> <height> <width> <day> <month> <year>" },
383a96ef450SBaptiste Daroussin     { "dselect",	opW(dselect),		2, "<directory> <height> <width>" },
384a96ef450SBaptiste Daroussin     { "editbox",	opW(editbox),		2, "<file> <height> <width>" },
385a96ef450SBaptiste Daroussin     { "fselect",	opW(fselect),		2, "<filepath> <height> <width>" },
386a96ef450SBaptiste Daroussin     { "timebox",	opW(timebox),		2, "<text> <height> <width> <hour> <minute> <second>" },
387a96ef450SBaptiste Daroussin     { "week-start",	svS(week_start),	1, "<str>" },
388a96ef450SBaptiste Daroussin     { "iso-week",	svT(iso_week),		1, NULL },
3892a3e3873SBaptiste Daroussin #endif
3902a3e3873SBaptiste Daroussin #ifdef HAVE_XDIALOG2
391a96ef450SBaptiste Daroussin     { "buildlist",	opW(buildlist),		2, "<text> <height> <width> <list-height> <tag1> <item1> <status1>..." },
392a96ef450SBaptiste Daroussin     { "no-items", 	svT(no_items),		1, "" },
393a96ef450SBaptiste Daroussin     { "no-tags", 	svT(no_tags),		1, "" },
394a96ef450SBaptiste Daroussin     { "rangebox",	opW(rangebox),		2, "<text> <height> <width> <min-value> <max-value> <default-value>" },
395a96ef450SBaptiste Daroussin     { "reorder", 	svT(reorder),		1, "" },
396a96ef450SBaptiste Daroussin     { "treeview",	opW(treeview),		2, "<text> <height> <width> <list-height> <tag1> <item1> <status1> <depth1>..." },
3972a3e3873SBaptiste Daroussin #endif
3982a3e3873SBaptiste Daroussin #if defined(HAVE_XDIALOG2) || defined(HAVE_WHIPTAIL)
399a96ef450SBaptiste Daroussin     { "noitem", 	svT(no_items),		1, NULL },
400a96ef450SBaptiste Daroussin     { "notags", 	svT(no_tags),		1, NULL },
4012a3e3873SBaptiste Daroussin #endif
4024c8945a0SNathan Whitehorn #ifdef HAVE_DLG_TRACE
403a96ef450SBaptiste Daroussin     { "trace",		opO(trace),		1, "<file>" },
4044c8945a0SNathan Whitehorn #endif
4054c8945a0SNathan Whitehorn };
4064c8945a0SNathan Whitehorn /* *INDENT-ON* */
4074c8945a0SNathan Whitehorn 
408f4f33ea0SBaptiste Daroussin #ifdef NO_LEAKS
409f4f33ea0SBaptiste Daroussin static void
ignore_leak(void * value)410f4f33ea0SBaptiste Daroussin ignore_leak(void *value)
411f4f33ea0SBaptiste Daroussin {
412f4f33ea0SBaptiste Daroussin     AllBlobs *next = dlg_calloc(AllBlobs, (size_t) 1);
413f4f33ea0SBaptiste Daroussin     if (next != 0) {
414f4f33ea0SBaptiste Daroussin 	next->blob = value;
415f4f33ea0SBaptiste Daroussin 	next->next = all_blobs;
416f4f33ea0SBaptiste Daroussin 	all_blobs = next;
417f4f33ea0SBaptiste Daroussin     }
418f4f33ea0SBaptiste Daroussin }
419f4f33ea0SBaptiste Daroussin 
420f4f33ea0SBaptiste Daroussin static void
handle_leaks(void)421f4f33ea0SBaptiste Daroussin handle_leaks(void)
422f4f33ea0SBaptiste Daroussin {
423f4f33ea0SBaptiste Daroussin     while (all_blobs != 0) {
424f4f33ea0SBaptiste Daroussin 	char *blob = all_blobs->blob;
425f4f33ea0SBaptiste Daroussin 	AllBlobs *next = all_blobs->next;
426f4f33ea0SBaptiste Daroussin 	free(blob);
427f4f33ea0SBaptiste Daroussin 	free(all_blobs);
428f4f33ea0SBaptiste Daroussin 	all_blobs = next;
429f4f33ea0SBaptiste Daroussin     }
430f4f33ea0SBaptiste Daroussin     free(dialog_opts);
431f4f33ea0SBaptiste Daroussin     if (special_argv != 0) {
432f4f33ea0SBaptiste Daroussin 	free(special_argv[0]);
433f4f33ea0SBaptiste Daroussin 	free(special_argv);
434f4f33ea0SBaptiste Daroussin 	special_argv = 0;
435f4f33ea0SBaptiste Daroussin 	special_argc = 0;
436f4f33ea0SBaptiste Daroussin     }
437f4f33ea0SBaptiste Daroussin }
438f4f33ea0SBaptiste Daroussin #else
439f4f33ea0SBaptiste Daroussin #define handle_leaks()		/* nothing */
440f4f33ea0SBaptiste Daroussin #define ignore_leak(n)		/* nothing */
441f4f33ea0SBaptiste Daroussin #endif
442f4f33ea0SBaptiste Daroussin 
443f4f33ea0SBaptiste Daroussin #define OptionChars "\
444f4f33ea0SBaptiste Daroussin 0123456789\
445f4f33ea0SBaptiste Daroussin -\
446f4f33ea0SBaptiste Daroussin abcdefghijklmnopqrstuvwxyz\
447f4f33ea0SBaptiste Daroussin "
448f4f33ea0SBaptiste Daroussin 
449f4f33ea0SBaptiste Daroussin /*
450f4f33ea0SBaptiste Daroussin  * Check if the given string from main's argv is an option.
451f4f33ea0SBaptiste Daroussin  */
452f4f33ea0SBaptiste Daroussin static bool
isOption(const char * arg)453f4f33ea0SBaptiste Daroussin isOption(const char *arg)
454f4f33ea0SBaptiste Daroussin {
455f4f33ea0SBaptiste Daroussin     bool result = FALSE;
456f4f33ea0SBaptiste Daroussin 
457f4f33ea0SBaptiste Daroussin     if (arg != 0) {
458f4f33ea0SBaptiste Daroussin 	if (dialog_opts != 0) {
459f4f33ea0SBaptiste Daroussin 	    int n;
460f4f33ea0SBaptiste Daroussin 	    for (n = 0; dialog_opts[n] != 0; ++n) {
461f4f33ea0SBaptiste Daroussin 		if (dialog_opts[n] == arg) {
462f4f33ea0SBaptiste Daroussin 		    result = TRUE;
463f4f33ea0SBaptiste Daroussin 		    break;
464f4f33ea0SBaptiste Daroussin 		}
465f4f33ea0SBaptiste Daroussin 	    }
466f4f33ea0SBaptiste Daroussin 	} else if (!strncmp(arg, "--", (size_t) 2) && isalpha(UCH(arg[2]))) {
467f4f33ea0SBaptiste Daroussin 	    if (strlen(arg) == (strspn) (arg, OptionChars)) {
468f4f33ea0SBaptiste Daroussin 		result = TRUE;
469f4f33ea0SBaptiste Daroussin 	    } else {
470f4f33ea0SBaptiste Daroussin 		handle_leaks();
471f4f33ea0SBaptiste Daroussin 		dlg_exiterr("Invalid option \"%s\"", arg);
472f4f33ea0SBaptiste Daroussin 	    }
473f4f33ea0SBaptiste Daroussin 	}
474f4f33ea0SBaptiste Daroussin     }
475f4f33ea0SBaptiste Daroussin     return result;
476f4f33ea0SBaptiste Daroussin }
477f4f33ea0SBaptiste Daroussin 
4784c8945a0SNathan Whitehorn /*
4794c8945a0SNathan Whitehorn  * Make an array showing which argv[] entries are options.  Use "--" as a
4804c8945a0SNathan Whitehorn  * special token to escape the next argument, allowing it to begin with "--".
4814c8945a0SNathan Whitehorn  * When we find a "--" argument, also remove it from argv[] and adjust argc.
4824c8945a0SNathan Whitehorn  * That appears to be an undocumented feature of the popt library.
4834c8945a0SNathan Whitehorn  *
4844c8945a0SNathan Whitehorn  * Also, if we see a "--file", expand it into the parameter list by reading the
4854c8945a0SNathan Whitehorn  * text from the given file and stripping quotes, treating whitespace outside
4864c8945a0SNathan Whitehorn  * quotes as a parameter delimiter.
4874c8945a0SNathan Whitehorn  *
4884c8945a0SNathan Whitehorn  * Finally, if we see a "--args", dump the current list of arguments to the
4894c8945a0SNathan Whitehorn  * standard error.  This is used for debugging complex --file combinations.
4904c8945a0SNathan Whitehorn  */
4914c8945a0SNathan Whitehorn static void
unescape_argv(int * argcp,char *** argvp)4924c8945a0SNathan Whitehorn unescape_argv(int *argcp, char ***argvp)
4934c8945a0SNathan Whitehorn {
4944c8945a0SNathan Whitehorn     int j, k;
4954c8945a0SNathan Whitehorn     int limit_includes = 20 + *argcp;
4964c8945a0SNathan Whitehorn     int count_includes = 0;
4974c8945a0SNathan Whitehorn     bool doalloc = FALSE;
4984c8945a0SNathan Whitehorn     char *filename;
499f4f33ea0SBaptiste Daroussin     const char **my_argv = 0;
500f4f33ea0SBaptiste Daroussin     int my_argc;
5014c8945a0SNathan Whitehorn 
502f4f33ea0SBaptiste Daroussin     DLG_TRACE(("# unescape_argv\n"));
503f4f33ea0SBaptiste Daroussin     for (k = 0; k < 2; ++k) {
504f4f33ea0SBaptiste Daroussin 
505f4f33ea0SBaptiste Daroussin 	my_argc = 0;
506f4f33ea0SBaptiste Daroussin 	if (special_argv != 0) {
507f4f33ea0SBaptiste Daroussin 	    for (j = 0; special_argv[j] != 0; ++j) {
508f4f33ea0SBaptiste Daroussin 		if (!strcmp(special_argv[j], "--")) {
509f4f33ea0SBaptiste Daroussin 		    break;
510f4f33ea0SBaptiste Daroussin 		} else if (isOption(special_argv[j])) {
511f4f33ea0SBaptiste Daroussin 		    if (k != 0)
512f4f33ea0SBaptiste Daroussin 			my_argv[my_argc] = special_argv[j];
513f4f33ea0SBaptiste Daroussin 		    my_argc++;
514f4f33ea0SBaptiste Daroussin 		}
515f4f33ea0SBaptiste Daroussin 	    }
516f4f33ea0SBaptiste Daroussin 	}
517f4f33ea0SBaptiste Daroussin 
518f4f33ea0SBaptiste Daroussin 	if (k == 0) {
519f4f33ea0SBaptiste Daroussin 	    my_argc += (*argcp + 1);
520f4f33ea0SBaptiste Daroussin 	    my_argv = dlg_calloc(const char *, (size_t) my_argc);
521f4f33ea0SBaptiste Daroussin 	    assert_ptr(my_argv, "unescape_argv");
522f4f33ea0SBaptiste Daroussin 	}
523f4f33ea0SBaptiste Daroussin     }
5244c8945a0SNathan Whitehorn 
5254c8945a0SNathan Whitehorn     for (j = 1; j < *argcp; j++) {
5264c8945a0SNathan Whitehorn 	bool escaped = FALSE;
5274c8945a0SNathan Whitehorn 	if (!strcmp((*argvp)[j], "--")) {
5284c8945a0SNathan Whitehorn 	    escaped = TRUE;
529f4f33ea0SBaptiste Daroussin 	    dlg_eat_argv(argcp, argvp, j, 1);
5304c8945a0SNathan Whitehorn 	} else if (!strcmp((*argvp)[j], "--args")) {
5314c8945a0SNathan Whitehorn 	    fprintf(stderr, "Showing arguments at arg%d\n", j);
5324c8945a0SNathan Whitehorn 	    for (k = 0; k < *argcp; ++k) {
5334c8945a0SNathan Whitehorn 		fprintf(stderr, " arg%d:%s\n", k, (*argvp)[k]);
5344c8945a0SNathan Whitehorn 	    }
535f4f33ea0SBaptiste Daroussin 	    dlg_eat_argv(argcp, argvp, j, 1);
536f4f33ea0SBaptiste Daroussin 	    --j;
5374c8945a0SNathan Whitehorn 	} else if (!strcmp((*argvp)[j], "--file")) {
538f4f33ea0SBaptiste Daroussin 	    if (++count_includes > limit_includes) {
539f4f33ea0SBaptiste Daroussin 		handle_leaks();
5404c8945a0SNathan Whitehorn 		dlg_exiterr("Too many --file options");
541f4f33ea0SBaptiste Daroussin 	    }
5424c8945a0SNathan Whitehorn 
5434c8945a0SNathan Whitehorn 	    if ((filename = (*argvp)[j + 1]) != 0) {
5444c8945a0SNathan Whitehorn 		FILE *fp;
5454c8945a0SNathan Whitehorn 		char **list;
5464c8945a0SNathan Whitehorn 
5474c8945a0SNathan Whitehorn 		if (*filename == '&') {
5484c8945a0SNathan Whitehorn 		    fp = fdopen(atoi(filename + sizeof(char)), "r");
5494c8945a0SNathan Whitehorn 		} else {
5504c8945a0SNathan Whitehorn 		    fp = fopen(filename, "r");
5514c8945a0SNathan Whitehorn 		}
5524c8945a0SNathan Whitehorn 
5534c8945a0SNathan Whitehorn 		if (fp) {
554a96ef450SBaptiste Daroussin 		    char *blob;
555a96ef450SBaptiste Daroussin 		    int added;
556a96ef450SBaptiste Daroussin 		    size_t bytes_read;
557a96ef450SBaptiste Daroussin 		    size_t length;
558a96ef450SBaptiste Daroussin 		    int n;
559a96ef450SBaptiste Daroussin 
560f4f33ea0SBaptiste Daroussin 		    DLG_TRACE(("# opened --file %s ..\n", filename));
5614c8945a0SNathan Whitehorn 		    blob = NULL;
5624c8945a0SNathan Whitehorn 		    length = 0;
5634c8945a0SNathan Whitehorn 		    do {
5644c8945a0SNathan Whitehorn 			blob = dlg_realloc(char, length + BUFSIZ + 1, blob);
5654c8945a0SNathan Whitehorn 			assert_ptr(blob, "unescape_argv");
5664c8945a0SNathan Whitehorn 			bytes_read = fread(blob + length,
5674c8945a0SNathan Whitehorn 					   sizeof(char),
5687a1c0d96SNathan Whitehorn 					     (size_t) BUFSIZ,
5694c8945a0SNathan Whitehorn 					   fp);
5704c8945a0SNathan Whitehorn 			length += bytes_read;
571f4f33ea0SBaptiste Daroussin 			if (ferror(fp)) {
572f4f33ea0SBaptiste Daroussin 			    handle_leaks();
5734c8945a0SNathan Whitehorn 			    dlg_exiterr("error on filehandle in unescape_argv");
574f4f33ea0SBaptiste Daroussin 			}
5754c8945a0SNathan Whitehorn 		    } while (bytes_read == BUFSIZ);
5764c8945a0SNathan Whitehorn 		    fclose(fp);
5774c8945a0SNathan Whitehorn 
5784c8945a0SNathan Whitehorn 		    blob[length] = '\0';
579f4f33ea0SBaptiste Daroussin 		    ignore_leak(blob);
5804c8945a0SNathan Whitehorn 
5817a1c0d96SNathan Whitehorn 		    list = dlg_string_to_argv(blob);
582f4f33ea0SBaptiste Daroussin 		    added = dlg_count_argv(list);
5834c8945a0SNathan Whitehorn 		    if (added > 2) {
584f4f33ea0SBaptiste Daroussin 			/* *argcp arguments before the expansion of --file
585f4f33ea0SBaptiste Daroussin 			   - 2 for the removal of '--file <filepath>'
586f4f33ea0SBaptiste Daroussin 			   + added for the arguments contained in <filepath>
587f4f33ea0SBaptiste Daroussin 			   + 1 for the terminating NULL pointer */
588f4f33ea0SBaptiste Daroussin 			size_t need = (size_t) (*argcp + added - 1);
5894c8945a0SNathan Whitehorn 			if (doalloc) {
5904c8945a0SNathan Whitehorn 			    *argvp = dlg_realloc(char *, need, *argvp);
5914c8945a0SNathan Whitehorn 			    assert_ptr(*argvp, "unescape_argv");
5924c8945a0SNathan Whitehorn 			} else {
5934c8945a0SNathan Whitehorn 			    char **newp = dlg_malloc(char *, need);
594f4f33ea0SBaptiste Daroussin 			    ignore_leak(newp);
5954c8945a0SNathan Whitehorn 			    assert_ptr(newp, "unescape_argv");
5964c8945a0SNathan Whitehorn 			    for (n = 0; n < *argcp; ++n) {
5974c8945a0SNathan Whitehorn 				newp[n] = (*argvp)[n];
5984c8945a0SNathan Whitehorn 			    }
599f4f33ea0SBaptiste Daroussin 			    /* The new array is not NULL-terminated yet. */
6004c8945a0SNathan Whitehorn 			    *argvp = newp;
6014c8945a0SNathan Whitehorn 			    doalloc = TRUE;
6024c8945a0SNathan Whitehorn 			}
603f4f33ea0SBaptiste Daroussin 			my_argv = dlg_realloc(const char *, need, my_argv);
604f4f33ea0SBaptiste Daroussin 			assert_ptr(my_argv, "unescape_argv");
605f4f33ea0SBaptiste Daroussin 
606f4f33ea0SBaptiste Daroussin 			/* Shift the arguments after '--file <filepath>'
607f4f33ea0SBaptiste Daroussin 			   right by (added - 2) positions */
608f4f33ea0SBaptiste Daroussin 			for (n = *argcp - 1; n >= j + 2; --n) {
6094c8945a0SNathan Whitehorn 			    (*argvp)[n + added - 2] = (*argvp)[n];
6104c8945a0SNathan Whitehorn 			}
611f4f33ea0SBaptiste Daroussin 		    } else if (added < 2) {
612f4f33ea0SBaptiste Daroussin 			/* 0 or 1 argument read from the included file
613f4f33ea0SBaptiste Daroussin 			   -> shift the arguments after '--file <filepath>'
614f4f33ea0SBaptiste Daroussin 			   left by (2 - added) positions */
615f4f33ea0SBaptiste Daroussin 			for (n = j + added; n + 2 - added < *argcp; ++n) {
616f4f33ea0SBaptiste Daroussin 			    (*argvp)[n] = (*argvp)[n + 2 - added];
617f4f33ea0SBaptiste Daroussin 			}
618f4f33ea0SBaptiste Daroussin 		    }
619f4f33ea0SBaptiste Daroussin 		    /* Copy the inserted arguments to *argvp */
6204c8945a0SNathan Whitehorn 		    for (n = 0; n < added; ++n) {
6214c8945a0SNathan Whitehorn 			(*argvp)[n + j] = list[n];
6224c8945a0SNathan Whitehorn 		    }
6234c8945a0SNathan Whitehorn 		    *argcp += added - 2;
624f4f33ea0SBaptiste Daroussin 		    (*argvp)[*argcp] = 0;	/* Write the NULL terminator */
625f4f33ea0SBaptiste Daroussin 		    free(list);	/* No-op if 'list' is NULL */
626f4f33ea0SBaptiste Daroussin 		    /* Force rescan starting from the first inserted argument */
627f4f33ea0SBaptiste Daroussin 		    --j;
628f4f33ea0SBaptiste Daroussin 		    DLG_TRACE(("# finished --file\n"));
6294c8945a0SNathan Whitehorn 		    continue;
6304c8945a0SNathan Whitehorn 		} else {
631f4f33ea0SBaptiste Daroussin 		    handle_leaks();
632f4f33ea0SBaptiste Daroussin 		    dlg_exiterr("Cannot open --file %s", filename);
633f4f33ea0SBaptiste Daroussin 		}
634f4f33ea0SBaptiste Daroussin 	    } else {
635f4f33ea0SBaptiste Daroussin 		handle_leaks();
6364c8945a0SNathan Whitehorn 		dlg_exiterr("No value given for --file");
6374c8945a0SNathan Whitehorn 	    }
6384c8945a0SNathan Whitehorn 	}
6394c8945a0SNathan Whitehorn 	if (!escaped
6404c8945a0SNathan Whitehorn 	    && (*argvp)[j] != 0
6417a1c0d96SNathan Whitehorn 	    && !strncmp((*argvp)[j], "--", (size_t) 2)
6424c8945a0SNathan Whitehorn 	    && isalpha(UCH((*argvp)[j][2]))) {
643f4f33ea0SBaptiste Daroussin 	    my_argv[my_argc++] = (*argvp)[j];
644f4f33ea0SBaptiste Daroussin 	    DLG_TRACE(("#\toption argv[%d]=%s\n", j, (*argvp)[j]));
6454c8945a0SNathan Whitehorn 	}
6464c8945a0SNathan Whitehorn     }
6474c8945a0SNathan Whitehorn 
648f4f33ea0SBaptiste Daroussin     my_argv[my_argc] = 0;
649f4f33ea0SBaptiste Daroussin 
650f4f33ea0SBaptiste Daroussin     known_opts = my_argc;
651f4f33ea0SBaptiste Daroussin     dialog_opts = my_argv;
652f4f33ea0SBaptiste Daroussin 
653f4f33ea0SBaptiste Daroussin     DLG_TRACE(("#\t%d options vs %d arguments\n", known_opts, *argcp));
6544c8945a0SNathan Whitehorn     dialog_argv = (*argvp);
6554c8945a0SNathan Whitehorn }
6564c8945a0SNathan Whitehorn 
657a96ef450SBaptiste Daroussin static const Options *
findOption(const char * name,int pass,bool recur)658a96ef450SBaptiste Daroussin findOption(const char *name, int pass, bool recur)
6594c8945a0SNathan Whitehorn {
660a96ef450SBaptiste Daroussin     const Options *result = NULL;
6614c8945a0SNathan Whitehorn 
662a96ef450SBaptiste Daroussin     if (!strncmp(name, "--", 2) && isalpha(UCH(name[2]))) {
663a96ef450SBaptiste Daroussin 	unsigned n;
664a96ef450SBaptiste Daroussin 
665a96ef450SBaptiste Daroussin 	name += 2;		/* skip the "--" */
666a96ef450SBaptiste Daroussin 	for (n = 0; n < TableSize(options); n++) {
6674c8945a0SNathan Whitehorn 	    if ((pass & options[n].pass) != 0
6684c8945a0SNathan Whitehorn 		&& !strcmp(name, options[n].name)) {
669a96ef450SBaptiste Daroussin 		result = &options[n];
6702a3e3873SBaptiste Daroussin 		break;
6714c8945a0SNathan Whitehorn 	    }
6724c8945a0SNathan Whitehorn 	}
673a96ef450SBaptiste Daroussin 	if (result == NULL && !recur) {
674a96ef450SBaptiste Daroussin 	    char *temp = malloc(8 + strlen(name));
675a96ef450SBaptiste Daroussin 	    if (temp != NULL) {
676a96ef450SBaptiste Daroussin 		if (!strncmp(name, "no", 2)) {
677a96ef450SBaptiste Daroussin 		    int skip = !strncmp(name, "no-", 3) ? 3 : 2;
678a96ef450SBaptiste Daroussin 		    sprintf(temp, "--no-%s", name + skip);
679a96ef450SBaptiste Daroussin 		    result = findOption(temp, pass, TRUE);
680a96ef450SBaptiste Daroussin 		    if (result == NULL) {
681a96ef450SBaptiste Daroussin 			sprintf(temp, "--%s", name + skip);
682a96ef450SBaptiste Daroussin 			result = findOption(temp, pass, TRUE);
683a96ef450SBaptiste Daroussin 		    }
684a96ef450SBaptiste Daroussin 		}
685a96ef450SBaptiste Daroussin 		if (result == NULL && strncmp(name, "no", 2)) {
686a96ef450SBaptiste Daroussin 		    sprintf(temp, "--no-%s", name);
687a96ef450SBaptiste Daroussin 		    result = findOption(temp, pass, TRUE);
688a96ef450SBaptiste Daroussin 		}
689a96ef450SBaptiste Daroussin 		free(temp);
690a96ef450SBaptiste Daroussin 	    }
691a96ef450SBaptiste Daroussin 	}
692a96ef450SBaptiste Daroussin     }
693a96ef450SBaptiste Daroussin     return result;
694a96ef450SBaptiste Daroussin }
695a96ef450SBaptiste Daroussin 
696a96ef450SBaptiste Daroussin static eOptions
lookupOption(const char * name,int pass)697a96ef450SBaptiste Daroussin lookupOption(const char *name, int pass)
698a96ef450SBaptiste Daroussin {
699a96ef450SBaptiste Daroussin     eOptions result = o_unknown;
700a96ef450SBaptiste Daroussin     const Options *data = findOption(name, pass, FALSE);
701a96ef450SBaptiste Daroussin     if (data != NULL) {
702a96ef450SBaptiste Daroussin 	result = data->code;
7034c8945a0SNathan Whitehorn     }
7042a3e3873SBaptiste Daroussin     return result;
7054c8945a0SNathan Whitehorn }
7064c8945a0SNathan Whitehorn 
7074c8945a0SNathan Whitehorn static void
Usage(const char * msg)7084c8945a0SNathan Whitehorn Usage(const char *msg)
7094c8945a0SNathan Whitehorn {
710f4f33ea0SBaptiste Daroussin     handle_leaks();
711a96ef450SBaptiste Daroussin     dlg_exiterr("%s.\nUse --help to list options.\n\n", msg);
7124c8945a0SNathan Whitehorn }
7134c8945a0SNathan Whitehorn 
7144c8945a0SNathan Whitehorn /*
7154c8945a0SNathan Whitehorn  * Count arguments, stopping at the end of the argument list, or on any of our
7164c8945a0SNathan Whitehorn  * "--" tokens.
7174c8945a0SNathan Whitehorn  */
7184c8945a0SNathan Whitehorn static int
arg_rest(char * argv[])7194c8945a0SNathan Whitehorn arg_rest(char *argv[])
7204c8945a0SNathan Whitehorn {
7214c8945a0SNathan Whitehorn     int i = 1;			/* argv[0] points to a "--" token */
7224c8945a0SNathan Whitehorn 
7234c8945a0SNathan Whitehorn     while (argv[i] != 0
7244c8945a0SNathan Whitehorn 	   && (!isOption(argv[i]) || lookupOption(argv[i], 7) == o_unknown))
7254c8945a0SNathan Whitehorn 	i++;
7264c8945a0SNathan Whitehorn     return i;
7274c8945a0SNathan Whitehorn }
7284c8945a0SNathan Whitehorn 
7294c8945a0SNathan Whitehorn /*
7304c8945a0SNathan Whitehorn  * In MultiWidget this function is needed to count how many tags
7314c8945a0SNathan Whitehorn  * a widget (menu, checklist, radiolist) has
7324c8945a0SNathan Whitehorn  */
7334c8945a0SNathan Whitehorn static int
howmany_tags(char * argv[],int group)7344c8945a0SNathan Whitehorn howmany_tags(char *argv[], int group)
7354c8945a0SNathan Whitehorn {
7364c8945a0SNathan Whitehorn     int result = 0;
7374c8945a0SNathan Whitehorn     char temp[80];
7384c8945a0SNathan Whitehorn 
7394c8945a0SNathan Whitehorn     while (argv[0] != 0) {
740a96ef450SBaptiste Daroussin 	int have;
741a96ef450SBaptiste Daroussin 
7424c8945a0SNathan Whitehorn 	if (isOption(argv[0]))
7434c8945a0SNathan Whitehorn 	    break;
7444c8945a0SNathan Whitehorn 	if ((have = arg_rest(argv)) < group) {
745f4f33ea0SBaptiste Daroussin 	    const char *format = _("Expected %d arguments, found only %d");
7464c8945a0SNathan Whitehorn 	    sprintf(temp, format, group, have);
7474c8945a0SNathan Whitehorn 	    Usage(temp);
748f4f33ea0SBaptiste Daroussin 	} else if ((have % group) != 0) {
749f4f33ea0SBaptiste Daroussin 	    const char *format = _("Expected %d arguments, found extra %d");
750f4f33ea0SBaptiste Daroussin 	    sprintf(temp, format, group, (have % group));
751f4f33ea0SBaptiste Daroussin 	    Usage(temp);
7524c8945a0SNathan Whitehorn 	}
7534c8945a0SNathan Whitehorn 
754f4f33ea0SBaptiste Daroussin 	argv += have;
755f4f33ea0SBaptiste Daroussin 	result += (have / group);
7564c8945a0SNathan Whitehorn     }
7574c8945a0SNathan Whitehorn 
7584c8945a0SNathan Whitehorn     return result;
7594c8945a0SNathan Whitehorn }
7604c8945a0SNathan Whitehorn 
7614c8945a0SNathan Whitehorn static int
numeric_arg(char ** av,int n)7624c8945a0SNathan Whitehorn numeric_arg(char **av, int n)
7634c8945a0SNathan Whitehorn {
7642a3e3873SBaptiste Daroussin     int result = 0;
7652a3e3873SBaptiste Daroussin 
7662a3e3873SBaptiste Daroussin     if (n < dlg_count_argv(av)) {
7672a3e3873SBaptiste Daroussin 	char *last = 0;
7682a3e3873SBaptiste Daroussin 	result = (int) strtol(av[n], &last, 10);
7694c8945a0SNathan Whitehorn 
7704c8945a0SNathan Whitehorn 	if (last == 0 || *last != 0) {
771a96ef450SBaptiste Daroussin 	    char msg[80];
772a96ef450SBaptiste Daroussin 
7734c8945a0SNathan Whitehorn 	    sprintf(msg, "Expected a number for token %d of %.20s", n, av[0]);
7744c8945a0SNathan Whitehorn 	    Usage(msg);
7754c8945a0SNathan Whitehorn 	}
7762a3e3873SBaptiste Daroussin     }
7774c8945a0SNathan Whitehorn     return result;
7784c8945a0SNathan Whitehorn }
7794c8945a0SNathan Whitehorn 
7804c8945a0SNathan Whitehorn static char *
optional_str(char ** av,int n,char * dft)7814c8945a0SNathan Whitehorn optional_str(char **av, int n, char *dft)
7824c8945a0SNathan Whitehorn {
7834c8945a0SNathan Whitehorn     char *ret = dft;
7844c8945a0SNathan Whitehorn     if (arg_rest(av) > n)
7854c8945a0SNathan Whitehorn 	ret = av[n];
7864c8945a0SNathan Whitehorn     return ret;
7874c8945a0SNathan Whitehorn }
7884c8945a0SNathan Whitehorn 
7894c8945a0SNathan Whitehorn #if defined(HAVE_DLG_GAUGE) || defined(HAVE_XDIALOG)
7904c8945a0SNathan Whitehorn static int
optional_num(char ** av,int n,int dft)7914c8945a0SNathan Whitehorn optional_num(char **av, int n, int dft)
7924c8945a0SNathan Whitehorn {
7934c8945a0SNathan Whitehorn     int ret = dft;
7944c8945a0SNathan Whitehorn     if (arg_rest(av) > n)
7954c8945a0SNathan Whitehorn 	ret = numeric_arg(av, n);
7964c8945a0SNathan Whitehorn     return ret;
7974c8945a0SNathan Whitehorn }
7984c8945a0SNathan Whitehorn #endif
7994c8945a0SNathan Whitehorn 
8004c8945a0SNathan Whitehorn /*
8014c8945a0SNathan Whitehorn  * On AIX 4.x, we have to flush the output right away since there is a bug in
8024c8945a0SNathan Whitehorn  * the curses package which discards stdout even when we've used newterm to
8034c8945a0SNathan Whitehorn  * redirect output to /dev/tty.
8044c8945a0SNathan Whitehorn  */
8054c8945a0SNathan Whitehorn static int
show_result(int ret)8064c8945a0SNathan Whitehorn show_result(int ret)
8074c8945a0SNathan Whitehorn {
8084c8945a0SNathan Whitehorn     bool either = FALSE;
8094c8945a0SNathan Whitehorn 
8104c8945a0SNathan Whitehorn     switch (ret) {
8114c8945a0SNathan Whitehorn     case DLG_EXIT_OK:
8124c8945a0SNathan Whitehorn     case DLG_EXIT_EXTRA:
8134c8945a0SNathan Whitehorn     case DLG_EXIT_HELP:
8144c8945a0SNathan Whitehorn     case DLG_EXIT_ITEM_HELP:
8154c8945a0SNathan Whitehorn 	if ((dialog_state.output_count > 1) && !dialog_vars.separate_output) {
8164c8945a0SNathan Whitehorn 	    fputs((dialog_state.separate_str
8174c8945a0SNathan Whitehorn 		   ? dialog_state.separate_str
8184c8945a0SNathan Whitehorn 		   : DEFAULT_SEPARATE_STR),
8194c8945a0SNathan Whitehorn 		  dialog_state.output);
8204c8945a0SNathan Whitehorn 	    either = TRUE;
8214c8945a0SNathan Whitehorn 	}
8227a1c0d96SNathan Whitehorn 	if (dialog_vars.input_result != 0
8237a1c0d96SNathan Whitehorn 	    && dialog_vars.input_result[0] != '\0') {
8244c8945a0SNathan Whitehorn 	    fputs(dialog_vars.input_result, dialog_state.output);
825f4f33ea0SBaptiste Daroussin 	    DLG_TRACE(("# input_result:\n%s\n", dialog_vars.input_result));
8264c8945a0SNathan Whitehorn 	    either = TRUE;
8274c8945a0SNathan Whitehorn 	}
8284c8945a0SNathan Whitehorn 	if (either) {
8294c8945a0SNathan Whitehorn 	    fflush(dialog_state.output);
8304c8945a0SNathan Whitehorn 	}
8314c8945a0SNathan Whitehorn 	break;
8324c8945a0SNathan Whitehorn     }
8334c8945a0SNathan Whitehorn     return ret;
8344c8945a0SNathan Whitehorn }
8354c8945a0SNathan Whitehorn 
8364c8945a0SNathan Whitehorn /*
8374c8945a0SNathan Whitehorn  * These are the widget callers.
8384c8945a0SNathan Whitehorn  */
8394c8945a0SNathan Whitehorn 
8404c8945a0SNathan Whitehorn static int
call_yesno(CALLARGS)8414c8945a0SNathan Whitehorn call_yesno(CALLARGS)
8424c8945a0SNathan Whitehorn {
8434c8945a0SNathan Whitehorn     *offset_add = 4;
8444c8945a0SNathan Whitehorn     return dialog_yesno(t,
8454c8945a0SNathan Whitehorn 			av[1],
8464c8945a0SNathan Whitehorn 			numeric_arg(av, 2),
8474c8945a0SNathan Whitehorn 			numeric_arg(av, 3));
8484c8945a0SNathan Whitehorn }
8494c8945a0SNathan Whitehorn 
8504c8945a0SNathan Whitehorn static int
call_msgbox(CALLARGS)8514c8945a0SNathan Whitehorn call_msgbox(CALLARGS)
8524c8945a0SNathan Whitehorn {
8534c8945a0SNathan Whitehorn     *offset_add = 4;
8544c8945a0SNathan Whitehorn     return dialog_msgbox(t,
8554c8945a0SNathan Whitehorn 			 av[1],
8564c8945a0SNathan Whitehorn 			 numeric_arg(av, 2),
8574c8945a0SNathan Whitehorn 			 numeric_arg(av, 3), 1);
8584c8945a0SNathan Whitehorn }
8594c8945a0SNathan Whitehorn 
8604c8945a0SNathan Whitehorn static int
call_infobox(CALLARGS)8614c8945a0SNathan Whitehorn call_infobox(CALLARGS)
8624c8945a0SNathan Whitehorn {
8634c8945a0SNathan Whitehorn     *offset_add = 4;
8644c8945a0SNathan Whitehorn     return dialog_msgbox(t,
8654c8945a0SNathan Whitehorn 			 av[1],
8664c8945a0SNathan Whitehorn 			 numeric_arg(av, 2),
8674c8945a0SNathan Whitehorn 			 numeric_arg(av, 3), 0);
8684c8945a0SNathan Whitehorn }
8694c8945a0SNathan Whitehorn 
8704c8945a0SNathan Whitehorn static int
call_textbox(CALLARGS)8714c8945a0SNathan Whitehorn call_textbox(CALLARGS)
8724c8945a0SNathan Whitehorn {
8734c8945a0SNathan Whitehorn     *offset_add = 4;
8744c8945a0SNathan Whitehorn     return dialog_textbox(t,
8754c8945a0SNathan Whitehorn 			  av[1],
8764c8945a0SNathan Whitehorn 			  numeric_arg(av, 2),
8774c8945a0SNathan Whitehorn 			  numeric_arg(av, 3));
8784c8945a0SNathan Whitehorn }
8794c8945a0SNathan Whitehorn 
8804c8945a0SNathan Whitehorn static int
call_menu(CALLARGS)8814c8945a0SNathan Whitehorn call_menu(CALLARGS)
8824c8945a0SNathan Whitehorn {
8834c8945a0SNathan Whitehorn     int tags = howmany_tags(av + 5, MENUBOX_TAGS);
8844c8945a0SNathan Whitehorn     *offset_add = 5 + tags * MENUBOX_TAGS;
8854c8945a0SNathan Whitehorn 
8864c8945a0SNathan Whitehorn     return dialog_menu(t,
8874c8945a0SNathan Whitehorn 		       av[1],
8884c8945a0SNathan Whitehorn 		       numeric_arg(av, 2),
8894c8945a0SNathan Whitehorn 		       numeric_arg(av, 3),
8904c8945a0SNathan Whitehorn 		       numeric_arg(av, 4),
8914c8945a0SNathan Whitehorn 		       tags, av + 5);
8924c8945a0SNathan Whitehorn }
8934c8945a0SNathan Whitehorn 
8944c8945a0SNathan Whitehorn static int
call_inputmenu(CALLARGS)8954c8945a0SNathan Whitehorn call_inputmenu(CALLARGS)
8964c8945a0SNathan Whitehorn {
8974c8945a0SNathan Whitehorn     int tags = howmany_tags(av + 5, MENUBOX_TAGS);
8987a1c0d96SNathan Whitehorn     bool free_extra_label = FALSE;
8997a1c0d96SNathan Whitehorn     int result;
9004c8945a0SNathan Whitehorn 
9014c8945a0SNathan Whitehorn     dialog_vars.input_menu = TRUE;
9024c8945a0SNathan Whitehorn 
9034c8945a0SNathan Whitehorn     if (dialog_vars.max_input == 0)
9044c8945a0SNathan Whitehorn 	dialog_vars.max_input = MAX_LEN / 2;
9054c8945a0SNathan Whitehorn 
9067a1c0d96SNathan Whitehorn     if (dialog_vars.extra_label == 0) {
9077a1c0d96SNathan Whitehorn 	free_extra_label = TRUE;
9087a1c0d96SNathan Whitehorn 	dialog_vars.extra_label = dlg_strclone(_("Rename"));
9097a1c0d96SNathan Whitehorn     }
9104c8945a0SNathan Whitehorn 
9114c8945a0SNathan Whitehorn     dialog_vars.extra_button = TRUE;
9124c8945a0SNathan Whitehorn 
9134c8945a0SNathan Whitehorn     *offset_add = 5 + tags * MENUBOX_TAGS;
9147a1c0d96SNathan Whitehorn     result = dialog_menu(t,
9154c8945a0SNathan Whitehorn 			 av[1],
9164c8945a0SNathan Whitehorn 			 numeric_arg(av, 2),
9174c8945a0SNathan Whitehorn 			 numeric_arg(av, 3),
9184c8945a0SNathan Whitehorn 			 numeric_arg(av, 4),
9194c8945a0SNathan Whitehorn 			 tags, av + 5);
9207a1c0d96SNathan Whitehorn     if (free_extra_label) {
9217a1c0d96SNathan Whitehorn 	free(dialog_vars.extra_label);
9227a1c0d96SNathan Whitehorn 	dialog_vars.extra_label = 0;
9237a1c0d96SNathan Whitehorn     }
9247a1c0d96SNathan Whitehorn     return result;
9254c8945a0SNathan Whitehorn }
9264c8945a0SNathan Whitehorn 
9274c8945a0SNathan Whitehorn static int
call_checklist(CALLARGS)9284c8945a0SNathan Whitehorn call_checklist(CALLARGS)
9294c8945a0SNathan Whitehorn {
9304c8945a0SNathan Whitehorn     int tags = howmany_tags(av + 5, CHECKBOX_TAGS);
9314c8945a0SNathan Whitehorn     int code;
9324c8945a0SNathan Whitehorn 
9334c8945a0SNathan Whitehorn     *offset_add = 5 + tags * CHECKBOX_TAGS;
9344c8945a0SNathan Whitehorn     code = dialog_checklist(t,
9354c8945a0SNathan Whitehorn 			    av[1],
9364c8945a0SNathan Whitehorn 			    numeric_arg(av, 2),
9374c8945a0SNathan Whitehorn 			    numeric_arg(av, 3),
9384c8945a0SNathan Whitehorn 			    numeric_arg(av, 4),
9394c8945a0SNathan Whitehorn 			    tags, av + 5, FLAG_CHECK);
9404c8945a0SNathan Whitehorn     return code;
9414c8945a0SNathan Whitehorn }
9424c8945a0SNathan Whitehorn 
9434c8945a0SNathan Whitehorn static int
call_radiolist(CALLARGS)9444c8945a0SNathan Whitehorn call_radiolist(CALLARGS)
9454c8945a0SNathan Whitehorn {
9464c8945a0SNathan Whitehorn     int tags = howmany_tags(av + 5, CHECKBOX_TAGS);
9474c8945a0SNathan Whitehorn     *offset_add = 5 + tags * CHECKBOX_TAGS;
9484c8945a0SNathan Whitehorn     return dialog_checklist(t,
9494c8945a0SNathan Whitehorn 			    av[1],
9504c8945a0SNathan Whitehorn 			    numeric_arg(av, 2),
9514c8945a0SNathan Whitehorn 			    numeric_arg(av, 3),
9524c8945a0SNathan Whitehorn 			    numeric_arg(av, 4),
9534c8945a0SNathan Whitehorn 			    tags, av + 5, FLAG_RADIO);
9544c8945a0SNathan Whitehorn }
9554c8945a0SNathan Whitehorn 
9564c8945a0SNathan Whitehorn static int
call_inputbox(CALLARGS)9574c8945a0SNathan Whitehorn call_inputbox(CALLARGS)
9584c8945a0SNathan Whitehorn {
9594c8945a0SNathan Whitehorn     *offset_add = arg_rest(av);
9604c8945a0SNathan Whitehorn     return dialog_inputbox(t,
9614c8945a0SNathan Whitehorn 			   av[1],
9624c8945a0SNathan Whitehorn 			   numeric_arg(av, 2),
9634c8945a0SNathan Whitehorn 			   numeric_arg(av, 3),
9644c8945a0SNathan Whitehorn 			   optional_str(av, 4, 0), 0);
9654c8945a0SNathan Whitehorn }
9664c8945a0SNathan Whitehorn 
9674c8945a0SNathan Whitehorn static int
call_passwordbox(CALLARGS)9684c8945a0SNathan Whitehorn call_passwordbox(CALLARGS)
9694c8945a0SNathan Whitehorn {
9704c8945a0SNathan Whitehorn     *offset_add = arg_rest(av);
9714c8945a0SNathan Whitehorn     return dialog_inputbox(t,
9724c8945a0SNathan Whitehorn 			   av[1],
9734c8945a0SNathan Whitehorn 			   numeric_arg(av, 2),
9744c8945a0SNathan Whitehorn 			   numeric_arg(av, 3),
9754c8945a0SNathan Whitehorn 			   optional_str(av, 4, 0), 1);
9764c8945a0SNathan Whitehorn }
9774c8945a0SNathan Whitehorn 
9784c8945a0SNathan Whitehorn #ifdef HAVE_XDIALOG
9794c8945a0SNathan Whitehorn static int
call_calendar(CALLARGS)9804c8945a0SNathan Whitehorn call_calendar(CALLARGS)
9814c8945a0SNathan Whitehorn {
9824c8945a0SNathan Whitehorn     *offset_add = arg_rest(av);
9834c8945a0SNathan Whitehorn     return dialog_calendar(t,
9844c8945a0SNathan Whitehorn 			   av[1],
9854c8945a0SNathan Whitehorn 			   numeric_arg(av, 2),
9864c8945a0SNathan Whitehorn 			   numeric_arg(av, 3),
9874c8945a0SNathan Whitehorn 			   optional_num(av, 4, -1),
9884c8945a0SNathan Whitehorn 			   optional_num(av, 5, -1),
9894c8945a0SNathan Whitehorn 			   optional_num(av, 6, -1));
9904c8945a0SNathan Whitehorn }
9914c8945a0SNathan Whitehorn 
9924c8945a0SNathan Whitehorn static int
call_dselect(CALLARGS)9934c8945a0SNathan Whitehorn call_dselect(CALLARGS)
9944c8945a0SNathan Whitehorn {
9954c8945a0SNathan Whitehorn     *offset_add = arg_rest(av);
9964c8945a0SNathan Whitehorn     return dialog_dselect(t,
9974c8945a0SNathan Whitehorn 			  av[1],
9984c8945a0SNathan Whitehorn 			  numeric_arg(av, 2),
9994c8945a0SNathan Whitehorn 			  numeric_arg(av, 3));
10004c8945a0SNathan Whitehorn }
10014c8945a0SNathan Whitehorn 
10024c8945a0SNathan Whitehorn static int
call_editbox(CALLARGS)10034c8945a0SNathan Whitehorn call_editbox(CALLARGS)
10044c8945a0SNathan Whitehorn {
10054c8945a0SNathan Whitehorn     *offset_add = 4;
10064c8945a0SNathan Whitehorn     return dialog_editbox(t,
10074c8945a0SNathan Whitehorn 			  av[1],
10084c8945a0SNathan Whitehorn 			  numeric_arg(av, 2),
10094c8945a0SNathan Whitehorn 			  numeric_arg(av, 3));
10104c8945a0SNathan Whitehorn }
10114c8945a0SNathan Whitehorn 
10124c8945a0SNathan Whitehorn static int
call_fselect(CALLARGS)10134c8945a0SNathan Whitehorn call_fselect(CALLARGS)
10144c8945a0SNathan Whitehorn {
10154c8945a0SNathan Whitehorn     *offset_add = arg_rest(av);
10164c8945a0SNathan Whitehorn     return dialog_fselect(t,
10174c8945a0SNathan Whitehorn 			  av[1],
10184c8945a0SNathan Whitehorn 			  numeric_arg(av, 2),
10194c8945a0SNathan Whitehorn 			  numeric_arg(av, 3));
10204c8945a0SNathan Whitehorn }
10214c8945a0SNathan Whitehorn 
10224c8945a0SNathan Whitehorn static int
call_timebox(CALLARGS)10234c8945a0SNathan Whitehorn call_timebox(CALLARGS)
10244c8945a0SNathan Whitehorn {
10254c8945a0SNathan Whitehorn     *offset_add = arg_rest(av);
10264c8945a0SNathan Whitehorn     return dialog_timebox(t,
10274c8945a0SNathan Whitehorn 			  av[1],
10284c8945a0SNathan Whitehorn 			  numeric_arg(av, 2),
10294c8945a0SNathan Whitehorn 			  numeric_arg(av, 3),
10304c8945a0SNathan Whitehorn 			  optional_num(av, 4, -1),
10314c8945a0SNathan Whitehorn 			  optional_num(av, 5, -1),
10324c8945a0SNathan Whitehorn 			  optional_num(av, 6, -1));
10334c8945a0SNathan Whitehorn }
10344c8945a0SNathan Whitehorn #endif /* HAVE_XDIALOG */
10354c8945a0SNathan Whitehorn 
10362a3e3873SBaptiste Daroussin /* dialog 1.2 widgets */
10372a3e3873SBaptiste Daroussin #ifdef HAVE_XDIALOG2
10382a3e3873SBaptiste Daroussin 
10392a3e3873SBaptiste Daroussin #define DisableNoTags() \
10402a3e3873SBaptiste Daroussin 	bool save_no_tags = dialog_vars.no_tags; \
10412a3e3873SBaptiste Daroussin 	bool save_no_items = dialog_vars.no_items; \
10422a3e3873SBaptiste Daroussin 	dialog_vars.no_tags = TRUE; \
10432a3e3873SBaptiste Daroussin 	dialog_vars.no_items = FALSE
10442a3e3873SBaptiste Daroussin 
10452a3e3873SBaptiste Daroussin #define RestoreNoTags() \
10462a3e3873SBaptiste Daroussin 	dialog_vars.no_tags = save_no_tags; \
10472a3e3873SBaptiste Daroussin 	dialog_vars.no_items = save_no_items
10482a3e3873SBaptiste Daroussin 
10492a3e3873SBaptiste Daroussin static int
call_buildlist(CALLARGS)10502a3e3873SBaptiste Daroussin call_buildlist(CALLARGS)
10512a3e3873SBaptiste Daroussin {
10522a3e3873SBaptiste Daroussin     int tags = howmany_tags(av + 5, CHECKBOX_TAGS);
10532a3e3873SBaptiste Daroussin     int result;
10542a3e3873SBaptiste Daroussin 
10552a3e3873SBaptiste Daroussin     DisableNoTags();
10562a3e3873SBaptiste Daroussin 
10572a3e3873SBaptiste Daroussin     *offset_add = 5 + tags * CHECKBOX_TAGS;
10582a3e3873SBaptiste Daroussin     result = dialog_buildlist(t,
10592a3e3873SBaptiste Daroussin 			      av[1],
10602a3e3873SBaptiste Daroussin 			      numeric_arg(av, 2),
10612a3e3873SBaptiste Daroussin 			      numeric_arg(av, 3),
10622a3e3873SBaptiste Daroussin 			      numeric_arg(av, 4),
10632a3e3873SBaptiste Daroussin 			      tags, av + 5,
1064f4f33ea0SBaptiste Daroussin 			      dialog_vars.reorder);
10652a3e3873SBaptiste Daroussin     RestoreNoTags();
10662a3e3873SBaptiste Daroussin     return result;
10672a3e3873SBaptiste Daroussin }
10682a3e3873SBaptiste Daroussin 
10692a3e3873SBaptiste Daroussin static int
call_rangebox(CALLARGS)10702a3e3873SBaptiste Daroussin call_rangebox(CALLARGS)
10712a3e3873SBaptiste Daroussin {
10722a3e3873SBaptiste Daroussin     int min_value;
10732a3e3873SBaptiste Daroussin 
10742a3e3873SBaptiste Daroussin     *offset_add = arg_rest(av);
10752a3e3873SBaptiste Daroussin     min_value = numeric_arg(av, 4);
10762a3e3873SBaptiste Daroussin     return dialog_rangebox(t,
10772a3e3873SBaptiste Daroussin 			   av[1],
10782a3e3873SBaptiste Daroussin 			   numeric_arg(av, 2),
10792a3e3873SBaptiste Daroussin 			   numeric_arg(av, 3),
10802a3e3873SBaptiste Daroussin 			   min_value,
10812a3e3873SBaptiste Daroussin 			   numeric_arg(av, 5),
10822a3e3873SBaptiste Daroussin 			   (*offset_add > 6) ? numeric_arg(av, 6) : min_value);
10832a3e3873SBaptiste Daroussin }
10842a3e3873SBaptiste Daroussin 
10852a3e3873SBaptiste Daroussin static int
call_treeview(CALLARGS)10862a3e3873SBaptiste Daroussin call_treeview(CALLARGS)
10872a3e3873SBaptiste Daroussin {
10882a3e3873SBaptiste Daroussin     int tags = howmany_tags(av + 5, TREEVIEW_TAGS);
10892a3e3873SBaptiste Daroussin     int result;
10902a3e3873SBaptiste Daroussin 
10912a3e3873SBaptiste Daroussin     DisableNoTags();
10922a3e3873SBaptiste Daroussin 
10932a3e3873SBaptiste Daroussin     *offset_add = arg_rest(av);
10942a3e3873SBaptiste Daroussin     result = dialog_treeview(t,
10952a3e3873SBaptiste Daroussin 			     av[1],
10962a3e3873SBaptiste Daroussin 			     numeric_arg(av, 2),
10972a3e3873SBaptiste Daroussin 			     numeric_arg(av, 3),
10982a3e3873SBaptiste Daroussin 			     numeric_arg(av, 4),
10992a3e3873SBaptiste Daroussin 			     tags, av + 5, FLAG_RADIO);
11002a3e3873SBaptiste Daroussin     RestoreNoTags();
11012a3e3873SBaptiste Daroussin     return result;
11022a3e3873SBaptiste Daroussin }
11032a3e3873SBaptiste Daroussin #endif /* HAVE_XDIALOG */
11042a3e3873SBaptiste Daroussin 
11054c8945a0SNathan Whitehorn #ifdef HAVE_DLG_FORMBOX
11064c8945a0SNathan Whitehorn static int
call_form(CALLARGS)11074c8945a0SNathan Whitehorn call_form(CALLARGS)
11084c8945a0SNathan Whitehorn {
11094c8945a0SNathan Whitehorn     int group = FORMBOX_TAGS;
11104c8945a0SNathan Whitehorn     int tags = howmany_tags(av + 5, group);
11114c8945a0SNathan Whitehorn     *offset_add = 5 + tags * group;
11124c8945a0SNathan Whitehorn 
11134c8945a0SNathan Whitehorn     return dialog_form(t,
11144c8945a0SNathan Whitehorn 		       av[1],
11154c8945a0SNathan Whitehorn 		       numeric_arg(av, 2),
11164c8945a0SNathan Whitehorn 		       numeric_arg(av, 3),
11174c8945a0SNathan Whitehorn 		       numeric_arg(av, 4),
11184c8945a0SNathan Whitehorn 		       tags, av + 5);
11194c8945a0SNathan Whitehorn }
11204c8945a0SNathan Whitehorn 
11214c8945a0SNathan Whitehorn static int
call_password_form(CALLARGS)11224c8945a0SNathan Whitehorn call_password_form(CALLARGS)
11234c8945a0SNathan Whitehorn {
11244c8945a0SNathan Whitehorn     unsigned save = dialog_vars.formitem_type;
11254c8945a0SNathan Whitehorn     int result;
11264c8945a0SNathan Whitehorn 
11274c8945a0SNathan Whitehorn     dialog_vars.formitem_type = 1;
11284c8945a0SNathan Whitehorn     result = call_form(PASSARGS);
11294c8945a0SNathan Whitehorn     dialog_vars.formitem_type = save;
11304c8945a0SNathan Whitehorn 
11314c8945a0SNathan Whitehorn     return result;
11324c8945a0SNathan Whitehorn }
11334c8945a0SNathan Whitehorn #endif /* HAVE_DLG_FORMBOX */
11344c8945a0SNathan Whitehorn 
11354c8945a0SNathan Whitehorn #ifdef HAVE_DLG_MIXEDFORM
11364c8945a0SNathan Whitehorn static int
call_mixed_form(CALLARGS)11374c8945a0SNathan Whitehorn call_mixed_form(CALLARGS)
11384c8945a0SNathan Whitehorn {
11394c8945a0SNathan Whitehorn     int group = MIXEDFORM_TAGS;
11404c8945a0SNathan Whitehorn     int tags = howmany_tags(av + 5, group);
11414c8945a0SNathan Whitehorn     *offset_add = 5 + tags * group;
11424c8945a0SNathan Whitehorn 
11434c8945a0SNathan Whitehorn     return dialog_mixedform(t,
11444c8945a0SNathan Whitehorn 			    av[1],
11454c8945a0SNathan Whitehorn 			    numeric_arg(av, 2),
11464c8945a0SNathan Whitehorn 			    numeric_arg(av, 3),
11474c8945a0SNathan Whitehorn 			    numeric_arg(av, 4),
11484c8945a0SNathan Whitehorn 			    tags, av + 5);
11494c8945a0SNathan Whitehorn }
11504c8945a0SNathan Whitehorn #endif /* HAVE_DLG_MIXEDFORM */
11514c8945a0SNathan Whitehorn 
11524c8945a0SNathan Whitehorn #ifdef HAVE_DLG_GAUGE
11534c8945a0SNathan Whitehorn static int
call_gauge(CALLARGS)11544c8945a0SNathan Whitehorn call_gauge(CALLARGS)
11554c8945a0SNathan Whitehorn {
11564c8945a0SNathan Whitehorn     *offset_add = arg_rest(av);
11574c8945a0SNathan Whitehorn     return dialog_gauge(t,
11584c8945a0SNathan Whitehorn 			av[1],
11594c8945a0SNathan Whitehorn 			numeric_arg(av, 2),
11604c8945a0SNathan Whitehorn 			numeric_arg(av, 3),
11614c8945a0SNathan Whitehorn 			optional_num(av, 4, 0));
11624c8945a0SNathan Whitehorn }
11634c8945a0SNathan Whitehorn 
11644c8945a0SNathan Whitehorn static int
call_pause(CALLARGS)11654c8945a0SNathan Whitehorn call_pause(CALLARGS)
11664c8945a0SNathan Whitehorn {
11674c8945a0SNathan Whitehorn     *offset_add = arg_rest(av);
11684c8945a0SNathan Whitehorn     return dialog_pause(t,
11694c8945a0SNathan Whitehorn 			av[1],
11704c8945a0SNathan Whitehorn 			numeric_arg(av, 2),
11714c8945a0SNathan Whitehorn 			numeric_arg(av, 3),
11724c8945a0SNathan Whitehorn 			numeric_arg(av, 4));
11734c8945a0SNathan Whitehorn }
11744c8945a0SNathan Whitehorn #endif
11754c8945a0SNathan Whitehorn 
11764c8945a0SNathan Whitehorn #ifdef HAVE_MIXEDGAUGE
11774c8945a0SNathan Whitehorn static int
call_mixed_gauge(CALLARGS)11784c8945a0SNathan Whitehorn call_mixed_gauge(CALLARGS)
11794c8945a0SNathan Whitehorn {
11804c8945a0SNathan Whitehorn #define MIXEDGAUGE_BASE 5
11814c8945a0SNathan Whitehorn     int tags = howmany_tags(av + MIXEDGAUGE_BASE, MIXEDGAUGE_TAGS);
11824c8945a0SNathan Whitehorn     *offset_add = MIXEDGAUGE_BASE + tags * MIXEDGAUGE_TAGS;
11834c8945a0SNathan Whitehorn     return dialog_mixedgauge(t,
11844c8945a0SNathan Whitehorn 			     av[1],
11854c8945a0SNathan Whitehorn 			     numeric_arg(av, 2),
11864c8945a0SNathan Whitehorn 			     numeric_arg(av, 3),
11874c8945a0SNathan Whitehorn 			     numeric_arg(av, 4),
11884c8945a0SNathan Whitehorn 			     tags, av + MIXEDGAUGE_BASE);
11894c8945a0SNathan Whitehorn }
11904c8945a0SNathan Whitehorn #endif
11914c8945a0SNathan Whitehorn 
1192682c9e0fSNathan Whitehorn #ifdef HAVE_DLG_GAUGE
11937a1c0d96SNathan Whitehorn static int
call_prgbox(CALLARGS)11947a1c0d96SNathan Whitehorn call_prgbox(CALLARGS)
11957a1c0d96SNathan Whitehorn {
11967a1c0d96SNathan Whitehorn     *offset_add = arg_rest(av);
11977a1c0d96SNathan Whitehorn     /* the original version does not accept a prompt string, but for
11987a1c0d96SNathan Whitehorn      * consistency we allow it.
11997a1c0d96SNathan Whitehorn      */
12007a1c0d96SNathan Whitehorn     return ((*offset_add == 5)
12017a1c0d96SNathan Whitehorn 	    ? dialog_prgbox(t,
12027a1c0d96SNathan Whitehorn 			    av[1],
12037a1c0d96SNathan Whitehorn 			    av[2],
12047a1c0d96SNathan Whitehorn 			    numeric_arg(av, 3),
12057a1c0d96SNathan Whitehorn 			    numeric_arg(av, 4), TRUE)
12067a1c0d96SNathan Whitehorn 	    : dialog_prgbox(t,
12077a1c0d96SNathan Whitehorn 			    "",
12087a1c0d96SNathan Whitehorn 			    av[1],
12097a1c0d96SNathan Whitehorn 			    numeric_arg(av, 2),
12107a1c0d96SNathan Whitehorn 			    numeric_arg(av, 3), TRUE));
12117a1c0d96SNathan Whitehorn }
1212682c9e0fSNathan Whitehorn #endif
12137a1c0d96SNathan Whitehorn 
12147a1c0d96SNathan Whitehorn #ifdef HAVE_DLG_GAUGE
12157a1c0d96SNathan Whitehorn static int
call_programbox(CALLARGS)12167a1c0d96SNathan Whitehorn call_programbox(CALLARGS)
12177a1c0d96SNathan Whitehorn {
12187a1c0d96SNathan Whitehorn     int result;
12197a1c0d96SNathan Whitehorn 
12207a1c0d96SNathan Whitehorn     *offset_add = arg_rest(av);
12217a1c0d96SNathan Whitehorn     /* this function is a compromise between --prgbox and --progressbox.
12227a1c0d96SNathan Whitehorn      */
12237a1c0d96SNathan Whitehorn     result = ((*offset_add == 4)
12247a1c0d96SNathan Whitehorn 	      ? dlg_progressbox(t,
12257a1c0d96SNathan Whitehorn 				av[1],
12267a1c0d96SNathan Whitehorn 				numeric_arg(av, 2),
12277a1c0d96SNathan Whitehorn 				numeric_arg(av, 3),
12287a1c0d96SNathan Whitehorn 				TRUE,
12297a1c0d96SNathan Whitehorn 				dialog_state.pipe_input)
12307a1c0d96SNathan Whitehorn 	      : dlg_progressbox(t,
12317a1c0d96SNathan Whitehorn 				"",
12327a1c0d96SNathan Whitehorn 				numeric_arg(av, 1),
12337a1c0d96SNathan Whitehorn 				numeric_arg(av, 2),
12347a1c0d96SNathan Whitehorn 				TRUE,
12357a1c0d96SNathan Whitehorn 				dialog_state.pipe_input));
12367a1c0d96SNathan Whitehorn     dialog_state.pipe_input = 0;
12377a1c0d96SNathan Whitehorn     return result;
12387a1c0d96SNathan Whitehorn }
12397a1c0d96SNathan Whitehorn #endif
12407a1c0d96SNathan Whitehorn 
12414c8945a0SNathan Whitehorn #ifdef HAVE_DLG_GAUGE
12424c8945a0SNathan Whitehorn static int
call_progressbox(CALLARGS)12434c8945a0SNathan Whitehorn call_progressbox(CALLARGS)
12444c8945a0SNathan Whitehorn {
12454c8945a0SNathan Whitehorn     *offset_add = arg_rest(av);
12464c8945a0SNathan Whitehorn     /* the original version does not accept a prompt string, but for
12474c8945a0SNathan Whitehorn      * consistency we allow it.
12484c8945a0SNathan Whitehorn      */
12494c8945a0SNathan Whitehorn     return ((*offset_add == 4)
12504c8945a0SNathan Whitehorn 	    ? dialog_progressbox(t,
12514c8945a0SNathan Whitehorn 				 av[1],
12524c8945a0SNathan Whitehorn 				 numeric_arg(av, 2),
12534c8945a0SNathan Whitehorn 				 numeric_arg(av, 3))
12544c8945a0SNathan Whitehorn 	    : dialog_progressbox(t,
12554c8945a0SNathan Whitehorn 				 "",
12564c8945a0SNathan Whitehorn 				 numeric_arg(av, 1),
12574c8945a0SNathan Whitehorn 				 numeric_arg(av, 2)));
12584c8945a0SNathan Whitehorn }
12594c8945a0SNathan Whitehorn #endif
12604c8945a0SNathan Whitehorn 
12614c8945a0SNathan Whitehorn #ifdef HAVE_DLG_TAILBOX
12624c8945a0SNathan Whitehorn static int
call_tailbox(CALLARGS)12634c8945a0SNathan Whitehorn call_tailbox(CALLARGS)
12644c8945a0SNathan Whitehorn {
12654c8945a0SNathan Whitehorn     *offset_add = 4;
12664c8945a0SNathan Whitehorn     return dialog_tailbox(t,
12674c8945a0SNathan Whitehorn 			  av[1],
12684c8945a0SNathan Whitehorn 			  numeric_arg(av, 2),
12694c8945a0SNathan Whitehorn 			  numeric_arg(av, 3),
12704c8945a0SNathan Whitehorn 			  FALSE);
12714c8945a0SNathan Whitehorn }
12724c8945a0SNathan Whitehorn 
12734c8945a0SNathan Whitehorn static int
call_tailboxbg(CALLARGS)12744c8945a0SNathan Whitehorn call_tailboxbg(CALLARGS)
12754c8945a0SNathan Whitehorn {
12764c8945a0SNathan Whitehorn     *offset_add = 4;
12774c8945a0SNathan Whitehorn     return dialog_tailbox(t,
12784c8945a0SNathan Whitehorn 			  av[1],
12794c8945a0SNathan Whitehorn 			  numeric_arg(av, 2),
12804c8945a0SNathan Whitehorn 			  numeric_arg(av, 3),
12814c8945a0SNathan Whitehorn 			  TRUE);
12824c8945a0SNathan Whitehorn }
12834c8945a0SNathan Whitehorn #endif
12844c8945a0SNathan Whitehorn /* *INDENT-OFF* */
12854c8945a0SNathan Whitehorn static const Mode modes[] =
12864c8945a0SNathan Whitehorn {
12874c8945a0SNathan Whitehorn     {o_yesno,           4, 4, call_yesno},
12884c8945a0SNathan Whitehorn     {o_msgbox,          4, 4, call_msgbox},
12894c8945a0SNathan Whitehorn     {o_infobox,         4, 4, call_infobox},
12904c8945a0SNathan Whitehorn     {o_textbox,         4, 4, call_textbox},
1291f4f33ea0SBaptiste Daroussin     {o_menu,            6, 0, call_menu},
1292f4f33ea0SBaptiste Daroussin     {o_inputmenu,       6, 0, call_inputmenu},
1293f4f33ea0SBaptiste Daroussin     {o_checklist,       7, 0, call_checklist},
1294f4f33ea0SBaptiste Daroussin     {o_radiolist,       7, 0, call_radiolist},
12954c8945a0SNathan Whitehorn     {o_inputbox,        4, 5, call_inputbox},
12964c8945a0SNathan Whitehorn     {o_passwordbox,     4, 5, call_passwordbox},
12974c8945a0SNathan Whitehorn #ifdef HAVE_DLG_GAUGE
12984c8945a0SNathan Whitehorn     {o_gauge,           4, 5, call_gauge},
12994c8945a0SNathan Whitehorn     {o_pause,           5, 5, call_pause},
13007a1c0d96SNathan Whitehorn     {o_prgbox,          4, 5, call_prgbox},
13017a1c0d96SNathan Whitehorn     {o_programbox,      3, 4, call_programbox},
13024c8945a0SNathan Whitehorn     {o_progressbox,     3, 4, call_progressbox},
13034c8945a0SNathan Whitehorn #endif
13044c8945a0SNathan Whitehorn #ifdef HAVE_DLG_FORMBOX
13054c8945a0SNathan Whitehorn     {o_passwordform,   13, 0, call_password_form},
13064c8945a0SNathan Whitehorn     {o_form,           13, 0, call_form},
13074c8945a0SNathan Whitehorn #endif
13084c8945a0SNathan Whitehorn #ifdef HAVE_MIXEDGAUGE
13094c8945a0SNathan Whitehorn     {o_mixedgauge,      MIXEDGAUGE_BASE, 0, call_mixed_gauge},
13104c8945a0SNathan Whitehorn #endif
13114c8945a0SNathan Whitehorn #ifdef HAVE_DLG_MIXEDFORM
13124c8945a0SNathan Whitehorn     {o_mixedform,      13, 0, call_mixed_form},
13134c8945a0SNathan Whitehorn #endif
13144c8945a0SNathan Whitehorn #ifdef HAVE_DLG_TAILBOX
13154c8945a0SNathan Whitehorn     {o_tailbox,         4, 4, call_tailbox},
13164c8945a0SNathan Whitehorn     {o_tailboxbg,       4, 4, call_tailboxbg},
13174c8945a0SNathan Whitehorn #endif
13184c8945a0SNathan Whitehorn #ifdef HAVE_XDIALOG
13194c8945a0SNathan Whitehorn     {o_calendar,        4, 7, call_calendar},
13204c8945a0SNathan Whitehorn     {o_dselect,         4, 5, call_dselect},
13214c8945a0SNathan Whitehorn     {o_editbox,         4, 4, call_editbox},
13224c8945a0SNathan Whitehorn     {o_fselect,         4, 5, call_fselect},
13234c8945a0SNathan Whitehorn     {o_timebox,         4, 7, call_timebox},
1324f4f33ea0SBaptiste Daroussin #endif
1325f4f33ea0SBaptiste Daroussin #ifdef HAVE_XDIALOG2
1326f4f33ea0SBaptiste Daroussin     {o_buildlist,       4, 0, call_buildlist},
1327f4f33ea0SBaptiste Daroussin     {o_rangebox,        5, 7, call_rangebox},
13282a3e3873SBaptiste Daroussin     {o_treeview,        4, 0, call_treeview},
13294c8945a0SNathan Whitehorn #endif
13304c8945a0SNathan Whitehorn };
13314c8945a0SNathan Whitehorn /* *INDENT-ON* */
13324c8945a0SNathan Whitehorn 
13334c8945a0SNathan Whitehorn static char *
optionString(char ** argv,int * num)13344c8945a0SNathan Whitehorn optionString(char **argv, int *num)
13354c8945a0SNathan Whitehorn {
13364c8945a0SNathan Whitehorn     int next = *num + 1;
13374c8945a0SNathan Whitehorn     char *result = argv[next];
13384c8945a0SNathan Whitehorn     if (result == 0) {
13394c8945a0SNathan Whitehorn 	char temp[80];
13404c8945a0SNathan Whitehorn 	sprintf(temp, "Expected a string-parameter for %.20s", argv[*num]);
13414c8945a0SNathan Whitehorn 	Usage(temp);
13424c8945a0SNathan Whitehorn     }
13434c8945a0SNathan Whitehorn     *num = next;
13444c8945a0SNathan Whitehorn     return result;
13454c8945a0SNathan Whitehorn }
13464c8945a0SNathan Whitehorn 
13474c8945a0SNathan Whitehorn static int
optionValue(char ** argv,int * num)13484c8945a0SNathan Whitehorn optionValue(char **argv, int *num)
13494c8945a0SNathan Whitehorn {
13504c8945a0SNathan Whitehorn     int next = *num + 1;
13514c8945a0SNathan Whitehorn     char *src = argv[next];
13524c8945a0SNathan Whitehorn     char *tmp = 0;
13534c8945a0SNathan Whitehorn     int result = 0;
13544c8945a0SNathan Whitehorn 
13554c8945a0SNathan Whitehorn     if (src != 0) {
13567a1c0d96SNathan Whitehorn 	result = (int) strtol(src, &tmp, 0);
13574c8945a0SNathan Whitehorn 	if (tmp == 0 || *tmp != 0)
13584c8945a0SNathan Whitehorn 	    src = 0;
13594c8945a0SNathan Whitehorn     }
13604c8945a0SNathan Whitehorn 
13614c8945a0SNathan Whitehorn     if (src == 0) {
13624c8945a0SNathan Whitehorn 	char temp[80];
13634c8945a0SNathan Whitehorn 	sprintf(temp, "Expected a numeric-parameter for %.20s", argv[*num]);
13644c8945a0SNathan Whitehorn 	Usage(temp);
13654c8945a0SNathan Whitehorn     }
13664c8945a0SNathan Whitehorn     *num = next;
13674c8945a0SNathan Whitehorn     return result;
13684c8945a0SNathan Whitehorn }
13694c8945a0SNathan Whitehorn 
1370a96ef450SBaptiste Daroussin /*
1371a96ef450SBaptiste Daroussin  * In findOption, we made provision for adding/removing a "no" prefix from
1372a96ef450SBaptiste Daroussin  * any boolean option.  This function determines the actual true/false result.
1373a96ef450SBaptiste Daroussin  */
1374a96ef450SBaptiste Daroussin static bool
optionBool(const char * actual,const Options * data)1375a96ef450SBaptiste Daroussin optionBool(const char *actual, const Options * data)
1376a96ef450SBaptiste Daroussin {
1377a96ef450SBaptiste Daroussin     bool normal = (data->type == tTrue) ? TRUE : FALSE;
1378a96ef450SBaptiste Daroussin     bool result = !strcmp(actual + 2, data->name) ? normal : !normal;
1379a96ef450SBaptiste Daroussin     if (result != normal) {
1380a96ef450SBaptiste Daroussin 	int want_no = (strncmp(actual + 2, "no", 2) == 0);
1381a96ef450SBaptiste Daroussin 	int have_no = (strncmp(data->name, "no", 2) == 0);
1382a96ef450SBaptiste Daroussin 	if (have_no == want_no)
1383a96ef450SBaptiste Daroussin 	    result = normal;
1384a96ef450SBaptiste Daroussin     }
1385a96ef450SBaptiste Daroussin     return result;
1386a96ef450SBaptiste Daroussin }
1387a96ef450SBaptiste Daroussin 
13882a3e3873SBaptiste Daroussin /* Return exit-code for a named button */
13892a3e3873SBaptiste Daroussin static int
button_code(const char * name)13902a3e3873SBaptiste Daroussin button_code(const char *name)
13912a3e3873SBaptiste Daroussin {
13922a3e3873SBaptiste Daroussin     /* *INDENT-OFF* */
13932a3e3873SBaptiste Daroussin     static struct {
13942a3e3873SBaptiste Daroussin 	const char *name;
13952a3e3873SBaptiste Daroussin 	int code;
13962a3e3873SBaptiste Daroussin     } table[] = {
13972a3e3873SBaptiste Daroussin 	{ "ok",	    DLG_EXIT_OK },
13982a3e3873SBaptiste Daroussin 	{ "yes",    DLG_EXIT_OK },
13992a3e3873SBaptiste Daroussin 	{ "cancel", DLG_EXIT_CANCEL },
14002a3e3873SBaptiste Daroussin 	{ "no",	    DLG_EXIT_CANCEL },
14012a3e3873SBaptiste Daroussin 	{ "help",   DLG_EXIT_HELP },
14022a3e3873SBaptiste Daroussin 	{ "extra",  DLG_EXIT_EXTRA },
14032a3e3873SBaptiste Daroussin     };
14042a3e3873SBaptiste Daroussin     /* *INDENT-ON* */
14052a3e3873SBaptiste Daroussin 
14062a3e3873SBaptiste Daroussin     int code = DLG_EXIT_ERROR;
14072a3e3873SBaptiste Daroussin     size_t i;
14082a3e3873SBaptiste Daroussin 
1409a96ef450SBaptiste Daroussin     for (i = 0; i < TableSize(table); i++) {
14102a3e3873SBaptiste Daroussin 	if (!dlg_strcmp(name, table[i].name)) {
14112a3e3873SBaptiste Daroussin 	    code = table[i].code;
14122a3e3873SBaptiste Daroussin 	    break;
14132a3e3873SBaptiste Daroussin 	}
14142a3e3873SBaptiste Daroussin     }
14152a3e3873SBaptiste Daroussin 
14162a3e3873SBaptiste Daroussin     if (code == DLG_EXIT_ERROR) {
14172a3e3873SBaptiste Daroussin 	char temp[80];
14182a3e3873SBaptiste Daroussin 	sprintf(temp, "Button name \"%.20s\" unknown", name);
14192a3e3873SBaptiste Daroussin 	Usage(temp);
14202a3e3873SBaptiste Daroussin     }
14212a3e3873SBaptiste Daroussin 
14222a3e3873SBaptiste Daroussin     return code;
14232a3e3873SBaptiste Daroussin }
14242a3e3873SBaptiste Daroussin 
14254c8945a0SNathan Whitehorn /*
1426f4f33ea0SBaptiste Daroussin  * If this is the last option, we do not want any error messages - just our
1427f4f33ea0SBaptiste Daroussin  * output.  Calling end_dialog() cancels the refresh() at the end of the
1428f4f33ea0SBaptiste Daroussin  * program as well.
1429f4f33ea0SBaptiste Daroussin  */
1430f4f33ea0SBaptiste Daroussin static void
IgnoreNonScreen(char ** argv,int offset)1431f4f33ea0SBaptiste Daroussin IgnoreNonScreen(char **argv, int offset)
1432f4f33ea0SBaptiste Daroussin {
1433f4f33ea0SBaptiste Daroussin     if (argv[offset + 1] == 0) {
1434f4f33ea0SBaptiste Daroussin 	ignore_unknown = TRUE;
1435f4f33ea0SBaptiste Daroussin 	end_dialog();
1436f4f33ea0SBaptiste Daroussin     }
1437f4f33ea0SBaptiste Daroussin }
1438f4f33ea0SBaptiste Daroussin 
1439f4f33ea0SBaptiste Daroussin static void
PrintTextOnly(char ** argv,int * offset,eOptions code)1440f4f33ea0SBaptiste Daroussin PrintTextOnly(char **argv, int *offset, eOptions code)
1441f4f33ea0SBaptiste Daroussin {
1442f4f33ea0SBaptiste Daroussin     /* TODO - handle two optional numeric params */
1443f4f33ea0SBaptiste Daroussin     char *text;
1444f4f33ea0SBaptiste Daroussin     int height = 0;
1445f4f33ea0SBaptiste Daroussin     int width = 0;
1446f4f33ea0SBaptiste Daroussin     int height2 = 0;
1447f4f33ea0SBaptiste Daroussin     int width2 = 0;
1448f4f33ea0SBaptiste Daroussin     int next = arg_rest(argv + *offset);
1449f4f33ea0SBaptiste Daroussin 
1450a96ef450SBaptiste Daroussin     if (LINES <= 0 && COLS <= 0) {
1451a96ef450SBaptiste Daroussin 	dlg_ttysize(fileno(dialog_state.input),
1452a96ef450SBaptiste Daroussin 		    &dialog_state.screen_height,
1453a96ef450SBaptiste Daroussin 		    &dialog_state.screen_width);
1454a96ef450SBaptiste Daroussin     }
1455f4f33ea0SBaptiste Daroussin 
1456f4f33ea0SBaptiste Daroussin     text = strdup(optionString(argv, offset));
1457f4f33ea0SBaptiste Daroussin     IgnoreNonScreen(argv, *offset);
1458f4f33ea0SBaptiste Daroussin 
1459f4f33ea0SBaptiste Daroussin     if (next >= 1) {
1460f4f33ea0SBaptiste Daroussin 	next = MIN(next, 3);
1461f4f33ea0SBaptiste Daroussin 	height = numeric_arg(argv, *offset + 1);
1462f4f33ea0SBaptiste Daroussin 	if (next >= 2)
1463f4f33ea0SBaptiste Daroussin 	    width = numeric_arg(argv, *offset + 2);
1464f4f33ea0SBaptiste Daroussin 	*offset += next - 1;
1465f4f33ea0SBaptiste Daroussin     }
1466f4f33ea0SBaptiste Daroussin 
1467f4f33ea0SBaptiste Daroussin     dlg_trim_string(text);
1468f4f33ea0SBaptiste Daroussin     dlg_auto_size(NULL, text, &height2, &width2, height, width);
1469f4f33ea0SBaptiste Daroussin 
1470f4f33ea0SBaptiste Daroussin     switch (code) {
1471a96ef450SBaptiste Daroussin     case o_text_only:
1472f4f33ea0SBaptiste Daroussin 	dialog_state.text_only = TRUE;
1473f4f33ea0SBaptiste Daroussin 	dlg_print_autowrap(stdscr, text, height2, width2);
1474f4f33ea0SBaptiste Daroussin 	dialog_state.text_only = FALSE;
1475f4f33ea0SBaptiste Daroussin 	break;
1476f4f33ea0SBaptiste Daroussin     case o_print_text_size:
1477f4f33ea0SBaptiste Daroussin 	fprintf(dialog_state.output, "%d %d\n",
1478f4f33ea0SBaptiste Daroussin 		dialog_state.text_height,
1479f4f33ea0SBaptiste Daroussin 		dialog_state.text_width);
1480f4f33ea0SBaptiste Daroussin 	break;
1481f4f33ea0SBaptiste Daroussin     default:
1482f4f33ea0SBaptiste Daroussin 	break;
1483f4f33ea0SBaptiste Daroussin     }
1484f4f33ea0SBaptiste Daroussin }
1485f4f33ea0SBaptiste Daroussin 
1486f4f33ea0SBaptiste Daroussin /*
14874c8945a0SNathan Whitehorn  * Print parts of a message
14884c8945a0SNathan Whitehorn  */
14894c8945a0SNathan Whitehorn static void
PrintList(const char * const * list)14904c8945a0SNathan Whitehorn PrintList(const char *const *list)
14914c8945a0SNathan Whitehorn {
14924c8945a0SNathan Whitehorn     const char *leaf = strrchr(program, '/');
14934c8945a0SNathan Whitehorn     unsigned n = 0;
14944c8945a0SNathan Whitehorn 
14954c8945a0SNathan Whitehorn     if (leaf != 0)
14964c8945a0SNathan Whitehorn 	leaf++;
14974c8945a0SNathan Whitehorn     else
14984c8945a0SNathan Whitehorn 	leaf = program;
14994c8945a0SNathan Whitehorn 
15004c8945a0SNathan Whitehorn     while (*list != 0) {
15014c8945a0SNathan Whitehorn 	fprintf(dialog_state.output, *list, n ? leaf : dialog_version());
15024c8945a0SNathan Whitehorn 	(void) fputc('\n', dialog_state.output);
15034c8945a0SNathan Whitehorn 	n = 1;
15044c8945a0SNathan Whitehorn 	list++;
15054c8945a0SNathan Whitehorn     }
15064c8945a0SNathan Whitehorn }
15074c8945a0SNathan Whitehorn 
15084c8945a0SNathan Whitehorn static const Mode *
lookupMode(eOptions code)15094c8945a0SNathan Whitehorn lookupMode(eOptions code)
15104c8945a0SNathan Whitehorn {
15114c8945a0SNathan Whitehorn     const Mode *modePtr = 0;
15124c8945a0SNathan Whitehorn     unsigned n;
15134c8945a0SNathan Whitehorn 
1514a96ef450SBaptiste Daroussin     for (n = 0; n < TableSize(modes); n++) {
15154c8945a0SNathan Whitehorn 	if (modes[n].code == code) {
15164c8945a0SNathan Whitehorn 	    modePtr = &modes[n];
15174c8945a0SNathan Whitehorn 	    break;
15184c8945a0SNathan Whitehorn 	}
15194c8945a0SNathan Whitehorn     }
15204c8945a0SNathan Whitehorn     return modePtr;
15214c8945a0SNathan Whitehorn }
15224c8945a0SNathan Whitehorn 
15234c8945a0SNathan Whitehorn static int
compare_opts(const void * a,const void * b)15244c8945a0SNathan Whitehorn compare_opts(const void *a, const void *b)
15254c8945a0SNathan Whitehorn {
15264c8945a0SNathan Whitehorn     Options *const *p = (Options * const *) a;
15274c8945a0SNathan Whitehorn     Options *const *q = (Options * const *) b;
15284c8945a0SNathan Whitehorn     return strcmp((*p)->name, (*q)->name);
15294c8945a0SNathan Whitehorn }
15304c8945a0SNathan Whitehorn 
15314c8945a0SNathan Whitehorn /*
1532682c9e0fSNathan Whitehorn  * Print program's version.
1533682c9e0fSNathan Whitehorn  */
1534682c9e0fSNathan Whitehorn static void
PrintVersion(FILE * fp)1535682c9e0fSNathan Whitehorn PrintVersion(FILE *fp)
1536682c9e0fSNathan Whitehorn {
1537682c9e0fSNathan Whitehorn     fprintf(fp, "Version: %s\n", dialog_version());
1538682c9e0fSNathan Whitehorn }
1539682c9e0fSNathan Whitehorn 
1540682c9e0fSNathan Whitehorn /*
15414c8945a0SNathan Whitehorn  * Print program help-message
15424c8945a0SNathan Whitehorn  */
15434c8945a0SNathan Whitehorn static void
Help(void)15444c8945a0SNathan Whitehorn Help(void)
15454c8945a0SNathan Whitehorn {
15464c8945a0SNathan Whitehorn     static const char *const tbl_1[] =
15474c8945a0SNathan Whitehorn     {
15484c8945a0SNathan Whitehorn 	"cdialog (ComeOn Dialog!) version %s",
1549a96ef450SBaptiste Daroussin 	"Copyright 2000-2020,2021 Thomas E. Dickey",
15504c8945a0SNathan Whitehorn 	"This is free software; see the source for copying conditions.  There is NO",
15514c8945a0SNathan Whitehorn 	"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.",
15524c8945a0SNathan Whitehorn 	"",
15534c8945a0SNathan Whitehorn 	"* Display dialog boxes from shell scripts *",
15544c8945a0SNathan Whitehorn 	"",
15554c8945a0SNathan Whitehorn 	"Usage: %s <options> { --and-widget <options> }",
15564c8945a0SNathan Whitehorn 	"where options are \"common\" options, followed by \"box\" options",
15574c8945a0SNathan Whitehorn 	"",
15584c8945a0SNathan Whitehorn #ifdef HAVE_RC_FILE
15594c8945a0SNathan Whitehorn 	"Special options:",
15604c8945a0SNathan Whitehorn 	"  [--create-rc \"file\"]",
15614c8945a0SNathan Whitehorn #endif
15624c8945a0SNathan Whitehorn 	0
15634c8945a0SNathan Whitehorn     }, *const tbl_3[] =
15644c8945a0SNathan Whitehorn     {
15654c8945a0SNathan Whitehorn 	"",
15664c8945a0SNathan Whitehorn 	"Auto-size with height and width = 0. Maximize with height and width = -1.",
15674c8945a0SNathan Whitehorn 	"Global-auto-size if also menu_height/list_height = 0.",
15684c8945a0SNathan Whitehorn 	0
15694c8945a0SNathan Whitehorn     };
1570a96ef450SBaptiste Daroussin     size_t limit = TableSize(options);
15717a1c0d96SNathan Whitehorn     size_t j, k;
15724c8945a0SNathan Whitehorn     const Options **opts;
15734c8945a0SNathan Whitehorn 
1574682c9e0fSNathan Whitehorn     end_dialog();
1575682c9e0fSNathan Whitehorn     dialog_state.output = stdout;
1576682c9e0fSNathan Whitehorn 
15774c8945a0SNathan Whitehorn     opts = dlg_calloc(const Options *, limit);
15784c8945a0SNathan Whitehorn     assert_ptr(opts, "Help");
15794c8945a0SNathan Whitehorn     for (j = 0; j < limit; ++j) {
15804c8945a0SNathan Whitehorn 	opts[j] = &(options[j]);
15814c8945a0SNathan Whitehorn     }
15824c8945a0SNathan Whitehorn     qsort(opts, limit, sizeof(Options *), compare_opts);
15834c8945a0SNathan Whitehorn 
15844c8945a0SNathan Whitehorn     PrintList(tbl_1);
15854c8945a0SNathan Whitehorn     fprintf(dialog_state.output, "Common options:\n ");
15864c8945a0SNathan Whitehorn     for (j = k = 0; j < limit; j++) {
15874c8945a0SNathan Whitehorn 	if ((opts[j]->pass & 1)
15884c8945a0SNathan Whitehorn 	    && opts[j]->help != 0) {
15897a1c0d96SNathan Whitehorn 	    size_t len = 6 + strlen(opts[j]->name) + strlen(opts[j]->help);
15904c8945a0SNathan Whitehorn 	    k += len;
15914c8945a0SNathan Whitehorn 	    if (k > 75) {
15924c8945a0SNathan Whitehorn 		fprintf(dialog_state.output, "\n ");
15934c8945a0SNathan Whitehorn 		k = len;
15944c8945a0SNathan Whitehorn 	    }
15954c8945a0SNathan Whitehorn 	    fprintf(dialog_state.output, " [--%s%s%s]", opts[j]->name,
15964c8945a0SNathan Whitehorn 		    *(opts[j]->help) ? " " : "", opts[j]->help);
15974c8945a0SNathan Whitehorn 	}
15984c8945a0SNathan Whitehorn     }
15994c8945a0SNathan Whitehorn     fprintf(dialog_state.output, "\nBox options:\n");
16004c8945a0SNathan Whitehorn     for (j = 0; j < limit; j++) {
16014c8945a0SNathan Whitehorn 	if ((opts[j]->pass & 2) != 0
16024c8945a0SNathan Whitehorn 	    && opts[j]->help != 0
1603f4f33ea0SBaptiste Daroussin 	    && lookupMode(opts[j]->code)) {
16044c8945a0SNathan Whitehorn 	    fprintf(dialog_state.output, "  --%-12s %s\n", opts[j]->name,
16054c8945a0SNathan Whitehorn 		    opts[j]->help);
16064c8945a0SNathan Whitehorn 	}
1607f4f33ea0SBaptiste Daroussin     }
16084c8945a0SNathan Whitehorn     PrintList(tbl_3);
16094c8945a0SNathan Whitehorn 
16104c8945a0SNathan Whitehorn     free(opts);
1611f4f33ea0SBaptiste Daroussin     handle_leaks();
16124c8945a0SNathan Whitehorn     dlg_exit(DLG_EXIT_OK);
16134c8945a0SNathan Whitehorn }
16144c8945a0SNathan Whitehorn 
16152a3e3873SBaptiste Daroussin #ifdef HAVE_DLG_TRACE
16162a3e3873SBaptiste Daroussin /*
16172a3e3873SBaptiste Daroussin  * Only the first call to dlg_trace will open a trace file.  But each time
16182a3e3873SBaptiste Daroussin  * --trace is parsed, we show the whole parameter list as it is at that moment,
16192a3e3873SBaptiste Daroussin  * counting discarded parameters.  The only way to capture the whole parameter
16202a3e3873SBaptiste Daroussin  * list is if --trace is the first option.
16212a3e3873SBaptiste Daroussin  */
16222a3e3873SBaptiste Daroussin static void
process_trace_option(char ** argv,int * offset)16232a3e3873SBaptiste Daroussin process_trace_option(char **argv, int *offset)
16242a3e3873SBaptiste Daroussin {
16252a3e3873SBaptiste Daroussin     int j;
16262a3e3873SBaptiste Daroussin 
16272a3e3873SBaptiste Daroussin     if (dialog_state.trace_output == 0) {
16282a3e3873SBaptiste Daroussin 	dlg_trace(optionString(argv, offset));
16292a3e3873SBaptiste Daroussin     } else {
1630f4f33ea0SBaptiste Daroussin 	DLG_TRACE(("# ignore extra --trace option\n"));
16312a3e3873SBaptiste Daroussin 	*offset += 1;
16322a3e3873SBaptiste Daroussin     }
16332a3e3873SBaptiste Daroussin 
1634f4f33ea0SBaptiste Daroussin     DLG_TRACE(("# Parameters:\n"));
16352a3e3873SBaptiste Daroussin     for (j = 0; argv[j] != 0; ++j) {
1636f4f33ea0SBaptiste Daroussin 	DLG_TRACE(("#\targv[%d] = %s\n", j, argv[j]));
16372a3e3873SBaptiste Daroussin     }
16382a3e3873SBaptiste Daroussin }
16392a3e3873SBaptiste Daroussin #endif
16402a3e3873SBaptiste Daroussin 
16414c8945a0SNathan Whitehorn /*
16424c8945a0SNathan Whitehorn  * "Common" options apply to all widgets more/less.  Most of the common options
16434c8945a0SNathan Whitehorn  * set values in dialog_vars, a few set dialog_state and a couple write to the
16444c8945a0SNathan Whitehorn  * output stream.
16454c8945a0SNathan Whitehorn  */
16464c8945a0SNathan Whitehorn static int
process_common_options(int argc,char ** argv,int offset,int output)1647a96ef450SBaptiste Daroussin process_common_options(int argc, char **argv, int offset, int output)
16484c8945a0SNathan Whitehorn {
1649a96ef450SBaptiste Daroussin     const Options *data = NULL;
16504c8945a0SNathan Whitehorn     bool done = FALSE;
16514c8945a0SNathan Whitehorn 
1652a96ef450SBaptiste Daroussin     DLG_TRACE(("# process_common_options, offset %d:%d\n", offset, argc));
16532a3e3873SBaptiste Daroussin 
1654a96ef450SBaptiste Daroussin     while (!done && offset < argc) {
1655a96ef450SBaptiste Daroussin 	static char empty[] = "";
1656a96ef450SBaptiste Daroussin 	eOptions code = o_unknown;
1657a96ef450SBaptiste Daroussin 	char *sval = empty;
1658a96ef450SBaptiste Daroussin 	bool bval;
1659a96ef450SBaptiste Daroussin 	int nval;
1660a96ef450SBaptiste Daroussin 	char *target;
1661a96ef450SBaptiste Daroussin 
1662f4f33ea0SBaptiste Daroussin 	DLG_TRACE(("#\targv[%d] = %s\n", offset, argv[offset]));
1663a96ef450SBaptiste Daroussin 	if ((data = findOption(argv[offset], 1, FALSE)) == NULL) {
1664a96ef450SBaptiste Daroussin 	    done = TRUE;
1665a96ef450SBaptiste Daroussin 	    continue;
1666a96ef450SBaptiste Daroussin 	}
1667a96ef450SBaptiste Daroussin 
1668a96ef450SBaptiste Daroussin 	switch (data->vars) {
1669a96ef450SBaptiste Daroussin 	default:
1670a96ef450SBaptiste Daroussin 	    target = NULL;
16714c8945a0SNathan Whitehorn 	    break;
1672a96ef450SBaptiste Daroussin 	case 1:
1673a96ef450SBaptiste Daroussin 	    target = (char *) &dialog_state;
16744c8945a0SNathan Whitehorn 	    break;
1675a96ef450SBaptiste Daroussin 	case 2:
1676a96ef450SBaptiste Daroussin 	    target = (char *) &dialog_vars;
16774c8945a0SNathan Whitehorn 	    break;
1678a96ef450SBaptiste Daroussin 	}
1679a96ef450SBaptiste Daroussin 
1680a96ef450SBaptiste Daroussin #define TraceTarget \
1681a96ef450SBaptiste Daroussin 	    ((target == (char *) &dialog_state) \
1682a96ef450SBaptiste Daroussin 	     ? "dialog_state" \
1683a96ef450SBaptiste Daroussin 	     : "dialog_vars")
1684a96ef450SBaptiste Daroussin #define TraceBVal (bval ? "TRUE" : "FALSE")
1685a96ef450SBaptiste Daroussin #define TraceDone(fmt,value) \
1686a96ef450SBaptiste Daroussin 	DLG_TRACE(("#\t.. set %s.%s = %"fmt"\n", TraceTarget, data->name, value))
1687a96ef450SBaptiste Daroussin #define TraceLate(fmt,value) \
1688a96ef450SBaptiste Daroussin 	DLG_TRACE(("#\t.. defer setting %s = %"fmt"\n", data->name, value))
1689a96ef450SBaptiste Daroussin 
1690a96ef450SBaptiste Daroussin 	code = data->code;
1691a96ef450SBaptiste Daroussin 	switch (data->type) {
1692a96ef450SBaptiste Daroussin 	default:
16934c8945a0SNathan Whitehorn 	    break;
1694a96ef450SBaptiste Daroussin 	case tFalse:
1695a96ef450SBaptiste Daroussin 	case tTrue:
1696a96ef450SBaptiste Daroussin 	    bval = optionBool(argv[offset], data);
1697a96ef450SBaptiste Daroussin 	    if (target != NULL) {
1698a96ef450SBaptiste Daroussin 		*(bool *) (target + data->offset) = bval;
1699a96ef450SBaptiste Daroussin 		TraceDone("s", TraceBVal);
1700a96ef450SBaptiste Daroussin 	    } else {
1701a96ef450SBaptiste Daroussin 		TraceLate("s", TraceBVal);
1702a96ef450SBaptiste Daroussin 	    }
17034c8945a0SNathan Whitehorn 	    break;
1704a96ef450SBaptiste Daroussin 	case tNumber:
1705a96ef450SBaptiste Daroussin 	    nval = optionValue(argv, &offset);
1706a96ef450SBaptiste Daroussin 	    if (target != NULL) {
1707a96ef450SBaptiste Daroussin 		*(int *) (void *) (target + data->offset) = nval;
1708a96ef450SBaptiste Daroussin 		TraceDone("d", nval);
1709a96ef450SBaptiste Daroussin 	    } else {
1710a96ef450SBaptiste Daroussin 		TraceLate("d", nval);
1711a96ef450SBaptiste Daroussin 	    }
17124c8945a0SNathan Whitehorn 	    break;
1713a96ef450SBaptiste Daroussin 	case tString:
1714a96ef450SBaptiste Daroussin 	    sval = optionString(argv, &offset);
1715a96ef450SBaptiste Daroussin 	    if (target != NULL) {
1716a96ef450SBaptiste Daroussin 		*(char **) (void *) (target + data->offset) = sval;
1717a96ef450SBaptiste Daroussin 		TraceDone("s", sval);
1718a96ef450SBaptiste Daroussin 	    } else {
1719a96ef450SBaptiste Daroussin 		TraceLate("s", sval);
1720a96ef450SBaptiste Daroussin 	    }
1721682c9e0fSNathan Whitehorn 	    break;
1722a96ef450SBaptiste Daroussin 	}
1723a96ef450SBaptiste Daroussin 
1724a96ef450SBaptiste Daroussin 	switch (code) {
17254c8945a0SNathan Whitehorn 	case o_defaultno:
17262a3e3873SBaptiste Daroussin 	    dialog_vars.default_button = DLG_EXIT_CANCEL;
17272a3e3873SBaptiste Daroussin 	    break;
17282a3e3873SBaptiste Daroussin 	case o_default_button:
1729a96ef450SBaptiste Daroussin 	    dialog_vars.default_button = button_code(sval);
17302a3e3873SBaptiste Daroussin 	    dialog_vars.defaultno = dialog_vars.default_button == DLG_EXIT_CANCEL;
17314c8945a0SNathan Whitehorn 	    break;
1732a96ef450SBaptiste Daroussin 	case o_text_only:
1733f4f33ea0SBaptiste Daroussin 	case o_print_text_size:
1734f4f33ea0SBaptiste Daroussin 	    PrintTextOnly(argv, &offset, code);
1735f4f33ea0SBaptiste Daroussin 	    break;
17364c8945a0SNathan Whitehorn 	case o_print_maxsize:
17374c8945a0SNathan Whitehorn 	    if (output) {
1738f4f33ea0SBaptiste Daroussin 		IgnoreNonScreen(argv, offset);
17394c8945a0SNathan Whitehorn 		fflush(dialog_state.output);
17404c8945a0SNathan Whitehorn 		fprintf(dialog_state.output, "MaxSize: %d, %d\n", SLINES, SCOLS);
17414c8945a0SNathan Whitehorn 	    }
17424c8945a0SNathan Whitehorn 	    break;
17434c8945a0SNathan Whitehorn 	case o_print_version:
17444c8945a0SNathan Whitehorn 	    if (output) {
1745682c9e0fSNathan Whitehorn 		PrintVersion(dialog_state.output);
17464c8945a0SNathan Whitehorn 	    }
17474c8945a0SNathan Whitehorn 	    break;
17484c8945a0SNathan Whitehorn 	case o_visit_items:
17492a3e3873SBaptiste Daroussin 	    dialog_state.visit_cols = 1;
17504c8945a0SNathan Whitehorn 	    break;
1751a96ef450SBaptiste Daroussin 	case o_begin_set:
1752a96ef450SBaptiste Daroussin #ifdef HAVE_WHIPTAIL
1753a96ef450SBaptiste Daroussin 	    if (!strcmp(argv[offset], "--topleft")) {
1754a96ef450SBaptiste Daroussin 		dialog_vars.begin_y = 0;
1755a96ef450SBaptiste Daroussin 		dialog_vars.begin_x = 0;
1756a96ef450SBaptiste Daroussin 	    } else
1757a96ef450SBaptiste Daroussin #endif
1758a96ef450SBaptiste Daroussin 	    {
17594c8945a0SNathan Whitehorn 		dialog_vars.begin_y = optionValue(argv, &offset);
17604c8945a0SNathan Whitehorn 		dialog_vars.begin_x = optionValue(argv, &offset);
1761a96ef450SBaptiste Daroussin 	    }
17624c8945a0SNathan Whitehorn 	    break;
17634c8945a0SNathan Whitehorn 	case o_ascii_lines:
17644c8945a0SNathan Whitehorn 	    dialog_vars.no_lines = FALSE;
17654c8945a0SNathan Whitehorn 	    break;
17664c8945a0SNathan Whitehorn 	case o_no_lines:
17674c8945a0SNathan Whitehorn 	    dialog_vars.ascii_lines = FALSE;
17684c8945a0SNathan Whitehorn 	    break;
17697a1c0d96SNathan Whitehorn 	case o_no_mouse:
1770682c9e0fSNathan Whitehorn 	    mouse_close();
17717a1c0d96SNathan Whitehorn 	    break;
17724c8945a0SNathan Whitehorn 	case o_unknown:
1773a96ef450SBaptiste Daroussin 	    done = !ignore_unknown;
1774a96ef450SBaptiste Daroussin 	default:
17754c8945a0SNathan Whitehorn 	    break;
17764c8945a0SNathan Whitehorn #ifdef HAVE_DLG_TRACE
17774c8945a0SNathan Whitehorn 	case o_trace:
17782a3e3873SBaptiste Daroussin 	    process_trace_option(argv, &offset);
17792a3e3873SBaptiste Daroussin 	    break;
17802a3e3873SBaptiste Daroussin #endif
1781f4f33ea0SBaptiste Daroussin 	case o_iso_week:
1782f4f33ea0SBaptiste Daroussin 	    if (dialog_vars.week_start == 0) {	/* Monday is implied */
1783f4f33ea0SBaptiste Daroussin 		static char default_1st[] = "1";
1784f4f33ea0SBaptiste Daroussin 		dialog_vars.week_start = default_1st;
1785f4f33ea0SBaptiste Daroussin 	    }
1786f4f33ea0SBaptiste Daroussin 	    break;
17874c8945a0SNathan Whitehorn 	}
17884c8945a0SNathan Whitehorn 	if (!done)
17894c8945a0SNathan Whitehorn 	    offset++;
17904c8945a0SNathan Whitehorn     }
1791f4f33ea0SBaptiste Daroussin 
1792f4f33ea0SBaptiste Daroussin     if (dialog_state.aspect_ratio == 0)
1793f4f33ea0SBaptiste Daroussin 	dialog_state.aspect_ratio = DEFAULT_ASPECT_RATIO;
1794f4f33ea0SBaptiste Daroussin 
17954c8945a0SNathan Whitehorn     return offset;
17964c8945a0SNathan Whitehorn }
17974c8945a0SNathan Whitehorn 
17984c8945a0SNathan Whitehorn /*
17994c8945a0SNathan Whitehorn  * Initialize options at the start of a series of common options culminating
18004c8945a0SNathan Whitehorn  * in a widget.
18014c8945a0SNathan Whitehorn  */
18024c8945a0SNathan Whitehorn static void
init_result(char * buffer)18034c8945a0SNathan Whitehorn init_result(char *buffer)
18044c8945a0SNathan Whitehorn {
18054c8945a0SNathan Whitehorn     static bool first = TRUE;
18064c8945a0SNathan Whitehorn 
1807f4f33ea0SBaptiste Daroussin     DLG_TRACE(("# init_result\n"));
18082a3e3873SBaptiste Daroussin 
18094c8945a0SNathan Whitehorn     /* clear everything we do not save for the next widget */
18104c8945a0SNathan Whitehorn     memset(&dialog_vars, 0, sizeof(dialog_vars));
18114c8945a0SNathan Whitehorn 
18124c8945a0SNathan Whitehorn     dialog_vars.input_result = buffer;
18134c8945a0SNathan Whitehorn     dialog_vars.input_result[0] = '\0';
18144c8945a0SNathan Whitehorn 
18152a3e3873SBaptiste Daroussin     dialog_vars.default_button = -1;
18162a3e3873SBaptiste Daroussin 
18174c8945a0SNathan Whitehorn     /*
18184c8945a0SNathan Whitehorn      * The first time this is called, check for common options given by an
18194c8945a0SNathan Whitehorn      * environment variable.
18204c8945a0SNathan Whitehorn      */
18214c8945a0SNathan Whitehorn     if (first) {
1822a96ef450SBaptiste Daroussin 	char *env = dlg_getenv_str("DIALOGOPTS");
18234c8945a0SNathan Whitehorn 	if (env != 0)
18244c8945a0SNathan Whitehorn 	    env = dlg_strclone(env);
18254c8945a0SNathan Whitehorn 	if (env != 0) {
18267a1c0d96SNathan Whitehorn 	    special_argv = dlg_string_to_argv(env);
18277a1c0d96SNathan Whitehorn 	    special_argc = dlg_count_argv(special_argv);
18284c8945a0SNathan Whitehorn 	}
18292a3e3873SBaptiste Daroussin 	first = FALSE;
18304c8945a0SNathan Whitehorn     }
18312a3e3873SBaptiste Daroussin 
18324c8945a0SNathan Whitehorn     if (special_argv != 0) {
18334c8945a0SNathan Whitehorn 	process_common_options(special_argc, special_argv, 0, FALSE);
18344c8945a0SNathan Whitehorn     }
18354c8945a0SNathan Whitehorn }
18364c8945a0SNathan Whitehorn 
18374c8945a0SNathan Whitehorn int
main(int argc,char * argv[])18384c8945a0SNathan Whitehorn main(int argc, char *argv[])
18394c8945a0SNathan Whitehorn {
18404c8945a0SNathan Whitehorn     char temp[256];
18414c8945a0SNathan Whitehorn     bool esc_pressed = FALSE;
18424c8945a0SNathan Whitehorn     bool keep_tite = FALSE;
1843a96ef450SBaptiste Daroussin     int initial = 1;
18444c8945a0SNathan Whitehorn     int offset = 1;
1845a96ef450SBaptiste Daroussin     int offset_add = 0;
18464c8945a0SNathan Whitehorn     int retval = DLG_EXIT_OK;
1847a96ef450SBaptiste Daroussin     int j;
18484c8945a0SNathan Whitehorn     eOptions code;
18494c8945a0SNathan Whitehorn     char my_buffer[MAX_LEN + 1];
18504c8945a0SNathan Whitehorn 
18514c8945a0SNathan Whitehorn     memset(&dialog_state, 0, sizeof(dialog_state));
18524c8945a0SNathan Whitehorn     memset(&dialog_vars, 0, sizeof(dialog_vars));
18534c8945a0SNathan Whitehorn 
18544c8945a0SNathan Whitehorn #if defined(ENABLE_NLS)
18554c8945a0SNathan Whitehorn     /* initialize locale support */
18564c8945a0SNathan Whitehorn     setlocale(LC_ALL, "");
18577a1c0d96SNathan Whitehorn     bindtextdomain(NLS_TEXTDOMAIN, LOCALEDIR);
18587a1c0d96SNathan Whitehorn     textdomain(NLS_TEXTDOMAIN);
18594c8945a0SNathan Whitehorn #elif defined(HAVE_SETLOCALE)
18604c8945a0SNathan Whitehorn     (void) setlocale(LC_ALL, "");
18614c8945a0SNathan Whitehorn #endif
18624c8945a0SNathan Whitehorn 
1863f4f33ea0SBaptiste Daroussin     init_result(my_buffer);	/* honor $DIALOGOPTS */
18644c8945a0SNathan Whitehorn     unescape_argv(&argc, &argv);
18654c8945a0SNathan Whitehorn     program = argv[0];
18664c8945a0SNathan Whitehorn     dialog_state.output = stderr;
18674c8945a0SNathan Whitehorn     dialog_state.input = stdin;
18684c8945a0SNathan Whitehorn 
18694c8945a0SNathan Whitehorn     /*
18704c8945a0SNathan Whitehorn      * Look for the last --stdout, --stderr or --output-fd option, and use
18714c8945a0SNathan Whitehorn      * that.  We can only write to one of them.  If --stdout is used, that
18724c8945a0SNathan Whitehorn      * can interfere with initializing the curses library, so we want to
18734c8945a0SNathan Whitehorn      * know explicitly if it is used.
1874682c9e0fSNathan Whitehorn      *
1875682c9e0fSNathan Whitehorn      * Also, look for any --version or --help message, processing those
1876682c9e0fSNathan Whitehorn      * immediately.
18774c8945a0SNathan Whitehorn      */
18784c8945a0SNathan Whitehorn     while (offset < argc) {
18794c8945a0SNathan Whitehorn 	int base = offset;
18804c8945a0SNathan Whitehorn 	switch (lookupOption(argv[offset], 7)) {
1881a96ef450SBaptiste Daroussin 	case o_output_stdout:
18824c8945a0SNathan Whitehorn 	    dialog_state.output = stdout;
18834c8945a0SNathan Whitehorn 	    break;
1884a96ef450SBaptiste Daroussin 	case o_output_stderr:
18854c8945a0SNathan Whitehorn 	    dialog_state.output = stderr;
18864c8945a0SNathan Whitehorn 	    break;
18874c8945a0SNathan Whitehorn 	case o_input_fd:
18884c8945a0SNathan Whitehorn 	    if ((j = optionValue(argv, &offset)) < 0
1889f4f33ea0SBaptiste Daroussin 		|| (dialog_state.input = fdopen(j, "r")) == 0) {
1890f4f33ea0SBaptiste Daroussin 		handle_leaks();
18914c8945a0SNathan Whitehorn 		dlg_exiterr("Cannot open input-fd\n");
1892f4f33ea0SBaptiste Daroussin 	    }
18934c8945a0SNathan Whitehorn 	    break;
18944c8945a0SNathan Whitehorn 	case o_output_fd:
18954c8945a0SNathan Whitehorn 	    if ((j = optionValue(argv, &offset)) < 0
1896f4f33ea0SBaptiste Daroussin 		|| (dialog_state.output = fdopen(j, "w")) == 0) {
1897f4f33ea0SBaptiste Daroussin 		handle_leaks();
18984c8945a0SNathan Whitehorn 		dlg_exiterr("Cannot open output-fd\n");
1899f4f33ea0SBaptiste Daroussin 	    }
19004c8945a0SNathan Whitehorn 	    break;
19014c8945a0SNathan Whitehorn 	case o_keep_tite:
19024c8945a0SNathan Whitehorn 	    keep_tite = TRUE;
19034c8945a0SNathan Whitehorn 	    break;
1904682c9e0fSNathan Whitehorn 	case o_version:
1905682c9e0fSNathan Whitehorn 	    dialog_state.output = stdout;
1906682c9e0fSNathan Whitehorn 	    PrintVersion(dialog_state.output);
1907f4f33ea0SBaptiste Daroussin 	    dlg_exit(DLG_EXIT_OK);
1908682c9e0fSNathan Whitehorn 	    break;
1909682c9e0fSNathan Whitehorn 	case o_help:
1910682c9e0fSNathan Whitehorn 	    Help();
1911682c9e0fSNathan Whitehorn 	    break;
19122a3e3873SBaptiste Daroussin #ifdef HAVE_DLG_TRACE
19132a3e3873SBaptiste Daroussin 	case o_trace:
19142a3e3873SBaptiste Daroussin 	    /*
19152a3e3873SBaptiste Daroussin 	     * Process/remove the --trace option if it is the first option.
19162a3e3873SBaptiste Daroussin 	     * Otherwise, process it in more/less expected order as a
19172a3e3873SBaptiste Daroussin 	     * "common" option.
19182a3e3873SBaptiste Daroussin 	     */
19192a3e3873SBaptiste Daroussin 	    if (base == 1) {
19202a3e3873SBaptiste Daroussin 		process_trace_option(argv, &offset);
19212a3e3873SBaptiste Daroussin 		break;
19222a3e3873SBaptiste Daroussin 	    } else {
19232a3e3873SBaptiste Daroussin 		++offset;
19242a3e3873SBaptiste Daroussin 		continue;
19252a3e3873SBaptiste Daroussin 	    }
19262a3e3873SBaptiste Daroussin #endif
19274c8945a0SNathan Whitehorn 	default:
19284c8945a0SNathan Whitehorn 	    ++offset;
19294c8945a0SNathan Whitehorn 	    continue;
19304c8945a0SNathan Whitehorn 	}
1931f4f33ea0SBaptiste Daroussin 	DLG_TRACE(("# discarding %d parameters starting with argv[%d] (%s)\n",
19322a3e3873SBaptiste Daroussin 		   1 + offset - base, base,
1933f4f33ea0SBaptiste Daroussin 		   argv[base]));
19344c8945a0SNathan Whitehorn 	for (j = base; j < argc; ++j) {
19354c8945a0SNathan Whitehorn 	    dialog_argv[j] = dialog_argv[j + 1 + (offset - base)];
19364c8945a0SNathan Whitehorn 	}
19374c8945a0SNathan Whitehorn 	argc -= (1 + offset - base);
19384c8945a0SNathan Whitehorn 	offset = base;
19394c8945a0SNathan Whitehorn     }
19404c8945a0SNathan Whitehorn     offset = 1;
19414c8945a0SNathan Whitehorn     init_result(my_buffer);
1942a96ef450SBaptiste Daroussin     dialog_vars.keep_tite |= keep_tite;		/* init_result() cleared global */
19434c8945a0SNathan Whitehorn 
1944682c9e0fSNathan Whitehorn     /*
1945682c9e0fSNathan Whitehorn      * Dialog's output may be redirected (see above).  Handle the special
1946682c9e0fSNathan Whitehorn      * case of options that only report information without interaction.
1947682c9e0fSNathan Whitehorn      */
1948682c9e0fSNathan Whitehorn     if (argc == 2) {
1949f4f33ea0SBaptiste Daroussin 	switch (code = lookupOption(argv[1], 7)) {
19504c8945a0SNathan Whitehorn 	case o_print_maxsize:
19514c8945a0SNathan Whitehorn 	    (void) initscr();
19524c8945a0SNathan Whitehorn 	    endwin();
19534c8945a0SNathan Whitehorn 	    fflush(dialog_state.output);
19544c8945a0SNathan Whitehorn 	    fprintf(dialog_state.output, "MaxSize: %d, %d\n", SLINES, SCOLS);
19554c8945a0SNathan Whitehorn 	    break;
19564c8945a0SNathan Whitehorn 	case o_print_version:
1957682c9e0fSNathan Whitehorn 	    PrintVersion(dialog_state.output);
19584c8945a0SNathan Whitehorn 	    break;
1959a96ef450SBaptiste Daroussin 	case o_dlg_clear_screen:
19604c8945a0SNathan Whitehorn 	    initscr();
19614c8945a0SNathan Whitehorn 	    refresh();
1962a96ef450SBaptiste Daroussin 	    dlg_keep_tite((dialog_state.output == stdout) ? stderr : stdout);
19634c8945a0SNathan Whitehorn 	    endwin();
19644c8945a0SNathan Whitehorn 	    break;
19654c8945a0SNathan Whitehorn 	case o_ignore:
19664c8945a0SNathan Whitehorn 	    break;
19674c8945a0SNathan Whitehorn 	default:
19684c8945a0SNathan Whitehorn 	    Help();
19694c8945a0SNathan Whitehorn 	    break;
19704c8945a0SNathan Whitehorn 	}
1971f4f33ea0SBaptiste Daroussin 	dlg_exit(DLG_EXIT_OK);
1972f4f33ea0SBaptiste Daroussin     } else if (argc < 2) {
19734c8945a0SNathan Whitehorn 	Help();
19744c8945a0SNathan Whitehorn     }
19754c8945a0SNathan Whitehorn #ifdef HAVE_RC_FILE
1976f4f33ea0SBaptiste Daroussin     else if (lookupOption(argv[1], 7) == o_create_rc) {
19774c8945a0SNathan Whitehorn 	if (argc != 3) {
19784c8945a0SNathan Whitehorn 	    sprintf(temp, "Expected a filename for %.50s", argv[1]);
19794c8945a0SNathan Whitehorn 	    Usage(temp);
19804c8945a0SNathan Whitehorn 	}
1981f4f33ea0SBaptiste Daroussin 	if (dlg_parse_rc() == -1) {	/* Read the configuration file */
1982f4f33ea0SBaptiste Daroussin 	    handle_leaks();
19834c8945a0SNathan Whitehorn 	    dlg_exiterr("dialog: dlg_parse_rc");
1984f4f33ea0SBaptiste Daroussin 	}
19854c8945a0SNathan Whitehorn 	dlg_create_rc(argv[2]);
1986f4f33ea0SBaptiste Daroussin 	dlg_exit(DLG_EXIT_OK);
19874c8945a0SNathan Whitehorn     }
19884c8945a0SNathan Whitehorn #endif
1989f4f33ea0SBaptiste Daroussin     else {
1990f4f33ea0SBaptiste Daroussin 	/*
1991f4f33ea0SBaptiste Daroussin 	 * Handle combinations of common options including --print-text-only
1992f4f33ea0SBaptiste Daroussin 	 * which can be done before involving curses, in case we can exit
1993f4f33ea0SBaptiste Daroussin 	 * without initializing curses (and writing to the terminal).
1994f4f33ea0SBaptiste Daroussin 	 */
1995a96ef450SBaptiste Daroussin 	initial = process_common_options(argc, argv, offset, TRUE);
1996a96ef450SBaptiste Daroussin 	if (initial >= argc)
1997f4f33ea0SBaptiste Daroussin 	    dlg_exit(DLG_EXIT_OK);
1998f4f33ea0SBaptiste Daroussin     }
19994c8945a0SNathan Whitehorn 
20004c8945a0SNathan Whitehorn     init_dialog(dialog_state.input, dialog_state.output);
20014c8945a0SNathan Whitehorn 
20024c8945a0SNathan Whitehorn     while (offset < argc && !esc_pressed) {
2003a96ef450SBaptiste Daroussin 	int have;
2004a96ef450SBaptiste Daroussin 	const Mode *modePtr;
2005a96ef450SBaptiste Daroussin 
20064c8945a0SNathan Whitehorn 	init_result(my_buffer);
2007a96ef450SBaptiste Daroussin 	offset = process_common_options(argc, argv, offset, offset > initial);
20084c8945a0SNathan Whitehorn 
20094c8945a0SNathan Whitehorn 	if (argv[offset] == NULL) {
20104c8945a0SNathan Whitehorn 	    if (ignore_unknown)
20114c8945a0SNathan Whitehorn 		break;
20124c8945a0SNathan Whitehorn 	    Usage("Expected a box option");
20134c8945a0SNathan Whitehorn 	}
20144c8945a0SNathan Whitehorn 
2015f4f33ea0SBaptiste Daroussin 	if (dialog_vars.separate_output) {
2016f4f33ea0SBaptiste Daroussin 	    switch (lookupOption(argv[offset], 2)) {
2017f4f33ea0SBaptiste Daroussin #ifdef HAVE_XDIALOG2
2018f4f33ea0SBaptiste Daroussin 	    case o_buildlist:
2019f4f33ea0SBaptiste Daroussin 	    case o_treeview:
2020f4f33ea0SBaptiste Daroussin #endif
2021f4f33ea0SBaptiste Daroussin 	    case o_checklist:
2022f4f33ea0SBaptiste Daroussin 		break;
2023f4f33ea0SBaptiste Daroussin 	    default:
2024f4f33ea0SBaptiste Daroussin 		sprintf(temp,
2025f4f33ea0SBaptiste Daroussin 			"Unexpected widget with --separate-output %.20s",
2026f4f33ea0SBaptiste Daroussin 			argv[offset]);
20274c8945a0SNathan Whitehorn 		Usage(temp);
20284c8945a0SNathan Whitehorn 	    }
2029f4f33ea0SBaptiste Daroussin 	}
20304c8945a0SNathan Whitehorn 
20314c8945a0SNathan Whitehorn 	dlg_put_backtitle();
20324c8945a0SNathan Whitehorn 
20334c8945a0SNathan Whitehorn 	/* use a table to look for the requested mode, to avoid code duplication */
20344c8945a0SNathan Whitehorn 
20354c8945a0SNathan Whitehorn 	modePtr = 0;
20364c8945a0SNathan Whitehorn 	if ((code = lookupOption(argv[offset], 2)) != o_unknown)
20374c8945a0SNathan Whitehorn 	    modePtr = lookupMode(code);
20384c8945a0SNathan Whitehorn 	if (modePtr == 0) {
20394c8945a0SNathan Whitehorn 	    sprintf(temp, "%s option %.20s",
20404c8945a0SNathan Whitehorn 		    lookupOption(argv[offset], 7) != o_unknown
20414c8945a0SNathan Whitehorn 		    ? "Unexpected"
20424c8945a0SNathan Whitehorn 		    : "Unknown",
20434c8945a0SNathan Whitehorn 		    argv[offset]);
20444c8945a0SNathan Whitehorn 	    Usage(temp);
20454c8945a0SNathan Whitehorn 	}
20464c8945a0SNathan Whitehorn 
20474c8945a0SNathan Whitehorn 	have = arg_rest(&argv[offset]);
20484c8945a0SNathan Whitehorn 	if (have < modePtr->argmin) {
20494c8945a0SNathan Whitehorn 	    sprintf(temp, "Expected at least %d tokens for %.20s, have %d",
20504c8945a0SNathan Whitehorn 		    modePtr->argmin - 1, argv[offset],
20514c8945a0SNathan Whitehorn 		    have - 1);
20524c8945a0SNathan Whitehorn 	    Usage(temp);
20534c8945a0SNathan Whitehorn 	}
20544c8945a0SNathan Whitehorn 	if (modePtr->argmax && have > modePtr->argmax) {
20554c8945a0SNathan Whitehorn 	    sprintf(temp,
20564c8945a0SNathan Whitehorn 		    "Expected no more than %d tokens for %.20s, have %d",
20574c8945a0SNathan Whitehorn 		    modePtr->argmax - 1, argv[offset],
20584c8945a0SNathan Whitehorn 		    have - 1);
20594c8945a0SNathan Whitehorn 	    Usage(temp);
20604c8945a0SNathan Whitehorn 	}
20614c8945a0SNathan Whitehorn 
20624c8945a0SNathan Whitehorn 	/*
20634c8945a0SNathan Whitehorn 	 * Trim whitespace from non-title option values, e.g., the ones that
20644c8945a0SNathan Whitehorn 	 * will be used as captions or prompts.   Do that only for the widget
20654c8945a0SNathan Whitehorn 	 * we are about to process, since the "--trim" option is reset before
20664c8945a0SNathan Whitehorn 	 * accumulating options for each widget.
20674c8945a0SNathan Whitehorn 	 */
20684c8945a0SNathan Whitehorn 	for (j = offset + 1; j <= offset + have; j++) {
20694c8945a0SNathan Whitehorn 	    switch (lookupOption(argv[j - 1], 7)) {
20704c8945a0SNathan Whitehorn 	    case o_unknown:
20714c8945a0SNathan Whitehorn 	    case o_title:
20724c8945a0SNathan Whitehorn 	    case o_backtitle:
2073682c9e0fSNathan Whitehorn 	    case o_help_line:
2074682c9e0fSNathan Whitehorn 	    case o_help_file:
20754c8945a0SNathan Whitehorn 		break;
20764c8945a0SNathan Whitehorn 	    default:
20774c8945a0SNathan Whitehorn 		if (argv[j] != 0) {
20782a3e3873SBaptiste Daroussin 		    char *argv_j = strdup(argv[j]);
20792a3e3873SBaptiste Daroussin 		    if (argv_j != 0) {
20802a3e3873SBaptiste Daroussin 			dlg_trim_string(argv_j);
20812a3e3873SBaptiste Daroussin 			argv[j] = argv_j;
20822a3e3873SBaptiste Daroussin 		    } else {
20832a3e3873SBaptiste Daroussin 			argv[j] = strdup("?");
20842a3e3873SBaptiste Daroussin 		    }
2085f4f33ea0SBaptiste Daroussin 		    ignore_leak(argv[j]);
20864c8945a0SNathan Whitehorn 		}
20874c8945a0SNathan Whitehorn 		break;
20884c8945a0SNathan Whitehorn 	    }
20894c8945a0SNathan Whitehorn 	}
20904c8945a0SNathan Whitehorn 
2091f4f33ea0SBaptiste Daroussin 	DLG_TRACE(("# execute %s\n", argv[offset]));
20924c8945a0SNathan Whitehorn 	retval = show_result((*(modePtr->jumper)) (dialog_vars.title,
20934c8945a0SNathan Whitehorn 						   argv + offset,
20944c8945a0SNathan Whitehorn 						   &offset_add));
2095f4f33ea0SBaptiste Daroussin 	DLG_TRACE(("# widget returns %d\n", retval));
20964c8945a0SNathan Whitehorn 	offset += offset_add;
20974c8945a0SNathan Whitehorn 
20987a1c0d96SNathan Whitehorn 	if (dialog_vars.input_result != my_buffer) {
20994c8945a0SNathan Whitehorn 	    free(dialog_vars.input_result);
21007a1c0d96SNathan Whitehorn 	    dialog_vars.input_result = 0;
21017a1c0d96SNathan Whitehorn 	}
21024c8945a0SNathan Whitehorn 
21034c8945a0SNathan Whitehorn 	if (retval == DLG_EXIT_ESC) {
21044c8945a0SNathan Whitehorn 	    esc_pressed = TRUE;
21054c8945a0SNathan Whitehorn 	} else {
21064c8945a0SNathan Whitehorn 
21074c8945a0SNathan Whitehorn 	    if (dialog_vars.beep_after_signal)
21084c8945a0SNathan Whitehorn 		(void) beep();
21094c8945a0SNathan Whitehorn 
21104c8945a0SNathan Whitehorn 	    if (dialog_vars.sleep_secs)
21114c8945a0SNathan Whitehorn 		(void) napms(dialog_vars.sleep_secs * 1000);
21124c8945a0SNathan Whitehorn 
21134c8945a0SNathan Whitehorn 	    if (offset < argc) {
21144c8945a0SNathan Whitehorn 		switch (lookupOption(argv[offset], 7)) {
21154c8945a0SNathan Whitehorn 		case o_and_widget:
21164c8945a0SNathan Whitehorn 		    offset++;
21174c8945a0SNathan Whitehorn 		    break;
21184c8945a0SNathan Whitehorn 		case o_unknown:
21194c8945a0SNathan Whitehorn 		    sprintf(temp, "Expected --and-widget, not %.20s",
21204c8945a0SNathan Whitehorn 			    argv[offset]);
21214c8945a0SNathan Whitehorn 		    Usage(temp);
21224c8945a0SNathan Whitehorn 		    break;
21234c8945a0SNathan Whitehorn 		default:
21244c8945a0SNathan Whitehorn 		    /* if we got a cancel, etc., stop chaining */
21254c8945a0SNathan Whitehorn 		    if (retval != DLG_EXIT_OK)
21264c8945a0SNathan Whitehorn 			esc_pressed = TRUE;
21274c8945a0SNathan Whitehorn 		    else
21284c8945a0SNathan Whitehorn 			dialog_vars.dlg_clear_screen = TRUE;
21294c8945a0SNathan Whitehorn 		    break;
21304c8945a0SNathan Whitehorn 		}
21314c8945a0SNathan Whitehorn 	    }
21324c8945a0SNathan Whitehorn 	    if (dialog_vars.dlg_clear_screen)
21334c8945a0SNathan Whitehorn 		dlg_clear();
21344c8945a0SNathan Whitehorn 	}
21354c8945a0SNathan Whitehorn     }
21364c8945a0SNathan Whitehorn 
21374c8945a0SNathan Whitehorn     dlg_killall_bg(&retval);
21384c8945a0SNathan Whitehorn     if (dialog_state.screen_initialized) {
21394c8945a0SNathan Whitehorn 	(void) refresh();
21404c8945a0SNathan Whitehorn 	end_dialog();
21414c8945a0SNathan Whitehorn     }
2142f4f33ea0SBaptiste Daroussin     handle_leaks();
21434c8945a0SNathan Whitehorn     dlg_exit(retval);
21444c8945a0SNathan Whitehorn }
2145