54eb7ac5740d891bd32c6a7bcc57655cbf19bc8a
[petitboot] / ui / ncurses / nc-config.c
1 /*
2  *  Copyright (C) 2013 IBM Corporation
3  *
4  *  This program is free software; you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation; version 2 of the License.
7  *
8  *  This program is distributed in the hope that it will be useful,
9  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11  *  GNU General Public License for more details.
12  *
13  *  You should have received a copy of the GNU General Public License
14  *  along with this program; if not, write to the Free Software
15  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  */
17
18 #if defined(HAVE_CONFIG_H)
19 #include "config.h"
20 #endif
21
22 #include <errno.h>
23 #include <stdlib.h>
24 #include <string.h>
25
26 #include <pb-config/pb-config.h>
27 #include <talloc/talloc.h>
28 #include <types/types.h>
29 #include <log/log.h>
30 #include <i18n/i18n.h>
31
32 #include "nc-cui.h"
33 #include "nc-config.h"
34 #include "nc-widgets.h"
35
36 #define N_FIELDS        25
37
38 extern const char *config_help_text;
39
40 enum autoboot_type {
41         AUTOBOOT_ANY,
42         AUTOBOOT_ONE,
43         AUTOBOOT_DISABLED,
44 };
45
46 enum net_conf_type {
47         NET_CONF_TYPE_DHCP_ALL,
48         NET_CONF_TYPE_DHCP_ONE,
49         NET_CONF_TYPE_STATIC,
50 };
51
52 struct config_screen {
53         struct nc_scr           scr;
54         struct cui              *cui;
55         struct nc_widgetset     *widgetset;
56         WINDOW                  *pad;
57
58         bool                    exit;
59         bool                    show_help;
60         void                    (*on_exit)(struct cui *);
61
62         int                     scroll_y;
63
64         int                     label_x;
65         int                     field_x;
66         int                     network_config_y;
67
68         enum net_conf_type      net_conf_type;
69         enum autoboot_type      autoboot_type;
70
71         struct {
72                 struct nc_widget_select         *autoboot_f;
73                 struct nc_widget_label          *autoboot_l;
74                 struct nc_widget_select         *boot_device_f;
75                 struct nc_widget_textbox        *timeout_f;
76                 struct nc_widget_label          *timeout_l;
77                 struct nc_widget_label          *timeout_help_l;
78
79                 struct nc_widget_label          *network_l;
80                 struct nc_widget_select         *network_f;
81
82                 struct nc_widget_label          *iface_l;
83                 struct nc_widget_select         *iface_f;
84                 struct nc_widget_label          *ip_addr_l;
85                 struct nc_widget_textbox        *ip_addr_f;
86                 struct nc_widget_label          *ip_mask_l;
87                 struct nc_widget_textbox        *ip_mask_f;
88                 struct nc_widget_label          *ip_addr_mask_help_l;
89                 struct nc_widget_label          *gateway_l;
90                 struct nc_widget_textbox        *gateway_f;
91                 struct nc_widget_label          *gateway_help_l;
92                 struct nc_widget_label          *dns_l;
93                 struct nc_widget_textbox        *dns_f;
94                 struct nc_widget_label          *dns_dhcp_help_l;
95                 struct nc_widget_label          *dns_help_l;
96
97                 struct nc_widget_button         *ok_b;
98                 struct nc_widget_button         *help_b;
99                 struct nc_widget_button         *cancel_b;
100         } widgets;
101 };
102
103 static struct config_screen *config_screen_from_scr(struct nc_scr *scr)
104 {
105         struct config_screen *config_screen;
106
107         assert(scr->sig == pb_config_screen_sig);
108         config_screen = (struct config_screen *)
109                 ((char *)scr - (size_t)&((struct config_screen *)0)->scr);
110         assert(config_screen->scr.sig == pb_config_screen_sig);
111         return config_screen;
112 }
113
114 static void pad_refresh(struct config_screen *screen)
115 {
116         int y, x, rows, cols;
117
118         getmaxyx(screen->scr.sub_ncw, rows, cols);
119         getbegyx(screen->scr.sub_ncw, y, x);
120
121         prefresh(screen->pad, screen->scroll_y, 0, y, x, rows, cols);
122 }
123
124 static void config_screen_process_key(struct nc_scr *scr, int key)
125 {
126         struct config_screen *screen = config_screen_from_scr(scr);
127         bool handled;
128
129         handled = widgetset_process_key(screen->widgetset, key);
130
131         if (!handled) {
132                 switch (key) {
133                 case 'x':
134                 case 27: /* esc */
135                         screen->exit = true;
136                         break;
137                 case 'h':
138                         screen->show_help = true;
139                         break;
140                 }
141         }
142
143         if (screen->exit) {
144                 screen->on_exit(screen->cui);
145
146         } else if (screen->show_help) {
147                 screen->show_help = false;
148                 cui_show_help(screen->cui, _("System Configuration"),
149                                 config_help_text);
150
151         } else if (handled) {
152                 pad_refresh(screen);
153         }
154 }
155
156 static void config_screen_resize(struct nc_scr *scr)
157 {
158         struct config_screen *screen = config_screen_from_scr(scr);
159         (void)screen;
160 }
161
162 static int config_screen_post(struct nc_scr *scr)
163 {
164         struct config_screen *screen = config_screen_from_scr(scr);
165         widgetset_post(screen->widgetset);
166         nc_scr_frame_draw(scr);
167         redrawwin(scr->main_ncw);
168         wrefresh(screen->scr.main_ncw);
169         pad_refresh(screen);
170         return 0;
171 }
172
173 static int config_screen_unpost(struct nc_scr *scr)
174 {
175         struct config_screen *screen = config_screen_from_scr(scr);
176         widgetset_unpost(screen->widgetset);
177         return 0;
178 }
179
180 struct nc_scr *config_screen_scr(struct config_screen *screen)
181 {
182         return &screen->scr;
183 }
184
185 static int screen_process_form(struct config_screen *screen)
186 {
187         const struct system_info *sysinfo = screen->cui->sysinfo;
188         enum net_conf_type net_conf_type;
189         struct interface_config *iface;
190         struct config *config;
191         char *str, *end;
192         int rc, idx;
193
194         config = config_copy(screen, screen->cui->config);
195
196         screen->autoboot_type =
197                 widget_select_get_value(screen->widgets.autoboot_f);
198
199         config->autoboot_enabled = screen->autoboot_type != AUTOBOOT_DISABLED;
200
201         if (screen->autoboot_type == AUTOBOOT_ONE) {
202                 char mac[20], *uuid = NULL;
203
204                 /* if idx is -1 here, we have an unknown UUID selected.
205                  * Otherwise, it's a blockdev index (idx <= n_blockdevs) or an
206                  * interface index.
207                  */
208                 idx = widget_select_get_value(screen->widgets.boot_device_f);
209                 if (idx >= (int)sysinfo->n_blockdevs) {
210                         struct interface_info *info = sysinfo->
211                                 interfaces[idx - sysinfo->n_blockdevs];
212                         mac_str(info->hwaddr, info->hwaddr_size,
213                                         mac, sizeof(mac));
214                         uuid = mac;
215                 } else if (idx != -1) {
216                         uuid = sysinfo->blockdevs[idx]->uuid;
217                 }
218
219                 if (uuid) {
220                         talloc_free(config->boot_device);
221                         config->boot_device = talloc_strdup(config, uuid);
222                 }
223         }
224
225         str = widget_textbox_get_value(screen->widgets.timeout_f);
226         if (str) {
227                 unsigned long x;
228                 errno = 0;
229                 x = strtoul(str, &end, 10);
230                 if (!errno && end != str)
231                         config->autoboot_timeout_sec = x;
232         }
233
234         net_conf_type = widget_select_get_value(screen->widgets.network_f);
235
236         /* if we don't have any network interfaces, prevent per-interface
237          * configuration */
238         if (sysinfo->n_interfaces == 0)
239                 net_conf_type = NET_CONF_TYPE_DHCP_ALL;
240
241         if (net_conf_type == NET_CONF_TYPE_DHCP_ALL) {
242                 config->network.n_interfaces = 0;
243
244         } else {
245                 iface = talloc_zero(config, struct interface_config);
246                 config->network.n_interfaces = 1;
247                 config->network.interfaces = talloc_array(config,
248                                 struct interface_config *, 1);
249                 config->network.interfaces[0] = iface;
250
251                 /* copy hwaddr (from the sysinfo interface data) to
252                  * the configuration */
253                 idx = widget_select_get_value(screen->widgets.iface_f);
254                 memcpy(iface->hwaddr, sysinfo->interfaces[idx]->hwaddr,
255                                 sizeof(iface->hwaddr));
256         }
257
258         if (net_conf_type == NET_CONF_TYPE_DHCP_ONE) {
259                 iface->method = CONFIG_METHOD_DHCP;
260         }
261
262         if (net_conf_type == NET_CONF_TYPE_STATIC) {
263                 char *ip, *mask, *gateway;
264
265                 ip = widget_textbox_get_value(screen->widgets.ip_addr_f);
266                 mask = widget_textbox_get_value(screen->widgets.ip_mask_f);
267                 gateway = widget_textbox_get_value(screen->widgets.gateway_f);
268
269                 if (!ip || !*ip || !mask || !*mask) {
270                         screen->scr.frame.status =
271                                 _("No IP / mask values are set");
272                         nc_scr_frame_draw(&screen->scr);
273                         talloc_free(config);
274                         return -1;
275                 }
276
277                 iface->method = CONFIG_METHOD_STATIC;
278                 iface->static_config.address = talloc_asprintf(iface, "%s/%s",
279                                 ip, mask);
280                 iface->static_config.gateway = talloc_strdup(iface, gateway);
281         }
282
283         str = widget_textbox_get_value(screen->widgets.dns_f);
284         talloc_free(config->network.dns_servers);
285         config->network.dns_servers = NULL;
286         config->network.n_dns_servers = 0;
287
288         if (str && strlen(str)) {
289                 char *dns, *tmp;
290                 int i;
291
292                 for (;;) {
293                         dns = strtok_r(str, " \t", &tmp);
294
295                         if (!dns)
296                                 break;
297
298                         i = config->network.n_dns_servers++;
299                         config->network.dns_servers = talloc_realloc(config,
300                                         config->network.dns_servers,
301                                         const char *,
302                                         config->network.n_dns_servers);
303                         config->network.dns_servers[i] =
304                                 talloc_strdup(config, dns);
305
306                         str = NULL;
307                 }
308         }
309
310         rc = cui_send_config(screen->cui, config);
311         talloc_free(config);
312
313         if (rc)
314                 pb_log("cui_send_config failed!\n");
315         else
316                 pb_debug("config sent!\n");
317
318         return 0;
319 }
320
321 static void ok_click(void *arg)
322 {
323         struct config_screen *screen = arg;
324         if (screen_process_form(screen))
325                 /* errors are written to the status line, so we'll need
326                  * to refresh */
327                 wrefresh(screen->scr.main_ncw);
328         else
329                 screen->exit = true;
330 }
331
332 static void help_click(void *arg)
333 {
334         struct config_screen *screen = arg;
335         screen->show_help = true;
336 }
337
338 static void cancel_click(void *arg)
339 {
340         struct config_screen *screen = arg;
341         screen->exit = true;
342 }
343
344 static int layout_pair(struct config_screen *screen, int y,
345                 struct nc_widget_label *label,
346                 struct nc_widget *field)
347 {
348         struct nc_widget *label_w = widget_label_base(label);
349         widget_move(label_w, y, screen->label_x);
350         widget_move(field, y, screen->field_x);
351         return max(widget_height(label_w), widget_height(field));
352 }
353
354 static void config_screen_layout_widgets(struct config_screen *screen)
355 {
356         struct nc_widget *wl, *wf, *wh;
357         int y, x, help_x;
358         bool show;
359
360         y = 1;
361         help_x = screen->field_x + 2 +
362                 widget_width(widget_textbox_base(screen->widgets.dns_f));
363
364         y += layout_pair(screen, y, screen->widgets.autoboot_l,
365                         widget_select_base(screen->widgets.autoboot_f));
366
367         wf = widget_select_base(screen->widgets.boot_device_f);
368         if (screen->autoboot_type == AUTOBOOT_ONE) {
369                 widget_set_visible(wf, true);
370                 widget_move(wf, y, screen->field_x + 3);
371                 y += widget_height(wf);
372         } else {
373                 widget_set_visible(wf, false);
374         }
375
376         y += 1;
377
378         wf = widget_textbox_base(screen->widgets.timeout_f);
379         wl = widget_label_base(screen->widgets.timeout_l);
380         wh = widget_label_base(screen->widgets.timeout_help_l);
381         if (screen->autoboot_type != AUTOBOOT_DISABLED) {
382                 widget_set_visible(wl, true);
383                 widget_set_visible(wf, true);
384                 widget_set_visible(wh, true);
385                 widget_move(wl, y, screen->label_x);
386                 widget_move(wf, y, screen->field_x);
387                 widget_move(wh, y, screen->field_x + widget_width(wf) + 1);
388                 y += 2;
389         } else {
390                 widget_set_visible(wl, false);
391                 widget_set_visible(wf, false);
392                 widget_set_visible(wh, false);
393         }
394
395         y += layout_pair(screen, y, screen->widgets.network_l,
396                         widget_select_base(screen->widgets.network_f));
397
398         y += 1;
399
400         /* conditionally show iface select */
401         wl = widget_label_base(screen->widgets.iface_l);
402         wf = widget_select_base(screen->widgets.iface_f);
403
404         show = screen->net_conf_type == NET_CONF_TYPE_DHCP_ONE ||
405                 screen->net_conf_type == NET_CONF_TYPE_STATIC;
406
407         widget_set_visible(wl, show);
408         widget_set_visible(wf, show);
409
410         if (show)
411                 y += layout_pair(screen, y, screen->widgets.iface_l, wf) + 1;
412
413         /* conditionally show static IP params */
414         show = screen->net_conf_type == NET_CONF_TYPE_STATIC;
415
416         wl = widget_label_base(screen->widgets.ip_addr_l);
417         wf = widget_textbox_base(screen->widgets.ip_addr_f);
418         widget_set_visible(wl, show);
419         widget_set_visible(wf, show);
420         x = screen->field_x + widget_width(wf) + 1;
421
422         if (show)
423                 layout_pair(screen, y, screen->widgets.ip_addr_l, wf);
424
425         wl = widget_label_base(screen->widgets.ip_mask_l);
426         wf = widget_textbox_base(screen->widgets.ip_mask_f);
427         widget_set_visible(wl, show);
428         widget_set_visible(wf, show);
429
430         if (show) {
431                 widget_move(wl, y, x);
432                 widget_move(wf, y, x + 2);
433         }
434
435         /* help for IP/mask */
436         wh = widget_label_base(screen->widgets.ip_addr_mask_help_l);
437         widget_set_visible(wh, show);
438         if (show) {
439                 widget_move(wh, y, help_x);
440                 y++;
441         }
442
443         wl = widget_label_base(screen->widgets.gateway_l);
444         wf = widget_textbox_base(screen->widgets.gateway_f);
445         wh = widget_label_base(screen->widgets.gateway_help_l);
446         widget_set_visible(wl, show);
447         widget_set_visible(wf, show);
448         widget_set_visible(wh, show);
449
450         if (show) {
451                 layout_pair(screen, y, screen->widgets.gateway_l, wf);
452                 widget_move(wh, y, help_x);
453                 y++;
454         }
455
456         wh = widget_label_base(screen->widgets.dns_help_l);
457         layout_pair(screen, y, screen->widgets.dns_l,
458                         widget_textbox_base(screen->widgets.dns_f));
459         widget_move(wh, y, help_x);
460         y++;
461
462         /* we show the DNS/DHCP help if we're configuring DHCP */
463         show = screen->net_conf_type != NET_CONF_TYPE_STATIC;
464         wl = widget_label_base(screen->widgets.dns_dhcp_help_l);
465         widget_set_visible(wl, show);
466         if (show) {
467                 widget_move(wl, y, screen->field_x);
468                 y += 1;
469         }
470
471         y += 1;
472
473         widget_move(widget_button_base(screen->widgets.ok_b),
474                         y, screen->field_x);
475         widget_move(widget_button_base(screen->widgets.help_b),
476                         y, screen->field_x + 10);
477         widget_move(widget_button_base(screen->widgets.cancel_b),
478                         y, screen->field_x + 20);
479 }
480
481 static void config_screen_network_change(void *arg, int value)
482 {
483         struct config_screen *screen = arg;
484         screen->net_conf_type = value;
485         widgetset_unpost(screen->widgetset);
486         config_screen_layout_widgets(screen);
487         widgetset_post(screen->widgetset);
488 }
489
490 static void config_screen_autoboot_change(void *arg, int value)
491 {
492         struct config_screen *screen = arg;
493         screen->autoboot_type = value;
494         widgetset_unpost(screen->widgetset);
495         config_screen_layout_widgets(screen);
496         widgetset_post(screen->widgetset);
497 }
498
499 static struct interface_config *first_active_interface(
500                 const struct config *config)
501 {
502         unsigned int i;
503
504         for (i = 0; i < config->network.n_interfaces; i++) {
505                 if (config->network.interfaces[i]->ignore)
506                         continue;
507                 return config->network.interfaces[i];
508         }
509         return NULL;
510 }
511
512 static enum net_conf_type find_net_conf_type(const struct config *config)
513 {
514         struct interface_config *ifcfg;
515
516         ifcfg = first_active_interface(config);
517
518         if (!ifcfg)
519                 return NET_CONF_TYPE_DHCP_ALL;
520
521         else if (ifcfg->method == CONFIG_METHOD_DHCP)
522                 return NET_CONF_TYPE_DHCP_ONE;
523
524         else if (ifcfg->method == CONFIG_METHOD_STATIC)
525                 return NET_CONF_TYPE_STATIC;
526
527         assert(0);
528         return NET_CONF_TYPE_DHCP_ALL;
529 }
530
531 static void config_screen_setup_empty(struct config_screen *screen)
532 {
533         widget_new_label(screen->widgetset, 2, screen->field_x,
534                         _("Waiting for configuration data..."));
535         screen->widgets.cancel_b = widget_new_button(screen->widgetset,
536                         4, screen->field_x, 6, _("Cancel"),
537                         cancel_click, screen);
538 }
539
540
541 static void config_screen_setup_widgets(struct config_screen *screen,
542                 const struct config *config,
543                 const struct system_info *sysinfo)
544 {
545         struct nc_widgetset *set = screen->widgetset;
546         struct interface_config *ifcfg;
547         char *str, *ip, *mask, *gw;
548         enum net_conf_type type;
549         unsigned int i;
550         bool found;
551
552         build_assert(sizeof(screen->widgets) / sizeof(struct widget *)
553                         == N_FIELDS);
554
555         type = screen->net_conf_type;
556         ifcfg = first_active_interface(config);
557
558         screen->widgets.autoboot_l = widget_new_label(set, 0, 0,
559                                         _("Autoboot:"));
560         screen->widgets.autoboot_f = widget_new_select(set, 0, 0, 55);
561
562         widget_select_on_change(screen->widgets.autoboot_f,
563                         config_screen_autoboot_change, screen);
564
565         screen->widgets.boot_device_f = widget_new_select(set, 0, 0, 55);
566
567         widget_select_add_option(screen->widgets.autoboot_f,
568                                         AUTOBOOT_DISABLED,
569                                         _("Don't autoboot"),
570                                         screen->autoboot_type ==
571                                                 AUTOBOOT_DISABLED);
572         widget_select_add_option(screen->widgets.autoboot_f,
573                                         AUTOBOOT_ANY,
574                                         _("Autoboot from any "
575                                                 "disk/network device"),
576                                         screen->autoboot_type ==
577                                                 AUTOBOOT_ANY);
578         widget_select_add_option(screen->widgets.autoboot_f,
579                                         AUTOBOOT_ONE,
580                                         _("Only autoboot from a specific "
581                                                 "disk/network device"),
582                                         screen->autoboot_type ==
583                                                 AUTOBOOT_ONE);
584
585         found = false;
586
587         for (i = 0; i < sysinfo->n_blockdevs; i++) {
588                 struct blockdev_info *bd = sysinfo->blockdevs[i];
589                 bool selected;
590                 char *label;
591
592                 selected = config->boot_device &&
593                                 !strcmp(config->boot_device, bd->uuid);
594                 if (selected)
595                         found = true;
596
597                 label = talloc_asprintf(screen, _("disk: %s [uuid: %s]"),
598                                 bd->name, bd->uuid);
599
600                 widget_select_add_option(screen->widgets.boot_device_f, i,
601                                         label, selected);
602         }
603
604         for (i = 0; i < sysinfo->n_interfaces; i++) {
605                 struct interface_info *info = sysinfo->interfaces[i];
606                 char *label, mac[20];
607                 bool selected;
608
609                 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
610                 selected = config->boot_device &&
611                                 !strcmp(config->boot_device, mac);
612                 if (selected)
613                         found = true;
614
615                 label = talloc_asprintf(screen, _("net:  %s [mac: %s]"),
616                                 info->name, mac);
617
618                 widget_select_add_option(screen->widgets.boot_device_f,
619                                                 i + sysinfo->n_blockdevs,
620                                                 label, selected);
621         }
622
623         if (screen->autoboot_type == AUTOBOOT_ONE && !found) {
624                 char *label;
625
626                 label = talloc_asprintf(screen, _("Unknown UUID: %s"),
627                                 config->boot_device);
628
629                 widget_select_add_option(screen->widgets.boot_device_f, -1,
630                                                 label, true);
631         }
632
633         str = talloc_asprintf(screen, "%d", config->autoboot_timeout_sec);
634         screen->widgets.timeout_l = widget_new_label(set, 0, 0, _("Timeout:"));
635         screen->widgets.timeout_f = widget_new_textbox(set, 0, 0, 5, str);
636         screen->widgets.timeout_help_l = widget_new_label(set, 0, 0,
637                                         _("seconds"));
638
639         widget_textbox_set_fixed_size(screen->widgets.timeout_f);
640         widget_textbox_set_validator_integer(screen->widgets.timeout_f, 0, 999);
641
642         screen->widgets.network_l = widget_new_label(set, 0, 0, _("Network:"));
643         screen->widgets.network_f = widget_new_select(set, 0, 0, 50);
644
645         widget_select_add_option(screen->widgets.network_f,
646                                         NET_CONF_TYPE_DHCP_ALL,
647                                         _("DHCP on all active interfaces"),
648                                         type == NET_CONF_TYPE_DHCP_ALL);
649         widget_select_add_option(screen->widgets.network_f,
650                                         NET_CONF_TYPE_DHCP_ONE,
651                                         _("DHCP on a specific interface"),
652                                         type == NET_CONF_TYPE_DHCP_ONE);
653         widget_select_add_option(screen->widgets.network_f,
654                                         NET_CONF_TYPE_STATIC,
655                                         _("Static IP configuration"),
656                                         type == NET_CONF_TYPE_STATIC);
657
658         widget_select_on_change(screen->widgets.network_f,
659                         config_screen_network_change, screen);
660
661         screen->widgets.iface_l = widget_new_label(set, 0, 0, _("Device:"));
662         screen->widgets.iface_f = widget_new_select(set, 0, 0, 50);
663
664         for (i = 0; i < sysinfo->n_interfaces; i++) {
665                 struct interface_info *info = sysinfo->interfaces[i];
666                 char str[50], mac[20];
667                 bool is_default;
668
669                 is_default = ifcfg && !memcmp(ifcfg->hwaddr, info->hwaddr,
670                                         sizeof(ifcfg->hwaddr));
671
672                 mac_str(info->hwaddr, info->hwaddr_size, mac, sizeof(mac));
673                 snprintf(str, sizeof(str), "%s [%s, %s]", info->name, mac,
674                                 info->link ? _("link up") : _("link down"));
675
676                 widget_select_add_option(screen->widgets.iface_f,
677                                                 i, str, is_default);
678         }
679
680         gw = ip = mask = NULL;
681         if (ifcfg && ifcfg->method == CONFIG_METHOD_STATIC) {
682                 char *sep;
683
684                 str = talloc_strdup(screen, ifcfg->static_config.address);
685                 sep = strchr(str, '/');
686                 ip = str;
687
688                 if (sep) {
689                         *sep = '\0';
690                         mask = sep + 1;
691                 }
692                 gw = ifcfg->static_config.gateway;
693         }
694
695         screen->widgets.ip_addr_l = widget_new_label(set, 0, 0, _("IP/mask:"));
696         screen->widgets.ip_addr_f = widget_new_textbox(set, 0, 0, 16, ip);
697         screen->widgets.ip_mask_l = widget_new_label(set, 0, 0, "/");
698         screen->widgets.ip_mask_f = widget_new_textbox(set, 0, 0, 3, mask);
699         screen->widgets.ip_addr_mask_help_l =
700                 widget_new_label(set, 0, 0, _("(eg. 192.168.0.10 / 24)"));
701
702         widget_textbox_set_fixed_size(screen->widgets.ip_addr_f);
703         widget_textbox_set_fixed_size(screen->widgets.ip_mask_f);
704         widget_textbox_set_validator_ipv4(screen->widgets.ip_addr_f);
705         widget_textbox_set_validator_integer(screen->widgets.ip_mask_f, 1, 31);
706
707         screen->widgets.gateway_l = widget_new_label(set, 0, 0, _("Gateway:"));
708         screen->widgets.gateway_f = widget_new_textbox(set, 0, 0, 16, gw);
709         screen->widgets.gateway_help_l =
710                 widget_new_label(set, 0, 0, _("(eg. 192.168.0.1)"));
711
712         widget_textbox_set_fixed_size(screen->widgets.gateway_f);
713         widget_textbox_set_validator_ipv4(screen->widgets.gateway_f);
714
715         str = talloc_strdup(screen, "");
716         for (i = 0; i < config->network.n_dns_servers; i++) {
717                 str = talloc_asprintf_append(str, "%s%s",
718                                 (i == 0) ? "" : " ",
719                                 config->network.dns_servers[i]);
720         }
721
722         screen->widgets.dns_l = widget_new_label(set, 0, 0,
723                                         _("DNS Server(s):"));
724         screen->widgets.dns_f = widget_new_textbox(set, 0, 0, 32, str);
725         screen->widgets.dns_help_l =
726                 widget_new_label(set, 0, 0, _("(eg. 192.168.0.2)"));
727
728         widget_textbox_set_validator_ipv4_multi(screen->widgets.dns_f);
729
730         screen->widgets.dns_dhcp_help_l = widget_new_label(set, 0, 0,
731                         _("(if not provided by DHCP server)"));
732
733         screen->widgets.ok_b = widget_new_button(set, 0, 0, 6, _("OK"),
734                         ok_click, screen);
735         screen->widgets.help_b = widget_new_button(set, 0, 0, 6, _("Help"),
736                         help_click, screen);
737         screen->widgets.cancel_b = widget_new_button(set, 0, 0, 6, _("Cancel"),
738                         cancel_click, screen);
739 }
740
741 static void config_screen_widget_focus(struct nc_widget *widget, void *arg)
742 {
743         struct config_screen *screen = arg;
744         int w_y, s_max;
745
746         w_y = widget_y(widget) + widget_focus_y(widget);
747         s_max = getmaxy(screen->scr.sub_ncw) - 1;
748
749         if (w_y < screen->scroll_y)
750                 screen->scroll_y = w_y;
751
752         else if (w_y + screen->scroll_y + 1 > s_max)
753                 screen->scroll_y = 1 + w_y - s_max;
754
755         else
756                 return;
757
758         pad_refresh(screen);
759 }
760
761 static void config_screen_draw(struct config_screen *screen,
762                 const struct config *config,
763                 const struct system_info *sysinfo)
764 {
765         bool repost = false;
766         int height;
767
768         /* The size of the pad we'll need depends on the number of interfaces.
769          *
770          * We use N_FIELDS (which is quite conservative, as some fields share
771          * a line) as a base, then add 3 (as the network select field is
772          * takes 3 lines), and n_interfaces (as the network interface field
773          * has n_interfaces lines).
774          */
775         height = N_FIELDS + 3;
776         if (sysinfo)
777                 height += sysinfo->n_interfaces;
778         if (!screen->pad || getmaxy(screen->pad) < height) {
779                 if (screen->pad)
780                         delwin(screen->pad);
781                 screen->pad = newpad(height, COLS);
782         }
783
784         if (screen->widgetset) {
785                 widgetset_unpost(screen->widgetset);
786                 talloc_free(screen->widgetset);
787                 repost = true;
788         }
789
790         screen->widgetset = widgetset_create(screen, screen->scr.main_ncw,
791                         screen->pad);
792         widgetset_set_widget_focus(screen->widgetset,
793                         config_screen_widget_focus, screen);
794
795         if (!config || !sysinfo) {
796                 config_screen_setup_empty(screen);
797         } else {
798                 screen->net_conf_type = find_net_conf_type(config);
799                 if (!config->autoboot_enabled)
800                         screen->autoboot_type = AUTOBOOT_DISABLED;
801                 else
802                         screen->autoboot_type = config->boot_device ?
803                                         AUTOBOOT_ONE : AUTOBOOT_ANY;
804
805                 config_screen_setup_widgets(screen, config, sysinfo);
806                 config_screen_layout_widgets(screen);
807         }
808
809         if (repost)
810                 widgetset_post(screen->widgetset);
811 }
812
813 void config_screen_update(struct config_screen *screen,
814                 const struct config *config,
815                 const struct system_info *sysinfo)
816 {
817         config_screen_draw(screen, config, sysinfo);
818         pad_refresh(screen);
819 }
820
821 static int config_screen_destroy(void *arg)
822 {
823         struct config_screen *screen = arg;
824         if (screen->pad)
825                 delwin(screen->pad);
826         return 0;
827 }
828
829 struct config_screen *config_screen_init(struct cui *cui,
830                 const struct config *config,
831                 const struct system_info *sysinfo,
832                 void (*on_exit)(struct cui *))
833 {
834         struct config_screen *screen;
835
836         screen = talloc_zero(cui, struct config_screen);
837         talloc_set_destructor(screen, config_screen_destroy);
838         nc_scr_init(&screen->scr, pb_config_screen_sig, 0,
839                         cui, config_screen_process_key,
840                         config_screen_post, config_screen_unpost,
841                         config_screen_resize);
842
843         screen->cui = cui;
844         screen->on_exit = on_exit;
845         screen->label_x = 2;
846         screen->field_x = 17;
847
848         screen->scr.frame.ltitle = talloc_strdup(screen,
849                         _("Petitboot System Configuration"));
850         screen->scr.frame.rtitle = NULL;
851         screen->scr.frame.help = talloc_strdup(screen,
852                         _("tab=next, shift+tab=previous, x=exit, h=help"));
853         nc_scr_frame_draw(&screen->scr);
854
855         scrollok(screen->scr.sub_ncw, true);
856
857         config_screen_draw(screen, config, sysinfo);
858
859         return screen;
860 }