Debian Codegraph @

src/modules/frontend/ncurses/ncurses.c

raw
1 #include "common.h"
2 #include "template.h"
3 #include "question.h"
4 #include "frontend.h"
5 #include "database.h"
6 #include "ncurses_fe.h"
7 #include "strutl.h"
8
9 #include <ctype.h>
10 #include <string.h>
11 #include <stdio.h>
12 #include <stdarg.h>
13
14 #define WIN_QUERY 1
15 #define WIN_DESC 2
16 #define COLOR_FRAME 1
17 #define COLOR_QUERY 2
18 #define COLOR_DESC 3
19 #define UIDATA(obj) ((struct uidata *)(obj)->data)
20
21 static struct question_handlers {
22 const char *type;
23 int (*handler)(struct frontend *, struct question *q);
24 } question_handlers[] = {
25 { "boolean", nchandler_boolean },
26 { "multiselect", nchandler_multiselect },
27 { "note", nchandler_note },
28 { "password", nchandler_password },
29 { "select", nchandler_select },
30 { "string", nchandler_string },
31 { "text", nchandler_text },
32 { "error", nchandler_note },
33 };
34
35 /* Private variables */
36 struct uidata {
37 int qrylines, desclines;
38 WINDOW *qrywin, *descwin;
39 };
40
41 /* Slang compatibility functions */
42 #ifdef USESLANG
43 static int mvhline(int r, int c, int ch, int w)
44 {
45 int i;
46 if (ch == 0)
47 ch = ACS_HLINE;
48 for (i = 0; i < w; i++)
49 mvaddch(r, c + i, ch);
50 return 0;
51 }
52
53 static int mvvline(int r, int c, int ch, int h)
54 {
55 int i;
56 if (ch == 0)
57 ch = ACS_VLINE;
58 for (i = 0; i < h; i++)
59 mvaddch(r + i, c, ch);
60 return 0;
61 }
62
63 #endif
64
65 static void wfill(WINDOW *w)
66 {
67 int x, y, xmax, ymax;
68
69 getmaxyx(w, ymax, xmax);
70 for (x = 0; x < xmax; x++)
71 for (y = 0; y < ymax; y++)
72 mvwaddch(w, y, x, ' ');
73 }
74
75 static int can_go_back(struct frontend *ui)
76 {
77 return 1;
78 }
79
80 static int can_go_forward(struct frontend *ui)
81 {
82 return 1;
83 }
84
85 static int longestlen(char **strs, int count)
86 {
87 int i, max = 0;
88
89 for (i = 0; i < count; i++)
90 {
91 if (strlen(strs[i]) > max)
92 max = strlen(strs[i]);
93 }
94 return max;
95 }
96
97 /* not needed (yet?)
98 static void attrprintf(WINDOW *win, int y, int x, chtype attr, char *fmt, ...)
99 #ifdef __GNUC__
100 __attribute__((format(printf, 5, 6)))
101 #endif
102 {
103 char *buf = 0;
104 va_list ap;
105
106 va_start(ap, fmt);
107 vasprintf(&buf, fmt, ap);
108 va_end(ap);
109
110 while (*buf)
111 mvwaddch(win, y, x++, ((unsigned char)(*buf++))|attr);
112 }
113 */
114
115 static int wrapprint(WINDOW *win, const char *str, int ystart, const int width)
116 {
117 /* Simple greedy line-wrapper */
118 int len = STRLEN(str);
119 char line[width+1];
120 const char *s, *e, *end, *lb;
121
122 if (str == 0) return ystart;
123
124 s = e = str;
125 end = str + len;
126
127 while (len > 0)
128 {
129 /* try to fit the most characters */
130 e = s + width;
131
132 if (e >= end)
133 {
134 e = end;
135 }
136 else
137 {
138 while (e > s && isalnum(*e)) e--;
139 e++;
140 }
141 /* no word-break point found, so just break the line */
142 if (e == s) e = s + width;
143
144 /* if there's an explicit linebreak, honor it */
145 lb = strchr(s, '\n');
146 if (lb != NULL && lb < e) e = lb + 1;
147
148 strncpy(line, s, e-s);
149 line[e-s] = 0;
150 mvwprintw(win, ystart++, 0, "%s%s", line, (lb == NULL || lb >= e ? "\n" : ""));
151
152 len -= (e-s);
153 s = e;
154 while (*s == ' ') s++;
155 }
156 return ystart;
157 }
158
159 static void drawframe(struct frontend *ui, const int window, const char *title)
160 {
161 struct uidata *data = UIDATA(ui);
162 int ytop, ybottom;
163
164 switch (window)
165 {
166 case WIN_QUERY:
167 ytop = 0;
168 ybottom = data->qrylines - 1;
169 break;
170 case WIN_DESC:
171 ytop = data->qrylines;
172 ybottom = data->qrylines + data->desclines - 1;
173 break;
174 }
175 /* draw the borders */
176 mvvline(ytop + 1, 0, 0, ybottom-ytop-1);
177 mvvline(ytop + 1, COLS-1, 0, ybottom-ytop-1);
178 mvhline(ytop, 1, 0, COLS-2);
179 mvhline(ybottom, 1, 0, COLS-2);
180
181 /* draw the corners */
182 mvaddch(ytop, 0, ACS_ULCORNER);
183 mvaddch(ytop, COLS-1, ACS_URCORNER);
184 mvaddch(ybottom, 0, ACS_LLCORNER);
185 mvaddch(ybottom, COLS-1, ACS_LRCORNER);
186
187 /* draw the title */
188 if (title != 0)
189 {
190 mvprintw(ytop, (COLS-strlen(title))/2, "%s", title);
191 }
192 }
193
194 static void drawnavbuttons(struct frontend *ui, int selected)
195 {
196 struct uidata *data = UIDATA(ui);
197 WINDOW *win = data->qrywin;
198 int ybut = data->qrylines - 2;
199
200 mvhline(ybut - 1, 1, 0, COLS-2);
201 mvaddch(ybut - 1, 0, ACS_LTEE);
202 mvaddch(ybut - 1, COLS-1, ACS_RTEE);
203 refresh();
204
205 /* draw the actual buttons, note that these are drawn in the
206 * query window instead of in the parent (like the frame) */
207
208 if (can_go_back(ui))
209 {
210 if (selected == 0) wstandout(win); else wstandend(win);
211 mvwprintw(win, ybut-1, 2, "<Previous>");
212 }
213
214 if (can_go_forward(ui))
215 {
216 if (selected == 1) wstandout(win); else wstandend(win);
217 mvwprintw(win, ybut-1, COLS-10, "<Next>");
218 }
219 wstandend(win);
220
221 /* caller should call wrefresh() ! */
222 }
223
224 static void drawdesc(struct frontend *ui, struct question *q)
225 {
226 WINDOW *qrywin = UIDATA(ui)->qrywin;
227 WINDOW *descwin = UIDATA(ui)->descwin;
228 char *descr = q_get_description(ui, q);
229 char *ext_descr = q_get_extended_description(ui, q);
230
231 drawframe(ui, WIN_QUERY, ui->title);
232 wrapprint(qrywin, descr, 0, COLS-2);
233 if (*ext_descr)
234 wrapprint(descwin, ext_descr, 0, COLS-2);
235 wclrtobot(qrywin);
236 wclrtobot(descwin);
237 wrefresh(stdscr);
238 wrefresh(qrywin);
239 wrefresh(descwin);
240 free(descr);
241 free(ext_descr);
242 }
243
244 static int nchandler_boolean(struct frontend *ui, struct question *q)
245 {
246 const char *value = "true";
247 int ret = 0, ans, pos = 2;
248 int ybut = UIDATA(ui)->qrylines - 6;
249 const char *dft = question_getvalue(q, "");
250 WINDOW *win = UIDATA(ui)->qrywin;
251
252 if (dft)
253 value = dft;
254
255 ans = (strcmp(value, "true") == 0);
256
257 while (ret == 0)
258 {
259 /* Draw the radio boxes */
260 if (pos == 2) wstandout(win); else wstandend(win);
261 mvwprintw(win, ybut, (COLS/2)-11, " (%c) Yes ",
262 (ans ? '*' : ' '));
263 if (pos == 3) wstandout(win); else wstandend(win);
264 mvwprintw(win, ybut, (COLS/2)+4, " (%c) No ",
265 (ans ? ' ' : '*'));
266 wstandend(win);
267
268 drawnavbuttons(ui, pos);
269
270 wrefresh(win);
271
272 switch (getch())
273 {
274 case KEY_LEFT:
275 case KEY_UP:
276 pos--;
277 if (pos < 0) pos = 3;
278 break;
279 case KEY_RIGHT:
280 case KEY_DOWN:
281 case 9: /* tab */
282 pos++;
283 if (pos > 3) pos = 0;
284 break;
285 case ' ':
286 case '\r':
287 case '\n':
288 case KEY_ENTER:
289 switch (pos)
290 {
291 case 0:
292 if (can_go_back(ui))
293 ret = DC_GOBACK;
294 /* go to previous if possible */
295 break;
296 case 1:
297 ret = DC_OK;
298 case 2: ans = 1; break;
299 case 3: ans = 0; break;
300 }
301 }
302 }
303
304 if (ret == DC_OK)
305 question_setvalue(q, (ans ? "true" : "false"));
306
307 return ret;
308 }
309
310 static int nchandler_multiselect(struct frontend *ui, struct question *q)
311 {
312 return 0;
313 }
314
315 static int nchandler_note(struct frontend *ui, struct question *q)
316 {
317 int ret = 0, pos = 0;
318
319 /* TODO scrolling support */
320
321 while (ret == 0)
322 {
323 drawnavbuttons(ui, pos);
324 wrefresh(UIDATA(ui)->qrywin);
325
326 switch (getch())
327 {
328 case KEY_LEFT:
329 case KEY_UP:
330 case KEY_RIGHT:
331 case KEY_DOWN:
332 case 9: /* tab */
333 if (pos == 0) pos = 1; else pos = 0;
334 break;
335 case ' ':
336 case '\r':
337 case '\n':
338 case KEY_ENTER:
339 switch (pos)
340 {
341 case 0:
342 if (can_go_back(ui))
343 ret = DC_GOBACK;
344 /* go to previous if possible */
345 break;
346 case 1:
347 ret = DC_OK;
348 break;
349 }
350 }
351 }
352 return ret;
353 }
354
355 static int nchandler_password(struct frontend *ui, struct question *q)
356 {
357 return 0;
358 }
359
360 static int nchandler_select(struct frontend *ui, struct question *q)
361 {
362 char *choices[100] = {0};
363 char *choices_translated[100] = {0};
364 char *defaults[100] = {0};
365 const char *defval = question_getvalue(q, "");
366
367 int i, count, dcount, ret = 0, def = -1, pos = 2, xpos, ypos;
368 int top, bottom, longest;
369 WINDOW *win = UIDATA(ui)->qrywin;
370
371 /* Parse out all the choices */
372 count = strchoicesplit(q_get_choices_vals(ui, q), choices, DIM(choices));
373 if (count <= 0) return DC_NOTOK;
374 if (count == 1)
375 defval = choices[0];
376
377 strchoicesplit(q_get_choices(ui, q), choices_translated, DIM(choices_translated));
378 dcount = strchoicesplit(question_get_field(ui, q, "C", "value"), defaults, DIM(defaults));
379
380 /* See what the currently selected value should be -- either a
381 * previously selected value, or the default for the question
382 */
383 if (defval != NULL)
384 {
385 for (i = 0; i < count; i++)
386 if (strcmp(choices[i], defval) == 0)
387 def = i + 1;
388 }
389
390 longest = longestlen(choices_translated, count);
391 top = 0; bottom = MIN(count, UIDATA(ui)->qrylines-5);
392 xpos = (COLS-longest)/2-1;
393
394 while (ret == 0)
395 {
396 ypos = 2;
397 for (i = top; i < bottom; i++)
398 {
399 if (pos == 2 && i == def)
400 wstandout(win);
401 else
402 wstandend(win);
403 mvwprintw(win, ypos++, xpos, " %-*s ", longest, choices_translated[i]);
404 }
405 wstandend(win);
406
407 drawnavbuttons(ui, pos);
408
409 wrefresh(win);
410
411 switch (getch())
412 {
413 case KEY_LEFT:
414 case KEY_UP:
415 def--;
416 if (def < 0) def = count-1;
417
418 /* check def against top/bottom */
419 break;
420 case KEY_RIGHT:
421 case KEY_DOWN:
422 def++;
423 if (def >= count) def = 0;
424
425 /* check def against top/bottom */
426 break;
427 case 9: /* TAB */
428 pos++;
429 if (pos > 2) pos = 0;
430 break;
431 case ' ':
432 case '\r':
433 case '\n':
434 case KEY_ENTER:
435 switch (pos)
436 {
437 case 0:
438 if (can_go_back(ui))
439 ret = DC_GOBACK;
440 /* go to previous if possible */
441 break;
442 case 1:
443 ret = DC_OK;
444 break;
445 }
446 }
447 }
448 if (ret == DC_OK)
449 question_setvalue(q, choices[def]);
450 return ret;
451 }
452
453 static int nchandler_string(struct frontend *ui, struct question *q)
454 {
455 return 0;
456 }
457
458 static int nchandler_text(struct frontend *ui, struct question *q)
459 {
460 return 0;
461 }
462
463 /* ----------------------------------------------------------------------- */
464
465 static int ncurses_initialize(struct frontend *obj, struct configuration *cfg)
466 {
467 struct uidata *uid = NEW(struct uidata);
468 memset(uid, 0, sizeof(struct uidata));
469 obj->interactive = 1;
470 obj->data = uid;
471
472 initscr();
473 start_color();
474 cbreak();
475 noecho();
476 nonl();
477 keypad(stdscr, TRUE);
478
479 uid->qrylines = 3 * LINES / 5;
480 uid->desclines = LINES - uid->qrylines;
481
482 uid->qrywin = newwin(uid->qrylines - 2, COLS - 2, 1, 1);
483 uid->descwin = newwin(uid->desclines - 3, COLS - 2, uid->qrylines + 1, 1);
484
485 if (uid->qrywin == NULL || uid->descwin == NULL)
486 return DC_NOTOK;
487
488 init_pair(COLOR_FRAME, COLOR_YELLOW, COLOR_BLUE);
489 init_pair(COLOR_DESC, COLOR_WHITE, COLOR_BLUE);
490 init_pair(COLOR_QUERY, COLOR_WHITE, COLOR_BLUE);
491 init_pair(COLOR_DESC, COLOR_WHITE, COLOR_BLUE);
492
493 attrset(COLOR_PAIR(COLOR_QUERY)|' ');
494 wfill(stdscr);
495 wattrset(uid->qrywin, COLOR_PAIR(COLOR_QUERY)|A_BOLD|' ');
496 wfill(uid->qrywin);
497 wattrset(uid->descwin, COLOR_PAIR(COLOR_DESC)|A_BOLD|' ');
498 wfill(uid->descwin);
499
500 drawframe(obj, WIN_QUERY, NULL);
501 drawframe(obj, WIN_DESC, NULL);
502
503 refresh();
504
505 getch();
506
507 return DC_OK;
508 }
509
510 static int ncurses_shutdown(struct frontend *obj)
511 {
512 struct uidata *uid = UIDATA(obj);
513 delwin(uid->qrywin);
514 delwin(uid->descwin);
515 endwin();
516 DELETE(uid);
517 return DC_OK;
518 }
519
520 static int ncurses_go(struct frontend *obj)
521 {
522 struct question *q = obj->questions;
523 struct question *qlast = 0;
524 int i;
525 int ret;
526
527 while (q != 0)
528 {
529 ret = DC_OK;
530 for (i = 0; i < DIM(question_handlers); i++)
531 if (strcasecmp(q->template->type, question_handlers[i].type) == 0)
532 {
533 drawdesc(obj, q);
534 ret = question_handlers[i].handler(obj, q);
535 switch (ret)
536 {
537 case DC_OK:
538 frontend_qdb_set(obj->qdb, q, 0);
539 break;
540 case DC_GOBACK:
541 if (qlast != 0)
542 q = qlast;
543 else
544 return ret;
545 break;
546 default:
547 return ret;
548 }
549 break;
550 }
551 if (i == DIM(question_handlers))
552 return DC_NOTIMPL;
553
554 if (ret == DC_OK)
555 {
556 qlast = q;
557 q = q->next;
558 }
559
560 }
561
562 return DC_OK;
563 }
564
565 struct frontend_module debconf_frontend_module =
566 {
567 initialize: ncurses_initialize,
568 shutdown: ncurses_shutdown,
569 go: ncurses_go,
570 };