]> CyberLeo.Net >> Repos - FreeBSD/releng/8.1.git/blob - gnu/usr.bin/dialog/dialog.c
Copy stable/8 to releng/8.1 in preparation for 8.1-RC1.
[FreeBSD/releng/8.1.git] / gnu / usr.bin / dialog / dialog.c
1 /*
2  *  dialog - Display simple dialog boxes from shell scripts
3  *
4  *  AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
5  *
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.
10  *
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.
15  *
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.
19  *
20  * $FreeBSD$
21  *
22  *
23  *  HISTORY:
24  *
25  *  17/12/93 - Version 0.1 released.
26  *
27  *  19/12/93 - menu will now scroll if there are more items than can fit
28  *             on the screen.
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
31  *             returned on exit.
32  *
33  *  20/12/93 - Version 0.15 released.
34  *
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
44  *
45  *  03/01/94 - added 'textbox', a dialog box for displaying text from a file.
46  *           - Version 0.2 released.
47  *
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.
56  *
57  *  05/01/94 - some changes for faster screen update.
58  *
59  *  07/01/94 - much more flexible color settings. Can use all 16 colors
60  *             (8 normal, 8 highlight) of the Linux console.
61  *
62  *  08/01/94 - added run-time configuration using configuration file.
63  *
64  *  09/01/94 - some minor bug fixes and cleanups for menubox, checklist and
65  *             textbox.
66  *
67  *  11/01/94 - added a man page.
68  *
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.
72  *
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.
80  *
81  *  09/28/98 - Patches by Anatoly A. Orehovsky - tolik@mpeks.tomsk.su
82  *             Added ftree and tree options
83  *
84  */
85
86 #include <sys/types.h>
87
88 #include <stdio.h>
89 #include <stdlib.h>
90 #include <string.h>
91 #include <sys/wait.h>
92 #include <dialog.h>
93
94 void Usage(char *name);
95 void EndDialog(int cls);
96
97 int main(int argc, char *argv[])
98 {
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;
103
104   if (argc < 2)
105     Usage(argv[0]);
106   else if (!strcmp(argv[1], "--create-rc")) {
107 #ifdef HAVE_NCURSES
108     if (argc != 3)
109       Usage(argv[0]);
110     dialog_create_rc(argv[2]);
111     return 0;
112 #else
113     fprintf(stderr, "\nThis option is currently unsupported on your system.\n");
114     return -1;
115 #endif
116   }
117
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! */
121         Usage(argv[0]);
122       else {
123         title = argv[offset+2];
124         offset += 2;
125       }
126     }
127     else if (!strcmp(argv[offset+1], "--hline")) {
128       if (argc-offset < 3 || hline != NULL)      /* No two "--hline" please! */
129         Usage(argv[0]);
130       else {
131         hline = argv[offset+2];
132         use_helpline(hline);
133         offset += 2;
134       }
135     }
136     else if (!strcmp(argv[offset+1], "--hfile")) {
137       if (argc-offset < 3 || hfile != NULL)      /* No two "--hfile" please! */
138         Usage(argv[0]);
139       else {
140         hfile = argv[offset+2];
141         use_helpfile(hfile);
142         offset += 2;
143       }
144     }
145     else if (!strcmp(argv[offset+1], "--clear")) {
146       if (clear_screen)      /* Hey, "--clear" can't appear twice! */
147         Usage(argv[0]);
148       else if (argc == 2) {    /* we only want to clear the screen */
149         init_dialog();
150         dialog_update();    /* init_dialog() will clear the screen for us */
151         end_dialog();
152         return 0;
153       }
154       else {
155         clear_screen = 1;
156         offset++;
157       }
158     }
159     else    /* no more common options */
160       end_common_opts = 1;
161   }
162
163   if (argc-1 == offset)      /* no more options */
164     Usage(argv[0]);
165
166   /* Box options */
167
168   if (!strcmp(argv[offset+1], "--yesno")) {
169     if (argc-offset != 5 && argc-offset != 6)
170       Usage(argv[0]);
171     if (argc-offset == 6) {
172       if (!strcmp(argv[offset+5], "yes"))
173         default_yesno = 1;
174       else if (!strcmp(argv[offset+5], "no"))
175         default_yesno = 0;
176       else
177         Usage(argv[0]);
178     }
179     init_dialog();
180     if (default_yesno == 1)
181       retval = dialog_yesno(title, argv[offset+2], atoi(argv[offset+3]),
182                             atoi(argv[offset+4]));
183     else
184       retval = dialog_noyes(title, argv[offset+2], atoi(argv[offset+3]),
185                             atoi(argv[offset+4]));
186
187     dialog_update();
188     EndDialog(clear_screen);
189     return retval;
190   }
191   else if (!strcmp(argv[offset+1], "--msgbox")) {
192     if (argc-offset != 5)
193       Usage(argv[0]);
194     init_dialog();
195     retval = dialog_msgbox(title, argv[offset+2], atoi(argv[offset+3]),
196                            atoi(argv[offset+4]), 1);
197
198     dialog_update();
199     EndDialog(clear_screen);
200     return retval;
201   }
202   else if (!strcmp(argv[offset+1], "--prgbox")) {
203     if (argc-offset != 5)
204       Usage(argv[0]);
205     init_dialog();
206     retval = dialog_prgbox(title, argv[offset+2], atoi(argv[offset+3]),
207                            atoi(argv[offset+4]), TRUE, TRUE);
208
209     dialog_update();
210     EndDialog(clear_screen);
211     return WEXITSTATUS(retval);
212   }
213   else if (!strcmp(argv[offset+1], "--infobox")) {
214     if (argc-offset != 5)
215       Usage(argv[0]);
216     init_dialog();
217     retval = dialog_msgbox(title, argv[offset+2], atoi(argv[offset+3]),
218                            atoi(argv[offset+4]), 0);
219
220     dialog_update();
221     EndDialog(clear_screen);
222     return retval;
223   }
224   else if (!strcmp(argv[offset+1], "--textbox")) {
225     if (argc-offset != 5)
226       Usage(argv[0]);
227     init_dialog();
228     retval = dialog_textbox(title, argv[offset+2], atoi(argv[offset+3]),
229                             atoi(argv[offset+4]));
230
231     dialog_update();
232     EndDialog(clear_screen);
233     return retval;
234   }
235   else if (!strcmp(argv[offset+1], "--menu")) {
236     if (argc-offset < 8 || ((argc-offset) % 2))
237       Usage(argv[0]);
238     init_dialog();
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,
242                          NULL, NULL);
243     dialog_update();
244     if (retval == 0)
245         fputs(result, stderr);
246     EndDialog(clear_screen);
247     return retval;
248   }
249   else if (!strcmp(argv[offset+1], "--checklist")) {
250     if (argc-offset < 9 || ((argc-offset-6) % 3))
251       Usage(argv[0]);
252     init_dialog();
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);
256
257     dialog_update();
258     if (retval == 0) {
259       unsigned char *s, *h; int first;
260
261       h = result;
262       first = 1;
263       while ((s = strchr(h, '\n')) != NULL) {
264         *s++ = '\0';
265         if (!first)
266           fputc(' ', stderr);
267         else
268           first = 0;
269         fprintf(stderr, "\"%s\"", h);
270         h = s;
271       }
272     }
273     EndDialog(clear_screen);
274     return retval;
275   }
276   else if (!strcmp(argv[offset+1], "--radiolist")) {
277     if (argc-offset < 9 || ((argc-offset-6) % 3))
278       Usage(argv[0]);
279     init_dialog();
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);
283
284     dialog_update();
285     if (retval == 0)
286         fputs(result, stderr);
287     EndDialog(clear_screen);
288     return retval;
289   }
290   else if (!strcmp(argv[offset+1], "--inputbox")) {
291     if (argc-offset != 5 && argc-offset != 6)
292       Usage(argv[0]);
293     if (argc-offset == 6)
294       strcpy(result, argv[offset+5]);
295     else
296       *result = '\0';
297     init_dialog();
298     retval = dialog_inputbox(title, argv[offset+2], atoi(argv[offset+3]),
299                              atoi(argv[offset+4]), result);
300
301     dialog_update();
302     if (retval == 0)
303         fputs(result, stderr);
304     EndDialog(clear_screen);
305     return retval;
306   }
307 /* ftree and tree options */
308   else if (!strcmp(argv[offset+1], "--ftree")) {
309         unsigned char *tresult;
310     if (argc-offset != 8)
311       Usage(argv[0]);
312     init_dialog();
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);
316
317     dialog_update();
318     if (!retval)
319     {
320         fputs(tresult, stderr);
321         free(tresult);
322     }
323     EndDialog(clear_screen);
324     return retval;
325   }  
326   else if (!strcmp(argv[offset+1], "--tree")) {
327         unsigned char *tresult;
328     if (argc-offset < 8)
329       Usage(argv[0]);
330     init_dialog();
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);
334
335     dialog_update();
336     if (!retval)
337         fputs(tresult, stderr);
338
339     EndDialog(clear_screen);
340     return retval;
341   }    
342
343   Usage(argv[0]);
344
345   return 0;
346 }
347 /* End of main() */
348
349
350 /*
351  * Print program usage
352  */
353 void Usage(char *name)
354 {
355   fprintf(stderr, "\
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)\
360 \n\
361 \n* Display dialog boxes from shell scripts *\
362 \n\
363 \nUsage: %s --clear\
364 \n       %s --create-rc <file>\
365 \n       %s [--title <title>] [--clear] [--hline <line>] [--hfile <file>]\\\
366 \n              <Box options>\
367 \n\
368 \nBox options:\
369 \n\
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);
381   exit(-1);
382 }
383 /* End of Usage() */
384
385 /*
386  * End dialog
387  */
388 void EndDialog(int cls)
389 {
390     if (cls)    /* clear screen before exit */
391       dialog_clear();
392     end_dialog();
393 }
394 /* End of Dialog() */