X-Git-Url: http://git.ozlabs.org/?p=petitboot;a=blobdiff_plain;f=ui%2Fncurses%2Fgeneric-main.c;h=70627966a78653da5a77e0ba35147740798530ed;hp=560e3e5683e9b32b9ee9c965a4df01e32580a458;hb=18a47a31b46d916c58a31e8784a7c3a3abcae446;hpb=293fca73b2fe077e7780c1a42216a3db41a6e737 diff --git a/ui/ncurses/generic-main.c b/ui/ncurses/generic-main.c index 560e3e5..7062796 100644 --- a/ui/ncurses/generic-main.c +++ b/ui/ncurses/generic-main.c @@ -22,17 +22,21 @@ #include "config.h" #endif -#define _GNU_SOURCE +#include #include #include #include #include #include +#include #include +#include +#include #include "log/log.h" #include "talloc/talloc.h" #include "waiter/waiter.h" +#include "i18n/i18n.h" #include "ui/common/discover-client.h" #include "nc-cui.h" @@ -45,8 +49,9 @@ static void print_usage(void) { print_version(); printf( -"Usage: petitboot-nc [-h, --help] [-l, --log log-file]\n" -" [-s, --start-daemon] [-V, --version]\n"); +"%s: petitboot-nc [-h, --help] [-l, --log log-file] [-s, --start-daemon]\n" +" [-t, --timeout] [-v, --verbose] [-V, --version]\n", + _("Usage")); } /** @@ -63,6 +68,8 @@ struct opts { enum opt_value show_help; const char *log_file; enum opt_value start_daemon; + enum opt_value timeout; + enum opt_value verbose; enum opt_value show_version; }; @@ -76,13 +83,13 @@ static int opts_parse(struct opts *opts, int argc, char *argv[]) {"help", no_argument, NULL, 'h'}, {"log", required_argument, NULL, 'l'}, {"start-daemon", no_argument, NULL, 's'}, + {"timeout", no_argument, NULL, 't'}, + {"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:stvV"; + static const struct opts default_values = { 0 }; *opts = default_values; @@ -103,6 +110,12 @@ static int opts_parse(struct opts *opts, int argc, char *argv[]) case 's': opts->start_daemon = opt_yes; break; + case 't': + opts->timeout = opt_yes; + break; + case 'v': + opts->verbose = opt_yes; + break; case 'V': opts->show_version = opt_yes; break; @@ -115,65 +128,39 @@ static int opts_parse(struct opts *opts, int argc, char *argv[]) return 0; } -/** - * struct pb_cui - Main cui program instance. - * @mm: Main menu. - * @svm: Set video mode menu. - */ - -struct pb_cui { - struct pmenu *mm; - struct cui *cui; -}; - -/** - * pb_mm_init - Setup the main menu instance. - */ - -static struct pmenu *pb_mm_init(struct pb_cui *pb_cui) +static char *default_log_filename(void) { - int result; - struct pmenu *m; - struct pmenu_item *i; - - m = pmenu_init(pb_cui->cui, 1, cui_on_exit); + const char *base = "/var/log/petitboot/petitboot-nc"; + static char name[PATH_MAX]; + char *tty; + int i; - if (!m) { - pb_log("%s: failed\n", __func__); - return NULL; - } + tty = ttyname(STDIN_FILENO); - m->on_open = cui_on_open; + /* strip /dev/ */ + if (tty && !strncmp(tty, "/dev/", 5)) + tty += 5; - m->scr.frame.title = talloc_asprintf(m, - "Petitboot (" PACKAGE_VERSION ")"); - m->scr.frame.help = talloc_strdup(m, - "ESC=exit, Enter=accept, e=edit, o=open"); - m->scr.frame.status = talloc_strdup(m, "Welcome to Petitboot"); + /* change slashes to hyphens */ + for (i = 0; tty && tty[i]; i++) + if (tty[i] == '/') + tty[i] = '-'; - i = pmenu_item_init(m, 0, "Exit to Shell"); - i->on_execute = pmenu_exit_cb; + if (!tty || !*tty) + tty = "unknown"; - result = pmenu_setup(m); + snprintf(name, sizeof(name), "%s.%s.log", base, tty); - if (result) { - pb_log("%s:%d: pmenu_setup failed: %s\n", __func__, __LINE__, - strerror(errno)); - goto fail_setup; - } - - menu_opts_off(m->ncm, O_SHOWDESC); - set_menu_mark(m->ncm, " *"); - set_current_item(m->ncm, i->nci); - - return m; - -fail_setup: - talloc_free(m); - return NULL; + return name; } -static struct pb_cui pb; +static struct cui *cui; + +/* + * struct pb_cui - Main cui program instance. + * @mm: Main menu. + * @svm: Set video mode menu. + */ static void sig_handler(int signum) { @@ -181,8 +168,8 @@ static void sig_handler(int signum) switch (signum) { case SIGWINCH: - if (pb.cui) - cui_resize(pb.cui); + if (cui) + cui_resize(cui); break; default: assert(0 && "unknown sig"); @@ -190,8 +177,8 @@ static void sig_handler(int signum) case SIGINT: case SIGHUP: case SIGTERM: - if (pb.cui) - cui_abort(pb.cui); + if (cui) + cui_abort(cui); break; } } @@ -203,12 +190,18 @@ 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); + setlocale(LC_ALL, ""); + bindtextdomain(PACKAGE, LOCALEDIR); + textdomain(PACKAGE); + if (result) { print_usage(); return EXIT_FAILURE; @@ -224,20 +217,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(); + + log = stderr; + if (strcmp(log_filename, "-")) { + log = fopen(log_filename, "a"); if (!log) log = fopen("/dev/null", "a"); + } - assert(log); - pb_log_set_stream(log); - } else - pb_log_set_stream(stderr); + pb_log_init(log); -#if defined(DEBUG) - pb_log_always_flush(1); -#endif + if (opts.verbose == opt_yes) + pb_log_set_debug(true); pb_log("--- petitboot-nc ---\n"); @@ -253,19 +249,13 @@ int main(int argc, char *argv[]) return EXIT_FAILURE; } - pb.cui = cui_init(&pb, NULL, opts.start_daemon); - - if (!pb.cui) + cui = cui_init(NULL, NULL, opts.start_daemon, opts.timeout); + if (!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); - - pmenu_delete(pb.mm); + cui_result = cui_run(cui); - talloc_free(pb.cui); + talloc_free(cui); pb_log("--- end ---\n");