2 * $Id: dialog.c,v 1.177 2010/01/18 09:21:14 tom Exp $
4 * cdialog - Display simple dialog boxes from shell scripts
6 * Copyright 2000-2008,2010 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);
141 #ifdef HAVE_DLG_TRACE
147 * The bits in 'pass' are used to decide which options are applicable at
148 * different stages in the program:
149 * 1 flags before widgets
151 * 4 non-widget options
156 int pass; /* 1,2,4 or combination */
157 const char *help; /* NULL to suppress, non-empty to display params */
166 static bool *dialog_opts;
167 static char **dialog_argv;
169 static bool ignore_unknown = FALSE;
171 static const char *program = "dialog";
174 * The options[] table is organized this way to make it simple to maintain
175 * a sorted list of options for the help-message.
178 static const Options options[] = {
179 { "allow-close", o_allow_close, 1, NULL },
180 { "and-widget", o_and_widget, 4, NULL },
181 { "ascii-lines", o_ascii_lines, 1, "" },
182 { "aspect", o_aspect, 1, "<ratio>" },
183 { "auto-placement", o_auto_placement, 1, NULL },
184 { "backtitle", o_backtitle, 1, "<backtitle>" },
185 { "beep", o_beep, 1, NULL },
186 { "beep-after", o_beep_after, 1, NULL },
187 { "begin", o_begin, 1, "<y> <x>" },
188 { "calendar", o_calendar, 2, "<text> <height> <width> <day> <month> <year>" },
189 { "cancel-label", o_cancel_label, 1, "<str>" },
190 { "checklist", o_checklist, 2, "<text> <height> <width> <list height> <tag1> <item1> <status1>..." },
191 { "clear", o_clear, 1, "" },
192 { "colors", o_colors, 1, "" },
193 { "column-separator",o_column_separator, 1, "<str>" },
194 { "cr-wrap", o_cr_wrap, 1, "" },
195 { "create-rc", o_create_rc, 1, NULL },
196 { "date-format", o_date_format, 1, "<str>" },
197 { "default-item", o_default_item, 1, "<str>" },
198 { "defaultno", o_defaultno, 1, "" },
199 { "dselect", o_dselect, 2, "<directory> <height> <width>" },
200 { "editbox", o_editbox, 2, "<file> <height> <width>" },
201 { "exit-label", o_exit_label, 1, "<str>" },
202 { "extra-button", o_extra_button, 1, "" },
203 { "extra-label", o_extra_label, 1, "<str>" },
204 { "fb", o_fullbutton, 1, NULL },
205 { "fixed-font", o_fixed_font, 1, NULL },
206 { "form", o_form, 2, "<text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1>..." },
207 { "fselect", o_fselect, 2, "<filepath> <height> <width>" },
208 { "fullbutton", o_fullbutton, 1, NULL },
209 { "gauge", o_gauge, 2, "<text> <height> <width> [<percent>]" },
210 { "guage", o_gauge, 2, NULL },
211 { "help", o_help, 4, "" },
212 { "help-button", o_help_button, 1, "" },
213 { "help-label", o_help_label, 1, "<str>" },
214 { "help-status", o_help_status, 1, "" },
215 { "icon", o_icon, 1, NULL },
216 { "ignore", o_ignore, 1, "" },
217 { "infobox", o_infobox, 2, "<text> <height> <width>" },
218 { "input-fd", o_input_fd, 1, "<fd>" },
219 { "inputbox", o_inputbox, 2, "<text> <height> <width> [<init>]" },
220 { "inputmenu", o_inputmenu, 2, "<text> <height> <width> <menu height> <tag1> <item1>..." },
221 { "insecure", o_insecure, 1, "" },
222 { "item-help", o_item_help, 1, "" },
223 { "keep-colors", o_keep_colors, 1, NULL },
224 { "keep-tite", o_keep_tite, 1, "" },
225 { "keep-window", o_keep_window, 1, "" },
226 { "max-input", o_max_input, 1, "<n>" },
227 { "menu", o_menu, 2, "<text> <height> <width> <menu height> <tag1> <item1>..." },
228 { "mixedform", o_mixedform, 2, "<text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1> <itype>..." },
229 { "mixedgauge", o_mixedgauge, 2, "<text> <height> <width> <percent> <tag1> <item1>..." },
230 { "msgbox", o_msgbox, 2, "<text> <height> <width>" },
231 { "no-cancel", o_nocancel, 1, "" },
232 { "no-close", o_no_close, 1, NULL },
233 { "no-collapse", o_no_collapse, 1, "" },
234 { "no-cr-wrap", o_no_cr_wrap, 1, NULL },
235 { "no-kill", o_no_kill, 1, "" },
236 { "no-label", o_no_label, 1, "<str>" },
237 { "no-lines", o_no_lines, 1, "" },
238 { "no-ok", o_nook, 1, "" },
239 { "no-shadow", o_no_shadow, 1, "" },
240 { "nocancel", o_nocancel, 1, NULL }, /* see --no-cancel */
241 { "noitem", o_noitem, 1, NULL },
242 { "nook", o_nook, 1, "" }, /* See no-ok */
243 { "ok-label", o_ok_label, 1, "<str>" },
244 { "output-fd", o_output_fd, 1, "<fd>" },
245 { "output-separator",o_output_separator, 1, "<str>" },
246 { "passwordbox", o_passwordbox, 2, "<text> <height> <width> [<init>]" },
247 { "passwordform", o_passwordform, 2, "<text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1>..." },
248 { "pause", o_pause, 2, "<text> <height> <width> <seconds>" },
249 { "print-maxsize", o_print_maxsize, 1, "" },
250 { "print-size", o_print_size, 1, "" },
251 { "print-version", o_print_version, 5, "" },
252 { "progressbox", o_progressbox, 2, "<height> <width>" },
253 { "quoted", o_quoted, 1, "" },
254 { "radiolist", o_radiolist, 2, "<text> <height> <width> <list height> <tag1> <item1> <status1>..." },
255 { "screen-center", o_screen_center, 1, NULL },
256 { "scrollbar", o_scrollbar, 1, "" },
257 { "separate-output",o_separate_output, 1, "" },
258 { "separate-widget",o_separate_widget, 1, "<str>" },
259 { "separator", o_separator, 1, NULL },
260 { "shadow", o_shadow, 1, "" },
261 { "single-quoted", o_single_quoted, 1, "" },
262 { "size-err", o_size_err, 1, "" },
263 { "sleep", o_sleep, 1, "<secs>" },
264 { "smooth", o_smooth, 1, NULL },
265 { "stderr", o_stderr, 1, "" },
266 { "stdout", o_stdout, 1, "" },
267 { "tab-correct", o_tab_correct, 1, "" },
268 { "tab-len", o_tab_len, 1, "<n>" },
269 { "tailbox", o_tailbox, 2, "<file> <height> <width>" },
270 { "tailboxbg", o_tailboxbg, 2, "<file> <height> <width>" },
271 { "textbox", o_textbox, 2, "<file> <height> <width>" },
272 { "time-format", o_time_format, 1, "<str>" },
273 { "timebox", o_timebox, 2, "<text> <height> <width> <hour> <minute> <second>" },
274 { "timeout", o_timeout, 1, "<secs>" },
275 { "title", o_title, 1, "<title>" },
276 { "trim", o_trim, 1, "" },
277 { "under-mouse", o_under_mouse, 1, NULL },
278 { "version", o_print_version, 5, "" },
279 { "visit-items", o_visit_items, 1, "" },
280 { "wmclass", o_wmclass, 1, NULL },
281 { "yes-label", o_yes_label, 1, "<str>" },
282 { "yesno", o_yesno, 2, "<text> <height> <width>" },
283 #ifdef HAVE_DLG_TRACE
284 { "trace", o_trace, 1, "<file>" },
290 * Convert a string to an argv[], returning a char** index (which must be
291 * freed by the caller). The string is modified (replacing gaps between
292 * tokens with nulls).
295 string_to_argv(char *blob)
299 size_t length = strlen(blob);
302 for (pass = 0; pass < 2; ++pass) {
308 for (n = 0; n < length; ++n) {
309 if (quoted && blob[n] == '"') {
311 } else if (blob[n] == '"') {
315 result[count] = param;
319 } else if (blob[n] == '\\') {
320 if (quoted && !isspace(UCH(blob[n + 1]))) {
323 result[count] = param;
329 *param++ = blob[n + 1];
333 } else if (!quoted && isspace(UCH(blob[n]))) {
341 result[count] = param;
353 result = dlg_calloc(char *, count + 1);
354 assert_ptr(result, "string_to_argv");
356 break; /* no tokens found */
366 * Count the entries in an argv list.
369 count_argv(char **argv)
374 while (argv[result] != 0)
381 eat_argv(int *argcp, char ***argvp, int start, int count)
386 for (k = start; k <= *argcp; k++)
387 (*argvp)[k] = (*argvp)[k + count];
388 (*argvp)[*argcp] = 0;
393 * Make an array showing which argv[] entries are options. Use "--" as a
394 * special token to escape the next argument, allowing it to begin with "--".
395 * When we find a "--" argument, also remove it from argv[] and adjust argc.
396 * That appears to be an undocumented feature of the popt library.
398 * Also, if we see a "--file", expand it into the parameter list by reading the
399 * text from the given file and stripping quotes, treating whitespace outside
400 * quotes as a parameter delimiter.
402 * Finally, if we see a "--args", dump the current list of arguments to the
403 * standard error. This is used for debugging complex --file combinations.
406 unescape_argv(int *argcp, char ***argvp)
409 int limit_includes = 20 + *argcp;
410 int count_includes = 0;
411 bool changed = FALSE;
412 bool doalloc = FALSE;
415 dialog_opts = dlg_calloc(bool, (size_t) *argcp + 1);
416 assert_ptr(dialog_opts, "unescape_argv");
418 for (j = 1; j < *argcp; j++) {
419 bool escaped = FALSE;
420 if (!strcmp((*argvp)[j], "--")) {
422 changed = eat_argv(argcp, argvp, j, 1);
423 } else if (!strcmp((*argvp)[j], "--args")) {
424 fprintf(stderr, "Showing arguments at arg%d\n", j);
425 for (k = 0; k < *argcp; ++k) {
426 fprintf(stderr, " arg%d:%s\n", k, (*argvp)[k]);
428 changed = eat_argv(argcp, argvp, j, 1);
429 } else if (!strcmp((*argvp)[j], "--file")) {
430 if (++count_includes > limit_includes)
431 dlg_exiterr("Too many --file options");
433 if ((filename = (*argvp)[j + 1]) != 0) {
442 if (*filename == '&') {
443 fp = fdopen(atoi(filename + sizeof(char)), "r");
445 fp = fopen(filename, "r");
452 blob = dlg_realloc(char, length + BUFSIZ + 1, blob);
453 assert_ptr(blob, "unescape_argv");
454 bytes_read = fread(blob + length,
458 length += bytes_read;
460 dlg_exiterr("error on filehandle in unescape_argv");
461 } while (bytes_read == BUFSIZ);
466 list = string_to_argv(blob);
467 if ((added = count_argv(list)) != 0) {
469 size_t need = (size_t) (*argcp + added + 1);
471 *argvp = dlg_realloc(char *, need, *argvp);
472 assert_ptr(*argvp, "unescape_argv");
474 char **newp = dlg_malloc(char *, need);
475 assert_ptr(newp, "unescape_argv");
476 for (n = 0; n < *argcp; ++n) {
477 newp[n] = (*argvp)[n];
482 dialog_opts = dlg_realloc(bool, need, dialog_opts);
483 assert_ptr(dialog_opts, "unescape_argv");
485 for (n = *argcp; n >= j + 2; --n) {
486 (*argvp)[n + added - 2] = (*argvp)[n];
487 dialog_opts[n + added - 2] = dialog_opts[n];
489 for (n = 0; n < added; ++n) {
490 (*argvp)[n + j] = list[n];
491 dialog_opts[n + j] = FALSE;
497 dlg_exiterr("Cannot open --file %s", filename);
499 (*argvp)[*argcp] = 0;
503 dlg_exiterr("No value given for --file");
508 && !strncmp((*argvp)[j], "--", 2)
509 && isalpha(UCH((*argvp)[j][2]))) {
510 dialog_opts[j] = TRUE;
514 /* if we didn't find any "--" tokens, there's no reason to do the table
515 * lookup in isOption()
521 dialog_argv = (*argvp);
525 * Check if the given string from main's argv is an option.
528 isOption(const char *arg)
533 if (dialog_opts != 0) {
535 for (n = 1; dialog_argv[n] != 0; ++n) {
536 if (dialog_argv[n] == arg) {
537 result = dialog_opts[n];
541 } else if (!strncmp(arg, "--", 2) && isalpha(UCH(arg[2]))) {
549 lookupOption(const char *name, int pass)
553 if (isOption(name)) {
555 for (n = 0; n < sizeof(options) / sizeof(options[0]); n++) {
556 if ((pass & options[n].pass) != 0
557 && !strcmp(name, options[n].name)) {
558 return options[n].code;
566 Usage(const char *msg)
568 dlg_exiterr("Error: %s.\nUse --help to list options.\n\n", msg);
572 * Count arguments, stopping at the end of the argument list, or on any of our
576 arg_rest(char *argv[])
578 int i = 1; /* argv[0] points to a "--" token */
581 && (!isOption(argv[i]) || lookupOption(argv[i], 7) == o_unknown))
587 * In MultiWidget this function is needed to count how many tags
588 * a widget (menu, checklist, radiolist) has
591 howmany_tags(char *argv[], int group)
595 const char *format = "Expected %d arguments, found only %d";
598 while (argv[0] != 0) {
599 if (isOption(argv[0]))
601 if ((have = arg_rest(argv)) < group) {
602 sprintf(temp, format, group, have);
614 numeric_arg(char **av, int n)
617 int result = strtol(av[n], &last, 10);
620 if (last == 0 || *last != 0) {
621 sprintf(msg, "Expected a number for token %d of %.20s", n, av[0]);
628 optional_str(char **av, int n, char *dft)
631 if (arg_rest(av) > n)
636 #if defined(HAVE_DLG_GAUGE) || defined(HAVE_XDIALOG)
638 optional_num(char **av, int n, int dft)
641 if (arg_rest(av) > n)
642 ret = numeric_arg(av, n);
648 * On AIX 4.x, we have to flush the output right away since there is a bug in
649 * the curses package which discards stdout even when we've used newterm to
650 * redirect output to /dev/tty.
661 case DLG_EXIT_ITEM_HELP:
662 if ((dialog_state.output_count > 1) && !dialog_vars.separate_output) {
663 fputs((dialog_state.separate_str
664 ? dialog_state.separate_str
665 : DEFAULT_SEPARATE_STR),
666 dialog_state.output);
669 if (dialog_vars.input_result[0] != '\0') {
670 fputs(dialog_vars.input_result, dialog_state.output);
674 fflush(dialog_state.output);
682 * These are the widget callers.
689 return dialog_yesno(t,
696 call_msgbox(CALLARGS)
699 return dialog_msgbox(t,
702 numeric_arg(av, 3), 1);
706 call_infobox(CALLARGS)
709 return dialog_msgbox(t,
712 numeric_arg(av, 3), 0);
716 call_textbox(CALLARGS)
719 return dialog_textbox(t,
728 int tags = howmany_tags(av + 5, MENUBOX_TAGS);
729 *offset_add = 5 + tags * MENUBOX_TAGS;
731 return dialog_menu(t,
740 call_inputmenu(CALLARGS)
742 int tags = howmany_tags(av + 5, MENUBOX_TAGS);
744 dialog_vars.input_menu = TRUE;
746 if (dialog_vars.max_input == 0)
747 dialog_vars.max_input = MAX_LEN / 2;
749 if (dialog_vars.extra_label == 0)
750 dialog_vars.extra_label = _("Rename");
752 dialog_vars.extra_button = TRUE;
754 *offset_add = 5 + tags * MENUBOX_TAGS;
755 return dialog_menu(t,
764 call_checklist(CALLARGS)
766 int tags = howmany_tags(av + 5, CHECKBOX_TAGS);
768 bool save_quoted = dialog_vars.quoted;
770 dialog_vars.quoted = !dialog_vars.separate_output;
771 *offset_add = 5 + tags * CHECKBOX_TAGS;
772 code = dialog_checklist(t,
777 tags, av + 5, FLAG_CHECK);
778 dialog_vars.quoted = save_quoted;
783 call_radiolist(CALLARGS)
785 int tags = howmany_tags(av + 5, CHECKBOX_TAGS);
786 *offset_add = 5 + tags * CHECKBOX_TAGS;
787 return dialog_checklist(t,
792 tags, av + 5, FLAG_RADIO);
796 call_inputbox(CALLARGS)
798 *offset_add = arg_rest(av);
799 return dialog_inputbox(t,
803 optional_str(av, 4, 0), 0);
807 call_passwordbox(CALLARGS)
809 *offset_add = arg_rest(av);
810 return dialog_inputbox(t,
814 optional_str(av, 4, 0), 1);
819 call_calendar(CALLARGS)
821 *offset_add = arg_rest(av);
822 return dialog_calendar(t,
826 optional_num(av, 4, -1),
827 optional_num(av, 5, -1),
828 optional_num(av, 6, -1));
832 call_dselect(CALLARGS)
834 *offset_add = arg_rest(av);
835 return dialog_dselect(t,
842 call_editbox(CALLARGS)
845 return dialog_editbox(t,
852 call_fselect(CALLARGS)
854 *offset_add = arg_rest(av);
855 return dialog_fselect(t,
862 call_timebox(CALLARGS)
864 *offset_add = arg_rest(av);
865 return dialog_timebox(t,
869 optional_num(av, 4, -1),
870 optional_num(av, 5, -1),
871 optional_num(av, 6, -1));
873 #endif /* HAVE_XDIALOG */
875 #ifdef HAVE_DLG_FORMBOX
879 int group = FORMBOX_TAGS;
880 int tags = howmany_tags(av + 5, group);
881 *offset_add = 5 + tags * group;
883 return dialog_form(t,
892 call_password_form(CALLARGS)
894 unsigned save = dialog_vars.formitem_type;
897 dialog_vars.formitem_type = 1;
898 result = call_form(PASSARGS);
899 dialog_vars.formitem_type = save;
903 #endif /* HAVE_DLG_FORMBOX */
905 #ifdef HAVE_DLG_MIXEDFORM
907 call_mixed_form(CALLARGS)
909 int group = MIXEDFORM_TAGS;
910 int tags = howmany_tags(av + 5, group);
911 *offset_add = 5 + tags * group;
913 return dialog_mixedform(t,
920 #endif /* HAVE_DLG_MIXEDFORM */
922 #ifdef HAVE_DLG_GAUGE
926 *offset_add = arg_rest(av);
927 return dialog_gauge(t,
931 optional_num(av, 4, 0));
937 *offset_add = arg_rest(av);
938 return dialog_pause(t,
946 #ifdef HAVE_MIXEDGAUGE
948 call_mixed_gauge(CALLARGS)
950 #define MIXEDGAUGE_BASE 5
951 int tags = howmany_tags(av + MIXEDGAUGE_BASE, MIXEDGAUGE_TAGS);
952 *offset_add = MIXEDGAUGE_BASE + tags * MIXEDGAUGE_TAGS;
953 return dialog_mixedgauge(t,
958 tags, av + MIXEDGAUGE_BASE);
962 #ifdef HAVE_DLG_GAUGE
964 call_progressbox(CALLARGS)
966 *offset_add = arg_rest(av);
967 /* the original version does not accept a prompt string, but for
968 * consistency we allow it.
970 return ((*offset_add == 4)
971 ? dialog_progressbox(t,
975 : dialog_progressbox(t,
978 numeric_arg(av, 2)));
982 #ifdef HAVE_DLG_TAILBOX
984 call_tailbox(CALLARGS)
987 return dialog_tailbox(t,
995 call_tailboxbg(CALLARGS)
998 return dialog_tailbox(t,
1006 static const Mode modes[] =
1008 {o_yesno, 4, 4, call_yesno},
1009 {o_msgbox, 4, 4, call_msgbox},
1010 {o_infobox, 4, 4, call_infobox},
1011 {o_textbox, 4, 4, call_textbox},
1012 {o_menu, 7, 0, call_menu},
1013 {o_inputmenu, 7, 0, call_inputmenu},
1014 {o_checklist, 8, 0, call_checklist},
1015 {o_radiolist, 8, 0, call_radiolist},
1016 {o_inputbox, 4, 5, call_inputbox},
1017 {o_passwordbox, 4, 5, call_passwordbox},
1018 #ifdef HAVE_DLG_GAUGE
1019 {o_gauge, 4, 5, call_gauge},
1020 {o_pause, 5, 5, call_pause},
1021 {o_progressbox, 3, 4, call_progressbox},
1023 #ifdef HAVE_DLG_FORMBOX
1024 {o_passwordform, 13, 0, call_password_form},
1025 {o_form, 13, 0, call_form},
1027 #ifdef HAVE_MIXEDGAUGE
1028 {o_mixedgauge, MIXEDGAUGE_BASE, 0, call_mixed_gauge},
1030 #ifdef HAVE_DLG_MIXEDFORM
1031 {o_mixedform, 13, 0, call_mixed_form},
1033 #ifdef HAVE_DLG_TAILBOX
1034 {o_tailbox, 4, 4, call_tailbox},
1035 {o_tailboxbg, 4, 4, call_tailboxbg},
1038 {o_calendar, 4, 7, call_calendar},
1039 {o_dselect, 4, 5, call_dselect},
1040 {o_editbox, 4, 4, call_editbox},
1041 {o_fselect, 4, 5, call_fselect},
1042 {o_timebox, 4, 7, call_timebox},
1048 optionString(char **argv, int *num)
1050 int next = *num + 1;
1051 char *result = argv[next];
1054 sprintf(temp, "Expected a string-parameter for %.20s", argv[*num]);
1062 optionValue(char **argv, int *num)
1064 int next = *num + 1;
1065 char *src = argv[next];
1070 result = strtol(src, &tmp, 0);
1071 if (tmp == 0 || *tmp != 0)
1077 sprintf(temp, "Expected a numeric-parameter for %.20s", argv[*num]);
1085 * Print parts of a message
1088 PrintList(const char *const *list)
1090 const char *leaf = strrchr(program, '/');
1098 while (*list != 0) {
1099 fprintf(dialog_state.output, *list, n ? leaf : dialog_version());
1100 (void) fputc('\n', dialog_state.output);
1107 lookupMode(eOptions code)
1109 const Mode *modePtr = 0;
1112 for (n = 0; n < sizeof(modes) / sizeof(modes[0]); n++) {
1113 if (modes[n].code == code) {
1114 modePtr = &modes[n];
1122 compare_opts(const void *a, const void *b)
1124 Options *const *p = (Options * const *) a;
1125 Options *const *q = (Options * const *) b;
1126 return strcmp((*p)->name, (*q)->name);
1130 * Print program help-message
1135 static const char *const tbl_1[] =
1137 "cdialog (ComeOn Dialog!) version %s",
1138 "Copyright 2000-2007,2008 Thomas E. Dickey",
1139 "This is free software; see the source for copying conditions. There is NO",
1140 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.",
1142 "* Display dialog boxes from shell scripts *",
1144 "Usage: %s <options> { --and-widget <options> }",
1145 "where options are \"common\" options, followed by \"box\" options",
1149 " [--create-rc \"file\"]",
1155 "Auto-size with height and width = 0. Maximize with height and width = -1.",
1156 "Global-auto-size if also menu_height/list_height = 0.",
1159 unsigned limit = sizeof(options) / sizeof(options[0]);
1161 const Options **opts;
1163 opts = dlg_calloc(const Options *, limit);
1164 assert_ptr(opts, "Help");
1165 for (j = 0; j < limit; ++j) {
1166 opts[j] = &(options[j]);
1168 qsort(opts, limit, sizeof(Options *), compare_opts);
1171 fprintf(dialog_state.output, "Common options:\n ");
1172 for (j = k = 0; j < limit; j++) {
1173 if ((opts[j]->pass & 1)
1174 && opts[j]->help != 0) {
1175 unsigned len = 6 + strlen(opts[j]->name) + strlen(opts[j]->help);
1178 fprintf(dialog_state.output, "\n ");
1181 fprintf(dialog_state.output, " [--%s%s%s]", opts[j]->name,
1182 *(opts[j]->help) ? " " : "", opts[j]->help);
1185 fprintf(dialog_state.output, "\nBox options:\n");
1186 for (j = 0; j < limit; j++) {
1187 if ((opts[j]->pass & 2) != 0
1188 && opts[j]->help != 0
1189 && lookupMode(opts[j]->code))
1190 fprintf(dialog_state.output, " --%-12s %s\n", opts[j]->name,
1196 dlg_exit(DLG_EXIT_OK);
1200 * "Common" options apply to all widgets more/less. Most of the common options
1201 * set values in dialog_vars, a few set dialog_state and a couple write to the
1205 process_common_options(int argc, char **argv, int offset, bool output)
1209 while (offset < argc && !done) { /* Common options */
1210 switch (lookupOption(argv[offset], 1)) {
1212 dialog_vars.title = optionString(argv, &offset);
1215 dialog_vars.backtitle = optionString(argv, &offset);
1217 case o_separate_widget:
1218 dialog_state.separate_str = optionString(argv, &offset);
1220 case o_separate_output:
1221 dialog_vars.separate_output = TRUE;
1224 dialog_vars.colors = TRUE;
1227 dialog_vars.cr_wrap = TRUE;
1230 dialog_vars.nocollapse = TRUE;
1233 dialog_vars.cant_kill = TRUE;
1236 dialog_vars.nocancel = TRUE;
1239 dialog_vars.nook = TRUE;
1242 dialog_vars.quoted = TRUE;
1244 case o_single_quoted:
1245 dialog_vars.single_quoted = TRUE;
1248 dialog_vars.size_err = TRUE;
1251 dialog_vars.beep_signal = TRUE;
1254 dialog_vars.beep_after_signal = TRUE;
1257 dialog_state.use_scrollbar = TRUE;
1260 dialog_state.use_shadow = TRUE;
1263 dialog_vars.defaultno = TRUE;
1265 case o_default_item:
1266 dialog_vars.default_item = optionString(argv, &offset);
1269 dialog_vars.insecure = TRUE;
1272 dialog_vars.item_help = TRUE;
1275 dialog_vars.help_button = TRUE;
1278 dialog_vars.help_status = TRUE;
1280 case o_extra_button:
1281 dialog_vars.extra_button = TRUE;
1284 ignore_unknown = TRUE;
1287 dialog_vars.keep_window = TRUE;
1290 dialog_state.use_shadow = FALSE;
1293 dialog_vars.print_siz = TRUE;
1295 case o_print_maxsize:
1298 * If this is the last option, we do not want any error
1299 * messages - just our output. Calling end_dialog() cancels
1300 * the refresh() at the end of the program as well.
1302 if (argv[offset + 1] == 0) {
1303 ignore_unknown = TRUE;
1306 fflush(dialog_state.output);
1307 fprintf(dialog_state.output, "MaxSize: %d, %d\n", SLINES, SCOLS);
1310 case o_print_version:
1312 fprintf(stdout, "Version: %s\n", dialog_version());
1316 case o_output_separator:
1317 dialog_vars.output_separator = optionString(argv, &offset);
1319 case o_column_separator:
1320 dialog_vars.column_separator = optionString(argv, &offset);
1323 dialog_vars.tab_correct = TRUE;
1326 dialog_vars.sleep_secs = optionValue(argv, &offset);
1329 dialog_vars.timeout_secs = optionValue(argv, &offset);
1332 dialog_vars.max_input = optionValue(argv, &offset);
1335 dialog_state.tab_len = optionValue(argv, &offset);
1338 dialog_vars.trim_whitespace = TRUE;
1341 dialog_state.visit_items = TRUE;
1344 dialog_state.aspect_ratio = optionValue(argv, &offset);
1347 dialog_vars.begin_set = TRUE;
1348 dialog_vars.begin_y = optionValue(argv, &offset);
1349 dialog_vars.begin_x = optionValue(argv, &offset);
1352 dialog_vars.dlg_clear_screen = TRUE;
1355 dialog_vars.yes_label = optionString(argv, &offset);
1358 dialog_vars.no_label = optionString(argv, &offset);
1361 dialog_vars.ok_label = optionString(argv, &offset);
1363 case o_cancel_label:
1364 dialog_vars.cancel_label = optionString(argv, &offset);
1367 dialog_vars.extra_label = optionString(argv, &offset);
1370 dialog_vars.exit_label = optionString(argv, &offset);
1373 dialog_vars.help_label = optionString(argv, &offset);
1376 dialog_vars.date_format = optionString(argv, &offset);
1379 dialog_vars.time_format = optionString(argv, &offset);
1382 dialog_vars.keep_tite = TRUE;
1385 dialog_vars.ascii_lines = TRUE;
1386 dialog_vars.no_lines = FALSE;
1389 dialog_vars.no_lines = TRUE;
1390 dialog_vars.ascii_lines = FALSE;
1396 /* options of Xdialog which we ignore */
1399 (void) optionString(argv, &offset);
1402 case o_auto_placement:
1407 case o_screen_center:
1415 default: /* no more common options */
1418 #ifdef HAVE_DLG_TRACE
1420 dlg_trace(optionString(argv, &offset));
1431 * Initialize options at the start of a series of common options culminating
1435 init_result(char *buffer)
1437 static bool first = TRUE;
1438 static char **special_argv = 0;
1439 static int special_argc = 0;
1441 /* clear everything we do not save for the next widget */
1442 memset(&dialog_vars, 0, sizeof(dialog_vars));
1444 dialog_vars.input_result = buffer;
1445 dialog_vars.input_result[0] = '\0';
1448 * The first time this is called, check for common options given by an
1449 * environment variable.
1452 char *env = getenv("DIALOGOPTS");
1454 env = dlg_strclone(env);
1456 special_argv = string_to_argv(env);
1457 special_argc = count_argv(special_argv);
1460 if (special_argv != 0) {
1461 process_common_options(special_argc, special_argv, 0, FALSE);
1463 free(special_argv[0]);
1471 main(int argc, char *argv[])
1474 bool esc_pressed = FALSE;
1475 bool keep_tite = FALSE;
1478 int retval = DLG_EXIT_OK;
1481 const Mode *modePtr;
1482 char my_buffer[MAX_LEN + 1];
1484 memset(&dialog_state, 0, sizeof(dialog_state));
1485 memset(&dialog_vars, 0, sizeof(dialog_vars));
1487 #if defined(ENABLE_NLS)
1488 /* initialize locale support */
1489 setlocale(LC_ALL, "");
1490 bindtextdomain(PACKAGE, LOCALEDIR);
1491 textdomain(PACKAGE);
1492 #elif defined(HAVE_SETLOCALE)
1493 (void) setlocale(LC_ALL, "");
1496 unescape_argv(&argc, &argv);
1498 dialog_state.output = stderr;
1499 dialog_state.input = stdin;
1502 * Look for the last --stdout, --stderr or --output-fd option, and use
1503 * that. We can only write to one of them. If --stdout is used, that
1504 * can interfere with initializing the curses library, so we want to
1505 * know explicitly if it is used.
1507 while (offset < argc) {
1509 switch (lookupOption(argv[offset], 7)) {
1511 dialog_state.output = stdout;
1514 dialog_state.output = stderr;
1517 if ((j = optionValue(argv, &offset)) < 0
1518 || (dialog_state.input = fdopen(j, "r")) == 0)
1519 dlg_exiterr("Cannot open input-fd\n");
1522 if ((j = optionValue(argv, &offset)) < 0
1523 || (dialog_state.output = fdopen(j, "w")) == 0)
1524 dlg_exiterr("Cannot open output-fd\n");
1533 for (j = base; j < argc; ++j) {
1534 dialog_argv[j] = dialog_argv[j + 1 + (offset - base)];
1535 if (dialog_opts != 0)
1536 dialog_opts[j] = dialog_opts[j + 1 + (offset - base)];
1538 argc -= (1 + offset - base);
1542 init_result(my_buffer);
1544 if (argc == 2) { /* if we don't want clear screen */
1545 switch (lookupOption(argv[1], 7)) {
1546 case o_print_maxsize:
1549 fflush(dialog_state.output);
1550 fprintf(dialog_state.output, "MaxSize: %d, %d\n", SLINES, SCOLS);
1552 case o_print_version:
1553 fprintf(stdout, "Version: %s\n", dialog_version());
1564 dialog_state.output = stdout;
1575 if (lookupOption(argv[1], 7) == o_create_rc) {
1577 sprintf(temp, "Expected a filename for %.50s", argv[1]);
1580 if (dlg_parse_rc() == -1) /* Read the configuration file */
1581 dlg_exiterr("dialog: dlg_parse_rc");
1582 dlg_create_rc(argv[2]);
1587 dialog_vars.keep_tite = keep_tite; /* init_result() cleared global */
1589 init_dialog(dialog_state.input, dialog_state.output);
1591 while (offset < argc && !esc_pressed) {
1592 init_result(my_buffer);
1594 offset = process_common_options(argc, argv, offset, TRUE);
1596 if (argv[offset] == NULL) {
1599 Usage("Expected a box option");
1602 if (lookupOption(argv[offset], 2) != o_checklist
1603 && dialog_vars.separate_output) {
1604 sprintf(temp, "Expected --checklist, not %.20s", argv[offset]);
1608 if (dialog_state.aspect_ratio == 0)
1609 dialog_state.aspect_ratio = DEFAULT_ASPECT_RATIO;
1611 dlg_put_backtitle();
1613 /* use a table to look for the requested mode, to avoid code duplication */
1616 if ((code = lookupOption(argv[offset], 2)) != o_unknown)
1617 modePtr = lookupMode(code);
1619 sprintf(temp, "%s option %.20s",
1620 lookupOption(argv[offset], 7) != o_unknown
1627 have = arg_rest(&argv[offset]);
1628 if (have < modePtr->argmin) {
1629 sprintf(temp, "Expected at least %d tokens for %.20s, have %d",
1630 modePtr->argmin - 1, argv[offset],
1634 if (modePtr->argmax && have > modePtr->argmax) {
1636 "Expected no more than %d tokens for %.20s, have %d",
1637 modePtr->argmax - 1, argv[offset],
1643 * Trim whitespace from non-title option values, e.g., the ones that
1644 * will be used as captions or prompts. Do that only for the widget
1645 * we are about to process, since the "--trim" option is reset before
1646 * accumulating options for each widget.
1648 for (j = offset + 1; j <= offset + have; j++) {
1649 switch (lookupOption(argv[j - 1], 7)) {
1656 dlg_trim_string(argv[j]);
1662 retval = show_result((*(modePtr->jumper)) (dialog_vars.title,
1665 offset += offset_add;
1667 if (dialog_vars.input_result != my_buffer)
1668 free(dialog_vars.input_result);
1670 if (retval == DLG_EXIT_ESC) {
1674 if (dialog_vars.beep_after_signal)
1677 if (dialog_vars.sleep_secs)
1678 (void) napms(dialog_vars.sleep_secs * 1000);
1680 if (offset < argc) {
1681 switch (lookupOption(argv[offset], 7)) {
1686 sprintf(temp, "Expected --and-widget, not %.20s",
1691 /* if we got a cancel, etc., stop chaining */
1692 if (retval != DLG_EXIT_OK)
1695 dialog_vars.dlg_clear_screen = TRUE;
1699 if (dialog_vars.dlg_clear_screen)
1704 dlg_killall_bg(&retval);
1705 if (dialog_state.screen_initialized) {