Debian Codegraph @

src/modules/frontend/text/text.c

raw
1 /***********************************************************************
2 *
3 * cdebconf - An implementation of the Debian Configuration Management
4 * System
5 *
6 * File: text.c
7 *
8 * Description: text UI for cdebconf
9 * Some notes on the implementation - this is meant to be an accessibility-
10 * friendly implementation. I've taken care to make the prompts work well
11 * with screen readers and the like.
12 *
13 * cdebconf is (c) 2000-2001 Randolph Chung and others under the following
14 * license.
15 *
16 * Redistribution and use in source and binary forms, with or without
17 * modification, are permitted provided that the following conditions
18 * are met:
19 *
20 * 1. Redistributions of source code must retain the above copyright
21 * notice, this list of conditions and the following disclaimer.
22 *
23 * 2. Redistributions in binary form must reproduce the above copyright
24 * notice, this list of conditions and the following disclaimer in the
25 * documentation and/or other materials provided with the distribution.
26 *
27 * THIS SOFTWARE IS PROVIDED BY THE AUTHORS AND CONTRIBUTORS ``AS IS'' AND
28 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
29 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
30 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
31 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
32 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
33 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
34 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
35 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
36 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
37 * SUCH DAMAGE.
38 *
39 ***********************************************************************/
40 #include "common.h"
41 #include "template.h"
42 #include "question.h"
43 #include "frontend.h"
44 #include "database.h"
45 #include "plugin.h"
46 #include "strutl.h"
47
48 #include "cdebconf_text.h"
49
50 #include <ctype.h>
51 #include <fcntl.h>
52 #include <signal.h>
53 #include <string.h>
54 #include <limits.h>
55 #include <wchar.h>
56 #include <stdlib.h>
57 #include <termios.h>
58 #include <unistd.h>
59 #include <time.h>
60 #include <sys/ioctl.h>
61 #include <sys/stat.h>
62 #include <sys/types.h>
63
64 #include <readline/readline.h>
65 #include <readline/history.h>
66
67 struct frontend_data {
68 char *previous_title;
69 };
70
71 typedef int (text_handler)(struct frontend *obj, unsigned printed, struct question *q);
72
73 #define MAKE_UPPER(C) do { if (islower((int) C)) { C = (char) toupper((int) C); } } while(0)
74
75 #if defined(__s390__) || defined (__s390x__)
76 #define ISEMPTY(buf) (buf[0] == 0 || (buf[0] == '.' && buf[1] == 0))
77 #else
78 #define ISEMPTY(buf) (buf[0] == 0)
79 #endif
80
81 #define PROGRESS_NONE -10
82 #define PROGRESS_STEP 10
83 static int last_progress = PROGRESS_NONE;
84 #define PROGRESS_TIME_STEP 60
85 static int last_progress_time = 0;
86
87 static sigset_t sigint_set;
88
89 static const struct timespec null_timeout = {
90 .tv_sec = 0,
91 .tv_nsec = 0
92 };
93
94 /*
95 * Function: getwidth
96 * Input: none
97 * Output: int - width of screen
98 * Description: get the width of the current terminal
99 * Assumptions: doesn't handle resizing; caches value on first call
100 */
101 static int getwidth(void)
102 {
103 static int res = 80;
104 static int inited = 0;
105 int fd;
106 struct winsize ws;
107
108 if (inited == 0)
109 {
110 inited = 1;
111 if ((fd = open("/dev/tty", O_RDONLY)) > 0)
112 {
113 if (ioctl(fd, TIOCGWINSZ, &ws) == 0 && ws.ws_col > 0)
114 res = ws.ws_col;
115 close(fd);
116 }
117 }
118 return res;
119 }
120
121 /*
122 * Function: getheight
123 * Input: none
124 * Output: int - height of screen
125 * Description: get the height of the current terminal
126 * Assumptions: doesn't handle resizing; caches value on first call
127 */
128 static int getheight(void)
129 {
130 static int res = 25;
131 static int inited = 0;
132 int fd;
133 struct winsize ws;
134
135 if (inited == 0)
136 {
137 inited = 1;
138 if ((fd = open("/dev/tty", O_RDONLY)) > 0)
139 {
140 if (ioctl(fd, TIOCGWINSZ, &ws) == 0 && ws.ws_row > 0)
141 res = ws.ws_row;
142 close(fd);
143 }
144 }
145 return res;
146 }
147
148 /*
149 * Function: wrap_print
150 * Input: const char *str - string to display
151 * Output: unsigned printed - number of printed lines
152 * Description: prints a string to the screen with word wrapping
153 * Assumptions: string fits in <500 lines
154 */
155 static unsigned wrap_print(const char *str)
156 {
157 /* Simple greedy line-wrapper */
158 int i, lc;
159 char *lines[500];
160
161 lc = strwrap(str, getwidth() - 1, lines, DIM(lines));
162
163 for (i = 0; i < lc; i++)
164 {
165 printf("%s\n", lines[i]);
166 DELETE(lines[i]);
167 }
168 return lc;
169 }
170
171 /*
172 * Function: text_handler_displaydesc
173 * Input: struct frontend *obj - UI object
174 * unsigned printed - number of already printed lines
175 * struct question *q - question for which to display the description
176 * Output: unsigned - number of printed lines
177 * Description: displays the description for a given question
178 * Assumptions: none
179 */
180 static unsigned text_handler_displaydesc(struct frontend *obj, unsigned printed, struct question *q)
181 {
182 char *descr = q_get_description(obj, q);
183 char *ext_descr = q_get_extended_description(obj, q);
184 printed = 0;
185 if (strcmp(q->template->type, "note") == 0 ||
186 strcmp(q->template->type, "error") == 0)
187 {
188 if (strcmp(q->template->type, "error") == 0)
189 printf(question_get_text(obj, "debconf/text-error", "!! ERROR: %s"), descr);
190 else
191 printf("%s", descr);
192 printf("\n\n");
193 printed += 2;
194 if (*ext_descr)
195 printed += wrap_print(ext_descr);
196 }
197 else
198 {
199 if (*ext_descr)
200 printed += wrap_print(ext_descr);
201 printed += wrap_print(descr);
202 }
203 free(descr);
204 free(ext_descr);
205 return printed;
206 }
207
208 static char *
209 get_answer(const char *prompt)
210 {
211 char *answer;
212 printf("%s\n", prompt);
213 answer = readline("");
214 if (!answer)
215 answer = strdup("");
216 return answer;
217 }
218
219 static unsigned
220 show_help (struct frontend *obj, struct question *q)
221 {
222 char *descr = q_get_description(obj, q);
223 char *help = q_get_help(obj, q);
224 unsigned printed = 0;
225 if (*help) {
226 struct question *help_q = obj->qdb->methods.get(obj->qdb, help);
227 if (help_q) {
228 char *help_descr = q_get_description(obj, help_q);
229 char *help_ext_descr = q_get_extended_description(obj, help_q);
230 printed += wrap_print(help_descr);
231 printf("\n");
232 printed++;
233 if (*help_ext_descr) {
234 printed += wrap_print(help_ext_descr);
235 printf("\n");
236 printed++;
237 }
238 free(help_ext_descr);
239 free(help_descr);
240 question_deref(help_q);
241 }
242 free(help);
243 }
244 printf("%s\n", question_get_text(obj, "debconf/text-help-keystrokes", "KEYSTROKES:"));
245 printed++;
246 printf(" ");
247 printf(question_get_text(obj, "debconf/text-help-keystroke", "'%c'"), CHAR_HELP);
248 printf(" %s\n", question_get_text(obj, "debconf/text-help-help", "Display this help message"));
249 printed++;
250 if (obj->methods.can_go_back (obj, q)) {
251 printf(" ");
252 printf(question_get_text(obj, "debconf/text-help-keystroke", "'%c'"), CHAR_GOBACK);
253 printf(" %s\n", question_get_text(obj, "debconf/text-help-goback", "Go back to previous question"));
254 printed++;
255 }
256 if (strcmp(q->template->type, "string") == 0 ||
257 strcmp(q->template->type, "password") == 0 ||
258 strcmp(q->template->type, "multiselect") == 0) {
259 printf(" ");
260 printf(question_get_text(obj, "debconf/text-help-keystroke", "'%c'"), CHAR_CLEAR);
261 printf(" %s\n", question_get_text(obj, "debconf/text-help-clear", "Select an empty entry"));
262 printed++;
263 }
264 printed += wrap_print(descr);
265 free(descr);
266 return printed;
267 }
268
269 struct choices {
270 int count;
271 char **choices;
272 char **choices_translated;
273 char *selected;
274 int *tindex;
275 };
276
277 /*
278 * Function: printlist
279 * Input: struct frontend *obj - UI object
280 * unsigned max_lines - maximum number of lines to print
281 * unsigned start - which line of choices should be printed first.
282 * struct question *q - question for which to display the description
283 * Output: bool - whether we managed to print all choices from 'start' to last or not
284 * Description: displays the description for a given question
285 * Assumptions: none
286 *
287 * If it didn't fit, it will have printed a one-line comment about it, thus one
288 * line of choices less.
289 */
290 static unsigned
291 printlist (struct frontend *obj, unsigned max_lines, unsigned start, struct question *q, const struct choices *choices)
292 {
293 int choice_min = -1;
294 int num_cols, num_lines;
295 int used_cols = 1;
296 int last_col_height = 0;
297 int i, k, l;
298 int logcount = 0;
299 int *col_width;
300 int total_width = 0;
301 char **output;
302 int line = 0, max_len = 0, col = 0;
303 int width = getwidth();
304 char **fchoices = malloc(sizeof(char *) * choices->count);
305 int horiz = 1;
306 bool all = false;
307 char *debconf_text_horiz = getenv("DEBCONF_TEXT_HORIZ");
308
309 if (debconf_text_horiz && !atoi(debconf_text_horiz))
310 horiz = 0;
311
312 i = choices->count;
313 do {
314 i /= 10;
315 logcount++;
316 } while (i > 0);
317
318 if (obj->methods.can_align(obj, q)) {
319 stralign(choices->choices_translated, choices->count);
320 /* Display only one column to get meaningful alignment. */
321 width = 1;
322 }
323
324 /* Set string arrays */
325 for (i=0; i < choices->count; i++)
326 {
327 /* Trailing spaces are a placeholder to add [*] for
328 selected values
329 Comma is needed for proper speech synthesis pause */
330 asprintf(&(fchoices[i]), "%3d: %s, ", i+1, choices->choices_translated[i]);
331 if (choices->selected[choices->tindex[i]])
332 strcpy(fchoices[i]+strlen(fchoices[i])-5, " [*],");
333 if (strwidth(fchoices[i]) < choice_min || choice_min == -1)
334 choice_min = strwidth(fchoices[i]);
335 if (strwidth(fchoices[i]) > width)
336 width = strwidth(fchoices[i]);
337 }
338 num_cols = width / choice_min;
339 if (num_cols > choices->count)
340 num_cols = choices->count;
341 col_width = malloc (sizeof(int) * num_cols);
342 num_cols++;
343 while (1)
344 {
345 COLUMN:
346 num_cols--;
347 if (num_cols == 0)
348 break;
349 num_lines = (choices->count - 1) / num_cols + 1;
350 used_cols = (choices->count - 1) / num_lines + 1;
351 last_col_height = choices->count % num_lines;
352 for (i = 0; i < num_cols; i++)
353 col_width[i] = 0;
354 for (i = 0; i < choices->count; i++)
355 {
356 int current_col;
357 if (horiz)
358 {
359 if (!last_col_height || i < last_col_height * num_cols)
360 current_col = i % used_cols;
361 else
362 current_col = ((i - last_col_height * num_cols) % (used_cols-1));
363 }
364 else
365 current_col = i / num_lines;
366 if (strwidth(fchoices[i]) > col_width[current_col])
367 {
368 col_width[current_col] = strwidth(fchoices[i]);
369 total_width = 0;
370 for (k = 0; k < num_cols; k++)
371 total_width += col_width[k];
372 if (total_width > width)
373 goto COLUMN;
374 }
375 }
376 break;
377 }
378 if (num_cols == 0)
379 {
380 num_lines = choices->count;
381 num_cols = 1;
382 }
383
384 output = malloc(sizeof(char *) * num_lines);
385 for (i = 0; i < num_lines; i++)
386 {
387 output[i] = malloc(MB_LEN_MAX * width + 1);
388 *(output[i]) = '\0';
389 }
390 for (i = 0; i < choices->count; i++)
391 {
392 if (horiz)
393 {
394 int j = line * used_cols + col;
395 if (last_col_height && line > last_col_height)
396 j -= line - last_col_height;
397 strcat(output[line], fchoices[j]);
398 }
399 else
400 strcat(output[line], fchoices[i]);
401 if (strwidth(output[line]) > max_len)
402 max_len = strwidth(output[line]);
403 line++;
404 if (line >= num_lines)
405 {
406 col++;
407 if (col != num_cols)
408 {
409 for (l = 0; l < num_lines; l++)
410 strpad(output[l], max_len);
411 }
412
413 line = 0;
414 max_len = 0;
415 }
416 }
417
418 /* Disable paging in text frontend for now, espeakup does not pronounce
419 * the keys to change pages (see #690343) and thus users are confounded
420 * and think these are the only choices. */
421 /*if (max_lines >= num_lines - start)*/
422 if (1)
423 {
424 /* More than enough room */
425 max_lines = num_lines - start;
426 all = true;
427 }
428 else
429 /* Keep one line for the "more choices" prompt */
430 max_lines--;
431 /* Don't display the beginning */
432 for (l = 0; l < start; l++)
433 free(output[l]);
434 /* Display only what fits in the screen */
435 for ( ; l < start + max_lines; l++)
436 {
437 printf("%s\n", output[l]);
438 free(output[l]);
439 }
440 /* Don't display the end */
441 for ( ; l < num_lines; l++)
442 free(output[l]);
443
444 free(output);
445 free(col_width);
446 for (i = 0; i < choices->count; i++)
447 free(fchoices[i]);
448 free(fchoices);
449
450 if (start > 0 && !all)
451 printf(question_get_text(obj, "debconf/text-help-otherchoices", "Other choices are available with '%c' and '%c'"), CHAR_PREV, CHAR_NEXT);
452 if (start > 0 && all)
453 printf(question_get_text(obj, "debconf/text-help-prevchoices", "Previous choices are available with '%c'"), CHAR_PREV);
454 if (start == 0 && !all)
455 printf(question_get_text(obj, "debconf/text-help-nextchoices", "Next choices are available with '%c'"), CHAR_NEXT);
456 if (start > 0 || !all)
457 printf("\n");
458
459 return all;
460 }
461
462 /*
463 * Function: text_handler_boolean
464 * Input: struct frontend *obj - frontend object
465 * unsigned printed - number of already printed lines
466 * struct question *q - question to ask
467 * Output: int - DC_OK, DC_NOTOK, DC_GOBACK
468 * Description: handler for the boolean question type
469 * Assumptions: none
470 */
471 static int text_handler_boolean(struct frontend *obj, unsigned printed, struct question *q)
472 {
473 char *buf;
474 int ans = 0;
475 int def = 0;
476 const char *defval;
477 char *prompt;
478
479 defval = question_getvalue(q, "");
480 if (defval)
481 {
482 if (strcmp(defval, "true") == 0)
483 def = 1;
484 else
485 def = 2;
486 }
487
488 if (def)
489 asprintf(&prompt, question_get_text(obj, "debconf/text-prompt-default",
490 "Prompt: '%c' for help, default=%d> "), CHAR_HELP, def);
491 else
492 asprintf(&prompt, question_get_text(obj, "debconf/text-prompt",
493 "Prompt: '%c' for help> "), CHAR_HELP);
494
495 asprintf(&buf, "%d: %s%s", 2, question_get_text(obj, "debconf/no", "No"), def == 2 ? " [*]" : "");
496 add_history(buf);
497 free(buf);
498 asprintf(&buf, "%d: %s%s", 1, question_get_text(obj, "debconf/yes", "Yes"), def == 1 ? " [*]" : "");
499 add_history(buf);
500 free(buf);
501
502 do {
503 printf(" %d: %s%s", 1, question_get_text(obj, "debconf/yes", "Yes"), (1 == def ? " [*]" : " "));
504 printf(" %d: %s%s", 2, question_get_text(obj, "debconf/no", "No"), (2 == def ? " [*]" : ""));
505 printf("\n");
506 buf = get_answer(prompt);
507 if (buf[0] == CHAR_HELP && buf[1] == 0)
508 printed += show_help(obj, q);
509 else if (obj->methods.can_go_back (obj, q) &&
510 buf[0] == CHAR_GOBACK && buf[1] == 0) {
511 clear_history();
512 free(buf);
513 free(prompt);
514 return DC_GOBACK;
515 }
516 else if (buf[0] == '1')
517 ans = 1;
518 else if (buf[0] == '2')
519 ans = 2;
520 else if (defval != 0 && ISEMPTY(buf))
521 ans = def;
522 free(buf);
523 } while (ans == 0);
524
525 question_setvalue(q, (ans == 1 ? "true" : "false"));
526
527 clear_history();
528 free(prompt);
529 return DC_OK;
530 }
531
532 /*
533 * Function: choices_delete
534 * Input: struct choices *c - choices values, translations, indices, and select
535 * Description: frees the structure and its content
536 * Assumptions: none
537 */
538 static void choices_delete(struct choices *c)
539 {
540 int i;
541 if (c)
542 {
543 if (c->choices)
544 {
545 for (i = 0; i < c->count; i++)
546 free(c->choices[i]);
547 free(c->choices);
548 }
549 if (c->choices_translated)
550 {
551 for (i = 0; i < c->count; i++)
552 free(c->choices_translated[i]);
553 free(c->choices_translated);
554 }
555 free(c->selected);
556 free(c->tindex);
557 free(c);
558 }
559 }
560
561 /*
562 * Function: choices_get
563 * Input: struct frontend *obj - frontend object
564 * unsigned printed - number of already printed lines
565 * struct question *q - question to ask
566 * Output: struct choices * - choices values, translations, indices, and select
567 * Description: retrieve question choices, translations, indices
568 * Assumptions: none
569 */
570 static struct choices *choices_get(struct frontend *obj, struct question *q)
571 {
572 struct choices *retval = NULL;
573 int i, count;
574 char *choices_vals = q_get_choices_vals(obj, q);
575
576 count = strgetargc(choices_vals);
577 if (count > 0)
578 {
579 int dcount;
580 retval = (struct choices *)malloc(sizeof(struct choices));
581 retval->count = count;
582 retval->choices = malloc(sizeof(char *) * count);
583 retval->choices_translated = malloc(sizeof(char *) * count);
584 for (i = 0; i < count; ++i)
585 {
586 retval->choices[i] = retval->choices_translated[i] = NULL;
587 }
588 retval->tindex = malloc(sizeof(int) * count);
589 retval->selected = calloc(1, sizeof(char) * retval->count);
590
591 char *indices = q_get_indices(obj, q);
592 char *choices = q_get_choices(obj, q);
593 dcount = strchoicesplitsort(choices_vals, choices, indices, retval->choices, retval->choices_translated, retval->tindex, count);
594 free(choices);
595 free(indices);
596
597 if (dcount != count)
598 {
599 choices_delete(retval);
600 retval = NULL;
601 }
602 }
603
604 free(choices_vals);
605
606 return retval;
607 }
608
609 /*
610 * Function: text_handler_multiselect
611 * Input: struct frontend *obj - frontend object
612 * unsigned printed - number of already printed lines
613 * struct question *q - question to ask
614 * Output: int - DC_OK, DC_NOTOK
615 * Description: handler for the multiselect question type
616 * Assumptions: none
617 */
618 static int text_handler_multiselect(struct frontend *obj, unsigned printed, struct question *q)
619 {
620 struct choices *choices = NULL;
621 char **defaults;
622 char *defval, *cp;
623 char *answer = NULL;
624 size_t answer_len;
625 int i, j, dcount, choice;
626 int ret = DC_OK;
627 unsigned start = 0;
628 bool all;
629 char *prompt;
630
631 choices = choices_get(obj, q);
632 if (choices == NULL)
633 return DC_NOTOK;
634
635 defaults = malloc(sizeof(char *) * choices->count);
636 dcount = strchoicesplit(question_getvalue(q, ""), defaults, choices->count);
637
638 for (j = 0; j < dcount; j++) {
639 for (i = 0; i < choices->count; i++) {
640 if (strcmp(choices->choices[choices->tindex[i]], defaults[j]) == 0)
641 choices->selected[choices->tindex[i]] = 1;
642 }
643 free(defaults[j]);
644 }
645 free(defaults);
646
647 i = 0;
648
649 defval = malloc(10*choices->count);
650 *defval = '\0';
651 for (i = 0; i < choices->count; i++)
652 if (choices->selected[choices->tindex[i]])
653 {
654 char buf[10];
655 if (*defval != '\0')
656 strcat(defval, " ");
657 sprintf(buf, "%d", i+1);
658 strcat(defval, buf);
659 }
660
661 asprintf(&prompt, question_get_text(obj, "debconf/text-prompt-default-string",
662 "Prompt: '%c' for help, default=%s> "), CHAR_HELP, defval);
663 DISPLAY:
664 all = printlist (obj, getheight() - printed - 1, start, q, choices);
665 free(answer);
666 answer = get_answer(prompt);
667 if (answer[0] == CHAR_HELP && answer[1] == 0)
668 {
669 printed = 0;
670 printed += show_help(obj, q);
671 goto DISPLAY;
672 }
673 else if (answer[0] == CHAR_CLEAR && answer[1] == 0)
674 {
675 for (i = 0; i < choices->count; i++)
676 choices->selected[i] = 0;
677 }
678 else if (obj->methods.can_go_back (obj, q) &&
679 answer[0] == CHAR_GOBACK && answer[1] == 0)
680 {
681 ret = DC_GOBACK;
682 goto CleanUp_DEFVAL;
683 }
684 else if (answer[0] == CHAR_NEXT && answer[1] == 0)
685 {
686 if (!all)
687 start += getheight() - printed - 2;
688 printed = 0;
689 goto DISPLAY;
690 }
691 else if (answer[0] == CHAR_PREV && answer[1] == 0)
692 {
693 /* Note: 'printed' may not always contain the same value, when
694 * the title banner is not printed again notably. This however
695 * only happens once, the second time the question in answered,
696 * and start will have then been 0 already so this does not
697 * pose problem. */
698 if (start <= getheight() - printed - 2)
699 start = 0;
700 else
701 start -= getheight() - printed - 2;
702 printed = 0;
703 goto DISPLAY;
704 }
705
706 if (!(ISEMPTY(answer)))
707 {
708 for (i = 0; i < choices->count; i++)
709 choices->selected[i] = 0;
710 for (cp = answer; cp != NULL && *cp != '\0'; cp = strchr(cp+1, ' '))
711 {
712 choice = atoi(cp) - 1;
713 if (choice >= 0 && choice < choices->count)
714 choices->selected[choices->tindex[choice]] = 1;
715 }
716 }
717
718 answer_len = 0;
719 for (i = 0; i < choices->count; i++)
720 {
721 if (choices->selected[i])
722 answer_len += strlen(choices->choices[i]) + 2;
723 }
724
725 free(answer);
726 answer = malloc(answer_len);
727 if (answer_len > 0)
728 answer[0] = 0;
729 for (i = 0; i < choices->count; i++)
730 {
731 if (choices->selected[i])
732 {
733 if (answer[0] != 0)
734 strvacat(answer, answer_len, ", ", NULL);
735 strvacat(answer, answer_len, choices->choices[i], NULL);
736 }
737 }
738 question_setvalue(q, answer);
739
740 CleanUp_DEFVAL:
741 free(answer);
742 free(prompt);
743 free(defval);
744 choices_delete(choices);
745
746 return ret;
747 }
748
749 /*
750 * Function: text_handler_select
751 * Input: struct frontend *obj - frontend object
752 * unsigned printed - number of already printed lines
753 * struct question *q - question to ask
754 * Output: int - DC_OK, DC_NOTOK
755 * Description: handler for the select question type
756 * Assumptions: none
757 *
758 * TODO: factor common code with multiselect
759 */
760 static int text_handler_select(struct frontend *obj, unsigned printed, struct question *q)
761 {
762 struct choices *choices = NULL;
763 char *answer;
764 int i, choice, def = -1;
765 const char *defval;
766 int ret = DC_OK;
767 unsigned start = 0;
768 bool all;
769 char *prompt;
770
771 choices = choices_get(obj, q);
772 if (choices == NULL)
773 return DC_NOTOK;
774
775 if (choices->count == 1)
776 defval = choices->choices[0];
777 else
778 defval = question_getvalue(q, "");
779
780 if (defval != NULL)
781 {
782 for (i = 0; i < choices->count; i++)
783 if (strcmp(choices->choices[choices->tindex[i]], defval) == 0)
784 {
785 choices->selected[choices->tindex[i]] = 1;
786 def = i;
787 }
788 }
789
790 if (def >= 0 && choices->choices_translated[def]) {
791 asprintf(&prompt, question_get_text(obj, "debconf/text-prompt-default",
792 "Prompt: '%c' for help, default=%d> "),
793 CHAR_HELP, def+1);
794 } else {
795 asprintf(&prompt, question_get_text(obj, "debconf/text-prompt",
796 "Prompt: '%c' for help> "), CHAR_HELP);
797 }
798
799 for (i = choices->count; i > 0; i--) {
800 asprintf(&answer, "%d: %s%s", i, choices->choices_translated[i-1], i == def+1 ? " [*]" : "");
801 add_history(answer);
802 free(answer);
803 }
804
805 answer = NULL;
806 i = 0;
807 choice = -1;
808 do {
809 all = printlist (obj, getheight() - printed - 1, start, q, choices);
810 free(answer);
811 answer = get_answer(prompt);
812 if (answer[0] == CHAR_HELP)
813 {
814 printed += show_help(obj, q);
815 continue;
816 }
817 if (obj->methods.can_go_back (obj, q) &&
818 answer[0] == CHAR_GOBACK && answer[1] == 0)
819 {
820 ret = DC_GOBACK;
821 goto CleanUp_SELECTED;
822 }
823 else if (answer[0] == CHAR_NEXT && answer[1] == 0)
824 {
825 if (!all)
826 start += getheight() - printed - 2;
827 printed = 0;
828 continue;
829 }
830 else if (answer[0] == CHAR_PREV && answer[1] == 0)
831 {
832 /* Note: 'printed' may not always contain the same value, when
833 * the title banner is not printed again notably. This however
834 * only happens once, the second time the question in answered,
835 * and start will have then been 0 already so this does not
836 * pose problem. */
837 if (start < getheight() - printed - 2)
838 start = 0;
839 else
840 start -= getheight() - printed - 2;
841 printed = 0;
842 continue;
843 }
844 if (ISEMPTY(answer))
845 choice = def;
846 else {
847 choice = atoi(answer) - 1;
848 if (choice == -1) {
849 /* Not a number, perhaps the value */
850 for (i = 0; i < choices->count; i++) {
851 if (strcasecmp(choices->choices[choices->tindex[i]], answer) == 0) {
852 /* Yes */
853 choice = i;
854 break;
855 }
856 }
857 }
858 }
859 printed = 0;
860 } while (choice < 0 || choice >= choices->count);
861 question_setvalue(q, choices->choices[choices->tindex[choice]]);
862
863 CleanUp_SELECTED:
864 clear_history();
865 free(answer);
866 free(prompt);
867 choices_delete(choices);
868
869 return ret;
870 }
871
872 /*
873 * Function: text_handler_note
874 * Input: struct frontend *obj - frontend object
875 * unsigned printed - number of already printed lines
876 * struct question *q - question to ask
877 * Output: int - DC_OK, DC_NOTOK, DC_GOBACK
878 * Description: handler for the note question type
879 * Assumptions: none
880 */
881 static int text_handler_note(struct frontend *obj, unsigned printed, struct question *q)
882 {
883 char *buf;
884 char *prompt;
885 asprintf(&prompt, "%s ", question_get_text(obj, "debconf/cont-prompt",
886 "[Press enter to continue]"));
887 while (1)
888 {
889 buf = get_answer(prompt);
890 if (buf[0] == CHAR_HELP && buf[1] == 0)
891 printed += show_help(obj, q);
892 else if (obj->methods.can_go_back (obj, q) &&
893 buf[0] == CHAR_GOBACK && buf[1] == 0) {
894 free(buf);
895 free(prompt);
896 return DC_GOBACK;
897 }
898 else
899 break;
900 free(buf);
901 }
902 free(buf);
903 free(prompt);
904 return DC_OK;
905 }
906
907 /*
908 * Function: text_handler_password
909 * Input: struct frontend *obj - frontend object
910 * unsigned printed - number of already printed lines
911 * struct question *q - question to ask
912 * Output: int - DC_OK, DC_NOTOK
913 * Description: handler for the password question type
914 * Assumptions: none
915 *
916 * TODO: this can be *MUCH* improved. no editing is possible right now
917 */
918 static int text_handler_password(struct frontend *obj, unsigned printed, struct question *q)
919 {
920 struct termios oldt, newt;
921 char passwd[256] = {0};
922 int i, c;
923
924 tcgetattr(0, &oldt);
925 memcpy(&newt, &oldt, sizeof(struct termios));
926 cfmakeraw(&newt);
927 while (1)
928 {
929 tcsetattr(0, TCSANOW, &newt);
930 i = 0;
931 while ((c = fgetc(stdin)) != EOF)
932 {
933 if (c == '\r' || c == '\n')
934 break;
935 else if (c == '\b')
936 {
937 if (i > 0)
938 i--;
939 continue;
940 }
941 passwd[i] = (char)c;
942 i++;
943 }
944 passwd[i] = 0;
945 tcsetattr(0, TCSANOW, &oldt);
946 if (passwd[0] == CHAR_HELP && passwd[1] == 0)
947 printed += show_help(obj, q);
948 else
949 break;
950 }
951 if (obj->methods.can_go_back (obj, q) &&
952 passwd[0] == CHAR_GOBACK && passwd[1] == 0)
953 return DC_GOBACK;
954 if (passwd[0] == CHAR_CLEAR && passwd[1] == 0)
955 question_setvalue(q, "");
956 else
957 question_setvalue(q, passwd);
958 return DC_OK;
959 }
960
961 /*
962 * Function: text_handler_string
963 * Input: struct frontend *obj - frontend object
964 * unsigned printed - number of already printed lines
965 * struct question *q - question to ask
966 * Output: int - DC_OK, DC_NOTOK
967 * Description: handler for the string question type
968 * Assumptions: none
969 */
970 static int text_handler_string(struct frontend *obj, unsigned printed, struct question *q)
971 {
972 char *buf;
973 const char *defval = question_getvalue(q, "");
974 char *prompt;
975 if (defval)
976 asprintf(&prompt, question_get_text(obj, "debconf/text-prompt-default-string", "Prompt: '%c' for help, default=%s> "), CHAR_HELP, defval);
977 else
978 asprintf(&prompt, question_get_text(obj, "debconf/text-prompt", "Prompt: '%c' for help> "), CHAR_HELP);
979 while (1) {
980 buf = get_answer(prompt);
981 if (buf[0] == CHAR_HELP && buf[1] == 0)
982 printed += show_help(obj, q);
983 else
984 break;
985 free(buf);
986 }
987 if (obj->methods.can_go_back (obj, q) &&
988 buf[0] == CHAR_GOBACK && buf[1] == 0) {
989 free(buf);
990 free(prompt);
991 return DC_GOBACK;
992 }
993 if (ISEMPTY(buf) && defval == 0)
994 question_setvalue(q, "");
995 else if (ISEMPTY(buf) && defval != 0)
996 question_setvalue(q, defval);
997 else if (buf[0] == CHAR_CLEAR && buf[1] == 0)
998 question_setvalue(q, "");
999 else
1000 question_setvalue(q, buf);
1001 free(buf);
1002 free(prompt);
1003 return DC_OK;
1004 }
1005
1006 /*
1007 * Function: text_handler_text
1008 * Input: struct frontend *obj - frontend object
1009 * unsigned printed - number of already printed lines
1010 * struct question *q - question to ask
1011 * Output: int - DC_OK, DC_NOTOK, DC_GOBACK
1012 * Description: handler for the text question type
1013 * Assumptions: none
1014 */
1015 static int text_handler_text(struct frontend *obj, unsigned printed, struct question *q)
1016 {
1017 return text_handler_note(obj, printed, q);
1018 }
1019
1020 /*
1021 * Function: text_handler_error
1022 * Input: struct frontend *obj - frontend object
1023 * unsigned printed - number of already printed lines
1024 * struct question *q - question to ask
1025 * Output: int - DC_OK, DC_NOTOK, DC_GOBACK
1026 * Description: handler for the error question type. Currently equal to _note
1027 * Assumptions: none
1028 */
1029 static int text_handler_error(struct frontend *obj, unsigned printed, struct question *q)
1030 {
1031 return text_handler_note(obj, printed, q);
1032 }
1033
1034 /* ----------------------------------------------------------------------- */
1035 static struct question_handlers {
1036 const char *type;
1037 text_handler *handler;
1038 } question_handlers[] = {
1039 { "boolean", text_handler_boolean },
1040 { "multiselect", text_handler_multiselect },
1041 { "note", text_handler_note },
1042 { "password", text_handler_password },
1043 { "select", text_handler_select },
1044 { "string", text_handler_string },
1045 { "text", text_handler_text },
1046 { "error", text_handler_error },
1047 { "", NULL },
1048 };
1049
1050 /*
1051 * Function: text_initialize
1052 * Input: struct frontend *obj - frontend UI object
1053 * struct configuration *cfg - configuration parameters
1054 * Output: int - DC_OK, DC_NOTOK
1055 * Description: initializes the text UI
1056 * Assumptions: none
1057 */
1058 static int text_initialize(struct frontend *obj, struct configuration *conf)
1059 {
1060 struct frontend_data *data = NEW(struct frontend_data);
1061 char *term = getenv("TERM");
1062 char *palette = getenv("FRONTEND_BACKGROUND");
1063 data->previous_title = NULL;
1064 obj->data = data;
1065 obj->interactive = 1;
1066
1067 /* Prepare for catching SIGINT synchronously */
1068 signal(SIGINT, SIG_IGN);
1069 sigemptyset(&sigint_set);
1070 sigaddset(&sigint_set, SIGINT);
1071 sigprocmask(SIG_BLOCK, &sigint_set, NULL);
1072
1073 if (palette && !strcmp(palette, "dark") &&
1074 term && (!strcmp(term, "linux") || !strcmp(term, "bterm"))) {
1075 /* Hard-code for these cases */
1076 printf("\e[37m\e[40m\e[1m\e[H\e[J");
1077 fflush(stdout);
1078 }
1079 return DC_OK;
1080 }
1081
1082 static int text_shutdown(struct frontend *obj)
1083 {
1084 struct frontend_data *data = (struct frontend_data *)obj->data;
1085 if (NULL != data)
1086 {
1087 free(data->previous_title);
1088 free(data);
1089 obj->data = NULL;
1090 }
1091 return DC_OK;
1092 }
1093
1094 /*
1095 * Function: text_can_go_back
1096 * Input: struct frontend *obj - frontend object
1097 * unsigned printed - number of already printed lines
1098 * struct question *q - question object
1099 * Output: int - DC_OK, DC_NOTOK
1100 * Description: tells whether confmodule supports backing up
1101 * Assumptions: none
1102 */
1103 static bool
1104 text_can_go_back(struct frontend *obj, struct question *q)
1105 {
1106 return (obj->capability & DCF_CAPB_BACKUP);
1107 }
1108
1109 /*
1110 * Function: text_can_cancel_progress
1111 * Input: struct frontend *obj - frontend object
1112 * Output: boolean
1113 * Description: tells whether confmodule supports cancelling a step
1114 * Assumptions: none
1115 */
1116 static bool
1117 text_can_cancel_progress(struct frontend *obj)
1118 {
1119 return (obj->capability & DCF_CAPB_PROGRESSCANCEL);
1120 }
1121
1122 /*
1123 * Function: text_can_align
1124 * Input: struct frontend *obj - frontend object
1125 * unsigned printed - number of already printed lines
1126 * struct question *q - question object
1127 * Output: int - DC_OK, DC_NOTOK
1128 * Description: tells whether confmodule supports aligning columns
1129 * Assumptions: none
1130 */
1131 static bool
1132 text_can_align(struct frontend *obj, struct question *q)
1133 {
1134 return (obj->capability & DCF_CAPB_ALIGN);
1135 }
1136
1137 /*
1138 * Function: text_lookup_directive
1139 * Input: struct frontend *obj - frontend object
1140 * const char *directive - the directive
1141 * Output: const char * - directive value or NULL
1142 * Description: returns directive value in the given context
1143 * Assumptions: none
1144 */
1145 static const char *
1146 text_lookup_directive(struct frontend *obj, const char *directive)
1147 {
1148 if (obj->methods.can_align(obj, obj->questions)) {
1149 if (strcmp("TAB", directive) == 0) {
1150 return STRALIGN_TAB;
1151 } else if (strcmp("ALIGN=CENTER", directive) == 0) {
1152 return STRALIGN_ALIGN_CENTER;
1153 } else if (strcmp("ALIGN=RIGHT", directive) == 0) {
1154 return STRALIGN_ALIGN_RIGHT;
1155 }
1156 }
1157 /* Remove unhandled directives */
1158 return "";
1159 }
1160
1161 /*
1162 * Function: text_go
1163 * Input: struct frontend *obj - frontend object
1164 * Output: int - DC_OK, DC_NOTOK
1165 * Description: asks all pending questions
1166 * Assumptions: none
1167 */
1168 static int text_go(struct frontend *obj)
1169 {
1170 struct frontend_data *data = (struct frontend_data *) obj->data;
1171 struct question *q = obj->questions;
1172 int i;
1173 int ret = DC_OK;
1174 unsigned printed;
1175
1176 /* Make sure that after asking the questions we show that we are back to
1177 * progressing. */
1178 last_progress = PROGRESS_NONE;
1179 last_progress_time = 0;
1180
1181 while (q != NULL) {
1182 for (i = 0; i < DIM(question_handlers); i++) {
1183 text_handler *handler;
1184 struct plugin *plugin = NULL;
1185
1186 printed = 0;
1187 if (*question_handlers[i].type)
1188 handler = question_handlers[i].handler;
1189 else {
1190 plugin = plugin_find(obj, q->template->type);
1191 if (plugin) {
1192 INFO(INFO_DEBUG, "Found plugin for %s", q->template->type);
1193 handler = (text_handler *) plugin->handler;
1194 } else {
1195 INFO(INFO_DEBUG, "No plugin for %s", q->template->type);
1196 continue;
1197 }
1198 }
1199
1200 if (plugin || strcmp(q->template->type, question_handlers[i].type) == 0)
1201 {
1202 if (!data->previous_title ||
1203 strcmp(obj->title, data->previous_title) != 0)
1204 {
1205 size_t underline_len;
1206 char *underline;
1207
1208 /* TODO: can't tell if we called go()
1209 * twice during one progress bar, but
1210 * I'm guessing that's relatively
1211 * unimportant (c.f. #271707).
1212 */
1213 if (obj->progress_title != NULL)
1214 putchar('\n');
1215 underline_len = strlen(obj->title);
1216 underline = malloc(underline_len + 1);
1217 memset(underline, '-', underline_len);
1218 underline[underline_len] = '\0';
1219 printf("%s\n%s\n\n", obj->title, underline);
1220 printed += 3;
1221 free(underline);
1222 free(data->previous_title);
1223 data->previous_title = strdup(obj->title);
1224 }
1225 printed += text_handler_displaydesc(obj, printed, q);
1226 ret = handler(obj, printed, q);
1227 putchar('\n');
1228 if (ret == DC_OK)
1229 frontend_qdb_set(obj->qdb, q, 0);
1230 if (plugin)
1231 plugin_delete(plugin);
1232 break;
1233 }
1234 }
1235 if (ret == DC_NOTOK)
1236 break;
1237 if (i == DIM(question_handlers))
1238 return DC_NOTIMPL;
1239 if (ret == DC_OK)
1240 q = q->next;
1241 else if (ret == DC_GOBACK) {
1242 do {
1243 q = q->prev;
1244 } while (q != NULL && 0 == strcmp("error", q->template->type));
1245 }
1246 }
1247 return ret;
1248 }
1249
1250 static void text_progress_start(struct frontend *obj, int min, int max, struct question *title)
1251 {
1252 char *title_desc;
1253
1254 question_deref(obj->progress_title);
1255 obj->progress_title = title;
1256 question_ref(obj->progress_title);
1257 obj->progress_min = min;
1258 obj->progress_max = max;
1259 obj->progress_cur = min;
1260
1261 title_desc = q_get_raw_description(title);
1262 printf("%s ", title_desc);
1263 free(title_desc);
1264 fflush(stdout);
1265
1266 /* Clear any previous SIGINT */
1267 int res = sigtimedwait(&sigint_set, NULL, &null_timeout);
1268 }
1269
1270 static int text_progress_set(struct frontend *obj, int val)
1271 {
1272 int new;
1273 time_t now = time(NULL);
1274
1275 /* Catch SIGINT, and in that case, cancel the current step */
1276 if (obj->methods.can_cancel_progress(obj)
1277 && sigtimedwait(&sigint_set, NULL, &null_timeout) == SIGINT)
1278 return DC_GOBACK;
1279
1280 obj->progress_cur = val;
1281 new = ((double)(obj->progress_cur - obj->progress_min) /
1282 (double)(obj->progress_max - obj->progress_min) * 100.0);
1283 if (new < last_progress)
1284 last_progress = PROGRESS_NONE;
1285 if (last_progress_time == 0)
1286 last_progress_time = now;
1287 /* Prevent verbose output */
1288 if (new / PROGRESS_STEP == last_progress / PROGRESS_STEP
1289 && now < last_progress_time + PROGRESS_TIME_STEP)
1290 return DC_OK;
1291 last_progress = new;
1292 last_progress_time = now;
1293 printf("... %d%%", new);
1294 fflush(stdout);
1295
1296 return DC_OK;
1297 }
1298
1299 static void text_progress_stop(struct frontend *obj)
1300 {
1301 INFO(INFO_DEBUG, "%s", __FUNCTION__);
1302 printf("\n");
1303 fflush(stdout);
1304 question_deref(obj->progress_title);
1305 obj->progress_title = NULL;
1306 }
1307
1308 struct frontend_module debconf_frontend_module =
1309 {
1310 .initialize = text_initialize,
1311 .shutdown = text_shutdown,
1312 .lookup_directive = text_lookup_directive,
1313 .can_go_back = text_can_go_back,
1314 .can_cancel_progress = text_can_cancel_progress,
1315 .can_align = text_can_align,
1316 .go = text_go,
1317 .progress_start = text_progress_start,
1318 .progress_set = text_progress_set,
1319 .progress_stop = text_progress_stop,
1320 };