lxdialog.c 5.3 KB
Newer Older
L
Linus Torvalds 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
/*
 *  dialog - Display simple dialog boxes from shell scripts
 *
 *  ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
 *  MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
 *
 *  This program is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License
 *  as published by the Free Software Foundation; either version 2
 *  of the License, or (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include "dialog.h"

S
Sam Ravnborg 已提交
24
static void Usage(const char *name);
L
Linus Torvalds 已提交
25

S
Sam Ravnborg 已提交
26
typedef int (jumperFn) (const char *title, int argc, const char *const *argv);
L
Linus Torvalds 已提交
27 28

struct Mode {
S
Sam Ravnborg 已提交
29 30 31
	char *name;
	int argmin, argmax, argmod;
	jumperFn *jumper;
L
Linus Torvalds 已提交
32 33
};

34
jumperFn j_menu, j_radiolist, j_yesno, j_textbox, j_inputbox;
L
Linus Torvalds 已提交
35 36
jumperFn j_msgbox, j_infobox;

S
Sam Ravnborg 已提交
37 38 39 40 41 42 43 44 45
static struct Mode modes[] = {
	{"--menu", 9, 0, 3, j_menu},
	{"--radiolist", 9, 0, 3, j_radiolist},
	{"--yesno", 5, 5, 1, j_yesno},
	{"--textbox", 5, 5, 1, j_textbox},
	{"--inputbox", 5, 6, 1, j_inputbox},
	{"--msgbox", 5, 5, 1, j_msgbox},
	{"--infobox", 5, 5, 1, j_infobox},
	{NULL, 0, 0, 0, NULL}
L
Linus Torvalds 已提交
46 47 48 49 50 51 52 53
};

static struct Mode *modePtr;

#ifdef LOCALE
#include <locale.h>
#endif

S
Sam Ravnborg 已提交
54
int main(int argc, const char *const *argv)
L
Linus Torvalds 已提交
55
{
S
Sam Ravnborg 已提交
56 57
	int offset = 0, opt_clear = 0, end_common_opts = 0, retval;
	const char *title = NULL;
L
Linus Torvalds 已提交
58 59

#ifdef LOCALE
S
Sam Ravnborg 已提交
60
	(void)setlocale(LC_ALL, "");
L
Linus Torvalds 已提交
61 62 63
#endif

#ifdef TRACE
S
Sam Ravnborg 已提交
64
	trace(TRACE_CALLS | TRACE_UPDATE);
L
Linus Torvalds 已提交
65
#endif
S
Sam Ravnborg 已提交
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131
	if (argc < 2) {
		Usage(argv[0]);
		exit(-1);
	}

	while (offset < argc - 1 && !end_common_opts) {	/* Common options */
		if (!strcmp(argv[offset + 1], "--title")) {
			if (argc - offset < 3 || title != NULL) {
				Usage(argv[0]);
				exit(-1);
			} else {
				title = argv[offset + 2];
				offset += 2;
			}
		} else if (!strcmp(argv[offset + 1], "--backtitle")) {
			if (backtitle != NULL) {
				Usage(argv[0]);
				exit(-1);
			} else {
				backtitle = argv[offset + 2];
				offset += 2;
			}
		} else if (!strcmp(argv[offset + 1], "--clear")) {
			if (opt_clear) {	/* Hey, "--clear" can't appear twice! */
				Usage(argv[0]);
				exit(-1);
			} else if (argc == 2) {	/* we only want to clear the screen */
				init_dialog();
				refresh();	/* init_dialog() will clear the screen for us */
				end_dialog();
				return 0;
			} else {
				opt_clear = 1;
				offset++;
			}
		} else		/* no more common options */
			end_common_opts = 1;
	}

	if (argc - 1 == offset) {	/* no more options */
		Usage(argv[0]);
		exit(-1);
	}
	/* use a table to look for the requested mode, to avoid code duplication */

	for (modePtr = modes; modePtr->name; modePtr++)	/* look for the mode */
		if (!strcmp(argv[offset + 1], modePtr->name))
			break;

	if (!modePtr->name)
		Usage(argv[0]);
	if (argc - offset < modePtr->argmin)
		Usage(argv[0]);
	if (modePtr->argmax && argc - offset > modePtr->argmax)
		Usage(argv[0]);

	init_dialog();
	retval = (*(modePtr->jumper)) (title, argc - offset, argv + offset);

	if (opt_clear) {	/* clear screen before exit */
		attr_clear(stdscr, LINES, COLS, screen_attr);
		refresh();
	}
	end_dialog();

	exit(retval);
