Debian Codegraph @

src/modules/frontend/slang/slang.c

raw
1 /***********************************************************************
2 *
3 * cdebconf - An implementation of the Debian Configuration Management
4 * System
5 *
6 * File: slang.c
7 *
8 * Description: SLang-based cdebconf UI module
9 *
10 * cdebconf is (c) 2000-2001 Randolph Chung and others under the following
11 * license.
12 *
13 * Redistribution and use in source and binary forms, with or without
14 * modification, are permitted provided that the following conditions
15 * are met:
16 *
17 * 1. Redistributions of source code must retain the above copyright
18 * notice, this list of conditions and the following disclaimer.
19 *
20 * 2. Redistributions in binary form must reproduce the above copyright
21 * notice, this list of conditions and the following disclaimer in the
22 * documentation and/or other materials provided with the distribution.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 *
36 ***********************************************************************/
37 #include "common.h"
38 #include "configuration.h"
39 #include "debug.h"
40 #include "template.h"
41 #include "question.h"
42 #include "frontend.h"
43 #include "database.h"
44 #include "slang_fe.h"
45 #include "strutl.h"
46
47 #include <ctype.h>
48 #include <string.h>
49 #include <stdio.h>
50 #include <stdarg.h>
51
52 #define WIN_QUERY 1
53 #define WIN_DESC 2
54 #define WIN_PROGRESS 3
55 #define LINES (SLtt_Screen_Rows ? SLtt_Screen_Rows : 24)
56 #define COLS (SLtt_Screen_Cols ? SLtt_Screen_Cols : 80)
57 #define UIDATA(obj) ((struct uidata *)(obj)->data)
58
59 /* Private variables */
60 struct uidata {
61 struct slwindow qrywin, descwin, progwin;
62 int descstart;
63 };
64
65 /*
66 * slang_flush
67 *
68 * flushes all pending draw commands to the screen, and moves the cursor to
69 * the bottom right corner
70 */
71 static void slang_flush(void)
72 {
73 SLsmg_gotorc(SLtt_Screen_Rows-1, SLtt_Screen_Cols-1);
74 SLsmg_refresh();
75 }
76
77 /*
78 * slang_setcolor
79 *
80 * sets the color of a given object from the configuration
81 */
82 static void slang_setcolor(struct frontend *ui, int *handle, const char *obj,
83 const char *defaultfg, const char *defaultbg)
84 {
85 static int colorsused = 0;
86 char fgname[50], bgname[50];
87
88 snprintf(fgname, sizeof(fgname), "%s::%s_fg", ui->configpath, obj);
89 snprintf(bgname, sizeof(bgname), "%s::%s_bg", ui->configpath, obj);
90 *handle = colorsused++;
91 SLtt_set_color(*handle, 0, (char *)ui->config->get(ui->config, fgname,
92 defaultfg), (char *)ui->config->get(ui->config, bgname,
93 defaultbg));
94 }
95
96 /*
97 * slang_initwin
98 *
99 * initializes the various "windows" -- setup positions, colors, etc
100 */
101 static void slang_initwin(struct frontend *ui, int type, struct slwindow *win)
102 {
103 switch (type)
104 {
105 case WIN_QUERY:
106 win->border = 1;
107 win->x = win->y = 0;
108 win->w = COLS;
109 win->h = LINES*2/3;
110 slang_setcolor(ui, &win->drawcolor, "query::draw", "yellow",
111 "blue");
112 slang_setcolor(ui, &win->fillcolor, "query::fill", "blue",
113 "blue");
114 slang_setcolor(ui, &win->selectedcolor, "query::selected",
115 "black", "yellow");
116 break;
117 case WIN_DESC:
118 win->border = 1;
119 win->x = 0;
120 win->y = LINES*2/3;
121 win->w = COLS;
122 win->h = LINES - win->y;
123 slang_setcolor(ui, &win->drawcolor, "desc::draw", "yellow",
124 "blue");
125 slang_setcolor(ui, &win->fillcolor, "desc::fill", "blue",
126 "blue");
127 slang_setcolor(ui, &win->selectedcolor, "desc::selected",
128 "black", "yellow");
129 break;
130 case WIN_PROGRESS:
131 win->border = 1;
132 win->x = 2;
133 win->y = (LINES*2/3) / 2 - 1;
134 win->w = COLS - 4;
135 win->h = 3;
136 slang_setcolor(ui, &win->drawcolor, "progress::draw", "yellow",
137 "blue");
138 slang_setcolor(ui, &win->fillcolor, "progress::fill", "blue",
139 "blue");
140 slang_setcolor(ui, &win->donecolor, "progress::done", "white",
141 "red");
142 break;
143 default:
144 INFO(INFO_DEBUG, "Unrecognized window type");
145 }
146 }
147
148 /*
149 * slang_drawwin
150 *
151 * Draws a window onto the screen; if the window has a border, draw that too
152 */
153 static void slang_drawwin(const struct slwindow *win)
154 {
155 SLsmg_set_color(win->fillcolor);
156 SLsmg_fill_region(win->y, win->x, win->h, win->w, ' ');
157
158 SLsmg_set_color(win->drawcolor);
159 if (win->border > 0)
160 SLsmg_draw_box(win->y, win->x, win->h, win->w);
161 }
162
163 /*
164 * slang_wrapprint
165 *
166 * print a string to a window with linewrap
167 */
168 static void slang_wrapprint(struct slwindow *win, const char *str, int start)
169 {
170 int i, lc;
171 char *lines[500];
172
173 lc = strwrap(str, win->w - win->border * 2 - 1, lines, DIM(lines));
174
175 SLsmg_set_color(win->drawcolor);
176 for (i = start; i < lc && (i - start) < (win->h - win->border * 2); i++)
177 {
178 SLsmg_gotorc(win->y + win->border + i - start, win->x + win->border);
179 SLsmg_write_string(lines[i]);
180 DELETE(lines[i]);
181 }
182
183 if (start > 0)
184 {
185 SLsmg_gotorc(win->y + win->border, win->x + win->w - 1);
186 SLsmg_write_char('^');
187 }
188 if (i < lc)
189 {
190 SLsmg_gotorc(win->y + win->h - 2, win->x + win->w - 1);
191 SLsmg_write_char('v');
192 }
193 }
194
195 /*
196 * slang_drawdesc
197 *
198 * draws the description for a question onto the screen
199 */
200 static void slang_drawdesc(struct frontend *ui, struct question *q)
201 {
202 struct uidata *uid = UIDATA(ui);
203 char *descr = q_get_description(ui, q);
204 char *ext_descr = q_get_extended_description(ui, q);
205
206 /* Clear the windows */
207 slang_drawwin(&uid->qrywin);
208 slang_drawwin(&uid->descwin);
209 /* Draw in the descriptions */
210 slang_wrapprint(&uid->qrywin, descr, 0);
211 slang_wrapprint(&uid->descwin, ext_descr, uid->descstart);
212 free(descr);
213 free(ext_descr);
214
215 /* caller should call slang_flush() ! */
216 }
217
218 #ifdef __GNUC__
219 static void slang_printf(int r, int c, int col, char *fmt, ...)
220 __attribute__((format(printf, 4, 5)));
221 #endif
222
223 /*
224 * slang_printf
225 *
226 * prints the given string at a specific screen coordinate and color
227 */
228 static void slang_printf(int r, int c, int col, char *fmt, ...)
229 {
230 va_list ap;
231
232 SLsmg_gotorc(r, c);
233 SLsmg_set_color(col);
234 va_start(ap, fmt);
235 SLsmg_vprintf(fmt, ap);
236 va_end(ap);
237 }
238
239 /*
240 * slang_keyhandler
241 *
242 * common portion of a keyboard handler for each of the widgets; handles
243 * scrolling between the navigation buttons and other active widgets. When
244 * the user presses ENTER the currently active element is returned, otherwise
245 * returns -1
246 */
247 static int slang_keyhandler(struct frontend *ui, struct question *q, int *pos,
248 int maxpos, int ch)
249 {
250 struct uidata *uid = UIDATA(ui);
251
252 if (ch == 0) ch = SLkp_getkey();
253 switch (ch)
254 {
255 case SL_KEY_PPAGE:
256 uid->descstart -= (uid->descwin.h / 2);
257 if (uid->descstart < 0) uid->descstart = 0;
258 slang_drawdesc(ui, q);
259 break;
260 case SL_KEY_NPAGE:
261 uid->descstart += (uid->descwin.h / 2);
262 slang_drawdesc(ui, q);
263 break;
264 case SL_KEY_LEFT:
265 case SL_KEY_UP:
266 (*pos)--;
267 if (*pos == 1 && !ui->methods.can_go_back(ui, q)) (*pos)--;
268 if (*pos < 0) *pos = maxpos;
269 break;
270 case SL_KEY_RIGHT:
271 case SL_KEY_DOWN:
272 case 9: /* tab */
273 (*pos)++;
274 if (*pos == 1 && !ui->methods.can_go_back(ui, q)) (*pos)++;
275 if (*pos > maxpos) *pos = 0;
276 break;
277 case ' ':
278 case '\r':
279 case '\n':
280 case SL_KEY_ENTER:
281 return *pos;
282 break;
283 }
284
285 return -1;
286 }
287
288 static char *get_text(struct frontend *obj, const char *template, char *fallback)
289 {
290 struct question *q = obj->qdb->methods.get(obj->qdb, template);
291 return q ? q_get_description(obj, q) : fallback;
292 }
293
294 static char *button_text(struct frontend *obj, const char *template, char *fallback)
295 {
296 char text[50];
297 struct question *q = obj->qdb->methods.get(obj->qdb, template);
298 sprintf(text, "< %s >", q ? q_get_description(obj, q) : fallback);
299 return strdup(text);
300 }
301
302
303
304 /*
305 * slang_navbuttons
306 *
307 * draws the navigation buttons (previous, next) onto the screen
308 *
309 * TODO: if can_go_back/forward is false, draw the buttons in a "disabled"
310 * color
311 */
312 static void slang_navbuttons(struct frontend *ui, struct question *q,
313 int selected)
314 {
315 struct uidata *uid = UIDATA(ui);
316 struct slwindow *win = &uid->qrywin;
317 int ybut = win->y + win->h - win->border;
318 static char *continue_text = NULL, *goback_text;
319
320 SLsmg_set_color(win->drawcolor);
321 SLsmg_gotorc(ybut-2, 1);
322 SLsmg_draw_hline(COLS-2);
323 SLsmg_set_char_set(1);
324 SLsmg_gotorc(ybut-2, 0);
325 SLsmg_write_char(SLSMG_LTEE_CHAR);
326 SLsmg_gotorc(ybut-2, COLS-1);
327 SLsmg_write_char(SLSMG_RTEE_CHAR);
328 SLsmg_set_char_set(0);
329
330 /* Get the translated button labels */
331 if (!continue_text)
332 continue_text = button_text(ui, "debconf/button-continue", "Continue");
333 if (!goback_text)
334 goback_text = button_text(ui, "debconf/button-goback", "Go Back");
335
336 /* draw the actual buttons, note that these are drawn in the
337 * query window instead of in the parent (like the frame) */
338
339 if (ui->methods.can_go_forward(ui, q))
340 {
341 slang_printf(ybut - 1, 2, (selected == 0 ?
342 win->selectedcolor : win->drawcolor), continue_text);
343 }
344
345 if (ui->methods.can_go_back(ui, q))
346 {
347
348 slang_printf(ybut - 1, COLS-2-strlen(goback_text),
349 (selected == 1 ? win->selectedcolor : win->drawcolor),
350 goback_text);
351 }
352
353 /* caller should call slang_flush() ! */
354 }
355
356 /* --------------------------------------------------------------------- */
357 static int slang_boolean(struct frontend *ui, struct question *q)
358 {
359 struct uidata *uid = UIDATA(ui);
360 struct slwindow *win = &uid->qrywin;
361 int ybut = win->y + win->h - win->border - 4;
362 const char *value = "true";
363 int ret = 0, ans, pos;
364 static char *yes_text = NULL, *no_text = NULL;
365
366 if (!yes_text) yes_text = get_text(ui, "debconf/button-yes", "Yes");
367 if (!no_text) no_text = get_text(ui, "debconf/button-no", "No");
368
369 value = question_get_field(ui, q, "C", "value");
370
371 ans = (strcmp(value, "true") == 0);
372 pos = (ans ? 2 : 3);
373
374 while (ret == 0)
375 {
376 /* Draw the radio boxes */
377 slang_printf(ybut, (COLS/2)-strlen(yes_text)-8,
378 (pos == 2 ? win->selectedcolor : win->drawcolor),
379 " (%c) %s ", (ans ? '*' : ' '), yes_text);
380 slang_printf(ybut, (COLS/2)+4,
381 (pos == 3 ? win->selectedcolor : win->drawcolor),
382 " (%c) %s ", (ans ? ' ' : '*'), no_text);
383
384 slang_navbuttons(ui, q, pos);
385
386 slang_flush();
387
388 switch (slang_keyhandler(ui, q, &pos, 3, 0))
389 {
390 case 0: ret = DC_OK; break;
391 case 1: ret = DC_GOBACK; break;
392 case 2: ans = 1; break;
393 case 3: ans = 0; break;
394 }
395 }
396
397 if (ret == DC_OK)
398 question_setvalue(q, (ans ? "true" : "false"));
399
400 return ret;
401 }
402
403 static int slang_note(struct frontend *ui, struct question *q)
404 {
405 int ret = 0, pos = 0;
406
407 while (ret == 0)
408 {
409 slang_navbuttons(ui, q, pos);
410 slang_flush();
411
412 switch (slang_keyhandler(ui, q, &pos, 1, 0))
413 {
414 case 0: ret = DC_OK; break;
415 case 1: ret = DC_GOBACK; break;
416 }
417 }
418 return ret;
419 }
420
421 static int slang_getselect(struct frontend *ui, struct question *q, int multi)
422 {
423 char **choices;
424 char **choices_translated;
425 char **defaults;
426 char *selected;
427 char answer[1024] = {0};
428 int *tindex = NULL;
429 const char *indices = q_get_indices(ui, q);
430 int i, j, count, dcount, ret = 0, val = 0, pos = 2, xpos, ypos;
431 int top, bottom, longest, ch;
432 struct uidata *uid = UIDATA(ui);
433 struct slwindow *win = &uid->qrywin;
434
435 /* Parse out all the choices */
436 count = strgetargc(q_get_choices_vals(ui, q));
437 if (count <= 0)
438 return DC_NOTOK;
439 choices = malloc(sizeof(char *) * count);
440 choices_translated = malloc(sizeof(char *) * count);
441 tindex = malloc(sizeof(int) * count);
442 if (strchoicesplitsort(q_get_choices_vals(ui, q), q_get_choices(ui, q), indices, choices, choices_translated, tindex, count) != count)
443 return DC_NOTOK;
444
445 defaults = malloc(sizeof(char *) * count);
446 dcount = strchoicesplit(question_get_field(ui, q, "C", "value"), defaults, count);
447 INFO(INFO_VERBOSE, "Parsed out %d choices, %d defaults", count, dcount);
448 if (dcount <= 0) return DC_NOTOK;
449 if (count == 1 && !multi)
450 {
451 dcount = 1;
452 defaults[0] = choices[0];
453 }
454
455 /* See what the currently selected value should be -- either a
456 * previously selected value, or the default for the question
457 * Loop in descending order so that cursor is at the first
458 * selected value
459 */
460 selected = malloc(sizeof(char) * count);
461 for (i = count-1; i >=0; i--)
462 {
463 selected[tindex[i]] = 0;
464 for (j = 0; j < dcount; j++)
465 if (strcmp(choices[tindex[i]], defaults[j]) == 0)
466 {
467 selected[tindex[i]] = 1;
468 val = i;
469 }
470 }
471
472 longest = strlongest(choices, count);
473 top = val - win->h + 8;
474 if (top < 0) top = 0;
475 xpos = (COLS-longest)/2-1;
476
477 while (ret == 0)
478 {
479 ypos = 3;
480 bottom = top + MIN(count, win->h - 7);
481
482 INFO(INFO_VERBOSE, "[val: %d, top: %d, bottom: %d]", val,
483 top, bottom);
484 for (i = top; i < bottom; i++)
485 {
486 slang_printf(ypos++, xpos, ((pos == 2 && i == val) ?
487 win->selectedcolor : win->drawcolor),
488 "(%c) %-*s ", (selected[tindex[i]] ? '*' : ' '),
489 longest, choices_translated[i]);
490
491 INFO(INFO_VERBOSE, "(%c) %-*s", (selected[tindex[i]] ? '*' :
492 ' '), longest, choices_translated[i]);
493 }
494
495 slang_navbuttons(ui, q, pos);
496 slang_flush();
497
498 switch ((ch = SLkp_getkey()))
499 {
500 case SL_KEY_LEFT:
501 case SL_KEY_UP:
502 val--;
503 if (val < 0)
504 {
505 val = count-1;
506 top = val - win->h + 8;
507 if (top < 0) top = 0;
508 }
509 if (val < top) top = val;
510 /* TODO: check val against top/bottom */
511 break;
512 case SL_KEY_RIGHT:
513 case SL_KEY_DOWN:
514 val++;
515 if (val >= count)
516 {
517 val = 0;
518 top = 0;
519 }
520 if (val >= bottom) top++;
521 /* TODO: check val against top/bottom */
522 break;
523 case 9: /* tab */
524 pos++;
525 if (pos == 1 && !ui->methods.can_go_back(ui, q)) pos++;
526 if (pos > 2) pos = 0;
527 break;
528 default:
529 switch (slang_keyhandler(ui, q, &pos, 2, ch))
530 {
531 case 0: ret = DC_OK; break;
532 case 1: ret = DC_GOBACK; break;
533 case -1: break;
534 default:
535 if (multi == 0)
536 {
537 memset(selected, 0, sizeof(*selected) * count);
538 selected[tindex[val]] = 1;
539 }
540 else
541 {
542 selected[tindex[val]] = !selected[tindex[val]];
543 }
544 }
545 }
546 }
547 if (ret != DC_OK)
548 {
549 free(selected);
550 return ret;
551 }
552
553 for (i = 0; i < count; i++)
554 {
555 if (selected[i])
556 {
557 if (answer[0] != 0)
558 strvacat(answer, sizeof(answer), ", ", NULL);
559 strvacat(answer, sizeof(answer), choices[i], NULL);
560 }
561 free(choices[i]);
562 free(choices_translated[i]);
563 }
564 free(choices);
565 free(choices_translated);
566 for (i = 0; i < dcount; i++)
567 free(defaults[i]);
568 free(defaults);
569 free(selected);
570 question_setvalue(q, answer);
571
572 free(tindex);
573
574 return DC_OK;
575 }
576
577 static int slang_select(struct frontend *ui, struct question *q)
578 {
579 INFO(INFO_VERBOSE, "calling getselect");
580 return slang_getselect(ui, q, 0);
581 }
582
583 static int slang_multiselect(struct frontend *ui, struct question *q)
584 {
585 return slang_getselect(ui, q, 1);
586 }
587
588 static int slang_getstring(struct frontend *ui, struct question *q, char showch)
589 {
590 char value[1024] = {0};
591 int ret = 0, pos = 2, ch;
592 struct uidata *uid = UIDATA(ui);
593 struct slwindow *win = &uid->qrywin;
594 int xpos = 0, ypos = win->y + win->border + 4;
595 int cursor;
596
597 STRCPY(value, question_get_field(ui, q, "C", "value"));
598 cursor = strlen(value);
599
600 /* TODO: scrolling */
601 while (ret == 0)
602 {
603 if (showch == 0)
604 {
605 slang_printf(ypos, 2, win->selectedcolor, "%-*s",
606 win->w - 5, value);
607 }
608 else
609 {
610 slang_printf(ypos, 2, win->selectedcolor, "%-*s",
611 win->w - 5, " ");
612 for (xpos = 0; xpos < strlen(value); xpos++)
613 {
614 SLsmg_gotorc(ypos, xpos+2);
615 SLsmg_write_char(showch);
616 }
617 }
618
619 slang_navbuttons(ui, q, pos);
620 slang_flush();
621
622 if (pos == 2)
623 {
624 SLsmg_gotorc(ypos, 2+cursor);
625 SLsmg_refresh();
626 }
627
628 ch = SLkp_getkey();
629
630 if (isprint(ch))
631 {
632 memmove(&value[cursor+1], &value[cursor], sizeof(value)-cursor-1);
633 value[cursor] = (char)ch;
634 if (cursor < sizeof(value))
635 cursor++;
636 }
637 else
638 {
639 switch (ch)
640 {
641 case SL_KEY_HOME:
642 case 1: /* ^A */
643 cursor = 0;
644 break;
645 case SL_KEY_END:
646 case 5: /* ^E */
647 cursor = strlen(value);
648 break;
649 case 21: /* ^U */
650 memset(value, 0, sizeof(value));
651 cursor = 0;
652 break;
653 case SL_KEY_BACKSPACE:
654 if (cursor > 0)
655 {
656 memmove(&value[cursor-1], &value[cursor], sizeof(value)-cursor-1);
657 cursor--;
658 }
659 break;
660 case SL_KEY_DELETE:
661 if (cursor < sizeof(value))
662 memmove(&value[cursor], &value[cursor+1], sizeof(value)-cursor-1);
663 break;
664 case SL_KEY_LEFT:
665 if (cursor > 0) cursor--;
666 break;
667 case SL_KEY_RIGHT:
668 if (cursor < strlen(value)) cursor++;
669 break;
670 default:
671 switch (slang_keyhandler(ui, q, &pos, 2, ch))
672 {
673 case 0: ret = DC_OK; break;
674 case 1: ret = DC_GOBACK; break;
675 case 2: ret = DC_OK; break; /* Enter key */
676 }
677 }
678 }
679 }
680
681 if (ret == DC_OK)
682 question_setvalue(q, value);
683 return ret;
684 }
685
686 static int slang_string(struct frontend *ui, struct question *q)
687 {
688 return slang_getstring(ui, q, 0);
689 }
690
691 static int slang_password(struct frontend *ui, struct question *q)
692 {
693 return slang_getstring(ui, q, '*');
694 }
695
696 static int slang_text(struct frontend *ui, struct question *q)
697 {
698 int ret = 0, pos = 0;
699
700 while (ret == 0)
701 {
702 slang_navbuttons(ui, q, pos);
703 slang_flush();
704
705 switch (slang_keyhandler(ui, q, &pos, 1, 0))
706 {
707 case 0: ret = DC_OK; break;
708 case 1: ret = DC_GOBACK; break;
709 }
710 }
711 return ret;
712 }
713
714 /* ----------------------------------------------------------------------- */
715 static struct question_handlers {
716 const char *type;
717 int (*handler)(struct frontend *, struct question *q);
718 } question_handlers[] = {
719 { "boolean", slang_boolean },
720 { "multiselect", slang_multiselect },
721 { "note", slang_note },
722 { "password", slang_password },
723 { "select", slang_select },
724 { "string", slang_string },
725 { "text", slang_text },
726 { "error", slang_note },
727 };
728
729 static int slang_initialize(struct frontend *obj, struct configuration *cfg)
730 {
731 struct uidata *uid = NEW(struct uidata);
732 int ret;
733
734 memset(uid, 0, sizeof(struct uidata));
735 obj->interactive = 1;
736 obj->data = uid;
737
738 SLtt_get_terminfo();
739 #if SLANG_VERSION > 20000
740 SLutf8_enable(-1);
741 #endif
742 ret = SLsmg_init_smg();
743 INFO(INFO_DEBUG, "SLsmg_init_smg returned %d", ret);
744 ret = SLang_init_tty(-1, 0, 0);
745 INFO(INFO_DEBUG, "SLang_init_tty returned %d", ret);
746 ret = SLkp_init();
747 INFO(INFO_DEBUG, "SLkp_init returned %d", ret);
748
749 slang_initwin(obj, WIN_QUERY, &uid->qrywin);
750 slang_initwin(obj, WIN_DESC, &uid->descwin);
751 slang_initwin(obj, WIN_PROGRESS, &uid->progwin);
752
753 slang_drawwin(&uid->qrywin);
754 slang_drawwin(&uid->descwin);
755 slang_flush();
756
757 return DC_OK;
758 }
759
760 static int slang_shutdown(struct frontend *obj)
761 {
762 struct uidata *uid = UIDATA(obj);
763
764 SLsmg_reset_smg();
765 SLang_reset_tty();
766
767 DELETE(uid);
768 return DC_OK;
769 }
770
771 static bool slang_can_go_back(struct frontend *obj, struct question *q)
772 {
773 return (obj->capability & DCF_CAPB_BACKUP);
774 }
775
776 static int slang_go(struct frontend *obj)
777 {
778 struct question *q = obj->questions;
779 int i;
780 int ret;
781
782 SLsmg_init_smg();
783 SLang_init_tty(-1, 0, 0);
784 while (q != 0)
785 {
786 ret = DC_OK;
787 for (i = 0; i < DIM(question_handlers); i++)
788 {
789 INFO(INFO_VERBOSE, "For question [%s], comparing question type [%s] against handler %d (%s)", q->tag, q->template->type, i, question_handlers[i].type);
790 if (strcasecmp(q->template->type, question_handlers[i].type) == 0)
791 {
792 INFO(INFO_VERBOSE, "Found one!");
793 UIDATA(obj)->descstart = 0;
794 slang_drawdesc(obj, q);
795 ret = question_handlers[i].handler(obj, q);
796 INFO(INFO_VERBOSE, "Return code = %d", ret);
797 switch (ret)
798 {
799 case DC_OK:
800 frontend_qdb_set(obj->qdb, q, 0);
801 break;
802 case DC_GOBACK:
803 if (q->prev != NULL)
804 {
805 q = q->prev;
806 break;
807 }
808 /* fallthrough */
809 default:
810 SLsmg_reset_smg();
811 SLang_reset_tty();
812 return ret;
813 }
814 break;
815 }
816 }
817 if (i == DIM(question_handlers)) {
818 SLsmg_reset_smg();
819 SLang_reset_tty();
820 return DC_NOTIMPL;
821 }
822 if (ret == DC_OK)
823 q = q->next;
824 }
825 SLsmg_reset_smg();
826 SLang_reset_tty();
827
828 return DC_OK;
829 }
830
831 static int
832 slang_progress_set(struct frontend *obj, int val);
833
834 static void
835 slang_progress_start(struct frontend *obj, int min, int max, struct question *title)
836 {
837 struct uidata *uid = UIDATA(obj);
838 char *title_desc;
839
840 question_deref(obj->progress_title);
841 obj->progress_title = title;
842 question_ref(obj->progress_title);
843 obj->progress_min = min;
844 obj->progress_max = max;
845
846 slang_drawwin(&uid->descwin);
847 title_desc = q_get_raw_description(title);
848 slang_wrapprint(&uid->descwin, title_desc, 0);
849 free(title_desc);
850
851 slang_progress_set(obj, min);
852 }
853
854 static int
855 slang_progress_set(struct frontend *obj, int val)
856 {
857 struct uidata *uid = UIDATA(obj);
858 char buf[64], *percstr;
859 float perc;
860 int w, fillw, pos, i;
861
862 w = uid->progwin.w - 2;
863
864 obj->progress_cur = val;
865 if (obj->progress_max - obj->progress_min < 0 || w < 4)
866 return DC_NOTOK;
867
868 perc = 100.0 * (float)(obj->progress_cur - obj->progress_min) /
869 (float)(obj->progress_max - obj->progress_min);
870 sprintf(buf, "%3d%%", (int)perc);
871
872 percstr = (char *)malloc(w);
873 memset(percstr, ' ', w);
874
875 /* center the text in the percent string */
876 pos = (w - strlen(buf)) / 2;
877 memcpy(percstr + pos, buf, strlen(buf));
878
879 /* now draw the progress bar with the right amount filled */
880 slang_drawwin(&uid->qrywin);
881 slang_drawwin(&uid->progwin);
882 slang_flush();
883
884 fillw = (int)(w * perc * 0.01);
885
886 for (i = 0; i < w; i++) {
887 if (i < fillw)
888 SLsmg_set_color(uid->progwin.donecolor);
889 else
890 SLsmg_set_color(uid->progwin.drawcolor);
891
892 SLsmg_gotorc(uid->progwin.y + 1, uid->progwin.x + i + 1);
893 SLsmg_write_char(percstr[i]);
894 }
895
896 free(percstr);
897
898 return DC_OK;
899 }
900
901 static int
902 slang_progress_info(struct frontend *obj, struct question *info)
903 {
904 struct uidata *uid = UIDATA(obj);
905 char *title_desc = q_get_raw_description(obj->progress_title);
906 char *info_desc = q_get_raw_description(info);
907 char *buf = (char *)
908 malloc(strlen(title_desc) + strlen(info_desc) + 3);
909
910 strcpy(buf, title_desc);
911 strcat(buf, ":\n");
912 strcat(buf, info_desc);
913 free(info_desc);
914 free(title_desc);
915
916 slang_drawwin(&uid->descwin);
917 slang_wrapprint(&uid->descwin, buf, 0);
918 slang_flush();
919
920 free(buf);
921
922 return DC_OK;
923 }
924
925 static void
926 slang_progress_stop(struct frontend *obj)
927 {
928 struct uidata *uid = UIDATA(obj);
929
930 slang_drawwin(&uid->descwin);
931 slang_drawwin(&uid->qrywin);
932 slang_flush();
933 }
934
935 struct frontend_module debconf_frontend_module =
936 {
937 initialize: slang_initialize,
938 shutdown: slang_shutdown,
939 can_go_back: slang_can_go_back,
940 go: slang_go,
941 progress_start: slang_progress_start,
942 progress_set: slang_progress_set,
943 progress_info: slang_progress_info,
944 progress_stop: slang_progress_stop,
945 };