kconfig: lxdialog is now sparse clean
[firefly-linux-kernel-4.4.55.git] / scripts / lxdialog / util.c
1 /*
2  *  util.c
3  *
4  *  ORIGINAL AUTHOR: Savio Lam (lam836@cs.cuhk.hk)
5  *  MODIFIED FOR LINUX KERNEL CONFIG BY: William Roadcap (roadcap@cfw.com)
6  *
7  *  This program is free software; you can redistribute it and/or
8  *  modify it under the terms of the GNU General Public License
9  *  as published by the Free Software Foundation; either version 2
10  *  of the License, or (at your option) any later version.
11  *
12  *  This program is distributed in the hope that it will be useful,
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  *  GNU General Public License for more details.
16  *
17  *  You should have received a copy of the GNU General Public License
18  *  along with this program; if not, write to the Free Software
19  *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  */
21
22 #include "dialog.h"
23
24 /* use colors by default? */
25 bool use_colors = 1;
26
27 const char *backtitle = NULL;
28
29 /*
30  * Attribute values, default is for mono display
31  */
32 chtype attributes[] = {
33         A_NORMAL,               /* screen_attr */
34         A_NORMAL,               /* shadow_attr */
35         A_NORMAL,               /* dialog_attr */
36         A_BOLD,                 /* title_attr */
37         A_NORMAL,               /* border_attr */
38         A_REVERSE,              /* button_active_attr */
39         A_DIM,                  /* button_inactive_attr */
40         A_REVERSE,              /* button_key_active_attr */
41         A_BOLD,                 /* button_key_inactive_attr */
42         A_REVERSE,              /* button_label_active_attr */
43         A_NORMAL,               /* button_label_inactive_attr */
44         A_NORMAL,               /* inputbox_attr */
45         A_NORMAL,               /* inputbox_border_attr */
46         A_NORMAL,               /* searchbox_attr */
47         A_BOLD,                 /* searchbox_title_attr */
48         A_NORMAL,               /* searchbox_border_attr */
49         A_BOLD,                 /* position_indicator_attr */
50         A_NORMAL,               /* menubox_attr */
51         A_NORMAL,               /* menubox_border_attr */
52         A_NORMAL,               /* item_attr */
53         A_REVERSE,              /* item_selected_attr */
54         A_BOLD,                 /* tag_attr */
55         A_REVERSE,              /* tag_selected_attr */
56         A_BOLD,                 /* tag_key_attr */
57         A_REVERSE,              /* tag_key_selected_attr */
58         A_BOLD,                 /* check_attr */
59         A_REVERSE,              /* check_selected_attr */
60         A_BOLD,                 /* uarrow_attr */
61         A_BOLD                  /* darrow_attr */
62 };
63
64 #include "colors.h"
65
66 /*
67  * Table of color values
68  */
69 int color_table[][3] = {
70         {SCREEN_FG, SCREEN_BG, SCREEN_HL},
71         {SHADOW_FG, SHADOW_BG, SHADOW_HL},
72         {DIALOG_FG, DIALOG_BG, DIALOG_HL},
73         {TITLE_FG, TITLE_BG, TITLE_HL},
74         {BORDER_FG, BORDER_BG, BORDER_HL},
75         {BUTTON_ACTIVE_FG, BUTTON_ACTIVE_BG, BUTTON_ACTIVE_HL},
76         {BUTTON_INACTIVE_FG, BUTTON_INACTIVE_BG, BUTTON_INACTIVE_HL},
77         {BUTTON_KEY_ACTIVE_FG, BUTTON_KEY_ACTIVE_BG, BUTTON_KEY_ACTIVE_HL},
78         {BUTTON_KEY_INACTIVE_FG, BUTTON_KEY_INACTIVE_BG,
79          BUTTON_KEY_INACTIVE_HL},
80         {BUTTON_LABEL_ACTIVE_FG, BUTTON_LABEL_ACTIVE_BG,
81          BUTTON_LABEL_ACTIVE_HL},
82         {BUTTON_LABEL_INACTIVE_FG, BUTTON_LABEL_INACTIVE_BG,
83          BUTTON_LABEL_INACTIVE_HL},
84         {INPUTBOX_FG, INPUTBOX_BG, INPUTBOX_HL},
85         {INPUTBOX_BORDER_FG, INPUTBOX_BORDER_BG, INPUTBOX_BORDER_HL},
86         {SEARCHBOX_FG, SEARCHBOX_BG, SEARCHBOX_HL},
87         {SEARCHBOX_TITLE_FG, SEARCHBOX_TITLE_BG, SEARCHBOX_TITLE_HL},
88         {SEARCHBOX_BORDER_FG, SEARCHBOX_BORDER_BG, SEARCHBOX_BORDER_HL},
89         {POSITION_INDICATOR_FG, POSITION_INDICATOR_BG, POSITION_INDICATOR_HL},
90         {MENUBOX_FG, MENUBOX_BG, MENUBOX_HL},
91         {MENUBOX_BORDER_FG, MENUBOX_BORDER_BG, MENUBOX_BORDER_HL},
92         {ITEM_FG, ITEM_BG, ITEM_HL},
93         {ITEM_SELECTED_FG, ITEM_SELECTED_BG, ITEM_SELECTED_HL},
94         {TAG_FG, TAG_BG, TAG_HL},
95         {TAG_SELECTED_FG, TAG_SELECTED_BG, TAG_SELECTED_HL},
96         {TAG_KEY_FG, TAG_KEY_BG, TAG_KEY_HL},
97         {TAG_KEY_SELECTED_FG, TAG_KEY_SELECTED_BG, TAG_KEY_SELECTED_HL},
98         {CHECK_FG, CHECK_BG, CHECK_HL},
99         {CHECK_SELECTED_FG, CHECK_SELECTED_BG, CHECK_SELECTED_HL},
100         {UARROW_FG, UARROW_BG, UARROW_HL},
101         {DARROW_FG, DARROW_BG, DARROW_HL},
102 };                              /* color_table */
103
104 /*
105  * Set window to attribute 'attr'
106  */
107 void attr_clear(WINDOW * win, int height, int width, chtype attr)
108 {
109         int i, j;
110
111         wattrset(win, attr);
112         for (i = 0; i < height; i++) {
113                 wmove(win, i, 0);
114                 for (j = 0; j < width; j++)
115                         waddch(win, ' ');
116         }
117         touchwin(win);
118 }
119
120 void dialog_clear(void)
121 {
122         attr_clear(stdscr, LINES, COLS, screen_attr);
123         /* Display background title if it exists ... - SLH */
124         if (backtitle != NULL) {
125                 int i;
126
127                 wattrset(stdscr, screen_attr);
128                 mvwaddstr(stdscr, 0, 1, (char *)backtitle);
129                 wmove(stdscr, 1, 1);
130                 for (i = 1; i < COLS - 1; i++)
131                         waddch(stdscr, ACS_HLINE);
132         }
133         wnoutrefresh(stdscr);
134 }
135
136 /*
137  * Do some initialization for dialog
138  */
139 void init_dialog(void)
140 {
141         initscr();              /* Init curses */
142         keypad(stdscr, TRUE);
143         cbreak();
144         noecho();
145
146         if (use_colors)         /* Set up colors */
147                 color_setup();
148
149         dialog_clear();
150 }
151
152 /*
153  * Setup for color display
154  */
155 void color_setup(void)
156 {
157         int i;
158
159         if (has_colors()) {     /* Terminal supports color? */
160                 start_color();
161
162                 /* Initialize color pairs */
163                 for (i = 0; i < ATTRIBUTE_COUNT; i++)
164                         init_pair(i + 1, color_table[i][0], color_table[i][1]);
165
166                 /* Setup color attributes */
167                 for (i = 0; i < ATTRIBUTE_COUNT; i++)
168                         attributes[i] = C_ATTR(color_table[i][2], i + 1);
169         }
170 }
171
172 /*
173  * End using dialog functions.
174  */
175 void end_dialog(void)
176 {
177         endwin();
178 }
179
180 /*
181  * Print a string of text in a window, automatically wrap around to the
182  * next line if the string is too long to fit on one line. Newline
183  * characters '\n' are replaced by spaces.  We start on a new line
184  * if there is no room for at least 4 nonblanks following a double-space.
185  */
186 void print_autowrap(WINDOW * win, const char *prompt, int width, int y, int x)
187 {
188         int newl, cur_x, cur_y;
189         int i, prompt_len, room, wlen;
190         char tempstr[MAX_LEN + 1], *word, *sp, *sp2;
191
192         strcpy(tempstr, prompt);
193
194         prompt_len = strlen(tempstr);
195
196         /*
197          * Remove newlines
198          */
199         for (i = 0; i < prompt_len; i++) {
200                 if (tempstr[i] == '\n')
201                         tempstr[i] = ' ';
202         }
203
204         if (prompt_len <= width - x * 2) {      /* If prompt is short */
205                 wmove(win, y, (width - prompt_len) / 2);
206                 waddstr(win, tempstr);
207         } else {
208                 cur_x = x;
209                 cur_y = y;
210                 newl = 1;
211                 word = tempstr;
212                 while (word && *word) {
213                         sp = index(word, ' ');
214                         if (sp)
215                                 *sp++ = 0;
216
217                         /* Wrap to next line if either the word does not fit,
218                            or it is the first word of a new sentence, and it is
219                            short, and the next word does not fit. */
220                         room = width - cur_x;
221                         wlen = strlen(word);
222                         if (wlen > room ||
223                             (newl && wlen < 4 && sp
224                              && wlen + 1 + strlen(sp) > room
225                              && (!(sp2 = index(sp, ' '))
226                                  || wlen + 1 + (sp2 - sp) > room))) {
227                                 cur_y++;
228                                 cur_x = x;
229                         }
230                         wmove(win, cur_y, cur_x);
231                         waddstr(win, word);
232                         getyx(win, cur_y, cur_x);
233                         cur_x++;
234                         if (sp && *sp == ' ') {
235                                 cur_x++;        /* double space */
236                                 while (*++sp == ' ') ;
237                                 newl = 1;
238                         } else
239                                 newl = 0;
240                         word = sp;
241                 }
242         }
243 }
244
245 /*
246  * Print a button
247  */
248 void print_button(WINDOW * win, const char *label, int y, int x, int selected)
249 {
250         int i, temp;
251
252         wmove(win, y, x);
253         wattrset(win, selected ? button_active_attr : button_inactive_attr);
254         waddstr(win, "<");
255         temp = strspn(label, " ");
256         label += temp;
257         wattrset(win, selected ? button_label_active_attr
258                  : button_label_inactive_attr);
259         for (i = 0; i < temp; i++)
260                 waddch(win, ' ');
261         wattrset(win, selected ? button_key_active_attr
262                  : button_key_inactive_attr);
263         waddch(win, label[0]);
264         wattrset(win, selected ? button_label_active_attr
265                  : button_label_inactive_attr);
266         waddstr(win, (char *)label + 1);
267         wattrset(win, selected ? button_active_attr : button_inactive_attr);
268         waddstr(win, ">");
269         wmove(win, y, x + temp + 1);
270 }
271
272 /*
273  * Draw a rectangular box with line drawing characters
274  */
275 void
276 draw_box(WINDOW * win, int y, int x, int height, int width,
277          chtype box, chtype border)
278 {
279         int i, j;
280
281         wattrset(win, 0);
282         for (i = 0; i < height; i++) {
283                 wmove(win, y + i, x);
284                 for (j = 0; j < width; j++)
285                         if (!i && !j)
286                                 waddch(win, border | ACS_ULCORNER);
287                         else if (i == height - 1 && !j)
288                                 waddch(win, border | ACS_LLCORNER);
289                         else if (!i && j == width - 1)
290                                 waddch(win, box | ACS_URCORNER);
291                         else if (i == height - 1 && j == width - 1)
292                                 waddch(win, box | ACS_LRCORNER);
293                         else if (!i)
294                                 waddch(win, border | ACS_HLINE);
295                         else if (i == height - 1)
296                                 waddch(win, box | ACS_HLINE);
297                         else if (!j)
298                                 waddch(win, border | ACS_VLINE);
299                         else if (j == width - 1)
300                                 waddch(win, box | ACS_VLINE);
301                         else
302                                 waddch(win, box | ' ');
303         }
304 }
305
306 /*
307  * Draw shadows along the right and bottom edge to give a more 3D look
308  * to the boxes
309  */
310 void draw_shadow(WINDOW * win, int y, int x, int height, int width)
311 {
312         int i;
313
314         if (has_colors()) {     /* Whether terminal supports color? */
315                 wattrset(win, shadow_attr);
316                 wmove(win, y + height, x + 2);
317                 for (i = 0; i < width; i++)
318                         waddch(win, winch(win) & A_CHARTEXT);
319                 for (i = y + 1; i < y + height + 1; i++) {
320                         wmove(win, i, x + width);
321                         waddch(win, winch(win) & A_CHARTEXT);
322                         waddch(win, winch(win) & A_CHARTEXT);
323                 }
324                 wnoutrefresh(win);
325         }
326 }
327
328 /*
329  *  Return the position of the first alphabetic character in a string.
330  */
331 int first_alpha(const char *string, const char *exempt)
332 {
333         int i, in_paren = 0, c;
334
335         for (i = 0; i < strlen(string); i++) {
336                 c = tolower(string[i]);
337
338                 if (strchr("<[(", c))
339                         ++in_paren;
340                 if (strchr(">])", c) && in_paren > 0)
341                         --in_paren;
342
343                 if ((!in_paren) && isalpha(c) && strchr(exempt, c) == 0)
344                         return i;
345         }
346
347         return 0;
348 }