]> git.ozlabs.org Git - petitboot/blobdiff - ui/ncurses/nc-cui.c
ncurses: Fix bad strncmp
[petitboot] / ui / ncurses / nc-cui.c
index 8060510da0cd29010b33af506cc3da399dab3a40..20a90483cd07accc318546d9fc26aab48c9f72dc 100644 (file)
@@ -24,6 +24,7 @@
 #include <ctype.h>
 #include <errno.h>
 #include <stdlib.h>
+#include <locale.h>
 #include <string.h>
 #include <sys/ioctl.h>
 #include <sys/reboot.h>
@@ -56,6 +57,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;
@@ -98,8 +101,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 */
@@ -156,6 +169,8 @@ void cui_on_exit(struct 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\";\
@@ -949,8 +964,8 @@ fallback:
         * If this option was faked above move the context under
         * the item so it is cleaned up later in cui_plugins_remove().
         */
-       if (strncmp(cod->opt->id, "dummy", strlen("dummy") == 0 &&
-                               cod->dev->type == DEVICE_TYPE_UNKNOWN)) {
+       if (strcmp(cod->opt->id, "dummy") == 0 &&
+                       cod->dev->type == DEVICE_TYPE_UNKNOWN) {
                talloc_steal(item, cod->dev);
                talloc_steal(item, cod->opt);
        }
@@ -1228,6 +1243,14 @@ static struct pmenu *main_menu_init(struct cui *cui)
                return NULL;
        }
 
+       m->n_hot_keys = 1;
+       m->hot_keys = talloc_array(m, hot_key_fn, m->n_hot_keys);
+       if (!m->hot_keys) {
+               pb_log("%s: failed to allocate hot_keys\n", __func__);
+               talloc_free(m);
+               return NULL;
+       }
+       m->hot_keys[0] = pmenu_main_hot_keys;
        m->on_new = cui_item_new;
 
        m->scr.frame.ltitle = talloc_asprintf(m,
@@ -1311,9 +1334,8 @@ static struct pmenu *plugin_menu_init(struct cui *cui)
        int result;
 
        m = pmenu_init(cui, 2, cui_plugin_menu_exit);
-       m->on_new = cui_item_new;
        m->scr.frame.ltitle = talloc_asprintf(m, _("Petitboot Plugins"));
-       m->scr.frame.rtitle = talloc_asprintf(m, NULL);
+       m->scr.frame.rtitle = talloc_asprintf(m, "%s", "");
        m->scr.frame.help = talloc_strdup(m,
                _("Enter=install, e=details, x=exit, h=help"));
        m->scr.frame.status = talloc_asprintf(m,
@@ -1357,31 +1379,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.
  *
@@ -1530,6 +1527,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.
@@ -1542,9 +1574,8 @@ fail_alloc:
 
 int cui_run(struct cui *cui)
 {
-       pid_t pid;
-
-       assert(main);
+       assert(cui);
+       assert(cui->main);
 
        cui->current = &cui->main->scr;
        cui->default_item = 0;
@@ -1568,18 +1599,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;
 }