File: dialog.c

package info (click to toggle)
sabre 0.2.4b-13
  • links: PTS
  • area: main
  • in suites: woody
  • size: 5,556 kB
  • ctags: 7,207
  • sloc: cpp: 36,934; ansic: 8,272; sh: 2,546; makefile: 227
file content (271 lines) | stat: -rw-r--r-- 7,018 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/*
 *  dialog - Display simple dialog boxes from shell scripts
 *
 *  AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
 *
 *  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"

static void Usage (const char *name);

static int separate_output = 0;

typedef int (jumperFn) (const char *title, int argc, const char * const * argv);

struct Mode {
    char *name;
    int argmin, argmax, argmod;
    jumperFn *jumper;
};

jumperFn j_yesno, j_msgbox, j_infobox, j_textbox, j_menu;
jumperFn j_checklist, j_radiolist, j_inputbox, j_guage;

/*
 * All functions are used in the slackware root disk, apart from "guage"
 */

static struct Mode modes[] =
{
    {"--yesno", 5, 5, 1, j_yesno},
    {"--msgbox", 5, 5, 1, j_msgbox},
    {"--infobox", 5, 5, 1, j_infobox},
    {"--textbox", 5, 5, 1, j_textbox},
    {"--menu", 8, 0, 2, j_menu},
    {"--checklist", 9, 0, 3, j_checklist},
    {"--radiolist", 9, 0, 3, j_radiolist},
    {"--inputbox", 5, 6, 1, j_inputbox},
#ifdef HAVE_GUAGE
    {"--guage", 6, 6, 1, j_guage},
#endif
    {NULL, 0, 0, 0, NULL}
};

static struct Mode *modePtr;

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

int
main (int argc, const char * const * argv)
{
    int offset = 0, clear_screen = 0, end_common_opts = 0, retval;
    const char *title = NULL;

#ifdef LOCALE
    (void) setlocale (LC_ALL, "");
#endif

    if (argc < 2) {
	Usage (argv[0]);
	exit (-1);
    }
#ifdef HAVE_RC_FILE

    else if (!strcmp (argv[1], "--create-rc")) {
#ifdef HAVE_LIBNCURSES
	if (argc != 3) {
	    Usage (argv[0]);
	    exit (-1);
	}
	create_rc (argv[2]);
	return 0;
#else
	fprintf (stderr, "This option is currently unsupported "
		 "on your system.\n");
	return -1;
#endif
    }
#endif

    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], "--separate-output")) {
	    separate_output = 1;
	    offset++;
	} else if (!strcmp (argv[offset + 1], "--clear")) {
	    if (clear_screen) {	/* 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 {
		clear_screen = 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]);
    if ((argc - offset) % modePtr->argmod)
	Usage (argv[0]);

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

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

    exit (retval);
}

/*
 * Print program usage
 */
static void
Usage (const char *name)
{
    fprintf (stderr, "\
\ndialog version 0.3, by Savio Lam (lam836@cs.cuhk.hk).\
\n  patched to version 0.6 by Stuart Herbert (S.Herbert@shef.ac.uk)\
\n  patched for SABRE flight simulator version %s\
\n\
\n* Display dialog boxes from shell scripts *\
\n\
\nUsage: %s --clear\
\n       %s --create-rc <file>\
\n       %s [--title <title>] [--separate-output] [--backtitle <backtitle>] [--clear] <Box options>\
\n\
\nBox options:\
\n\
\n  --yesno     <text> <height> <width>\
\n  --msgbox    <text> <height> <width>\
\n  --infobox   <text> <height> <width>\
\n  --inputbox  <text> <height> <width> [<init>]\
\n  --textbox   <file> <height> <width>\
\n  --menu      <text> <height> <width> <menu height> <tag1> <item1>...\
\n  --checklist <text> <height> <width> <list height> <tag1> <item1> <status1>...\
\n  --radiolist <text> <height> <width> <list height> <tag1> <item1> <status1>...\
", VERSION, name, name, name);
#ifdef HAVE_GUAGE   
    fprintf(stderr,"\
\n  --guage     <text> <height> <width> <percent>\n");
#endif   
    exit (-1);
}

/*
 * These are the program jumpers
 */

int
j_yesno (const char *t, int ac, const char * const * av)
{
    return dialog_yesno (t, av[2], atoi (av[3]), atoi (av[4]));
}

int
j_msgbox (const char *t, int ac, const char * const * av)
{
    return dialog_msgbox (t, av[2], atoi (av[3]), atoi (av[4]), 1);
}

int
j_infobox (const char *t, int ac, const char * const * av)
{
    return dialog_msgbox (t, av[2], atoi (av[3]), atoi (av[4]), 0);
}

int
j_textbox (const char *t, int ac, const char * const * av)
{
    return dialog_textbox (t, av[2], atoi (av[3]), atoi (av[4]));
}

int
j_menu (const char *t, int ac, const char * const * av)
{
    int ret = dialog_menu (t, av[2], atoi (av[3]), atoi (av[4]),
			atoi (av[5]), (ac - 6) / 2, av + 6);
    if (ret >= 0) {
	fprintf(stderr, av[6 + ret * 2]);
	return 0;
    } else if (ret == -2)
	return 1;	/* CANCEL */
    return ret;		/* ESC pressed, ret == -1 */
}

int
j_checklist (const char *t, int ac, const char * const * av)
{
    return dialog_checklist (t, av[2], atoi (av[3]), atoi (av[4]),
	atoi (av[5]), (ac - 6) / 3, av + 6, FLAG_CHECK, separate_output);
}

int
j_radiolist (const char *t, int ac, const char * const * av)
{
    return dialog_checklist (t, av[2], atoi (av[3]), atoi (av[4]),
	atoi (av[5]), (ac - 6) / 3, av + 6, FLAG_RADIO, separate_output);
}

int
j_inputbox (const char *t, int ac, const char * const * av)
{
    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;
}

#ifdef HAVE_GUAGE
int
j_guage (const char *t, int ac, const char * const * av)
{
    return dialog_guage (t, av[2], atoi (av[3]), atoi (av[4]),
			 atoi (av[5]));
}
#endif