]> git.ozlabs.org Git - petitboot/blobdiff - ui/ncurses/generic-main.c
discover,ui: fix checks for debug option
[petitboot] / ui / ncurses / generic-main.c
index 983ba474917ab629a47b2c36b61398c31125452c..fe288ee3cad85f25628ab5ed0e45139b63803891 100644 (file)
 #include "config.h"
 #endif
 
-#define _GNU_SOURCE
+#include <assert.h>
 #include <errno.h>
 #include <getopt.h>
 #include <signal.h>
 #include <stdlib.h>
 #include <string.h>
+#include <limits.h>
 #include <sys/time.h>
 
 #include "log/log.h"
@@ -36,6 +37,8 @@
 #include "ui/common/discover-client.h"
 #include "nc-cui.h"
 
+extern const char *main_menu_help_text;
+
 static void print_version(void)
 {
        printf("petitboot-nc (" PACKAGE_NAME ") " PACKAGE_VERSION "\n");
@@ -45,8 +48,8 @@ static void print_usage(void)
 {
        print_version();
        printf(
-"Usage: petitboot-nc [-d, --dry-run] [-h, --help] [-l, --log log-file]\n"
-"                    [-s, --start-daemon] [-V, --version]\n");
+"Usage: petitboot-nc [-h, --help] [-l, --log log-file]\n"
+"                    [-s, --start-daemon] [-v, --verbose] [-V, --version]\n");
 }
 
 /**
@@ -60,10 +63,10 @@ enum opt_value {opt_undef = 0, opt_yes, opt_no};
  */
 
 struct opts {
-       enum opt_value dry_run;
        enum opt_value show_help;
        const char *log_file;
        enum opt_value start_daemon;
+       enum opt_value verbose;
        enum opt_value show_version;
 };
 
