2 * $Id: dialog.c,v 1.186 2011/03/02 09:58:29 tom Exp $
4 * cdialog - Display simple dialog boxes from shell scripts
6 * Copyright 2000-2010,2011 Thomas E. Dickey
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License, version 2.1
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to
19 * Free Software Foundation, Inc.
20 * 51 Franklin St., Fifth Floor
21 * Boston, MA 02110, USA.
23 * An earlier version of this program lists as authors
24 * Savio Lam (lam836@cs.cuhk.hk)
29 #include <sys/types.h>
36 #define PASSARGS t, av, offset_add
37 #define CALLARGS const char *t, char *av[], int *offset_add
38 typedef int (callerFn) (CALLARGS);
144 #ifdef HAVE_DLG_TRACE
150 * The bits in 'pass' are used to decide which options are applicable at
151 * different stages in the program:
152 * 1 flags before widgets
154 * 4 non-widget options
159 int pass; /* 1,2,4 or combination */
160 const char *help; /* NULL to suppress, non-empty to display params */
169 static bool *dialog_opts;
170 static char **dialog_argv;
172 static bool ignore_unknown = FALSE;
174 static const char *program = "dialog";
177 * The options[] table is organized this way to make it simple to maintain
178 * a sorted list of options for the help-message.
181 static const Options options[] = {
182 { "allow-close", o_allow_close, 1, NULL },
183 { "and-widget", o_and_widget, 4, NULL },
184 { "ascii-lines", o_ascii_lines, 1, "" },
185 { "aspect", o_aspect, 1, "<ratio>" },
186 { "auto-placement", o_auto_placement, 1, NULL },
187 { "backtitle", o_backtitle, 1, "<backtitle>" },
188 { "beep", o_beep, 1, NULL },
189 { "beep-after", o_beep_after, 1, NULL },
190 { "begin", o_begin, 1, "<y> <x>" },
191 { "calendar", o_calendar, 2, "<text> <height> <width> <day> <month> <year>" },
192 { "cancel-label", o_cancel_label, 1, "<str>" },
193 { "checklist", o_checklist, 2, "<text> <height> <width> <list height> <tag1> <item1> <status1>..." },
194 { "clear", o_clear, 1, "" },
195 { "colors", o_colors, 1, "" },
196 { "column-separator",o_column_separator, 1, "<str>" },
197 { "cr-wrap", o_cr_wrap, 1, "" },
198 { "create-rc", o_create_rc, 1, NULL },
199 { "date-format", o_date_format, 1, "<str>" },
200 { "default-item", o_default_item, 1, "<str>" },
201 { "defaultno", o_defaultno, 1, "" },
202 { "dselect", o_dselect, 2, "<directory> <height> <width>" },
203 { "editbox", o_editbox, 2, "<file> <height> <width>" },
204 { "exit-label", o_exit_label, 1, "<str>" },
205 { "extra-button", o_extra_button, 1, "" },
206 { "extra-label", o_extra_label, 1, "<str>" },
207 { "fb", o_fullbutton, 1, NULL },
208 { "fixed-font", o_fixed_font, 1, NULL },
209 { "form", o_form, 2, "<text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1>..." },
210 { "fselect", o_fselect, 2, "<filepath> <height> <width>" },
211 { "fullbutton", o_fullbutton, 1, NULL },
212 { "gauge", o_gauge, 2, "<text> <height> <width> [<percent>]" },
213 { "guage", o_gauge, 2, NULL },
214 { "help", o_help, 4, "" },
215 { "help-button", o_help_button, 1, "" },
216 { "help-label", o_help_label, 1, "<str>" },
217 { "help-status", o_help_status, 1, "" },
218 { "icon", o_icon, 1, NULL },
219 { "ignore", o_ignore, 1, "" },
220 { "infobox", o_infobox, 2, "<text> <height> <width>" },
221 { "input-fd", o_input_fd, 1, "<fd>" },
222 { "inputbox", o_inputbox, 2, "<text> <height> <width> [<init>]" },
223 { "inputmenu", o_inputmenu, 2, "<text> <height> <width> <menu height> <tag1> <item1>..." },
224 { "insecure", o_insecure, 1, "" },
225 { "item-help", o_item_help, 1, "" },
226 { "keep-colors", o_keep_colors, 1, NULL },
227 { "keep-tite", o_keep_tite, 1, "" },
228 { "keep-window", o_keep_window, 1, "" },
229 { "max-input", o_max_input, 1, "<n>" },
230 { "menu", o_menu, 2, "<text> <height> <width> <menu height> <tag1> <item1>..." },
231 { "mixedform", o_mixedform, 2, "<text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1> <itype>..." },
232 { "mixedgauge", o_mixedgauge, 2, "<text> <height> <width> <percent> <tag1> <item1>..." },
233 { "msgbox", o_msgbox, 2, "<text> <height> <width>" },
234 { "no-cancel", o_nocancel, 1, "" },
235 { "no-close", o_no_close, 1, NULL },
236 { "no-collapse", o_no_collapse, 1, "" },
237 { "no-cr-wrap", o_no_cr_wrap, 1, NULL },
238 { "no-kill", o_no_kill, 1, "" },
239 { "no-label", o_no_label, 1, "<str>" },
240 { "no-lines", o_no_lines, 1, "" },
241 { "no-mouse", o_no_mouse, 1, "" },
242 { "no-ok", o_nook, 1, "" },
243 { "no-shadow", o_no_shadow, 1, "" },
244 { "nocancel", o_nocancel, 1, NULL }, /* see --no-cancel */
245 { "noitem", o_noitem, 1, NULL },
246 { "nook", o_nook, 1, "" }, /* See no-ok */
247 { "ok-label", o_ok_label, 1, "<str>" },
248 { "output-fd", o_output_fd, 1, "<fd>" },
249 { "output-separator",o_output_separator, 1, "<str>" },
250 { "passwordbox", o_passwordbox, 2, "<text> <height> <width> [<init>]" },
251 { "passwordform", o_passwordform, 2, "<text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1>..." },
252 { "pause", o_pause, 2, "<text> <height> <width> <seconds>" },
253 { "prgbox", o_prgbox, 2, "<text> <command> <height> <width>" },
254 { "print-maxsize", o_print_maxsize, 1, "" },
255 { "print-size", o_print_size, 1, "" },
256 { "print-version", o_print_version, 5, "" },
257 { "programbox", o_programbox, 2, "<text> <height> <width>" },
258 { "progressbox", o_progressbox, 2, "<text> <height> <width>" },
259 { "quoted", o_quoted, 1, "" },
260 { "radiolist", o_radiolist, 2, "<text> <height> <width> <list height> <tag1> <item1> <status1>..." },
261 { "screen-center", o_screen_center, 1, NULL },
262 { "scrollbar", o_scrollbar, 1, "" },
263 { "separate-output",o_separate_output, 1, "" },
264 { "separate-widget",o_separate_widget, 1, "<str>" },
265 { "separator", o_separator, 1, NULL },
266 { "shadow", o_shadow, 1, "" },
267 { "single-quoted", o_single_quoted, 1, "" },
268 { "size-err", o_size_err, 1, "" },
269 { "sleep", o_sleep, 1, "<secs>" },
270 { "smooth", o_smooth, 1, NULL },
271 { "stderr", o_stderr, 1, "" },
272 { "stdout", o_stdout, 1, "" },
273 { "tab-correct", o_tab_correct, 1, "" },
274 { "tab-len", o_tab_len, 1, "<n>" },
275 { "tailbox", o_tailbox, 2, "<file> <height> <width>" },
276 { "tailboxbg", o_tailboxbg, 2, "<file> <height> <width>" },
277 { "textbox", o_textbox, 2, "<file> <height> <width>" },
278 { "time-format", o_time_format, 1, "<str>" },
279 { "timebox", o_timebox, 2, "<text> <height> <width> <hour> <minute> <second>" },
280 { "timeout", o_timeout, 1, "<secs>" },
281 { "title", o_title, 1, "<title>" },
282 { "trim", o_trim, 1, "" },
283 { "under-mouse", o_under_mouse, 1, NULL },
284 { "version", o_print_version, 5, "" },
285 { "visit-items", o_visit_items, 1, "" },
286 { "wmclass", o_wmclass, 1, NULL },
287 { "yes-label", o_yes_label, 1, "<str>" },
288 { "yesno", o_yesno, 2, "<text> <height> <width>" },
289 #ifdef HAVE_DLG_TRACE
290 { "trace", o_trace, 1, "<file>" },
296 * Make an array showing which argv[] entries are options. Use "--" as a
297 * special token to escape the next argument, allowing it to begin with "--".
298 * When we find a "--" argument, also remove it from argv[] and adjust argc.
299 * That appears to be an undocumented feature of the popt library.
301 * Also, if we see a "--file", expand it into the parameter list by reading the
302 * text from the given file and stripping quotes, treating whitespace outside
303 * quotes as a parameter delimiter.
305 * Finally, if we see a "--args", dump the current list of arguments to the
306 * standard error. This is used for debugging complex --file combinations.
309 unescape_argv(int *argcp, char ***argvp)
312 int limit_includes = 20 + *argcp;
313 int count_includes = 0;
314 bool changed = FALSE;
315 bool doalloc = FALSE;
318 dialog_opts = dlg_calloc(bool, (size_t) *argcp + 1);
319 assert_ptr(dialog_opts, "unescape_argv");
321 for (j = 1; j < *argcp; j++) {
322 bool escaped = FALSE;
323 if (!strcmp((*argvp)[j], "--")) {
325 changed = dlg_eat_argv(argcp, argvp, j, 1);
326 } else if (!strcmp((*argvp)[j], "--args")) {
327 fprintf(stderr, "Showing arguments at arg%d\n", j);
328 for (k = 0; k < *argcp; ++k) {
329 fprintf(stderr, " arg%d:%s\n", k, (*argvp)[k]);
331 changed = dlg_eat_argv(argcp, argvp, j, 1);
332 } else if (!strcmp((*argvp)[j], "--file")) {
333 if (++count_includes > limit_includes)
334 dlg_exiterr("Too many --file options");
336 if ((filename = (*argvp)[j + 1]) != 0) {
345 if (*filename == '&') {
346 fp = fdopen(atoi(filename + sizeof(char)), "r");
348 fp = fopen(filename, "r");
355 blob = dlg_realloc(char, length + BUFSIZ + 1, blob);
356 assert_ptr(blob, "unescape_argv");
357 bytes_read = fread(blob + length,
361 length += bytes_read;
363 dlg_exiterr("error on filehandle in unescape_argv");
364 } while (bytes_read == BUFSIZ);
369 list = dlg_string_to_argv(blob);
370 if ((added = dlg_count_argv(list)) != 0) {
372 size_t need = (size_t) (*argcp + added + 1);
374 *argvp = dlg_realloc(char *, need, *argvp);
375 assert_ptr(*argvp, "unescape_argv");
377 char **newp = dlg_malloc(char *, need);
378 assert_ptr(newp, "unescape_argv");
379 for (n = 0; n < *argcp; ++n) {
380 newp[n] = (*argvp)[n];
385 dialog_opts = dlg_realloc(bool, need, dialog_opts);
386 assert_ptr(dialog_opts, "unescape_argv");
388 for (n = *argcp; n >= j + 2; --n) {
389 (*argvp)[n + added - 2] = (*argvp)[n];
390 dialog_opts[n + added - 2] = dialog_opts[n];
392 for (n = 0; n < added; ++n) {
393 (*argvp)[n + j] = list[n];
394 dialog_opts[n + j] = FALSE;
400 dlg_exiterr("Cannot open --file %s", filename);
402 (*argvp)[*argcp] = 0;
406 dlg_exiterr("No value given for --file");
411 && !strncmp((*argvp)[j], "--", (size_t) 2)
412 && isalpha(UCH((*argvp)[j][2]))) {
413 dialog_opts[j] = TRUE;
417 /* if we didn't find any "--" tokens, there's no reason to do the table
418 * lookup in isOption()
424 dialog_argv = (*argvp);
428 * Check if the given string from main's argv is an option.
431 isOption(const char *arg)
436 if (dialog_opts != 0) {
438 for (n = 1; dialog_argv[n] != 0; ++n) {
439 if (dialog_argv[n] == arg) {
440 result = dialog_opts[n];
444 } else if (!strncmp(arg, "--", (size_t) 2) && isalpha(UCH(arg[2]))) {
452 lookupOption(const char *name, int pass)
456 if (isOption(name)) {
458 for (n = 0; n < sizeof(options) / sizeof(options[0]); n++) {
459 if ((pass & options[n].pass) != 0
460 && !strcmp(name, options[n].name)) {
461 return options[n].code;
469 Usage(const char *msg)
471 dlg_exiterr("Error: %s.\nUse --help to list options.\n\n", msg);
475 * Count arguments, stopping at the end of the argument list, or on any of our
479 arg_rest(char *argv[])
481 int i = 1; /* argv[0] points to a "--" token */
484 && (!isOption(argv[i]) || lookupOption(argv[i], 7) == o_unknown))
490 * In MultiWidget this function is needed to count how many tags
491 * a widget (menu, checklist, radiolist) has
494 howmany_tags(char *argv[], int group)
498 const char *format = "Expected %d arguments, found only %d";
501 while (argv[0] != 0) {
502 if (isOption(argv[0]))
504 if ((have = arg_rest(argv)) < group) {
505 sprintf(temp, format, group, have);
517 numeric_arg(char **av, int n)
520 int result = (int) strtol(av[n], &last, 10);
523 if (last == 0 || *last != 0) {
524 sprintf(msg, "Expected a number for token %d of %.20s", n, av[0]);
531 optional_str(char **av, int n, char *dft)
534 if (arg_rest(av) > n)
539 #if defined(HAVE_DLG_GAUGE) || defined(HAVE_XDIALOG)
541 optional_num(char **av, int n, int dft)
544 if (arg_rest(av) > n)
545 ret = numeric_arg(av, n);
551 * On AIX 4.x, we have to flush the output right away since there is a bug in
552 * the curses package which discards stdout even when we've used newterm to
553 * redirect output to /dev/tty.
564 case DLG_EXIT_ITEM_HELP:
565 if ((dialog_state.output_count > 1) && !dialog_vars.separate_output) {
566 fputs((dialog_state.separate_str
567 ? dialog_state.separate_str
568 : DEFAULT_SEPARATE_STR),
569 dialog_state.output);
572 if (dialog_vars.input_result != 0
573 && dialog_vars.input_result[0] != '\0') {
574 fputs(dialog_vars.input_result, dialog_state.output);
578 fflush(dialog_state.output);
586 * These are the widget callers.
593 return dialog_yesno(t,
600 call_msgbox(CALLARGS)
603 return dialog_msgbox(t,
606 numeric_arg(av, 3), 1);
610 call_infobox(CALLARGS)
613 return dialog_msgbox(t,
616 numeric_arg(av, 3), 0);
620 call_textbox(CALLARGS)
623 return dialog_textbox(t,
632 int tags = howmany_tags(av + 5, MENUBOX_TAGS);
633 *offset_add = 5 + tags * MENUBOX_TAGS;
635 return dialog_menu(t,
644 call_inputmenu(CALLARGS)
646 int tags = howmany_tags(av + 5, MENUBOX_TAGS);
647 bool free_extra_label = FALSE;
650 dialog_vars.input_menu = TRUE;
652 if (dialog_vars.max_input == 0)
653 dialog_vars.max_input = MAX_LEN / 2;
655 if (dialog_vars.extra_label == 0) {
656 free_extra_label = TRUE;
657 dialog_vars.extra_label = dlg_strclone(_("Rename"));
660 dialog_vars.extra_button = TRUE;
662 *offset_add = 5 + tags * MENUBOX_TAGS;
663 result = dialog_menu(t,
669 if (free_extra_label) {
670 free(dialog_vars.extra_label);
671 dialog_vars.extra_label = 0;
677 call_checklist(CALLARGS)
679 int tags = howmany_tags(av + 5, CHECKBOX_TAGS);
681 bool save_quoted = dialog_vars.quoted;
683 dialog_vars.quoted = !dialog_vars.separate_output;
684 *offset_add = 5 + tags * CHECKBOX_TAGS;
685 code = dialog_checklist(t,
690 tags, av + 5, FLAG_CHECK);
691 dialog_vars.quoted = save_quoted;
696 call_radiolist(CALLARGS)
698 int tags = howmany_tags(av + 5, CHECKBOX_TAGS);
699 *offset_add = 5 + tags * CHECKBOX_TAGS;
700 return dialog_checklist(t,
705 tags, av + 5, FLAG_RADIO);
709 call_inputbox(CALLARGS)
711 *offset_add = arg_rest(av);
712 return dialog_inputbox(t,
716 optional_str(av, 4, 0), 0);
720 call_passwordbox(CALLARGS)
722 *offset_add = arg_rest(av);
723 return dialog_inputbox(t,
727 optional_str(av, 4, 0), 1);
732 call_calendar(CALLARGS)
734 *offset_add = arg_rest(av);
735 return dialog_calendar(t,
739 optional_num(av, 4, -1),
740 optional_num(av, 5, -1),
741 optional_num(av, 6, -1));
745 call_dselect(CALLARGS)
747 *offset_add = arg_rest(av);
748 return dialog_dselect(t,
755 call_editbox(CALLARGS)
758 return dialog_editbox(t,
765 call_fselect(CALLARGS)
767 *offset_add = arg_rest(av);
768 return dialog_fselect(t,
775 call_timebox(CALLARGS)
777 *offset_add = arg_rest(av);
778 return dialog_timebox(t,
782 optional_num(av, 4, -1),
783 optional_num(av, 5, -1),
784 optional_num(av, 6, -1));
786 #endif /* HAVE_XDIALOG */
788 #ifdef HAVE_DLG_FORMBOX
792 int group = FORMBOX_TAGS;
793 int tags = howmany_tags(av + 5, group);
794 *offset_add = 5 + tags * group;
796 return dialog_form(t,
805 call_password_form(CALLARGS)
807 unsigned save = dialog_vars.formitem_type;
810 dialog_vars.formitem_type = 1;
811 result = call_form(PASSARGS);
812 dialog_vars.formitem_type = save;
816 #endif /* HAVE_DLG_FORMBOX */
818 #ifdef HAVE_DLG_MIXEDFORM
820 call_mixed_form(CALLARGS)
822 int group = MIXEDFORM_TAGS;
823 int tags = howmany_tags(av + 5, group);
824 *offset_add = 5 + tags * group;
826 return dialog_mixedform(t,
833 #endif /* HAVE_DLG_MIXEDFORM */
835 #ifdef HAVE_DLG_GAUGE
839 *offset_add = arg_rest(av);
840 return dialog_gauge(t,
844 optional_num(av, 4, 0));
850 *offset_add = arg_rest(av);
851 return dialog_pause(t,
859 #ifdef HAVE_MIXEDGAUGE
861 call_mixed_gauge(CALLARGS)
863 #define MIXEDGAUGE_BASE 5
864 int tags = howmany_tags(av + MIXEDGAUGE_BASE, MIXEDGAUGE_TAGS);
865 *offset_add = MIXEDGAUGE_BASE + tags * MIXEDGAUGE_TAGS;
866 return dialog_mixedgauge(t,
871 tags, av + MIXEDGAUGE_BASE);
876 call_prgbox(CALLARGS)
878 *offset_add = arg_rest(av);
879 /* the original version does not accept a prompt string, but for
880 * consistency we allow it.
882 return ((*offset_add == 5)
887 numeric_arg(av, 4), TRUE)
892 numeric_arg(av, 3), TRUE));
895 #ifdef HAVE_DLG_GAUGE
897 call_programbox(CALLARGS)
901 *offset_add = arg_rest(av);
902 /* this function is a compromise between --prgbox and --progressbox.
904 result = ((*offset_add == 4)
910 dialog_state.pipe_input)
916 dialog_state.pipe_input));
917 dialog_state.pipe_input = 0;
922 #ifdef HAVE_DLG_GAUGE
924 call_progressbox(CALLARGS)
926 *offset_add = arg_rest(av);
927 /* the original version does not accept a prompt string, but for
928 * consistency we allow it.
930 return ((*offset_add == 4)
931 ? dialog_progressbox(t,
935 : dialog_progressbox(t,
938 numeric_arg(av, 2)));
942 #ifdef HAVE_DLG_TAILBOX
944 call_tailbox(CALLARGS)
947 return dialog_tailbox(t,
955 call_tailboxbg(CALLARGS)
958 return dialog_tailbox(t,
966 static const Mode modes[] =
968 {o_yesno, 4, 4, call_yesno},
969 {o_msgbox, 4, 4, call_msgbox},
970 {o_infobox, 4, 4, call_infobox},
971 {o_textbox, 4, 4, call_textbox},
972 {o_menu, 7, 0, call_menu},
973 {o_inputmenu, 7, 0, call_inputmenu},
974 {o_checklist, 8, 0, call_checklist},
975 {o_radiolist, 8, 0, call_radiolist},
976 {o_inputbox, 4, 5, call_inputbox},
977 {o_passwordbox, 4, 5, call_passwordbox},
978 #ifdef HAVE_DLG_GAUGE
979 {o_gauge, 4, 5, call_gauge},
980 {o_pause, 5, 5, call_pause},
981 {o_prgbox, 4, 5, call_prgbox},
982 {o_programbox, 3, 4, call_programbox},
983 {o_progressbox, 3, 4, call_progressbox},
985 #ifdef HAVE_DLG_FORMBOX
986 {o_passwordform, 13, 0, call_password_form},
987 {o_form, 13, 0, call_form},
989 #ifdef HAVE_MIXEDGAUGE
990 {o_mixedgauge, MIXEDGAUGE_BASE, 0, call_mixed_gauge},
992 #ifdef HAVE_DLG_MIXEDFORM
993 {o_mixedform, 13, 0, call_mixed_form},
995 #ifdef HAVE_DLG_TAILBOX
996 {o_tailbox, 4, 4, call_tailbox},
997 {o_tailboxbg, 4, 4, call_tailboxbg},
1000 {o_calendar, 4, 7, call_calendar},
1001 {o_dselect, 4, 5, call_dselect},
1002 {o_editbox, 4, 4, call_editbox},
1003 {o_fselect, 4, 5, call_fselect},
1004 {o_timebox, 4, 7, call_timebox},
1010 optionString(char **argv, int *num)
1012 int next = *num + 1;
1013 char *result = argv[next];
1016 sprintf(temp, "Expected a string-parameter for %.20s", argv[*num]);
1024 optionValue(char **argv, int *num)
1026 int next = *num + 1;
1027 char *src = argv[next];
1032 result = (int) strtol(src, &tmp, 0);
1033 if (tmp == 0 || *tmp != 0)
1039 sprintf(temp, "Expected a numeric-parameter for %.20s", argv[*num]);
1047 * Print parts of a message
1050 PrintList(const char *const *list)
1052 const char *leaf = strrchr(program, '/');
1060 while (*list != 0) {
1061 fprintf(dialog_state.output, *list, n ? leaf : dialog_version());
1062 (void) fputc('\n', dialog_state.output);
1069 lookupMode(eOptions code)
1071 const Mode *modePtr = 0;
1074 for (n = 0; n < sizeof(modes) / sizeof(modes[0]); n++) {
1075 if (modes[n].code == code) {
1076 modePtr = &modes[n];
1084 compare_opts(const void *a, const void *b)
1086 Options *const *p = (Options * const *) a;
1087 Options *const *q = (Options * const *) b;
1088 return strcmp((*p)->name, (*q)->name);
1092 * Print program help-message
1097 static const char *const tbl_1[] =
1099 "cdialog (ComeOn Dialog!) version %s",
1100 "Copyright 2000-2008,2011 Thomas E. Dickey",
1101 "This is free software; see the source for copying conditions. There is NO",
1102 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.",
1104 "* Display dialog boxes from shell scripts *",
1106 "Usage: %s <options> { --and-widget <options> }",
1107 "where options are \"common\" options, followed by \"box\" options",
1111 " [--create-rc \"file\"]",
1117 "Auto-size with height and width = 0. Maximize with height and width = -1.",
1118 "Global-auto-size if also menu_height/list_height = 0.",
1121 size_t limit = sizeof(options) / sizeof(options[0]);
1123 const Options **opts;
1125 opts = dlg_calloc(const Options *, limit);
1126 assert_ptr(opts, "Help");
1127 for (j = 0; j < limit; ++j) {
1128 opts[j] = &(options[j]);
1130 qsort(opts, limit, sizeof(Options *), compare_opts);
1133 fprintf(dialog_state.output, "Common options:\n ");
1134 for (j = k = 0; j < limit; j++) {
1135 if ((opts[j]->pass & 1)
1136 && opts[j]->help != 0) {
1137 size_t len = 6 + strlen(opts[j]->name) + strlen(opts[j]->help);
1140 fprintf(dialog_state.output, "\n ");
1143 fprintf(dialog_state.output, " [--%s%s%s]", opts[j]->name,
1144 *(opts[j]->help) ? " " : "", opts[j]->help);
1147 fprintf(dialog_state.output, "\nBox options:\n");
1148 for (j = 0; j < limit; j++) {
1149 if ((opts[j]->pass & 2) != 0
1150 && opts[j]->help != 0
1151 && lookupMode(opts[j]->code))
1152 fprintf(dialog_state.output, " --%-12s %s\n", opts[j]->name,
1158 dlg_exit(DLG_EXIT_OK);
1162 * "Common" options apply to all widgets more/less. Most of the common options
1163 * set values in dialog_vars, a few set dialog_state and a couple write to the
1167 process_common_options(int argc, char **argv, int offset, bool output)
1169 #ifdef HAVE_DLG_TRACE
1174 while (offset < argc && !done) { /* Common options */
1175 switch (lookupOption(argv[offset], 1)) {
1177 dialog_vars.title = optionString(argv, &offset);
1180 dialog_vars.backtitle = optionString(argv, &offset);
1182 case o_separate_widget:
1183 dialog_state.separate_str = optionString(argv, &offset);
1185 case o_separate_output:
1186 dialog_vars.separate_output = TRUE;
1189 dialog_vars.colors = TRUE;
1192 dialog_vars.cr_wrap = TRUE;
1195 dialog_vars.nocollapse = TRUE;
1198 dialog_vars.cant_kill = TRUE;
1201 dialog_vars.nocancel = TRUE;
1204 dialog_vars.nook = TRUE;
1207 dialog_vars.quoted = TRUE;
1209 case o_single_quoted:
1210 dialog_vars.single_quoted = TRUE;
1213 dialog_vars.size_err = TRUE;
1216 dialog_vars.beep_signal = TRUE;
1219 dialog_vars.beep_after_signal = TRUE;
1222 dialog_state.use_scrollbar = TRUE;
1225 dialog_state.use_shadow = TRUE;
1228 dialog_vars.defaultno = TRUE;
1230 case o_default_item:
1231 dialog_vars.default_item = optionString(argv, &offset);
1234 dialog_vars.insecure = TRUE;
1237 dialog_vars.item_help = TRUE;
1240 dialog_vars.help_button = TRUE;
1243 dialog_vars.help_status = TRUE;
1245 case o_extra_button:
1246 dialog_vars.extra_button = TRUE;
1249 ignore_unknown = TRUE;
1252 dialog_vars.keep_window = TRUE;
1255 dialog_state.use_shadow = FALSE;
1258 dialog_vars.print_siz = TRUE;
1260 case o_print_maxsize:
1263 * If this is the last option, we do not want any error
1264 * messages - just our output. Calling end_dialog() cancels
1265 * the refresh() at the end of the program as well.
1267 if (argv[offset + 1] == 0) {
1268 ignore_unknown = TRUE;
1271 fflush(dialog_state.output);
1272 fprintf(dialog_state.output, "MaxSize: %d, %d\n", SLINES, SCOLS);
1275 case o_print_version:
1277 fprintf(stdout, "Version: %s\n", dialog_version());
1281 case o_output_separator:
1282 dialog_vars.output_separator = optionString(argv, &offset);
1284 case o_column_separator:
1285 dialog_vars.column_separator = optionString(argv, &offset);
1288 dialog_vars.tab_correct = TRUE;
1291 dialog_vars.sleep_secs = optionValue(argv, &offset);
1294 dialog_vars.timeout_secs = optionValue(argv, &offset);
1297 dialog_vars.max_input = optionValue(argv, &offset);
1300 dialog_state.tab_len = optionValue(argv, &offset);
1303 dialog_vars.trim_whitespace = TRUE;
1306 dialog_state.visit_items = TRUE;
1309 dialog_state.aspect_ratio = optionValue(argv, &offset);
1312 dialog_vars.begin_set = TRUE;
1313 dialog_vars.begin_y = optionValue(argv, &offset);
1314 dialog_vars.begin_x = optionValue(argv, &offset);
1317 dialog_vars.dlg_clear_screen = TRUE;
1320 dialog_vars.yes_label = optionString(argv, &offset);
1323 dialog_vars.no_label = optionString(argv, &offset);
1326 dialog_vars.ok_label = optionString(argv, &offset);
1328 case o_cancel_label:
1329 dialog_vars.cancel_label = optionString(argv, &offset);
1332 dialog_vars.extra_label = optionString(argv, &offset);
1335 dialog_vars.exit_label = optionString(argv, &offset);
1338 dialog_vars.help_label = optionString(argv, &offset);
1341 dialog_vars.date_format = optionString(argv, &offset);
1344 dialog_vars.time_format = optionString(argv, &offset);
1347 dialog_vars.keep_tite = TRUE;
1350 dialog_vars.ascii_lines = TRUE;
1351 dialog_vars.no_lines = FALSE;
1354 dialog_vars.no_lines = TRUE;
1355 dialog_vars.ascii_lines = FALSE;
1358 dialog_state.no_mouse = TRUE;
1364 /* options of Xdialog which we ignore */
1367 (void) optionString(argv, &offset);
1370 case o_auto_placement:
1375 case o_screen_center:
1383 default: /* no more common options */
1386 #ifdef HAVE_DLG_TRACE
1388 dlg_trace(optionString(argv, &offset));
1389 for (n = 0; argv[n] != 0; ++n) {
1390 dlg_trace_msg("argv[%d] = %s\n", n, argv[n]);
1402 * Initialize options at the start of a series of common options culminating
1406 init_result(char *buffer)
1408 static bool first = TRUE;
1409 static char **special_argv = 0;
1410 static int special_argc = 0;
1412 /* clear everything we do not save for the next widget */
1413 memset(&dialog_vars, 0, sizeof(dialog_vars));
1415 dialog_vars.input_result = buffer;
1416 dialog_vars.input_result[0] = '\0';
1419 * The first time this is called, check for common options given by an
1420 * environment variable.
1423 char *env = getenv("DIALOGOPTS");
1425 env = dlg_strclone(env);
1427 special_argv = dlg_string_to_argv(env);
1428 special_argc = dlg_count_argv(special_argv);
1431 if (special_argv != 0) {
1432 process_common_options(special_argc, special_argv, 0, FALSE);
1434 free(special_argv[0]);
1442 main(int argc, char *argv[])
1445 bool esc_pressed = FALSE;
1446 bool keep_tite = FALSE;
1449 int retval = DLG_EXIT_OK;
1452 const Mode *modePtr;
1453 char my_buffer[MAX_LEN + 1];
1455 memset(&dialog_state, 0, sizeof(dialog_state));
1456 memset(&dialog_vars, 0, sizeof(dialog_vars));
1458 #if defined(ENABLE_NLS)
1459 /* initialize locale support */
1460 setlocale(LC_ALL, "");
1461 bindtextdomain(NLS_TEXTDOMAIN, LOCALEDIR);
1462 textdomain(NLS_TEXTDOMAIN);
1463 #elif defined(HAVE_SETLOCALE)
1464 (void) setlocale(LC_ALL, "");
1467 unescape_argv(&argc, &argv);
1469 dialog_state.output = stderr;
1470 dialog_state.input = stdin;
1473 * Look for the last --stdout, --stderr or --output-fd option, and use
1474 * that. We can only write to one of them. If --stdout is used, that
1475 * can interfere with initializing the curses library, so we want to
1476 * know explicitly if it is used.
1478 while (offset < argc) {
1480 switch (lookupOption(argv[offset], 7)) {
1482 dialog_state.output = stdout;
1485 dialog_state.output = stderr;
1488 if ((j = optionValue(argv, &offset)) < 0
1489 || (dialog_state.input = fdopen(j, "r")) == 0)
1490 dlg_exiterr("Cannot open input-fd\n");
1493 if ((j = optionValue(argv, &offset)) < 0
1494 || (dialog_state.output = fdopen(j, "w")) == 0)
1495 dlg_exiterr("Cannot open output-fd\n");
1504 for (j = base; j < argc; ++j) {
1505 dialog_argv[j] = dialog_argv[j + 1 + (offset - base)];
1506 if (dialog_opts != 0)
1507 dialog_opts[j] = dialog_opts[j + 1 + (offset - base)];
1509 argc -= (1 + offset - base);
1513 init_result(my_buffer);
1515 if (argc == 2) { /* if we don't want clear screen */
1516 switch (lookupOption(argv[1], 7)) {
1517 case o_print_maxsize:
1520 fflush(dialog_state.output);
1521 fprintf(dialog_state.output, "MaxSize: %d, %d\n", SLINES, SCOLS);
1523 case o_print_version:
1524 fprintf(stdout, "Version: %s\n", dialog_version());
1535 dialog_state.output = stdout;
1546 if (lookupOption(argv[1], 7) == o_create_rc) {
1548 sprintf(temp, "Expected a filename for %.50s", argv[1]);
1551 if (dlg_parse_rc() == -1) /* Read the configuration file */
1552 dlg_exiterr("dialog: dlg_parse_rc");
1553 dlg_create_rc(argv[2]);
1558 dialog_vars.keep_tite = keep_tite; /* init_result() cleared global */
1560 init_dialog(dialog_state.input, dialog_state.output);
1562 while (offset < argc && !esc_pressed) {
1563 init_result(my_buffer);
1565 offset = process_common_options(argc, argv, offset, TRUE);
1567 if (argv[offset] == NULL) {
1570 Usage("Expected a box option");
1573 if (lookupOption(argv[offset], 2) != o_checklist
1574 && dialog_vars.separate_output) {
1575 sprintf(temp, "Expected --checklist, not %.20s", argv[offset]);
1579 if (dialog_state.aspect_ratio == 0)
1580 dialog_state.aspect_ratio = DEFAULT_ASPECT_RATIO;
1582 dlg_put_backtitle();
1584 /* use a table to look for the requested mode, to avoid code duplication */
1587 if ((code = lookupOption(argv[offset], 2)) != o_unknown)
1588 modePtr = lookupMode(code);
1590 sprintf(temp, "%s option %.20s",
1591 lookupOption(argv[offset], 7) != o_unknown
1598 have = arg_rest(&argv[offset]);
1599 if (have < modePtr->argmin) {
1600 sprintf(temp, "Expected at least %d tokens for %.20s, have %d",
1601 modePtr->argmin - 1, argv[offset],
1605 if (modePtr->argmax && have > modePtr->argmax) {
1607 "Expected no more than %d tokens for %.20s, have %d",
1608 modePtr->argmax - 1, argv[offset],
1614 * Trim whitespace from non-title option values, e.g., the ones that
1615 * will be used as captions or prompts. Do that only for the widget
1616 * we are about to process, since the "--trim" option is reset before
1617 * accumulating options for each widget.
1619 for (j = offset + 1; j <= offset + have; j++) {
1620 switch (lookupOption(argv[j - 1], 7)) {
1627 dlg_trim_string(argv[j]);
1633 retval = show_result((*(modePtr->jumper)) (dialog_vars.title,
1636 offset += offset_add;
1638 if (dialog_vars.input_result != my_buffer) {
1639 free(dialog_vars.input_result);
1640 dialog_vars.input_result = 0;
1643 if (retval == DLG_EXIT_ESC) {
1647 if (dialog_vars.beep_after_signal)
1650 if (dialog_vars.sleep_secs)
1651 (void) napms(dialog_vars.sleep_secs * 1000);
1653 if (offset < argc) {
1654 switch (lookupOption(argv[offset], 7)) {
1659 sprintf(temp, "Expected --and-widget, not %.20s",
1664 /* if we got a cancel, etc., stop chaining */
1665 if (retval != DLG_EXIT_OK)
1668 dialog_vars.dlg_clear_screen = TRUE;
1672 if (dialog_vars.dlg_clear_screen)
1677 dlg_killall_bg(&retval);
1678 if (dialog_state.screen_initialized) {