]> git.ozlabs.org Git - petitboot/blobdiff - ui/ncurses/nc-widgets.c
ui/ncurses: in lockdown ensure system reboot in ncurses menu exit
[petitboot] / ui / ncurses / nc-widgets.c
index 5c67d827b506e5372158586beeb761b9d2b1bce0..30915a97d89e57b2ca51dd1ce002627f96e159d4 100644 (file)
 #  error "Curses form.h not found."
 #endif
 
-#include <string.h>
+#include <arpa/inet.h>
 #include <ctype.h>
+#include <stdlib.h>
+#include <string.h>
 
 #include <talloc/talloc.h>
 #include <types/types.h>
 #include <log/log.h>
 #include <util/util.h>
+#include <i18n/i18n.h>
+#include <fold/fold.h>
+#include <url/url.h>
 
 #include "nc-cui.h"
 #include "nc-widgets.h"
@@ -80,7 +85,9 @@ struct nc_widgetset {
        FIELD   *cur_field;
 
        /* custom validators */
-       FIELDTYPE *ipv4_multi_type;
+       FIELDTYPE *ip_multi_type;
+       FIELDTYPE *ip_type;
+       FIELDTYPE *url_type;
 };
 
 struct nc_widget {
@@ -137,6 +144,7 @@ struct nc_widget_select {
                char            *str;
                int             val;
                FIELD           *field;
+               int             lines;
        } *options;
        int                     top, left, size;
        int                     n_options, selected_option;
@@ -159,21 +167,6 @@ static bool key_is_select(int key)
        return key == ' ' || key == '\r' || key == '\n' || key == KEY_ENTER;
 }
 
-static bool key_is_minus(int key)
-{
-       return key == 055;
-}
-
-static bool key_is_left(int key)
-{
-       return key == KEY_LEFT;
-}
-
-static bool key_is_right(int key)
-{
-       return key == KEY_RIGHT;
-}
-
 static bool process_key_nop(struct nc_widget *widget __attribute__((unused)),
                FORM *form __attribute((unused)),
                int key __attribute__((unused)))
