2 * Petitboot generic ncurses bootloader UI
4 * Copyright (C) 2009 Sony Computer Entertainment Inc.
5 * Copyright 2009 Sony Corp.
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; version 2 of the License.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #if defined(HAVE_CONFIG_H)
37 #include "talloc/talloc.h"
38 #include "waiter/waiter.h"
39 #include "i18n/i18n.h"
40 #include "ui/common/discover-client.h"
43 static void print_version(void)
45 printf("petitboot-nc (" PACKAGE_NAME ") " PACKAGE_VERSION "\n");
48 static void print_usage(void)
52 "%s: petitboot-nc [-h, --help] [-l, --log log-file] [-s, --start-daemon]\n"
53 " [-t, --timeout] [-v, --verbose] [-V, --version]\n",
58 * enum opt_value - Tri-state options variables.
61 enum opt_value {opt_undef = 0, opt_yes, opt_no};
64 * struct opts - Values from command line options.
68 enum opt_value show_help;
70 enum opt_value start_daemon;
71 enum opt_value timeout;
72 enum opt_value verbose;
73 enum opt_value show_version;
77 * opts_parse - Parse the command line options.
80 static int opts_parse(struct opts *opts, int argc, char *argv[])
82 static const struct option long_options[] = {
83 {"help", no_argument, NULL, 'h'},
84 {"log", required_argument, NULL, 'l'},
85 {"start-daemon", no_argument, NULL, 's'},
86 {"timeout", no_argument, NULL, 't'},
87 {"verbose", no_argument, NULL, 'v'},
88 {"version", no_argument, NULL, 'V'},
91 static const char short_options[] = "dhl:stvV";
92 static const struct opts default_values = { 0 };
94 *opts = default_values;
97 int c = getopt_long(argc, argv, short_options, long_options,
105 opts->show_help = opt_yes;
108 opts->log_file = optarg;
111 opts->start_daemon = opt_yes;
114 opts->timeout = opt_yes;
117 opts->verbose = opt_yes;
120 opts->show_version = opt_yes;
123 opts->show_help = opt_yes;
131 static char *default_log_filename(void)
133 const char *base = "/var/log/petitboot/petitboot-nc";
134 static char name[PATH_MAX];
138 tty = ttyname(STDIN_FILENO);
141 if (tty && !strncmp(tty, "/dev/", 5))
144 /* change slashes to hyphens */
145 for (i = 0; tty && tty[i]; i++)
152 snprintf(name, sizeof(name), "%s.%s.log", base, tty);
157 static struct cui *cui;
160 * struct pb_cui - Main cui program instance.
162 * @svm: Set video mode menu.
165 static void sig_handler(int signum)
167 DBGS("%d\n", signum);
175 assert(0 && "unknown sig");
187 * main - cui bootloader main routine.
190 int main(int argc, char *argv[])
192 static struct sigaction sa;
193 const char *log_filename;
199 result = opts_parse(&opts, argc, argv);
201 setlocale(LC_ALL, "");
202 bindtextdomain(PACKAGE, LOCALEDIR);
210 if (opts.show_help == opt_yes) {
215 if (opts.show_version == opt_yes) {
221 log_filename = opts.log_file;
223 log_filename = default_log_filename();
226 if (strcmp(log_filename, "-")) {
227 log = fopen(log_filename, "a");
230 log = fopen("/dev/null", "a");
235 if (opts.verbose == opt_yes)
236 pb_log_set_debug(true);
238 pb_log("--- petitboot-nc ---\n");
240 sa.sa_handler = sig_handler;
241 result = sigaction(SIGALRM, &sa, NULL);
242 result += sigaction(SIGHUP, &sa, NULL);
243 result += sigaction(SIGINT, &sa, NULL);
244 result += sigaction(SIGTERM, &sa, NULL);
245 result += sigaction(SIGWINCH, &sa, NULL);
248 pb_log("%s sigaction failed.\n", __func__);
252 cui = cui_init(NULL, NULL, opts.start_daemon, opts.timeout);
256 cui_result = cui_run(cui);
260 pb_log("--- end ---\n");
262 return cui_result ? EXIT_FAILURE : EXIT_SUCCESS;