blob: 2c34ea1e0a41f3ac25f62719ca2a222f04ea0692 [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
34jumperFn j_menu, j_checklist, j_radiolist, j_yesno, j_textbox, j_inputbox;
35jumperFn j_msgbox, j_infobox;
36
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010037static struct Mode modes[] = {
38 {"--menu", 9, 0, 3, j_menu},
39 {"--checklist", 9, 0, 3, j_checklist},
40 {"--radiolist", 9, 0, 3, j_radiolist},
41 {"--yesno", 5, 5, 1, j_yesno},
42 {"--textbox", 5, 5, 1, j_textbox},
43 {"--inputbox", 5, 6, 1, j_inputbox},
44 {"--msgbox", 5, 5, 1, j_msgbox},
45 {"--infobox", 5, 5, 1, j_infobox},
46 {NULL, 0, 0, 0, NULL}
Linus Torvalds1da177e2005-04-16 15:20:36 -070047};
48
49static struct Mode *modePtr;
50
51#ifdef LOCALE
52#include <locale.h>
53#endif
54
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010055int main(int argc, const char *const *argv)
Linus Torvalds1da177e2005-04-16 15:20:36 -070056{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010057 int offset = 0, opt_clear = 0, end_common_opts = 0, retval;
58 const char *title = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -070059
60#ifdef LOCALE
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010061 (void)setlocale(LC_ALL, "");
Linus Torvalds1da177e2005-04-16 15:20:36 -070062#endif
63
64#ifdef TRACE
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010065 trace(TRACE_CALLS | TRACE_UPDATE);
Linus Torvalds1da177e2005-04-16 15:20:36 -070066#endif
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010067 if (argc < 2) {
68 Usage(argv[0]);
69 exit(-1);
70 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070071
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +010072 while (offset < argc - 1 && !end_common_opts) { /* Common options */
73 if (!strcmp(argv[offset + 1], "--title")) {
74 if (argc - offset < 3 || title != NULL) {
75 Usage(argv[0]);
76 exit(-1);
77 } else {
78 title = argv[offset + 2];
79 offset += 2;
80 }
81 } else if (!strcmp(argv[offset + 1], "--backtitle")) {
82 if (backtitle != NULL) {
83 Usage(argv[0]);
84 exit(-1);
85 } else {
86 backtitle = argv[offset + 2];
87 offset += 2;
88 }
89 } else if (!strcmp(argv[offset + 1], "--clear")) {
90 if (opt_clear) { /* Hey, "--clear" can't appear twice! */
91 Usage(argv[0]);
92 exit(-1);
93 } else if (argc == 2) { /* we only want to clear the screen */
94 init_dialog();
95 refresh(); /* init_dialog() will clear the screen for us */
96 end_dialog();
97 return 0;
98 } else {
99 opt_clear = 1;
100 offset++;
101 }
102 } else /* no more common options */
103 end_common_opts = 1;
104 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100106 if (argc - 1 == offset) { /* no more options */
107 Usage(argv[0]);
108 exit(-1);
109 }
110 /* use a table to look for the requested mode, to avoid code duplication */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700111
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100112 for (modePtr = modes; modePtr->name; modePtr++) /* look for the mode */
113 if (!strcmp(argv[offset + 1], modePtr->name))
114 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100116 if (!modePtr->name)
117 Usage(argv[0]);
118 if (argc - offset < modePtr->argmin)
119 Usage(argv[0]);
120 if (modePtr->argmax && argc - offset > modePtr->argmax)
121 Usage(argv[0]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700122
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100123 init_dialog();
124 retval = (*(modePtr->jumper)) (title, argc - offset, argv + offset);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100126 if (opt_clear) { /* clear screen before exit */
127 attr_clear(stdscr, LINES, COLS, screen_attr);
128 refresh();
129 }
130 end_dialog();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100132 exit(retval);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133}
134
135/*
136 * Print program usage
137 */
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100138static void Usage(const char *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100140 fprintf(stderr, "\
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141\ndialog, by Savio Lam (lam836@cs.cuhk.hk).\
142\n patched by Stuart Herbert (S.Herbert@shef.ac.uk)\
143\n modified/gutted for use as a Linux kernel config tool by \
144\n William Roadcap (roadcapw@cfw.com)\
145\n\
146\n* Display dialog boxes from shell scripts *\
147\n\
148\nUsage: %s --clear\
149\n %s [--title <title>] [--backtitle <backtitle>] --clear <Box options>\
150\n\
151\nBox options:\
152\n\
153\n --menu <text> <height> <width> <menu height> <tag1> <item1>...\
154\n --checklist <text> <height> <width> <list height> <tag1> <item1> <status1>...\
155\n --radiolist <text> <height> <width> <list height> <tag1> <item1> <status1>...\
156\n --textbox <file> <height> <width>\
157\n --inputbox <text> <height> <width> [<init>]\
158\n --yesno <text> <height> <width>\
159\n", name, name);
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100160 exit(-1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
163/*
164 * These are the program jumpers
165 */
166
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100167int j_menu(const char *t, int ac, const char *const *av)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100169 return dialog_menu(t, av[2], atoi(av[3]), atoi(av[4]),
170 atoi(av[5]), av[6], (ac - 6) / 2, av + 7);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171}
172
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100173int j_checklist(const char *t, int ac, const char *const *av)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100175 return dialog_checklist(t, av[2], atoi(av[3]), atoi(av[4]),
176 atoi(av[5]), (ac - 6) / 3, av + 6, FLAG_CHECK);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700177}
178
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100179int j_radiolist(const char *t, int ac, const char *const *av)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100181 return dialog_checklist(t, av[2], atoi(av[3]), atoi(av[4]),
182 atoi(av[5]), (ac - 6) / 3, av + 6, FLAG_RADIO);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183}
184
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100185int j_textbox(const char *t, int ac, const char *const *av)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100187 return dialog_textbox(t, av[2], atoi(av[3]), atoi(av[4]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188}
189
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100190int j_yesno(const char *t, int ac, const char *const *av)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100192 return dialog_yesno(t, av[2], atoi(av[3]), atoi(av[4]));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193}
194
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100195int j_inputbox(const char *t, int ac, const char *const *av)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100197 int ret = dialog_inputbox(t, av[2], atoi(av[3]), atoi(av[4]),
198 ac == 6 ? av[5] : (char *)NULL);
199 if (ret == 0)
200 fprintf(stderr, dialog_input_result);
201 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202}
203
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100204int j_msgbox(const char *t, int ac, const char *const *av)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700205{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100206 return dialog_msgbox(t, av[2], atoi(av[3]), atoi(av[4]), 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207}
208
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100209int j_infobox(const char *t, int ac, const char *const *av)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210{
Sam Ravnborgb1c5f1c2005-11-19 19:13:34 +0100211 return dialog_msgbox(t, av[2], atoi(av[3]), atoi(av[4]), 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700212}