Initial import from FreeBSD RELENG_4:
[games.git] / gnu / lib / libdialog / kernel.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  *
21  *  HISTORY:
22  *
23  *  17/12/93 - Version 0.1 released.
24  *
25  *  19/12/93 - menu will now scroll if there are more items than can fit
26  *             on the screen.
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
29  *             returned on exit.
30  *
31  *  20/12/93 - Version 0.15 released.
32  *
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
42  *
43  *  03/01/94 - added 'textbox', a dialog box for displaying text from a file.
44  *           - Version 0.2 released.
45  *
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.
54  *
55  *  05/01/94 - some changes for faster screen update.
56  *
57  *  07/01/94 - much more flexible color settings. Can use all 16 colors
58  *             (8 normal, 8 highlight) of the Linux console.
59  *
60  *  08/01/94 - added run-time configuration using configuration file.
61  *
62  *  09/01/94 - some minor bug fixes and cleanups for menubox, checklist and
63  *             textbox.
64  *
65  *  11/01/94 - added a man page.
66  *
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.
70  *
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  *           - Version 0.4 released.
78  *
79  * $FreeBSD: src/gnu/lib/libdialog/kernel.c,v 1.26.6.1 2003/02/15 05:32:04 kris Exp $
80  */
81
82 #define __DIALOG_MAIN__
83
84 #include <dialog.h>
85 #include <err.h>
86 #include "dialog.priv.h"
87 #ifdef HAVE_NCURSES
88 #include "colors.h"
89 #endif
90
91 /* These are two "secret" globals that can be fiddled to make a dialog
92  * come up someplace other than a "centered" calculation for X,Y
93  */
94 int DialogX, DialogY;
95
96 /* This "secret" global allows you to change the behavior of an input field */
97 int DialogInputAttrs;
98
99 /*
100  * Do some initialization for dialog
101  */
102 void init_dialog(void)
103 {
104
105   if (issetugid()) {
106         errx(1, "libdialog is unsafe to use in setugid applications");
107   }
108
109 #if defined(LOCALE)
110   (void) setlocale(LC_ALL, "");
111 #endif
112
113 #ifdef HAVE_NCURSES
114   if (parse_rc() == -1)    /* Read the configuration file */
115     exit(-1);
116 #endif
117
118   if (initscr() == NULL) { /* Init curses */
119     fprintf(stderr, "\nCurses initialization error.\n");
120     exit(-1);
121   }
122   keypad(stdscr, TRUE);
123   cbreak();
124   noecho();
125
126 #ifdef HAVE_NCURSES
127   if (use_colors || use_shadow)    /* Set up colors */
128     color_setup();
129 #endif
130
131   /* Set screen to screen attribute */
132   dialog_clear_norefresh();
133   DialogX = DialogY = 0;
134 }
135 /* End of init_dialog() */
136
137
138 #ifdef HAVE_NCURSES
139 /*
140  * Setup for color display
141  */
142 void color_setup(void)
143 {
144   int i;
145
146   if (has_colors()) {    /* Terminal supports color? */
147     start_color();
148
149     /* Initialize color pairs */
150     for (i = 0; i < ATTRIBUTE_COUNT; i++)
151       init_pair(i+1, color_table[i][0], color_table[i][1]);
152
153     /* Setup color attributes */
154     for (i = 0; i < ATTRIBUTE_COUNT; i++)
155       attributes[i] = C_ATTR(color_table[i][2], i+1);
156   }
157 }
158 /* End of color_setup() */
159 #endif
160
161
162 /*
163  * Set window to attribute 'attr'
164  */
165 void attr_clear(WINDOW *win, int height, int width, chtype attr)
166 {
167   int i, j;
168
169   wattrset(win, attr);    /* Set window to attribute 'attr' */
170   for (i = 0; i < height; i++) {
171     wmove(win, i, 0);
172     for (j = 0; j < width; j++)
173       waddch(win, ' ');
174   }
175 }
176 /* End of attr_clear() */
177
178
179 /*
180  * Print a string of text in a window, automatically wrap around to the
181  * next line if the string is too long to fit on one line. Note that the
182  * string may contain "\n" to represent a newline character or the real
183  * newline '\n', but in that case, auto wrap around will be disabled.
184  */
185 void print_autowrap(WINDOW *win, unsigned char *prompt, int height, int width, int maxwidth, int y, int x, int center, int rawmode)
186 {
187   int cur_x, cur_y, i;
188   unsigned char tempstr[MAX_LEN+1], *word, *tempptr, *tempptr1;
189   chtype ostuff[132], attrs = 0, init_bottom = 0;
190
191   wsetscrreg(win, y, height);
192   getyx(win, cur_y, cur_x);
193
194   strncpy(tempstr, prompt, MAX_LEN);
195   tempstr[MAX_LEN] = '\0';
196   if ((!rawmode && strstr(tempstr, "\\n") != NULL) ||
197       (strchr(tempstr, '\n') != NULL)) {    /* Prompt contains "\n" or '\n' */
198     word = tempstr;
199     while (1) {
200       tempptr = rawmode ? NULL : strstr(word, "\\n");
201       tempptr1 = strchr(word, '\n');
202       if (tempptr == NULL && tempptr1 == NULL)
203         break;
204       else if (tempptr == NULL) {    /* No more "\n" */
205         tempptr = tempptr1;
206         tempptr[0] = '\0';
207       }
208       else if (tempptr1 == NULL) {    /* No more '\n' */
209         tempptr[0] = '\0';
210         tempptr++;
211       }
212       else {    /* Prompt contains both "\n" and '\n' */
213         if (strlen(tempptr)-2 < strlen(tempptr1)-1) {
214           tempptr = tempptr1;
215           tempptr[0] = '\0';
216         }
217         else {
218           tempptr[0] = '\0';
219           tempptr++;
220         }
221       }
222
223       waddstr(win, word);
224       word = tempptr + 1;
225       if (++cur_y > height) {
226         cur_y--;
227         if (!init_bottom) {
228           for (i = 0; i < x; i++)
229             ostuff[i] = mvwinch(win, cur_y, i);
230           for (i = width; i < maxwidth; i++)
231             ostuff[i] = mvwinch(win, cur_y, i);
232           attrs = getattrs(win);
233           init_bottom = 1;
234         }
235         scrollok(win, TRUE);
236         scroll(win);
237         scrollok(win, FALSE);
238         wmove(win, cur_y, 0);
239         for (i = 0; i < x; i++) {
240           wattrset(win, ostuff[i]&A_ATTRIBUTES);
241           waddch(win, ostuff[i]);
242         }
243         wattrset(win, attrs);
244         for ( ; i < width; i++)
245           waddch(win, ' ');
246         for ( ; i < maxwidth; i++) {
247           wattrset(win, ostuff[i]&A_ATTRIBUTES);
248           waddch(win, ostuff[i]);
249         }
250         wattrset(win, attrs);
251         wrefresh(win);
252       }
253       wmove(win, cur_y, cur_x = x);
254     }
255     waddstr(win, word);
256   }
257   else if (center && strlen(tempstr) <= width-x*2) {    /* If prompt is short */
258     wmove(win, cur_y, (width - strlen(tempstr)) / 2);
259     waddstr(win, tempstr);
260   }
261   else if (!center && strlen(tempstr) <= width-cur_x) {    /* If prompt is short */
262     waddstr(win, tempstr);
263   }
264   else {
265     char *p = tempstr;
266
267     /* Print prompt word by word, wrap around if necessary */
268     while ((word = strsep(&p, "\t\n ")) != NULL) {
269       int loop;
270       unsigned char sc;
271
272       if (*word == '\0')
273         continue;
274       do {
275         loop = 0;
276         if (cur_x+strlen(word) >= width+1) {    /* wrap around to next line */
277           if (x+strlen(word) >= width+1) {
278             sc = word[width-cur_x-1];
279             word[width-cur_x-1] = '\0';
280             wmove(win, cur_y, cur_x);
281             waddstr(win, word);
282             word[width-cur_x-1] = sc;
283             word += width-cur_x-1;
284             getyx(win, cur_y, cur_x);
285             loop = 1;
286           }
287           cur_y++;
288           cur_x = x;
289           if (cur_y > height) {
290             cur_y--;
291             if (!init_bottom) {
292               for (i = 0; i < x; i++)
293                 ostuff[i] = mvwinch(win, cur_y, i);
294               for (i = width; i < maxwidth; i++)
295                 ostuff[i] = mvwinch(win, cur_y, i);
296               attrs = getattrs(win);
297               init_bottom = 1;
298             }
299             scrollok(win, TRUE);
300             scroll(win);
301             scrollok(win, FALSE);
302             wmove(win, cur_y, 0);
303             for (i = 0; i < x; i++) {
304               wattrset(win, ostuff[i]&A_ATTRIBUTES);
305               waddch(win, ostuff[i]);
306             }
307             wattrset(win, attrs);
308             for ( ; i < width; i++)
309               waddch(win, ' ');
310             for ( ; i < maxwidth; i++) {
311               wattrset(win, ostuff[i]&A_ATTRIBUTES);
312               waddch(win, ostuff[i]);
313             }
314             wattrset(win, attrs);
315             wrefresh(win);
316           }
317         }
318       }
319       while(loop);
320       wmove(win, cur_y, cur_x);
321       waddstr(win, word);
322       getyx(win, cur_y, cur_x);
323       cur_x++;
324     }
325   }
326 }
327 /* End of print_autowrap() */
328
329
330 /*
331  * Print a button
332  */
333 void print_button(WINDOW *win, unsigned char *label, int y, int x, int selected)
334 {
335   int i, temp;
336
337   wmove(win, y, x);
338   wattrset(win, selected ? button_active_attr : button_inactive_attr);
339   waddstr(win, selected ? "[" : " ");
340   temp = strspn(label, " ");
341   label += temp;
342   for (i = 0; i < temp; i++)
343     waddch(win, ' ');
344   wattrset(win, selected ? button_key_active_attr : button_key_inactive_attr);
345   waddch(win, label[0]);
346   wattrset(win, selected ? button_active_attr : button_inactive_attr);
347   waddstr(win, label+1);
348   waddstr(win, selected ? "]" : " ");
349   wmove(win, y, x+temp+1);
350 }
351 /* End of print_button() */
352
353
354 /*
355  * Draw a rectangular box with line drawing characters
356  */
357 void draw_box(WINDOW *win, int y, int x, int height, int width, chtype box, chtype border)
358 {
359   int i, j;
360
361   wattrset(win, 0);
362   for (i = 0; i < height; i++) {
363     wmove(win, y + i, x);
364     for (j = 0; j < width; j++)
365       if (!i && !j)
366         waddch(win, border | ACS_ULCORNER);
367       else if (i == height-1 && !j)
368         waddch(win, border | ACS_LLCORNER);
369       else if (!i && j == width-1)
370         waddch(win, box | ACS_URCORNER);
371       else if (i == height-1 && j == width-1)
372         waddch(win, box | ACS_LRCORNER);
373       else if (!i)
374         waddch(win, border | ACS_HLINE);
375       else if (i == height-1)
376         waddch(win, box | ACS_HLINE);
377       else if (!j)
378         waddch(win, border | ACS_VLINE);
379       else if (j == width-1)
380         waddch(win, box | ACS_VLINE);
381       else
382         waddch(win, box | ' ');
383   }
384 }
385 /* End of draw_box() */
386
387
388 #ifdef HAVE_NCURSES
389 /*
390  * Draw shadows along the right and bottom edge to give a more 3D look
391  * to the boxes
392  */
393 void draw_shadow(WINDOW *win, int y, int x, int height, int width)
394 {
395   int i,sx,sy;
396   chtype attrs;
397
398   if (has_colors()) {    /* Whether terminal supports color? */
399     getbegyx(win,sy,sx);
400     attrs = getattrs(win);
401     if (y+height < getmaxy(win)) {
402         /* small touch */
403         wattrset(win, A_INVIS);
404         wmove(win, y + height, x + 2);
405         for (i = 0; i < width; i++)
406             if (i+x+2 < getmaxx(win))
407                waddch(win, ' ');
408         /* end touch */
409         wattrset(win, shadow_attr);
410         wmove(win, y + height, x + 2);
411         for (i = 0; i < width; i++)
412             if (i+x+2 < getmaxx(win))
413                waddch(win, mvwinch(newscr, sy+y+height, sx+x+2+i) & A_CHARTEXT);
414     }
415     if (x+width < getmaxx(win)) {
416         for (i = y + 1; i < y + height + 1; i++) {
417           if (i < getmaxy(win)) {
418               /* small touch */
419               wattrset(win, A_INVIS);
420               wmove(win, i, x + width);
421               waddch(win, ' ');
422               if (x+width+1 < getmaxx(win))
423                     waddch(win, ' ');
424               /* end touch */
425               wattrset(win, shadow_attr);
426               wmove(win, i, x + width);
427               waddch(win, mvwinch(newscr, sy+i, sx+x+width) & A_CHARTEXT);
428               if (x+width+1 < getmaxx(win))
429                     waddch(win, mvwinch(newscr, sy+i, sx+x+width+1) & A_CHARTEXT);
430           }
431         }
432     }
433     wattrset(win, attrs);
434     wnoutrefresh(win);
435   }
436 }
437 /* End of draw_shadow() */
438 #endif
439
440 void dialog_clear_norefresh(void)
441 {
442     attr_clear(stdscr, LINES, COLS, screen_attr);
443     touchwin(stdscr);
444     wnoutrefresh(stdscr);
445 }
446
447 void dialog_clear(void)
448 {
449     dialog_clear_norefresh();
450     doupdate();
451 }
452
453 void dialog_update(void)
454 {
455     refresh();
456 }
457
458 void end_dialog(void)
459 {
460     endwin();
461 }
462
463 int strwidth(const char *p)
464 {
465         int i = 0, len, incr;
466         const char *start, *s, *s1, *s2;
467
468         for (start = s = p; ; start = (s += incr)) {
469                 s1 = strchr(s, '\n');
470                 s2 = strstr(s, "\\n");
471                 if (s2 == NULL)
472                         s = s1;
473                 else if (s1 == NULL)
474                         s = s2;
475                 else
476                         s = MIN(s1, s2);
477                 if (s == NULL)
478                         break;
479                 incr = 1 + (s == s2);
480                 len = s - start;
481                 if (len > i)
482                         i = len;
483         }
484         len = strlen(start);
485         if (len > i)
486                 i = len;
487         return i;
488 }
489
490 int strheight(const char *p)
491 {
492         int i = 1, incr;
493         const char *s, *s1, *s2;
494
495         for (s = p; ; s += incr) {
496                 s1 = strchr(s, '\n');
497                 s2 = strstr(s, "\\n");
498                 if (s2 == NULL)
499                         s = s1;
500                 else if (s1 == NULL)
501                         s = s2;
502                 else
503                         s = MIN(s1, s2);
504                 if (s == NULL)
505                         break;
506                 incr = 1 + (s == s2);
507                 i++;
508         }
509         return i;
510 }
511
512 void print_arrows(WINDOW *dialog, int scroll, int menu_height, int item_no,
513                   int box_x, int box_y, int tag_x, int cur_x, int cur_y)
514 {
515     wmove(dialog, box_y, box_x + tag_x + 1);
516     wattrset(dialog, scroll ? uarrow_attr : menubox_attr);
517     waddch(dialog, scroll ? ACS_UARROW : ACS_HLINE);
518     wmove(dialog, box_y, box_x + tag_x + 2);
519     waddch(dialog, scroll ? '(' : ACS_HLINE);
520     wmove(dialog, box_y, box_x + tag_x + 3);
521     waddch(dialog, scroll ? '-' : ACS_HLINE);
522     wmove(dialog, box_y, box_x + tag_x + 4);
523     waddch(dialog, scroll ? ')' : ACS_HLINE);
524     wmove(dialog, box_y + menu_height + 1, box_x + tag_x + 1);
525     wattrset(dialog, scroll+menu_height < item_no ? darrow_attr : menubox_border_attr);
526     waddch(dialog, scroll+menu_height < item_no ? ACS_DARROW : ACS_HLINE);
527     wmove(dialog, box_y + menu_height + 1, box_x + tag_x + 2);
528     waddch(dialog, scroll+menu_height < item_no ? '(' : ACS_HLINE);
529     wmove(dialog, box_y + menu_height + 1, box_x + tag_x + 3);
530     waddch(dialog, scroll+menu_height < item_no ? '+' : ACS_HLINE);
531     wmove(dialog, box_y + menu_height + 1, box_x + tag_x + 4);
532     waddch(dialog, scroll+menu_height < item_no ? ')' : ACS_HLINE);
533     wmove(dialog, cur_y, cur_x);  /* Restore cursor position */
534 }
535