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)
35 #include "talloc/talloc.h"
36 #include "waiter/waiter.h"
37 #include "ui/common/discover-client.h"
40 extern const char *main_menu_help_text;
42 static void print_version(void)
44 printf("petitboot-nc (" PACKAGE_NAME ") " PACKAGE_VERSION "\n");
47 static void print_usage(void)
51 "Usage: petitboot-nc [-h, --help] [-l, --log log-file]\n"
52 " [-s, --start-daemon] [-V, --version]\n");
56 * enum opt_value - Tri-state options variables.
59 enum opt_value {opt_undef = 0, opt_yes, opt_no};
62 * struct opts - Values from command line options.
66 enum opt_value show_help;
68 enum opt_value start_daemon;
69 enum opt_value show_version;
73 * opts_parse - Parse the command line options.
76 static int opts_parse(struct opts *opts, int argc, char *argv[])
78 static const struct option long_options[] = {
79 {"help", no_argument, NULL, 'h'},
80 {"log", required_argument, NULL, 'l'},
81 {"start-daemon", no_argument, NULL, 's'},
82 {"version", no_argument, NULL, 'V'},
85 static const char short_options[] = "dhl:sV";
86 static const struct opts default_values = { 0 };
88 *opts = default_values;
91 int c = getopt_long(argc, argv, short_options, long_options,
99 opts->show_help = opt_yes;
102 opts->log_file = optarg;
105 opts->start_daemon = opt_yes;
108 opts->show_version = opt_yes;
111 opts->show_help = opt_yes;
119 static char *default_log_filename(void)
121 const char *base = "/var/log/petitboot/petitboot-nc";
122 static char name[PATH_MAX];
126 tty = ttyname(STDIN_FILENO);
129 if (tty && !strncmp(tty, "/dev/", 5))
132 /* change slashes to hyphens */
133 for (i = 0; tty && tty[i]; i++)
140 snprintf(name, sizeof(name), "%s.%s.log", base, tty);
145 * struct pb_cui - Main cui program instance.
147 * @svm: Set video mode menu.
155 static int pmenu_sysinfo(struct pmenu_item *item)
157 cui_show_sysinfo(cui_from_item(item));
161 static int pmenu_config(struct pmenu_item *item)
163 cui_show_config(cui_from_item(item));
167 static int pmenu_reinit(struct pmenu_item *item)
169 cui_send_reinit(cui_from_item(item));
174 * pb_mm_init - Setup the main menu instance.
177 static struct pmenu *pb_mm_init(struct pb_cui *pb_cui)
181 struct pmenu_item *i;
183 m = pmenu_init(pb_cui->cui, 5, cui_on_exit);
186 pb_log("%s: failed\n", __func__);
190 m->on_new = cui_item_new;
192 m->scr.frame.ltitle = talloc_asprintf(m,
193 "Petitboot (" PACKAGE_VERSION ")");
194 m->scr.frame.rtitle = NULL;
195 m->scr.frame.help = talloc_strdup(m,
196 "Enter=accept, e=edit, n=new, x=exit, h=help");
197 m->scr.frame.status = talloc_strdup(m, "Welcome to Petitboot");
199 i = pmenu_item_init(m, 0, " ");
200 item_opts_off(i->nci, O_SELECTABLE);
201 i = pmenu_item_init(m, 1, "System information");
202 i->on_execute = pmenu_sysinfo;
203 i = pmenu_item_init(m, 2, "System configuration");
204 i->on_execute = pmenu_config;
205 i = pmenu_item_init(m, 3, "Rescan devices");
206 i->on_execute = pmenu_reinit;
207 i = pmenu_item_init(m, 4, "Exit to shell");
208 i->on_execute = pmenu_exit_cb;
210 result = pmenu_setup(m);
213 pb_log("%s:%d: pmenu_setup failed: %s\n", __func__, __LINE__,
218 m->help_title = "main menu";
219 m->help_text = main_menu_help_text;
221 menu_opts_off(m->ncm, O_SHOWDESC);
222 set_menu_mark(m->ncm, " *");
223 set_current_item(m->ncm, i->nci);
232 static struct pb_cui pb;
234 static void sig_handler(int signum)
236 DBGS("%d\n", signum);
244 assert(0 && "unknown sig");
256 * main - cui bootloader main routine.
259 int main(int argc, char *argv[])
261 static struct sigaction sa;
262 const char *log_filename;
268 result = opts_parse(&opts, argc, argv);
275 if (opts.show_help == opt_yes) {
280 if (opts.show_version == opt_yes) {
286 log_filename = opts.log_file;
288 log_filename = default_log_filename();
291 if (strcmp(log_filename, "-")) {
292 log = fopen(log_filename, "a");
295 log = fopen("/dev/null", "a");
300 pb_log("--- petitboot-nc ---\n");
302 sa.sa_handler = sig_handler;
303 result = sigaction(SIGALRM, &sa, NULL);
304 result += sigaction(SIGHUP, &sa, NULL);
305 result += sigaction(SIGINT, &sa, NULL);
306 result += sigaction(SIGTERM, &sa, NULL);
307 result += sigaction(SIGWINCH, &sa, NULL);
310 pb_log("%s sigaction failed.\n", __func__);
314 pb.cui = cui_init(&pb, NULL, opts.start_daemon);
319 pb.mm = pb_mm_init(&pb);
321 cui_result = cui_run(pb.cui, pb.mm, 0);
327 pb_log("--- end ---\n");
329 return cui_result ? EXIT_FAILURE : EXIT_SUCCESS;