L
Linus Torvalds 已提交
132 133 134 135 136
}

/*
 * Print program usage
 */
S
Sam Ravnborg 已提交
137
static void Usage(const char *name)
L
Linus Torvalds 已提交
138
{
S
Sam Ravnborg 已提交
139
	fprintf(stderr, "\
L
Linus Torvalds 已提交
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
\ndialog, by Savio Lam (lam836@cs.cuhk.hk).\
\n  patched by Stuart Herbert (S.Herbert@shef.ac.uk)\
\n  modified/gutted for use as a Linux kernel config tool by \
\n  William Roadcap (roadcapw@cfw.com)\
\n\
\n* Display dialog boxes from shell scripts *\
\n\
\nUsage: %s --clear\
\n       %s [--title <title>] [--backtitle <backtitle>] --clear <Box options>\
\n\
\nBox options:\
\n\
\n  --menu      <text> <height> <width> <menu height> <tag1> <item1>...\
\n  --radiolist <text> <height> <width> <list height> <tag1> <item1> <status1>...\
\n  --textbox   <file> <height> <width>\
\n  --inputbox  <text> <height> <width> [<init>]\
\n  --yesno     <text> <height> <width>\
\n", name, name);
S
Sam Ravnborg 已提交
158
	exit(-1);
L
Linus Torvalds 已提交
159 160 161 162 163 164
}

/*
 * These are the program jumpers
 */

S
Sam Ravnborg 已提交
165
int j_menu(const char *t, int ac, const char *const *av)
L
Linus Torvalds 已提交
166
{
S
Sam Ravnborg 已提交
167 168
	return dialog_menu(t, av[2], atoi(av[3]), atoi(av[4]),
			   atoi(av[5]), av[6], (ac - 6) / 2, av + 7);
L
Linus Torvalds 已提交
169 170
}

S
Sam Ravnborg 已提交
171
int j_radiolist(const char *t, int ac, const char *const *av)
L
Linus Torvalds 已提交
172
{
S
Sam Ravnborg 已提交
173
	return dialog_checklist(t, av[2], atoi(av[3]), atoi(av[4]),
174
				atoi(av[5]), (ac - 6) / 3, av + 6);
L
Linus Torvalds 已提交
175 176
}

S
Sam Ravnborg 已提交
177
int j_textbox(const char *t, int ac, const char *const *av)
L
Linus Torvalds 已提交
178
{
S
Sam Ravnborg 已提交
179
	return dialog_textbox(t, av[2], atoi(av[3]), atoi(av[4]));
L
Linus Torvalds 已提交
180 181
}

S
Sam Ravnborg 已提交
182
int j_yesno(const char *t, int ac, const char *const *av)
L
Linus Torvalds 已提交
183
{
S
Sam Ravnborg 已提交
184
	return dialog_yesno(t, av[2], atoi(av[3]), atoi(av[4]));
L
Linus Torvalds 已提交
185 186
}

S
Sam Ravnborg 已提交
187
int j_inputbox(const char *t, int ac, const char *const *av)
L
Linus Torvalds 已提交
188
{
S
Sam Ravnborg 已提交
189 190 191 192 193
	int ret = dialog_inputbox(t, av[2], atoi(av[3]), atoi(av[4]),
				  ac == 6 ? av[5] : (char *)NULL);
	if (ret == 0)
		fprintf(stderr, dialog_input_result);
	return ret;
L
Linus Torvalds 已提交
194 195
}

S
Sam Ravnborg 已提交
196
int j_msgbox(const char *t, int ac, const char *const *av)
L
Linus Torvalds 已提交
197
{
S
Sam Ravnborg 已提交
198
	return dialog_msgbox(t, av[2], atoi(av[3]), atoi(av[4]), 1);
L
Linus Torvalds 已提交
199 200
}

S
Sam Ravnborg 已提交
201
int j_infobox(const char *t, int ac, const char *const *av)
L
Linus Torvalds 已提交
202
{
S
Sam Ravnborg 已提交
203
	return dialog_msgbox(t, av[2], atoi(av[3]), atoi(av[4]), 0);
L
Linus Torvalds 已提交
204
}