xref: /freebsd/contrib/dialog/dialog.c (revision f4f33ea0)
14c8945a0SNathan Whitehorn /*
2f4f33ea0SBaptiste Daroussin  * $Id: dialog.c,v 1.268 2018/06/21 09:16:05 tom Exp $
34c8945a0SNathan Whitehorn  *
44c8945a0SNathan Whitehorn  *  cdialog - Display simple dialog boxes from shell scripts
54c8945a0SNathan Whitehorn  *
6f4f33ea0SBaptiste Daroussin  *  Copyright 2000-2017,2018	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>
284c8945a0SNathan Whitehorn #include <string.h>
294c8945a0SNathan Whitehorn #include <sys/types.h>
304c8945a0SNathan Whitehorn #include <sys/stat.h>
314c8945a0SNathan Whitehorn 
324c8945a0SNathan Whitehorn #ifdef HAVE_SETLOCALE
334c8945a0SNathan Whitehorn #include <locale.h>
344c8945a0SNathan Whitehorn #endif
354c8945a0SNathan Whitehorn 
364c8945a0SNathan Whitehorn #define PASSARGS             t,       av,        offset_add
374c8945a0SNathan Whitehorn #define CALLARGS const char *t, char *av[], int *offset_add
384c8945a0SNathan Whitehorn typedef int (callerFn) (CALLARGS);
394c8945a0SNathan Whitehorn 
404c8945a0SNathan Whitehorn typedef enum {
414c8945a0SNathan Whitehorn     o_unknown = 0
424c8945a0SNathan Whitehorn     ,o_allow_close
434c8945a0SNathan Whitehorn     ,o_and_widget
444c8945a0SNathan Whitehorn     ,o_ascii_lines
454c8945a0SNathan Whitehorn     ,o_aspect
464c8945a0SNathan Whitehorn     ,o_auto_placement
474c8945a0SNathan Whitehorn     ,o_backtitle
484c8945a0SNathan Whitehorn     ,o_beep
494c8945a0SNathan Whitehorn     ,o_beep_after
504c8945a0SNathan Whitehorn     ,o_begin
514c8945a0SNathan Whitehorn     ,o_cancel_label
524c8945a0SNathan Whitehorn     ,o_checklist
534c8945a0SNathan Whitehorn     ,o_clear
544c8945a0SNathan Whitehorn     ,o_colors
554c8945a0SNathan Whitehorn     ,o_column_separator
564c8945a0SNathan Whitehorn     ,o_cr_wrap
574c8945a0SNathan Whitehorn     ,o_create_rc
584c8945a0SNathan Whitehorn     ,o_date_format
592a3e3873SBaptiste Daroussin     ,o_default_button
604c8945a0SNathan Whitehorn     ,o_default_item
614c8945a0SNathan Whitehorn     ,o_defaultno
624c8945a0SNathan Whitehorn     ,o_exit_label
634c8945a0SNathan Whitehorn     ,o_extra_button
644c8945a0SNathan Whitehorn     ,o_extra_label
654c8945a0SNathan Whitehorn     ,o_fixed_font
664c8945a0SNathan Whitehorn     ,o_form
674c8945a0SNathan Whitehorn     ,o_gauge
684c8945a0SNathan Whitehorn     ,o_help
694c8945a0SNathan Whitehorn     ,o_help_button
70682c9e0fSNathan Whitehorn     ,o_help_file
714c8945a0SNathan Whitehorn     ,o_help_label
72682c9e0fSNathan Whitehorn     ,o_help_line
734c8945a0SNathan Whitehorn     ,o_help_status
74febdb468SDevin Teske     ,o_help_tags
754c8945a0SNathan Whitehorn     ,o_icon
764c8945a0SNathan Whitehorn     ,o_ignore
774c8945a0SNathan Whitehorn     ,o_infobox
784c8945a0SNathan Whitehorn     ,o_input_fd
794c8945a0SNathan Whitehorn     ,o_inputbox
804c8945a0SNathan Whitehorn     ,o_inputmenu
814c8945a0SNathan Whitehorn     ,o_insecure
824c8945a0SNathan Whitehorn     ,o_item_help
834c8945a0SNathan Whitehorn     ,o_keep_colors
844c8945a0SNathan Whitehorn     ,o_keep_tite
854c8945a0SNathan Whitehorn     ,o_keep_window
862a3e3873SBaptiste Daroussin     ,o_last_key
874c8945a0SNathan Whitehorn     ,o_max_input
884c8945a0SNathan Whitehorn     ,o_menu
894c8945a0SNathan Whitehorn     ,o_mixedform
904c8945a0SNathan Whitehorn     ,o_mixedgauge
914c8945a0SNathan Whitehorn     ,o_msgbox
924c8945a0SNathan Whitehorn     ,o_no_close
934c8945a0SNathan Whitehorn     ,o_no_collapse
944c8945a0SNathan Whitehorn     ,o_no_cr_wrap
954c8945a0SNathan Whitehorn     ,o_no_kill
964c8945a0SNathan Whitehorn     ,o_no_label
974c8945a0SNathan Whitehorn     ,o_no_lines
987a1c0d96SNathan Whitehorn     ,o_no_mouse
99682c9e0fSNathan Whitehorn     ,o_no_nl_expand
1004c8945a0SNathan Whitehorn     ,o_no_shadow
1014c8945a0SNathan Whitehorn     ,o_nocancel
1024c8945a0SNathan Whitehorn     ,o_nook
1034c8945a0SNathan Whitehorn     ,o_ok_label
1044c8945a0SNathan Whitehorn     ,o_output_fd
1054c8945a0SNathan Whitehorn     ,o_output_separator
1064c8945a0SNathan Whitehorn     ,o_passwordbox
1074c8945a0SNathan Whitehorn     ,o_passwordform
1084c8945a0SNathan Whitehorn     ,o_pause
1097a1c0d96SNathan Whitehorn     ,o_prgbox
1104c8945a0SNathan Whitehorn     ,o_print_maxsize
1114c8945a0SNathan Whitehorn     ,o_print_size
112f4f33ea0SBaptiste Daroussin     ,o_print_text_only
113f4f33ea0SBaptiste Daroussin     ,o_print_text_size
1144c8945a0SNathan Whitehorn     ,o_print_version
1157a1c0d96SNathan Whitehorn     ,o_programbox
1164c8945a0SNathan Whitehorn     ,o_progressbox
1174c8945a0SNathan Whitehorn     ,o_quoted
1184c8945a0SNathan Whitehorn     ,o_radiolist
1194c8945a0SNathan Whitehorn     ,o_screen_center
1204c8945a0SNathan Whitehorn     ,o_scrollbar
1214c8945a0SNathan Whitehorn     ,o_separate_output
1224c8945a0SNathan Whitehorn     ,o_separate_widget
1234c8945a0SNathan Whitehorn     ,o_separator
1244c8945a0SNathan Whitehorn     ,o_shadow
1254c8945a0SNathan Whitehorn     ,o_single_quoted
1264c8945a0SNathan Whitehorn     ,o_size_err
1274c8945a0SNathan Whitehorn     ,o_sleep
1284c8945a0SNathan Whitehorn     ,o_smooth
1294c8945a0SNathan Whitehorn     ,o_stderr
1304c8945a0SNathan Whitehorn     ,o_stdout
1314c8945a0SNathan Whitehorn     ,o_tab_correct
1324c8945a0SNathan Whitehorn     ,o_tab_len
1334c8945a0SNathan Whitehorn     ,o_tailbox
1344c8945a0SNathan Whitehorn     ,o_tailboxbg
1354c8945a0SNathan Whitehorn     ,o_textbox
1364c8945a0SNathan Whitehorn     ,o_time_format
1374c8945a0SNathan Whitehorn     ,o_timeout
1384c8945a0SNathan Whitehorn     ,o_title
1394c8945a0SNathan Whitehorn     ,o_trim
1404c8945a0SNathan Whitehorn     ,o_under_mouse
141682c9e0fSNathan Whitehorn     ,o_version
1424c8945a0SNathan Whitehorn     ,o_visit_items
1434c8945a0SNathan Whitehorn     ,o_wmclass
1444c8945a0SNathan Whitehorn     ,o_yes_label
1454c8945a0SNathan Whitehorn     ,o_yesno
1462a3e3873SBaptiste Daroussin #ifdef HAVE_WHIPTAIL
1472a3e3873SBaptiste Daroussin     ,o_fullbutton
1482a3e3873SBaptiste Daroussin     ,o_topleft
1492a3e3873SBaptiste Daroussin #endif
1502a3e3873SBaptiste Daroussin #ifdef HAVE_XDIALOG
1512a3e3873SBaptiste Daroussin     ,o_calendar
1522a3e3873SBaptiste Daroussin     ,o_dselect
1532a3e3873SBaptiste Daroussin     ,o_editbox
1542a3e3873SBaptiste Daroussin     ,o_fselect
1552a3e3873SBaptiste Daroussin     ,o_timebox
156f4f33ea0SBaptiste Daroussin     ,o_week_start
1572a3e3873SBaptiste Daroussin #endif
1582a3e3873SBaptiste Daroussin #ifdef HAVE_XDIALOG2
1592a3e3873SBaptiste Daroussin     ,o_buildlist
1602a3e3873SBaptiste Daroussin     ,o_rangebox
161f4f33ea0SBaptiste Daroussin     ,o_reorder
1622a3e3873SBaptiste Daroussin     ,o_treeview
1632a3e3873SBaptiste Daroussin #endif
1642a3e3873SBaptiste Daroussin #if defined(HAVE_XDIALOG2) || defined(HAVE_WHIPTAIL)
1652a3e3873SBaptiste Daroussin     ,o_no_items
1662a3e3873SBaptiste Daroussin     ,o_no_tags
1672a3e3873SBaptiste Daroussin #endif
1684c8945a0SNathan Whitehorn #ifdef HAVE_DLG_TRACE
1694c8945a0SNathan Whitehorn     ,o_trace
1704c8945a0SNathan Whitehorn #endif
171f4f33ea0SBaptiste Daroussin     ,o_iso_week
1724c8945a0SNathan Whitehorn } eOptions;
1734c8945a0SNathan Whitehorn 
1744c8945a0SNathan Whitehorn /*
1754c8945a0SNathan Whitehorn  * The bits in 'pass' are used to decide which options are applicable at
1764c8945a0SNathan Whitehorn  * different stages in the program:
1774c8945a0SNathan Whitehorn  *	1 flags before widgets
1784c8945a0SNathan Whitehorn  *	2 widgets
1794c8945a0SNathan Whitehorn  *	4 non-widget options
1804c8945a0SNathan Whitehorn  */
1814c8945a0SNathan Whitehorn typedef struct {
1824c8945a0SNathan Whitehorn     const char *name;
1834c8945a0SNathan Whitehorn     eOptions code;
1844c8945a0SNathan Whitehorn     int pass;			/* 1,2,4 or combination */
1854c8945a0SNathan Whitehorn     const char *help;		/* NULL to suppress, non-empty to display params */
1864c8945a0SNathan Whitehorn } Options;
1874c8945a0SNathan Whitehorn 
1884c8945a0SNathan Whitehorn typedef struct {
1894c8945a0SNathan Whitehorn     eOptions code;
1904c8945a0SNathan Whitehorn     int argmin, argmax;
1914c8945a0SNathan Whitehorn     callerFn *jumper;
1924c8945a0SNathan Whitehorn } Mode;
1934c8945a0SNathan Whitehorn 
194f4f33ea0SBaptiste Daroussin static int known_opts = 0;
195f4f33ea0SBaptiste Daroussin static const char **dialog_opts;
1964c8945a0SNathan Whitehorn static char **dialog_argv;
1974c8945a0SNathan Whitehorn 
198f4f33ea0SBaptiste Daroussin static char **special_argv = 0;
199f4f33ea0SBaptiste Daroussin static int special_argc = 0;
200f4f33ea0SBaptiste Daroussin 
2014c8945a0SNathan Whitehorn static bool ignore_unknown = FALSE;
2024c8945a0SNathan Whitehorn 
2034c8945a0SNathan Whitehorn static const char *program = "dialog";
2044c8945a0SNathan Whitehorn 
205f4f33ea0SBaptiste Daroussin #ifdef NO_LEAKS
206f4f33ea0SBaptiste Daroussin typedef struct _all_blobs {
207f4f33ea0SBaptiste Daroussin     struct _all_blobs *next;
208f4f33ea0SBaptiste Daroussin     void *blob;
209f4f33ea0SBaptiste Daroussin } AllBlobs;
210f4f33ea0SBaptiste Daroussin 
211f4f33ea0SBaptiste Daroussin static AllBlobs *all_blobs;
212f4f33ea0SBaptiste Daroussin #endif
213f4f33ea0SBaptiste Daroussin 
2144c8945a0SNathan Whitehorn /*
2154c8945a0SNathan Whitehorn  * The options[] table is organized this way to make it simple to maintain
2164c8945a0SNathan Whitehorn  * a sorted list of options for the help-message.
2174c8945a0SNathan Whitehorn  */
2184c8945a0SNathan Whitehorn /* *INDENT-OFF* */
2194c8945a0SNathan Whitehorn static const Options options[] = {
2204c8945a0SNathan Whitehorn     { "allow-close",	o_allow_close,		1, NULL },
2214c8945a0SNathan Whitehorn     { "and-widget",	o_and_widget,		4, NULL },
2224c8945a0SNathan Whitehorn     { "ascii-lines",	o_ascii_lines, 		1, "" },
2234c8945a0SNathan Whitehorn     { "aspect",		o_aspect,		1, "<ratio>" },
2244c8945a0SNathan Whitehorn     { "auto-placement", o_auto_placement,	1, NULL },
2254c8945a0SNathan Whitehorn     { "backtitle",	o_backtitle,		1, "<backtitle>" },
2262a3e3873SBaptiste Daroussin     { "beep",		o_beep,			1, "" },
2272a3e3873SBaptiste Daroussin     { "beep-after",	o_beep_after,		1, "" },
2284c8945a0SNathan Whitehorn     { "begin",		o_begin,		1, "<y> <x>" },
2294c8945a0SNathan Whitehorn     { "cancel-label",	o_cancel_label,		1, "<str>" },
2304c8945a0SNathan Whitehorn     { "checklist",	o_checklist,		2, "<text> <height> <width> <list height> <tag1> <item1> <status1>..." },
2314c8945a0SNathan Whitehorn     { "clear",		o_clear,		1, "" },
2324c8945a0SNathan Whitehorn     { "colors",		o_colors,		1, "" },
2334c8945a0SNathan Whitehorn     { "column-separator",o_column_separator,	1, "<str>" },
2344c8945a0SNathan Whitehorn     { "cr-wrap",	o_cr_wrap,		1, "" },
2354c8945a0SNathan Whitehorn     { "create-rc",	o_create_rc,		1, NULL },
2364c8945a0SNathan Whitehorn     { "date-format",	o_date_format,		1, "<str>" },
2372a3e3873SBaptiste Daroussin     { "default-button",	o_default_button,	1, "<str>" },
2384c8945a0SNathan Whitehorn     { "default-item",	o_default_item,		1, "<str>" },
2394c8945a0SNathan Whitehorn     { "defaultno",	o_defaultno,		1, "" },
2404c8945a0SNathan Whitehorn     { "exit-label",	o_exit_label,		1, "<str>" },
2414c8945a0SNathan Whitehorn     { "extra-button",	o_extra_button,		1, "" },
2424c8945a0SNathan Whitehorn     { "extra-label",	o_extra_label,		1, "<str>" },
2434c8945a0SNathan Whitehorn     { "fixed-font",	o_fixed_font,		1, NULL },
2444c8945a0SNathan Whitehorn     { "form",		o_form,			2, "<text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1>..." },
2454c8945a0SNathan Whitehorn     { "gauge",		o_gauge,		2, "<text> <height> <width> [<percent>]" },
2464c8945a0SNathan Whitehorn     { "guage",		o_gauge,		2, NULL },
2474c8945a0SNathan Whitehorn     { "help",		o_help,			4, "" },
2484c8945a0SNathan Whitehorn     { "help-button",	o_help_button,		1, "" },
2494c8945a0SNathan Whitehorn     { "help-label",	o_help_label,		1, "<str>" },
2504c8945a0SNathan Whitehorn     { "help-status",	o_help_status,		1, "" },
251febdb468SDevin Teske     { "help-tags",	o_help_tags,		1, "" },
252682c9e0fSNathan Whitehorn     { "hfile",		o_help_file,		1, "<str>" },
253682c9e0fSNathan Whitehorn     { "hline",		o_help_line,		1, "<str>" },
2544c8945a0SNathan Whitehorn     { "icon",		o_icon,			1, NULL },
2554c8945a0SNathan Whitehorn     { "ignore",		o_ignore,		1, "" },
2564c8945a0SNathan Whitehorn     { "infobox",	o_infobox,		2, "<text> <height> <width>" },
2574c8945a0SNathan Whitehorn     { "input-fd",	o_input_fd,		1, "<fd>" },
2584c8945a0SNathan Whitehorn     { "inputbox",	o_inputbox,		2, "<text> <height> <width> [<init>]" },
2594c8945a0SNathan Whitehorn     { "inputmenu",	o_inputmenu,		2, "<text> <height> <width> <menu height> <tag1> <item1>..." },
2604c8945a0SNathan Whitehorn     { "insecure",	o_insecure,		1, "" },
2614c8945a0SNathan Whitehorn     { "item-help",	o_item_help,		1, "" },
2624c8945a0SNathan Whitehorn     { "keep-colors",	o_keep_colors,		1, NULL },
2634c8945a0SNathan Whitehorn     { "keep-tite",	o_keep_tite,		1, "" },
2644c8945a0SNathan Whitehorn     { "keep-window",	o_keep_window,		1, "" },
2652a3e3873SBaptiste Daroussin     { "last-key",	o_last_key,		1, "" },
2664c8945a0SNathan Whitehorn     { "max-input",	o_max_input,		1, "<n>" },
2674c8945a0SNathan Whitehorn     { "menu",		o_menu,			2, "<text> <height> <width> <menu height> <tag1> <item1>..." },
2684c8945a0SNathan Whitehorn     { "mixedform",	o_mixedform,		2, "<text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1> <itype>..." },
2694c8945a0SNathan Whitehorn     { "mixedgauge",	o_mixedgauge,		2, "<text> <height> <width> <percent> <tag1> <item1>..." },
2704c8945a0SNathan Whitehorn     { "msgbox",		o_msgbox,		2, "<text> <height> <width>" },
2714c8945a0SNathan Whitehorn     { "no-cancel",	o_nocancel,		1, "" },
2724c8945a0SNathan Whitehorn     { "no-close",	o_no_close,		1, NULL },
2734c8945a0SNathan Whitehorn     { "no-collapse",	o_no_collapse,		1, "" },
2742a3e3873SBaptiste Daroussin     { "no-cr-wrap",	o_no_cr_wrap,		1, "" },
2754c8945a0SNathan Whitehorn     { "no-kill",	o_no_kill,		1, "" },
2764c8945a0SNathan Whitehorn     { "no-label",	o_no_label,		1, "<str>" },
2774c8945a0SNathan Whitehorn     { "no-lines",	o_no_lines, 		1, "" },
2787a1c0d96SNathan Whitehorn     { "no-mouse",	o_no_mouse,		1, "" },
279682c9e0fSNathan Whitehorn     { "no-nl-expand",	o_no_nl_expand,		1, "" },
2804c8945a0SNathan Whitehorn     { "no-ok",		o_nook,			1, "" },
2814c8945a0SNathan Whitehorn     { "no-shadow",	o_no_shadow,		1, "" },
2824c8945a0SNathan Whitehorn     { "nocancel",	o_nocancel,		1, NULL }, /* see --no-cancel */
2834c8945a0SNathan Whitehorn     { "nook",		o_nook,			1, "" }, /* See no-ok */
2844c8945a0SNathan Whitehorn     { "ok-label",	o_ok_label,		1, "<str>" },
2854c8945a0SNathan Whitehorn     { "output-fd",	o_output_fd,		1, "<fd>" },
2864c8945a0SNathan Whitehorn     { "output-separator",o_output_separator,	1, "<str>" },
2874c8945a0SNathan Whitehorn     { "passwordbox",	o_passwordbox,		2, "<text> <height> <width> [<init>]" },
2884c8945a0SNathan Whitehorn     { "passwordform",	o_passwordform,		2, "<text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1>..." },
2894c8945a0SNathan Whitehorn     { "pause",		o_pause,		2, "<text> <height> <width> <seconds>" },
2907a1c0d96SNathan Whitehorn     { "prgbox",		o_prgbox,		2, "<text> <command> <height> <width>" },
2914c8945a0SNathan Whitehorn     { "print-maxsize",	o_print_maxsize,	1, "" },
2924c8945a0SNathan Whitehorn     { "print-size",	o_print_size,		1, "" },
293f4f33ea0SBaptiste Daroussin     { "print-text-only",o_print_text_only,	5, "<text> <height> <width>" },
294f4f33ea0SBaptiste Daroussin     { "print-text-size",o_print_text_size,	5, "<text> <height> <width>" },
2954c8945a0SNathan Whitehorn     { "print-version",	o_print_version,	5, "" },
2967a1c0d96SNathan Whitehorn     { "programbox",	o_programbox,		2, "<text> <height> <width>" },
2977a1c0d96SNathan Whitehorn     { "progressbox",	o_progressbox,		2, "<text> <height> <width>" },
2984c8945a0SNathan Whitehorn     { "quoted",		o_quoted,		1, "" },
2994c8945a0SNathan Whitehorn     { "radiolist",	o_radiolist,		2, "<text> <height> <width> <list height> <tag1> <item1> <status1>..." },
3004c8945a0SNathan Whitehorn     { "screen-center",	o_screen_center,	1, NULL },
3014c8945a0SNathan Whitehorn     { "scrollbar",	o_scrollbar,		1, "" },
3024c8945a0SNathan Whitehorn     { "separate-output",o_separate_output,	1, "" },
3034c8945a0SNathan Whitehorn     { "separate-widget",o_separate_widget,	1, "<str>" },
3044c8945a0SNathan Whitehorn     { "separator",	o_separator,		1, NULL },
3054c8945a0SNathan Whitehorn     { "shadow",		o_shadow,		1, "" },
3064c8945a0SNathan Whitehorn     { "single-quoted",	o_single_quoted,	1, "" },
3074c8945a0SNathan Whitehorn     { "size-err",	o_size_err,		1, "" },
3084c8945a0SNathan Whitehorn     { "sleep",		o_sleep,		1, "<secs>" },
3094c8945a0SNathan Whitehorn     { "smooth",		o_smooth,		1, NULL },
3104c8945a0SNathan Whitehorn     { "stderr",		o_stderr,		1, "" },
3114c8945a0SNathan Whitehorn     { "stdout",		o_stdout,		1, "" },
3124c8945a0SNathan Whitehorn     { "tab-correct",	o_tab_correct,		1, "" },
3134c8945a0SNathan Whitehorn     { "tab-len",	o_tab_len,		1, "<n>" },
3144c8945a0SNathan Whitehorn     { "tailbox",	o_tailbox,		2, "<file> <height> <width>" },
3154c8945a0SNathan Whitehorn     { "tailboxbg",	o_tailboxbg,		2, "<file> <height> <width>" },
3164c8945a0SNathan Whitehorn     { "textbox",	o_textbox,		2, "<file> <height> <width>" },
3174c8945a0SNathan Whitehorn     { "time-format",	o_time_format,		1, "<str>" },
3184c8945a0SNathan Whitehorn     { "timeout",	o_timeout,		1, "<secs>" },
3194c8945a0SNathan Whitehorn     { "title",		o_title,		1, "<title>" },
3204c8945a0SNathan Whitehorn     { "trim",		o_trim,			1, "" },
3214c8945a0SNathan Whitehorn     { "under-mouse", 	o_under_mouse,		1, NULL },
322682c9e0fSNathan Whitehorn     { "version",	o_version,		5, "" },
3234c8945a0SNathan Whitehorn     { "visit-items", 	o_visit_items,		1, "" },
3244c8945a0SNathan Whitehorn     { "wmclass",	o_wmclass,		1, NULL },
3254c8945a0SNathan Whitehorn     { "yes-label",	o_yes_label,		1, "<str>" },
3264c8945a0SNathan Whitehorn     { "yesno",		o_yesno,		2, "<text> <height> <width>" },
3272a3e3873SBaptiste Daroussin #ifdef HAVE_WHIPTAIL
3282a3e3873SBaptiste Daroussin     { "cancel-button",	o_cancel_label,		1, NULL },
3292a3e3873SBaptiste Daroussin     { "fb",		o_fullbutton,		1, NULL },
3302a3e3873SBaptiste Daroussin     { "fullbutton",	o_fullbutton,		1, NULL },
3312a3e3873SBaptiste Daroussin     { "no-button",	o_no_label,		1, NULL },
3322a3e3873SBaptiste Daroussin     { "ok-button",	o_ok_label,		1, NULL },
3332a3e3873SBaptiste Daroussin     { "scrolltext",	o_scrollbar,		1, NULL },
3342a3e3873SBaptiste Daroussin     { "topleft",	o_topleft,		1, NULL },
3352a3e3873SBaptiste Daroussin     { "yes-button",	o_yes_label,		1, NULL },
3362a3e3873SBaptiste Daroussin #endif
3372a3e3873SBaptiste Daroussin #ifdef HAVE_XDIALOG
3382a3e3873SBaptiste Daroussin     { "calendar",	o_calendar,		2, "<text> <height> <width> <day> <month> <year>" },
3392a3e3873SBaptiste Daroussin     { "dselect",	o_dselect,		2, "<directory> <height> <width>" },
3402a3e3873SBaptiste Daroussin     { "editbox",	o_editbox,		2, "<file> <height> <width>" },
3412a3e3873SBaptiste Daroussin     { "fselect",	o_fselect,		2, "<filepath> <height> <width>" },
3422a3e3873SBaptiste Daroussin     { "timebox",	o_timebox,		2, "<text> <height> <width> <hour> <minute> <second>" },
343f4f33ea0SBaptiste Daroussin     { "week-start",	o_week_start,		1, "<str>" },
344f4f33ea0SBaptiste Daroussin     { "iso-week",	o_iso_week,		1, NULL },
3452a3e3873SBaptiste Daroussin #endif
3462a3e3873SBaptiste Daroussin #ifdef HAVE_XDIALOG2
347f4f33ea0SBaptiste Daroussin     { "buildlist",	o_buildlist,		2, "<text> <height> <width> <list-height> <tag1> <item1> <status1>..." },
3482a3e3873SBaptiste Daroussin     { "no-items", 	o_no_items,		1, "" },
3492a3e3873SBaptiste Daroussin     { "no-tags", 	o_no_tags,		1, "" },
3502a3e3873SBaptiste Daroussin     { "rangebox",	o_rangebox,		2, "<text> <height> <width> <min-value> <max-value> <default-value>" },
351f4f33ea0SBaptiste Daroussin     { "reorder", 	o_reorder,		1, "" },
3522a3e3873SBaptiste Daroussin     { "treeview",	o_treeview,		2, "<text> <height> <width> <list-height> <tag1> <item1> <status1> <depth1>..." },
3532a3e3873SBaptiste Daroussin #endif
3542a3e3873SBaptiste Daroussin #if defined(HAVE_XDIALOG2) || defined(HAVE_WHIPTAIL)
3552a3e3873SBaptiste Daroussin     { "noitem", 	o_no_items,		1, NULL },
3562a3e3873SBaptiste Daroussin     { "notags", 	o_no_tags,		1, NULL },
3572a3e3873SBaptiste Daroussin #endif
3584c8945a0SNathan Whitehorn #ifdef HAVE_DLG_TRACE
3594c8945a0SNathan Whitehorn     { "trace",		o_trace,		1, "<file>" },
3604c8945a0SNathan Whitehorn #endif
3614c8945a0SNathan Whitehorn };
3624c8945a0SNathan Whitehorn /* *INDENT-ON* */
3634c8945a0SNathan Whitehorn 
364f4f33ea0SBaptiste Daroussin #ifdef NO_LEAKS
365f4f33ea0SBaptiste Daroussin static void
366f4f33ea0SBaptiste Daroussin ignore_leak(void *value)
367f4f33ea0SBaptiste Daroussin {
368f4f33ea0SBaptiste Daroussin     AllBlobs *next = dlg_calloc(AllBlobs, (size_t) 1);
369f4f33ea0SBaptiste Daroussin     if (next != 0) {
370f4f33ea0SBaptiste Daroussin 	next->blob = value;
371f4f33ea0SBaptiste Daroussin 	next->next = all_blobs;
372f4f33ea0SBaptiste Daroussin 	all_blobs = next;
373f4f33ea0SBaptiste Daroussin     }
374f4f33ea0SBaptiste Daroussin }
375f4f33ea0SBaptiste Daroussin 
376f4f33ea0SBaptiste Daroussin static void
377f4f33ea0SBaptiste Daroussin handle_leaks(void)
378f4f33ea0SBaptiste Daroussin {
379f4f33ea0SBaptiste Daroussin     while (all_blobs != 0) {
380f4f33ea0SBaptiste Daroussin 	char *blob = all_blobs->blob;
381f4f33ea0SBaptiste Daroussin 	AllBlobs *next = all_blobs->next;
382f4f33ea0SBaptiste Daroussin 	free(blob);
383f4f33ea0SBaptiste Daroussin 	free(all_blobs);
384f4f33ea0SBaptiste Daroussin 	all_blobs = next;
385f4f33ea0SBaptiste Daroussin     }
386f4f33ea0SBaptiste Daroussin     free(dialog_opts);
387f4f33ea0SBaptiste Daroussin     if (special_argv != 0) {
388f4f33ea0SBaptiste Daroussin 	free(special_argv[0]);
389f4f33ea0SBaptiste Daroussin 	free(special_argv);
390f4f33ea0SBaptiste Daroussin 	special_argv = 0;
391f4f33ea0SBaptiste Daroussin 	special_argc = 0;
392f4f33ea0SBaptiste Daroussin     }
393f4f33ea0SBaptiste Daroussin }
394f4f33ea0SBaptiste Daroussin #else
395f4f33ea0SBaptiste Daroussin #define handle_leaks()		/* nothing */
396f4f33ea0SBaptiste Daroussin #define ignore_leak(n)		/* nothing */
397f4f33ea0SBaptiste Daroussin #endif
398f4f33ea0SBaptiste Daroussin 
399f4f33ea0SBaptiste Daroussin #define OptionChars "\
400f4f33ea0SBaptiste Daroussin 0123456789\
401f4f33ea0SBaptiste Daroussin -\
402f4f33ea0SBaptiste Daroussin abcdefghijklmnopqrstuvwxyz\
403f4f33ea0SBaptiste Daroussin "
404f4f33ea0SBaptiste Daroussin 
405f4f33ea0SBaptiste Daroussin /*
406f4f33ea0SBaptiste Daroussin  * Check if the given string from main's argv is an option.
407f4f33ea0SBaptiste Daroussin  */
408f4f33ea0SBaptiste Daroussin static bool
409f4f33ea0SBaptiste Daroussin isOption(const char *arg)
410f4f33ea0SBaptiste Daroussin {
411f4f33ea0SBaptiste Daroussin     bool result = FALSE;
412f4f33ea0SBaptiste Daroussin 
413f4f33ea0SBaptiste Daroussin     if (arg != 0) {
414f4f33ea0SBaptiste Daroussin 	if (dialog_opts != 0) {
415f4f33ea0SBaptiste Daroussin 	    int n;
416f4f33ea0SBaptiste Daroussin 	    for (n = 0; dialog_opts[n] != 0; ++n) {
417f4f33ea0SBaptiste Daroussin 		if (dialog_opts[n] == arg) {
418f4f33ea0SBaptiste Daroussin 		    result = TRUE;
419f4f33ea0SBaptiste Daroussin 		    break;
420f4f33ea0SBaptiste Daroussin 		}
421f4f33ea0SBaptiste Daroussin 	    }
422f4f33ea0SBaptiste Daroussin 	} else if (!strncmp(arg, "--", (size_t) 2) && isalpha(UCH(arg[2]))) {
423f4f33ea0SBaptiste Daroussin 	    if (strlen(arg) == (strspn) (arg, OptionChars)) {
424f4f33ea0SBaptiste Daroussin 		result = TRUE;
425f4f33ea0SBaptiste Daroussin 	    } else {
426f4f33ea0SBaptiste Daroussin 		handle_leaks();
427f4f33ea0SBaptiste Daroussin 		dlg_exiterr("Invalid option \"%s\"", arg);
428f4f33ea0SBaptiste Daroussin 	    }
429f4f33ea0SBaptiste Daroussin 	}
430f4f33ea0SBaptiste Daroussin     }
431f4f33ea0SBaptiste Daroussin     return result;
432f4f33ea0SBaptiste Daroussin }
433f4f33ea0SBaptiste Daroussin 
4344c8945a0SNathan Whitehorn /*
4354c8945a0SNathan Whitehorn  * Make an array showing which argv[] entries are options.  Use "--" as a
4364c8945a0SNathan Whitehorn  * special token to escape the next argument, allowing it to begin with "--".
4374c8945a0SNathan Whitehorn  * When we find a "--" argument, also remove it from argv[] and adjust argc.
4384c8945a0SNathan Whitehorn  * That appears to be an undocumented feature of the popt library.
4394c8945a0SNathan Whitehorn  *
4404c8945a0SNathan Whitehorn  * Also, if we see a "--file", expand it into the parameter list by reading the
4414c8945a0SNathan Whitehorn  * text from the given file and stripping quotes, treating whitespace outside
4424c8945a0SNathan Whitehorn  * quotes as a parameter delimiter.
4434c8945a0SNathan Whitehorn  *
4444c8945a0SNathan Whitehorn  * Finally, if we see a "--args", dump the current list of arguments to the
4454c8945a0SNathan Whitehorn  * standard error.  This is used for debugging complex --file combinations.
4464c8945a0SNathan Whitehorn  */
4474c8945a0SNathan Whitehorn static void
4484c8945a0SNathan Whitehorn unescape_argv(int *argcp, char ***argvp)
4494c8945a0SNathan Whitehorn {
4504c8945a0SNathan Whitehorn     int j, k;
4514c8945a0SNathan Whitehorn     int limit_includes = 20 + *argcp;
4524c8945a0SNathan Whitehorn     int count_includes = 0;
4534c8945a0SNathan Whitehorn     bool doalloc = FALSE;
4544c8945a0SNathan Whitehorn     char *filename;
455f4f33ea0SBaptiste Daroussin     const char **my_argv = 0;
456f4f33ea0SBaptiste Daroussin     int my_argc;
4574c8945a0SNathan Whitehorn 
458f4f33ea0SBaptiste Daroussin     DLG_TRACE(("# unescape_argv\n"));
459f4f33ea0SBaptiste Daroussin     for (k = 0; k < 2; ++k) {
460f4f33ea0SBaptiste Daroussin 
461f4f33ea0SBaptiste Daroussin 	my_argc = 0;
462f4f33ea0SBaptiste Daroussin 	if (special_argv != 0) {
463f4f33ea0SBaptiste Daroussin 	    for (j = 0; special_argv[j] != 0; ++j) {
464f4f33ea0SBaptiste Daroussin 		if (!strcmp(special_argv[j], "--")) {
465f4f33ea0SBaptiste Daroussin 		    break;
466f4f33ea0SBaptiste Daroussin 		} else if (isOption(special_argv[j])) {
467f4f33ea0SBaptiste Daroussin 		    if (k != 0)
468f4f33ea0SBaptiste Daroussin 			my_argv[my_argc] = special_argv[j];
469f4f33ea0SBaptiste Daroussin 		    my_argc++;
470f4f33ea0SBaptiste Daroussin 		}
471f4f33ea0SBaptiste Daroussin 	    }
472f4f33ea0SBaptiste Daroussin 	}
473f4f33ea0SBaptiste Daroussin 
474f4f33ea0SBaptiste Daroussin 	if (k == 0) {
475f4f33ea0SBaptiste Daroussin 	    my_argc += (*argcp + 1);
476f4f33ea0SBaptiste Daroussin 	    my_argv = dlg_calloc(const char *, (size_t) my_argc);
477f4f33ea0SBaptiste Daroussin 	    assert_ptr(my_argv, "unescape_argv");
478f4f33ea0SBaptiste Daroussin 	}
479f4f33ea0SBaptiste Daroussin     }
4804c8945a0SNathan Whitehorn 
4814c8945a0SNathan Whitehorn     for (j = 1; j < *argcp; j++) {
4824c8945a0SNathan Whitehorn 	bool escaped = FALSE;
4834c8945a0SNathan Whitehorn 	if (!strcmp((*argvp)[j], "--")) {
4844c8945a0SNathan Whitehorn 	    escaped = TRUE;
485f4f33ea0SBaptiste Daroussin 	    dlg_eat_argv(argcp, argvp, j, 1);
4864c8945a0SNathan Whitehorn 	} else if (!strcmp((*argvp)[j], "--args")) {
4874c8945a0SNathan Whitehorn 	    fprintf(stderr, "Showing arguments at arg%d\n", j);
4884c8945a0SNathan Whitehorn 	    for (k = 0; k < *argcp; ++k) {
4894c8945a0SNathan Whitehorn 		fprintf(stderr, " arg%d:%s\n", k, (*argvp)[k]);
4904c8945a0SNathan Whitehorn 	    }
491f4f33ea0SBaptiste Daroussin 	    dlg_eat_argv(argcp, argvp, j, 1);
492f4f33ea0SBaptiste Daroussin 	    --j;
4934c8945a0SNathan Whitehorn 	} else if (!strcmp((*argvp)[j], "--file")) {
494f4f33ea0SBaptiste Daroussin 	    if (++count_includes > limit_includes) {
495f4f33ea0SBaptiste Daroussin 		handle_leaks();
4964c8945a0SNathan Whitehorn 		dlg_exiterr("Too many --file options");
497f4f33ea0SBaptiste Daroussin 	    }
4984c8945a0SNathan Whitehorn 
4994c8945a0SNathan Whitehorn 	    if ((filename = (*argvp)[j + 1]) != 0) {
5004c8945a0SNathan Whitehorn 		FILE *fp;
5014c8945a0SNathan Whitehorn 		char **list;
5024c8945a0SNathan Whitehorn 		char *blob;
5034c8945a0SNathan Whitehorn 		int added;
5044c8945a0SNathan Whitehorn 		size_t bytes_read;
5054c8945a0SNathan Whitehorn 		size_t length;
5064c8945a0SNathan Whitehorn 		int n;
5074c8945a0SNathan Whitehorn 
5084c8945a0SNathan Whitehorn 		if (*filename == '&') {
5094c8945a0SNathan Whitehorn 		    fp = fdopen(atoi(filename + sizeof(char)), "r");
5104c8945a0SNathan Whitehorn 		} else {
5114c8945a0SNathan Whitehorn 		    fp = fopen(filename, "r");
5124c8945a0SNathan Whitehorn 		}
5134c8945a0SNathan Whitehorn 
5144c8945a0SNathan Whitehorn 		if (fp) {
515f4f33ea0SBaptiste Daroussin 		    DLG_TRACE(("# opened --file %s ..\n", filename));
5164c8945a0SNathan Whitehorn 		    blob = NULL;
5174c8945a0SNathan Whitehorn 		    length = 0;
5184c8945a0SNathan Whitehorn 		    do {
5194c8945a0SNathan Whitehorn 			blob = dlg_realloc(char, length + BUFSIZ + 1, blob);
5204c8945a0SNathan Whitehorn 			assert_ptr(blob, "unescape_argv");
5214c8945a0SNathan Whitehorn 			bytes_read = fread(blob + length,
5224c8945a0SNathan Whitehorn 					   sizeof(char),
5237a1c0d96SNathan Whitehorn 					     (size_t) BUFSIZ,
5244c8945a0SNathan Whitehorn 					   fp);
5254c8945a0SNathan Whitehorn 			length += bytes_read;
526f4f33ea0SBaptiste Daroussin 			if (ferror(fp)) {
527f4f33ea0SBaptiste Daroussin 			    handle_leaks();
5284c8945a0SNathan Whitehorn 			    dlg_exiterr("error on filehandle in unescape_argv");
529f4f33ea0SBaptiste Daroussin 			}
5304c8945a0SNathan Whitehorn 		    } while (bytes_read == BUFSIZ);
5314c8945a0SNathan Whitehorn 		    fclose(fp);
5324c8945a0SNathan Whitehorn 
5334c8945a0SNathan Whitehorn 		    blob[length] = '\0';
534f4f33ea0SBaptiste Daroussin 		    ignore_leak(blob);
5354c8945a0SNathan Whitehorn 
5367a1c0d96SNathan Whitehorn 		    list = dlg_string_to_argv(blob);
537f4f33ea0SBaptiste Daroussin 		    added = dlg_count_argv(list);
5384c8945a0SNathan Whitehorn 		    if (added > 2) {
539f4f33ea0SBaptiste Daroussin 			/* *argcp arguments before the expansion of --file
540f4f33ea0SBaptiste Daroussin 			   - 2 for the removal of '--file <filepath>'
541f4f33ea0SBaptiste Daroussin 			   + added for the arguments contained in <filepath>
542f4f33ea0SBaptiste Daroussin 			   + 1 for the terminating NULL pointer */
543f4f33ea0SBaptiste Daroussin 			size_t need = (size_t) (*argcp + added - 1);
5444c8945a0SNathan Whitehorn 			if (doalloc) {
5454c8945a0SNathan Whitehorn 			    *argvp = dlg_realloc(char *, need, *argvp);
5464c8945a0SNathan Whitehorn 			    assert_ptr(*argvp, "unescape_argv");
5474c8945a0SNathan Whitehorn 			} else {
5484c8945a0SNathan Whitehorn 			    char **newp = dlg_malloc(char *, need);
549f4f33ea0SBaptiste Daroussin 			    ignore_leak(newp);
5504c8945a0SNathan Whitehorn 			    assert_ptr(newp, "unescape_argv");
5514c8945a0SNathan Whitehorn 			    for (n = 0; n < *argcp; ++n) {
5524c8945a0SNathan Whitehorn 				newp[n] = (*argvp)[n];
5534c8945a0SNathan Whitehorn 			    }
554f4f33ea0SBaptiste Daroussin 			    /* The new array is not NULL-terminated yet. */
5554c8945a0SNathan Whitehorn 			    *argvp = newp;
5564c8945a0SNathan Whitehorn 			    doalloc = TRUE;
5574c8945a0SNathan Whitehorn 			}
558f4f33ea0SBaptiste Daroussin 			my_argv = dlg_realloc(const char *, need, my_argv);
559f4f33ea0SBaptiste Daroussin 			assert_ptr(my_argv, "unescape_argv");
560f4f33ea0SBaptiste Daroussin 
561f4f33ea0SBaptiste Daroussin 			/* Shift the arguments after '--file <filepath>'
562f4f33ea0SBaptiste Daroussin 			   right by (added - 2) positions */
563f4f33ea0SBaptiste Daroussin 			for (n = *argcp - 1; n >= j + 2; --n) {
5644c8945a0SNathan Whitehorn 			    (*argvp)[n + added - 2] = (*argvp)[n];
5654c8945a0SNathan Whitehorn 			}
566f4f33ea0SBaptiste Daroussin 		    } else if (added < 2) {
567f4f33ea0SBaptiste Daroussin 			/* 0 or 1 argument read from the included file
568f4f33ea0SBaptiste Daroussin 			   -> shift the arguments after '--file <filepath>'
569f4f33ea0SBaptiste Daroussin 			   left by (2 - added) positions */
570f4f33ea0SBaptiste Daroussin 			for (n = j + added; n + 2 - added < *argcp; ++n) {
571f4f33ea0SBaptiste Daroussin 			    (*argvp)[n] = (*argvp)[n + 2 - added];
572f4f33ea0SBaptiste Daroussin 			}
573f4f33ea0SBaptiste Daroussin 		    }
574f4f33ea0SBaptiste Daroussin 		    /* Copy the inserted arguments to *argvp */
5754c8945a0SNathan Whitehorn 		    for (n = 0; n < added; ++n) {
5764c8945a0SNathan Whitehorn 			(*argvp)[n + j] = list[n];
5774c8945a0SNathan Whitehorn 		    }
5784c8945a0SNathan Whitehorn 		    *argcp += added - 2;
579f4f33ea0SBaptiste Daroussin 		    (*argvp)[*argcp] = 0;	/* Write the NULL terminator */
580f4f33ea0SBaptiste Daroussin 		    free(list);	/* No-op if 'list' is NULL */
581f4f33ea0SBaptiste Daroussin 		    /* Force rescan starting from the first inserted argument */
582f4f33ea0SBaptiste Daroussin 		    --j;
583f4f33ea0SBaptiste Daroussin 		    DLG_TRACE(("# finished --file\n"));
5844c8945a0SNathan Whitehorn 		    continue;
5854c8945a0SNathan Whitehorn 		} else {
586f4f33ea0SBaptiste Daroussin 		    handle_leaks();
587f4f33ea0SBaptiste Daroussin 		    dlg_exiterr("Cannot open --file %s", filename);
588f4f33ea0SBaptiste Daroussin 		}
589f4f33ea0SBaptiste Daroussin 	    } else {
590f4f33ea0SBaptiste Daroussin 		handle_leaks();
5914c8945a0SNathan Whitehorn 		dlg_exiterr("No value given for --file");
5924c8945a0SNathan Whitehorn 	    }
5934c8945a0SNathan Whitehorn 	}
5944c8945a0SNathan Whitehorn 	if (!escaped
5954c8945a0SNathan Whitehorn 	    && (*argvp)[j] != 0
5967a1c0d96SNathan Whitehorn 	    && !strncmp((*argvp)[j], "--", (size_t) 2)
5974c8945a0SNathan Whitehorn 	    && isalpha(UCH((*argvp)[j][2]))) {
598f4f33ea0SBaptiste Daroussin 	    my_argv[my_argc++] = (*argvp)[j];
599f4f33ea0SBaptiste Daroussin 	    DLG_TRACE(("#\toption argv[%d]=%s\n", j, (*argvp)[j]));
6004c8945a0SNathan Whitehorn 	}
6014c8945a0SNathan Whitehorn     }
6024c8945a0SNathan Whitehorn 
603f4f33ea0SBaptiste Daroussin     my_argv[my_argc] = 0;
604f4f33ea0SBaptiste Daroussin 
605f4f33ea0SBaptiste Daroussin     known_opts = my_argc;
606f4f33ea0SBaptiste Daroussin     dialog_opts = my_argv;
607f4f33ea0SBaptiste Daroussin 
608f4f33ea0SBaptiste Daroussin     DLG_TRACE(("#\t%d options vs %d arguments\n", known_opts, *argcp));
6094c8945a0SNathan Whitehorn     dialog_argv = (*argvp);
6104c8945a0SNathan Whitehorn }
6114c8945a0SNathan Whitehorn 
6124c8945a0SNathan Whitehorn static eOptions
6134c8945a0SNathan Whitehorn lookupOption(const char *name, int pass)
6144c8945a0SNathan Whitehorn {
6154c8945a0SNathan Whitehorn     unsigned n;
6162a3e3873SBaptiste Daroussin     eOptions result = o_unknown;
6174c8945a0SNathan Whitehorn 
6184c8945a0SNathan Whitehorn     if (isOption(name)) {
6194c8945a0SNathan Whitehorn 	name += 2;
6204c8945a0SNathan Whitehorn 	for (n = 0; n < sizeof(options) / sizeof(options[0]); n++) {
6214c8945a0SNathan Whitehorn 	    if ((pass & options[n].pass) != 0
6224c8945a0SNathan Whitehorn 		&& !strcmp(name, options[n].name)) {
6232a3e3873SBaptiste Daroussin 		result = options[n].code;
6242a3e3873SBaptiste Daroussin 		break;
6254c8945a0SNathan Whitehorn 	    }
6264c8945a0SNathan Whitehorn 	}
6274c8945a0SNathan Whitehorn     }
6282a3e3873SBaptiste Daroussin     return result;
6294c8945a0SNathan Whitehorn }
6304c8945a0SNathan Whitehorn 
6314c8945a0SNathan Whitehorn static void
6324c8945a0SNathan Whitehorn Usage(const char *msg)
6334c8945a0SNathan Whitehorn {
634f4f33ea0SBaptiste Daroussin     handle_leaks();
6354c8945a0SNathan Whitehorn     dlg_exiterr("Error: %s.\nUse --help to list options.\n\n", msg);
6364c8945a0SNathan Whitehorn }
6374c8945a0SNathan Whitehorn 
6384c8945a0SNathan Whitehorn /*
6394c8945a0SNathan Whitehorn  * Count arguments, stopping at the end of the argument list, or on any of our
6404c8945a0SNathan Whitehorn  * "--" tokens.
6414c8945a0SNathan Whitehorn  */
6424c8945a0SNathan Whitehorn static int
6434c8945a0SNathan Whitehorn arg_rest(char *argv[])
6444c8945a0SNathan Whitehorn {
6454c8945a0SNathan Whitehorn     int i = 1;			/* argv[0] points to a "--" token */
6464c8945a0SNathan Whitehorn 
6474c8945a0SNathan Whitehorn     while (argv[i] != 0
6484c8945a0SNathan Whitehorn 	   && (!isOption(argv[i]) || lookupOption(argv[i], 7) == o_unknown))
6494c8945a0SNathan Whitehorn 	i++;
6504c8945a0SNathan Whitehorn     return i;
6514c8945a0SNathan Whitehorn }
6524c8945a0SNathan Whitehorn 
6534c8945a0SNathan Whitehorn /*
6544c8945a0SNathan Whitehorn  * In MultiWidget this function is needed to count how many tags
6554c8945a0SNathan Whitehorn  * a widget (menu, checklist, radiolist) has
6564c8945a0SNathan Whitehorn  */
6574c8945a0SNathan Whitehorn static int
6584c8945a0SNathan Whitehorn howmany_tags(char *argv[], int group)
6594c8945a0SNathan Whitehorn {
6604c8945a0SNathan Whitehorn     int result = 0;
6614c8945a0SNathan Whitehorn     int have;
6624c8945a0SNathan Whitehorn     char temp[80];
6634c8945a0SNathan Whitehorn 
6644c8945a0SNathan Whitehorn     while (argv[0] != 0) {
6654c8945a0SNathan Whitehorn 	if (isOption(argv[0]))
6664c8945a0SNathan Whitehorn 	    break;
6674c8945a0SNathan Whitehorn 	if ((have = arg_rest(argv)) < group) {
668f4f33ea0SBaptiste Daroussin 	    const char *format = _("Expected %d arguments, found only %d");
6694c8945a0SNathan Whitehorn 	    sprintf(temp, format, group, have);
6704c8945a0SNathan Whitehorn 	    Usage(temp);
671f4f33ea0SBaptiste Daroussin 	} else if ((have % group) != 0) {
672f4f33ea0SBaptiste Daroussin 	    const char *format = _("Expected %d arguments, found extra %d");
673f4f33ea0SBaptiste Daroussin 	    sprintf(temp, format, group, (have % group));
674f4f33ea0SBaptiste Daroussin 	    Usage(temp);
6754c8945a0SNathan Whitehorn 	}
6764c8945a0SNathan Whitehorn 
677f4f33ea0SBaptiste Daroussin 	argv += have;
678f4f33ea0SBaptiste Daroussin 	result += (have / group);
6794c8945a0SNathan Whitehorn     }
6804c8945a0SNathan Whitehorn 
6814c8945a0SNathan Whitehorn     return result;
6824c8945a0SNathan Whitehorn }
6834c8945a0SNathan Whitehorn 
6844c8945a0SNathan Whitehorn static int
6854c8945a0SNathan Whitehorn numeric_arg(char **av, int n)
6864c8945a0SNathan Whitehorn {
6872a3e3873SBaptiste Daroussin     int result = 0;
6882a3e3873SBaptiste Daroussin 
6892a3e3873SBaptiste Daroussin     if (n < dlg_count_argv(av)) {
6904c8945a0SNathan Whitehorn 	char msg[80];
6912a3e3873SBaptiste Daroussin 	char *last = 0;
6922a3e3873SBaptiste Daroussin 	result = (int) strtol(av[n], &last, 10);
6934c8945a0SNathan Whitehorn 
6944c8945a0SNathan Whitehorn 	if (last == 0 || *last != 0) {
6954c8945a0SNathan Whitehorn 	    sprintf(msg, "Expected a number for token %d of %.20s", n, av[0]);
6964c8945a0SNathan Whitehorn 	    Usage(msg);
6974c8945a0SNathan Whitehorn 	}
6982a3e3873SBaptiste Daroussin     }
6994c8945a0SNathan Whitehorn     return result;
7004c8945a0SNathan Whitehorn }
7014c8945a0SNathan Whitehorn 
7024c8945a0SNathan Whitehorn static char *
7034c8945a0SNathan Whitehorn optional_str(char **av, int n, char *dft)
7044c8945a0SNathan Whitehorn {
7054c8945a0SNathan Whitehorn     char *ret = dft;
7064c8945a0SNathan Whitehorn     if (arg_rest(av) > n)
7074c8945a0SNathan Whitehorn 	ret = av[n];
7084c8945a0SNathan Whitehorn     return ret;
7094c8945a0SNathan Whitehorn }
7104c8945a0SNathan Whitehorn 
7114c8945a0SNathan Whitehorn #if defined(HAVE_DLG_GAUGE) || defined(HAVE_XDIALOG)
7124c8945a0SNathan Whitehorn static int
7134c8945a0SNathan Whitehorn optional_num(char **av, int n, int dft)
7144c8945a0SNathan Whitehorn {
7154c8945a0SNathan Whitehorn     int ret = dft;
7164c8945a0SNathan Whitehorn     if (arg_rest(av) > n)
7174c8945a0SNathan Whitehorn 	ret = numeric_arg(av, n);
7184c8945a0SNathan Whitehorn     return ret;
7194c8945a0SNathan Whitehorn }
7204c8945a0SNathan Whitehorn #endif
7214c8945a0SNathan Whitehorn 
7224c8945a0SNathan Whitehorn /*
7234c8945a0SNathan Whitehorn  * On AIX 4.x, we have to flush the output right away since there is a bug in
7244c8945a0SNathan Whitehorn  * the curses package which discards stdout even when we've used newterm to
7254c8945a0SNathan Whitehorn  * redirect output to /dev/tty.
7264c8945a0SNathan Whitehorn  */
7274c8945a0SNathan Whitehorn static int
7284c8945a0SNathan Whitehorn show_result(int ret)
7294c8945a0SNathan Whitehorn {
7304c8945a0SNathan Whitehorn     bool either = FALSE;
7314c8945a0SNathan Whitehorn 
7324c8945a0SNathan Whitehorn     switch (ret) {
7334c8945a0SNathan Whitehorn     case DLG_EXIT_OK:
7344c8945a0SNathan Whitehorn     case DLG_EXIT_EXTRA:
7354c8945a0SNathan Whitehorn     case DLG_EXIT_HELP:
7364c8945a0SNathan Whitehorn     case DLG_EXIT_ITEM_HELP:
7374c8945a0SNathan Whitehorn 	if ((dialog_state.output_count > 1) && !dialog_vars.separate_output) {
7384c8945a0SNathan Whitehorn 	    fputs((dialog_state.separate_str
7394c8945a0SNathan Whitehorn 		   ? dialog_state.separate_str
7404c8945a0SNathan Whitehorn 		   : DEFAULT_SEPARATE_STR),
7414c8945a0SNathan Whitehorn 		  dialog_state.output);
7424c8945a0SNathan Whitehorn 	    either = TRUE;
7434c8945a0SNathan Whitehorn 	}
7447a1c0d96SNathan Whitehorn 	if (dialog_vars.input_result != 0
7457a1c0d96SNathan Whitehorn 	    && dialog_vars.input_result[0] != '\0') {
7464c8945a0SNathan Whitehorn 	    fputs(dialog_vars.input_result, dialog_state.output);
747f4f33ea0SBaptiste Daroussin 	    DLG_TRACE(("# input_result:\n%s\n", dialog_vars.input_result));
7484c8945a0SNathan Whitehorn 	    either = TRUE;
7494c8945a0SNathan Whitehorn 	}
7504c8945a0SNathan Whitehorn 	if (either) {
7514c8945a0SNathan Whitehorn 	    fflush(dialog_state.output);
7524c8945a0SNathan Whitehorn 	}
7534c8945a0SNathan Whitehorn 	break;
7544c8945a0SNathan Whitehorn     }
7554c8945a0SNathan Whitehorn     return ret;
7564c8945a0SNathan Whitehorn }
7574c8945a0SNathan Whitehorn 
7584c8945a0SNathan Whitehorn /*
7594c8945a0SNathan Whitehorn  * These are the widget callers.
7604c8945a0SNathan Whitehorn  */
7614c8945a0SNathan Whitehorn 
7624c8945a0SNathan Whitehorn static int
7634c8945a0SNathan Whitehorn call_yesno(CALLARGS)
7644c8945a0SNathan Whitehorn {
7654c8945a0SNathan Whitehorn     *offset_add = 4;
7664c8945a0SNathan Whitehorn     return dialog_yesno(t,
7674c8945a0SNathan Whitehorn 			av[1],
7684c8945a0SNathan Whitehorn 			numeric_arg(av, 2),
7694c8945a0SNathan Whitehorn 			numeric_arg(av, 3));
7704c8945a0SNathan Whitehorn }
7714c8945a0SNathan Whitehorn 
7724c8945a0SNathan Whitehorn static int
7734c8945a0SNathan Whitehorn call_msgbox(CALLARGS)
7744c8945a0SNathan Whitehorn {
7754c8945a0SNathan Whitehorn     *offset_add = 4;
7764c8945a0SNathan Whitehorn     return dialog_msgbox(t,
7774c8945a0SNathan Whitehorn 			 av[1],
7784c8945a0SNathan Whitehorn 			 numeric_arg(av, 2),
7794c8945a0SNathan Whitehorn 			 numeric_arg(av, 3), 1);
7804c8945a0SNathan Whitehorn }
7814c8945a0SNathan Whitehorn 
7824c8945a0SNathan Whitehorn static int
7834c8945a0SNathan Whitehorn call_infobox(CALLARGS)
7844c8945a0SNathan Whitehorn {
7854c8945a0SNathan Whitehorn     *offset_add = 4;
7864c8945a0SNathan Whitehorn     return dialog_msgbox(t,
7874c8945a0SNathan Whitehorn 			 av[1],
7884c8945a0SNathan Whitehorn 			 numeric_arg(av, 2),
7894c8945a0SNathan Whitehorn 			 numeric_arg(av, 3), 0);
7904c8945a0SNathan Whitehorn }
7914c8945a0SNathan Whitehorn 
7924c8945a0SNathan Whitehorn static int
7934c8945a0SNathan Whitehorn call_textbox(CALLARGS)
7944c8945a0SNathan Whitehorn {
7954c8945a0SNathan Whitehorn     *offset_add = 4;
7964c8945a0SNathan Whitehorn     return dialog_textbox(t,
7974c8945a0SNathan Whitehorn 			  av[1],
7984c8945a0SNathan Whitehorn 			  numeric_arg(av, 2),
7994c8945a0SNathan Whitehorn 			  numeric_arg(av, 3));
8004c8945a0SNathan Whitehorn }
8014c8945a0SNathan Whitehorn 
8024c8945a0SNathan Whitehorn static int
8034c8945a0SNathan Whitehorn call_menu(CALLARGS)
8044c8945a0SNathan Whitehorn {
8054c8945a0SNathan Whitehorn     int tags = howmany_tags(av + 5, MENUBOX_TAGS);
8064c8945a0SNathan Whitehorn     *offset_add = 5 + tags * MENUBOX_TAGS;
8074c8945a0SNathan Whitehorn 
8084c8945a0SNathan Whitehorn     return dialog_menu(t,
8094c8945a0SNathan Whitehorn 		       av[1],
8104c8945a0SNathan Whitehorn 		       numeric_arg(av, 2),
8114c8945a0SNathan Whitehorn 		       numeric_arg(av, 3),
8124c8945a0SNathan Whitehorn 		       numeric_arg(av, 4),
8134c8945a0SNathan Whitehorn 		       tags, av + 5);
8144c8945a0SNathan Whitehorn }
8154c8945a0SNathan Whitehorn 
8164c8945a0SNathan Whitehorn static int
8174c8945a0SNathan Whitehorn call_inputmenu(CALLARGS)
8184c8945a0SNathan Whitehorn {
8194c8945a0SNathan Whitehorn     int tags = howmany_tags(av + 5, MENUBOX_TAGS);
8207a1c0d96SNathan Whitehorn     bool free_extra_label = FALSE;
8217a1c0d96SNathan Whitehorn     int result;
8224c8945a0SNathan Whitehorn 
8234c8945a0SNathan Whitehorn     dialog_vars.input_menu = TRUE;
8244c8945a0SNathan Whitehorn 
8254c8945a0SNathan Whitehorn     if (dialog_vars.max_input == 0)
8264c8945a0SNathan Whitehorn 	dialog_vars.max_input = MAX_LEN / 2;
8274c8945a0SNathan Whitehorn 
8287a1c0d96SNathan Whitehorn     if (dialog_vars.extra_label == 0) {
8297a1c0d96SNathan Whitehorn 	free_extra_label = TRUE;
8307a1c0d96SNathan Whitehorn 	dialog_vars.extra_label = dlg_strclone(_("Rename"));
8317a1c0d96SNathan Whitehorn     }
8324c8945a0SNathan Whitehorn 
8334c8945a0SNathan Whitehorn     dialog_vars.extra_button = TRUE;
8344c8945a0SNathan Whitehorn 
8354c8945a0SNathan Whitehorn     *offset_add = 5 + tags * MENUBOX_TAGS;
8367a1c0d96SNathan Whitehorn     result = dialog_menu(t,
8374c8945a0SNathan Whitehorn 			 av[1],
8384c8945a0SNathan Whitehorn 			 numeric_arg(av, 2),
8394c8945a0SNathan Whitehorn 			 numeric_arg(av, 3),
8404c8945a0SNathan Whitehorn 			 numeric_arg(av, 4),
8414c8945a0SNathan Whitehorn 			 tags, av + 5);
8427a1c0d96SNathan Whitehorn     if (free_extra_label) {
8437a1c0d96SNathan Whitehorn 	free(dialog_vars.extra_label);
8447a1c0d96SNathan Whitehorn 	dialog_vars.extra_label = 0;
8457a1c0d96SNathan Whitehorn     }
8467a1c0d96SNathan Whitehorn     return result;
8474c8945a0SNathan Whitehorn }
8484c8945a0SNathan Whitehorn 
8494c8945a0SNathan Whitehorn static int
8504c8945a0SNathan Whitehorn call_checklist(CALLARGS)
8514c8945a0SNathan Whitehorn {
8524c8945a0SNathan Whitehorn     int tags = howmany_tags(av + 5, CHECKBOX_TAGS);
8534c8945a0SNathan Whitehorn     int code;
8544c8945a0SNathan Whitehorn 
8554c8945a0SNathan Whitehorn     *offset_add = 5 + tags * CHECKBOX_TAGS;
8564c8945a0SNathan Whitehorn     code = dialog_checklist(t,
8574c8945a0SNathan Whitehorn 			    av[1],
8584c8945a0SNathan Whitehorn 			    numeric_arg(av, 2),
8594c8945a0SNathan Whitehorn 			    numeric_arg(av, 3),
8604c8945a0SNathan Whitehorn 			    numeric_arg(av, 4),
8614c8945a0SNathan Whitehorn 			    tags, av + 5, FLAG_CHECK);
8624c8945a0SNathan Whitehorn     return code;
8634c8945a0SNathan Whitehorn }
8644c8945a0SNathan Whitehorn 
8654c8945a0SNathan Whitehorn static int
8664c8945a0SNathan Whitehorn call_radiolist(CALLARGS)
8674c8945a0SNathan Whitehorn {
8684c8945a0SNathan Whitehorn     int tags = howmany_tags(av + 5, CHECKBOX_TAGS);
8694c8945a0SNathan Whitehorn     *offset_add = 5 + tags * CHECKBOX_TAGS;
8704c8945a0SNathan Whitehorn     return dialog_checklist(t,
8714c8945a0SNathan Whitehorn 			    av[1],
8724c8945a0SNathan Whitehorn 			    numeric_arg(av, 2),
8734c8945a0SNathan Whitehorn 			    numeric_arg(av, 3),
8744c8945a0SNathan Whitehorn 			    numeric_arg(av, 4),
8754c8945a0SNathan Whitehorn 			    tags, av + 5, FLAG_RADIO);
8764c8945a0SNathan Whitehorn }
8774c8945a0SNathan Whitehorn 
8784c8945a0SNathan Whitehorn static int
8794c8945a0SNathan Whitehorn call_inputbox(CALLARGS)
8804c8945a0SNathan Whitehorn {
8814c8945a0SNathan Whitehorn     *offset_add = arg_rest(av);
8824c8945a0SNathan Whitehorn     return dialog_inputbox(t,
8834c8945a0SNathan Whitehorn 			   av[1],
8844c8945a0SNathan Whitehorn 			   numeric_arg(av, 2),
8854c8945a0SNathan Whitehorn 			   numeric_arg(av, 3),
8864c8945a0SNathan Whitehorn 			   optional_str(av, 4, 0), 0);
8874c8945a0SNathan Whitehorn }
8884c8945a0SNathan Whitehorn 
8894c8945a0SNathan Whitehorn static int
8904c8945a0SNathan Whitehorn call_passwordbox(CALLARGS)
8914c8945a0SNathan Whitehorn {
8924c8945a0SNathan Whitehorn     *offset_add = arg_rest(av);
8934c8945a0SNathan Whitehorn     return dialog_inputbox(t,
8944c8945a0SNathan Whitehorn 			   av[1],
8954c8945a0SNathan Whitehorn 			   numeric_arg(av, 2),
8964c8945a0SNathan Whitehorn 			   numeric_arg(av, 3),
8974c8945a0SNathan Whitehorn 			   optional_str(av, 4, 0), 1);
8984c8945a0SNathan Whitehorn }
8994c8945a0SNathan Whitehorn 
9004c8945a0SNathan Whitehorn #ifdef HAVE_XDIALOG
9014c8945a0SNathan Whitehorn static int
9024c8945a0SNathan Whitehorn call_calendar(CALLARGS)
9034c8945a0SNathan Whitehorn {
9044c8945a0SNathan Whitehorn     *offset_add = arg_rest(av);
9054c8945a0SNathan Whitehorn     return dialog_calendar(t,
9064c8945a0SNathan Whitehorn 			   av[1],
9074c8945a0SNathan Whitehorn 			   numeric_arg(av, 2),
9084c8945a0SNathan Whitehorn 			   numeric_arg(av, 3),
9094c8945a0SNathan Whitehorn 			   optional_num(av, 4, -1),
9104c8945a0SNathan Whitehorn 			   optional_num(av, 5, -1),
9114c8945a0SNathan Whitehorn 			   optional_num(av, 6, -1));
9124c8945a0SNathan Whitehorn }
9134c8945a0SNathan Whitehorn 
9144c8945a0SNathan Whitehorn static int
9154c8945a0SNathan Whitehorn call_dselect(CALLARGS)
9164c8945a0SNathan Whitehorn {
9174c8945a0SNathan Whitehorn     *offset_add = arg_rest(av);
9184c8945a0SNathan Whitehorn     return dialog_dselect(t,
9194c8945a0SNathan Whitehorn 			  av[1],
9204c8945a0SNathan Whitehorn 			  numeric_arg(av, 2),
9214c8945a0SNathan Whitehorn 			  numeric_arg(av, 3));
9224c8945a0SNathan Whitehorn }
9234c8945a0SNathan Whitehorn 
9244c8945a0SNathan Whitehorn static int
9254c8945a0SNathan Whitehorn call_editbox(CALLARGS)
9264c8945a0SNathan Whitehorn {
9274c8945a0SNathan Whitehorn     *offset_add = 4;
9284c8945a0SNathan Whitehorn     return dialog_editbox(t,
9294c8945a0SNathan Whitehorn 			  av[1],
9304c8945a0SNathan Whitehorn 			  numeric_arg(av, 2),
9314c8945a0SNathan Whitehorn 			  numeric_arg(av, 3));
9324c8945a0SNathan Whitehorn }
9334c8945a0SNathan Whitehorn 
9344c8945a0SNathan Whitehorn static int
9354c8945a0SNathan Whitehorn call_fselect(CALLARGS)
9364c8945a0SNathan Whitehorn {
9374c8945a0SNathan Whitehorn     *offset_add = arg_rest(av);
9384c8945a0SNathan Whitehorn     return dialog_fselect(t,
9394c8945a0SNathan Whitehorn 			  av[1],
9404c8945a0SNathan Whitehorn 			  numeric_arg(av, 2),
9414c8945a0SNathan Whitehorn 			  numeric_arg(av, 3));
9424c8945a0SNathan Whitehorn }
9434c8945a0SNathan Whitehorn 
9444c8945a0SNathan Whitehorn static int
9454c8945a0SNathan Whitehorn call_timebox(CALLARGS)
9464c8945a0SNathan Whitehorn {
9474c8945a0SNathan Whitehorn     *offset_add = arg_rest(av);
9484c8945a0SNathan Whitehorn     return dialog_timebox(t,
9494c8945a0SNathan Whitehorn 			  av[1],
9504c8945a0SNathan Whitehorn 			  numeric_arg(av, 2),
9514c8945a0SNathan Whitehorn 			  numeric_arg(av, 3),
9524c8945a0SNathan Whitehorn 			  optional_num(av, 4, -1),
9534c8945a0SNathan Whitehorn 			  optional_num(av, 5, -1),
9544c8945a0SNathan Whitehorn 			  optional_num(av, 6, -1));
9554c8945a0SNathan Whitehorn }
9564c8945a0SNathan Whitehorn #endif /* HAVE_XDIALOG */
9574c8945a0SNathan Whitehorn 
9582a3e3873SBaptiste Daroussin /* dialog 1.2 widgets */
9592a3e3873SBaptiste Daroussin #ifdef HAVE_XDIALOG2
9602a3e3873SBaptiste Daroussin 
9612a3e3873SBaptiste Daroussin #define DisableNoTags() \
9622a3e3873SBaptiste Daroussin 	bool save_no_tags = dialog_vars.no_tags; \
9632a3e3873SBaptiste Daroussin 	bool save_no_items = dialog_vars.no_items; \
9642a3e3873SBaptiste Daroussin 	dialog_vars.no_tags = TRUE; \
9652a3e3873SBaptiste Daroussin 	dialog_vars.no_items = FALSE
9662a3e3873SBaptiste Daroussin 
9672a3e3873SBaptiste Daroussin #define RestoreNoTags() \
9682a3e3873SBaptiste Daroussin 	dialog_vars.no_tags = save_no_tags; \
9692a3e3873SBaptiste Daroussin 	dialog_vars.no_items = save_no_items
9702a3e3873SBaptiste Daroussin 
9712a3e3873SBaptiste Daroussin static int
9722a3e3873SBaptiste Daroussin call_buildlist(CALLARGS)
9732a3e3873SBaptiste Daroussin {
9742a3e3873SBaptiste Daroussin     int tags = howmany_tags(av + 5, CHECKBOX_TAGS);
9752a3e3873SBaptiste Daroussin     int result;
9762a3e3873SBaptiste Daroussin 
9772a3e3873SBaptiste Daroussin     DisableNoTags();
9782a3e3873SBaptiste Daroussin 
9792a3e3873SBaptiste Daroussin     *offset_add = 5 + tags * CHECKBOX_TAGS;
9802a3e3873SBaptiste Daroussin     result = dialog_buildlist(t,
9812a3e3873SBaptiste Daroussin 			      av[1],
9822a3e3873SBaptiste Daroussin 			      numeric_arg(av, 2),
9832a3e3873SBaptiste Daroussin 			      numeric_arg(av, 3),
9842a3e3873SBaptiste Daroussin 			      numeric_arg(av, 4),
9852a3e3873SBaptiste Daroussin 			      tags, av + 5,
986f4f33ea0SBaptiste Daroussin 			      dialog_vars.reorder);
9872a3e3873SBaptiste Daroussin     RestoreNoTags();
9882a3e3873SBaptiste Daroussin     return result;
9892a3e3873SBaptiste Daroussin }
9902a3e3873SBaptiste Daroussin 
9912a3e3873SBaptiste Daroussin static int
9922a3e3873SBaptiste Daroussin call_rangebox(CALLARGS)
9932a3e3873SBaptiste Daroussin {
9942a3e3873SBaptiste Daroussin     int min_value;
9952a3e3873SBaptiste Daroussin 
9962a3e3873SBaptiste Daroussin     *offset_add = arg_rest(av);
9972a3e3873SBaptiste Daroussin     min_value = numeric_arg(av, 4);
9982a3e3873SBaptiste Daroussin     return dialog_rangebox(t,
9992a3e3873SBaptiste Daroussin 			   av[1],
10002a3e3873SBaptiste Daroussin 			   numeric_arg(av, 2),
10012a3e3873SBaptiste Daroussin 			   numeric_arg(av, 3),
10022a3e3873SBaptiste Daroussin 			   min_value,
10032a3e3873SBaptiste Daroussin 			   numeric_arg(av, 5),
10042a3e3873SBaptiste Daroussin 			   (*offset_add > 6) ? numeric_arg(av, 6) : min_value);
10052a3e3873SBaptiste Daroussin }
10062a3e3873SBaptiste Daroussin 
10072a3e3873SBaptiste Daroussin static int
10082a3e3873SBaptiste Daroussin call_treeview(CALLARGS)
10092a3e3873SBaptiste Daroussin {
10102a3e3873SBaptiste Daroussin     int tags = howmany_tags(av + 5, TREEVIEW_TAGS);
10112a3e3873SBaptiste Daroussin     int result;
10122a3e3873SBaptiste Daroussin 
10132a3e3873SBaptiste Daroussin     DisableNoTags();
10142a3e3873SBaptiste Daroussin 
10152a3e3873SBaptiste Daroussin     *offset_add = arg_rest(av);
10162a3e3873SBaptiste Daroussin     result = dialog_treeview(t,
10172a3e3873SBaptiste Daroussin 			     av[1],
10182a3e3873SBaptiste Daroussin 			     numeric_arg(av, 2),
10192a3e3873SBaptiste Daroussin 			     numeric_arg(av, 3),
10202a3e3873SBaptiste Daroussin 			     numeric_arg(av, 4),
10212a3e3873SBaptiste Daroussin 			     tags, av + 5, FLAG_RADIO);
10222a3e3873SBaptiste Daroussin     RestoreNoTags();
10232a3e3873SBaptiste Daroussin     return result;
10242a3e3873SBaptiste Daroussin }
10252a3e3873SBaptiste Daroussin #endif /* HAVE_XDIALOG */
10262a3e3873SBaptiste Daroussin 
10274c8945a0SNathan Whitehorn #ifdef HAVE_DLG_FORMBOX
10284c8945a0SNathan Whitehorn static int
10294c8945a0SNathan Whitehorn call_form(CALLARGS)
10304c8945a0SNathan Whitehorn {
10314c8945a0SNathan Whitehorn     int group = FORMBOX_TAGS;
10324c8945a0SNathan Whitehorn     int tags = howmany_tags(av + 5, group);
10334c8945a0SNathan Whitehorn     *offset_add = 5 + tags * group;
10344c8945a0SNathan Whitehorn 
10354c8945a0SNathan Whitehorn     return dialog_form(t,
10364c8945a0SNathan Whitehorn 		       av[1],
10374c8945a0SNathan Whitehorn 		       numeric_arg(av, 2),
10384c8945a0SNathan Whitehorn 		       numeric_arg(av, 3),
10394c8945a0SNathan Whitehorn 		       numeric_arg(av, 4),
10404c8945a0SNathan Whitehorn 		       tags, av + 5);
10414c8945a0SNathan Whitehorn }
10424c8945a0SNathan Whitehorn 
10434c8945a0SNathan Whitehorn static int
10444c8945a0SNathan Whitehorn call_password_form(CALLARGS)
10454c8945a0SNathan Whitehorn {
10464c8945a0SNathan Whitehorn     unsigned save = dialog_vars.formitem_type;
10474c8945a0SNathan Whitehorn     int result;
10484c8945a0SNathan Whitehorn 
10494c8945a0SNathan Whitehorn     dialog_vars.formitem_type = 1;
10504c8945a0SNathan Whitehorn     result = call_form(PASSARGS);
10514c8945a0SNathan Whitehorn     dialog_vars.formitem_type = save;
10524c8945a0SNathan Whitehorn 
10534c8945a0SNathan Whitehorn     return result;
10544c8945a0SNathan Whitehorn }
10554c8945a0SNathan Whitehorn #endif /* HAVE_DLG_FORMBOX */
10564c8945a0SNathan Whitehorn 
10574c8945a0SNathan Whitehorn #ifdef HAVE_DLG_MIXEDFORM
10584c8945a0SNathan Whitehorn static int
10594c8945a0SNathan Whitehorn call_mixed_form(CALLARGS)
10604c8945a0SNathan Whitehorn {
10614c8945a0SNathan Whitehorn     int group = MIXEDFORM_TAGS;
10624c8945a0SNathan Whitehorn     int tags = howmany_tags(av + 5, group);
10634c8945a0SNathan Whitehorn     *offset_add = 5 + tags * group;
10644c8945a0SNathan Whitehorn 
10654c8945a0SNathan Whitehorn     return dialog_mixedform(t,
10664c8945a0SNathan Whitehorn 			    av[1],
10674c8945a0SNathan Whitehorn 			    numeric_arg(av, 2),
10684c8945a0SNathan Whitehorn 			    numeric_arg(av, 3),
10694c8945a0SNathan Whitehorn 			    numeric_arg(av, 4),
10704c8945a0SNathan Whitehorn 			    tags, av + 5);
10714c8945a0SNathan Whitehorn }
10724c8945a0SNathan Whitehorn #endif /* HAVE_DLG_MIXEDFORM */
10734c8945a0SNathan Whitehorn 
10744c8945a0SNathan Whitehorn #ifdef HAVE_DLG_GAUGE
10754c8945a0SNathan Whitehorn static int
10764c8945a0SNathan Whitehorn call_gauge(CALLARGS)
10774c8945a0SNathan Whitehorn {
10784c8945a0SNathan Whitehorn     *offset_add = arg_rest(av);
10794c8945a0SNathan Whitehorn     return dialog_gauge(t,
10804c8945a0SNathan Whitehorn 			av[1],
10814c8945a0SNathan Whitehorn 			numeric_arg(av, 2),
10824c8945a0SNathan Whitehorn 			numeric_arg(av, 3),
10834c8945a0SNathan Whitehorn 			optional_num(av, 4, 0));
10844c8945a0SNathan Whitehorn }
10854c8945a0SNathan Whitehorn 
10864c8945a0SNathan Whitehorn static int
10874c8945a0SNathan Whitehorn call_pause(CALLARGS)
10884c8945a0SNathan Whitehorn {
10894c8945a0SNathan Whitehorn     *offset_add = arg_rest(av);
10904c8945a0SNathan Whitehorn     return dialog_pause(t,
10914c8945a0SNathan Whitehorn 			av[1],
10924c8945a0SNathan Whitehorn 			numeric_arg(av, 2),
10934c8945a0SNathan Whitehorn 			numeric_arg(av, 3),
10944c8945a0SNathan Whitehorn 			numeric_arg(av, 4));
10954c8945a0SNathan Whitehorn }
10964c8945a0SNathan Whitehorn #endif
10974c8945a0SNathan Whitehorn 
10984c8945a0SNathan Whitehorn #ifdef HAVE_MIXEDGAUGE
10994c8945a0SNathan Whitehorn static int
11004c8945a0SNathan Whitehorn call_mixed_gauge(CALLARGS)
11014c8945a0SNathan Whitehorn {
11024c8945a0SNathan Whitehorn #define MIXEDGAUGE_BASE 5
11034c8945a0SNathan Whitehorn     int tags = howmany_tags(av + MIXEDGAUGE_BASE, MIXEDGAUGE_TAGS);
11044c8945a0SNathan Whitehorn     *offset_add = MIXEDGAUGE_BASE + tags * MIXEDGAUGE_TAGS;
11054c8945a0SNathan Whitehorn     return dialog_mixedgauge(t,
11064c8945a0SNathan Whitehorn 			     av[1],
11074c8945a0SNathan Whitehorn 			     numeric_arg(av, 2),
11084c8945a0SNathan Whitehorn 			     numeric_arg(av, 3),
11094c8945a0SNathan Whitehorn 			     numeric_arg(av, 4),
11104c8945a0SNathan Whitehorn 			     tags, av + MIXEDGAUGE_BASE);
11114c8945a0SNathan Whitehorn }
11124c8945a0SNathan Whitehorn #endif
11134c8945a0SNathan Whitehorn 
1114682c9e0fSNathan Whitehorn #ifdef HAVE_DLG_GAUGE
11157a1c0d96SNathan Whitehorn static int
11167a1c0d96SNathan Whitehorn call_prgbox(CALLARGS)
11177a1c0d96SNathan Whitehorn {
11187a1c0d96SNathan Whitehorn     *offset_add = arg_rest(av);
11197a1c0d96SNathan Whitehorn     /* the original version does not accept a prompt string, but for
11207a1c0d96SNathan Whitehorn      * consistency we allow it.
11217a1c0d96SNathan Whitehorn      */
11227a1c0d96SNathan Whitehorn     return ((*offset_add == 5)
11237a1c0d96SNathan Whitehorn 	    ? dialog_prgbox(t,
11247a1c0d96SNathan Whitehorn 			    av[1],
11257a1c0d96SNathan Whitehorn 			    av[2],
11267a1c0d96SNathan Whitehorn 			    numeric_arg(av, 3),
11277a1c0d96SNathan Whitehorn 			    numeric_arg(av, 4), TRUE)
11287a1c0d96SNathan Whitehorn 	    : dialog_prgbox(t,
11297a1c0d96SNathan Whitehorn 			    "",
11307a1c0d96SNathan Whitehorn 			    av[1],
11317a1c0d96SNathan Whitehorn 			    numeric_arg(av, 2),
11327a1c0d96SNathan Whitehorn 			    numeric_arg(av, 3), TRUE));
11337a1c0d96SNathan Whitehorn }
1134682c9e0fSNathan Whitehorn #endif
11357a1c0d96SNathan Whitehorn 
11367a1c0d96SNathan Whitehorn #ifdef HAVE_DLG_GAUGE
11377a1c0d96SNathan Whitehorn static int
11387a1c0d96SNathan Whitehorn call_programbox(CALLARGS)
11397a1c0d96SNathan Whitehorn {
11407a1c0d96SNathan Whitehorn     int result;
11417a1c0d96SNathan Whitehorn 
11427a1c0d96SNathan Whitehorn     *offset_add = arg_rest(av);
11437a1c0d96SNathan Whitehorn     /* this function is a compromise between --prgbox and --progressbox.
11447a1c0d96SNathan Whitehorn      */
11457a1c0d96SNathan Whitehorn     result = ((*offset_add == 4)
11467a1c0d96SNathan Whitehorn 	      ? dlg_progressbox(t,
11477a1c0d96SNathan Whitehorn 				av[1],
11487a1c0d96SNathan Whitehorn 				numeric_arg(av, 2),
11497a1c0d96SNathan Whitehorn 				numeric_arg(av, 3),
11507a1c0d96SNathan Whitehorn 				TRUE,
11517a1c0d96SNathan Whitehorn 				dialog_state.pipe_input)
11527a1c0d96SNathan Whitehorn 	      : dlg_progressbox(t,
11537a1c0d96SNathan Whitehorn 				"",
11547a1c0d96SNathan Whitehorn 				numeric_arg(av, 1),
11557a1c0d96SNathan Whitehorn 				numeric_arg(av, 2),
11567a1c0d96SNathan Whitehorn 				TRUE,
11577a1c0d96SNathan Whitehorn 				dialog_state.pipe_input));
11587a1c0d96SNathan Whitehorn     dialog_state.pipe_input = 0;
11597a1c0d96SNathan Whitehorn     return result;
11607a1c0d96SNathan Whitehorn }
11617a1c0d96SNathan Whitehorn #endif
11627a1c0d96SNathan Whitehorn 
11634c8945a0SNathan Whitehorn #ifdef HAVE_DLG_GAUGE
11644c8945a0SNathan Whitehorn static int
11654c8945a0SNathan Whitehorn call_progressbox(CALLARGS)
11664c8945a0SNathan Whitehorn {
11674c8945a0SNathan Whitehorn     *offset_add = arg_rest(av);
11684c8945a0SNathan Whitehorn     /* the original version does not accept a prompt string, but for
11694c8945a0SNathan Whitehorn      * consistency we allow it.
11704c8945a0SNathan Whitehorn      */
11714c8945a0SNathan Whitehorn     return ((*offset_add == 4)
11724c8945a0SNathan Whitehorn 	    ? dialog_progressbox(t,
11734c8945a0SNathan Whitehorn 				 av[1],
11744c8945a0SNathan Whitehorn 				 numeric_arg(av, 2),
11754c8945a0SNathan Whitehorn 				 numeric_arg(av, 3))
11764c8945a0SNathan Whitehorn 	    : dialog_progressbox(t,
11774c8945a0SNathan Whitehorn 				 "",
11784c8945a0SNathan Whitehorn 				 numeric_arg(av, 1),
11794c8945a0SNathan Whitehorn 				 numeric_arg(av, 2)));
11804c8945a0SNathan Whitehorn }
11814c8945a0SNathan Whitehorn #endif
11824c8945a0SNathan Whitehorn 
11834c8945a0SNathan Whitehorn #ifdef HAVE_DLG_TAILBOX
11844c8945a0SNathan Whitehorn static int
11854c8945a0SNathan Whitehorn call_tailbox(CALLARGS)
11864c8945a0SNathan Whitehorn {
11874c8945a0SNathan Whitehorn     *offset_add = 4;
11884c8945a0SNathan Whitehorn     return dialog_tailbox(t,
11894c8945a0SNathan Whitehorn 			  av[1],
11904c8945a0SNathan Whitehorn 			  numeric_arg(av, 2),
11914c8945a0SNathan Whitehorn 			  numeric_arg(av, 3),
11924c8945a0SNathan Whitehorn 			  FALSE);
11934c8945a0SNathan Whitehorn }
11944c8945a0SNathan Whitehorn 
11954c8945a0SNathan Whitehorn static int
11964c8945a0SNathan Whitehorn call_tailboxbg(CALLARGS)
11974c8945a0SNathan Whitehorn {
11984c8945a0SNathan Whitehorn     *offset_add = 4;
11994c8945a0SNathan Whitehorn     return dialog_tailbox(t,
12004c8945a0SNathan Whitehorn 			  av[1],
12014c8945a0SNathan Whitehorn 			  numeric_arg(av, 2),
12024c8945a0SNathan Whitehorn 			  numeric_arg(av, 3),
12034c8945a0SNathan Whitehorn 			  TRUE);
12044c8945a0SNathan Whitehorn }
12054c8945a0SNathan Whitehorn #endif
12064c8945a0SNathan Whitehorn /* *INDENT-OFF* */
12074c8945a0SNathan Whitehorn static const Mode modes[] =
12084c8945a0SNathan Whitehorn {
12094c8945a0SNathan Whitehorn     {o_yesno,           4, 4, call_yesno},
12104c8945a0SNathan Whitehorn     {o_msgbox,          4, 4, call_msgbox},
12114c8945a0SNathan Whitehorn     {o_infobox,         4, 4, call_infobox},
12124c8945a0SNathan Whitehorn     {o_textbox,         4, 4, call_textbox},
1213f4f33ea0SBaptiste Daroussin     {o_menu,            6, 0, call_menu},
1214f4f33ea0SBaptiste Daroussin     {o_inputmenu,       6, 0, call_inputmenu},
1215f4f33ea0SBaptiste Daroussin     {o_checklist,       7, 0, call_checklist},
1216f4f33ea0SBaptiste Daroussin     {o_radiolist,       7, 0, call_radiolist},
12174c8945a0SNathan Whitehorn     {o_inputbox,        4, 5, call_inputbox},
12184c8945a0SNathan Whitehorn     {o_passwordbox,     4, 5, call_passwordbox},
12194c8945a0SNathan Whitehorn #ifdef HAVE_DLG_GAUGE
12204c8945a0SNathan Whitehorn     {o_gauge,           4, 5, call_gauge},
12214c8945a0SNathan Whitehorn     {o_pause,           5, 5, call_pause},
12227a1c0d96SNathan Whitehorn     {o_prgbox,          4, 5, call_prgbox},
12237a1c0d96SNathan Whitehorn     {o_programbox,      3, 4, call_programbox},
12244c8945a0SNathan Whitehorn     {o_progressbox,     3, 4, call_progressbox},
12254c8945a0SNathan Whitehorn #endif
12264c8945a0SNathan Whitehorn #ifdef HAVE_DLG_FORMBOX
12274c8945a0SNathan Whitehorn     {o_passwordform,   13, 0, call_password_form},
12284c8945a0SNathan Whitehorn     {o_form,           13, 0, call_form},
12294c8945a0SNathan Whitehorn #endif
12304c8945a0SNathan Whitehorn #ifdef HAVE_MIXEDGAUGE
12314c8945a0SNathan Whitehorn     {o_mixedgauge,      MIXEDGAUGE_BASE, 0, call_mixed_gauge},
12324c8945a0SNathan Whitehorn #endif
12334c8945a0SNathan Whitehorn #ifdef HAVE_DLG_MIXEDFORM
12344c8945a0SNathan Whitehorn     {o_mixedform,      13, 0, call_mixed_form},
12354c8945a0SNathan Whitehorn #endif
12364c8945a0SNathan Whitehorn #ifdef HAVE_DLG_TAILBOX
12374c8945a0SNathan Whitehorn     {o_tailbox,         4, 4, call_tailbox},
12384c8945a0SNathan Whitehorn     {o_tailboxbg,       4, 4, call_tailboxbg},
12394c8945a0SNathan Whitehorn #endif
12404c8945a0SNathan Whitehorn #ifdef HAVE_XDIALOG
12414c8945a0SNathan Whitehorn     {o_calendar,        4, 7, call_calendar},
12424c8945a0SNathan Whitehorn     {o_dselect,         4, 5, call_dselect},
12434c8945a0SNathan Whitehorn     {o_editbox,         4, 4, call_editbox},
12444c8945a0SNathan Whitehorn     {o_fselect,         4, 5, call_fselect},
12454c8945a0SNathan Whitehorn     {o_timebox,         4, 7, call_timebox},
1246f4f33ea0SBaptiste Daroussin #endif
1247f4f33ea0SBaptiste Daroussin #ifdef HAVE_XDIALOG2
1248f4f33ea0SBaptiste Daroussin     {o_buildlist,       4, 0, call_buildlist},
1249f4f33ea0SBaptiste Daroussin     {o_rangebox,        5, 7, call_rangebox},
12502a3e3873SBaptiste Daroussin     {o_treeview,        4, 0, call_treeview},
12514c8945a0SNathan Whitehorn #endif
12524c8945a0SNathan Whitehorn };
12534c8945a0SNathan Whitehorn /* *INDENT-ON* */
12544c8945a0SNathan Whitehorn 
12554c8945a0SNathan Whitehorn static char *
12564c8945a0SNathan Whitehorn optionString(char **argv, int *num)
12574c8945a0SNathan Whitehorn {
12584c8945a0SNathan Whitehorn     int next = *num + 1;
12594c8945a0SNathan Whitehorn     char *result = argv[next];
12604c8945a0SNathan Whitehorn     if (result == 0) {
12614c8945a0SNathan Whitehorn 	char temp[80];
12624c8945a0SNathan Whitehorn 	sprintf(temp, "Expected a string-parameter for %.20s", argv[*num]);
12634c8945a0SNathan Whitehorn 	Usage(temp);
12644c8945a0SNathan Whitehorn     }
12654c8945a0SNathan Whitehorn     *num = next;
12664c8945a0SNathan Whitehorn     return result;
12674c8945a0SNathan Whitehorn }
12684c8945a0SNathan Whitehorn 
12694c8945a0SNathan Whitehorn static int
12704c8945a0SNathan Whitehorn optionValue(char **argv, int *num)
12714c8945a0SNathan Whitehorn {
12724c8945a0SNathan Whitehorn     int next = *num + 1;
12734c8945a0SNathan Whitehorn     char *src = argv[next];
12744c8945a0SNathan Whitehorn     char *tmp = 0;
12754c8945a0SNathan Whitehorn     int result = 0;
12764c8945a0SNathan Whitehorn 
12774c8945a0SNathan Whitehorn     if (src != 0) {
12787a1c0d96SNathan Whitehorn 	result = (int) strtol(src, &tmp, 0);
12794c8945a0SNathan Whitehorn 	if (tmp == 0 || *tmp != 0)
12804c8945a0SNathan Whitehorn 	    src = 0;
12814c8945a0SNathan Whitehorn     }
12824c8945a0SNathan Whitehorn 
12834c8945a0SNathan Whitehorn     if (src == 0) {
12844c8945a0SNathan Whitehorn 	char temp[80];
12854c8945a0SNathan Whitehorn 	sprintf(temp, "Expected a numeric-parameter for %.20s", argv[*num]);
12864c8945a0SNathan Whitehorn 	Usage(temp);
12874c8945a0SNathan Whitehorn     }
12884c8945a0SNathan Whitehorn     *num = next;
12894c8945a0SNathan Whitehorn     return result;
12904c8945a0SNathan Whitehorn }
12914c8945a0SNathan Whitehorn 
12922a3e3873SBaptiste Daroussin /* Return exit-code for a named button */
12932a3e3873SBaptiste Daroussin static int
12942a3e3873SBaptiste Daroussin button_code(const char *name)
12952a3e3873SBaptiste Daroussin {
12962a3e3873SBaptiste Daroussin     /* *INDENT-OFF* */
12972a3e3873SBaptiste Daroussin     static struct {
12982a3e3873SBaptiste Daroussin 	const char *name;
12992a3e3873SBaptiste Daroussin 	int code;
13002a3e3873SBaptiste Daroussin     } table[] = {
13012a3e3873SBaptiste Daroussin 	{ "ok",	    DLG_EXIT_OK },
13022a3e3873SBaptiste Daroussin 	{ "yes",    DLG_EXIT_OK },
13032a3e3873SBaptiste Daroussin 	{ "cancel", DLG_EXIT_CANCEL },
13042a3e3873SBaptiste Daroussin 	{ "no",	    DLG_EXIT_CANCEL },
13052a3e3873SBaptiste Daroussin 	{ "help",   DLG_EXIT_HELP },
13062a3e3873SBaptiste Daroussin 	{ "extra",  DLG_EXIT_EXTRA },
13072a3e3873SBaptiste Daroussin     };
13082a3e3873SBaptiste Daroussin     /* *INDENT-ON* */
13092a3e3873SBaptiste Daroussin 
13102a3e3873SBaptiste Daroussin     int code = DLG_EXIT_ERROR;
13112a3e3873SBaptiste Daroussin     size_t i;
13122a3e3873SBaptiste Daroussin 
13132a3e3873SBaptiste Daroussin     for (i = 0; i < (sizeof(table) / sizeof(table[0])); i++) {
13142a3e3873SBaptiste Daroussin 	if (!dlg_strcmp(name, table[i].name)) {
13152a3e3873SBaptiste Daroussin 	    code = table[i].code;
13162a3e3873SBaptiste Daroussin 	    break;
13172a3e3873SBaptiste Daroussin 	}
13182a3e3873SBaptiste Daroussin     }
13192a3e3873SBaptiste Daroussin 
13202a3e3873SBaptiste Daroussin     if (code == DLG_EXIT_ERROR) {
13212a3e3873SBaptiste Daroussin 	char temp[80];
13222a3e3873SBaptiste Daroussin 	sprintf(temp, "Button name \"%.20s\" unknown", name);
13232a3e3873SBaptiste Daroussin 	Usage(temp);
13242a3e3873SBaptiste Daroussin     }
13252a3e3873SBaptiste Daroussin 
13262a3e3873SBaptiste Daroussin     return code;
13272a3e3873SBaptiste Daroussin }
13282a3e3873SBaptiste Daroussin 
13294c8945a0SNathan Whitehorn /*
1330f4f33ea0SBaptiste Daroussin  * If this is the last option, we do not want any error messages - just our
1331f4f33ea0SBaptiste Daroussin  * output.  Calling end_dialog() cancels the refresh() at the end of the
1332f4f33ea0SBaptiste Daroussin  * program as well.
1333f4f33ea0SBaptiste Daroussin  */
1334f4f33ea0SBaptiste Daroussin static void
1335f4f33ea0SBaptiste Daroussin IgnoreNonScreen(char **argv, int offset)
1336f4f33ea0SBaptiste Daroussin {
1337f4f33ea0SBaptiste Daroussin     if (argv[offset + 1] == 0) {
1338f4f33ea0SBaptiste Daroussin 	ignore_unknown = TRUE;
1339f4f33ea0SBaptiste Daroussin 	end_dialog();
1340f4f33ea0SBaptiste Daroussin     }
1341f4f33ea0SBaptiste Daroussin }
1342f4f33ea0SBaptiste Daroussin 
1343f4f33ea0SBaptiste Daroussin static void
1344f4f33ea0SBaptiste Daroussin PrintTextOnly(char **argv, int *offset, eOptions code)
1345f4f33ea0SBaptiste Daroussin {
1346f4f33ea0SBaptiste Daroussin     /* TODO - handle two optional numeric params */
1347f4f33ea0SBaptiste Daroussin     char *text;
1348f4f33ea0SBaptiste Daroussin     int height = 0;
1349f4f33ea0SBaptiste Daroussin     int width = 0;
1350f4f33ea0SBaptiste Daroussin     int height2 = 0;
1351f4f33ea0SBaptiste Daroussin     int width2 = 0;
1352f4f33ea0SBaptiste Daroussin     int next = arg_rest(argv + *offset);
1353f4f33ea0SBaptiste Daroussin 
1354f4f33ea0SBaptiste Daroussin     if (LINES <= 0 && COLS <= 0)
1355f4f33ea0SBaptiste Daroussin 	dlg_ttysize(fileno(dialog_state.input), &LINES, &COLS);
1356f4f33ea0SBaptiste Daroussin 
1357f4f33ea0SBaptiste Daroussin     text = strdup(optionString(argv, offset));
1358f4f33ea0SBaptiste Daroussin     IgnoreNonScreen(argv, *offset);
1359f4f33ea0SBaptiste Daroussin 
1360f4f33ea0SBaptiste Daroussin     if (next >= 1) {
1361f4f33ea0SBaptiste Daroussin 	next = MIN(next, 3);
1362f4f33ea0SBaptiste Daroussin 	height = numeric_arg(argv, *offset + 1);
1363f4f33ea0SBaptiste Daroussin 	if (next >= 2)
1364f4f33ea0SBaptiste Daroussin 	    width = numeric_arg(argv, *offset + 2);
1365f4f33ea0SBaptiste Daroussin 	*offset += next - 1;
1366f4f33ea0SBaptiste Daroussin     }
1367f4f33ea0SBaptiste Daroussin 
1368f4f33ea0SBaptiste Daroussin     dlg_trim_string(text);
1369f4f33ea0SBaptiste Daroussin     dlg_auto_size(NULL, text, &height2, &width2, height, width);
1370f4f33ea0SBaptiste Daroussin 
1371f4f33ea0SBaptiste Daroussin     switch (code) {
1372f4f33ea0SBaptiste Daroussin     case o_print_text_only:
1373f4f33ea0SBaptiste Daroussin 	dialog_state.text_only = TRUE;
1374f4f33ea0SBaptiste Daroussin 	dlg_print_autowrap(stdscr, text, height2, width2);
1375f4f33ea0SBaptiste Daroussin 	dialog_state.text_only = FALSE;
1376f4f33ea0SBaptiste Daroussin 	break;
1377f4f33ea0SBaptiste Daroussin     case o_print_text_size:
1378f4f33ea0SBaptiste Daroussin 	fprintf(dialog_state.output, "%d %d\n",
1379f4f33ea0SBaptiste Daroussin 		dialog_state.text_height,
1380f4f33ea0SBaptiste Daroussin 		dialog_state.text_width);
1381f4f33ea0SBaptiste Daroussin 	break;
1382f4f33ea0SBaptiste Daroussin     default:
1383f4f33ea0SBaptiste Daroussin 	break;
1384f4f33ea0SBaptiste Daroussin     }
1385f4f33ea0SBaptiste Daroussin }
1386f4f33ea0SBaptiste Daroussin 
1387f4f33ea0SBaptiste Daroussin /*
13884c8945a0SNathan Whitehorn  * Print parts of a message
13894c8945a0SNathan Whitehorn  */
13904c8945a0SNathan Whitehorn static void
13914c8945a0SNathan Whitehorn PrintList(const char *const *list)
13924c8945a0SNathan Whitehorn {
13934c8945a0SNathan Whitehorn     const char *leaf = strrchr(program, '/');
13944c8945a0SNathan Whitehorn     unsigned n = 0;
13954c8945a0SNathan Whitehorn 
13964c8945a0SNathan Whitehorn     if (leaf != 0)
13974c8945a0SNathan Whitehorn 	leaf++;
13984c8945a0SNathan Whitehorn     else
13994c8945a0SNathan Whitehorn 	leaf = program;
14004c8945a0SNathan Whitehorn 
14014c8945a0SNathan Whitehorn     while (*list != 0) {
14024c8945a0SNathan Whitehorn 	fprintf(dialog_state.output, *list, n ? leaf : dialog_version());
14034c8945a0SNathan Whitehorn 	(void) fputc('\n', dialog_state.output);
14044c8945a0SNathan Whitehorn 	n = 1;
14054c8945a0SNathan Whitehorn 	list++;
14064c8945a0SNathan Whitehorn     }
14074c8945a0SNathan Whitehorn }
14084c8945a0SNathan Whitehorn 
14094c8945a0SNathan Whitehorn static const Mode *
14104c8945a0SNathan Whitehorn lookupMode(eOptions code)
14114c8945a0SNathan Whitehorn {
14124c8945a0SNathan Whitehorn     const Mode *modePtr = 0;
14134c8945a0SNathan Whitehorn     unsigned n;
14144c8945a0SNathan Whitehorn 
14154c8945a0SNathan Whitehorn     for (n = 0; n < sizeof(modes) / sizeof(modes[0]); n++) {
14164c8945a0SNathan Whitehorn 	if (modes[n].code == code) {
14174c8945a0SNathan Whitehorn 	    modePtr = &modes[n];
14184c8945a0SNathan Whitehorn 	    break;
14194c8945a0SNathan Whitehorn 	}
14204c8945a0SNathan Whitehorn     }
14214c8945a0SNathan Whitehorn     return modePtr;
14224c8945a0SNathan Whitehorn }
14234c8945a0SNathan Whitehorn 
14244c8945a0SNathan Whitehorn static int
14254c8945a0SNathan Whitehorn compare_opts(const void *a, const void *b)
14264c8945a0SNathan Whitehorn {
14274c8945a0SNathan Whitehorn     Options *const *p = (Options * const *) a;
14284c8945a0SNathan Whitehorn     Options *const *q = (Options * const *) b;
14294c8945a0SNathan Whitehorn     return strcmp((*p)->name, (*q)->name);
14304c8945a0SNathan Whitehorn }
14314c8945a0SNathan Whitehorn 
14324c8945a0SNathan Whitehorn /*
1433682c9e0fSNathan Whitehorn  * Print program's version.
1434682c9e0fSNathan Whitehorn  */
1435682c9e0fSNathan Whitehorn static void
1436682c9e0fSNathan Whitehorn PrintVersion(FILE *fp)
1437682c9e0fSNathan Whitehorn {
1438682c9e0fSNathan Whitehorn     fprintf(fp, "Version: %s\n", dialog_version());
1439682c9e0fSNathan Whitehorn }
1440682c9e0fSNathan Whitehorn 
1441682c9e0fSNathan Whitehorn /*
14424c8945a0SNathan Whitehorn  * Print program help-message
14434c8945a0SNathan Whitehorn  */
14444c8945a0SNathan Whitehorn static void
14454c8945a0SNathan Whitehorn Help(void)
14464c8945a0SNathan Whitehorn {
14474c8945a0SNathan Whitehorn     static const char *const tbl_1[] =
14484c8945a0SNathan Whitehorn     {
14494c8945a0SNathan Whitehorn 	"cdialog (ComeOn Dialog!) version %s",
1450f4f33ea0SBaptiste Daroussin 	"Copyright 2000-2017,2018 Thomas E. Dickey",
14514c8945a0SNathan Whitehorn 	"This is free software; see the source for copying conditions.  There is NO",
14524c8945a0SNathan Whitehorn 	"warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.",
14534c8945a0SNathan Whitehorn 	"",
14544c8945a0SNathan Whitehorn 	"* Display dialog boxes from shell scripts *",
14554c8945a0SNathan Whitehorn 	"",
14564c8945a0SNathan Whitehorn 	"Usage: %s <options> { --and-widget <options> }",
14574c8945a0SNathan Whitehorn 	"where options are \"common\" options, followed by \"box\" options",
14584c8945a0SNathan Whitehorn 	"",
14594c8945a0SNathan Whitehorn #ifdef HAVE_RC_FILE
14604c8945a0SNathan Whitehorn 	"Special options:",
14614c8945a0SNathan Whitehorn 	"  [--create-rc \"file\"]",
14624c8945a0SNathan Whitehorn #endif
14634c8945a0SNathan Whitehorn 	0
14644c8945a0SNathan Whitehorn     }, *const tbl_3[] =
14654c8945a0SNathan Whitehorn     {
14664c8945a0SNathan Whitehorn 	"",
14674c8945a0SNathan Whitehorn 	"Auto-size with height and width = 0. Maximize with height and width = -1.",
14684c8945a0SNathan Whitehorn 	"Global-auto-size if also menu_height/list_height = 0.",
14694c8945a0SNathan Whitehorn 	0
14704c8945a0SNathan Whitehorn     };
14717a1c0d96SNathan Whitehorn     size_t limit = sizeof(options) / sizeof(options[0]);
14727a1c0d96SNathan Whitehorn     size_t j, k;
14734c8945a0SNathan Whitehorn     const Options **opts;
14744c8945a0SNathan Whitehorn 
1475682c9e0fSNathan Whitehorn     end_dialog();
1476682c9e0fSNathan Whitehorn     dialog_state.output = stdout;
1477682c9e0fSNathan Whitehorn 
14784c8945a0SNathan Whitehorn     opts = dlg_calloc(const Options *, limit);
14794c8945a0SNathan Whitehorn     assert_ptr(opts, "Help");
14804c8945a0SNathan Whitehorn     for (j = 0; j < limit; ++j) {
14814c8945a0SNathan Whitehorn 	opts[j] = &(options[j]);
14824c8945a0SNathan Whitehorn     }
14834c8945a0SNathan Whitehorn     qsort(opts, limit, sizeof(Options *), compare_opts);
14844c8945a0SNathan Whitehorn 
14854c8945a0SNathan Whitehorn     PrintList(tbl_1);
14864c8945a0SNathan Whitehorn     fprintf(dialog_state.output, "Common options:\n ");
14874c8945a0SNathan Whitehorn     for (j = k = 0; j < limit; j++) {
14884c8945a0SNathan Whitehorn 	if ((opts[j]->pass & 1)
14894c8945a0SNathan Whitehorn 	    && opts[j]->help != 0) {
14907a1c0d96SNathan Whitehorn 	    size_t len = 6 + strlen(opts[j]->name) + strlen(opts[j]->help);
14914c8945a0SNathan Whitehorn 	    k += len;
14924c8945a0SNathan Whitehorn 	    if (k > 75) {
14934c8945a0SNathan Whitehorn 		fprintf(dialog_state.output, "\n ");
14944c8945a0SNathan Whitehorn 		k = len;
14954c8945a0SNathan Whitehorn 	    }
14964c8945a0SNathan Whitehorn 	    fprintf(dialog_state.output, " [--%s%s%s]", opts[j]->name,
14974c8945a0SNathan Whitehorn 		    *(opts[j]->help) ? " " : "", opts[j]->help);
14984c8945a0SNathan Whitehorn 	}
14994c8945a0SNathan Whitehorn     }
15004c8945a0SNathan Whitehorn     fprintf(dialog_state.output, "\nBox options:\n");
15014c8945a0SNathan Whitehorn     for (j = 0; j < limit; j++) {
15024c8945a0SNathan Whitehorn 	if ((opts[j]->pass & 2) != 0
15034c8945a0SNathan Whitehorn 	    && opts[j]->help != 0
1504f4f33ea0SBaptiste Daroussin 	    && lookupMode(opts[j]->code)) {
15054c8945a0SNathan Whitehorn 	    fprintf(dialog_state.output, "  --%-12s %s\n", opts[j]->name,
15064c8945a0SNathan Whitehorn 		    opts[j]->help);
15074c8945a0SNathan Whitehorn 	}
1508f4f33ea0SBaptiste Daroussin     }
15094c8945a0SNathan Whitehorn     PrintList(tbl_3);
15104c8945a0SNathan Whitehorn 
15114c8945a0SNathan Whitehorn     free(opts);
1512f4f33ea0SBaptiste Daroussin     handle_leaks();
15134c8945a0SNathan Whitehorn     dlg_exit(DLG_EXIT_OK);
15144c8945a0SNathan Whitehorn }
15154c8945a0SNathan Whitehorn 
15162a3e3873SBaptiste Daroussin #ifdef HAVE_DLG_TRACE
15172a3e3873SBaptiste Daroussin /*
15182a3e3873SBaptiste Daroussin  * Only the first call to dlg_trace will open a trace file.  But each time
15192a3e3873SBaptiste Daroussin  * --trace is parsed, we show the whole parameter list as it is at that moment,
15202a3e3873SBaptiste Daroussin  * counting discarded parameters.  The only way to capture the whole parameter
15212a3e3873SBaptiste Daroussin  * list is if --trace is the first option.
15222a3e3873SBaptiste Daroussin  */
15232a3e3873SBaptiste Daroussin static void
15242a3e3873SBaptiste Daroussin process_trace_option(char **argv, int *offset)
15252a3e3873SBaptiste Daroussin {
15262a3e3873SBaptiste Daroussin     int j;
15272a3e3873SBaptiste Daroussin 
15282a3e3873SBaptiste Daroussin     if (dialog_state.trace_output == 0) {
15292a3e3873SBaptiste Daroussin 	dlg_trace(optionString(argv, offset));
15302a3e3873SBaptiste Daroussin     } else {
1531f4f33ea0SBaptiste Daroussin 	DLG_TRACE(("# ignore extra --trace option\n"));
15322a3e3873SBaptiste Daroussin 	*offset += 1;
15332a3e3873SBaptiste Daroussin     }
15342a3e3873SBaptiste Daroussin 
1535f4f33ea0SBaptiste Daroussin     DLG_TRACE(("# Parameters:\n"));
15362a3e3873SBaptiste Daroussin     for (j = 0; argv[j] != 0; ++j) {
1537f4f33ea0SBaptiste Daroussin 	DLG_TRACE(("#\targv[%d] = %s\n", j, argv[j]));
15382a3e3873SBaptiste Daroussin     }
15392a3e3873SBaptiste Daroussin }
15402a3e3873SBaptiste Daroussin #endif
15412a3e3873SBaptiste Daroussin 
15424c8945a0SNathan Whitehorn /*
15434c8945a0SNathan Whitehorn  * "Common" options apply to all widgets more/less.  Most of the common options
15444c8945a0SNathan Whitehorn  * set values in dialog_vars, a few set dialog_state and a couple write to the
15454c8945a0SNathan Whitehorn  * output stream.
15464c8945a0SNathan Whitehorn  */
15474c8945a0SNathan Whitehorn static int
15484c8945a0SNathan Whitehorn process_common_options(int argc, char **argv, int offset, bool output)
15494c8945a0SNathan Whitehorn {
15504c8945a0SNathan Whitehorn     bool done = FALSE;
1551f4f33ea0SBaptiste Daroussin     eOptions code;
15524c8945a0SNathan Whitehorn 
1553f4f33ea0SBaptiste Daroussin     DLG_TRACE(("# process_common_options, offset %d\n", offset));
15542a3e3873SBaptiste Daroussin 
15554c8945a0SNathan Whitehorn     while (offset < argc && !done) {	/* Common options */
1556f4f33ea0SBaptiste Daroussin 	DLG_TRACE(("#\targv[%d] = %s\n", offset, argv[offset]));
1557f4f33ea0SBaptiste Daroussin 	switch (code = lookupOption(argv[offset], 1)) {
15584c8945a0SNathan Whitehorn 	case o_title:
15594c8945a0SNathan Whitehorn 	    dialog_vars.title = optionString(argv, &offset);
15604c8945a0SNathan Whitehorn 	    break;
15614c8945a0SNathan Whitehorn 	case o_backtitle:
15624c8945a0SNathan Whitehorn 	    dialog_vars.backtitle = optionString(argv, &offset);
15634c8945a0SNathan Whitehorn 	    break;
15644c8945a0SNathan Whitehorn 	case o_separate_widget:
15654c8945a0SNathan Whitehorn 	    dialog_state.separate_str = optionString(argv, &offset);
15664c8945a0SNathan Whitehorn 	    break;
15674c8945a0SNathan Whitehorn 	case o_separate_output:
15684c8945a0SNathan Whitehorn 	    dialog_vars.separate_output = TRUE;
15694c8945a0SNathan Whitehorn 	    break;
15704c8945a0SNathan Whitehorn 	case o_colors:
15714c8945a0SNathan Whitehorn 	    dialog_vars.colors = TRUE;
15724c8945a0SNathan Whitehorn 	    break;
15734c8945a0SNathan Whitehorn 	case o_cr_wrap:
15744c8945a0SNathan Whitehorn 	    dialog_vars.cr_wrap = TRUE;
15754c8945a0SNathan Whitehorn 	    break;
1576682c9e0fSNathan Whitehorn 	case o_no_nl_expand:
1577682c9e0fSNathan Whitehorn 	    dialog_vars.no_nl_expand = TRUE;
1578682c9e0fSNathan Whitehorn 	    break;
15794c8945a0SNathan Whitehorn 	case o_no_collapse:
15804c8945a0SNathan Whitehorn 	    dialog_vars.nocollapse = TRUE;
15814c8945a0SNathan Whitehorn 	    break;
15824c8945a0SNathan Whitehorn 	case o_no_kill:
15834c8945a0SNathan Whitehorn 	    dialog_vars.cant_kill = TRUE;
15844c8945a0SNathan Whitehorn 	    break;
15854c8945a0SNathan Whitehorn 	case o_nocancel:
15864c8945a0SNathan Whitehorn 	    dialog_vars.nocancel = TRUE;
15874c8945a0SNathan Whitehorn 	    break;
15884c8945a0SNathan Whitehorn 	case o_nook:
15894c8945a0SNathan Whitehorn 	    dialog_vars.nook = TRUE;
15904c8945a0SNathan Whitehorn 	    break;
15914c8945a0SNathan Whitehorn 	case o_quoted:
15924c8945a0SNathan Whitehorn 	    dialog_vars.quoted = TRUE;
15934c8945a0SNathan Whitehorn 	    break;
15944c8945a0SNathan Whitehorn 	case o_single_quoted:
15954c8945a0SNathan Whitehorn 	    dialog_vars.single_quoted = TRUE;
15964c8945a0SNathan Whitehorn 	    break;
15974c8945a0SNathan Whitehorn 	case o_size_err:
15984c8945a0SNathan Whitehorn 	    dialog_vars.size_err = TRUE;
15994c8945a0SNathan Whitehorn 	    break;
16004c8945a0SNathan Whitehorn 	case o_beep:
16014c8945a0SNathan Whitehorn 	    dialog_vars.beep_signal = TRUE;
16024c8945a0SNathan Whitehorn 	    break;
16034c8945a0SNathan Whitehorn 	case o_beep_after:
16044c8945a0SNathan Whitehorn 	    dialog_vars.beep_after_signal = TRUE;
16054c8945a0SNathan Whitehorn 	    break;
16064c8945a0SNathan Whitehorn 	case o_scrollbar:
16074c8945a0SNathan Whitehorn 	    dialog_state.use_scrollbar = TRUE;
16084c8945a0SNathan Whitehorn 	    break;
16094c8945a0SNathan Whitehorn 	case o_shadow:
16104c8945a0SNathan Whitehorn 	    dialog_state.use_shadow = TRUE;
16114c8945a0SNathan Whitehorn 	    break;
16124c8945a0SNathan Whitehorn 	case o_defaultno:
16134c8945a0SNathan Whitehorn 	    dialog_vars.defaultno = TRUE;
16142a3e3873SBaptiste Daroussin 	    dialog_vars.default_button = DLG_EXIT_CANCEL;
16152a3e3873SBaptiste Daroussin 	    break;
16162a3e3873SBaptiste Daroussin 	case o_default_button:
16172a3e3873SBaptiste Daroussin 	    dialog_vars.default_button = button_code(optionString(argv, &offset));
16182a3e3873SBaptiste Daroussin 	    dialog_vars.defaultno = dialog_vars.default_button == DLG_EXIT_CANCEL;
16194c8945a0SNathan Whitehorn 	    break;
16204c8945a0SNathan Whitehorn 	case o_default_item:
16214c8945a0SNathan Whitehorn 	    dialog_vars.default_item = optionString(argv, &offset);
16224c8945a0SNathan Whitehorn 	    break;
16234c8945a0SNathan Whitehorn 	case o_insecure:
16244c8945a0SNathan Whitehorn 	    dialog_vars.insecure = TRUE;
16254c8945a0SNathan Whitehorn 	    break;
16264c8945a0SNathan Whitehorn 	case o_item_help:
16274c8945a0SNathan Whitehorn 	    dialog_vars.item_help = TRUE;
16284c8945a0SNathan Whitehorn 	    break;
1629682c9e0fSNathan Whitehorn 	case o_help_line:
1630682c9e0fSNathan Whitehorn 	    dialog_vars.help_line = optionString(argv, &offset);
1631682c9e0fSNathan Whitehorn 	    break;
1632682c9e0fSNathan Whitehorn 	case o_help_file:
1633682c9e0fSNathan Whitehorn 	    dialog_vars.help_file = optionString(argv, &offset);
1634682c9e0fSNathan Whitehorn 	    break;
16354c8945a0SNathan Whitehorn 	case o_help_button:
16364c8945a0SNathan Whitehorn 	    dialog_vars.help_button = TRUE;
16374c8945a0SNathan Whitehorn 	    break;
16384c8945a0SNathan Whitehorn 	case o_help_status:
16394c8945a0SNathan Whitehorn 	    dialog_vars.help_status = TRUE;
16404c8945a0SNathan Whitehorn 	    break;
1641febdb468SDevin Teske 	case o_help_tags:
1642febdb468SDevin Teske 	    dialog_vars.help_tags = TRUE;
1643febdb468SDevin Teske 	    break;
16444c8945a0SNathan Whitehorn 	case o_extra_button:
16454c8945a0SNathan Whitehorn 	    dialog_vars.extra_button = TRUE;
16464c8945a0SNathan Whitehorn 	    break;
16474c8945a0SNathan Whitehorn 	case o_ignore:
16484c8945a0SNathan Whitehorn 	    ignore_unknown = TRUE;
16494c8945a0SNathan Whitehorn 	    break;
16504c8945a0SNathan Whitehorn 	case o_keep_window:
16514c8945a0SNathan Whitehorn 	    dialog_vars.keep_window = TRUE;
16524c8945a0SNathan Whitehorn 	    break;
16532a3e3873SBaptiste Daroussin 	case o_last_key:
16542a3e3873SBaptiste Daroussin 	    dialog_vars.last_key = TRUE;
16552a3e3873SBaptiste Daroussin 	    break;
16564c8945a0SNathan Whitehorn 	case o_no_shadow:
16574c8945a0SNathan Whitehorn 	    dialog_state.use_shadow = FALSE;
16584c8945a0SNathan Whitehorn 	    break;
16594c8945a0SNathan Whitehorn 	case o_print_size:
16604c8945a0SNathan Whitehorn 	    dialog_vars.print_siz = TRUE;
16614c8945a0SNathan Whitehorn 	    break;
1662f4f33ea0SBaptiste Daroussin 	case o_print_text_only:
1663f4f33ea0SBaptiste Daroussin 	case o_print_text_size:
1664f4f33ea0SBaptiste Daroussin 	    PrintTextOnly(argv, &offset, code);
1665f4f33ea0SBaptiste Daroussin 	    break;
16664c8945a0SNathan Whitehorn 	case o_print_maxsize:
16674c8945a0SNathan Whitehorn 	    if (output) {
1668f4f33ea0SBaptiste Daroussin 		IgnoreNonScreen(argv, offset);
16694c8945a0SNathan Whitehorn 		fflush(dialog_state.output);
16704c8945a0SNathan Whitehorn 		fprintf(dialog_state.output, "MaxSize: %d, %d\n", SLINES, SCOLS);
16714c8945a0SNathan Whitehorn 	    }
16724c8945a0SNathan Whitehorn 	    break;
16734c8945a0SNathan Whitehorn 	case o_print_version:
16744c8945a0SNathan Whitehorn 	    if (output) {
1675682c9e0fSNathan Whitehorn 		PrintVersion(dialog_state.output);
16764c8945a0SNathan Whitehorn 	    }
16774c8945a0SNathan Whitehorn 	    break;
16784c8945a0SNathan Whitehorn 	case o_separator:
16794c8945a0SNathan Whitehorn 	case o_output_separator:
16804c8945a0SNathan Whitehorn 	    dialog_vars.output_separator = optionString(argv, &offset);
16814c8945a0SNathan Whitehorn 	    break;
16824c8945a0SNathan Whitehorn 	case o_column_separator:
16834c8945a0SNathan Whitehorn 	    dialog_vars.column_separator = optionString(argv, &offset);
16844c8945a0SNathan Whitehorn 	    break;
16854c8945a0SNathan Whitehorn 	case o_tab_correct:
16864c8945a0SNathan Whitehorn 	    dialog_vars.tab_correct = TRUE;
16874c8945a0SNathan Whitehorn 	    break;
16884c8945a0SNathan Whitehorn 	case o_sleep:
16894c8945a0SNathan Whitehorn 	    dialog_vars.sleep_secs = optionValue(argv, &offset);
16904c8945a0SNathan Whitehorn 	    break;
16914c8945a0SNathan Whitehorn 	case o_timeout:
16924c8945a0SNathan Whitehorn 	    dialog_vars.timeout_secs = optionValue(argv, &offset);
16934c8945a0SNathan Whitehorn 	    break;
16944c8945a0SNathan Whitehorn 	case o_max_input:
16954c8945a0SNathan Whitehorn 	    dialog_vars.max_input = optionValue(argv, &offset);
16964c8945a0SNathan Whitehorn 	    break;
16974c8945a0SNathan Whitehorn 	case o_tab_len:
16984c8945a0SNathan Whitehorn 	    dialog_state.tab_len = optionValue(argv, &offset);
16994c8945a0SNathan Whitehorn 	    break;
17004c8945a0SNathan Whitehorn 	case o_trim:
17014c8945a0SNathan Whitehorn 	    dialog_vars.trim_whitespace = TRUE;
17024c8945a0SNathan Whitehorn 	    break;
17034c8945a0SNathan Whitehorn 	case o_visit_items:
17044c8945a0SNathan Whitehorn 	    dialog_state.visit_items = TRUE;
17052a3e3873SBaptiste Daroussin 	    dialog_state.visit_cols = 1;
17064c8945a0SNathan Whitehorn 	    break;
17074c8945a0SNathan Whitehorn 	case o_aspect:
17084c8945a0SNathan Whitehorn 	    dialog_state.aspect_ratio = optionValue(argv, &offset);
17094c8945a0SNathan Whitehorn 	    break;
17104c8945a0SNathan Whitehorn 	case o_begin:
17114c8945a0SNathan Whitehorn 	    dialog_vars.begin_set = TRUE;
17124c8945a0SNathan Whitehorn 	    dialog_vars.begin_y = optionValue(argv, &offset);
17134c8945a0SNathan Whitehorn 	    dialog_vars.begin_x = optionValue(argv, &offset);
17144c8945a0SNathan Whitehorn 	    break;
17154c8945a0SNathan Whitehorn 	case o_clear:
17164c8945a0SNathan Whitehorn 	    dialog_vars.dlg_clear_screen = TRUE;
17174c8945a0SNathan Whitehorn 	    break;
17184c8945a0SNathan Whitehorn 	case o_yes_label:
17194c8945a0SNathan Whitehorn 	    dialog_vars.yes_label = optionString(argv, &offset);
17204c8945a0SNathan Whitehorn 	    break;
17214c8945a0SNathan Whitehorn 	case o_no_label:
17224c8945a0SNathan Whitehorn 	    dialog_vars.no_label = optionString(argv, &offset);
17234c8945a0SNathan Whitehorn 	    break;
17244c8945a0SNathan Whitehorn 	case o_ok_label:
17254c8945a0SNathan Whitehorn 	    dialog_vars.ok_label = optionString(argv, &offset);
17264c8945a0SNathan Whitehorn 	    break;
17274c8945a0SNathan Whitehorn 	case o_cancel_label:
17284c8945a0SNathan Whitehorn 	    dialog_vars.cancel_label = optionString(argv, &offset);
17294c8945a0SNathan Whitehorn 	    break;
17304c8945a0SNathan Whitehorn 	case o_extra_label:
17314c8945a0SNathan Whitehorn 	    dialog_vars.extra_label = optionString(argv, &offset);
17324c8945a0SNathan Whitehorn 	    break;
17334c8945a0SNathan Whitehorn 	case o_exit_label:
17344c8945a0SNathan Whitehorn 	    dialog_vars.exit_label = optionString(argv, &offset);
17354c8945a0SNathan Whitehorn 	    break;
17364c8945a0SNathan Whitehorn 	case o_help_label:
17374c8945a0SNathan Whitehorn 	    dialog_vars.help_label = optionString(argv, &offset);
17384c8945a0SNathan Whitehorn 	    break;
17394c8945a0SNathan Whitehorn 	case o_date_format:
17404c8945a0SNathan Whitehorn 	    dialog_vars.date_format = optionString(argv, &offset);
17414c8945a0SNathan Whitehorn 	    break;
17424c8945a0SNathan Whitehorn 	case o_time_format:
17434c8945a0SNathan Whitehorn 	    dialog_vars.time_format = optionString(argv, &offset);
17444c8945a0SNathan Whitehorn 	    break;
17454c8945a0SNathan Whitehorn 	case o_keep_tite:
17464c8945a0SNathan Whitehorn 	    dialog_vars.keep_tite = TRUE;
17474c8945a0SNathan Whitehorn 	    break;
17484c8945a0SNathan Whitehorn 	case o_ascii_lines:
17494c8945a0SNathan Whitehorn 	    dialog_vars.ascii_lines = TRUE;
17504c8945a0SNathan Whitehorn 	    dialog_vars.no_lines = FALSE;
17514c8945a0SNathan Whitehorn 	    break;
17524c8945a0SNathan Whitehorn 	case o_no_lines:
17534c8945a0SNathan Whitehorn 	    dialog_vars.no_lines = TRUE;
17544c8945a0SNathan Whitehorn 	    dialog_vars.ascii_lines = FALSE;
17554c8945a0SNathan Whitehorn 	    break;
17567a1c0d96SNathan Whitehorn 	case o_no_mouse:
17577a1c0d96SNathan Whitehorn 	    dialog_state.no_mouse = TRUE;
1758682c9e0fSNathan Whitehorn 	    mouse_close();
17597a1c0d96SNathan Whitehorn 	    break;
17602a3e3873SBaptiste Daroussin #ifdef HAVE_WHIPTAIL
17612a3e3873SBaptiste Daroussin 	case o_topleft:
17622a3e3873SBaptiste Daroussin 	    dialog_vars.begin_set = TRUE;
17632a3e3873SBaptiste Daroussin 	    dialog_vars.begin_y = 0;
17642a3e3873SBaptiste Daroussin 	    dialog_vars.begin_x = 0;
17652a3e3873SBaptiste Daroussin 	    break;
17664c8945a0SNathan Whitehorn 	case o_fullbutton:
17674c8945a0SNathan Whitehorn 	    /* ignore */
17684c8945a0SNathan Whitehorn 	    break;
17692a3e3873SBaptiste Daroussin #endif
17704c8945a0SNathan Whitehorn 	    /* options of Xdialog which we ignore */
17714c8945a0SNathan Whitehorn 	case o_icon:
17724c8945a0SNathan Whitehorn 	case o_wmclass:
17734c8945a0SNathan Whitehorn 	    (void) optionString(argv, &offset);
17744c8945a0SNathan Whitehorn 	    /* FALLTHRU */
17754c8945a0SNathan Whitehorn 	case o_allow_close:
17764c8945a0SNathan Whitehorn 	case o_auto_placement:
17774c8945a0SNathan Whitehorn 	case o_fixed_font:
17784c8945a0SNathan Whitehorn 	case o_keep_colors:
17794c8945a0SNathan Whitehorn 	case o_no_close:
17804c8945a0SNathan Whitehorn 	case o_no_cr_wrap:
17814c8945a0SNathan Whitehorn 	case o_screen_center:
17824c8945a0SNathan Whitehorn 	case o_smooth:
17834c8945a0SNathan Whitehorn 	case o_under_mouse:
17844c8945a0SNathan Whitehorn 	    break;
17854c8945a0SNathan Whitehorn 	case o_unknown:
17864c8945a0SNathan Whitehorn 	    if (ignore_unknown)
17874c8945a0SNathan Whitehorn 		break;
17884c8945a0SNathan Whitehorn 	    /* FALLTHRU */
17894c8945a0SNathan Whitehorn 	default:		/* no more common options */
17904c8945a0SNathan Whitehorn 	    done = TRUE;
17914c8945a0SNathan Whitehorn 	    break;
17924c8945a0SNathan Whitehorn #ifdef HAVE_DLG_TRACE
17934c8945a0SNathan Whitehorn 	case o_trace:
17942a3e3873SBaptiste Daroussin 	    process_trace_option(argv, &offset);
17952a3e3873SBaptiste Daroussin 	    break;
17962a3e3873SBaptiste Daroussin #endif
17972a3e3873SBaptiste Daroussin #if defined(HAVE_XDIALOG2) || defined(HAVE_WHIPTAIL)
17982a3e3873SBaptiste Daroussin 	case o_no_items:
17992a3e3873SBaptiste Daroussin 	    dialog_vars.no_items = TRUE;
18002a3e3873SBaptiste Daroussin 	    break;
18012a3e3873SBaptiste Daroussin 	case o_no_tags:
18022a3e3873SBaptiste Daroussin 	    dialog_vars.no_tags = TRUE;
18034c8945a0SNathan Whitehorn 	    break;
18044c8945a0SNathan Whitehorn #endif
1805f4f33ea0SBaptiste Daroussin #ifdef HAVE_XDIALOG2
1806f4f33ea0SBaptiste Daroussin 	case o_reorder:
1807f4f33ea0SBaptiste Daroussin 	    dialog_vars.reorder = TRUE;
1808f4f33ea0SBaptiste Daroussin 	    break;
1809f4f33ea0SBaptiste Daroussin #endif
1810f4f33ea0SBaptiste Daroussin #ifdef HAVE_XDIALOG
1811f4f33ea0SBaptiste Daroussin 	case o_week_start:
1812f4f33ea0SBaptiste Daroussin 	    dialog_vars.week_start = optionString(argv, &offset);
1813f4f33ea0SBaptiste Daroussin 	    break;
1814f4f33ea0SBaptiste Daroussin #endif
1815f4f33ea0SBaptiste Daroussin 	case o_iso_week:
1816f4f33ea0SBaptiste Daroussin 	    dialog_vars.iso_week = TRUE;
1817f4f33ea0SBaptiste Daroussin 	    if (dialog_vars.week_start == 0) {	/* Monday is implied */
1818f4f33ea0SBaptiste Daroussin 		static char default_1st[] = "1";
1819f4f33ea0SBaptiste Daroussin 		dialog_vars.week_start = default_1st;
1820f4f33ea0SBaptiste Daroussin 	    }
1821f4f33ea0SBaptiste Daroussin 	    break;
18224c8945a0SNathan Whitehorn 	}
18234c8945a0SNathan Whitehorn 	if (!done)
18244c8945a0SNathan Whitehorn 	    offset++;
18254c8945a0SNathan Whitehorn     }
1826f4f33ea0SBaptiste Daroussin 
1827f4f33ea0SBaptiste Daroussin     if (dialog_state.aspect_ratio == 0)
1828f4f33ea0SBaptiste Daroussin 	dialog_state.aspect_ratio = DEFAULT_ASPECT_RATIO;
1829f4f33ea0SBaptiste Daroussin 
18304c8945a0SNathan Whitehorn     return offset;
18314c8945a0SNathan Whitehorn }
18324c8945a0SNathan Whitehorn 
18334c8945a0SNathan Whitehorn /*
18344c8945a0SNathan Whitehorn  * Initialize options at the start of a series of common options culminating
18354c8945a0SNathan Whitehorn  * in a widget.
18364c8945a0SNathan Whitehorn  */
18374c8945a0SNathan Whitehorn static void
18384c8945a0SNathan Whitehorn init_result(char *buffer)
18394c8945a0SNathan Whitehorn {
18404c8945a0SNathan Whitehorn     static bool first = TRUE;
18414c8945a0SNathan Whitehorn 
1842f4f33ea0SBaptiste Daroussin     DLG_TRACE(("# init_result\n"));
18432a3e3873SBaptiste Daroussin 
18444c8945a0SNathan Whitehorn     /* clear everything we do not save for the next widget */
18454c8945a0SNathan Whitehorn     memset(&dialog_vars, 0, sizeof(dialog_vars));
18464c8945a0SNathan Whitehorn 
18474c8945a0SNathan Whitehorn     dialog_vars.input_result = buffer;
18484c8945a0SNathan Whitehorn     dialog_vars.input_result[0] = '\0';
18494c8945a0SNathan Whitehorn 
18502a3e3873SBaptiste Daroussin     dialog_vars.default_button = -1;
18512a3e3873SBaptiste Daroussin 
18524c8945a0SNathan Whitehorn     /*
18534c8945a0SNathan Whitehorn      * The first time this is called, check for common options given by an
18544c8945a0SNathan Whitehorn      * environment variable.
18554c8945a0SNathan Whitehorn      */
18564c8945a0SNathan Whitehorn     if (first) {
18574c8945a0SNathan Whitehorn 	char *env = getenv("DIALOGOPTS");
18584c8945a0SNathan Whitehorn 	if (env != 0)
18594c8945a0SNathan Whitehorn 	    env = dlg_strclone(env);
18604c8945a0SNathan Whitehorn 	if (env != 0) {
18617a1c0d96SNathan Whitehorn 	    special_argv = dlg_string_to_argv(env);
18627a1c0d96SNathan Whitehorn 	    special_argc = dlg_count_argv(special_argv);
18634c8945a0SNathan Whitehorn 	}
18642a3e3873SBaptiste Daroussin 	first = FALSE;
18654c8945a0SNathan Whitehorn     }
18662a3e3873SBaptiste Daroussin 
18674c8945a0SNathan Whitehorn     if (special_argv != 0) {
18684c8945a0SNathan Whitehorn 	process_common_options(special_argc, special_argv, 0, FALSE);
18694c8945a0SNathan Whitehorn     }
18704c8945a0SNathan Whitehorn }
18714c8945a0SNathan Whitehorn 
18724c8945a0SNathan Whitehorn int
18734c8945a0SNathan Whitehorn main(int argc, char *argv[])
18744c8945a0SNathan Whitehorn {
18754c8945a0SNathan Whitehorn     char temp[256];
18764c8945a0SNathan Whitehorn     bool esc_pressed = FALSE;
18774c8945a0SNathan Whitehorn     bool keep_tite = FALSE;
1878f4f33ea0SBaptiste Daroussin     bool first_time = TRUE;
18794c8945a0SNathan Whitehorn     int offset = 1;
18804c8945a0SNathan Whitehorn     int offset_add;
18814c8945a0SNathan Whitehorn     int retval = DLG_EXIT_OK;
18824c8945a0SNathan Whitehorn     int j, have;
18834c8945a0SNathan Whitehorn     eOptions code;
18844c8945a0SNathan Whitehorn     const Mode *modePtr;
18854c8945a0SNathan Whitehorn     char my_buffer[MAX_LEN + 1];
18864c8945a0SNathan Whitehorn 
18874c8945a0SNathan Whitehorn     memset(&dialog_state, 0, sizeof(dialog_state));
18884c8945a0SNathan Whitehorn     memset(&dialog_vars, 0, sizeof(dialog_vars));
18894c8945a0SNathan Whitehorn 
18904c8945a0SNathan Whitehorn #if defined(ENABLE_NLS)
18914c8945a0SNathan Whitehorn     /* initialize locale support */
18924c8945a0SNathan Whitehorn     setlocale(LC_ALL, "");
18937a1c0d96SNathan Whitehorn     bindtextdomain(NLS_TEXTDOMAIN, LOCALEDIR);
18947a1c0d96SNathan Whitehorn     textdomain(NLS_TEXTDOMAIN);
18954c8945a0SNathan Whitehorn #elif defined(HAVE_SETLOCALE)
18964c8945a0SNathan Whitehorn     (void) setlocale(LC_ALL, "");
18974c8945a0SNathan Whitehorn #endif
18984c8945a0SNathan Whitehorn 
1899f4f33ea0SBaptiste Daroussin     init_result(my_buffer);	/* honor $DIALOGOPTS */
19004c8945a0SNathan Whitehorn     unescape_argv(&argc, &argv);
19014c8945a0SNathan Whitehorn     program = argv[0];
19024c8945a0SNathan Whitehorn     dialog_state.output = stderr;
19034c8945a0SNathan Whitehorn     dialog_state.input = stdin;
19044c8945a0SNathan Whitehorn 
19054c8945a0SNathan Whitehorn     /*
19064c8945a0SNathan Whitehorn      * Look for the last --stdout, --stderr or --output-fd option, and use
19074c8945a0SNathan Whitehorn      * that.  We can only write to one of them.  If --stdout is used, that
19084c8945a0SNathan Whitehorn      * can interfere with initializing the curses library, so we want to
19094c8945a0SNathan Whitehorn      * know explicitly if it is used.
1910682c9e0fSNathan Whitehorn      *
1911682c9e0fSNathan Whitehorn      * Also, look for any --version or --help message, processing those
1912682c9e0fSNathan Whitehorn      * immediately.
19134c8945a0SNathan Whitehorn      */
19144c8945a0SNathan Whitehorn     while (offset < argc) {
19154c8945a0SNathan Whitehorn 	int base = offset;
19164c8945a0SNathan Whitehorn 	switch (lookupOption(argv[offset], 7)) {
19174c8945a0SNathan Whitehorn 	case o_stdout:
19184c8945a0SNathan Whitehorn 	    dialog_state.output = stdout;
19194c8945a0SNathan Whitehorn 	    break;
19204c8945a0SNathan Whitehorn 	case o_stderr:
19214c8945a0SNathan Whitehorn 	    dialog_state.output = stderr;
19224c8945a0SNathan Whitehorn 	    break;
19234c8945a0SNathan Whitehorn 	case o_input_fd:
19244c8945a0SNathan Whitehorn 	    if ((j = optionValue(argv, &offset)) < 0
1925f4f33ea0SBaptiste Daroussin 		|| (dialog_state.input = fdopen(j, "r")) == 0) {
1926f4f33ea0SBaptiste Daroussin 		handle_leaks();
19274c8945a0SNathan Whitehorn 		dlg_exiterr("Cannot open input-fd\n");
1928f4f33ea0SBaptiste Daroussin 	    }
19294c8945a0SNathan Whitehorn 	    break;
19304c8945a0SNathan Whitehorn 	case o_output_fd:
19314c8945a0SNathan Whitehorn 	    if ((j = optionValue(argv, &offset)) < 0
1932f4f33ea0SBaptiste Daroussin 		|| (dialog_state.output = fdopen(j, "w")) == 0) {
1933f4f33ea0SBaptiste Daroussin 		handle_leaks();
19344c8945a0SNathan Whitehorn 		dlg_exiterr("Cannot open output-fd\n");
1935f4f33ea0SBaptiste Daroussin 	    }
19364c8945a0SNathan Whitehorn 	    break;
19374c8945a0SNathan Whitehorn 	case o_keep_tite:
19384c8945a0SNathan Whitehorn 	    keep_tite = TRUE;
19394c8945a0SNathan Whitehorn 	    break;
1940682c9e0fSNathan Whitehorn 	case o_version:
1941682c9e0fSNathan Whitehorn 	    dialog_state.output = stdout;
1942682c9e0fSNathan Whitehorn 	    PrintVersion(dialog_state.output);
1943f4f33ea0SBaptiste Daroussin 	    dlg_exit(DLG_EXIT_OK);
1944682c9e0fSNathan Whitehorn 	    break;
1945682c9e0fSNathan Whitehorn 	case o_help:
1946682c9e0fSNathan Whitehorn 	    Help();
1947682c9e0fSNathan Whitehorn 	    break;
19482a3e3873SBaptiste Daroussin #ifdef HAVE_DLG_TRACE
19492a3e3873SBaptiste Daroussin 	case o_trace:
19502a3e3873SBaptiste Daroussin 	    /*
19512a3e3873SBaptiste Daroussin 	     * Process/remove the --trace option if it is the first option.
19522a3e3873SBaptiste Daroussin 	     * Otherwise, process it in more/less expected order as a
19532a3e3873SBaptiste Daroussin 	     * "common" option.
19542a3e3873SBaptiste Daroussin 	     */
19552a3e3873SBaptiste Daroussin 	    if (base == 1) {
19562a3e3873SBaptiste Daroussin 		process_trace_option(argv, &offset);
19572a3e3873SBaptiste Daroussin 		break;
19582a3e3873SBaptiste Daroussin 	    } else {
19592a3e3873SBaptiste Daroussin 		++offset;
19602a3e3873SBaptiste Daroussin 		continue;
19612a3e3873SBaptiste Daroussin 	    }
19622a3e3873SBaptiste Daroussin #endif
19634c8945a0SNathan Whitehorn 	default:
19644c8945a0SNathan Whitehorn 	    ++offset;
19654c8945a0SNathan Whitehorn 	    continue;
19664c8945a0SNathan Whitehorn 	}
1967f4f33ea0SBaptiste Daroussin 	DLG_TRACE(("# discarding %d parameters starting with argv[%d] (%s)\n",
19682a3e3873SBaptiste Daroussin 		   1 + offset - base, base,
1969f4f33ea0SBaptiste Daroussin 		   argv[base]));
19704c8945a0SNathan Whitehorn 	for (j = base; j < argc; ++j) {
19714c8945a0SNathan Whitehorn 	    dialog_argv[j] = dialog_argv[j + 1 + (offset - base)];
19724c8945a0SNathan Whitehorn 	}
19734c8945a0SNathan Whitehorn 	argc -= (1 + offset - base);
19744c8945a0SNathan Whitehorn 	offset = base;
19754c8945a0SNathan Whitehorn     }
19764c8945a0SNathan Whitehorn     offset = 1;
19774c8945a0SNathan Whitehorn     init_result(my_buffer);
1978f4f33ea0SBaptiste Daroussin     dialog_vars.keep_tite = keep_tite;	/* init_result() cleared global */
19794c8945a0SNathan Whitehorn 
1980682c9e0fSNathan Whitehorn     /*
1981682c9e0fSNathan Whitehorn      * Dialog's output may be redirected (see above).  Handle the special
1982682c9e0fSNathan Whitehorn      * case of options that only report information without interaction.
1983682c9e0fSNathan Whitehorn      */
1984682c9e0fSNathan Whitehorn     if (argc == 2) {
1985f4f33ea0SBaptiste Daroussin 	switch (code = lookupOption(argv[1], 7)) {
19864c8945a0SNathan Whitehorn 	case o_print_maxsize:
19874c8945a0SNathan Whitehorn 	    (void) initscr();
19884c8945a0SNathan Whitehorn 	    endwin();
19894c8945a0SNathan Whitehorn 	    fflush(dialog_state.output);
19904c8945a0SNathan Whitehorn 	    fprintf(dialog_state.output, "MaxSize: %d, %d\n", SLINES, SCOLS);
19914c8945a0SNathan Whitehorn 	    break;
19924c8945a0SNathan Whitehorn 	case o_print_version:
1993682c9e0fSNathan Whitehorn 	    PrintVersion(dialog_state.output);
19944c8945a0SNathan Whitehorn 	    break;
19954c8945a0SNathan Whitehorn 	case o_clear:
19964c8945a0SNathan Whitehorn 	    initscr();
19974c8945a0SNathan Whitehorn 	    refresh();
19984c8945a0SNathan Whitehorn 	    endwin();
19994c8945a0SNathan Whitehorn 	    break;
20004c8945a0SNathan Whitehorn 	case o_ignore:
20014c8945a0SNathan Whitehorn 	    break;
20024c8945a0SNathan Whitehorn 	default:
20034c8945a0SNathan Whitehorn 	    Help();
20044c8945a0SNathan Whitehorn 	    break;
20054c8945a0SNathan Whitehorn 	}
2006f4f33ea0SBaptiste Daroussin 	dlg_exit(DLG_EXIT_OK);
2007f4f33ea0SBaptiste Daroussin     } else if (argc < 2) {
20084c8945a0SNathan Whitehorn 	Help();
20094c8945a0SNathan Whitehorn     }
20104c8945a0SNathan Whitehorn #ifdef HAVE_RC_FILE
2011f4f33ea0SBaptiste Daroussin     else if (lookupOption(argv[1], 7) == o_create_rc) {
20124c8945a0SNathan Whitehorn 	if (argc != 3) {
20134c8945a0SNathan Whitehorn 	    sprintf(temp, "Expected a filename for %.50s", argv[1]);
20144c8945a0SNathan Whitehorn 	    Usage(temp);
20154c8945a0SNathan Whitehorn 	}
2016f4f33ea0SBaptiste Daroussin 	if (dlg_parse_rc() == -1) {	/* Read the configuration file */
2017f4f33ea0SBaptiste Daroussin 	    handle_leaks();
20184c8945a0SNathan Whitehorn 	    dlg_exiterr("dialog: dlg_parse_rc");
2019f4f33ea0SBaptiste Daroussin 	}
20204c8945a0SNathan Whitehorn 	dlg_create_rc(argv[2]);
2021f4f33ea0SBaptiste Daroussin 	dlg_exit(DLG_EXIT_OK);
20224c8945a0SNathan Whitehorn     }
20234c8945a0SNathan Whitehorn #endif
2024f4f33ea0SBaptiste Daroussin     else {
2025f4f33ea0SBaptiste Daroussin 	/*
2026f4f33ea0SBaptiste Daroussin 	 * Handle combinations of common options including --print-text-only
2027f4f33ea0SBaptiste Daroussin 	 * which can be done before involving curses, in case we can exit
2028f4f33ea0SBaptiste Daroussin 	 * without initializing curses (and writing to the terminal).
2029f4f33ea0SBaptiste Daroussin 	 */
2030f4f33ea0SBaptiste Daroussin 	offset = process_common_options(argc, argv, offset, TRUE);
2031f4f33ea0SBaptiste Daroussin 	if (offset >= argc)
2032f4f33ea0SBaptiste Daroussin 	    dlg_exit(DLG_EXIT_OK);
2033f4f33ea0SBaptiste Daroussin     }
20344c8945a0SNathan Whitehorn 
20354c8945a0SNathan Whitehorn     init_dialog(dialog_state.input, dialog_state.output);
20364c8945a0SNathan Whitehorn 
20374c8945a0SNathan Whitehorn     while (offset < argc && !esc_pressed) {
2038f4f33ea0SBaptiste Daroussin 	if (first_time) {
2039f4f33ea0SBaptiste Daroussin 	    first_time = FALSE;
2040f4f33ea0SBaptiste Daroussin 	} else {
20414c8945a0SNathan Whitehorn 	    init_result(my_buffer);
20424c8945a0SNathan Whitehorn 	    offset = process_common_options(argc, argv, offset, TRUE);
2043f4f33ea0SBaptiste Daroussin 	}
20444c8945a0SNathan Whitehorn 
20454c8945a0SNathan Whitehorn 	if (argv[offset] == NULL) {
20464c8945a0SNathan Whitehorn 	    if (ignore_unknown)
20474c8945a0SNathan Whitehorn 		break;
20484c8945a0SNathan Whitehorn 	    Usage("Expected a box option");
20494c8945a0SNathan Whitehorn 	}
20504c8945a0SNathan Whitehorn 
2051f4f33ea0SBaptiste Daroussin 	if (dialog_vars.separate_output) {
2052f4f33ea0SBaptiste Daroussin 	    switch (lookupOption(argv[offset], 2)) {
2053f4f33ea0SBaptiste Daroussin #ifdef HAVE_XDIALOG2
2054f4f33ea0SBaptiste Daroussin 	    case o_buildlist:
2055f4f33ea0SBaptiste Daroussin 	    case o_treeview:
2056f4f33ea0SBaptiste Daroussin #endif
2057f4f33ea0SBaptiste Daroussin 	    case o_checklist:
2058f4f33ea0SBaptiste Daroussin 		break;
2059f4f33ea0SBaptiste Daroussin 	    default:
2060f4f33ea0SBaptiste Daroussin 		sprintf(temp,
2061f4f33ea0SBaptiste Daroussin 			"Unexpected widget with --separate-output %.20s",
2062f4f33ea0SBaptiste Daroussin 			argv[offset]);
20634c8945a0SNathan Whitehorn 		Usage(temp);
20644c8945a0SNathan Whitehorn 	    }
2065f4f33ea0SBaptiste Daroussin 	}
20664c8945a0SNathan Whitehorn 
20674c8945a0SNathan Whitehorn 	dlg_put_backtitle();
20684c8945a0SNathan Whitehorn 
20694c8945a0SNathan Whitehorn 	/* use a table to look for the requested mode, to avoid code duplication */
20704c8945a0SNathan Whitehorn 
20714c8945a0SNathan Whitehorn 	modePtr = 0;
20724c8945a0SNathan Whitehorn 	if ((code = lookupOption(argv[offset], 2)) != o_unknown)
20734c8945a0SNathan Whitehorn 	    modePtr = lookupMode(code);
20744c8945a0SNathan Whitehorn 	if (modePtr == 0) {
20754c8945a0SNathan Whitehorn 	    sprintf(temp, "%s option %.20s",
20764c8945a0SNathan Whitehorn 		    lookupOption(argv[offset], 7) != o_unknown
20774c8945a0SNathan Whitehorn 		    ? "Unexpected"
20784c8945a0SNathan Whitehorn 		    : "Unknown",
20794c8945a0SNathan Whitehorn 		    argv[offset]);
20804c8945a0SNathan Whitehorn 	    Usage(temp);
20814c8945a0SNathan Whitehorn 	}
20824c8945a0SNathan Whitehorn 
20834c8945a0SNathan Whitehorn 	have = arg_rest(&argv[offset]);
20844c8945a0SNathan Whitehorn 	if (have < modePtr->argmin) {
20854c8945a0SNathan Whitehorn 	    sprintf(temp, "Expected at least %d tokens for %.20s, have %d",
20864c8945a0SNathan Whitehorn 		    modePtr->argmin - 1, argv[offset],
20874c8945a0SNathan Whitehorn 		    have - 1);
20884c8945a0SNathan Whitehorn 	    Usage(temp);
20894c8945a0SNathan Whitehorn 	}
20904c8945a0SNathan Whitehorn 	if (modePtr->argmax && have > modePtr->argmax) {
20914c8945a0SNathan Whitehorn 	    sprintf(temp,
20924c8945a0SNathan Whitehorn 		    "Expected no more than %d tokens for %.20s, have %d",
20934c8945a0SNathan Whitehorn 		    modePtr->argmax - 1, argv[offset],
20944c8945a0SNathan Whitehorn 		    have - 1);
20954c8945a0SNathan Whitehorn 	    Usage(temp);
20964c8945a0SNathan Whitehorn 	}
20974c8945a0SNathan Whitehorn 
20984c8945a0SNathan Whitehorn 	/*
20994c8945a0SNathan Whitehorn 	 * Trim whitespace from non-title option values, e.g., the ones that
21004c8945a0SNathan Whitehorn 	 * will be used as captions or prompts.   Do that only for the widget
21014c8945a0SNathan Whitehorn 	 * we are about to process, since the "--trim" option is reset before
21024c8945a0SNathan Whitehorn 	 * accumulating options for each widget.
21034c8945a0SNathan Whitehorn 	 */
21044c8945a0SNathan Whitehorn 	for (j = offset + 1; j <= offset + have; j++) {
21054c8945a0SNathan Whitehorn 	    switch (lookupOption(argv[j - 1], 7)) {
21064c8945a0SNathan Whitehorn 	    case o_unknown:
21074c8945a0SNathan Whitehorn 	    case o_title:
21084c8945a0SNathan Whitehorn 	    case o_backtitle:
2109682c9e0fSNathan Whitehorn 	    case o_help_line:
2110682c9e0fSNathan Whitehorn 	    case o_help_file:
21114c8945a0SNathan Whitehorn 		break;
21124c8945a0SNathan Whitehorn 	    default:
21134c8945a0SNathan Whitehorn 		if (argv[j] != 0) {
21142a3e3873SBaptiste Daroussin 		    char *argv_j = strdup(argv[j]);
21152a3e3873SBaptiste Daroussin 		    if (argv_j != 0) {
21162a3e3873SBaptiste Daroussin 			dlg_trim_string(argv_j);
21172a3e3873SBaptiste Daroussin 			argv[j] = argv_j;
21182a3e3873SBaptiste Daroussin 		    } else {
21192a3e3873SBaptiste Daroussin 			argv[j] = strdup("?");
21202a3e3873SBaptiste Daroussin 		    }
2121f4f33ea0SBaptiste Daroussin 		    ignore_leak(argv[j]);
21224c8945a0SNathan Whitehorn 		}
21234c8945a0SNathan Whitehorn 		break;
21244c8945a0SNathan Whitehorn 	    }
21254c8945a0SNathan Whitehorn 	}
21264c8945a0SNathan Whitehorn 
2127f4f33ea0SBaptiste Daroussin 	DLG_TRACE(("# execute %s\n", argv[offset]));
21284c8945a0SNathan Whitehorn 	retval = show_result((*(modePtr->jumper)) (dialog_vars.title,
21294c8945a0SNathan Whitehorn 						   argv + offset,
21304c8945a0SNathan Whitehorn 						   &offset_add));
2131f4f33ea0SBaptiste Daroussin 	DLG_TRACE(("# widget returns %d\n", retval));
21324c8945a0SNathan Whitehorn 	offset += offset_add;
21334c8945a0SNathan Whitehorn 
21347a1c0d96SNathan Whitehorn 	if (dialog_vars.input_result != my_buffer) {
21354c8945a0SNathan Whitehorn 	    free(dialog_vars.input_result);
21367a1c0d96SNathan Whitehorn 	    dialog_vars.input_result = 0;
21377a1c0d96SNathan Whitehorn 	}
21384c8945a0SNathan Whitehorn 
21394c8945a0SNathan Whitehorn 	if (retval == DLG_EXIT_ESC) {
21404c8945a0SNathan Whitehorn 	    esc_pressed = TRUE;
21414c8945a0SNathan Whitehorn 	} else {
21424c8945a0SNathan Whitehorn 
21434c8945a0SNathan Whitehorn 	    if (dialog_vars.beep_after_signal)
21444c8945a0SNathan Whitehorn 		(void) beep();
21454c8945a0SNathan Whitehorn 
21464c8945a0SNathan Whitehorn 	    if (dialog_vars.sleep_secs)
21474c8945a0SNathan Whitehorn 		(void) napms(dialog_vars.sleep_secs * 1000);
21484c8945a0SNathan Whitehorn 
21494c8945a0SNathan Whitehorn 	    if (offset < argc) {
21504c8945a0SNathan Whitehorn 		switch (lookupOption(argv[offset], 7)) {
21514c8945a0SNathan Whitehorn 		case o_and_widget:
21524c8945a0SNathan Whitehorn 		    offset++;
21534c8945a0SNathan Whitehorn 		    break;
21544c8945a0SNathan Whitehorn 		case o_unknown:
21554c8945a0SNathan Whitehorn 		    sprintf(temp, "Expected --and-widget, not %.20s",
21564c8945a0SNathan Whitehorn 			    argv[offset]);
21574c8945a0SNathan Whitehorn 		    Usage(temp);
21584c8945a0SNathan Whitehorn 		    break;
21594c8945a0SNathan Whitehorn 		default:
21604c8945a0SNathan Whitehorn 		    /* if we got a cancel, etc., stop chaining */
21614c8945a0SNathan Whitehorn 		    if (retval != DLG_EXIT_OK)
21624c8945a0SNathan Whitehorn 			esc_pressed = TRUE;
21634c8945a0SNathan Whitehorn 		    else
21644c8945a0SNathan Whitehorn 			dialog_vars.dlg_clear_screen = TRUE;
21654c8945a0SNathan Whitehorn 		    break;
21664c8945a0SNathan Whitehorn 		}
21674c8945a0SNathan Whitehorn 	    }
21684c8945a0SNathan Whitehorn 	    if (dialog_vars.dlg_clear_screen)
21694c8945a0SNathan Whitehorn 		dlg_clear();
21704c8945a0SNathan Whitehorn 	}
21714c8945a0SNathan Whitehorn     }
21724c8945a0SNathan Whitehorn 
21734c8945a0SNathan Whitehorn     dlg_killall_bg(&retval);
21744c8945a0SNathan Whitehorn     if (dialog_state.screen_initialized) {
21754c8945a0SNathan Whitehorn 	(void) refresh();
21764c8945a0SNathan Whitehorn 	end_dialog();
21774c8945a0SNathan Whitehorn     }
2178f4f33ea0SBaptiste Daroussin     handle_leaks();
21794c8945a0SNathan Whitehorn     dlg_exit(retval);
21804c8945a0SNathan Whitehorn }
2181