#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"
#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");
print_version();
printf(
"Usage: petitboot-nc [-h, --help] [-l, --log log-file]\n"
-" [-s, --start-daemon] [-V, --version]\n");
+" [-s, --start-daemon] [-v, --verbose] [-V, --version]\n");
}
/**
enum opt_value show_help;
const char *log_file;
enum opt_value start_daemon;
+ enum opt_value verbose;
enum opt_value show_version;
};
{"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;
case 's':
opts->start_daemon = opt_yes;
break;
+ case 'v':
+ opts->verbose = opt_yes;
+ break;
case 'V':
opts->show_version = opt_yes;
break;
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.
struct cui *cui;
};
+static int pmenu_sysinfo(struct pmenu_item *item)
+{
+ cui_show_sysinfo(cui_from_item(item));
+ return 0;
+}
+
+static int pmenu_config(struct pmenu_item *item)
+{
+ cui_show_config(cui_from_item(item));
+ return 0;
+}
+
+static int pmenu_reinit(struct pmenu_item *item)
+{
+ cui_send_reinit(cui_from_item(item));
+ return 0;
+}
+
/**
* pb_mm_init - Setup the main menu instance.
*/
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,
+ 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);
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);
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);
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");