]> git.ozlabs.org Git - petitboot/commitdiff
ui/ncurses: Validate URL field v1.7.0
authorSamuel Mendoza-Jonas <sam@mendozajonas.com>
Mon, 26 Feb 2018 04:58:10 +0000 (15:58 +1100)
committerSamuel Mendoza-Jonas <sam@mendozajonas.com>
Tue, 27 Feb 2018 00:43:37 +0000 (11:43 +1100)
Make sure the URL field is a valid URL before allowing the user to
proceed.

Signed-off-by: Samuel Mendoza-Jonas <sam@mendozajonas.com>
ui/ncurses/nc-config.c
ui/ncurses/nc-widgets.c
ui/ncurses/nc-widgets.h

index 8ffa5ef0d2ffadc92d2b9c6570a09cc7c1ea0e60..5c0f23bd88d284ff962ed70aa4571c19d01bb215 100644 (file)
@@ -1042,6 +1042,7 @@ static void config_screen_setup_widgets(struct config_screen *screen,
 
        screen->widgets.url_l = widget_new_label(set, 0, 0, _("URL:"));
        screen->widgets.url_f = widget_new_textbox(set, 0, 0, 32, url);
+       widget_textbox_set_validator_url(screen->widgets.url_f);
        screen->widgets.url_help_l =
                widget_new_label(set, 0, 0, _("(eg. tftp://)"));
 
index 15cec80f3594ae3600f2cecf891db28d4532901e..93c882b0849af43a00cbe417584e7edd375eb46b 100644 (file)
@@ -53,6 +53,7 @@
 #include <util/util.h>
 #include <i18n/i18n.h>
 #include <fold/fold.h>
+#include <url/url.h>
 
 #include "nc-cui.h"
 #include "nc-widgets.h"
@@ -83,6 +84,7 @@ struct nc_widgetset {
 
        /* custom validators */
        FIELDTYPE *ipv4_multi_type;
+       FIELDTYPE *url_type;
 };
 
 struct nc_widget {
@@ -399,6 +401,20 @@ void widget_textbox_set_validator_integer(struct nc_widget_textbox *textbox,
        set_field_type(textbox->widget.field, TYPE_INTEGER, 1, min, max);
 }
 
+static bool check_url_field(FIELD *field,
+               const void *arg __attribute__((unused)))
+{
+       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);
+}
+
 void widget_textbox_set_validator_ipv4(struct nc_widget_textbox *textbox)
 {
        set_field_type(textbox->widget.field, TYPE_IPV4);
index 4b67da7463d24613d061bf9f15ec26a4255b175c..aa9263fbaa04bf44f4957ad968f5eb5dd461cef2 100644 (file)
@@ -42,6 +42,7 @@ void widget_textbox_set_validator_integer(struct nc_widget_textbox *textbox,
                long min, long max);
 void widget_textbox_set_validator_ipv4(struct nc_widget_textbox *textbox);
 void widget_textbox_set_validator_ipv4_multi(struct nc_widget_textbox *textbox);
+void widget_textbox_set_validator_url(struct nc_widget_textbox *textbox);
 
 void widget_subset_add_option(struct nc_widget_subset *subset, const char *text);
 void widget_subset_make_active(struct nc_widget_subset *subset, int idx);