@@ -349,6 +342,14 @@ static bool textbox_process_key(
        case KEY_DC:
                form_driver(form, REQ_DEL_CHAR);
                break;
+       case '\t':
+       case KEY_BTAB:
+       case KEY_UP:
+       case KEY_DOWN:
+       case KEY_PPAGE:
+       case KEY_NPAGE:
+               /* Don't catch navigational keys */
+               return false;
        default:
                form_driver(form, key);
                break;
@@ -403,54 +404,102 @@ void widget_textbox_set_validator_integer(struct nc_widget_textbox *textbox,
        set_field_type(textbox->widget.field, TYPE_INTEGER, 1, min, max);
 }
 
-void widget_textbox_set_validator_ipv4(struct nc_widget_textbox *textbox)
+static bool check_url_field(FIELD *field,
+               const void *arg __attribute__((unused)))
 {
-       set_field_type(textbox->widget.field, TYPE_IPV4);
+       return is_url(field_buffer(field, 0));
+}
+
+void widget_textbox_set_validator_url(struct nc_widget_textbox *textbox)
+{
+       if (!textbox->set->url_type)
+               textbox->set->url_type = new_fieldtype(check_url_field, NULL);
+
+       set_field_type(textbox->widget.field, textbox->set->url_type);
 }
 
-static bool check_ipv4_multi_char(int c,
+static bool check_ip_field(FIELD *field,
                const void *arg __attribute__((unused)))
+{
+       char *str;
+       int rc;
+
+       str = strip_string(field_buffer(field, 0));
+
+       rc = addr_scheme(str);
+
+       return (rc == AF_INET || rc == AF_INET6);
+}
+
+
+static bool check_ipv6_multi_char(int c)
+{
+       return isdigit(c) || (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') ||
+              c == ':' || c == ' ';
+}
+
+static bool check_ipv4_multi_char(int c)
 {
        return isdigit(c) || c == '.' || c == ' ';
 }
 
-static bool check_ipv4_multi_field(FIELD *field,
+static bool check_ip_multi_field(FIELD *field,
                const void *arg __attribute__((unused)))
 {
-       char *buf = field_buffer(field, 0);
-       unsigned int ip[4];
-       int n, len;
-
-       while (*buf != '\0') {
-               n = sscanf(buf, "%u.%u.%u.%u%n",
-                               &ip[0], &ip[1], &ip[2], &ip[3], &len);
-               if (n != 4)
-                       return false;
-
-               if (ip[0] > 255 || ip[1] > 255 || ip[2] > 255 || ip[3] > 255)
-                       return false;
-
-               for (buf += len; *buf != '\0'; buf++) {
-                       if (isspace(*buf))
-                               continue;
-                       else if (isdigit(*buf))
-                               break;
-                       else
-                               return false;
-               }
+       char *buf, *tok, *saveptr;
+       bool result;
+       int type;
+
+       /* Use strdup directly since we can't easily reach a talloc parent */
+       buf = strdup(strip_string(field_buffer(field, 0)));
+       if (!buf)
+               /* We tried */
+               return true;
+
+       result = false;
+       tok = strtok_r(buf, " ", &saveptr);
+       if (!tok || *tok == '\0')
+               goto err;
+
+       while (tok) {
+               type = addr_scheme(tok);
+               if (!(type == AF_INET || type == AF_INET6))
+                       goto err;
+
+               tok = strtok_r(NULL, " ", &saveptr);
        }
+       result = true;
 
-       return true;
+err:
+       free(buf);
+       return result;
+}
+
+static bool check_ip_multi_char(int c, const void *arg __attribute__((unused)))
+{
+       return (check_ipv4_multi_char(c) || check_ipv6_multi_char(c));
+}
+
+void widget_textbox_set_validator_ip(struct nc_widget_textbox *textbox)
+{
+       if (!textbox->set->ip_type) {
+               textbox->set->ip_type = new_fieldtype(check_ip_field, NULL);
+       }
+       set_field_type(textbox->widget.field, textbox->set->ip_type);
 }
 
-void widget_textbox_set_validator_ipv4_multi(struct nc_widget_textbox *textbox)
+/*
+ * In a perfect world we would use link_fieldtype() but segfaults do not
+ * enhance the user experience.
+ */
+void widget_textbox_set_validator_ip_multi(struct nc_widget_textbox *textbox)
 {
-       if (!textbox->set->ipv4_multi_type) {
-               textbox->set->ipv4_multi_type = new_fieldtype(
-                               check_ipv4_multi_field,
-                               check_ipv4_multi_char);
+       if (!textbox->set->ip_multi_type) {
+               textbox->set->ip_multi_type = new_fieldtype(
+                                               check_ip_multi_field,
+                                               check_ip_multi_char);
        }
-       set_field_type(textbox->widget.field, textbox->set->ipv4_multi_type);
+       set_field_type(textbox->widget.field, textbox->set->ip_multi_type);
 }
 
 static void subset_update_order(struct nc_widget_subset *subset)
@@ -478,6 +527,7 @@ static void subset_delete_active(struct nc_widget_subset *subset, int idx)
        struct nc_widget *widget;
        size_t rem;
        int i, val;
+       uint32_t opts;
 
        /* Shift field focus to nearest active option or next visible field */
        if (subset->n_active > 1) {
@@ -489,8 +539,11 @@ static void subset_delete_active(struct nc_widget_subset *subset, int idx)
        } else {
                for (i = 0; i < set->n_fields; i++)
                        if (field_visible(set->fields[i])) {
-                               set->cur_field = set->fields[i];
-                               break;
+                               opts = field_opts(set->fields[i]);
+                               if ((opts & O_ACTIVE) == O_ACTIVE) {
+                                       set->cur_field = set->fields[i];
+                                       break;
+                               }
                        }
        }
 
@@ -519,7 +572,7 @@ static bool subset_process_key(struct nc_widget *w, FORM *form, int key)
        int i, val, opt_idx = -1;
        FIELD *field;
 
-       if (!key_is_minus(key) && !key_is_left(key) && !key_is_right(key))
+       if (key != '-' && key != '+' && key != KEY_DC && key != KEY_BACKSPACE)
                return false;
 
        field = current_field(form);
@@ -535,10 +588,10 @@ static bool subset_process_key(struct nc_widget *w, FORM *form, int key)
        if (opt_idx < 0)
                return false;
 
-       if (key_is_minus(key))
+       if (key == KEY_DC || key == KEY_BACKSPACE)
                subset_delete_active(subset, opt_idx);
 
-       if (key_is_left(key)){
+       if (key == '-') {
                if (opt_idx == 0)
                        return true;
 
@@ -547,7 +600,7 @@ static bool subset_process_key(struct nc_widget *w, FORM *form, int key)
                subset->active[opt_idx - 1] = val;
        }
 
-       if (key_is_right(key)){
+       if (key == '+') {
                if (opt_idx >= subset->n_active - 1)
                        return true;
 
@@ -838,21 +891,25 @@ static void select_set_visible(struct nc_widget *widget, bool visible)
 static void select_move(struct nc_widget *widget, int y, int x)
 {
        struct nc_widget_select *select = to_select(widget);
-       int i;
+       int i, cur = 0;
 
-       for (i = 0; i < select->n_options; i++)
-               field_move(select->options[i].field, y + i, x);
+       for (i = 0; i < select->n_options; i++) {
+               field_move(select->options[i].field, y + cur, x);
+               cur += select->options[i].lines;
+       }
 }
 
 static void select_field_focus(struct nc_widget *widget, FIELD *field)
 {
        struct nc_widget_select *select = to_select(widget);
-       int i;
+       int i, cur = 0;
 
        for (i = 0; i < select->n_options; i++) {
-               if (field != select->options[i].field)
+               if (field != select->options[i].field) {
+                       cur += select->options[i].lines;
                        continue;
-               widget->focus_y = i;
+               }
+               widget->focus_y = cur;
                return;
        }
 }
@@ -894,10 +951,47 @@ struct nc_widget_select *widget_new_select(struct nc_widgetset *set,
        return select;
 }
 
+static int widget_select_fold_cb(void *arg, const char *buf, int len)
+{
+       struct nc_widget_select *select = arg;
+       char *line, *newstr, *padbuf = NULL;
+       int i, pad;
+
+       if (!len)
+               return 0;
+
+       line = talloc_strndup(select->options, buf, len);
+
+       i = select->n_options - 1;
+       pad = max(0, select->widget.width - strncols(line));
+
+       if (pad) {
+               padbuf = talloc_array(select->options, char, pad + 1);
+               memset(padbuf, ' ', pad);
+               padbuf[pad] = '\0';
+       }
+
+       if (select->options[i].str)
+               newstr = talloc_asprintf_append(select->options[i].str,
+                                                        "%s%s", line,
+                                                        pad ? padbuf : "");
+       else
+               newstr = talloc_asprintf(select->options, "%s%s", line,
+                                                        pad ? padbuf : "");
+
+       select->options[i].str = newstr;
+       select->options[i].lines++;
+
+       talloc_free(padbuf);
+       talloc_free(line);
+       return 0;
+}
+
 void widget_select_add_option(struct nc_widget_select *select, int value,
                const char *text, bool selected)
 {
        const char *str;
+       char *full_text;
        FIELD *f;
        int i;
 
@@ -916,23 +1010,29 @@ void widget_select_add_option(struct nc_widget_select *select, int value,
                str = select_unselected_str;
 
        i = select->n_options++;
-       select->widget.height = select->n_options;
-
        select->options = talloc_realloc(select, select->options,
                                struct select_option, i + 2);
        select->options[i].val = value;
-       select->options[i].str = talloc_asprintf(select->options,
-                                       "%s %s", str, text);
+       select->options[i].lines = 0;
+       select->options[i].str = NULL;
 
-       select->options[i].field = f = new_field(1, select->size,
-                                               select->top + i,
-                                               select->left, 0, 0);
+       full_text = talloc_asprintf(select->options, "%s %s", str, text);
+       fold_text(full_text, select->widget.width,
+                 widget_select_fold_cb, select);
 
-       field_opts_off(f, O_WRAP | O_EDIT);
+       select->options[i].field = f = new_field(select->options[i].lines,
+                                       select->size,
+                                       select->top + select->widget.height,
+                                       select->left, 0, 0);
+
+       select->widget.height += select->options[i].lines;
+
+       field_opts_off(f, O_EDIT);
        set_field_userptr(f, &select->widget);
        set_field_buffer(f, 0, select->options[i].str);
 
        widgetset_add_field(select->set, f);
+       talloc_free(full_text);
 }
 
 int widget_select_get_value(struct nc_widget_select *select)
@@ -944,7 +1044,7 @@ int widget_select_get_value(struct nc_widget_select *select)
 
 int widget_select_height(struct nc_widget_select *select)
 {
-       return select->n_options;
+       return select->widget.height;
 }
 
 void widget_select_on_change(struct nc_widget_select *select,
@@ -1002,16 +1102,18 @@ struct nc_widget_button *widget_new_button(struct nc_widgetset *set,
                void (*click)(void *), void *arg)
 {
        struct nc_widget_button *button;
+       int idx, len, pad1, pad2, bufsz;
        char *text;
        FIELD *f;
-       int idx, len;
+
+       int field_size = size + 2;
 
        button = talloc_zero(set, struct nc_widget_button);
        button->widget.height = 1;
-       button->widget.width = size;
+       button->widget.width = field_size;
        button->widget.x = x;
        button->widget.y = y;
-       button->widget.field = f = new_field(1, size + 2, y, x, 0, 0);
+       button->widget.field = f = new_field(1, field_size, y, x, 0, 0);
        button->widget.process_key = button_process_key;
        button->widget.focussed_attr = A_REVERSE;
        button->widget.unfocussed_attr = A_NORMAL;
@@ -1021,17 +1123,28 @@ struct nc_widget_button *widget_new_button(struct nc_widgetset *set,
        field_opts_off(f, O_EDIT);
        set_field_userptr(f, &button->widget);
 
-       /* center str in a size-char buffer, but don't overrun */
-       len = strlen(str);
-       len = min(len, size);
-       idx = (size - len) / 2;
+       /* Center str in the field. This depends on the number of columns used
+        * by the string, not the number of chars in str */
+       len = strncols(str);
+       if (len <= size) {
+               idx = (field_size - len) / 2;
+       } else {
+               idx = 1;
+               pb_log("Warning: '%s' %d columns wide "
+                      "but button is %d columns wide\n",
+                      str, len, size);
+       }
 
-       text = talloc_array(button, char, size + 3);
-       memset(text, ' ', size + 2);
-       memcpy(text + idx + 1, str, len);
+       pad1 = max(idx - 1, 0);
+       pad2 = max(size - len - pad1, 0);
+       bufsz = 1 + pad1 + strlen(str) + pad2 + 2;
+
+       text = talloc_array(button, char, bufsz);
+       memset(text, ' ', bufsz);
+       memcpy(text + idx, str, strlen(str));
        text[0] = '[';
-       text[size + 1] = ']';
-       text[size + 2] = '\0';
+       text[bufsz - 2] = ']';
+       text[bufsz - 1] = '\0';
 
        set_field_buffer(f, 0, text);
 
@@ -1058,6 +1171,12 @@ bool widgetset_process_key(struct nc_widgetset *set, int key)
        field = current_field(set->form);
        assert(field);
 
+       widget = field_userptr(field);
+
+       if (widget->process_key)
+               if (widget->process_key(widget, set->form, key))
+                       return true;
+
        tab = false;
 
        /* handle field change events */
@@ -1066,23 +1185,28 @@ bool widgetset_process_key(struct nc_widgetset *set, int key)
                tab = true;
                /* fall through */
        case KEY_UP:
-               req = REQ_PREV_FIELD;
+               req = REQ_SPREV_FIELD;
                break;
        case '\t':
                tab = true;
                /* fall through */
        case KEY_DOWN:
-               req = REQ_NEXT_FIELD;
+               req = REQ_SNEXT_FIELD;
                break;
        case KEY_PPAGE:
-               req = REQ_FIRST_FIELD;
+               req = REQ_SFIRST_FIELD;
                break;
        case KEY_NPAGE:
-               req = REQ_LAST_FIELD;
+               req = REQ_SLAST_FIELD;
+               break;
+       case KEY_LEFT:
+               req = REQ_LEFT_FIELD;
+               break;
+       case KEY_RIGHT:
+               req = REQ_RIGHT_FIELD;
                break;
        }
 
-       widget = field_userptr(field);
        if (req) {
                widget_focus_change(widget, field, false);
                form_driver(set->form, req);
@@ -1107,18 +1231,19 @@ bool widgetset_process_key(struct nc_widgetset *set, int key)
                return true;
        }
 
-       if (!widget->process_key)
-               return false;
-
-       return widget->process_key(widget, set->form, key);
+       return false;
 }
 
 static int widgetset_destructor(void *ptr)
 {
        struct nc_widgetset *set = ptr;
        free_form(set->form);
-       if (set->ipv4_multi_type)
-               free_fieldtype(set->ipv4_multi_type);
+       if (set->ip_type)
+               free_fieldtype(set->ip_type);
+       if (set->ip_multi_type)
+               free_fieldtype(set->ip_multi_type);
+       if (set->url_type)
+               free_fieldtype(set->url_type);
        return 0;
 }
 
@@ -1161,10 +1286,8 @@ void widgetset_post(struct nc_widgetset *set)
        post_form(set->form);
        form_driver(set->form, REQ_END_FIELD);
 
-       if (set->cur_field) {
+       if (set->cur_field)
                set_current_field(set->form, set->cur_field);
-               field = set->cur_field;
-       }
 
        field = current_field(set->form);
        widget = field_userptr(field);