2 * $Id: dialog.c,v 1.202 2012/07/01 20:20: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);
149 #ifdef HAVE_DLG_TRACE
155 * The bits in 'pass' are used to decide which options are applicable at
156 * different stages in the program:
157 * 1 flags before widgets
159 * 4 non-widget options
164 int pass; /* 1,2,4 or combination */
165 const char *help; /* NULL to suppress, non-empty to display params */
174 static bool *dialog_opts;
175 static char **dialog_argv;
177 static bool ignore_unknown = FALSE;
179 static const char *program = "dialog";
182 * The options[] table is organized this way to make it simple to maintain
183 * a sorted list of options for the help-message.
186 static const Options options[] = {
187 { "allow-close", o_allow_close, 1, NULL },
188 { "and-widget", o_and_widget, 4, NULL },
189 { "ascii-lines", o_ascii_lines, 1, "" },
190 { "aspect", o_aspect, 1, "<ratio>" },
191 { "auto-placement", o_auto_placement, 1, NULL },
192 { "backtitle", o_backtitle, 1, "<backtitle>" },
193 { "beep", o_beep, 1, NULL },
194 { "beep-after", o_beep_after, 1, NULL },
195 { "begin", o_begin, 1, "<y> <x>" },
196 { "calendar", o_calendar, 2, "<text> <height> <width> <day> <month> <year>" },
197 { "cancel-label", o_cancel_label, 1, "<str>" },
198 { "checklist", o_checklist, 2, "<text> <height> <width> <list height> <tag1> <item1> <status1>..." },
199 { "clear", o_clear, 1, "" },
200 { "colors", o_colors, 1, "" },
201 { "column-separator",o_column_separator, 1, "<str>" },
202 { "cr-wrap", o_cr_wrap, 1, "" },
203 { "create-rc", o_create_rc, 1, NULL },
204 { "date-format", o_date_format, 1, "<str>" },
205 { "default-button", o_default_button, 1, "<str>" },
206 { "default-item", o_default_item, 1, "<str>" },
207 { "defaultno", o_defaultno, 1, "" },
208 { "dselect", o_dselect, 2, "<directory> <height> <width>" },
209 { "editbox", o_editbox, 2, "<file> <height> <width>" },
210 { "exit-label", o_exit_label, 1, "<str>" },
211 { "extra-button", o_extra_button, 1, "" },
212 { "extra-label", o_extra_label, 1, "<str>" },
213 { "fb", o_fullbutton, 1, NULL },
214 { "fixed-font", o_fixed_font, 1, NULL },
215 { "form", o_form, 2, "<text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1>..." },
216 { "fselect", o_fselect, 2, "<filepath> <height> <width>" },
217 { "fullbutton", o_fullbutton, 1, NULL },
218 { "gauge", o_gauge, 2, "<text> <height> <width> [<percent>]" },
219 { "guage", o_gauge, 2, NULL },
220 { "help", o_help, 4, "" },
221 { "help-button", o_help_button, 1, "" },
222 { "help-label", o_help_label, 1, "<str>" },
223 { "help-status", o_help_status, 1, "" },
224 { "hfile", o_help_file, 1, "<str>" },
225 { "hline", o_help_line, 1, "<str>" },
226 { "icon", o_icon, 1, NULL },
227 { "ignore", o_ignore, 1, "" },
228 { "infobox", o_infobox, 2, "<text> <height> <width>" },
229 { "input-fd", o_input_fd, 1, "<fd>" },
230 { "inputbox", o_inputbox, 2, "<text> <height> <width> [<init>]" },
231 { "inputmenu", o_inputmenu, 2, "<text> <height> <width> <menu height> <tag1> <item1>..." },
232 { "insecure", o_insecure, 1, "" },
233 { "item-help", o_item_help, 1, "" },
234 { "keep-colors", o_keep_colors, 1, NULL },
235 { "keep-tite", o_keep_tite, 1, "" },
236 { "keep-window", o_keep_window, 1, "" },
237 { "max-input", o_max_input, 1, "<n>" },
238 { "menu", o_menu, 2, "<text> <height> <width> <menu height> <tag1> <item1>..." },
239 { "mixedform", o_mixedform, 2, "<text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1> <itype>..." },
240 { "mixedgauge", o_mixedgauge, 2, "<text> <height> <width> <percent> <tag1> <item1>..." },
241 { "msgbox", o_msgbox, 2, "<text> <height> <width>" },
242 { "no-cancel", o_nocancel, 1, "" },
243 { "no-close", o_no_close, 1, NULL },
244 { "no-collapse", o_no_collapse, 1, "" },
245 { "no-cr-wrap", o_no_cr_wrap, 1, NULL },
246 { "no-kill", o_no_kill, 1, "" },
247 { "no-label", o_no_label, 1, "<str>" },
248 { "no-lines", o_no_lines, 1, "" },
249 { "no-mouse", o_no_mouse, 1, "" },
250 { "no-nl-expand", o_no_nl_expand, 1, "" },
251 { "no-ok", o_nook, 1, "" },
252 { "no-shadow", o_no_shadow, 1, "" },
253 { "nocancel", o_nocancel, 1, NULL }, /* see --no-cancel */
254 { "noitem", o_noitem, 1, NULL },
255 { "nook", o_nook, 1, "" }, /* See no-ok */
256 { "ok-label", o_ok_label, 1, "<str>" },
257 { "output-fd", o_output_fd, 1, "<fd>" },
258 { "output-separator",o_output_separator, 1, "<str>" },
259 { "passwordbox", o_passwordbox, 2, "<text> <height> <width> [<init>]" },
260 { "passwordform", o_passwordform, 2, "<text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1>..." },
261 { "pause", o_pause, 2, "<text> <height> <width> <seconds>" },
262 { "prgbox", o_prgbox, 2, "<text> <command> <height> <width>" },
263 { "print-maxsize", o_print_maxsize, 1, "" },
264 { "print-size", o_print_size, 1, "" },
265 { "print-version", o_print_version, 5, "" },
266 { "programbox", o_programbox, 2, "<text> <height> <width>" },
267 { "progressbox", o_progressbox, 2, "<text> <height> <width>" },
268 { "quoted", o_quoted, 1, "" },
269 { "radiolist", o_radiolist, 2, "<text> <height> <width> <list height> <tag1> <item1> <status1>..." },
270 { "screen-center", o_screen_center, 1, NULL },
271 { "scrollbar", o_scrollbar, 1, "" },
272 { "separate-output",o_separate_output, 1, "" },
273 { "separate-widget",o_separate_widget, 1, "<str>" },
274 { "separator", o_separator, 1, NULL },
275 { "shadow", o_shadow, 1, "" },
276 { "single-quoted", o_single_quoted, 1, "" },
277 { "size-err", o_size_err, 1, "" },
278 { "sleep", o_sleep, 1, "<secs>" },
279 { "smooth", o_smooth, 1, NULL },
280 { "stderr", o_stderr, 1, "" },
281 { "stdout", o_stdout, 1, "" },
282 { "tab-correct", o_tab_correct, 1, "" },
283 { "tab-len", o_tab_len, 1, "<n>" },
284 { "tailbox", o_tailbox, 2, "<file> <height> <width>" },
285 { "tailboxbg", o_tailboxbg, 2, "<file> <height> <width>" },
286 { "textbox", o_textbox, 2, "<file> <height> <width>" },
287 { "time-format", o_time_format, 1, "<str>" },
288 { "timebox", o_timebox, 2, "<text> <height> <width> <hour> <minute> <second>" },
289 { "timeout", o_timeout, 1, "<secs>" },
290 { "title", o_title, 1, "<title>" },
291 { "trim", o_trim, 1, "" },
292 { "under-mouse", o_under_mouse, 1, NULL },
293 { "version", o_version, 5, "" },
294 { "visit-items", o_visit_items, 1, "" },
295 { "wmclass", o_wmclass, 1, NULL },
296 { "yes-label", o_yes_label, 1, "<str>" },
297 { "yesno", o_yesno, 2, "<text> <height> <width>" },
298 #ifdef HAVE_DLG_TRACE
299 { "trace", o_trace, 1, "<file>" },
305 * Make an array showing which argv[] entries are options. Use "--" as a
306 * special token to escape the next argument, allowing it to begin with "--".
307 * When we find a "--" argument, also remove it from argv[] and adjust argc.
308 * That appears to be an undocumented feature of the popt library.
310 * Also, if we see a "--file", expand it into the parameter list by reading the
311 * text from the given file and stripping quotes, treating whitespace outside
312 * quotes as a parameter delimiter.
314 * Finally, if we see a "--args", dump the current list of arguments to the
315 * standard error. This is used for debugging complex --file combinations.
318 unescape_argv(int *argcp, char ***argvp)
321 int limit_includes = 20 + *argcp;
322 int count_includes = 0;
323 bool changed = FALSE;
324 bool doalloc = FALSE;
327 dialog_opts = dlg_calloc(bool, (size_t) *argcp + 1);
328 assert_ptr(dialog_opts, "unescape_argv");
330 for (j = 1; j < *argcp; j++) {
331 bool escaped = FALSE;
332 if (!strcmp((*argvp)[j], "--")) {
334 changed = dlg_eat_argv(argcp, argvp, j, 1);
335 } else if (!strcmp((*argvp)[j], "--args")) {
336 fprintf(stderr, "Showing arguments at arg%d\n", j);
337 for (k = 0; k < *argcp; ++k) {
338 fprintf(stderr, " arg%d:%s\n", k, (*argvp)[k]);
340 changed = dlg_eat_argv(argcp, argvp, j, 1);
341 } else if (!strcmp((*argvp)[j], "--file")) {
342 if (++count_includes > limit_includes)
343 dlg_exiterr("Too many --file options");
345 if ((filename = (*argvp)[j + 1]) != 0) {
354 if (*filename == '&') {
355 fp = fdopen(atoi(filename + sizeof(char)), "r");
357 fp = fopen(filename, "r");
364 blob = dlg_realloc(char, length + BUFSIZ + 1, blob);
365 assert_ptr(blob, "unescape_argv");
366 bytes_read = fread(blob + length,
370 length += bytes_read;
372 dlg_exiterr("error on filehandle in unescape_argv");
373 } while (bytes_read == BUFSIZ);
378 list = dlg_string_to_argv(blob);
379 if ((added = dlg_count_argv(list)) != 0) {
381 size_t need = (size_t) (*argcp + added + 1);
383 *argvp = dlg_realloc(char *, need, *argvp);
384 assert_ptr(*argvp, "unescape_argv");
386 char **newp = dlg_malloc(char *, need);
387 assert_ptr(newp, "unescape_argv");
388 for (n = 0; n < *argcp; ++n) {
389 newp[n] = (*argvp)[n];
394 dialog_opts = dlg_realloc(bool, need, dialog_opts);
395 assert_ptr(dialog_opts, "unescape_argv");
397 for (n = *argcp; n >= j + 2; --n) {
398 (*argvp)[n + added - 2] = (*argvp)[n];
399 dialog_opts[n + added - 2] = dialog_opts[n];
401 for (n = 0; n < added; ++n) {
402 (*argvp)[n + j] = list[n];
403 dialog_opts[n + j] = FALSE;
409 dlg_exiterr("Cannot open --file %s", filename);
411 (*argvp)[*argcp] = 0;
415 dlg_exiterr("No value given for --file");
420 && !strncmp((*argvp)[j], "--", (size_t) 2)
421 && isalpha(UCH((*argvp)[j][2]))) {
422 dialog_opts[j] = TRUE;
426 /* if we didn't find any "--" tokens, there's no reason to do the table
427 * lookup in isOption()
433 dialog_argv = (*argvp);
436 #define OptionChars "\
439 abcdefghijklmnopqrstuvwxyz\
443 * Check if the given string from main's argv is an option.
446 isOption(const char *arg)
451 if (dialog_opts != 0) {
453 for (n = 1; dialog_argv[n] != 0; ++n) {
454 if (dialog_argv[n] == arg) {
455 result = dialog_opts[n];
459 } else if (!strncmp(arg, "--", (size_t) 2) && isalpha(UCH(arg[2]))) {
460 if (strlen(arg) == strspn(arg, OptionChars)) {
463 dlg_exiterr("Invalid option \"%s\"", arg);
471 lookupOption(const char *name, int pass)
474 eOptions result = o_unknown;
476 if (isOption(name)) {
478 for (n = 0; n < sizeof(options) / sizeof(options[0]); n++) {
479 if ((pass & options[n].pass) != 0
480 && !strcmp(name, options[n].name)) {
481 result = options[n].code;
490 Usage(const char *msg)
492 dlg_exiterr("Error: %s.\nUse --help to list options.\n\n", msg);
496 * Count arguments, stopping at the end of the argument list, or on any of our
500 arg_rest(char *argv[])
502 int i = 1; /* argv[0] points to a "--" token */
505 && (!isOption(argv[i]) || lookupOption(argv[i], 7) == o_unknown))
511 * In MultiWidget this function is needed to count how many tags
512 * a widget (menu, checklist, radiolist) has
515 howmany_tags(char *argv[], int group)
519 const char *format = "Expected %d arguments, found only %d";
522 while (argv[0] != 0) {
523 if (isOption(argv[0]))
525 if ((have = arg_rest(argv)) < group) {
526 sprintf(temp, format, group, have);
538 numeric_arg(char **av, int n)
541 int result = (int) strtol(av[n], &last, 10);
544 if (last == 0 || *last != 0) {
545 sprintf(msg, "Expected a number for token %d of %.20s", n, av[0]);
552 optional_str(char **av, int n, char *dft)
555 if (arg_rest(av) > n)
560 #if defined(HAVE_DLG_GAUGE) || defined(HAVE_XDIALOG)
562 optional_num(char **av, int n, int dft)
565 if (arg_rest(av) > n)
566 ret = numeric_arg(av, n);
572 * On AIX 4.x, we have to flush the output right away since there is a bug in
573 * the curses package which discards stdout even when we've used newterm to
574 * redirect output to /dev/tty.
585 case DLG_EXIT_ITEM_HELP:
586 if ((dialog_state.output_count > 1) && !dialog_vars.separate_output) {
587 fputs((dialog_state.separate_str
588 ? dialog_state.separate_str
589 : DEFAULT_SEPARATE_STR),
590 dialog_state.output);
593 if (dialog_vars.input_result != 0
594 && dialog_vars.input_result[0] != '\0') {
595 fputs(dialog_vars.input_result, dialog_state.output);
599 fflush(dialog_state.output);
607 * These are the widget callers.
614 return dialog_yesno(t,
621 call_msgbox(CALLARGS)
624 return dialog_msgbox(t,
627 numeric_arg(av, 3), 1);
631 call_infobox(CALLARGS)
634 return dialog_msgbox(t,
637 numeric_arg(av, 3), 0);
641 call_textbox(CALLARGS)
644 return dialog_textbox(t,
653 int tags = howmany_tags(av + 5, MENUBOX_TAGS);
654 *offset_add = 5 + tags * MENUBOX_TAGS;
656 return dialog_menu(t,
665 call_inputmenu(CALLARGS)
667 int tags = howmany_tags(av + 5, MENUBOX_TAGS);
668 bool free_extra_label = FALSE;
671 dialog_vars.input_menu = TRUE;
673 if (dialog_vars.max_input == 0)
674 dialog_vars.max_input = MAX_LEN / 2;
676 if (dialog_vars.extra_label == 0) {
677 free_extra_label = TRUE;
678 dialog_vars.extra_label = dlg_strclone(_("Rename"));
681 dialog_vars.extra_button = TRUE;
683 *offset_add = 5 + tags * MENUBOX_TAGS;
684 result = dialog_menu(t,
690 if (free_extra_label) {
691 free(dialog_vars.extra_label);
692 dialog_vars.extra_label = 0;
698 call_checklist(CALLARGS)
700 int tags = howmany_tags(av + 5, CHECKBOX_TAGS);
703 *offset_add = 5 + tags * CHECKBOX_TAGS;
704 code = dialog_checklist(t,
709 tags, av + 5, FLAG_CHECK);
714 call_radiolist(CALLARGS)
716 int tags = howmany_tags(av + 5, CHECKBOX_TAGS);
717 *offset_add = 5 + tags * CHECKBOX_TAGS;
718 return dialog_checklist(t,
723 tags, av + 5, FLAG_RADIO);
727 call_inputbox(CALLARGS)
729 *offset_add = arg_rest(av);
730 return dialog_inputbox(t,
734 optional_str(av, 4, 0), 0);
738 call_passwordbox(CALLARGS)
740 *offset_add = arg_rest(av);
741 return dialog_inputbox(t,
745 optional_str(av, 4, 0), 1);
750 call_calendar(CALLARGS)
752 *offset_add = arg_rest(av);
753 return dialog_calendar(t,
757 optional_num(av, 4, -1),
758 optional_num(av, 5, -1),
759 optional_num(av, 6, -1));
763 call_dselect(CALLARGS)
765 *offset_add = arg_rest(av);
766 return dialog_dselect(t,
773 call_editbox(CALLARGS)
776 return dialog_editbox(t,
783 call_fselect(CALLARGS)
785 *offset_add = arg_rest(av);
786 return dialog_fselect(t,
793 call_timebox(CALLARGS)
795 *offset_add = arg_rest(av);
796 return dialog_timebox(t,
800 optional_num(av, 4, -1),
801 optional_num(av, 5, -1),
802 optional_num(av, 6, -1));
804 #endif /* HAVE_XDIALOG */
806 #ifdef HAVE_DLG_FORMBOX
810 int group = FORMBOX_TAGS;
811 int tags = howmany_tags(av + 5, group);
812 *offset_add = 5 + tags * group;
814 return dialog_form(t,
823 call_password_form(CALLARGS)
825 unsigned save = dialog_vars.formitem_type;
828 dialog_vars.formitem_type = 1;
829 result = call_form(PASSARGS);
830 dialog_vars.formitem_type = save;
834 #endif /* HAVE_DLG_FORMBOX */
836 #ifdef HAVE_DLG_MIXEDFORM
838 call_mixed_form(CALLARGS)
840 int group = MIXEDFORM_TAGS;
841 int tags = howmany_tags(av + 5, group);
842 *offset_add = 5 + tags * group;
844 return dialog_mixedform(t,
851 #endif /* HAVE_DLG_MIXEDFORM */
853 #ifdef HAVE_DLG_GAUGE
857 *offset_add = arg_rest(av);
858 return dialog_gauge(t,
862 optional_num(av, 4, 0));
868 *offset_add = arg_rest(av);
869 return dialog_pause(t,
877 #ifdef HAVE_MIXEDGAUGE
879 call_mixed_gauge(CALLARGS)
881 #define MIXEDGAUGE_BASE 5
882 int tags = howmany_tags(av + MIXEDGAUGE_BASE, MIXEDGAUGE_TAGS);
883 *offset_add = MIXEDGAUGE_BASE + tags * MIXEDGAUGE_TAGS;
884 return dialog_mixedgauge(t,
889 tags, av + MIXEDGAUGE_BASE);
893 #ifdef HAVE_DLG_GAUGE
895 call_prgbox(CALLARGS)
897 *offset_add = arg_rest(av);
898 /* the original version does not accept a prompt string, but for
899 * consistency we allow it.
901 return ((*offset_add == 5)
906 numeric_arg(av, 4), TRUE)
911 numeric_arg(av, 3), TRUE));
915 #ifdef HAVE_DLG_GAUGE
917 call_programbox(CALLARGS)
921 *offset_add = arg_rest(av);
922 /* this function is a compromise between --prgbox and --progressbox.
924 result = ((*offset_add == 4)
930 dialog_state.pipe_input)
936 dialog_state.pipe_input));
937 dialog_state.pipe_input = 0;
942 #ifdef HAVE_DLG_GAUGE
944 call_progressbox(CALLARGS)
946 *offset_add = arg_rest(av);
947 /* the original version does not accept a prompt string, but for
948 * consistency we allow it.
950 return ((*offset_add == 4)
951 ? dialog_progressbox(t,
955 : dialog_progressbox(t,
958 numeric_arg(av, 2)));
962 #ifdef HAVE_DLG_TAILBOX
964 call_tailbox(CALLARGS)
967 return dialog_tailbox(t,
975 call_tailboxbg(CALLARGS)
978 return dialog_tailbox(t,
986 static const Mode modes[] =
988 {o_yesno, 4, 4, call_yesno},
989 {o_msgbox, 4, 4, call_msgbox},
990 {o_infobox, 4, 4, call_infobox},
991 {o_textbox, 4, 4, call_textbox},
992 {o_menu, 7, 0, call_menu},
993 {o_inputmenu, 7, 0, call_inputmenu},
994 {o_checklist, 8, 0, call_checklist},
995 {o_radiolist, 8, 0, call_radiolist},
996 {o_inputbox, 4, 5, call_inputbox},
997 {o_passwordbox, 4, 5, call_passwordbox},
998 #ifdef HAVE_DLG_GAUGE
999 {o_gauge, 4, 5, call_gauge},
1000 {o_pause, 5, 5, call_pause},
1001 {o_prgbox, 4, 5, call_prgbox},
1002 {o_programbox, 3, 4, call_programbox},
1003 {o_progressbox, 3, 4, call_progressbox},
1005 #ifdef HAVE_DLG_FORMBOX
1006 {o_passwordform, 13, 0, call_password_form},
1007 {o_form, 13, 0, call_form},
1009 #ifdef HAVE_MIXEDGAUGE
1010 {o_mixedgauge, MIXEDGAUGE_BASE, 0, call_mixed_gauge},
1012 #ifdef HAVE_DLG_MIXEDFORM
1013 {o_mixedform, 13, 0, call_mixed_form},
1015 #ifdef HAVE_DLG_TAILBOX
1016 {o_tailbox, 4, 4, call_tailbox},
1017 {o_tailboxbg, 4, 4, call_tailboxbg},
1020 {o_calendar, 4, 7, call_calendar},
1021 {o_dselect, 4, 5, call_dselect},
1022 {o_editbox, 4, 4, call_editbox},
1023 {o_fselect, 4, 5, call_fselect},
1024 {o_timebox, 4, 7, call_timebox},
1030 optionString(char **argv, int *num)
1032 int next = *num + 1;
1033 char *result = argv[next];
1036 sprintf(temp, "Expected a string-parameter for %.20s", argv[*num]);
1044 optionValue(char **argv, int *num)
1046 int next = *num + 1;
1047 char *src = argv[next];
1052 result = (int) strtol(src, &tmp, 0);
1053 if (tmp == 0 || *tmp != 0)
1059 sprintf(temp, "Expected a numeric-parameter for %.20s", argv[*num]);
1066 /* Return exit-code for a named button */
1068 button_code(const char *name)
1075 { "ok", DLG_EXIT_OK },
1076 { "yes", DLG_EXIT_OK },
1077 { "cancel", DLG_EXIT_CANCEL },
1078 { "no", DLG_EXIT_CANCEL },
1079 { "help", DLG_EXIT_HELP },
1080 { "extra", DLG_EXIT_EXTRA },
1084 int code = DLG_EXIT_ERROR;
1087 for (i = 0; i < (sizeof(table) / sizeof(table[0])); i++) {
1088 if (!dlg_strcmp(name, table[i].name)) {
1089 code = table[i].code;
1094 if (code == DLG_EXIT_ERROR) {
1096 sprintf(temp, "Button name \"%.20s\" unknown", name);
1104 * Print parts of a message
1107 PrintList(const char *const *list)
1109 const char *leaf = strrchr(program, '/');
1117 while (*list != 0) {
1118 fprintf(dialog_state.output, *list, n ? leaf : dialog_version());
1119 (void) fputc('\n', dialog_state.output);
1126 lookupMode(eOptions code)
1128 const Mode *modePtr = 0;
1131 for (n = 0; n < sizeof(modes) / sizeof(modes[0]); n++) {
1132 if (modes[n].code == code) {
1133 modePtr = &modes[n];
1141 compare_opts(const void *a, const void *b)
1143 Options *const *p = (Options * const *) a;
1144 Options *const *q = (Options * const *) b;
1145 return strcmp((*p)->name, (*q)->name);
1149 * Print program's version.
1152 PrintVersion(FILE *fp)
1154 fprintf(fp, "Version: %s\n", dialog_version());
1158 * Print program help-message
1163 static const char *const tbl_1[] =
1165 "cdialog (ComeOn Dialog!) version %s",
1166 "Copyright 2000-2011,2012 Thomas E. Dickey",
1167 "This is free software; see the source for copying conditions. There is NO",
1168 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.",
1170 "* Display dialog boxes from shell scripts *",
1172 "Usage: %s <options> { --and-widget <options> }",
1173 "where options are \"common\" options, followed by \"box\" options",
1177 " [--create-rc \"file\"]",
1183 "Auto-size with height and width = 0. Maximize with height and width = -1.",
1184 "Global-auto-size if also menu_height/list_height = 0.",
1187 size_t limit = sizeof(options) / sizeof(options[0]);
1189 const Options **opts;
1192 dialog_state.output = stdout;
1194 opts = dlg_calloc(const Options *, limit);
1195 assert_ptr(opts, "Help");
1196 for (j = 0; j < limit; ++j) {
1197 opts[j] = &(options[j]);
1199 qsort(opts, limit, sizeof(Options *), compare_opts);
1202 fprintf(dialog_state.output, "Common options:\n ");
1203 for (j = k = 0; j < limit; j++) {
1204 if ((opts[j]->pass & 1)
1205 && opts[j]->help != 0) {
1206 size_t len = 6 + strlen(opts[j]->name) + strlen(opts[j]->help);
1209 fprintf(dialog_state.output, "\n ");
1212 fprintf(dialog_state.output, " [--%s%s%s]", opts[j]->name,
1213 *(opts[j]->help) ? " " : "", opts[j]->help);
1216 fprintf(dialog_state.output, "\nBox options:\n");
1217 for (j = 0; j < limit; j++) {
1218 if ((opts[j]->pass & 2) != 0
1219 && opts[j]->help != 0
1220 && lookupMode(opts[j]->code))
1221 fprintf(dialog_state.output, " --%-12s %s\n", opts[j]->name,
1227 dlg_exit(DLG_EXIT_OK);
1230 #ifdef HAVE_DLG_TRACE
1232 * Only the first call to dlg_trace will open a trace file. But each time
1233 * --trace is parsed, we show the whole parameter list as it is at that moment,
1234 * counting discarded parameters. The only way to capture the whole parameter
1235 * list is if --trace is the first option.
1238 process_trace_option(char **argv, int *offset)
1242 if (dialog_state.trace_output == 0)
1243 dlg_trace(optionString(argv, offset));
1245 dlg_trace_msg("# Parameters:\n");
1246 for (j = 0; argv[j] != 0; ++j) {
1247 dlg_trace_msg("# argv[%d] = %s\n", j, argv[j]);
1254 * "Common" options apply to all widgets more/less. Most of the common options
1255 * set values in dialog_vars, a few set dialog_state and a couple write to the
1259 process_common_options(int argc, char **argv, int offset, bool output)
1263 dlg_trace_msg("# process_common_options, offset %d\n", offset);
1265 while (offset < argc && !done) { /* Common options */
1266 dlg_trace_msg("#\targv[%d] = %s\n", offset, argv[offset]);
1267 switch (lookupOption(argv[offset], 1)) {
1269 dialog_vars.title = optionString(argv, &offset);
1272 dialog_vars.backtitle = optionString(argv, &offset);
1274 case o_separate_widget:
1275 dialog_state.separate_str = optionString(argv, &offset);
1277 case o_separate_output:
1278 dialog_vars.separate_output = TRUE;
1281 dialog_vars.colors = TRUE;
1284 dialog_vars.cr_wrap = TRUE;
1286 case o_no_nl_expand:
1287 dialog_vars.no_nl_expand = TRUE;
1290 dialog_vars.nocollapse = TRUE;
1293 dialog_vars.cant_kill = TRUE;
1296 dialog_vars.nocancel = TRUE;
1299 dialog_vars.nook = TRUE;
1302 dialog_vars.quoted = TRUE;
1304 case o_single_quoted:
1305 dialog_vars.single_quoted = TRUE;
1308 dialog_vars.size_err = TRUE;
1311 dialog_vars.beep_signal = TRUE;
1314 dialog_vars.beep_after_signal = TRUE;
1317 dialog_state.use_scrollbar = TRUE;
1320 dialog_state.use_shadow = TRUE;
1323 dialog_vars.defaultno = TRUE;
1324 dialog_vars.default_button = DLG_EXIT_CANCEL;
1326 case o_default_button:
1327 dialog_vars.default_button = button_code(optionString(argv, &offset));
1328 dialog_vars.defaultno = dialog_vars.default_button == DLG_EXIT_CANCEL;
1330 case o_default_item:
1331 dialog_vars.default_item = optionString(argv, &offset);
1334 dialog_vars.insecure = TRUE;
1337 dialog_vars.item_help = TRUE;
1340 dialog_vars.help_line = optionString(argv, &offset);
1343 dialog_vars.help_file = optionString(argv, &offset);
1346 dialog_vars.help_button = TRUE;
1349 dialog_vars.help_status = TRUE;
1351 case o_extra_button:
1352 dialog_vars.extra_button = TRUE;
1355 ignore_unknown = TRUE;
1358 dialog_vars.keep_window = TRUE;
1361 dialog_state.use_shadow = FALSE;
1364 dialog_vars.print_siz = TRUE;
1366 case o_print_maxsize:
1369 * If this is the last option, we do not want any error
1370 * messages - just our output. Calling end_dialog() cancels
1371 * the refresh() at the end of the program as well.
1373 if (argv[offset + 1] == 0) {
1374 ignore_unknown = TRUE;
1377 fflush(dialog_state.output);
1378 fprintf(dialog_state.output, "MaxSize: %d, %d\n", SLINES, SCOLS);
1381 case o_print_version:
1383 PrintVersion(dialog_state.output);
1387 case o_output_separator:
1388 dialog_vars.output_separator = optionString(argv, &offset);
1390 case o_column_separator:
1391 dialog_vars.column_separator = optionString(argv, &offset);
1394 dialog_vars.tab_correct = TRUE;
1397 dialog_vars.sleep_secs = optionValue(argv, &offset);
1400 dialog_vars.timeout_secs = optionValue(argv, &offset);
1403 dialog_vars.max_input = optionValue(argv, &offset);
1406 dialog_state.tab_len = optionValue(argv, &offset);
1409 dialog_vars.trim_whitespace = TRUE;
1412 dialog_state.visit_items = TRUE;
1415 dialog_state.aspect_ratio = optionValue(argv, &offset);
1418 dialog_vars.begin_set = TRUE;
1419 dialog_vars.begin_y = optionValue(argv, &offset);
1420 dialog_vars.begin_x = optionValue(argv, &offset);
1423 dialog_vars.dlg_clear_screen = TRUE;
1426 dialog_vars.yes_label = optionString(argv, &offset);
1429 dialog_vars.no_label = optionString(argv, &offset);
1432 dialog_vars.ok_label = optionString(argv, &offset);
1434 case o_cancel_label:
1435 dialog_vars.cancel_label = optionString(argv, &offset);
1438 dialog_vars.extra_label = optionString(argv, &offset);
1441 dialog_vars.exit_label = optionString(argv, &offset);
1444 dialog_vars.help_label = optionString(argv, &offset);
1447 dialog_vars.date_format = optionString(argv, &offset);
1450 dialog_vars.time_format = optionString(argv, &offset);
1453 dialog_vars.keep_tite = TRUE;
1456 dialog_vars.ascii_lines = TRUE;
1457 dialog_vars.no_lines = FALSE;
1460 dialog_vars.no_lines = TRUE;
1461 dialog_vars.ascii_lines = FALSE;
1464 dialog_state.no_mouse = TRUE;
1471 /* options of Xdialog which we ignore */
1474 (void) optionString(argv, &offset);
1477 case o_auto_placement:
1482 case o_screen_center:
1490 default: /* no more common options */
1493 #ifdef HAVE_DLG_TRACE
1495 process_trace_option(argv, &offset);
1506 * Initialize options at the start of a series of common options culminating
1510 init_result(char *buffer)
1512 static bool first = TRUE;
1513 static char **special_argv = 0;
1514 static int special_argc = 0;
1516 dlg_trace_msg("# init_result\n");
1518 /* clear everything we do not save for the next widget */
1519 memset(&dialog_vars, 0, sizeof(dialog_vars));
1521 dialog_vars.input_result = buffer;
1522 dialog_vars.input_result[0] = '\0';
1524 dialog_vars.default_button = -1;
1527 * The first time this is called, check for common options given by an
1528 * environment variable.
1531 char *env = getenv("DIALOGOPTS");
1533 env = dlg_strclone(env);
1535 special_argv = dlg_string_to_argv(env);
1536 special_argc = dlg_count_argv(special_argv);
1542 * If we are not checking memory leaks, just do the parse of the
1545 if (special_argv != 0) {
1546 process_common_options(special_argc, special_argv, 0, FALSE);
1548 free(special_argv[0]);
1558 main(int argc, char *argv[])
1561 bool esc_pressed = FALSE;
1562 bool keep_tite = FALSE;
1565 int retval = DLG_EXIT_OK;
1568 const Mode *modePtr;
1569 char my_buffer[MAX_LEN + 1];
1571 memset(&dialog_state, 0, sizeof(dialog_state));
1572 memset(&dialog_vars, 0, sizeof(dialog_vars));
1574 #if defined(ENABLE_NLS)
1575 /* initialize locale support */
1576 setlocale(LC_ALL, "");
1577 bindtextdomain(NLS_TEXTDOMAIN, LOCALEDIR);
1578 textdomain(NLS_TEXTDOMAIN);
1579 #elif defined(HAVE_SETLOCALE)
1580 (void) setlocale(LC_ALL, "");
1583 unescape_argv(&argc, &argv);
1585 dialog_state.output = stderr;
1586 dialog_state.input = stdin;
1589 * Look for the last --stdout, --stderr or --output-fd option, and use
1590 * that. We can only write to one of them. If --stdout is used, that
1591 * can interfere with initializing the curses library, so we want to
1592 * know explicitly if it is used.
1594 * Also, look for any --version or --help message, processing those
1597 while (offset < argc) {
1599 switch (lookupOption(argv[offset], 7)) {
1601 dialog_state.output = stdout;
1604 dialog_state.output = stderr;
1607 if ((j = optionValue(argv, &offset)) < 0
1608 || (dialog_state.input = fdopen(j, "r")) == 0)
1609 dlg_exiterr("Cannot open input-fd\n");
1612 if ((j = optionValue(argv, &offset)) < 0
1613 || (dialog_state.output = fdopen(j, "w")) == 0)
1614 dlg_exiterr("Cannot open output-fd\n");
1620 dialog_state.output = stdout;
1621 PrintVersion(dialog_state.output);
1627 #ifdef HAVE_DLG_TRACE
1630 * Process/remove the --trace option if it is the first option.
1631 * Otherwise, process it in more/less expected order as a
1635 process_trace_option(argv, &offset);
1646 dlg_trace_msg("# discarding %d parameters starting with argv[%d] (%s)\n",
1647 1 + offset - base, base,
1649 for (j = base; j < argc; ++j) {
1650 dialog_argv[j] = dialog_argv[j + 1 + (offset - base)];
1651 if (dialog_opts != 0)
1652 dialog_opts[j] = dialog_opts[j + 1 + (offset - base)];
1654 argc -= (1 + offset - base);
1658 init_result(my_buffer);
1661 * Dialog's output may be redirected (see above). Handle the special
1662 * case of options that only report information without interaction.
1665 switch (lookupOption(argv[1], 7)) {
1666 case o_print_maxsize:
1669 fflush(dialog_state.output);
1670 fprintf(dialog_state.output, "MaxSize: %d, %d\n", SLINES, SCOLS);
1672 case o_print_version:
1673 PrintVersion(dialog_state.output);
1693 if (lookupOption(argv[1], 7) == o_create_rc) {
1695 sprintf(temp, "Expected a filename for %.50s", argv[1]);
1698 if (dlg_parse_rc() == -1) /* Read the configuration file */
1699 dlg_exiterr("dialog: dlg_parse_rc");
1700 dlg_create_rc(argv[2]);
1705 dialog_vars.keep_tite = keep_tite; /* init_result() cleared global */
1707 init_dialog(dialog_state.input, dialog_state.output);
1709 while (offset < argc && !esc_pressed) {
1710 init_result(my_buffer);
1712 offset = process_common_options(argc, argv, offset, TRUE);
1714 if (argv[offset] == NULL) {
1717 Usage("Expected a box option");
1720 if (lookupOption(argv[offset], 2) != o_checklist
1721 && dialog_vars.separate_output) {
1722 sprintf(temp, "Expected --checklist, not %.20s", argv[offset]);
1726 if (dialog_state.aspect_ratio == 0)
1727 dialog_state.aspect_ratio = DEFAULT_ASPECT_RATIO;
1729 dlg_put_backtitle();
1731 /* use a table to look for the requested mode, to avoid code duplication */
1734 if ((code = lookupOption(argv[offset], 2)) != o_unknown)
1735 modePtr = lookupMode(code);
1737 sprintf(temp, "%s option %.20s",
1738 lookupOption(argv[offset], 7) != o_unknown
1745 have = arg_rest(&argv[offset]);
1746 if (have < modePtr->argmin) {
1747 sprintf(temp, "Expected at least %d tokens for %.20s, have %d",
1748 modePtr->argmin - 1, argv[offset],
1752 if (modePtr->argmax && have > modePtr->argmax) {
1754 "Expected no more than %d tokens for %.20s, have %d",
1755 modePtr->argmax - 1, argv[offset],
1761 * Trim whitespace from non-title option values, e.g., the ones that
1762 * will be used as captions or prompts. Do that only for the widget
1763 * we are about to process, since the "--trim" option is reset before
1764 * accumulating options for each widget.
1766 for (j = offset + 1; j <= offset + have; j++) {
1767 switch (lookupOption(argv[j - 1], 7)) {
1776 dlg_trim_string(argv[j]);
1782 retval = show_result((*(modePtr->jumper)) (dialog_vars.title,
1785 dlg_trace_msg("# widget returns %d\n", retval);
1786 offset += offset_add;
1788 if (dialog_vars.input_result != my_buffer) {
1789 free(dialog_vars.input_result);
1790 dialog_vars.input_result = 0;
1793 if (retval == DLG_EXIT_ESC) {
1797 if (dialog_vars.beep_after_signal)
1800 if (dialog_vars.sleep_secs)
1801 (void) napms(dialog_vars.sleep_secs * 1000);
1803 if (offset < argc) {
1804 switch (lookupOption(argv[offset], 7)) {
1809 sprintf(temp, "Expected --and-widget, not %.20s",
1814 /* if we got a cancel, etc., stop chaining */
1815 if (retval != DLG_EXIT_OK)
1818 dialog_vars.dlg_clear_screen = TRUE;
1822 if (dialog_vars.dlg_clear_screen)
1827 dlg_killall_bg(&retval);
1828 if (dialog_state.screen_initialized) {