2 * dialog - Display simple dialog boxes from shell scripts
4 * AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
25 * 17/12/93 - Version 0.1 released.
27 * 19/12/93 - menu will now scroll if there are more items than can fit
29 * - added 'checklist', a dialog box with a list of options that
30 * can be turned on or off. A list of options that are on is
33 * 20/12/93 - Version 0.15 released.
35 * 29/12/93 - Incorporated patch from Patrick J. Volkerding
36 * (volkerdi@mhd1.moorhead.msus.edu) that made these changes:
37 * - increased MAX_LEN to 2048
38 * - added 'infobox', equivalent to a message box without pausing
39 * - added option '--clear' that will clear the screen
40 * - Explicit line breaking when printing prompt text can be
41 * invoked by real newline '\n' besides the string "\n"
42 * - an optional parameter '--title <string>' can be used to
43 * specify a title string for the dialog box
45 * 03/01/94 - added 'textbox', a dialog box for displaying text from a file.
46 * - Version 0.2 released.
48 * 04/01/94 - some fixes and improvements for 'textbox':
49 * - fixed a bug that will cause a segmentation violation when a
50 * line is longer than MAX_LEN characters. Lines will now be
51 * truncated if they are longer than MAX_LEN characters.
52 * - removed wrefresh() from print_line(). This will increase
53 * efficiency of print_page() which calls print_line().
54 * - display current position in the form of percentage into file.
55 * - Version 0.21 released.
57 * 05/01/94 - some changes for faster screen update.
59 * 07/01/94 - much more flexible color settings. Can use all 16 colors
60 * (8 normal, 8 highlight) of the Linux console.
62 * 08/01/94 - added run-time configuration using configuration file.
64 * 09/01/94 - some minor bug fixes and cleanups for menubox, checklist and
67 * 11/01/94 - added a man page.
69 * 13/01/94 - some changes for easier porting to other Unix systems (tested
70 * on Ultrix, SunOS and HPUX)
71 * - Version 0.3 released.
73 * 08/06/94 - Patches by Stuart Herbert - S.Herbert@shef.ac.uk
74 * Fixed attr_clear and the textbox stuff to work with ncurses 1.8.5
75 * Fixed the wordwrap routine - it'll actually wrap properly now
76 * Added a more 3D look to everything - having your own rc file could
77 * prove 'interesting' to say the least :-)
78 * Added radiolist option
79 * - Version 0.4 released.
81 * 09/28/98 - Patches by Anatoly A. Orehovsky - tolik@mpeks.tomsk.su
82 * Added ftree and tree options
86 #include <sys/types.h>
94 void Usage(char *name);
95 void EndDialog(int cls);
97 int main(int argc, char *argv[])
99 int offset = 0, clear_screen = 0, end_common_opts = 0, default_yesno = 1, retval;
100 unsigned char *title = NULL;
101 unsigned char result[MAX_LEN];
102 char *hline = NULL, *hfile = NULL;
106 else if (!strcmp(argv[1], "--create-rc")) {
110 dialog_create_rc(argv[2]);
113 fprintf(stderr, "\nThis option is currently unsupported on your system.\n");
118 while (offset < argc-1 && !end_common_opts) { /* Common options */
119 if (!strcmp(argv[offset+1], "--title")) {
120 if (argc-offset < 3 || title != NULL) /* No two "--title" please! */
123 title = argv[offset+2];
127 else if (!strcmp(argv[offset+1], "--hline")) {
128 if (argc-offset < 3 || hline != NULL) /* No two "--hline" please! */
131 hline = argv[offset+2];
136 else if (!strcmp(argv[offset+1], "--hfile")) {
137 if (argc-offset < 3 || hfile != NULL) /* No two "--hfile" please! */
140 hfile = argv[offset+2];
145 else if (!strcmp(argv[offset+1], "--clear")) {
146 if (clear_screen) /* Hey, "--clear" can't appear twice! */
148 else if (argc == 2) { /* we only want to clear the screen */
150 dialog_update(); /* init_dialog() will clear the screen for us */
159 else /* no more common options */
163 if (argc-1 == offset) /* no more options */
168 if (!strcmp(argv[offset+1], "--yesno")) {
169 if (argc-offset != 5 && argc-offset != 6)
171 if (argc-offset == 6) {
172 if (!strcmp(argv[offset+5], "yes"))
174 else if (!strcmp(argv[offset+5], "no"))
180 if (default_yesno == 1)
181 retval = dialog_yesno(title, argv[offset+2], atoi(argv[offset+3]),
182 atoi(argv[offset+4]));
184 retval = dialog_noyes(title, argv[offset+2], atoi(argv[offset+3]),
185 atoi(argv[offset+4]));
188 EndDialog(clear_screen);
191 else if (!strcmp(argv[offset+1], "--msgbox")) {
192 if (argc-offset != 5)
195 retval = dialog_msgbox(title, argv[offset+2], atoi(argv[offset+3]),
196 atoi(argv[offset+4]), 1);
199 EndDialog(clear_screen);
202 else if (!strcmp(argv[offset+1], "--prgbox")) {
203 if (argc-offset != 5)
206 retval = dialog_prgbox(title, argv[offset+2], atoi(argv[offset+3]),
207 atoi(argv[offset+4]), TRUE, TRUE);
210 EndDialog(clear_screen);
211 return WEXITSTATUS(retval);
213 else if (!strcmp(argv[offset+1], "--infobox")) {
214 if (argc-offset != 5)
217 retval = dialog_msgbox(title, argv[offset+2], atoi(argv[offset+3]),
218 atoi(argv[offset+4]), 0);
221 EndDialog(clear_screen);
224 else if (!strcmp(argv[offset+1], "--textbox")) {
225 if (argc-offset != 5)
228 retval = dialog_textbox(title, argv[offset+2], atoi(argv[offset+3]),
229 atoi(argv[offset+4]));
232 EndDialog(clear_screen);
235 else if (!strcmp(argv[offset+1], "--menu")) {
236 if (argc-offset < 8 || ((argc-offset) % 2))
239 retval = dialog_menu(title, argv[offset+2], atoi(argv[offset+3]),
240 atoi(argv[offset+4]), atoi(argv[offset+5]),
241 (argc-offset-6)/2, argv+offset + 6, result,
245 fputs(result, stderr);
246 EndDialog(clear_screen);
249 else if (!strcmp(argv[offset+1], "--checklist")) {
250 if (argc-offset < 9 || ((argc-offset-6) % 3))
253 retval = dialog_checklist(title, argv[offset+2], atoi(argv[offset+3]),
254 atoi(argv[offset+4]), atoi(argv[offset+5]),
255 (argc-offset-6)/3, argv+offset + 6, result);
259 unsigned char *s, *h; int first;
263 while ((s = strchr(h, '\n')) != NULL) {
269 fprintf(stderr, "\"%s\"", h);
273 EndDialog(clear_screen);
276 else if (!strcmp(argv[offset+1], "--radiolist")) {
277 if (argc-offset < 9 || ((argc-offset-6) % 3))
280 retval = dialog_radiolist(title, argv[offset+2], atoi(argv[offset+3]),
281 atoi(argv[offset+4]), atoi(argv[offset+5]),
282 (argc-offset-6)/3, argv+offset + 6, result);
286 fputs(result, stderr);
287 EndDialog(clear_screen);
290 else if (!strcmp(argv[offset+1], "--inputbox")) {
291 if (argc-offset != 5 && argc-offset != 6)
293 if (argc-offset == 6)
294 strcpy(result, argv[offset+5]);
298 retval = dialog_inputbox(title, argv[offset+2], atoi(argv[offset+3]),
299 atoi(argv[offset+4]), result);
303 fputs(result, stderr);
304 EndDialog(clear_screen);
307 /* ftree and tree options */
308 else if (!strcmp(argv[offset+1], "--ftree")) {
309 unsigned char *tresult;
310 if (argc-offset != 8)
313 retval = dialog_ftree(argv[offset+2], *argv[offset+3],
314 title, argv[offset+4], atoi(argv[offset+5]), atoi(argv[offset+6]),
315 atoi(argv[offset+7]), &tresult);
320 fputs(tresult, stderr);
323 EndDialog(clear_screen);
326 else if (!strcmp(argv[offset+1], "--tree")) {
327 unsigned char *tresult;
331 retval = dialog_tree((unsigned char **)argv+offset+7, argc-offset-7,
332 *argv[offset+2], title, argv[offset+3], atoi(argv[offset+4]),
333 atoi(argv[offset+5]), atoi(argv[offset+6]), &tresult);
337 fputs(tresult, stderr);
339 EndDialog(clear_screen);
351 * Print program usage
353 void Usage(char *name)
356 \ndialog version 0.3, by Savio Lam (lam836@cs.cuhk.hk).\
357 \n patched to version %s by Stuart Herbert (S.Herbert@shef.ac.uk)\
358 \n Changes Copyright (C) 1995 by Andrey A. Chernov, Moscow, Russia\
359 \n patched by Anatoly A. Orehovsky (tolik@mpeks.tomsk.su)\
361 \n* Display dialog boxes from shell scripts *\
364 \n %s --create-rc <file>\
365 \n %s [--title <title>] [--clear] [--hline <line>] [--hfile <file>]\\\
370 \n --yesno <text> <height> <width> [yes|no]\
371 \n --msgbox <text> <height> <width>\
372 \n --prgbox \"<command line>\" <height> <width>\
373 \n --infobox <text> <height> <width>\
374 \n --inputbox <text> <height> <width> [<init string>]\
375 \n --textbox <file> <height> <width>\
376 \n --menu <text> <height> <width> <menu height> <tag1> <item1>...\
377 \n --checklist <text> <height> <width> <list height> <tag1> <item1> <status1>...\
378 \n --radiolist <text> <height> <width> <list height> <tag1> <item1> <status1>...\
379 \n --ftree <file> <FS> <text> <height> <width> <menu height>\
380 \n --tree <FS> <text> <height> <width> <menu height> <item1>...\n", VERSION, name, name, name);
388 void EndDialog(int cls)
390 if (cls) /* clear screen before exit */
394 /* End of Dialog() */