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)
34 #include "talloc/talloc.h"
35 #include "waiter/waiter.h"
36 #include "ui/common/discover-client.h"
39 extern const char *main_menu_help_text;
41 static void print_version(void)
43 printf("petitboot-nc (" PACKAGE_NAME ") " PACKAGE_VERSION "\n");
46 static void print_usage(void)
50 "Usage: petitboot-nc [-h, --help] [-l, --log log-file]\n"
51 " [-s, --start-daemon] [-V, --version]\n");
55 * enum opt_value - Tri-state options variables.
58 enum opt_value {opt_undef = 0, opt_yes, opt_no};
61 * struct opts - Values from command line options.
65 enum opt_value show_help;
67 enum opt_value start_daemon;
68 enum opt_value show_version;
72 * opts_parse - Parse the command line options.
75 static int opts_parse(struct opts *opts, int argc, char *argv[])
77 static const struct option long_options[] = {
78 {"help", no_argument, NULL, 'h'},
79 {"log", required_argument, NULL, 'l'},
80 {"start-daemon", no_argument, NULL, 's'},
81 {"version", no_argument, NULL, 'V'},
84 static const char short_options[] = "dhl:sV";
85 static const struct opts default_values = {
86 .log_file = "/var/log/petitboot/petitboot-nc.log",
89 *opts = default_values;
92 int c = getopt_long(argc, argv, short_options, long_options,
100 opts->show_help = opt_yes;
103 opts->log_file = optarg;
106 opts->start_daemon = opt_yes;
109 opts->show_version = opt_yes;
112 opts->show_help = opt_yes;
121 * struct pb_cui - Main cui program instance.
123 * @svm: Set video mode menu.
131 static int pmenu_sysinfo(struct pmenu_item *item)
133 cui_show_sysinfo(cui_from_item(item));
137 static int pmenu_config(struct pmenu_item *item)
139 cui_show_config(cui_from_item(item));
143 static int pmenu_reinit(struct pmenu_item *item)
145 cui_send_reinit(cui_from_item(item));
150 * pb_mm_init - Setup the main menu instance.
153 static struct pmenu *pb_mm_init(struct pb_cui *pb_cui)
157 struct pmenu_item *i;
159 m = pmenu_init(pb_cui->cui, 5, cui_on_exit);
162 pb_log("%s: failed\n", __func__);
166 m->on_new = cui_item_new;
168 m->scr.frame.ltitle = talloc_asprintf(m,
169 "Petitboot (" PACKAGE_VERSION ")");
170 m->scr.frame.rtitle = NULL;
171 m->scr.frame.help = talloc_strdup(m,
172 "Enter=accept, e=edit, n=new, x=exit, h=help");
173 m->scr.frame.status = talloc_strdup(m, "Welcome to Petitboot");
175 i = pmenu_item_init(m, 0, " ");
176 item_opts_off(i->nci, O_SELECTABLE);
177 i = pmenu_item_init(m, 1, "System information");
178 i->on_execute = pmenu_sysinfo;
179 i = pmenu_item_init(m, 2, "System configuration");
180 i->on_execute = pmenu_config;
181 i = pmenu_item_init(m, 3, "Rescan devices");
182 i->on_execute = pmenu_reinit;
183 i = pmenu_item_init(m, 4, "Exit to shell");
184 i->on_execute = pmenu_exit_cb;
186 result = pmenu_setup(m);
189 pb_log("%s:%d: pmenu_setup failed: %s\n", __func__, __LINE__,
194 m->help_title = "main menu";
195 m->help_text = main_menu_help_text;
197 menu_opts_off(m->ncm, O_SHOWDESC);
198 set_menu_mark(m->ncm, " *");
199 set_current_item(m->ncm, i->nci);
208 static struct pb_cui pb;
210 static void sig_handler(int signum)
212 DBGS("%d\n", signum);
220 assert(0 && "unknown sig");
232 * main - cui bootloader main routine.
235 int main(int argc, char *argv[])
237 static struct sigaction sa;
243 result = opts_parse(&opts, argc, argv);
250 if (opts.show_help == opt_yes) {
255 if (opts.show_version == opt_yes) {
261 if (strcmp(opts.log_file, "-")) {
262 log = fopen(opts.log_file, "a");
265 log = fopen("/dev/null", "a");
270 pb_log("--- petitboot-nc ---\n");
272 sa.sa_handler = sig_handler;
273 result = sigaction(SIGALRM, &sa, NULL);
274 result += sigaction(SIGHUP, &sa, NULL);
275 result += sigaction(SIGINT, &sa, NULL);
276 result += sigaction(SIGTERM, &sa, NULL);
277 result += sigaction(SIGWINCH, &sa, NULL);
280 pb_log("%s sigaction failed.\n", __func__);
284 pb.cui = cui_init(&pb, NULL, opts.start_daemon);
289 pb.mm = pb_mm_init(&pb);
291 cui_result = cui_run(pb.cui, pb.mm, 0);
297 pb_log("--- end ---\n");
299 return cui_result ? EXIT_FAILURE : EXIT_SUCCESS;