2 * $Id: dialog.c,v 1.193 2011/06/29 09:10:56 tom Exp $
4 * cdialog - Display simple dialog boxes from shell scripts
6 * Copyright 2000-2010,2011 Thomas E. Dickey
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU Lesser General Public License, version 2.1
10 * as published by the Free Software Foundation.
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this program; if not, write to
19 * Free Software Foundation, Inc.
20 * 51 Franklin St., Fifth Floor
21 * Boston, MA 02110, USA.
23 * An earlier version of this program lists as authors
24 * Savio Lam (lam836@cs.cuhk.hk)
29 #include <sys/types.h>
36 #define PASSARGS t, av, offset_add
37 #define CALLARGS const char *t, char *av[], int *offset_add
38 typedef int (callerFn) (CALLARGS);
148 #ifdef HAVE_DLG_TRACE
154 * The bits in 'pass' are used to decide which options are applicable at
155 * different stages in the program:
156 * 1 flags before widgets
158 * 4 non-widget options
163 int pass; /* 1,2,4 or combination */
164 const char *help; /* NULL to suppress, non-empty to display params */
173 static bool *dialog_opts;
174 static char **dialog_argv;
176 static bool ignore_unknown = FALSE;
178 static const char *program = "dialog";
181 * The options[] table is organized this way to make it simple to maintain
182 * a sorted list of options for the help-message.
185 static const Options options[] = {
186 { "allow-close", o_allow_close, 1, NULL },
187 { "and-widget", o_and_widget, 4, NULL },
188 { "ascii-lines", o_ascii_lines, 1, "" },
189 { "aspect", o_aspect, 1, "<ratio>" },
190 { "auto-placement", o_auto_placement, 1, NULL },
191 { "backtitle", o_backtitle, 1, "<backtitle>" },
192 { "beep", o_beep, 1, NULL },
193 { "beep-after", o_beep_after, 1, NULL },
194 { "begin", o_begin, 1, "<y> <x>" },
195 { "calendar", o_calendar, 2, "<text> <height> <width> <day> <month> <year>" },
196 { "cancel-label", o_cancel_label, 1, "<str>" },
197 { "checklist", o_checklist, 2, "<text> <height> <width> <list height> <tag1> <item1> <status1>..." },
198 { "clear", o_clear, 1, "" },
199 { "colors", o_colors, 1, "" },
200 { "column-separator",o_column_separator, 1, "<str>" },
201 { "cr-wrap", o_cr_wrap, 1, "" },
202 { "create-rc", o_create_rc, 1, NULL },
203 { "date-format", o_date_format, 1, "<str>" },
204 { "default-item", o_default_item, 1, "<str>" },
205 { "defaultno", o_defaultno, 1, "" },
206 { "dselect", o_dselect, 2, "<directory> <height> <width>" },
207 { "editbox", o_editbox, 2, "<file> <height> <width>" },
208 { "exit-label", o_exit_label, 1, "<str>" },
209 { "extra-button", o_extra_button, 1, "" },
210 { "extra-label", o_extra_label, 1, "<str>" },
211 { "fb", o_fullbutton, 1, NULL },
212 { "fixed-font", o_fixed_font, 1, NULL },
213 { "form", o_form, 2, "<text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1>..." },
214 { "fselect", o_fselect, 2, "<filepath> <height> <width>" },
215 { "fullbutton", o_fullbutton, 1, NULL },
216 { "gauge", o_gauge, 2, "<text> <height> <width> [<percent>]" },
217 { "guage", o_gauge, 2, NULL },
218 { "help", o_help, 4, "" },
219 { "help-button", o_help_button, 1, "" },
220 { "help-label", o_help_label, 1, "<str>" },
221 { "help-status", o_help_status, 1, "" },
222 { "hfile", o_help_file, 1, "<str>" },
223 { "hline", o_help_line, 1, "<str>" },
224 { "icon", o_icon, 1, NULL },
225 { "ignore", o_ignore, 1, "" },
226 { "infobox", o_infobox, 2, "<text> <height> <width>" },
227 { "input-fd", o_input_fd, 1, "<fd>" },
228 { "inputbox", o_inputbox, 2, "<text> <height> <width> [<init>]" },
229 { "inputmenu", o_inputmenu, 2, "<text> <height> <width> <menu height> <tag1> <item1>..." },
230 { "insecure", o_insecure, 1, "" },
231 { "item-help", o_item_help, 1, "" },
232 { "keep-colors", o_keep_colors, 1, NULL },
233 { "keep-tite", o_keep_tite, 1, "" },
234 { "keep-window", o_keep_window, 1, "" },
235 { "max-input", o_max_input, 1, "<n>" },
236 { "menu", o_menu, 2, "<text> <height> <width> <menu height> <tag1> <item1>..." },
237 { "mixedform", o_mixedform, 2, "<text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1> <itype>..." },
238 { "mixedgauge", o_mixedgauge, 2, "<text> <height> <width> <percent> <tag1> <item1>..." },
239 { "msgbox", o_msgbox, 2, "<text> <height> <width>" },
240 { "no-cancel", o_nocancel, 1, "" },
241 { "no-close", o_no_close, 1, NULL },
242 { "no-collapse", o_no_collapse, 1, "" },
243 { "no-cr-wrap", o_no_cr_wrap, 1, NULL },
244 { "no-kill", o_no_kill, 1, "" },
245 { "no-label", o_no_label, 1, "<str>" },
246 { "no-lines", o_no_lines, 1, "" },
247 { "no-mouse", o_no_mouse, 1, "" },
248 { "no-nl-expand", o_no_nl_expand, 1, "" },
249 { "no-ok", o_nook, 1, "" },
250 { "no-shadow", o_no_shadow, 1, "" },
251 { "nocancel", o_nocancel, 1, NULL }, /* see --no-cancel */
252 { "noitem", o_noitem, 1, NULL },
253 { "nook", o_nook, 1, "" }, /* See no-ok */
254 { "ok-label", o_ok_label, 1, "<str>" },
255 { "output-fd", o_output_fd, 1, "<fd>" },
256 { "output-separator",o_output_separator, 1, "<str>" },
257 { "passwordbox", o_passwordbox, 2, "<text> <height> <width> [<init>]" },
258 { "passwordform", o_passwordform, 2, "<text> <height> <width> <form height> <label1> <l_y1> <l_x1> <item1> <i_y1> <i_x1> <flen1> <ilen1>..." },
259 { "pause", o_pause, 2, "<text> <height> <width> <seconds>" },
260 { "prgbox", o_prgbox, 2, "<text> <command> <height> <width>" },
261 { "print-maxsize", o_print_maxsize, 1, "" },
262 { "print-size", o_print_size, 1, "" },
263 { "print-version", o_print_version, 5, "" },
264 { "programbox", o_programbox, 2, "<text> <height> <width>" },
265 { "progressbox", o_progressbox, 2, "<text> <height> <width>" },
266 { "quoted", o_quoted, 1, "" },
267 { "radiolist", o_radiolist, 2, "<text> <height> <width> <list height> <tag1> <item1> <status1>..." },
268 { "screen-center", o_screen_center, 1, NULL },
269 { "scrollbar", o_scrollbar, 1, "" },
270 { "separate-output",o_separate_output, 1, "" },
271 { "separate-widget",o_separate_widget, 1, "<str>" },
272 { "separator", o_separator, 1, NULL },
273 { "shadow", o_shadow, 1, "" },
274 { "single-quoted", o_single_quoted, 1, "" },
275 { "size-err", o_size_err, 1, "" },
276 { "sleep", o_sleep, 1, "<secs>" },
277 { "smooth", o_smooth, 1, NULL },
278 { "stderr", o_stderr, 1, "" },
279 { "stdout", o_stdout, 1, "" },
280 { "tab-correct", o_tab_correct, 1, "" },
281 { "tab-len", o_tab_len, 1, "<n>" },
282 { "tailbox", o_tailbox, 2, "<file> <height> <width>" },
283 { "tailboxbg", o_tailboxbg, 2, "<file> <height> <width>" },
284 { "textbox", o_textbox, 2, "<file> <height> <width>" },
285 { "time-format", o_time_format, 1, "<str>" },
286 { "timebox", o_timebox, 2, "<text> <height> <width> <hour> <minute> <second>" },
287 { "timeout", o_timeout, 1, "<secs>" },
288 { "title", o_title, 1, "<title>" },
289 { "trim", o_trim, 1, "" },
290 { "under-mouse", o_under_mouse, 1, NULL },
291 { "version", o_version, 5, "" },
292 { "visit-items", o_visit_items, 1, "" },
293 { "wmclass", o_wmclass, 1, NULL },
294 { "yes-label", o_yes_label, 1, "<str>" },
295 { "yesno", o_yesno, 2, "<text> <height> <width>" },
296 #ifdef HAVE_DLG_TRACE
297 { "trace", o_trace, 1, "<file>" },
303 * Make an array showing which argv[] entries are options. Use "--" as a
304 * special token to escape the next argument, allowing it to begin with "--".
305 * When we find a "--" argument, also remove it from argv[] and adjust argc.
306 * That appears to be an undocumented feature of the popt library.
308 * Also, if we see a "--file", expand it into the parameter list by reading the
309 * text from the given file and stripping quotes, treating whitespace outside
310 * quotes as a parameter delimiter.
312 * Finally, if we see a "--args", dump the current list of arguments to the
313 * standard error. This is used for debugging complex --file combinations.
316 unescape_argv(int *argcp, char ***argvp)
319 int limit_includes = 20 + *argcp;
320 int count_includes = 0;
321 bool changed = FALSE;
322 bool doalloc = FALSE;
325 dialog_opts = dlg_calloc(bool, (size_t) *argcp + 1);
326 assert_ptr(dialog_opts, "unescape_argv");
328 for (j = 1; j < *argcp; j++) {
329 bool escaped = FALSE;
330 if (!strcmp((*argvp)[j], "--")) {
332 changed = dlg_eat_argv(argcp, argvp, j, 1);
333 } else if (!strcmp((*argvp)[j], "--args")) {
334 fprintf(stderr, "Showing arguments at arg%d\n", j);
335 for (k = 0; k < *argcp; ++k) {
336 fprintf(stderr, " arg%d:%s\n", k, (*argvp)[k]);
338 changed = dlg_eat_argv(argcp, argvp, j, 1);
339 } else if (!strcmp((*argvp)[j], "--file")) {
340 if (++count_includes > limit_includes)
341 dlg_exiterr("Too many --file options");
343 if ((filename = (*argvp)[j + 1]) != 0) {
352 if (*filename == '&') {
353 fp = fdopen(atoi(filename + sizeof(char)), "r");
355 fp = fopen(filename, "r");
362 blob = dlg_realloc(char, length + BUFSIZ + 1, blob);
363 assert_ptr(blob, "unescape_argv");
364 bytes_read = fread(blob + length,
368 length += bytes_read;
370 dlg_exiterr("error on filehandle in unescape_argv");
371 } while (bytes_read == BUFSIZ);
376 list = dlg_string_to_argv(blob);
377 if ((added = dlg_count_argv(list)) != 0) {
379 size_t need = (size_t) (*argcp + added + 1);
381 *argvp = dlg_realloc(char *, need, *argvp);
382 assert_ptr(*argvp, "unescape_argv");
384 char **newp = dlg_malloc(char *, need);
385 assert_ptr(newp, "unescape_argv");
386 for (n = 0; n < *argcp; ++n) {
387 newp[n] = (*argvp)[n];
392 dialog_opts = dlg_realloc(bool, need, dialog_opts);
393 assert_ptr(dialog_opts, "unescape_argv");
395 for (n = *argcp; n >= j + 2; --n) {
396 (*argvp)[n + added - 2] = (*argvp)[n];
397 dialog_opts[n + added - 2] = dialog_opts[n];
399 for (n = 0; n < added; ++n) {
400 (*argvp)[n + j] = list[n];
401 dialog_opts[n + j] = FALSE;
407 dlg_exiterr("Cannot open --file %s", filename);
409 (*argvp)[*argcp] = 0;
413 dlg_exiterr("No value given for --file");
418 && !strncmp((*argvp)[j], "--", (size_t) 2)
419 && isalpha(UCH((*argvp)[j][2]))) {
420 dialog_opts[j] = TRUE;
424 /* if we didn't find any "--" tokens, there's no reason to do the table
425 * lookup in isOption()
431 dialog_argv = (*argvp);
435 * Check if the given string from main's argv is an option.
438 isOption(const char *arg)
443 if (dialog_opts != 0) {
445 for (n = 1; dialog_argv[n] != 0; ++n) {
446 if (dialog_argv[n] == arg) {
447 result = dialog_opts[n];
451 } else if (!strncmp(arg, "--", (size_t) 2) && isalpha(UCH(arg[2]))) {
459 lookupOption(const char *name, int pass)
463 if (isOption(name)) {
465 for (n = 0; n < sizeof(options) / sizeof(options[0]); n++) {
466 if ((pass & options[n].pass) != 0
467 && !strcmp(name, options[n].name)) {
468 return options[n].code;
476 Usage(const char *msg)
478 dlg_exiterr("Error: %s.\nUse --help to list options.\n\n", msg);
482 * Count arguments, stopping at the end of the argument list, or on any of our
486 arg_rest(char *argv[])
488 int i = 1; /* argv[0] points to a "--" token */
491 && (!isOption(argv[i]) || lookupOption(argv[i], 7) == o_unknown))
497 * In MultiWidget this function is needed to count how many tags
498 * a widget (menu, checklist, radiolist) has
501 howmany_tags(char *argv[], int group)
505 const char *format = "Expected %d arguments, found only %d";
508 while (argv[0] != 0) {
509 if (isOption(argv[0]))
511 if ((have = arg_rest(argv)) < group) {
512 sprintf(temp, format, group, have);
524 numeric_arg(char **av, int n)
527 int result = (int) strtol(av[n], &last, 10);
530 if (last == 0 || *last != 0) {
531 sprintf(msg, "Expected a number for token %d of %.20s", n, av[0]);
538 optional_str(char **av, int n, char *dft)
541 if (arg_rest(av) > n)
546 #if defined(HAVE_DLG_GAUGE) || defined(HAVE_XDIALOG)
548 optional_num(char **av, int n, int dft)
551 if (arg_rest(av) > n)
552 ret = numeric_arg(av, n);
558 * On AIX 4.x, we have to flush the output right away since there is a bug in
559 * the curses package which discards stdout even when we've used newterm to
560 * redirect output to /dev/tty.
571 case DLG_EXIT_ITEM_HELP:
572 if ((dialog_state.output_count > 1) && !dialog_vars.separate_output) {
573 fputs((dialog_state.separate_str
574 ? dialog_state.separate_str
575 : DEFAULT_SEPARATE_STR),
576 dialog_state.output);
579 if (dialog_vars.input_result != 0
580 && dialog_vars.input_result[0] != '\0') {
581 fputs(dialog_vars.input_result, dialog_state.output);
585 fflush(dialog_state.output);
593 * These are the widget callers.
600 return dialog_yesno(t,
607 call_msgbox(CALLARGS)
610 return dialog_msgbox(t,
613 numeric_arg(av, 3), 1);
617 call_infobox(CALLARGS)
620 return dialog_msgbox(t,
623 numeric_arg(av, 3), 0);
627 call_textbox(CALLARGS)
630 return dialog_textbox(t,
639 int tags = howmany_tags(av + 5, MENUBOX_TAGS);
640 *offset_add = 5 + tags * MENUBOX_TAGS;
642 return dialog_menu(t,
651 call_inputmenu(CALLARGS)
653 int tags = howmany_tags(av + 5, MENUBOX_TAGS);
654 bool free_extra_label = FALSE;
657 dialog_vars.input_menu = TRUE;
659 if (dialog_vars.max_input == 0)
660 dialog_vars.max_input = MAX_LEN / 2;
662 if (dialog_vars.extra_label == 0) {
663 free_extra_label = TRUE;
664 dialog_vars.extra_label = dlg_strclone(_("Rename"));
667 dialog_vars.extra_button = TRUE;
669 *offset_add = 5 + tags * MENUBOX_TAGS;
670 result = dialog_menu(t,
676 if (free_extra_label) {
677 free(dialog_vars.extra_label);
678 dialog_vars.extra_label = 0;
684 call_checklist(CALLARGS)
686 int tags = howmany_tags(av + 5, CHECKBOX_TAGS);
688 bool save_quoted = dialog_vars.quoted;
690 dialog_vars.quoted = !dialog_vars.separate_output;
691 *offset_add = 5 + tags * CHECKBOX_TAGS;
692 code = dialog_checklist(t,
697 tags, av + 5, FLAG_CHECK);
698 dialog_vars.quoted = save_quoted;
703 call_radiolist(CALLARGS)
705 int tags = howmany_tags(av + 5, CHECKBOX_TAGS);
706 *offset_add = 5 + tags * CHECKBOX_TAGS;
707 return dialog_checklist(t,
712 tags, av + 5, FLAG_RADIO);
716 call_inputbox(CALLARGS)
718 *offset_add = arg_rest(av);
719 return dialog_inputbox(t,
723 optional_str(av, 4, 0), 0);
727 call_passwordbox(CALLARGS)
729 *offset_add = arg_rest(av);
730 return dialog_inputbox(t,
734 optional_str(av, 4, 0), 1);
739 call_calendar(CALLARGS)
741 *offset_add = arg_rest(av);
742 return dialog_calendar(t,
746 optional_num(av, 4, -1),
747 optional_num(av, 5, -1),
748 optional_num(av, 6, -1));
752 call_dselect(CALLARGS)
754 *offset_add = arg_rest(av);
755 return dialog_dselect(t,
762 call_editbox(CALLARGS)
765 return dialog_editbox(t,
772 call_fselect(CALLARGS)
774 *offset_add = arg_rest(av);
775 return dialog_fselect(t,
782 call_timebox(CALLARGS)
784 *offset_add = arg_rest(av);
785 return dialog_timebox(t,
789 optional_num(av, 4, -1),
790 optional_num(av, 5, -1),
791 optional_num(av, 6, -1));
793 #endif /* HAVE_XDIALOG */
795 #ifdef HAVE_DLG_FORMBOX
799 int group = FORMBOX_TAGS;
800 int tags = howmany_tags(av + 5, group);
801 *offset_add = 5 + tags * group;
803 return dialog_form(t,
812 call_password_form(CALLARGS)
814 unsigned save = dialog_vars.formitem_type;
817 dialog_vars.formitem_type = 1;
818 result = call_form(PASSARGS);
819 dialog_vars.formitem_type = save;
823 #endif /* HAVE_DLG_FORMBOX */
825 #ifdef HAVE_DLG_MIXEDFORM
827 call_mixed_form(CALLARGS)
829 int group = MIXEDFORM_TAGS;
830 int tags = howmany_tags(av + 5, group);
831 *offset_add = 5 + tags * group;
833 return dialog_mixedform(t,
840 #endif /* HAVE_DLG_MIXEDFORM */
842 #ifdef HAVE_DLG_GAUGE
846 *offset_add = arg_rest(av);
847 return dialog_gauge(t,
851 optional_num(av, 4, 0));
857 *offset_add = arg_rest(av);
858 return dialog_pause(t,
866 #ifdef HAVE_MIXEDGAUGE
868 call_mixed_gauge(CALLARGS)
870 #define MIXEDGAUGE_BASE 5
871 int tags = howmany_tags(av + MIXEDGAUGE_BASE, MIXEDGAUGE_TAGS);
872 *offset_add = MIXEDGAUGE_BASE + tags * MIXEDGAUGE_TAGS;
873 return dialog_mixedgauge(t,
878 tags, av + MIXEDGAUGE_BASE);
882 #ifdef HAVE_DLG_GAUGE
884 call_prgbox(CALLARGS)
886 *offset_add = arg_rest(av);
887 /* the original version does not accept a prompt string, but for
888 * consistency we allow it.
890 return ((*offset_add == 5)
895 numeric_arg(av, 4), TRUE)
900 numeric_arg(av, 3), TRUE));
904 #ifdef HAVE_DLG_GAUGE
906 call_programbox(CALLARGS)
910 *offset_add = arg_rest(av);
911 /* this function is a compromise between --prgbox and --progressbox.
913 result = ((*offset_add == 4)
919 dialog_state.pipe_input)
925 dialog_state.pipe_input));
926 dialog_state.pipe_input = 0;
931 #ifdef HAVE_DLG_GAUGE
933 call_progressbox(CALLARGS)
935 *offset_add = arg_rest(av);
936 /* the original version does not accept a prompt string, but for
937 * consistency we allow it.
939 return ((*offset_add == 4)
940 ? dialog_progressbox(t,
944 : dialog_progressbox(t,
947 numeric_arg(av, 2)));
951 #ifdef HAVE_DLG_TAILBOX
953 call_tailbox(CALLARGS)
956 return dialog_tailbox(t,
964 call_tailboxbg(CALLARGS)
967 return dialog_tailbox(t,
975 static const Mode modes[] =
977 {o_yesno, 4, 4, call_yesno},
978 {o_msgbox, 4, 4, call_msgbox},
979 {o_infobox, 4, 4, call_infobox},
980 {o_textbox, 4, 4, call_textbox},
981 {o_menu, 7, 0, call_menu},
982 {o_inputmenu, 7, 0, call_inputmenu},
983 {o_checklist, 8, 0, call_checklist},
984 {o_radiolist, 8, 0, call_radiolist},
985 {o_inputbox, 4, 5, call_inputbox},
986 {o_passwordbox, 4, 5, call_passwordbox},
987 #ifdef HAVE_DLG_GAUGE
988 {o_gauge, 4, 5, call_gauge},
989 {o_pause, 5, 5, call_pause},
990 {o_prgbox, 4, 5, call_prgbox},
991 {o_programbox, 3, 4, call_programbox},
992 {o_progressbox, 3, 4, call_progressbox},
994 #ifdef HAVE_DLG_FORMBOX
995 {o_passwordform, 13, 0, call_password_form},
996 {o_form, 13, 0, call_form},
998 #ifdef HAVE_MIXEDGAUGE
999 {o_mixedgauge, MIXEDGAUGE_BASE, 0, call_mixed_gauge},
1001 #ifdef HAVE_DLG_MIXEDFORM
1002 {o_mixedform, 13, 0, call_mixed_form},
1004 #ifdef HAVE_DLG_TAILBOX
1005 {o_tailbox, 4, 4, call_tailbox},
1006 {o_tailboxbg, 4, 4, call_tailboxbg},
1009 {o_calendar, 4, 7, call_calendar},
1010 {o_dselect, 4, 5, call_dselect},
1011 {o_editbox, 4, 4, call_editbox},
1012 {o_fselect, 4, 5, call_fselect},
1013 {o_timebox, 4, 7, call_timebox},
1019 optionString(char **argv, int *num)
1021 int next = *num + 1;
1022 char *result = argv[next];
1025 sprintf(temp, "Expected a string-parameter for %.20s", argv[*num]);
1033 optionValue(char **argv, int *num)
1035 int next = *num + 1;
1036 char *src = argv[next];
1041 result = (int) strtol(src, &tmp, 0);
1042 if (tmp == 0 || *tmp != 0)
1048 sprintf(temp, "Expected a numeric-parameter for %.20s", argv[*num]);
1056 * Print parts of a message
1059 PrintList(const char *const *list)
1061 const char *leaf = strrchr(program, '/');
1069 while (*list != 0) {
1070 fprintf(dialog_state.output, *list, n ? leaf : dialog_version());
1071 (void) fputc('\n', dialog_state.output);
1078 lookupMode(eOptions code)
1080 const Mode *modePtr = 0;
1083 for (n = 0; n < sizeof(modes) / sizeof(modes[0]); n++) {
1084 if (modes[n].code == code) {
1085 modePtr = &modes[n];
1093 compare_opts(const void *a, const void *b)
1095 Options *const *p = (Options * const *) a;
1096 Options *const *q = (Options * const *) b;
1097 return strcmp((*p)->name, (*q)->name);
1101 * Print program's version.
1104 PrintVersion(FILE *fp)
1106 fprintf(fp, "Version: %s\n", dialog_version());
1110 * Print program help-message
1115 static const char *const tbl_1[] =
1117 "cdialog (ComeOn Dialog!) version %s",
1118 "Copyright 2000-2008,2011 Thomas E. Dickey",
1119 "This is free software; see the source for copying conditions. There is NO",
1120 "warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.",
1122 "* Display dialog boxes from shell scripts *",
1124 "Usage: %s <options> { --and-widget <options> }",
1125 "where options are \"common\" options, followed by \"box\" options",
1129 " [--create-rc \"file\"]",
1135 "Auto-size with height and width = 0. Maximize with height and width = -1.",
1136 "Global-auto-size if also menu_height/list_height = 0.",
1139 size_t limit = sizeof(options) / sizeof(options[0]);
1141 const Options **opts;
1144 dialog_state.output = stdout;
1146 opts = dlg_calloc(const Options *, limit);
1147 assert_ptr(opts, "Help");
1148 for (j = 0; j < limit; ++j) {
1149 opts[j] = &(options[j]);
1151 qsort(opts, limit, sizeof(Options *), compare_opts);
1154 fprintf(dialog_state.output, "Common options:\n ");
1155 for (j = k = 0; j < limit; j++) {
1156 if ((opts[j]->pass & 1)
1157 && opts[j]->help != 0) {
1158 size_t len = 6 + strlen(opts[j]->name) + strlen(opts[j]->help);
1161 fprintf(dialog_state.output, "\n ");
1164 fprintf(dialog_state.output, " [--%s%s%s]", opts[j]->name,
1165 *(opts[j]->help) ? " " : "", opts[j]->help);
1168 fprintf(dialog_state.output, "\nBox options:\n");
1169 for (j = 0; j < limit; j++) {
1170 if ((opts[j]->pass & 2) != 0
1171 && opts[j]->help != 0
1172 && lookupMode(opts[j]->code))
1173 fprintf(dialog_state.output, " --%-12s %s\n", opts[j]->name,
1179 dlg_exit(DLG_EXIT_OK);
1183 * "Common" options apply to all widgets more/less. Most of the common options
1184 * set values in dialog_vars, a few set dialog_state and a couple write to the
1188 process_common_options(int argc, char **argv, int offset, bool output)
1190 #ifdef HAVE_DLG_TRACE
1195 while (offset < argc && !done) { /* Common options */
1196 switch (lookupOption(argv[offset], 1)) {
1198 dialog_vars.title = optionString(argv, &offset);
1201 dialog_vars.backtitle = optionString(argv, &offset);
1203 case o_separate_widget:
1204 dialog_state.separate_str = optionString(argv, &offset);
1206 case o_separate_output:
1207 dialog_vars.separate_output = TRUE;
1210 dialog_vars.colors = TRUE;
1213 dialog_vars.cr_wrap = TRUE;
1215 case o_no_nl_expand:
1216 dialog_vars.no_nl_expand = TRUE;
1219 dialog_vars.nocollapse = TRUE;
1222 dialog_vars.cant_kill = TRUE;
1225 dialog_vars.nocancel = TRUE;
1228 dialog_vars.nook = TRUE;
1231 dialog_vars.quoted = TRUE;
1233 case o_single_quoted:
1234 dialog_vars.single_quoted = TRUE;
1237 dialog_vars.size_err = TRUE;
1240 dialog_vars.beep_signal = TRUE;
1243 dialog_vars.beep_after_signal = TRUE;
1246 dialog_state.use_scrollbar = TRUE;
1249 dialog_state.use_shadow = TRUE;
1252 dialog_vars.defaultno = TRUE;
1254 case o_default_item:
1255 dialog_vars.default_item = optionString(argv, &offset);
1258 dialog_vars.insecure = TRUE;
1261 dialog_vars.item_help = TRUE;
1264 dialog_vars.help_line = optionString(argv, &offset);
1267 dialog_vars.help_file = optionString(argv, &offset);
1270 dialog_vars.help_button = TRUE;
1273 dialog_vars.help_status = TRUE;
1275 case o_extra_button:
1276 dialog_vars.extra_button = TRUE;
1279 ignore_unknown = TRUE;
1282 dialog_vars.keep_window = TRUE;
1285 dialog_state.use_shadow = FALSE;
1288 dialog_vars.print_siz = TRUE;
1290 case o_print_maxsize:
1293 * If this is the last option, we do not want any error
1294 * messages - just our output. Calling end_dialog() cancels
1295 * the refresh() at the end of the program as well.
1297 if (argv[offset + 1] == 0) {
1298 ignore_unknown = TRUE;
1301 fflush(dialog_state.output);
1302 fprintf(dialog_state.output, "MaxSize: %d, %d\n", SLINES, SCOLS);
1305 case o_print_version:
1307 PrintVersion(dialog_state.output);
1311 case o_output_separator:
1312 dialog_vars.output_separator = optionString(argv, &offset);
1314 case o_column_separator:
1315 dialog_vars.column_separator = optionString(argv, &offset);
1318 dialog_vars.tab_correct = TRUE;
1321 dialog_vars.sleep_secs = optionValue(argv, &offset);
1324 dialog_vars.timeout_secs = optionValue(argv, &offset);
1327 dialog_vars.max_input = optionValue(argv, &offset);
1330 dialog_state.tab_len = optionValue(argv, &offset);
1333 dialog_vars.trim_whitespace = TRUE;
1336 dialog_state.visit_items = TRUE;
1339 dialog_state.aspect_ratio = optionValue(argv, &offset);
1342 dialog_vars.begin_set = TRUE;
1343 dialog_vars.begin_y = optionValue(argv, &offset);
1344 dialog_vars.begin_x = optionValue(argv, &offset);
1347 dialog_vars.dlg_clear_screen = TRUE;
1350 dialog_vars.yes_label = optionString(argv, &offset);
1353 dialog_vars.no_label = optionString(argv, &offset);
1356 dialog_vars.ok_label = optionString(argv, &offset);
1358 case o_cancel_label:
1359 dialog_vars.cancel_label = optionString(argv, &offset);
1362 dialog_vars.extra_label = optionString(argv, &offset);
1365 dialog_vars.exit_label = optionString(argv, &offset);
1368 dialog_vars.help_label = optionString(argv, &offset);
1371 dialog_vars.date_format = optionString(argv, &offset);
1374 dialog_vars.time_format = optionString(argv, &offset);
1377 dialog_vars.keep_tite = TRUE;
1380 dialog_vars.ascii_lines = TRUE;
1381 dialog_vars.no_lines = FALSE;
1384 dialog_vars.no_lines = TRUE;
1385 dialog_vars.ascii_lines = FALSE;
1388 dialog_state.no_mouse = TRUE;
1395 /* options of Xdialog which we ignore */
1398 (void) optionString(argv, &offset);
1401 case o_auto_placement:
1406 case o_screen_center:
1414 default: /* no more common options */
1417 #ifdef HAVE_DLG_TRACE
1419 dlg_trace(optionString(argv, &offset));
1420 for (n = 0; argv[n] != 0; ++n) {
1421 dlg_trace_msg("argv[%d] = %s\n", n, argv[n]);
1433 * Initialize options at the start of a series of common options culminating
1437 init_result(char *buffer)
1439 static bool first = TRUE;
1440 static char **special_argv = 0;
1441 static int special_argc = 0;
1443 /* clear everything we do not save for the next widget */
1444 memset(&dialog_vars, 0, sizeof(dialog_vars));
1446 dialog_vars.input_result = buffer;
1447 dialog_vars.input_result[0] = '\0';
1450 * The first time this is called, check for common options given by an
1451 * environment variable.
1454 char *env = getenv("DIALOGOPTS");
1456 env = dlg_strclone(env);
1458 special_argv = dlg_string_to_argv(env);
1459 special_argc = dlg_count_argv(special_argv);
1462 if (special_argv != 0) {
1463 process_common_options(special_argc, special_argv, 0, FALSE);
1465 free(special_argv[0]);
1473 main(int argc, char *argv[])
1476 bool esc_pressed = FALSE;
1477 bool keep_tite = FALSE;
1480 int retval = DLG_EXIT_OK;
1483 const Mode *modePtr;
1484 char my_buffer[MAX_LEN + 1];
1486 memset(&dialog_state, 0, sizeof(dialog_state));
1487 memset(&dialog_vars, 0, sizeof(dialog_vars));
1489 #if defined(ENABLE_NLS)
1490 /* initialize locale support */
1491 setlocale(LC_ALL, "");
1492 bindtextdomain(NLS_TEXTDOMAIN, LOCALEDIR);
1493 textdomain(NLS_TEXTDOMAIN);
1494 #elif defined(HAVE_SETLOCALE)
1495 (void) setlocale(LC_ALL, "");
1498 unescape_argv(&argc, &argv);
1500 dialog_state.output = stderr;
1501 dialog_state.input = stdin;
1504 * Look for the last --stdout, --stderr or --output-fd option, and use
1505 * that. We can only write to one of them. If --stdout is used, that
1506 * can interfere with initializing the curses library, so we want to
1507 * know explicitly if it is used.
1509 * Also, look for any --version or --help message, processing those
1512 while (offset < argc) {
1514 switch (lookupOption(argv[offset], 7)) {
1516 dialog_state.output = stdout;
1519 dialog_state.output = stderr;
1522 if ((j = optionValue(argv, &offset)) < 0
1523 || (dialog_state.input = fdopen(j, "r")) == 0)
1524 dlg_exiterr("Cannot open input-fd\n");
1527 if ((j = optionValue(argv, &offset)) < 0
1528 || (dialog_state.output = fdopen(j, "w")) == 0)
1529 dlg_exiterr("Cannot open output-fd\n");
1535 dialog_state.output = stdout;
1536 PrintVersion(dialog_state.output);
1546 for (j = base; j < argc; ++j) {
1547 dialog_argv[j] = dialog_argv[j + 1 + (offset - base)];
1548 if (dialog_opts != 0)
1549 dialog_opts[j] = dialog_opts[j + 1 + (offset - base)];
1551 argc -= (1 + offset - base);
1555 init_result(my_buffer);
1558 * Dialog's output may be redirected (see above). Handle the special
1559 * case of options that only report information without interaction.
1562 switch (lookupOption(argv[1], 7)) {
1563 case o_print_maxsize:
1566 fflush(dialog_state.output);
1567 fprintf(dialog_state.output, "MaxSize: %d, %d\n", SLINES, SCOLS);
1569 case o_print_version:
1570 PrintVersion(dialog_state.output);
1590 if (lookupOption(argv[1], 7) == o_create_rc) {
1592 sprintf(temp, "Expected a filename for %.50s", argv[1]);
1595 if (dlg_parse_rc() == -1) /* Read the configuration file */
1596 dlg_exiterr("dialog: dlg_parse_rc");
1597 dlg_create_rc(argv[2]);
1602 dialog_vars.keep_tite = keep_tite; /* init_result() cleared global */
1604 init_dialog(dialog_state.input, dialog_state.output);
1606 while (offset < argc && !esc_pressed) {
1607 init_result(my_buffer);
1609 offset = process_common_options(argc, argv, offset, TRUE);
1611 if (argv[offset] == NULL) {
1614 Usage("Expected a box option");
1617 if (lookupOption(argv[offset], 2) != o_checklist
1618 && dialog_vars.separate_output) {
1619 sprintf(temp, "Expected --checklist, not %.20s", argv[offset]);
1623 if (dialog_state.aspect_ratio == 0)
1624 dialog_state.aspect_ratio = DEFAULT_ASPECT_RATIO;
1626 dlg_put_backtitle();
1628 /* use a table to look for the requested mode, to avoid code duplication */
1631 if ((code = lookupOption(argv[offset], 2)) != o_unknown)
1632 modePtr = lookupMode(code);
1634 sprintf(temp, "%s option %.20s",
1635 lookupOption(argv[offset], 7) != o_unknown
1642 have = arg_rest(&argv[offset]);
1643 if (have < modePtr->argmin) {
1644 sprintf(temp, "Expected at least %d tokens for %.20s, have %d",
1645 modePtr->argmin - 1, argv[offset],
1649 if (modePtr->argmax && have > modePtr->argmax) {
1651 "Expected no more than %d tokens for %.20s, have %d",
1652 modePtr->argmax - 1, argv[offset],
1658 * Trim whitespace from non-title option values, e.g., the ones that
1659 * will be used as captions or prompts. Do that only for the widget
1660 * we are about to process, since the "--trim" option is reset before
1661 * accumulating options for each widget.
1663 for (j = offset + 1; j <= offset + have; j++) {
1664 switch (lookupOption(argv[j - 1], 7)) {
1673 dlg_trim_string(argv[j]);
1679 retval = show_result((*(modePtr->jumper)) (dialog_vars.title,
1682 offset += offset_add;
1684 if (dialog_vars.input_result != my_buffer) {
1685 free(dialog_vars.input_result);
1686 dialog_vars.input_result = 0;
1689 if (retval == DLG_EXIT_ESC) {
1693 if (dialog_vars.beep_after_signal)
1696 if (dialog_vars.sleep_secs)
1697 (void) napms(dialog_vars.sleep_secs * 1000);
1699 if (offset < argc) {
1700 switch (lookupOption(argv[offset], 7)) {
1705 sprintf(temp, "Expected --and-widget, not %.20s",
1710 /* if we got a cancel, etc., stop chaining */
1711 if (retval != DLG_EXIT_OK)
1714 dialog_vars.dlg_clear_screen = TRUE;
1718 if (dialog_vars.dlg_clear_screen)
1723 dlg_killall_bg(&retval);
1724 if (dialog_state.screen_initialized) {