blob: 79f6c5fb5cef812df3473f7985cfc44a5bcdc936 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * dialog - Display simple dialog boxes from shell scripts
3 *
4 * ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
5 * MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
6 *
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
11 *
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
16 *
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 */
21
22#include "dialog.h"
23
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010024static void Usage(const char *name);
Linus Torvalds1da177e2005-04-16 15:20:36 -070025
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010026typedef int (jumperFn) (const char *title, int argc, const char *const *argv);
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
28struct Mode {
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010029 char *name;
30 int argmin, argmax, argmod;
31 jumperFn *jumper;
Linus Torvalds1da177e2005-04-16 15:20:36 -070032};
33
Petr Baudis00213b12005-12-22 04:44:04 +010034jumperFn j_menu, j_radiolist, j_yesno, j_textbox, j_inputbox;
Linus Torvalds1da177e2005-04-16 15:20:36 -070035jumperFn j_msgbox, j_infobox;
36
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010037static struct Mode modes[] = {
38 {"--menu", 9, 0, 3, j_menu},
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010039 {"--radiolist", 9, 0, 3, j_radiolist},
40 {"--yesno", 5, 5, 1, j_yesno},
41 {"--textbox", 5, 5, 1, j_textbox},
42 {"--inputbox", 5, 6, 1, j_inputbox},
43 {"--msgbox", 5, 5, 1, j_msgbox},
44 {"--infobox", 5, 5, 1, j_infobox},
45 {NULL, 0, 0, 0, NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -070046};
47
48static struct Mode *modePtr;
49
50#ifdef LOCALE
51#include <locale.h>
52#endif
53
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010054int main(int argc, const char *const *argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070055{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010056 int offset = 0, opt_clear = 0, end_common_opts = 0, retval;
57 const char *title = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
59#ifdef LOCALE
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010060 (void)setlocale(LC_ALL, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -070061#endif
62
63#ifdef TRACE
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010064 trace(TRACE_CALLS | TRACE_UPDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070065#endif
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010066 if (argc < 2) {
67 Usage(argv[0]);
68 exit(-1);
69 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010071 while (offset < argc - 1 && !end_common_opts) { /* Common options */
72 if (!strcmp(argv[offset + 1], "--title")) {
73 if (argc - offset < 3 || title != NULL) {
74 Usage(argv[0]);
75 exit(-1);
76 } else {
77 title = argv[offset + 2];
78 offset += 2;
79 }
80 } else if (!strcmp(argv[offset + 1], "--backtitle")) {
81 if (backtitle != NULL) {
82 Usage(argv[0]);
83 exit(-1);
84 } else {
85 backtitle = argv[offset + 2];
86 offset += 2;
87 }
88 } else if (!strcmp(argv[offset + 1], "--clear")) {
89 if (opt_clear) { /* Hey, "--clear" can't appear twice! */
90 Usage(argv[0]);
91 exit(-1);
92 } else if (argc == 2) { /* we only want to clear the screen */
93 init_dialog();
94 refresh(); /* init_dialog() will clear the screen for us */
95 end_dialog();
96 return 0;
97 } else {
98 opt_clear = 1;
99 offset++;
100 }
101 } else /* no more common options */
102 end_common_opts = 1;
103 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100105 if (argc - 1 == offset) { /* no more options */
106 Usage(argv[0]);
107 exit(-1);
108 }
109 /* use a table to look for the requested mode, to avoid code duplication */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100111 for (modePtr = modes; modePtr->name; modePtr++) /* look for the mode */
112 if (!strcmp(argv[offset + 1], modePtr->name))
113 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100115 if (!modePtr->name)
116 Usage(argv[0]);
117 if (argc - offset < modePtr->argmin)
118 Usage(argv[0]);
119 if (modePtr->argmax && argc - offset > modePtr->argmax)
120 Usage(argv[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100122 init_dialog();
123 retval = (*(modePtr->jumper)) (title, argc - offset, argv + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700124
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100125 if (opt_clear) { /* clear screen before exit */
126 attr_clear(stdscr, LINES, COLS, screen_attr);
127 refresh();
128 }
129 end_dialog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100131 exit(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132}
133
134/*
135 * Print program usage
136 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100137static void Usage(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700138{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100139 fprintf(stderr, "\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140\ndialog, by Savio Lam (lam836@cs.cuhk.hk).\
141\n patched by Stuart Herbert (S.Herbert@shef.ac.uk)\
142\n modified/gutted for use as a Linux kernel config tool by \
143\n William Roadcap (roadcapw@cfw.com)\
144\n\
145\n* Display dialog boxes from shell scripts *\
146\n\
147\nUsage: %s --clear\
148\n %s [--title <title>] [--backtitle <backtitle>] --clear <Box options>\
149\n\
150\nBox options:\
151\n\
152\n --menu <text> <height> <width> <menu height> <tag1> <item1>...\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153\n --radiolist <text> <height> <width> <list height> <tag1> <item1> <status1>...\
154\n --textbox <file> <height> <width>\
155\n --inputbox <text> <height> <width> [<init>]\
156\n --yesno <text> <height> <width>\
157\n", name, name);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100158 exit(-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159}
160
161/*
162 * These are the program jumpers
163 */
164
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100165int j_menu(const char *t, int ac, const char *const *av)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100167 return dialog_menu(t, av[2], atoi(av[3]), atoi(av[4]),
168 atoi(av[5]), av[6], (ac - 6) / 2, av + 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169}
170
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100171int j_radiolist(const char *t, int ac, const char *const *av)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100173 return dialog_checklist(t, av[2], atoi(av[3]), atoi(av[4]),
Petr Baudis00213b12005-12-22 04:44:04 +0100174 atoi(av[5]), (ac - 6) / 3, av + 6);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175}
176
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100177int j_textbox(const char *t, int ac, const char *const *av)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100179 return dialog_textbox(t, av[2], atoi(av[3]), atoi(av[4]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180}
181
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100182int j_yesno(const char *t, int ac, const char *const *av)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100184 return dialog_yesno(t, av[2], atoi(av[3]), atoi(av[4]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100187int j_inputbox(const char *t, int ac, const char *const *av)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100189 int ret = dialog_inputbox(t, av[2], atoi(av[3]), atoi(av[4]),
190 ac == 6 ? av[5] : (char *)NULL);
191 if (ret == 0)
192 fprintf(stderr, dialog_input_result);
193 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700194}
195
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100196int j_msgbox(const char *t, int ac, const char *const *av)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100198 return dialog_msgbox(t, av[2], atoi(av[3]), atoi(av[4]), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700199}
200
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100201int j_infobox(const char *t, int ac, const char *const *av)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100203 return dialog_msgbox(t, av[2], atoi(av[3]), atoi(av[4]), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204}