]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/nc-cui.c
ui/common: Add discover_client_enumerate to re-add device data
[petitboot] / ui / ncurses / nc-cui.c
1 /*
2  *  Copyright (C) 2009 Sony Computer Entertainment Inc.
3  *  Copyright 2009 Sony Corp.
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; version 2 of the License.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #if defined(HAVE_CONFIG_H)
20 #include "config.h"
21 #endif
22
23 #include <assert.h>
24 #include <errno.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <sys/ioctl.h>
28
29 #include "log/log.h"
30 #include "pb-protocol/pb-protocol.h"
31 #include "talloc/talloc.h"
32 #include "waiter/waiter.h"
33 #include "process/process.h"
34 #include "i18n/i18n.h"
35 #include "ui/common/discover-client.h"
36 #include "ui/common/ui-system.h"
37 #include "nc-cui.h"
38 #include "nc-boot-editor.h"
39 #include "nc-config.h"
40 #include "nc-sysinfo.h"
41 #include "nc-helpscreen.h"
42
43 extern const struct help_text main_menu_help_text;
44
45 static void cui_start(void)
46 {
47         initscr();                      /* Initialize ncurses. */
48         cbreak();                       /* Disable line buffering. */
49         noecho();                       /* Disable getch() echo. */
50         keypad(stdscr, TRUE);           /* Enable num keypad keys. */
51         nonl();                         /* Disable new-line translation. */
52         intrflush(stdscr, FALSE);       /* Disable interrupt flush. */
53         curs_set(0);                    /* Make cursor invisible */
54         nodelay(stdscr, TRUE);          /* Enable non-blocking getch() */
55
56         /* We may be operating with an incorrect $TERM type; in this case
57          * the keymappings will be slightly broken. We want at least
58          * backspace to work though, so we'll define both DEL and ^H to
59          * map to backspace */
60         define_key("\x7f", KEY_BACKSPACE);
61         define_key("\x08", KEY_BACKSPACE);
62
63         /* we need backtab too, for form navigation. vt220 doesn't include
64          * this (kcbt), but we don't want to require a full linux/xterm termcap
65          */
66         define_key("\x1b[Z", KEY_BTAB);
67
68         while (getch() != ERR)          /* flush stdin */
69                 (void)0;
70 }
71
72 static void cui_atexit(void)
73 {
74         clear();
75         refresh();
76         endwin();
77 }
78
79 /**
80  * cui_abort - Signal the main cui program loop to exit.
81  *
82  * Sets cui.abort, which causes the cui_run() routine to return.
83  */
84
85 void cui_abort(struct cui *cui)
86 {
87         pb_log("%s: exiting\n", __func__);
88         cui->abort = 1;
89 }
90
91 /**
92  * cui_resize - Signal the main cui program loop to resize
93  *
94  * Called at SIGWINCH.
95  */
96
97 void cui_resize(struct cui *cui)
98 {
99         pb_debug("%s: resizing\n", __func__);
100         cui->resize = 1;
101 }
102
103 /**
104  * cui_on_exit - A generic main menu exit callback.
105  */
106
107 void cui_on_exit(struct pmenu *menu)
108 {
109         cui_abort(cui_from_pmenu(menu));
110 }
111
112 /**
113  * cui_run_cmd - A generic cb to run the supplied command.
114  */
115
116 int cui_run_cmd(struct pmenu_item *item)
117 {
118         int result;
119         struct cui *cui = cui_from_item(item);
120         const char **cmd_argv = item->data;
121
122         nc_scr_status_printf(cui->current, _("Running %s..."), cmd_argv[0]);
123
124         def_prog_mode();
125
126         result = process_run_simple_argv(item, cmd_argv);
127
128         reset_prog_mode();
129         redrawwin(cui->current->main_ncw);
130
131         if (result) {
132                 pb_log("%s: failed: '%s'\n", __func__, cmd_argv[0]);
133                 nc_scr_status_printf(cui->current, _("Failed: %s"),
134                                 cmd_argv[0]);
135         }
136
137         return result;
138 }
139
140 /**
141  * cui_boot - A generic cb to run kexec.
142  */
143
144 static int cui_boot(struct pmenu_item *item)
145 {
146         int result;
147         struct cui *cui = cui_from_item(item);
148         struct cui_opt_data *cod = cod_from_item(item);
149
150         assert(cui->current == &cui->main->scr);
151
152         pb_debug("%s: %s\n", __func__, cod->name);
153
154         nc_scr_status_printf(cui->current, _("Booting %s..."), cod->name);
155
156         result = discover_client_boot(cui->client, NULL, cod->opt, cod->bd);
157
158         if (result) {
159                 nc_scr_status_printf(cui->current,
160                                 _("Failed: boot %s"), cod->bd->image);
161         }
162
163         return 0;
164 }
165
166 static void cui_boot_editor_on_exit(struct cui *cui,
167                 struct pmenu_item *item,
168                 struct pb_boot_data *bd)
169 {
170         struct pmenu *menu = cui->main;
171         struct cui_opt_data *cod;
172         static int user_idx = 0;
173
174         /* Was the edit cancelled? */
175         if (!bd) {
176                 cui_set_current(cui, &cui->main->scr);
177                 talloc_free(cui->boot_editor);
178                 cui->boot_editor = NULL;
179                 return;
180         }
181
182         /* Is this was a new item, we'll need to update the menu */
183         if (!item) {
184                 int insert_pt;
185
186                 cod = talloc_zero(NULL, struct cui_opt_data);
187                 cod->name = talloc_asprintf(cod, _("User item %u"), ++user_idx);
188
189                 item = pmenu_item_create(menu, cod->name);
190                 if (!item) {
191                         talloc_free(cod);
192                         goto out;
193                 }
194
195                 item->on_edit = cui_item_edit;
196                 item->on_execute = cui_boot;
197                 item->data = cod;
198
199                 talloc_steal(item, cod);
200
201                 /* Detach the items array. */
202                 set_menu_items(menu->ncm, NULL);
203
204                 /* Insert new item at insert_pt. */
205                 insert_pt = pmenu_grow(menu, 1);
206                 pmenu_item_insert(menu, item, insert_pt);
207
208                 /* Re-attach the items array. */
209                 set_menu_items(menu->ncm, menu->items);
210                 nc_scr_post(&menu->scr);
211         } else {
212                 cod = item->data;
213         }
214
215         cod->bd = talloc_steal(cod, bd);
216
217         set_current_item(item->pmenu->ncm, item->nci);
218 out:
219         cui_set_current(cui, &cui->main->scr);
220         talloc_free(cui->boot_editor);
221         cui->boot_editor = NULL;
222 }
223
224 void cui_item_edit(struct pmenu_item *item)
225 {
226         struct cui *cui = cui_from_item(item);
227         cui->boot_editor = boot_editor_init(cui, item, cui->sysinfo,
228                                         cui_boot_editor_on_exit);
229         cui_set_current(cui, boot_editor_scr(cui->boot_editor));
230 }
231
232 void cui_item_new(struct pmenu *menu)
233 {
234         struct cui *cui = cui_from_pmenu(menu);
235         cui->boot_editor = boot_editor_init(cui, NULL, cui->sysinfo,
236                                         cui_boot_editor_on_exit);
237         cui_set_current(cui, boot_editor_scr(cui->boot_editor));
238 }
239
240 static void cui_sysinfo_exit(struct cui *cui)
241 {
242         cui_set_current(cui, &cui->main->scr);
243         talloc_free(cui->sysinfo_screen);
244         cui->sysinfo_screen = NULL;
245 }
246
247 void cui_show_sysinfo(struct cui *cui)
248 {
249         cui->sysinfo_screen = sysinfo_screen_init(cui, cui->sysinfo,
250                         cui_sysinfo_exit);
251         cui_set_current(cui, sysinfo_screen_scr(cui->sysinfo_screen));
252 }
253
254 static void cui_config_exit(struct cui *cui)
255 {
256         cui_set_current(cui, &cui->main->scr);
257         talloc_free(cui->config_screen);
258         cui->config_screen = NULL;
259 }
260
261 void cui_show_config(struct cui *cui)
262 {
263         cui->config_screen = config_screen_init(cui, cui->config,
264                         cui->sysinfo, cui_config_exit);
265         cui_set_current(cui, config_screen_scr(cui->config_screen));
266 }
267
268 static void cui_help_exit(struct cui *cui)
269 {
270         cui_set_current(cui, help_screen_return_scr(cui->help_screen));
271         talloc_free(cui->help_screen);
272         cui->help_screen = NULL;
273 }
274
275 void cui_show_help(struct cui *cui, const char *title,
276                 const struct help_text *text)
277 {
278         if (!cui->current)
279                 return;
280
281         if (cui->help_screen)
282                 return;
283
284         cui->help_screen = help_screen_init(cui, cui->current,
285                         title, text, cui_help_exit);
286
287         if (cui->help_screen)
288                 cui_set_current(cui, help_screen_scr(cui->help_screen));
289 }
290
291 /**
292  * cui_set_current - Set the currently active screen and redraw it.
293  */
294
295 struct nc_scr *cui_set_current(struct cui *cui, struct nc_scr *scr)
296 {
297         struct nc_scr *old;
298
299         DBGS("%p -> %p\n", cui->current, scr);
300
301         assert(cui->current != scr);
302
303         old = cui->current;
304         nc_scr_unpost(old);
305
306         cui->current = scr;
307
308         nc_scr_post(cui->current);
309
310         return old;
311 }
312
313 static bool process_global_keys(struct cui *cui, int key)
314 {
315         switch (key) {
316         case 0xc:
317                 if (cui->current && cui->current->main_ncw)
318                         wrefresh(curscr);
319                 return true;
320         }
321         return false;
322 }
323
324 /**
325  * cui_process_key - Process input on stdin.
326  */
327
328 static int cui_process_key(void *arg)
329 {
330         struct cui *cui = cui_from_arg(arg);
331
332         assert(cui->current);
333
334         for (;;) {
335                 int c = getch();
336
337                 pb_debug("%s: got key %d\n", __func__, c);
338
339                 if (c == ERR)
340                         break;
341
342                 if (!cui->has_input) {
343                         pb_log("UI input received (key = %d), aborting "
344                                         "default boot\n", c);
345                         discover_client_cancel_default(cui->client);
346                         cui->has_input = true;
347                 }
348
349                 if (process_global_keys(cui, c))
350                         continue;
351
352                 cui->current->process_key(cui->current, c);
353         }
354
355         return 0;
356 }
357
358 /**
359  * cui_process_js - Process joystick events.
360  */
361
362 static int cui_process_js(void *arg)
363 {
364         struct cui *cui = cui_from_arg(arg);
365         int c;
366
367         c = pjs_process_event(cui->pjs);
368
369         if (c) {
370                 ungetch(c);
371                 cui_process_key(arg);
372         }
373
374         return 0;
375 }
376
377 /**
378  * cui_handle_resize - Handle the term resize.
379  */
380
381 static void cui_handle_resize(struct cui *cui)
382 {
383         struct winsize ws;
384
385         if (ioctl(1, TIOCGWINSZ, &ws) == -1) {
386                 pb_log("%s: ioctl failed: %s\n", __func__, strerror(errno));
387                 return;
388         }
389
390         pb_debug("%s: {%u,%u}\n", __func__, ws.ws_row, ws.ws_col);
391
392         wclear(cui->current->main_ncw);
393         resize_term(ws.ws_row, ws.ws_col);
394         cui->current->resize(cui->current);
395
396         /* For some reason this makes ncurses redraw the screen */
397         getch();
398         redrawwin(cui->current->main_ncw);
399         wrefresh(cui->current->main_ncw);
400 }
401
402 /**
403  * cui_device_add - Client device_add callback.
404  *
405  * Creates menu_items for all the device boot_options and inserts those
406  * menu_items into the main menu.  Redraws the main menu if it is active.
407  */
408
409 static int cui_boot_option_add(struct device *dev, struct boot_option *opt,
410                 void *arg)
411 {
412         struct pmenu_item *i, *dev_hdr = NULL;
413         struct cui *cui = cui_from_arg(arg);
414         struct cui_opt_data *cod;
415         const char *tab = "  ";
416         unsigned int insert_pt;
417         int result, rows, cols;
418         ITEM *selected;
419         char *name;
420
421         pb_debug("%s: %p %s\n", __func__, opt, opt->id);
422
423         selected = current_item(cui->main->ncm);
424         menu_format(cui->main->ncm, &rows, &cols);
425
426         if (cui->current == &cui->main->scr)
427                 nc_scr_unpost(cui->current);
428
429         /* Check if the boot device is new */
430         dev_hdr = pmenu_find_device(cui->main, dev, opt);
431
432         /* All actual boot entries are 'tabbed' across */
433         name = talloc_asprintf(cui->main, "%s%s",
434                         tab, opt->name ? : "Unknown Name");
435
436         /* Save the item in opt->ui_info for cui_device_remove() */
437         opt->ui_info = i = pmenu_item_create(cui->main, name);
438         talloc_free(name);
439         if (!i)
440                 return -1;
441
442         i->on_edit = cui_item_edit;
443         i->on_execute = cui_boot;
444         i->data = cod = talloc(i, struct cui_opt_data);
445
446         cod->opt = opt;
447         cod->dev = dev;
448         cod->opt_hash = pb_opt_hash(dev, opt);
449         cod->name = opt->name;
450         cod->bd = talloc(i, struct pb_boot_data);
451
452         cod->bd->image = talloc_strdup(cod->bd, opt->boot_image_file);
453         cod->bd->initrd = talloc_strdup(cod->bd, opt->initrd_file);
454         cod->bd->dtb = talloc_strdup(cod->bd, opt->dtb_file);
455         cod->bd->args = talloc_strdup(cod->bd, opt->boot_args);
456
457         /* This disconnects items array from menu. */
458         result = set_menu_items(cui->main->ncm, NULL);
459
460         if (result)
461                 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
462
463         /* Insert new items at insert_pt. */
464         if (dev_hdr) {
465                 insert_pt = pmenu_grow(cui->main, 2);
466                 pmenu_item_insert(cui->main, dev_hdr, insert_pt);
467                 pb_log("%s: adding new device hierarchy %s\n",
468                         __func__,opt->device_id);
469                 pmenu_item_insert(cui->main, i, insert_pt+1);
470         } else {
471                 insert_pt = pmenu_grow(cui->main, 1);
472                 pmenu_item_add(cui->main, i, insert_pt);
473         }
474
475         pb_log("%s: adding opt '%s'\n", __func__, cod->name);
476         pb_log("   image  '%s'\n", cod->bd->image);
477         pb_log("   initrd '%s'\n", cod->bd->initrd);
478         pb_log("   args   '%s'\n", cod->bd->args);
479
480         /* Re-attach the items array. */
481         result = set_menu_items(cui->main->ncm, cui->main->items);
482
483         if (result)
484                 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
485
486         if (0) {
487                 pb_log("%s\n", __func__);
488                 pmenu_dump_items(cui->main->items,
489                         item_count(cui->main->ncm) + 1);
490         }
491
492         if (!item_visible(selected)) {
493                 int idx, top;
494
495                 top = top_row(cui->main->ncm);
496                 idx = item_index(selected);
497
498                 /* If our index is above the current top row, align
499                  * us to the new top. Otherwise, align us to the new
500                  * bottom */
501                 top = top < idx ? idx - rows : idx;
502
503                 set_top_row(cui->main->ncm, top);
504                 set_current_item(cui->main->ncm, selected);
505         }
506
507         if (cui->current == &cui->main->scr)
508                 nc_scr_post(cui->current);
509
510         return 0;
511 }
512
513 /**
514  * cui_device_remove - Client device remove callback.
515  *
516  * Removes all the menu_items for the device from the main menu and redraws the
517  * main menu if it is active.
518  */
519
520 static void cui_device_remove(struct device *dev, void *arg)
521 {
522         struct cui *cui = cui_from_arg(arg);
523         struct boot_option *opt;
524         unsigned int i;
525         int result;
526
527         pb_log("%s: %p %s\n", __func__, dev, dev->id);
528
529         if (cui->current == &cui->main->scr)
530                 nc_scr_unpost(cui->current);
531
532         /* This disconnects items array from menu. */
533
534         result = set_menu_items(cui->main->ncm, NULL);
535
536         if (result)
537                 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
538
539         list_for_each_entry(&dev->boot_options, opt, list) {
540                 struct pmenu_item *item = pmenu_item_from_arg(opt->ui_info);
541
542                 assert(pb_protocol_device_cmp(dev, cod_from_item(item)->dev));
543                 pmenu_remove(cui->main, item);
544         }
545
546         /* Manually remove remaining device hierarchy item */
547         for (i=0; i < cui->main->item_count; i++) {
548                 struct pmenu_item *item = item_userptr(cui->main->items[i]);
549                 if (!item || !item->data )
550                         continue;
551
552                 struct cui_opt_data *data = item->data;
553                 if (data && data->dev && data->dev == dev)
554                         pmenu_remove(cui->main,item);
555         }
556
557         /* Re-attach the items array. */
558
559         result = set_menu_items(cui->main->ncm, cui->main->items);
560
561         if (result)
562                 pb_log("%s: set_menu_items failed: %d\n", __func__, result);
563
564         if (0) {
565                 pb_log("%s\n", __func__);
566                 pmenu_dump_items(cui->main->items,
567                         item_count(cui->main->ncm) + 1);
568         }
569
570         if (cui->current == &cui->main->scr)
571                 nc_scr_post(cui->current);
572 }
573
574 static void cui_update_status(struct boot_status *status, void *arg)
575 {
576         struct cui *cui = cui_from_arg(arg);
577
578         nc_scr_status_printf(cui->current,
579                         "%s: %s",
580                         status->type == BOOT_STATUS_ERROR ?
581                                 _("Error") : _("Info"),
582                         status->message);
583
584 }
585
586 static void cui_update_mm_title(struct cui *cui)
587 {
588         struct nc_frame *frame = &cui->main->scr.frame;
589
590         talloc_free(frame->rtitle);
591
592         frame->rtitle = talloc_strdup(cui->main, cui->sysinfo->type);
593         if (cui->sysinfo->identifier)
594                 frame->rtitle = talloc_asprintf_append(frame->rtitle,
595                                 " %s", cui->sysinfo->identifier);
596
597         if (cui->current == &cui->main->scr)
598                 nc_scr_post(cui->current);
599 }
600
601 static void cui_update_sysinfo(struct system_info *sysinfo, void *arg)
602 {
603         struct cui *cui = cui_from_arg(arg);
604         cui->sysinfo = talloc_steal(cui, sysinfo);
605
606         /* if we're currently displaying the system info screen, inform it
607          * of the updated information. */
608         if (cui->sysinfo_screen)
609                 sysinfo_screen_update(cui->sysinfo_screen, sysinfo);
610
611         /* ... and do the same with the config screen... */
612         if (cui->config_screen)
613                 config_screen_update(cui->config_screen, cui->config, sysinfo);
614
615         /* ... and the boot editor. */
616         if (cui->boot_editor)
617                 boot_editor_update(cui->boot_editor, sysinfo);
618
619         cui_update_mm_title(cui);
620 }
621
622 static void cui_update_config(struct config *config, void *arg)
623 {
624         struct cui *cui = cui_from_arg(arg);
625         cui->config = talloc_steal(cui, config);
626
627         if (cui->config_screen)
628                 config_screen_update(cui->config_screen, config, cui->sysinfo);
629
630         if (config->safe_mode)
631                 nc_scr_status_printf(cui->current,
632                                 _("SAFE MODE: select '%s' to continue"),
633                                 _("Rescan devices"));
634 }
635
636 int cui_send_config(struct cui *cui, struct config *config)
637 {
638         return discover_client_send_config(cui->client, config);
639 }
640
641 void cui_send_reinit(struct cui *cui)
642 {
643         discover_client_send_reinit(cui->client);
644 }
645
646 static int menu_sysinfo_execute(struct pmenu_item *item)
647 {
648         cui_show_sysinfo(cui_from_item(item));
649         return 0;
650 }
651
652 static int menu_config_execute(struct pmenu_item *item)
653 {
654         cui_show_config(cui_from_item(item));
655         return 0;
656 }
657
658 static int menu_reinit_execute(struct pmenu_item *item)
659 {
660         cui_send_reinit(cui_from_item(item));
661         return 0;
662 }
663
664 /**
665  * pb_mm_init - Setup the main menu instance.
666  */
667 static struct pmenu *main_menu_init(struct cui *cui)
668 {
669         struct pmenu_item *i;
670         struct pmenu *m;
671         int result;
672
673         m = pmenu_init(cui, 5, cui_on_exit);
674         if (!m) {
675                 pb_log("%s: failed\n", __func__);
676                 return NULL;
677         }
678
679         m->on_new = cui_item_new;
680
681         m->scr.frame.ltitle = talloc_asprintf(m,
682                 "Petitboot (" PACKAGE_VERSION ")");
683         m->scr.frame.rtitle = NULL;
684         m->scr.frame.help = talloc_strdup(m,
685                 _("Enter=accept, e=edit, n=new, x=exit, h=help"));
686         m->scr.frame.status = talloc_strdup(m, _("Welcome to Petitboot"));
687
688         /* add a separator */
689         i = pmenu_item_create(m, " ");
690         item_opts_off(i->nci, O_SELECTABLE);
691         pmenu_item_insert(m, i, 0);
692
693         /* add system items */
694         i = pmenu_item_create(m, _("System information"));
695         i->on_execute = menu_sysinfo_execute;
696         pmenu_item_insert(m, i, 1);
697
698         i = pmenu_item_create(m, _("System configuration"));
699         i->on_execute = menu_config_execute;
700         pmenu_item_insert(m, i, 2);
701
702         i = pmenu_item_create(m, _("Rescan devices"));
703         i->on_execute = menu_reinit_execute;
704         pmenu_item_insert(m, i, 3);
705
706         i = pmenu_item_create(m, _("Exit to shell"));
707         i->on_execute = pmenu_exit_cb;
708         pmenu_item_insert(m, i, 4);
709
710         result = pmenu_setup(m);
711
712         if (result) {
713                 pb_log("%s:%d: pmenu_setup failed: %s\n", __func__, __LINE__,
714                         strerror(errno));
715                 goto fail_setup;
716         }
717
718         m->help_title = _("main menu");
719         m->help_text = &main_menu_help_text;
720
721         menu_opts_off(m->ncm, O_SHOWDESC);
722         set_menu_mark(m->ncm, " *");
723         set_current_item(m->ncm, i->nci);
724
725         return m;
726
727 fail_setup:
728         talloc_free(m);
729         return NULL;
730 }
731
732 static struct discover_client_ops cui_client_ops = {
733         .device_add = NULL,
734         .boot_option_add = cui_boot_option_add,
735         .device_remove = cui_device_remove,
736         .update_status = cui_update_status,
737         .update_sysinfo = cui_update_sysinfo,
738         .update_config = cui_update_config,
739 };
740
741 /**
742  * cui_init - Setup the cui instance.
743  * @platform_info: A value for the struct cui platform_info member.
744  *
745  * Returns a pointer to a struct cui on success, or NULL on error.
746  *
747  * Allocates the cui instance, sets up the client and stdin waiters, and
748  * sets up the ncurses menu screen.
749  */
750
751 struct cui *cui_init(void* platform_info,
752         int (*js_map)(const struct js_event *e), int start_deamon)
753 {
754         struct cui *cui;
755         unsigned int i;
756
757         cui = talloc_zero(NULL, struct cui);
758         if (!cui) {
759                 pb_log("%s: alloc cui failed.\n", __func__);
760                 fprintf(stderr, _("%s: alloc cui failed.\n"), __func__);
761                 goto fail_alloc;
762         }
763
764         cui->c_sig = pb_cui_sig;
765         cui->platform_info = platform_info;
766         cui->waitset = waitset_create(cui);
767
768         process_init(cui, cui->waitset, false);
769
770         /* Loop here for scripts that just started the server. */
771
772 retry_start:
773         for (i = start_deamon ? 2 : 10; i; i--) {
774                 cui->client = discover_client_init(cui->waitset,
775                                 &cui_client_ops, cui);
776                 if (cui->client || !i)
777                         break;
778                 pb_log("%s: waiting for server %d\n", __func__, i);
779                 sleep(1);
780         }
781
782         if (!cui->client && start_deamon) {
783                 int result;
784
785                 start_deamon = 0;
786
787                 result = pb_start_daemon(cui);
788
789                 if (!result)
790                         goto retry_start;
791
792                 pb_log("%s: discover_client_init failed.\n", __func__);
793                 fprintf(stderr, _("%s: error: discover_client_init failed.\n"),
794                         __func__);
795                 fprintf(stderr, _("could not start pb-discover, the petitboot "
796                         "daemon.\n"));
797                 goto fail_client_init;
798         }
799
800         if (!cui->client) {
801                 pb_log("%s: discover_client_init failed.\n", __func__);
802                 fprintf(stderr, _("%s: error: discover_client_init failed.\n"),
803                         __func__);
804                 fprintf(stderr, _("check that pb-discover, "
805                         "the petitboot daemon is running.\n"));
806                 goto fail_client_init;
807         }
808
809         atexit(cui_atexit);
810         talloc_steal(cui, cui->client);
811         cui_start();
812
813         cui->main = main_menu_init(cui);
814         if (!cui->main)
815                 goto fail_client_init;
816
817         waiter_register_io(cui->waitset, STDIN_FILENO, WAIT_IN,
818                         cui_process_key, cui);
819
820         if (js_map) {
821
822                 cui->pjs = pjs_init(cui, js_map);
823
824                 if (cui->pjs)
825                         waiter_register_io(cui->waitset, pjs_get_fd(cui->pjs),
826                                         WAIT_IN, cui_process_js, cui);
827         }
828
829         return cui;
830
831 fail_client_init:
832         talloc_free(cui);
833 fail_alloc:
834         return NULL;
835 }
836
837 /**
838  * cui_run - The main cui program loop.
839  * @cui: The cui instance.
840  * @main: The menu to use as the main menu.
841  *
842  * Runs the cui engine.  Does not return until indicated to do so by some
843  * user action, or an error occurs.  Frees the cui object on return.
844  * Returns 0 on success (return to shell), -1 on error (should restart).
845  */
846
847 int cui_run(struct cui *cui)
848 {
849         assert(main);
850
851         cui->current = &cui->main->scr;
852         cui->default_item = 0;
853
854         nc_scr_post(cui->current);
855
856         while (1) {
857                 int result = waiter_poll(cui->waitset);
858
859                 if (result < 0) {
860                         pb_log("%s: poll: %s\n", __func__, strerror(errno));
861                         break;
862                 }
863
864                 if (cui->abort)
865                         break;
866
867                 while (cui->resize) {
868                         cui->resize = 0;
869                         cui_handle_resize(cui);
870                 }
871         }
872
873         cui_atexit();
874
875         return cui->abort ? 0 : -1;
876 }