Dash:
[t2.git] / misc / confdialog / confdialog.c
blobe0548e02336328cc9808730d0e7c1caa141f0b28
1 /*
2 * --- T2-COPYRIGHT-NOTE-BEGIN ---
3 * This copyright note is auto-generated by ./scripts/Create-CopyPatch.
4 *
5 * T2 SDE: misc/confdialog/confdialog.c
6 * Copyright (C) 2004 - 2005 The T2 SDE Project
7 * Copyright (C) 1998 - 2003 ROCK Linux Project
8 *
9 * More information can be found in the files COPYING and README.
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; version 2 of the License. A copy of the
14 * GNU General Public License can be found in the file COPYING.
15 * --- T2-COPYRIGHT-NOTE-END ---
18 * dialog - Display simple dialog boxes from shell scripts
20 * ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
21 * MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
22 * MODIFIED FOR ROCK LINUX CONFIG BY: Clifford Wolf (clifford@clifford.at)
24 * This program is free software; you can redistribute it and/or
25 * modify it under the terms of the GNU General Public License
26 * as published by the Free Software Foundation; either version 2
27 * of the License, or (at your option) any later version.
29 * This program is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU General Public License for more details.
34 * You should have received a copy of the GNU General Public License
35 * along with this program; if not, write to the Free Software
36 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
39 #include "dialog.h"
41 static void Usage (const char *name);
43 typedef int (jumperFn) (const char *title, int argc, const char * const * argv);
45 struct Mode {
46 char *name;
47 int argmin, argmax, argmod;
48 jumperFn *jumper;
51 jumperFn j_menu, j_checklist, j_radiolist, j_yesno, j_textbox, j_inputbox;
52 jumperFn j_msgbox, j_infobox;
54 static struct Mode modes[] =
56 {"--menu", 9, 0, 3, j_menu},
57 {"--checklist", 9, 0, 3, j_checklist},
58 {"--radiolist", 9, 0, 3, j_radiolist},
59 {"--yesno", 5,5,1, j_yesno},
60 {"--textbox", 5,5,1, j_textbox},
61 {"--inputbox", 5, 6, 1, j_inputbox},
62 {"--msgbox", 5, 5, 1, j_msgbox},
63 {"--infobox", 5, 5, 1, j_infobox},
64 {NULL, 0, 0, 0, NULL}
67 static struct Mode *modePtr;
69 #ifdef LOCALE
70 #include <locale.h>
71 #endif
73 int
74 main (int argc, const char * const * argv)
76 int offset = 0, clear_screen = 0, end_common_opts = 0, retval;
77 const char *title = NULL;
79 #ifdef LOCALE
80 (void) setlocale (LC_ALL, "");
81 #endif
83 #ifdef TRACE
84 trace(TRACE_CALLS|TRACE_UPDATE);
85 #endif
86 if (argc < 2) {
87 Usage (argv[0]);
88 exit (-1);
91 while (offset < argc - 1 && !end_common_opts) { /* Common options */
92 if (!strcmp (argv[offset + 1], "--title")) {
93 if (argc - offset < 3 || title != NULL) {
94 Usage (argv[0]);
95 exit (-1);
96 } else {
97 title = argv[offset + 2];
98 offset += 2;
100 } else if (!strcmp (argv[offset + 1], "--backtitle")) {
101 if (backtitle != NULL) {
102 Usage (argv[0]);
103 exit (-1);
104 } else {
105 backtitle = argv[offset + 2];
106 offset += 2;
108 } else if (!strcmp (argv[offset + 1], "--clear")) {
109 if (clear_screen) { /* Hey, "--clear" can't appear twice! */
110 Usage (argv[0]);
111 exit (-1);
112 } else if (argc == 2) { /* we only want to clear the screen */
113 init_dialog ();
114 refresh (); /* init_dialog() will clear the screen for us */
115 end_dialog ();
116 return 0;
117 } else {
118 clear_screen = 1;
119 offset++;
121 } else /* no more common options */
122 end_common_opts = 1;
125 if (argc - 1 == offset) { /* no more options */
126 Usage (argv[0]);
127 exit (-1);
129 /* use a table to look for the requested mode, to avoid code duplication */
131 for (modePtr = modes; modePtr->name; modePtr++) /* look for the mode */
132 if (!strcmp (argv[offset + 1], modePtr->name))
133 break;
135 if (!modePtr->name)
136 Usage (argv[0]);
137 if (argc - offset < modePtr->argmin)
138 Usage (argv[0]);
139 if (modePtr->argmax && argc - offset > modePtr->argmax)
140 Usage (argv[0]);
144 init_dialog ();
145 retval = (*(modePtr->jumper)) (title, argc - offset, argv + offset);
147 if (clear_screen) { /* clear screen before exit */
148 attr_clear (stdscr, LINES, COLS, screen_attr);
149 refresh ();
151 end_dialog();
153 exit (retval);
157 * Print program usage
159 static void
160 Usage (const char *name)
162 fprintf (stderr, "\
163 \ndialog, by Savio Lam (lam836@cs.cuhk.hk).\
164 \n patched by Stuart Herbert (S.Herbert@shef.ac.uk)\
165 \n modified/gutted for use as a Linux kernel config tool by \
166 \n William Roadcap (roadcapw@cfw.com)\
167 \n modified/gutted for use as a ROCK Linux config tool by \
168 \n Clifford Wolf (clifford@clifford.at)\
170 \n* Display dialog boxes from shell scripts *\
172 \nUsage: %s --clear\
173 \n %s [--title <title>] [--backtitle <backtitle>] --clear <Box options>\
175 \nBox options:\
177 \n --menu <text> <height> <width> <menu height> <active item> <tag1> <item1>...\
178 \n --checklist <text> <height> <width> <list height> <tag1> <item1> <status1>...\
179 \n --radiolist <text> <height> <width> <list height> <tag1> <item1> <status1>...\
180 \n --textbox <file> <height> <width>\
181 \n --inputbox <text> <height> <width> [<init>]\
182 \n --yesno <text> <height> <width>\
183 \n", name, name);
184 exit (-1);
188 * These are the program jumpers
192 j_menu (const char *t, int ac, const char * const * av)
194 return dialog_menu (t, av[2], atoi (av[3]), atoi (av[4]),
195 atoi (av[5]), av[6], (ac - 6) / 2, av + 7);
199 j_checklist (const char *t, int ac, const char * const * av)
201 return dialog_checklist (t, av[2], atoi (av[3]), atoi (av[4]),
202 atoi (av[5]), (ac - 6) / 3, av + 6, FLAG_CHECK);
206 j_radiolist (const char *t, int ac, const char * const * av)
208 return dialog_checklist (t, av[2], atoi (av[3]), atoi (av[4]),
209 atoi (av[5]), (ac - 6) / 3, av + 6, FLAG_RADIO);
213 j_textbox (const char *t, int ac, const char * const * av)
215 return dialog_textbox (t, av[2], atoi (av[3]), atoi (av[4]));
219 j_yesno (const char *t, int ac, const char * const * av)
221 return dialog_yesno (t, av[2], atoi (av[3]), atoi (av[4]));
225 j_inputbox (const char *t, int ac, const char * const * av)
227 int ret = dialog_inputbox (t, av[2], atoi (av[3]), atoi (av[4]),
228 ac == 6 ? av[5] : (char *) NULL);
229 if (ret == 0)
230 fprintf(stderr, dialog_input_result);
231 return ret;
235 j_msgbox (const char *t, int ac, const char * const * av)
237 return dialog_msgbox (t, av[2], atoi (av[3]), atoi (av[4]), 1);
241 j_infobox (const char *t, int ac, const char * const * av)
243 return dialog_msgbox (t, av[2], atoi (av[3]), atoi (av[4]), 0);