@@ -74,17 +77,15 @@ struct opts {
 static int opts_parse(struct opts *opts, int argc, char *argv[])
 {
        static const struct option long_options[] = {
-               {"dry-run",      no_argument,       NULL, 'd'},
                {"help",         no_argument,       NULL, 'h'},
                {"log",          required_argument, NULL, 'l'},
                {"start-daemon", no_argument,       NULL, 's'},
+               {"verbose",      no_argument,       NULL, 'v'},
                {"version",      no_argument,       NULL, 'V'},
                { NULL,          0,                 NULL, 0},
        };
-       static const char short_options[] = "dhl:sV";
-       static const struct opts default_values = {
-               .log_file = "/var/log/petitboot/petitboot-nc.log",
-       };
+       static const char short_options[] = "dhl:svV";
+       static const struct opts default_values = { 0 };
 
        *opts = default_values;
 
@@ -96,9 +97,6 @@ static int opts_parse(struct opts *opts, int argc, char *argv[])
                        break;
 
                switch (c) {
-               case 'd':
-                       opts->dry_run = opt_yes;
-                       break;
                case 'h':
                        opts->show_help = opt_yes;
                        break;
@@ -108,6 +106,9 @@ static int opts_parse(struct opts *opts, int argc, char *argv[])
                case 's':
                        opts->start_daemon = opt_yes;
                        break;
+               case 'v':
+                       opts->verbose = opt_yes;
+                       break;
                case 'V':
                        opts->show_version = opt_yes;
                        break;
@@ -120,6 +121,31 @@ static int opts_parse(struct opts *opts, int argc, char *argv[])
        return 0;
 }
 
+static char *default_log_filename(void)
+{
+       const char *base = "/var/log/petitboot/petitboot-nc";
+       static char name[PATH_MAX];
+       char *tty;
+       int i;
+
+       tty = ttyname(STDIN_FILENO);
+
+       /* strip /dev/ */
+       if (tty && !strncmp(tty, "/dev/", 5))
+               tty += 5;
+
+       /* change slashes to hyphens */
+       for (i = 0; tty && tty[i]; i++)
+               if (tty[i] == '/')
+                       tty[i] = '-';
+
+       if (!tty || !*tty)
+               tty = "unknown";
+
+       snprintf(name, sizeof(name), "%s.%s.log", base, tty);
+
+       return name;
+}
 /**
  * struct pb_cui - Main cui program instance.
  * @mm: Main menu.
@@ -131,29 +157,22 @@ struct pb_cui {
        struct cui *cui;
 };
 
-static struct pb_cui *pb_from_cui(struct cui *cui)
+static int pmenu_sysinfo(struct pmenu_item *item)
 {
-       struct pb_cui *pb;
-
-       assert(cui->c_sig == pb_cui_sig);
-       pb = cui->platform_info;
-       assert(pb->cui->c_sig == pb_cui_sig);
-       return pb;
+       cui_show_sysinfo(cui_from_item(item));
+       return 0;
 }
 
-/**
- * pb_kexec_cb - The kexec callback.
- */
-
-static int pb_kexec_cb(struct cui *cui, struct cui_opt_data *cod)
+static int pmenu_config(struct pmenu_item *item)
 {
-       struct pb_cui *pb = pb_from_cui(cui);
-
-       pb_log("%s: %s\n", __func__, cod->name);
-
-       assert(pb->cui->current == &pb->cui->main->scr);
+       cui_show_config(cui_from_item(item));
+       return 0;
+}
 
-       return pb_run_kexec(cod->bd, pb->cui->dry_run);
+static int pmenu_reinit(struct pmenu_item *item)
+{
+       cui_send_reinit(cui_from_item(item));
+       return 0;
 }
 
 /**
@@ -166,24 +185,41 @@ static struct pmenu *pb_mm_init(struct pb_cui *pb_cui)
        struct pmenu *m;
        struct pmenu_item *i;
 
-       m = pmenu_init(pb_cui->cui, 1, cui_on_exit);
+       m = pmenu_init(pb_cui->cui, 5, cui_on_exit);
 
        if (!m) {
                pb_log("%s: failed\n", __func__);
                return NULL;
        }
 
-       m->on_open = cui_on_open;
+       m->on_new = cui_item_new;
 
-       m->scr.frame.title = talloc_asprintf(m,
-               "Petitboot (" PACKAGE_VERSION ")%s",
-               (pb_cui->cui->dry_run ? " (dry-run)" : ""));
+       m->scr.frame.ltitle = talloc_asprintf(m,
+               "Petitboot (" PACKAGE_VERSION ")");
+       m->scr.frame.rtitle = NULL;
        m->scr.frame.help = talloc_strdup(m,
-               "ESC=exit, Enter=accept, e=edit, o=open");
+               "Enter=accept, e=edit, n=new, x=exit, h=help");
        m->scr.frame.status = talloc_strdup(m, "Welcome to Petitboot");
 
-       i = pmenu_item_init(m, 0, "Exit to Shell");
+       i = pmenu_item_create(m, " ");
+       item_opts_off(i->nci, O_SELECTABLE);
+       pmenu_item_insert(m, i, 0);
+
+       i = pmenu_item_create(m, "System information");
+       i->on_execute = pmenu_sysinfo;
+       pmenu_item_insert(m, i, 1);
+
+       i = pmenu_item_create(m, "System configuration");
+       i->on_execute = pmenu_config;
+       pmenu_item_insert(m, i, 2);
+
+       i = pmenu_item_create(m, "Rescan devices");
+       i->on_execute = pmenu_reinit;
+       pmenu_item_insert(m, i, 3);
+
+       i = pmenu_item_create(m, "Exit to shell");
        i->on_execute = pmenu_exit_cb;
+       pmenu_item_insert(m, i, 4);
 
        result = pmenu_setup(m);
 
@@ -193,6 +229,9 @@ static struct pmenu *pb_mm_init(struct pb_cui *pb_cui)
                goto fail_setup;
        }
 
+       m->help_title = "main menu";
+       m->help_text = main_menu_help_text;
+
        menu_opts_off(m->ncm, O_SHOWDESC);
        set_menu_mark(m->ncm, " *");
        set_current_item(m->ncm, i->nci);
@@ -211,10 +250,6 @@ static void sig_handler(int signum)
        DBGS("%d\n", signum);
 
        switch (signum) {
-       case SIGALRM:
-               if (pb.cui)
-                       ui_timer_sigalrm(&pb.cui->timer);
-               break;
        case SIGWINCH:
                if (pb.cui)
                        cui_resize(pb.cui);
@@ -238,9 +273,11 @@ static void sig_handler(int signum)
 int main(int argc, char *argv[])
 {
        static struct sigaction sa;
+       const char *log_filename;
        int result;
        int cui_result;
        struct opts opts;
+       FILE *log;
 
        result = opts_parse(&opts, argc, argv);
 
@@ -259,17 +296,23 @@ int main(int argc, char *argv[])
                return EXIT_SUCCESS;
        }
 
-       if (strcmp(opts.log_file, "-")) {
-               FILE *log = fopen(opts.log_file, "a");
+       if (opts.log_file)
+               log_filename = opts.log_file;
+       else
+               log_filename = default_log_filename();
 
-               assert(log);
-               pb_log_set_stream(log);
-       } else
-               pb_log_set_stream(stderr);
+       log = stderr;
+       if (strcmp(log_filename, "-")) {
+               log = fopen(log_filename, "a");
 
-#if defined(DEBUG)
-       pb_log_always_flush(1);
-#endif
+               if (!log)
+                       log = fopen("/dev/null", "a");
+       }
+
+       pb_log_init(log);
+
+       if (opts.verbose == opt_yes)
+               pb_log_set_debug(true);
 
        pb_log("--- petitboot-nc ---\n");
 
@@ -285,14 +328,12 @@ int main(int argc, char *argv[])
                return EXIT_FAILURE;
        }
 
-       pb.cui = cui_init(&pb, pb_kexec_cb, NULL, opts.start_daemon,
-               opts.dry_run);
+       pb.cui = cui_init(&pb, NULL, opts.start_daemon);
 
        if (!pb.cui)
                return EXIT_FAILURE;
 
        pb.mm = pb_mm_init(&pb);
-       ui_timer_disable(&pb.cui->timer);
 
        cui_result = cui_run(pb.cui, pb.mm, 0);