* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
+#if defined(HAVE_CONFIG_H)
#include "config.h"
-
-#define _GNU_SOURCE
+#endif
#include <assert.h>
#include <errno.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 [-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"));
}
/**
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;
};
{"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;
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;
-};
-
-/**
- * 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;
+ const char *base = "/var/log/petitboot/petitboot-nc";
+ static char name[PATH_MAX];
+ char *tty;
+ int i;
- m = pmenu_init(pb_cui->cui, 1, cui_on_exit);
-
- 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)
{
switch (signum) {
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;
+ 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;
}
- 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");
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);
-
- 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");