2 * $Id: dialog.c,v 1.228 2012/12/30 21:59:39 tom Exp $
4 * cdialog - Display simple dialog boxes from shell scripts
6 * Copyright 2000-2011,2012 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);
158 #if defined(HAVE_XDIALOG2) || defined(HAVE_WHIPTAIL)
162 #ifdef HAVE_DLG_TRACE
168 * The bits in 'pass' are used to decide which options are applicable at
169 * different stages in the program:
170 * 1 flags before widgets
172 * 4 non-widget options
177 int pass; /* 1,2,4 or combination */
178 const char *help; /* NULL to suppress, non-empty to display params */
187 static bool *dialog_opts;
188 static char **dialog_argv;
190 static bool ignore_unknown = FALSE;
192 static const char *program = "dialog";
195 * The options[] table is organized this way to make it simple to maintain
196 * a sorted list of options for the help-message.
199 static const Options options[] = {
200 { "allow-close", o_allow_close, 1, NULL },
201 { "and-widget", o_and_widget, 4, NULL },
202 { "ascii-lines", o_ascii_lines, 1, "" },
203 { "aspect", o_aspect, 1, "<ratio>" },
204 { "auto-placement", o_auto_placement, 1, NULL },
205 { "backtitle", o_backtitle, 1, "<backtitle>" },
206 { "beep", o_beep, 1, "" },
207 { "beep-after", o_beep_after, 1, "" },
208 { "begin", o_begin, 1, "<y> <x>" },
209 { "cancel-label", o_cancel_label, 1, "<str>" },
210 { "checklist", o_checklist, 2, "<text> <height> <width> <list height> <tag1> <item1> <status1>..." },
211 { "clear", o_clear, 1, "" },
212 { "colors", o_colors, 1, "" },
213 { "column-separator",o_column_separator, 1, "<str>" },
214 { "cr-wrap", o_cr_wrap, 1, "" },
215 { "create-rc", o_create_rc, 1, NULL },
216 { "date-format", o_date_format, 1, "<str>" },
217 { "default-button", o_default_button, 1, "<str>" },
218 { "default-item", o_default_item, 1, "<str>" },
219 { "defaultno", o_defaultno, 1, "" },
220 { "exit-label", o_exit_label, 1, "<str>" },
221 { "extra-button", o_extra_button, 1, "" },
222 { "extra-label", o_extra_label, 1, "<str>" },
223 { "fixed-font", o_fixed_font, 1, NULL },
224 { "form", o_form, 2, "<text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1>..." },
225 { "gauge", o_gauge, 2, "<text> <height> <width> [<percent>]" },
226 { "guage", o_gauge, 2, NULL },
227 { "help", o_help, 4, "" },
228 { "help-button", o_help_button, 1, "" },
229 { "help-label", o_help_label, 1, "<str>" },
230 { "help-status", o_help_status, 1, "" },
231 { "hfile", o_help_file, 1, "<str>" },
232 { "hline", o_help_line, 1, "<str>" },
233 { "icon", o_icon, 1, NULL },
234 { "ignore", o_ignore, 1, "" },
235 { "infobox", o_infobox, 2, "<text> <height> <width>" },
236 { "input-fd", o_input_fd, 1, "<fd>" },
237 { "inputbox", o_inputbox, 2, "<text> <height> <width> [<init>]" },
238 { "inputmenu", o_inputmenu, 2, "<text> <height> <width> <menu height> <tag1> <item1>..." },
239 { "insecure", o_insecure, 1, "" },
240 { "item-help", o_item_help, 1, "" },
241 { "keep-colors", o_keep_colors, 1, NULL },
242 { "keep-tite", o_keep_tite, 1, "" },
243 { "keep-window", o_keep_window, 1, "" },
244 { "max-input", o_max_input, 1, "<n>" },
245 { "menu", o_menu, 2, "<text> <height> <width> <menu height> <tag1> <item1>..." },
246 { "mixedform", o_mixedform, 2, "<text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1> <itype>..." },
247 { "mixedgauge", o_mixedgauge, 2, "<text> <height> <width> <percent> <tag1> <item1>..." },
248 { "msgbox", o_msgbox, 2, "<text> <height> <width>" },
249 { "no-cancel", o_nocancel, 1, "" },
250 { "no-close", o_no_close, 1, NULL },
251 { "no-collapse", o_no_collapse, 1, "" },
252 { "no-cr-wrap", o_no_cr_wrap, 1, "" },
253 { "no-kill", o_no_kill, 1, "" },
254 { "no-label", o_no_label, 1, "<str>" },
255 { "no-lines", o_no_lines, 1, "" },
256 { "no-mouse", o_no_mouse, 1, "" },
257 { "no-nl-expand", o_no_nl_expand, 1, "" },
258 { "no-ok", o_nook, 1, "" },
259 { "no-shadow", o_no_shadow, 1, "" },
260 { "nocancel", o_nocancel, 1, NULL }, /* see --no-cancel */
261 { "nook", o_nook, 1, "" }, /* See no-ok */
262 { "ok-label", o_ok_label, 1, "<str>" },
263 { "output-fd", o_output_fd, 1, "<fd>" },
264 { "output-separator",o_output_separator, 1, "<str>" },
265 { "passwordbox", o_passwordbox, 2, "<text> <height> <width> [<init>]" },
266 { "passwordform", o_passwordform, 2, "<text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1>..." },
267 { "pause", o_pause, 2, "<text> <height> <width> <seconds>" },
268 { "prgbox", o_prgbox, 2, "<text> <command> <height> <width>" },
269 { "print-maxsize", o_print_maxsize, 1, "" },
270 { "print-size", o_print_size, 1, "" },
271 { "print-version", o_print_version, 5, "" },
272 { "programbox", o_programbox, 2, "<text> <height> <width>" },
273 { "progressbox", o_progressbox, 2, "<text> <height> <width>" },
274 { "quoted", o_quoted, 1, "" },
275 { "radiolist", o_radiolist, 2, "<text> <height> <width> <list height> <tag1> <item1> <status1>..." },
276 { "screen-center", o_screen_center, 1, NULL },
277 { "scrollbar", o_scrollbar, 1, "" },
278 { "separate-output",o_separate_output, 1, "" },
279 { "separate-widget",o_separate_widget, 1, "<str>" },
280 { "separator", o_separator, 1, NULL },
281 { "shadow", o_shadow, 1, "" },
282 { "single-quoted", o_single_quoted, 1, "" },
283 { "size-err", o_size_err, 1, "" },
284 { "sleep", o_sleep, 1, "<secs>" },
285 { "smooth", o_smooth, 1, NULL },
286 { "stderr", o_stderr, 1, "" },
287 { "stdout", o_stdout, 1, "" },
288 { "tab-correct", o_tab_correct, 1, "" },
289 { "tab-len", o_tab_len, 1, "<n>" },
290 { "tailbox", o_tailbox, 2, "<file> <height> <width>" },
291 { "tailboxbg", o_tailboxbg, 2, "<file> <height> <width>" },
292 { "textbox", o_textbox, 2, "<file> <height> <width>" },
293 { "time-format", o_time_format, 1, "<str>" },
294 { "timeout", o_timeout, 1, "<secs>" },
295 { "title", o_title, 1, "<title>" },
296 { "trim", o_trim, 1, "" },
297 { "under-mouse", o_under_mouse, 1, NULL },
298 { "version", o_version, 5, "" },
299 { "visit-items", o_visit_items, 1, "" },
300 { "wmclass", o_wmclass, 1, NULL },
301 { "yes-label", o_yes_label, 1, "<str>" },
302 { "yesno", o_yesno, 2, "<text> <height> <width>" },
304 { "cancel-button", o_cancel_label, 1, NULL },
305 { "fb", o_fullbutton, 1, NULL },
306 { "fullbutton", o_fullbutton, 1, NULL },
307 { "no-button", o_no_label, 1, NULL },
308 { "ok-button", o_ok_label, 1, NULL },
309 { "scrolltext", o_scrollbar, 1, NULL },
310 { "topleft", o_topleft, 1, NULL },
311 { "yes-button", o_yes_label, 1, NULL },
314 { "calendar", o_calendar, 2, "<text> <height> <width> <day> <month> <year>" },
315 { "dselect", o_dselect, 2, "<directory> <height> <width>" },
316 { "editbox", o_editbox, 2, "<file> <height> <width>" },
317 { "fselect", o_fselect, 2, "<filepath> <height> <width>" },
318 { "timebox", o_timebox, 2, "<text> <height> <width> <hour> <minute> <second>" },
321 { "buildlist", o_buildlist, 2, "<text> <height> <width> <tag1> <item1> <status1>..." },
322 { "no-items", o_no_items, 1, "" },
323 { "no-tags", o_no_tags, 1, "" },
324 { "rangebox", o_rangebox, 2, "<text> <height> <width> <min-value> <max-value> <default-value>" },
325 { "treeview", o_treeview, 2, "<text> <height> <width> <list-height> <tag1> <item1> <status1> <depth1>..." },
327 #if defined(HAVE_XDIALOG2) || defined(HAVE_WHIPTAIL)
328 { "noitem", o_no_items, 1, NULL },
329 { "notags", o_no_tags, 1, NULL },
331 #ifdef HAVE_DLG_TRACE
332 { "trace", o_trace, 1, "<file>" },
338 * Make an array showing which argv[] entries are options. Use "--" as a
339 * special token to escape the next argument, allowing it to begin with "--".
340 * When we find a "--" argument, also remove it from argv[] and adjust argc.
341 * That appears to be an undocumented feature of the popt library.
343 * Also, if we see a "--file", expand it into the parameter list by reading the
344 * text from the given file and stripping quotes, treating whitespace outside
345 * quotes as a parameter delimiter.
347 * Finally, if we see a "--args", dump the current list of arguments to the
348 * standard error. This is used for debugging complex --file combinations.
351 unescape_argv(int *argcp, char ***argvp)
354 int limit_includes = 20 + *argcp;
355 int count_includes = 0;
356 bool changed = FALSE;
357 bool doalloc = FALSE;
360 dialog_opts = dlg_calloc(bool, (size_t) *argcp + 1);
361 assert_ptr(dialog_opts, "unescape_argv");
363 for (j = 1; j < *argcp; j++) {
364 bool escaped = FALSE;
365 if (!strcmp((*argvp)[j], "--")) {
367 changed = dlg_eat_argv(argcp, argvp, j, 1);
368 } else if (!strcmp((*argvp)[j], "--args")) {
369 fprintf(stderr, "Showing arguments at arg%d\n", j);
370 for (k = 0; k < *argcp; ++k) {
371 fprintf(stderr, " arg%d:%s\n", k, (*argvp)[k]);
373 changed = dlg_eat_argv(argcp, argvp, j, 1);
374 } else if (!strcmp((*argvp)[j], "--file")) {
375 if (++count_includes > limit_includes)
376 dlg_exiterr("Too many --file options");
378 if ((filename = (*argvp)[j + 1]) != 0) {
387 if (*filename == '&') {
388 fp = fdopen(atoi(filename + sizeof(char)), "r");
390 fp = fopen(filename, "r");
397 blob = dlg_realloc(char, length + BUFSIZ + 1, blob);
398 assert_ptr(blob, "unescape_argv");
399 bytes_read = fread(blob + length,
403 length += bytes_read;
405 dlg_exiterr("error on filehandle in unescape_argv");
406 } while (bytes_read == BUFSIZ);
411 list = dlg_string_to_argv(blob);
412 if ((added = dlg_count_argv(list)) != 0) {
414 size_t need = (size_t) (*argcp + added + 1);
416 *argvp = dlg_realloc(char *, need, *argvp);
417 assert_ptr(*argvp, "unescape_argv");
419 char **newp = dlg_malloc(char *, need);
420 assert_ptr(newp, "unescape_argv");
421 for (n = 0; n < *argcp; ++n) {
422 newp[n] = (*argvp)[n];
427 dialog_opts = dlg_realloc(bool, need, dialog_opts);
428 assert_ptr(dialog_opts, "unescape_argv");
430 for (n = *argcp; n >= j + 2; --n) {
431 (*argvp)[n + added - 2] = (*argvp)[n];
432 dialog_opts[n + added - 2] = dialog_opts[n];
434 for (n = 0; n < added; ++n) {
435 (*argvp)[n + j] = list[n];
436 dialog_opts[n + j] = FALSE;
442 dlg_exiterr("Cannot open --file %s", filename);
444 (*argvp)[*argcp] = 0;
448 dlg_exiterr("No value given for --file");
453 && !strncmp((*argvp)[j], "--", (size_t) 2)
454 && isalpha(UCH((*argvp)[j][2]))) {
455 dialog_opts[j] = TRUE;
459 /* if we didn't find any "--" tokens, there's no reason to do the table
460 * lookup in isOption()
466 dialog_argv = (*argvp);
469 #define OptionChars "\
472 abcdefghijklmnopqrstuvwxyz\
476 * Check if the given string from main's argv is an option.
479 isOption(const char *arg)
484 if (dialog_opts != 0) {
486 for (n = 1; dialog_argv[n] != 0; ++n) {
487 if (dialog_argv[n] == arg) {
488 result = dialog_opts[n];
492 } else if (!strncmp(arg, "--", (size_t) 2) && isalpha(UCH(arg[2]))) {
493 if (strlen(arg) == strspn(arg, OptionChars)) {
496 dlg_exiterr("Invalid option \"%s\"", arg);
504 lookupOption(const char *name, int pass)
507 eOptions result = o_unknown;
509 if (isOption(name)) {
511 for (n = 0; n < sizeof(options) / sizeof(options[0]); n++) {
512 if ((pass & options[n].pass) != 0
513 && !strcmp(name, options[n].name)) {
514 result = options[n].code;
523 Usage(const char *msg)
525 dlg_exiterr("Error: %s.\nUse --help to list options.\n\n", msg);
529 * Count arguments, stopping at the end of the argument list, or on any of our
533 arg_rest(char *argv[])
535 int i = 1; /* argv[0] points to a "--" token */
538 && (!isOption(argv[i]) || lookupOption(argv[i], 7) == o_unknown))
544 * In MultiWidget this function is needed to count how many tags
545 * a widget (menu, checklist, radiolist) has
548 howmany_tags(char *argv[], int group)
552 const char *format = "Expected %d arguments, found only %d";
555 while (argv[0] != 0) {
556 if (isOption(argv[0]))
558 if ((have = arg_rest(argv)) < group) {
559 sprintf(temp, format, group, have);
571 numeric_arg(char **av, int n)
575 if (n < dlg_count_argv(av)) {
578 result = (int) strtol(av[n], &last, 10);
580 if (last == 0 || *last != 0) {
581 sprintf(msg, "Expected a number for token %d of %.20s", n, av[0]);
589 optional_str(char **av, int n, char *dft)
592 if (arg_rest(av) > n)
597 #if defined(HAVE_DLG_GAUGE) || defined(HAVE_XDIALOG)
599 optional_num(char **av, int n, int dft)
602 if (arg_rest(av) > n)
603 ret = numeric_arg(av, n);
609 * On AIX 4.x, we have to flush the output right away since there is a bug in
610 * the curses package which discards stdout even when we've used newterm to
611 * redirect output to /dev/tty.
622 case DLG_EXIT_ITEM_HELP:
623 if ((dialog_state.output_count > 1) && !dialog_vars.separate_output) {
624 fputs((dialog_state.separate_str
625 ? dialog_state.separate_str
626 : DEFAULT_SEPARATE_STR),
627 dialog_state.output);
630 if (dialog_vars.input_result != 0
631 && dialog_vars.input_result[0] != '\0') {
632 fputs(dialog_vars.input_result, dialog_state.output);
636 fflush(dialog_state.output);
644 * These are the widget callers.
651 return dialog_yesno(t,
658 call_msgbox(CALLARGS)
661 return dialog_msgbox(t,
664 numeric_arg(av, 3), 1);
668 call_infobox(CALLARGS)
671 return dialog_msgbox(t,
674 numeric_arg(av, 3), 0);
678 call_textbox(CALLARGS)
681 return dialog_textbox(t,
690 int tags = howmany_tags(av + 5, MENUBOX_TAGS);
691 *offset_add = 5 + tags * MENUBOX_TAGS;
693 return dialog_menu(t,
702 call_inputmenu(CALLARGS)
704 int tags = howmany_tags(av + 5, MENUBOX_TAGS);
705 bool free_extra_label = FALSE;
708 dialog_vars.input_menu = TRUE;
710 if (dialog_vars.max_input == 0)
711 dialog_vars.max_input = MAX_LEN / 2;
713 if (dialog_vars.extra_label == 0) {
714 free_extra_label = TRUE;
715 dialog_vars.extra_label = dlg_strclone(_("Rename"));
718 dialog_vars.extra_button = TRUE;
720 *offset_add = 5 + tags * MENUBOX_TAGS;
721 result = dialog_menu(t,
727 if (free_extra_label) {
728 free(dialog_vars.extra_label);
729 dialog_vars.extra_label = 0;
735 call_checklist(CALLARGS)
737 int tags = howmany_tags(av + 5, CHECKBOX_TAGS);
740 *offset_add = 5 + tags * CHECKBOX_TAGS;
741 code = dialog_checklist(t,
746 tags, av + 5, FLAG_CHECK);
751 call_radiolist(CALLARGS)
753 int tags = howmany_tags(av + 5, CHECKBOX_TAGS);
754 *offset_add = 5 + tags * CHECKBOX_TAGS;
755 return dialog_checklist(t,
760 tags, av + 5, FLAG_RADIO);
764 call_inputbox(CALLARGS)
766 *offset_add = arg_rest(av);
767 return dialog_inputbox(t,
771 optional_str(av, 4, 0), 0);
775 call_passwordbox(CALLARGS)
777 *offset_add = arg_rest(av);
778 return dialog_inputbox(t,
782 optional_str(av, 4, 0), 1);
787 call_calendar(CALLARGS)
789 *offset_add = arg_rest(av);
790 return dialog_calendar(t,
794 optional_num(av, 4, -1),
795 optional_num(av, 5, -1),
796 optional_num(av, 6, -1));
800 call_dselect(CALLARGS)
802 *offset_add = arg_rest(av);
803 return dialog_dselect(t,
810 call_editbox(CALLARGS)
813 return dialog_editbox(t,
820 call_fselect(CALLARGS)
822 *offset_add = arg_rest(av);
823 return dialog_fselect(t,
830 call_timebox(CALLARGS)
832 *offset_add = arg_rest(av);
833 return dialog_timebox(t,
837 optional_num(av, 4, -1),
838 optional_num(av, 5, -1),
839 optional_num(av, 6, -1));
841 #endif /* HAVE_XDIALOG */
843 /* dialog 1.2 widgets */
846 #define DisableNoTags() \
847 bool save_no_tags = dialog_vars.no_tags; \
848 bool save_no_items = dialog_vars.no_items; \
849 dialog_vars.no_tags = TRUE; \
850 dialog_vars.no_items = FALSE
852 #define RestoreNoTags() \
853 dialog_vars.no_tags = save_no_tags; \
854 dialog_vars.no_items = save_no_items
857 call_buildlist(CALLARGS)
859 int tags = howmany_tags(av + 5, CHECKBOX_TAGS);
864 *offset_add = 5 + tags * CHECKBOX_TAGS;
865 result = dialog_buildlist(t,
877 call_rangebox(CALLARGS)
881 *offset_add = arg_rest(av);
882 min_value = numeric_arg(av, 4);
883 return dialog_rangebox(t,
889 (*offset_add > 6) ? numeric_arg(av, 6) : min_value);
893 call_treeview(CALLARGS)
895 int tags = howmany_tags(av + 5, TREEVIEW_TAGS);
900 *offset_add = arg_rest(av);
901 result = dialog_treeview(t,
906 tags, av + 5, FLAG_RADIO);
910 #endif /* HAVE_XDIALOG */
912 #ifdef HAVE_DLG_FORMBOX
916 int group = FORMBOX_TAGS;
917 int tags = howmany_tags(av + 5, group);
918 *offset_add = 5 + tags * group;
920 return dialog_form(t,
929 call_password_form(CALLARGS)
931 unsigned save = dialog_vars.formitem_type;
934 dialog_vars.formitem_type = 1;
935 result = call_form(PASSARGS);
936 dialog_vars.formitem_type = save;
940 #endif /* HAVE_DLG_FORMBOX */
942 #ifdef HAVE_DLG_MIXEDFORM
944 call_mixed_form(CALLARGS)
946 int group = MIXEDFORM_TAGS;
947 int tags = howmany_tags(av + 5, group);
948 *offset_add = 5 + tags * group;
950 return dialog_mixedform(t,
957 #endif /* HAVE_DLG_MIXEDFORM */
959 #ifdef HAVE_DLG_GAUGE
963 *offset_add = arg_rest(av);
964 return dialog_gauge(t,
968 optional_num(av, 4, 0));
974 *offset_add = arg_rest(av);
975 return dialog_pause(t,
983 #ifdef HAVE_MIXEDGAUGE
985 call_mixed_gauge(CALLARGS)
987 #define MIXEDGAUGE_BASE 5
988 int tags = howmany_tags(av + MIXEDGAUGE_BASE, MIXEDGAUGE_TAGS);
989 *offset_add = MIXEDGAUGE_BASE + tags * MIXEDGAUGE_TAGS;
990 return dialog_mixedgauge(t,
995 tags, av + MIXEDGAUGE_BASE);
999 #ifdef HAVE_DLG_GAUGE
1001 call_prgbox(CALLARGS)
1003 *offset_add = arg_rest(av);
1004 /* the original version does not accept a prompt string, but for
1005 * consistency we allow it.
1007 return ((*offset_add == 5)
1012 numeric_arg(av, 4), TRUE)
1017 numeric_arg(av, 3), TRUE));
1021 #ifdef HAVE_DLG_GAUGE
1023 call_programbox(CALLARGS)
1027 *offset_add = arg_rest(av);
1028 /* this function is a compromise between --prgbox and --progressbox.
1030 result = ((*offset_add == 4)
1031 ? dlg_progressbox(t,
1036 dialog_state.pipe_input)
1037 : dlg_progressbox(t,
1042 dialog_state.pipe_input));
1043 dialog_state.pipe_input = 0;
1048 #ifdef HAVE_DLG_GAUGE
1050 call_progressbox(CALLARGS)
1052 *offset_add = arg_rest(av);
1053 /* the original version does not accept a prompt string, but for
1054 * consistency we allow it.
1056 return ((*offset_add == 4)
1057 ? dialog_progressbox(t,
1061 : dialog_progressbox(t,
1064 numeric_arg(av, 2)));
1068 #ifdef HAVE_DLG_TAILBOX
1070 call_tailbox(CALLARGS)
1073 return dialog_tailbox(t,
1081 call_tailboxbg(CALLARGS)
1084 return dialog_tailbox(t,
1092 static const Mode modes[] =
1094 {o_yesno, 4, 4, call_yesno},
1095 {o_msgbox, 4, 4, call_msgbox},
1096 {o_infobox, 4, 4, call_infobox},
1097 {o_textbox, 4, 4, call_textbox},
1098 {o_menu, 7, 0, call_menu},
1099 {o_inputmenu, 7, 0, call_inputmenu},
1100 {o_checklist, 8, 0, call_checklist},
1101 {o_radiolist, 8, 0, call_radiolist},
1102 {o_inputbox, 4, 5, call_inputbox},
1103 {o_passwordbox, 4, 5, call_passwordbox},
1104 #ifdef HAVE_DLG_GAUGE
1105 {o_gauge, 4, 5, call_gauge},
1106 {o_pause, 5, 5, call_pause},
1107 {o_prgbox, 4, 5, call_prgbox},
1108 {o_programbox, 3, 4, call_programbox},
1109 {o_progressbox, 3, 4, call_progressbox},
1111 #ifdef HAVE_DLG_FORMBOX
1112 {o_passwordform, 13, 0, call_password_form},
1113 {o_form, 13, 0, call_form},
1115 #ifdef HAVE_MIXEDGAUGE
1116 {o_mixedgauge, MIXEDGAUGE_BASE, 0, call_mixed_gauge},
1118 #ifdef HAVE_DLG_MIXEDFORM
1119 {o_mixedform, 13, 0, call_mixed_form},
1121 #ifdef HAVE_DLG_TAILBOX
1122 {o_tailbox, 4, 4, call_tailbox},
1123 {o_tailboxbg, 4, 4, call_tailboxbg},
1126 {o_buildlist, 4, 0, call_buildlist},
1127 {o_calendar, 4, 7, call_calendar},
1128 {o_dselect, 4, 5, call_dselect},
1129 {o_editbox, 4, 4, call_editbox},
1130 {o_fselect, 4, 5, call_fselect},
1131 {o_rangebox, 5, 7, call_rangebox},
1132 {o_timebox, 4, 7, call_timebox},
1133 {o_treeview, 4, 0, call_treeview},
1139 optionString(char **argv, int *num)
1141 int next = *num + 1;
1142 char *result = argv[next];
1145 sprintf(temp, "Expected a string-parameter for %.20s", argv[*num]);
1153 optionValue(char **argv, int *num)
1155 int next = *num + 1;
1156 char *src = argv[next];
1161 result = (int) strtol(src, &tmp, 0);
1162 if (tmp == 0 || *tmp != 0)
1168 sprintf(temp, "Expected a numeric-parameter for %.20s", argv[*num]);
1175 /* Return exit-code for a named button */
1177 button_code(const char *name)
1184 { "ok", DLG_EXIT_OK },
1185 { "yes", DLG_EXIT_OK },
1186 { "cancel", DLG_EXIT_CANCEL },
1187 { "no", DLG_EXIT_CANCEL },
1188 { "help", DLG_EXIT_HELP },
1189 { "extra", DLG_EXIT_EXTRA },
1193 int code = DLG_EXIT_ERROR;
1196 for (i = 0; i < (sizeof(table) / sizeof(table[0])); i++) {
1197 if (!dlg_strcmp(name, table[i].name)) {
1198 code = table[i].code;
1203 if (code == DLG_EXIT_ERROR) {
1205 sprintf(temp, "Button name \"%.20s\" unknown", name);
1213 * Print parts of a message
1216 PrintList(const char *const *list)
1218 const char *leaf = strrchr(program, '/');
1226 while (*list != 0) {
1227 fprintf(dialog_state.output, *list, n ? leaf : dialog_version());
1228 (void) fputc('\n', dialog_state.output);
1235 lookupMode(eOptions code)
1237 const Mode *modePtr = 0;
1240 for (n = 0; n < sizeof(modes) / sizeof(modes[0]); n++) {
1241 if (modes[n].code == code) {
1242 modePtr = &modes[n];
1250 compare_opts(const void *a, const void *b)
1252 Options *const *p = (Options * const *) a;
1253 Options *const *q = (Options * const *) b;
1254 return strcmp((*p)->name, (*q)->name);
1258 * Print program's version.
1261 PrintVersion(FILE *fp)
1263 fprintf(fp, "Version: %s\n", dialog_version());
1267 * Print program help-message
1272 static const char *const tbl_1[] =
1274 "cdialog (ComeOn Dialog!) version %s",
1275 "Copyright 2000-2011,2012 Thomas E. Dickey",
1276 "This is free software; see the source for copying conditions. There is NO",
1277 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.",
1279 "* Display dialog boxes from shell scripts *",
1281 "Usage: %s <options> { --and-widget <options> }",
1282 "where options are \"common\" options, followed by \"box\" options",
1286 " [--create-rc \"file\"]",
1292 "Auto-size with height and width = 0. Maximize with height and width = -1.",
1293 "Global-auto-size if also menu_height/list_height = 0.",
1296 size_t limit = sizeof(options) / sizeof(options[0]);
1298 const Options **opts;
1301 dialog_state.output = stdout;
1303 opts = dlg_calloc(const Options *, limit);
1304 assert_ptr(opts, "Help");
1305 for (j = 0; j < limit; ++j) {
1306 opts[j] = &(options[j]);
1308 qsort(opts, limit, sizeof(Options *), compare_opts);
1311 fprintf(dialog_state.output, "Common options:\n ");
1312 for (j = k = 0; j < limit; j++) {
1313 if ((opts[j]->pass & 1)
1314 && opts[j]->help != 0) {
1315 size_t len = 6 + strlen(opts[j]->name) + strlen(opts[j]->help);
1318 fprintf(dialog_state.output, "\n ");
1321 fprintf(dialog_state.output, " [--%s%s%s]", opts[j]->name,
1322 *(opts[j]->help) ? " " : "", opts[j]->help);
1325 fprintf(dialog_state.output, "\nBox options:\n");
1326 for (j = 0; j < limit; j++) {
1327 if ((opts[j]->pass & 2) != 0
1328 && opts[j]->help != 0
1329 && lookupMode(opts[j]->code))
1330 fprintf(dialog_state.output, " --%-12s %s\n", opts[j]->name,
1336 dlg_exit(DLG_EXIT_OK);
1339 #ifdef HAVE_DLG_TRACE
1341 * Only the first call to dlg_trace will open a trace file. But each time
1342 * --trace is parsed, we show the whole parameter list as it is at that moment,
1343 * counting discarded parameters. The only way to capture the whole parameter
1344 * list is if --trace is the first option.
1347 process_trace_option(char **argv, int *offset)
1351 if (dialog_state.trace_output == 0) {
1352 dlg_trace(optionString(argv, offset));
1354 dlg_trace_msg("# ignore extra --trace option\n");
1358 dlg_trace_msg("# Parameters:\n");
1359 for (j = 0; argv[j] != 0; ++j) {
1360 dlg_trace_msg("# argv[%d] = %s\n", j, argv[j]);
1366 * "Common" options apply to all widgets more/less. Most of the common options
1367 * set values in dialog_vars, a few set dialog_state and a couple write to the
1371 process_common_options(int argc, char **argv, int offset, bool output)
1375 dlg_trace_msg("# process_common_options, offset %d\n", offset);
1377 while (offset < argc && !done) { /* Common options */
1378 dlg_trace_msg("#\targv[%d] = %s\n", offset, argv[offset]);
1379 switch (lookupOption(argv[offset], 1)) {
1381 dialog_vars.title = optionString(argv, &offset);
1384 dialog_vars.backtitle = optionString(argv, &offset);
1386 case o_separate_widget:
1387 dialog_state.separate_str = optionString(argv, &offset);
1389 case o_separate_output:
1390 dialog_vars.separate_output = TRUE;
1393 dialog_vars.colors = TRUE;
1396 dialog_vars.cr_wrap = TRUE;
1398 case o_no_nl_expand:
1399 dialog_vars.no_nl_expand = TRUE;
1402 dialog_vars.nocollapse = TRUE;
1405 dialog_vars.cant_kill = TRUE;
1408 dialog_vars.nocancel = TRUE;
1411 dialog_vars.nook = TRUE;
1414 dialog_vars.quoted = TRUE;
1416 case o_single_quoted:
1417 dialog_vars.single_quoted = TRUE;
1420 dialog_vars.size_err = TRUE;
1423 dialog_vars.beep_signal = TRUE;
1426 dialog_vars.beep_after_signal = TRUE;
1429 dialog_state.use_scrollbar = TRUE;
1432 dialog_state.use_shadow = TRUE;
1435 dialog_vars.defaultno = TRUE;
1436 dialog_vars.default_button = DLG_EXIT_CANCEL;
1438 case o_default_button:
1439 dialog_vars.default_button = button_code(optionString(argv, &offset));
1440 dialog_vars.defaultno = dialog_vars.default_button == DLG_EXIT_CANCEL;
1442 case o_default_item:
1443 dialog_vars.default_item = optionString(argv, &offset);
1446 dialog_vars.insecure = TRUE;
1449 dialog_vars.item_help = TRUE;
1452 dialog_vars.help_line = optionString(argv, &offset);
1455 dialog_vars.help_file = optionString(argv, &offset);
1458 dialog_vars.help_button = TRUE;
1461 dialog_vars.help_status = TRUE;
1463 case o_extra_button:
1464 dialog_vars.extra_button = TRUE;
1467 ignore_unknown = TRUE;
1470 dialog_vars.keep_window = TRUE;
1473 dialog_state.use_shadow = FALSE;
1476 dialog_vars.print_siz = TRUE;
1478 case o_print_maxsize:
1481 * If this is the last option, we do not want any error
1482 * messages - just our output. Calling end_dialog() cancels
1483 * the refresh() at the end of the program as well.
1485 if (argv[offset + 1] == 0) {
1486 ignore_unknown = TRUE;
1489 fflush(dialog_state.output);
1490 fprintf(dialog_state.output, "MaxSize: %d, %d\n", SLINES, SCOLS);
1493 case o_print_version:
1495 PrintVersion(dialog_state.output);
1499 case o_output_separator:
1500 dialog_vars.output_separator = optionString(argv, &offset);
1502 case o_column_separator:
1503 dialog_vars.column_separator = optionString(argv, &offset);
1506 dialog_vars.tab_correct = TRUE;
1509 dialog_vars.sleep_secs = optionValue(argv, &offset);
1512 dialog_vars.timeout_secs = optionValue(argv, &offset);
1515 dialog_vars.max_input = optionValue(argv, &offset);
1518 dialog_state.tab_len = optionValue(argv, &offset);
1521 dialog_vars.trim_whitespace = TRUE;
1524 dialog_state.visit_items = TRUE;
1525 dialog_state.visit_cols = 1;
1528 dialog_state.aspect_ratio = optionValue(argv, &offset);
1531 dialog_vars.begin_set = TRUE;
1532 dialog_vars.begin_y = optionValue(argv, &offset);
1533 dialog_vars.begin_x = optionValue(argv, &offset);
1536 dialog_vars.dlg_clear_screen = TRUE;
1539 dialog_vars.yes_label = optionString(argv, &offset);
1542 dialog_vars.no_label = optionString(argv, &offset);
1545 dialog_vars.ok_label = optionString(argv, &offset);
1547 case o_cancel_label:
1548 dialog_vars.cancel_label = optionString(argv, &offset);
1551 dialog_vars.extra_label = optionString(argv, &offset);
1554 dialog_vars.exit_label = optionString(argv, &offset);
1557 dialog_vars.help_label = optionString(argv, &offset);
1560 dialog_vars.date_format = optionString(argv, &offset);
1563 dialog_vars.time_format = optionString(argv, &offset);
1566 dialog_vars.keep_tite = TRUE;
1569 dialog_vars.ascii_lines = TRUE;
1570 dialog_vars.no_lines = FALSE;
1573 dialog_vars.no_lines = TRUE;
1574 dialog_vars.ascii_lines = FALSE;
1577 dialog_state.no_mouse = TRUE;
1580 #ifdef HAVE_WHIPTAIL
1582 dialog_vars.begin_set = TRUE;
1583 dialog_vars.begin_y = 0;
1584 dialog_vars.begin_x = 0;
1590 /* options of Xdialog which we ignore */
1593 (void) optionString(argv, &offset);
1596 case o_auto_placement:
1601 case o_screen_center:
1609 default: /* no more common options */
1612 #ifdef HAVE_DLG_TRACE
1614 process_trace_option(argv, &offset);
1617 #if defined(HAVE_XDIALOG2) || defined(HAVE_WHIPTAIL)
1619 dialog_vars.no_items = TRUE;
1622 dialog_vars.no_tags = TRUE;
1633 * Initialize options at the start of a series of common options culminating
1637 init_result(char *buffer)
1639 static bool first = TRUE;
1640 static char **special_argv = 0;
1641 static int special_argc = 0;
1643 dlg_trace_msg("# init_result\n");
1645 /* clear everything we do not save for the next widget */
1646 memset(&dialog_vars, 0, sizeof(dialog_vars));
1648 dialog_vars.input_result = buffer;
1649 dialog_vars.input_result[0] = '\0';
1651 dialog_vars.default_button = -1;
1654 * The first time this is called, check for common options given by an
1655 * environment variable.
1658 char *env = getenv("DIALOGOPTS");
1660 env = dlg_strclone(env);
1662 special_argv = dlg_string_to_argv(env);
1663 special_argc = dlg_count_argv(special_argv);
1669 * If we are not checking memory leaks, just do the parse of the
1672 if (special_argv != 0) {
1673 process_common_options(special_argc, special_argv, 0, FALSE);
1675 free(special_argv[0]);
1685 main(int argc, char *argv[])
1688 bool esc_pressed = FALSE;
1689 bool keep_tite = FALSE;
1692 int retval = DLG_EXIT_OK;
1695 const Mode *modePtr;
1696 char my_buffer[MAX_LEN + 1];
1698 memset(&dialog_state, 0, sizeof(dialog_state));
1699 memset(&dialog_vars, 0, sizeof(dialog_vars));
1701 #if defined(ENABLE_NLS)
1702 /* initialize locale support */
1703 setlocale(LC_ALL, "");
1704 bindtextdomain(NLS_TEXTDOMAIN, LOCALEDIR);
1705 textdomain(NLS_TEXTDOMAIN);
1706 #elif defined(HAVE_SETLOCALE)
1707 (void) setlocale(LC_ALL, "");
1710 unescape_argv(&argc, &argv);
1712 dialog_state.output = stderr;
1713 dialog_state.input = stdin;
1716 * Look for the last --stdout, --stderr or --output-fd option, and use
1717 * that. We can only write to one of them. If --stdout is used, that
1718 * can interfere with initializing the curses library, so we want to
1719 * know explicitly if it is used.
1721 * Also, look for any --version or --help message, processing those
1724 while (offset < argc) {
1726 switch (lookupOption(argv[offset], 7)) {
1728 dialog_state.output = stdout;
1731 dialog_state.output = stderr;
1734 if ((j = optionValue(argv, &offset)) < 0
1735 || (dialog_state.input = fdopen(j, "r")) == 0)
1736 dlg_exiterr("Cannot open input-fd\n");
1739 if ((j = optionValue(argv, &offset)) < 0
1740 || (dialog_state.output = fdopen(j, "w")) == 0)
1741 dlg_exiterr("Cannot open output-fd\n");
1747 dialog_state.output = stdout;
1748 PrintVersion(dialog_state.output);
1754 #ifdef HAVE_DLG_TRACE
1757 * Process/remove the --trace option if it is the first option.
1758 * Otherwise, process it in more/less expected order as a
1762 process_trace_option(argv, &offset);
1773 dlg_trace_msg("# discarding %d parameters starting with argv[%d] (%s)\n",
1774 1 + offset - base, base,
1776 for (j = base; j < argc; ++j) {
1777 dialog_argv[j] = dialog_argv[j + 1 + (offset - base)];
1778 if (dialog_opts != 0)
1779 dialog_opts[j] = dialog_opts[j + 1 + (offset - base)];
1781 argc -= (1 + offset - base);
1785 init_result(my_buffer);
1788 * Dialog's output may be redirected (see above). Handle the special
1789 * case of options that only report information without interaction.
1792 switch (lookupOption(argv[1], 7)) {
1793 case o_print_maxsize:
1796 fflush(dialog_state.output);
1797 fprintf(dialog_state.output, "MaxSize: %d, %d\n", SLINES, SCOLS);
1799 case o_print_version:
1800 PrintVersion(dialog_state.output);
1820 if (lookupOption(argv[1], 7) == o_create_rc) {
1822 sprintf(temp, "Expected a filename for %.50s", argv[1]);
1825 if (dlg_parse_rc() == -1) /* Read the configuration file */
1826 dlg_exiterr("dialog: dlg_parse_rc");
1827 dlg_create_rc(argv[2]);
1832 dialog_vars.keep_tite = keep_tite; /* init_result() cleared global */
1834 init_dialog(dialog_state.input, dialog_state.output);
1836 while (offset < argc && !esc_pressed) {
1837 init_result(my_buffer);
1839 offset = process_common_options(argc, argv, offset, TRUE);
1841 if (argv[offset] == NULL) {
1844 Usage("Expected a box option");
1847 if (lookupOption(argv[offset], 2) != o_checklist
1848 && dialog_vars.separate_output) {
1849 sprintf(temp, "Expected --checklist, not %.20s", argv[offset]);
1853 if (dialog_state.aspect_ratio == 0)
1854 dialog_state.aspect_ratio = DEFAULT_ASPECT_RATIO;
1856 dlg_put_backtitle();
1858 /* use a table to look for the requested mode, to avoid code duplication */
1861 if ((code = lookupOption(argv[offset], 2)) != o_unknown)
1862 modePtr = lookupMode(code);
1864 sprintf(temp, "%s option %.20s",
1865 lookupOption(argv[offset], 7) != o_unknown
1872 have = arg_rest(&argv[offset]);
1873 if (have < modePtr->argmin) {
1874 sprintf(temp, "Expected at least %d tokens for %.20s, have %d",
1875 modePtr->argmin - 1, argv[offset],
1879 if (modePtr->argmax && have > modePtr->argmax) {
1881 "Expected no more than %d tokens for %.20s, have %d",
1882 modePtr->argmax - 1, argv[offset],
1888 * Trim whitespace from non-title option values, e.g., the ones that
1889 * will be used as captions or prompts. Do that only for the widget
1890 * we are about to process, since the "--trim" option is reset before
1891 * accumulating options for each widget.
1893 for (j = offset + 1; j <= offset + have; j++) {
1894 switch (lookupOption(argv[j - 1], 7)) {
1903 char *argv_j = strdup(argv[j]);
1905 dlg_trim_string(argv_j);
1908 argv[j] = strdup("?");
1915 retval = show_result((*(modePtr->jumper)) (dialog_vars.title,
1918 dlg_trace_msg("# widget returns %d\n", retval);
1919 offset += offset_add;
1921 if (dialog_vars.input_result != my_buffer) {
1922 free(dialog_vars.input_result);
1923 dialog_vars.input_result = 0;
1926 if (retval == DLG_EXIT_ESC) {
1930 if (dialog_vars.beep_after_signal)
1933 if (dialog_vars.sleep_secs)
1934 (void) napms(dialog_vars.sleep_secs * 1000);
1936 if (offset < argc) {
1937 switch (lookupOption(argv[offset], 7)) {
1942 sprintf(temp, "Expected --and-widget, not %.20s",
1947 /* if we got a cancel, etc., stop chaining */
1948 if (retval != DLG_EXIT_OK)
1951 dialog_vars.dlg_clear_screen = TRUE;
1955 if (dialog_vars.dlg_clear_screen)
1960 dlg_killall_bg(&retval);
1961 if (dialog_state.screen_initialized) {