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.
23
* 17/12/93 - Version 0.1 released.
25
* 19/12/93 - menu will now scroll if there are more items than can fit
27
* - added 'checklist', a dialog box with a list of options that
28
* can be turned on or off. A list of options that are on is
31
* 20/12/93 - Version 0.15 released.
33
* 29/12/93 - Incorporated patch from Patrick J. Volkerding
34
* (volkerdi@mhd1.moorhead.msus.edu) that made these changes:
35
* - increased MAX_LEN to 2048
36
* - added 'infobox', equivalent to a message box without pausing
37
* - added option '--clear' that will clear the screen
38
* - Explicit line breaking when printing prompt text can be
39
* invoked by real newline '\n' besides the string "\n"
40
* - an optional parameter '--title <string>' can be used to
41
* specify a title string for the dialog box
43
* 03/01/94 - added 'textbox', a dialog box for displaying text from a file.
44
* - Version 0.2 released.
46
* 04/01/94 - some fixes and improvements for 'textbox':
47
* - fixed a bug that will cause a segmentation violation when a
48
* line is longer than MAX_LEN characters. Lines will now be
49
* truncated if they are longer than MAX_LEN characters.
50
* - removed wrefresh() from print_line(). This will increase
51
* efficiency of print_page() which calls print_line().
52
* - display current position in the form of percentage into file.
53
* - Version 0.21 released.
55
* 05/01/94 - some changes for faster screen update.
57
* 07/01/94 - much more flexible color settings. Can use all 16 colors
58
* (8 normal, 8 highlight) of the Linux console.
60
* 08/01/94 - added run-time configuration using configuration file.
62
* 09/01/94 - some minor bug fixes and cleanups for menubox, checklist and
65
* 11/01/94 - added a man page.
67
* 13/01/94 - some changes for easier porting to other Unix systems (tested
68
* on Ultrix, SunOS and HPUX)
69
* - Version 0.3 released.
71
* 08/06/94 - Patches by Stuart Herbert - S.Herbert@shef.ac.uk
72
* Fixed attr_clear and the textbox stuff to work with ncurses 1.8.5
73
* Fixed the wordwrap routine - it'll actually wrap properly now
74
* Added a more 3D look to everything - having your own rc file could
75
* prove 'interesting' to say the least :-)
76
* Added radiolist option
77
* Added backtitle option
78
* - Version 0.4 released.
80
* 10/10/94 - Patches by Marc Ewing - marc@redhat.com
81
* Added --separate-output
82
* Added init val to inputbox
87
#define __DIALOG_MAIN__
95
static char *backtitle = NULL;
96
int separate_output = 0;
98
int main(int argc, char *argv[])
100
int offset = 0, clear_screen = 0, end_common_opts = 0, retval;
104
(void) setlocale(LC_ALL, "");
111
else if (!strcmp(argv[1], "--create-rc")) {
120
fprintf(stderr, "\nThis option is currently unsupported on your system.\n");
125
while (offset < argc-1 && !end_common_opts) { /* Common options */
126
if (!strcmp(argv[offset+1], "--title")) {
127
if (argc-offset < 3 || title != NULL) { /* No two "--title" please! */
132
title = argv[offset+2];
136
else if (!strcmp(argv[offset+1], "--backtitle" ))
138
if (backtitle != NULL)
145
backtitle = argv[offset+2];
149
else if (!strcmp(argv[offset+1], "--separate-output" ))
154
else if (!strcmp(argv[offset+1], "--clear")) {
155
if (clear_screen) { /* Hey, "--clear" can't appear twice! */
159
else if (argc == 2) { /* we only want to clear the screen */
161
refresh(); /* init_dialog() will clear the screen for us */
170
else /* no more common options */
174
if (argc-1 == offset) { /* no more options */
181
if (!strcmp(argv[offset+1], "--yesno")) {
182
if (argc-offset != 5) {
187
retval = dialog_yesno(title, argv[offset+2], atoi(argv[offset+3]),
188
atoi(argv[offset+4]));
190
if (clear_screen) { /* clear screen before exit */
191
attr_clear(stdscr, LINES, COLS, screen_attr);
197
else if (!strcmp(argv[offset+1], "--msgbox")) {
198
if (argc-offset != 5) {
203
retval = dialog_msgbox(title, argv[offset+2], atoi(argv[offset+3]),
204
atoi(argv[offset+4]), 1);
206
if (clear_screen) { /* clear screen before exit */
207
attr_clear(stdscr, LINES, COLS, screen_attr);
213
else if (!strcmp(argv[offset+1], "--infobox")) {
214
if (argc-offset != 5) {
219
retval = dialog_msgbox(title, argv[offset+2], atoi(argv[offset+3]),
220
atoi(argv[offset+4]), 0);
222
if (clear_screen) { /* clear screen before exit */
223
attr_clear(stdscr, LINES, COLS, screen_attr);
229
else if (!strcmp(argv[offset+1], "--textbox")) {
230
if (argc-offset != 5) {
235
retval = dialog_textbox(title, argv[offset+2], atoi(argv[offset+3]),
236
atoi(argv[offset+4]));
238
if (clear_screen) { /* clear screen before exit */
239
attr_clear(stdscr, LINES, COLS, screen_attr);
245
else if (!strcmp(argv[offset+1], "--menu")) {
246
if (argc-offset < 8 || ((argc-offset) % 2)) {
251
retval = dialog_menu(title, argv[offset+2], atoi(argv[offset+3]),
252
atoi(argv[offset+4]), atoi(argv[offset+5]),
253
(argc-offset-6)/2, argv+offset + 6);
255
if (clear_screen) { /* clear screen before exit */
256
attr_clear(stdscr, LINES, COLS, screen_attr);
262
else if (!strcmp(argv[offset+1], "--checklist")) {
263
if (argc-offset < 9 || ((argc-offset-6) % 3)) {
268
retval = dialog_checklist(title, argv[offset+2], atoi(argv[offset+3]),
269
atoi(argv[offset+4]), atoi(argv[offset+5]),
270
(argc-offset-6)/3, argv+offset + 6);
272
if (clear_screen) { /* clear screen before exit */
273
attr_clear(stdscr, LINES, COLS, screen_attr);
279
else if (!strcmp(argv[offset+1], "--radiolist")) {
280
if (argc-offset < 9 || ((argc-offset-6) % 3)) {
285
retval = dialog_radiolist(title, argv[offset+2], atoi(argv[offset+3]),
286
atoi(argv[offset+4]), atoi(argv[offset+5]),
287
(argc-offset-6)/3, argv+offset + 6);
289
if (clear_screen) { /* clear screen before exit */
290
attr_clear(stdscr, LINES, COLS, screen_attr);
296
else if (!strcmp(argv[offset+1], "--guage")) {
297
if (argc-offset != 6) {
302
retval = dialog_guage(title, argv[offset+2],
303
atoi(argv[offset+3]), atoi(argv[offset+4]),
304
atoi(argv[offset+5]));
306
if (clear_screen) { /* clear screen before exit */
307
attr_clear(stdscr, LINES, COLS, screen_attr);
313
else if (!strcmp(argv[offset+1], "--inputbox")) {
314
if (argc-offset < 5 || argc-offset > 6) {
319
retval = dialog_inputbox(title, argv[offset+2], atoi(argv[offset+3]),
320
atoi(argv[offset+4]),
321
(argc-offset == 6) ? argv[offset+5] : (char *)NULL);
323
if (clear_screen) { /* clear screen before exit */
324
attr_clear(stdscr, LINES, COLS, screen_attr);
338
* Print program usage
340
void Usage(char *name)
343
\ndialog version 0.3, by Savio Lam (lam836@cs.cuhk.hk).\
344
\n patched to version %s by Stuart Herbert (S.Herbert@shef.ac.uk)\
346
\n* Display dialog boxes from shell scripts *\
349
\n %s --create-rc <file>\
350
\n %s [--title <title>] [--separate-output] [--backtitle <backtitle>] [--clear] <Box options>\
354
\n --yesno <text> <height> <width>\
355
\n --msgbox <text> <height> <width>\
356
\n --infobox <text> <height> <width>\
357
\n --inputbox <text> <height> <width> [<init>]\
358
\n --textbox <file> <height> <width>\
359
\n --menu <text> <height> <width> <menu height> <tag1> <item1>...\
360
\n --checklist <text> <height> <width> <list height> <tag1> <item1> <status1>...\
361
\n --radiolist <text> <height> <width> <list height> <tag1> <item1> <status1>...\
362
\n --guage <text> <height> <width> <percent>\n", VERSION, name, name, name);
368
* Do some initialization for dialog
370
void init_dialog(void)
375
if (parse_rc() == -1) /* Read the configuration file */
379
initscr(); /* Init curses */
380
keypad(stdscr, TRUE);
385
if (use_colors || use_shadow) /* Set up colors */
389
/* Set screen to screen attribute */
390
attr_clear(stdscr, LINES, COLS, screen_attr);
392
/* Display background title if it exists ... - SLH */
393
if (backtitle != NULL)
395
wattrset(stdscr, screen_attr);
397
waddstr(stdscr,backtitle);
399
for(i=1; i<COLS-1; i++)
400
waddch(stdscr, ACS_HLINE);
403
wnoutrefresh(stdscr);
405
/* End of init_dialog() */
410
* Setup for color display
412
void color_setup(void)
416
if (has_colors()) { /* Terminal supports color? */
419
/* Initialize color pairs */
420
for (i = 0; i < ATTRIBUTE_COUNT; i++)
421
init_pair(i+1, color_table[i][0], color_table[i][1]);
423
/* Setup color attributes */
424
for (i = 0; i < ATTRIBUTE_COUNT; i++)
425
attributes[i] = C_ATTR(color_table[i][2], i+1);
428
/* End of color_setup() */
433
* Set window to attribute 'attr'
435
void attr_clear(WINDOW *win, int height, int width, chtype attr)
437
/* ifdef HAVE_NCURSES
438
wattrset(win, attr); Set window to attribute 'attr'
443
wattrset(win, attr); /* Set window to attribute 'attr' */
444
for (i = 0; i < height; i++) {
446
for (j = 0; j < width; j++)
452
/* End of attr_clear() */
456
* Print a string of text in a window, automatically wrap around to the
457
* next line if the string is too long to fit on one line. Note that the
458
* string may contain "\n" to represent a newline character or the real
459
* newline '\n', but in that case, auto wrap around will be disabled.
461
void print_autowrap(WINDOW *win, char *prompt, int width, int y, int x)
463
int first = 1, cur_x, cur_y;
464
char tempstr[MAX_LEN+1], *word, *tempptr, *tempptr1;
466
strcpy(tempstr, prompt);
467
if ((strstr(tempstr, "\\n") != NULL) ||
468
(strchr(tempstr, '\n') != NULL)) { /* Prompt contains "\n" or '\n' */
471
wmove(win, cur_y, x);
473
tempptr = strstr(word, "\\n");
474
tempptr1 = strchr(word, '\n');
475
if (tempptr == NULL && tempptr1 == NULL)
477
else if (tempptr == NULL) { /* No more "\n" */
481
else if (tempptr1 == NULL) { /* No more '\n' */
485
else { /* Prompt contains both "\n" and '\n' */
486
if (strlen(tempptr)-2 < strlen(tempptr1)-1) {
498
wmove(win, ++cur_y, x);
502
else if (strlen(tempstr) <= width-x*2) { /* If prompt is short */
503
wmove(win, y, (width - strlen(tempstr)) / 2);
504
waddstr(win, tempstr);
509
/* Print prompt word by word, wrap around if necessary */
510
while ((word = strtok(first ? tempstr : NULL, " ")) != NULL) {
511
if (first) /* First iteration */
513
if (cur_x+strlen(word) > width) { /* wrap around to next line */
517
wmove(win, cur_y, cur_x);
519
getyx(win, cur_y, cur_x);
524
/* End of print_autowrap() */
530
void print_button(WINDOW *win, char *label, int y, int x, int selected)
535
wattrset(win, selected ? button_active_attr : button_inactive_attr);
537
temp = strspn(label, " ");
539
wattrset(win, selected ? button_label_active_attr : button_label_inactive_attr);
540
for (i = 0; i < temp; i++)
542
wattrset(win, selected ? button_key_active_attr : button_key_inactive_attr);
543
waddch(win, label[0]);
544
wattrset(win, selected ? button_label_active_attr : button_label_inactive_attr);
545
waddstr(win, label+1);
546
wattrset(win, selected ? button_active_attr : button_inactive_attr);
548
wmove(win, y, x+temp+1);
550
/* End of print_button() */
554
* Draw a rectangular box with line drawing characters
556
void draw_box(WINDOW *win, int y, int x, int height, int width, chtype box, chtype border)
561
for (i = 0; i < height; i++) {
562
wmove(win, y + i, x);
563
for (j = 0; j < width; j++)
565
waddch(win, border | ACS_ULCORNER);
566
else if (i == height-1 && !j)
567
waddch(win, border | ACS_LLCORNER);
568
else if (!i && j == width-1)
569
waddch(win, box | ACS_URCORNER);
570
else if (i == height-1 && j == width-1)
571
waddch(win, box | ACS_LRCORNER);
573
waddch(win, border | ACS_HLINE);
574
else if (i == height-1)
575
waddch(win, box | ACS_HLINE);
577
waddch(win, border | ACS_VLINE);
578
else if (j == width-1)
579
waddch(win, box | ACS_VLINE);
581
waddch(win, box | ' ');
584
/* End of draw_box() */
589
* Draw shadows along the right and bottom edge to give a more 3D look
592
void draw_shadow(WINDOW *win, int y, int x, int height, int width)
596
if (has_colors()) { /* Whether terminal supports color? */
597
wattrset(win, shadow_attr);
598
wmove(win, y + height, x + 2);
599
for (i = 0; i < width; i++)
600
waddch(win, winch(win) & A_CHARTEXT);
601
for (i = y + 1; i < y + height + 1; i++) {
602
wmove(win, i, x + width);
603
waddch(win, winch(win) & A_CHARTEXT);
604
waddch(win, winch(win) & A_CHARTEXT);
609
/* End of draw_shadow() */