]> git.ozlabs.org Git - petitboot/blobdiff - ui/ncurses/nc-cui.c
ui/ncurses: Always cancel autoboot on exit
[petitboot] / ui / ncurses / nc-cui.c
index 46b783972dbda3a2b9d97621be8425759295620c..72a056d2c6f93707fd867170a9d21e723b772480 100644 (file)
@@ -21,6 +21,7 @@
 #endif
 
 #include <assert.h>
+#include <ctype.h>
 #include <errno.h>
 #include <stdlib.h>
 #include <string.h>
@@ -45,6 +46,7 @@
 #include "nc-statuslog.h"
 #include "nc-subset.h"
 #include "nc-plugin.h"
+#include "console-codes.h"
 
 extern const struct help_text main_menu_help_text;
 extern const struct help_text plugin_menu_help_text;
@@ -54,6 +56,8 @@ static bool cui_detached = false;
 static struct pmenu *main_menu_init(struct cui *cui);
 static struct pmenu *plugin_menu_init(struct cui *cui);
 
+static void cui_cancel_autoboot_on_exit(struct cui *cui);
+
 static bool lockdown_active(void)
 {
        bool lockdown = false;
@@ -96,8 +100,18 @@ static void cui_start(void)
        define_key("\x1b\x4f\x46", KEY_END);
        define_key("OH", KEY_HOME);
        define_key("OF", KEY_END);
+
+       /* Arrow keys in normal cursor mode */
        define_key("\x1b\x5b\x41", KEY_UP);
        define_key("\x1b\x5b\x42", KEY_DOWN);
+       define_key("\x1b\x5b\x43", KEY_RIGHT);
+       define_key("\x1b\x5b\x44", KEY_LEFT);
+       /* Arrow keys in "application" cursor mode */
+       define_key("\x1b\x4f\x41", KEY_UP);
+       define_key("\x1b\x4f\x42", KEY_DOWN);
+       define_key("\x1b\x4f\x43", KEY_RIGHT);
+       define_key("\x1b\x4f\x44", KEY_LEFT);
+
        define_key("\x1b\x5b\x33\x7e", KEY_DC);
 
        while (getch() != ERR)          /* flush stdin */
@@ -151,7 +165,32 @@ void cui_resize(struct cui *cui)
 
 void cui_on_exit(struct pmenu *menu)
 {
-       cui_abort(cui_from_pmenu(menu));
+       struct cui *cui = cui_from_pmenu(menu);
+       char *sh_cmd;
+
+       cui_cancel_autoboot_on_exit(cui);
+
+       sh_cmd = talloc_asprintf(cui,
+               "echo \"Exiting petitboot. Type 'exit' to return.\";\
+                echo \"You may run 'pb-sos' to gather diagnostic data\";\
+                %s", pb_system_apps.sh);
+
+       if (!sh_cmd) {
+               pb_log("Failed to allocate shell arguments\n");
+               return;
+       }
+
+       const char *argv[] = {
+               pb_system_apps.sh,
+               "-c",
+               sh_cmd,
+               NULL
+       };
+
+       cui_run_cmd(cui, argv);
+
+       nc_scr_status_printf(cui->current, _("Returned from shell"));
+       talloc_free(sh_cmd);
 }
 
 /**
@@ -173,6 +212,10 @@ int cui_run_cmd(struct cui *cui, const char **cmd_argv)
 
        nc_scr_status_printf(cui->current, _("Running %s..."), cmd_argv[0]);
 
+       nc_scr_unpost(cui->current);
+       clear();
+       refresh();
+
        def_prog_mode();
        endwin();
 
@@ -182,6 +225,7 @@ int cui_run_cmd(struct cui *cui, const char **cmd_argv)
        refresh();
 
        redrawwin(cui->current->main_ncw);
+       nc_scr_post(cui->current);
 
        if (result) {
                pb_log("%s: failed: '%s'\n", __func__, cmd_argv[0]);
@@ -496,6 +540,9 @@ static bool process_global_keys(struct cui *cui, int key)
 static int cui_process_key(void *arg)
 {
        struct cui *cui = cui_from_arg(arg);
+       unsigned int i;
+       char *sequence;
+       int grab;
 
        assert(cui->current);
 
@@ -507,6 +554,29 @@ static int cui_process_key(void *arg)
                if (c == ERR)
                        break;
 
+               if (c == 27) {
+                       /*
+                        * If this is a console code sequence try to parse it
+                        * and don't treat this as a key press.
+                        */
+                       grab = getch();
+                       if (grab != ERR && grab != 27) {
+                               ungetch(grab);
+                               pb_debug("%s: Caught unhandled command sequence\n",
+                                               __func__);
+                               sequence = handle_control_sequence(cui, c);
+                               pb_debug("Caught sequence ");
+                               if (sequence) {
+                                       pb_debug("(%zu): ", strlen(sequence));
+                                       for (i = 0; i < strlen(sequence); i++)
+                                               pb_debug("0%o ", sequence[i]);
+                                       pb_debug("\n");
+                               } else
+                                       pb_debug("(0): (none)\n");
+                               continue;
+                       }
+               }
+
                if (!cui->has_input) {
                        cui->has_input = true;
                        if (cui->client) {
@@ -901,7 +971,7 @@ fallback:
 
        talloc_free(cod->name);
        /* Name is still tabbed across */
-       cod->name = talloc_asprintf(cod, "  %s [installed]", opt->name);
+       cod->name = talloc_asprintf(cod, _("  %s [installed]"), opt->name);
 
        cod->pd->opt = opt;
        item->on_execute = NULL;
@@ -1268,7 +1338,7 @@ static struct pmenu *plugin_menu_init(struct cui *cui)
        item_opts_off(i->nci, O_SELECTABLE);
        pmenu_item_insert(m, i, 0);
 
-       i = pmenu_item_create(m, ("Return to Main Menu"));
+       i = pmenu_item_create(m, _("Return to Main Menu"));
        i->on_execute = pmenu_exit_cb;
        pmenu_item_insert(m, i, 1);
 
@@ -1301,31 +1371,6 @@ static struct discover_client_ops cui_client_ops = {
        .update_config = cui_update_config,
 };
 
-/* cui_server_wait_on_exit - On exit spin until the server is available.
- *
- * If the program exits before connecting to the server autoboot won't be
- * cancelled even though there has been keyboard activity. This function is
- * called by a child process which will spin until the server is connected and
- * told to cancel autoboot.
- *
- * Processes exiting from this function will not carry out the cui_atexit()
- * steps.
- */
-static void cui_server_wait_on_exit(struct cui *cui)
-{
-       cui_detached = true;
-
-       while (!cui->client) {
-               cui->client = discover_client_init(cui->waitset,
-                               &cui_client_ops, cui);
-               if (!cui->client)
-                       sleep(1);
-       }
-
-       talloc_steal(cui, cui->client);
-       discover_client_cancel_default(cui->client);
-}
-
 /* cui_server_wait - Connect to the discover server.
  * @arg: Pointer to the cui instance.
  *
@@ -1474,6 +1519,41 @@ fail_alloc:
        return NULL;
 }
 
+/**
+ * cui_cancel_autoboot_on_exit - On exit spin until the server is available.
+ *
+ * If the program exits before connecting to the server autoboot won't be
+ * cancelled even though there has been keyboard activity. A child is forked
+ * which will spin until the server is connected and told to cancel autoboot.
+ */
+static void cui_cancel_autoboot_on_exit(struct cui *cui)
+{
+       pid_t pid;
+
+       if (!cui->client) {
+               /* Fork a child to tell the server to cancel autoboot */
+               pid = fork();
+               if (!pid) {
+                       cui_detached = true;
+
+                       /* Loop until connection established */
+                       while (!cui->client) {
+                               cui->client = discover_client_init(cui->waitset,
+                                               &cui_client_ops, cui);
+                               if (!cui->client)
+                                       sleep(1);
+                       }
+
+                       talloc_steal(cui, cui->client);
+                       discover_client_cancel_default(cui->client);
+                       exit(EXIT_SUCCESS);
+               }
+               if (pid < 0)
+                       pb_log("Failed to fork child on exit: %m\n");
+       } else
+               discover_client_cancel_default(cui->client);
+}
+
 /**
  * cui_run - The main cui program loop.
  * @cui: The cui instance.
@@ -1486,8 +1566,6 @@ fail_alloc:
 
 int cui_run(struct cui *cui)
 {
-       pid_t pid;
-
        assert(main);
 
        cui->current = &cui->main->scr;
@@ -1512,18 +1590,9 @@ int cui_run(struct cui *cui)
                }
        }
 
-       cui_atexit();
+       cui_cancel_autoboot_on_exit(cui);
 
-       if (!cui->client) {
-               /* Fork a child to tell the server to cancel autoboot */
-               pid = fork();
-               if (!pid) {
-                       cui_server_wait_on_exit(cui);
-                       exit(EXIT_SUCCESS);
-               }
-               if (pid < 0)
-                       pb_log("Failed to fork child on exit: %m\n");
-       }
+       cui_atexit();
 
        return cui->abort ? 0 : -1;
 }