#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 <libintl.h>
+#include <locale.h>
#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"
{
print_version();
printf(
-"Usage: petitboot-nc [-d, --start-daemon] [-h, --help] [-l, --log log-file]\n"
-" [-V, --version]\n");
+"%s: petitboot-nc [-h, --help] [-l, --log log-file] [-s, --start-daemon]\n"
+" [-t, --timeout] [-v, --verbose] [-V, --version]\n",
+ _("Usage"));
}
/**
*/
struct opts {
- enum opt_value start_daemon;
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;
};
static int opts_parse(struct opts *opts, int argc, char *argv[])
{
static const struct option long_options[] = {
- {"start-daemon", no_argument, NULL, 'd'},
{"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:V";
- 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;
break;
switch (c) {
- case 'd':
- opts->start_daemon = opt_yes;
- break;
case 'h':
opts->show_help = opt_yes;
break;
case 'l':
opts->log_file = optarg;
break;
+ 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;
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;
-};
-
-static struct pb_cui *pb_from_cui(struct cui *cui)
+static char *default_log_filename(void)
{
- struct pb_cui *pb;
+ const char *base = "/var/log/petitboot/petitboot-nc";
+ static char name[PATH_MAX];
+ char *tty;
+ int i;
- assert(cui->c_sig == pb_cui_sig);
- pb = cui->platform_info;
- assert(pb->cui->c_sig == pb_cui_sig);
- return pb;
-}
+ tty = ttyname(STDIN_FILENO);
-/**
- * pb_kexec_cb - The kexec callback.
- */
+ /* strip /dev/ */
+ if (tty && !strncmp(tty, "/dev/", 5))
+ tty += 5;
-static int pb_kexec_cb(struct cui *cui, struct cui_opt_data *cod)
-{
- struct pb_cui *pb = pb_from_cui(cui);
+ /* change slashes to hyphens */
+ for (i = 0; tty && tty[i]; i++)
+ if (tty[i] == '/')
+ tty[i] = '-';
- pb_log("%s: %s\n", __func__, cod->name);
+ if (!tty || !*tty)
+ tty = "unknown";
- assert(pb->cui->current == &pb->cui->main->scr);
+ snprintf(name, sizeof(name), "%s.%s.log", base, tty);
- return pb_run_kexec(cod->kd);
+ return name;
}
-/**
- * pb_mm_init - Setup the main menu instance.
- */
+static struct cui *cui;
-static struct pmenu *pb_mm_init(struct pb_cui *pb_cui)
-{
- int result;
- struct pmenu *m;
- struct pmenu_item *i;
-
- m = pmenu_init(pb_cui->cui, 1, cui_on_exit);
-
- if (!m) {
- pb_log("%s: failed\n", __func__);
- return NULL;
- }
-
- m->on_open = cui_on_open;
-
- m->scr.frame.title = talloc_strdup(m, "Petitboot");
- 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");
-
- i = pmenu_item_init(m, 0, "Exit to Shell");
- i->on_execute = pmenu_exit_cb;
-
- result = pmenu_setup(m);
-
- 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;
-}
-
-static struct pb_cui pb;
+/*
+ * struct pb_cui - Main cui program instance.
+ * @mm: Main menu.
+ * @svm: Set video mode menu.
+ */
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);
+ if (cui)
+ cui_resize(cui);
break;
default:
assert(0 && "unknown sig");
case SIGINT:
case SIGHUP:
case SIGTERM:
- if (pb.cui)
- cui_abort(pb.cui);
+ if (cui)
+ cui_abort(cui);
break;
}
}
int main(int argc, char *argv[])
{
static struct sigaction sa;
- static struct opts opts;
+ 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;
return EXIT_SUCCESS;
}
- log = fopen(opts.log_file, "a");
- assert(log);
- pb_log_set_stream(log);
+ if (opts.log_file)
+ log_filename = opts.log_file;
+ else
+ log_filename = default_log_filename();
-#if defined(DEBUG)
- pb_log_always_flush(1);
-#endif
+ log = stderr;
+ if (strcmp(log_filename, "-")) {
+ log = fopen(log_filename, "a");
+
+ 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");
result += sigaction(SIGWINCH, &sa, NULL);
if (result) {
- pb_log("%s sigaction failed.\n", __func__);
+ pb_log_fn("sigaction failed.\n");
return EXIT_FAILURE;
}
- pb.cui = cui_init(&pb, pb_kexec_cb, 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");