]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/generic-main.c
abf33563a13a10bf5cb82c3fb6a67bfe3a160d72
[petitboot] / ui / ncurses / generic-main.c
1 /*
2  * Petitboot generic ncurses bootloader UI
3  *
4  *  Copyright (C) 2009 Sony Computer Entertainment Inc.
5  *  Copyright 2009 Sony Corp.
6  *
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.
10  *
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.
15  *
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
19  */
20
21 #if defined(HAVE_CONFIG_H)
22 #include "config.h"
23 #endif
24
25 #include <assert.h>
26 #include <errno.h>
27 #include <getopt.h>
28 #include <signal.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <limits.h>
32 #include <sys/time.h>
33 #include <libintl.h>
34 #include <locale.h>
35
36 #include "log/log.h"
37 #include "talloc/talloc.h"
38 #include "waiter/waiter.h"
39 #include "i18n/i18n.h"
40 #include "ui/common/discover-client.h"
41 #include "nc-cui.h"
42
43 extern const char *main_menu_help_text;
44
45 static void print_version(void)
46 {
47         printf("petitboot-nc (" PACKAGE_NAME ") " PACKAGE_VERSION "\n");
48 }
49
50 static void print_usage(void)
51 {
52         print_version();
53         printf(
54 "%s: petitboot-nc [-h, --help] [-l, --log log-file]\n"
55 "                    [-s, --start-daemon] [-v, --verbose] [-V, --version]\n",
56                         _("Usage"));
57 }
58
59 /**
60  * enum opt_value - Tri-state options variables.
61  */
62
63 enum opt_value {opt_undef = 0, opt_yes, opt_no};
64
65 /**
66  * struct opts - Values from command line options.
67  */
68
69 struct opts {
70         enum opt_value show_help;
71         const char *log_file;
72         enum opt_value start_daemon;
73         enum opt_value verbose;
74         enum opt_value show_version;
75 };
76
77 /**
78  * opts_parse - Parse the command line options.
79  */
80
81 static int opts_parse(struct opts *opts, int argc, char *argv[])
82 {
83         static const struct option long_options[] = {
84                 {"help",         no_argument,       NULL, 'h'},
85                 {"log",          required_argument, NULL, 'l'},
86                 {"start-daemon", no_argument,       NULL, 's'},
87                 {"verbose",      no_argument,       NULL, 'v'},
88                 {"version",      no_argument,       NULL, 'V'},
89                 { NULL,          0,                 NULL, 0},
90         };
91         static const char short_options[] = "dhl:svV";
92         static const struct opts default_values = { 0 };
93
94         *opts = default_values;
95
96         while (1) {
97                 int c = getopt_long(argc, argv, short_options, long_options,
98                         NULL);
99
100                 if (c == EOF)
101                         break;
102
103                 switch (c) {
104                 case 'h':
105                         opts->show_help = opt_yes;
106                         break;
107                 case 'l':
108                         opts->log_file = optarg;
109                         break;
110                 case 's':
111                         opts->start_daemon = opt_yes;
112                         break;
113                 case 'v':
114                         opts->verbose = opt_yes;
115                         break;
116                 case 'V':
117                         opts->show_version = opt_yes;
118                         break;
119                 default:
120                         opts->show_help = opt_yes;
121                         return -1;
122                 }
123         }
124
125         return 0;
126 }
127
128 static char *default_log_filename(void)
129 {
130         const char *base = "/var/log/petitboot/petitboot-nc";
131         static char name[PATH_MAX];
132         char *tty;
133         int i;
134
135         tty = ttyname(STDIN_FILENO);
136
137         /* strip /dev/ */
138         if (tty && !strncmp(tty, "/dev/", 5))
139                 tty += 5;
140
141         /* change slashes to hyphens */
142         for (i = 0; tty && tty[i]; i++)
143                 if (tty[i] == '/')
144                         tty[i] = '-';
145
146         if (!tty || !*tty)
147                 tty = "unknown";
148
149         snprintf(name, sizeof(name), "%s.%s.log", base, tty);
150
151         return name;
152 }
153 /**
154  * struct pb_cui - Main cui program instance.
155  * @mm: Main menu.
156  * @svm: Set video mode menu.
157  */
158
159 struct pb_cui {
160         struct pmenu *mm;
161         struct cui *cui;
162 };
163
164 static int pmenu_sysinfo(struct pmenu_item *item)
165 {
166         cui_show_sysinfo(cui_from_item(item));
167         return 0;
168 }
169
170 static int pmenu_config(struct pmenu_item *item)
171 {
172         cui_show_config(cui_from_item(item));
173         return 0;
174 }
175
176 static int pmenu_reinit(struct pmenu_item *item)
177 {
178         cui_send_reinit(cui_from_item(item));
179         return 0;
180 }
181
182 /**
183  * pb_mm_init - Setup the main menu instance.
184  */
185
186 static struct pmenu *pb_mm_init(struct pb_cui *pb_cui)
187 {
188         int result;
189         struct pmenu *m;
190         struct pmenu_item *i;
191
192         m = pmenu_init(pb_cui->cui, 5, cui_on_exit);
193
194         if (!m) {
195                 pb_log("%s: failed\n", __func__);
196                 return NULL;
197         }
198
199         m->on_new = cui_item_new;
200
201         m->scr.frame.ltitle = talloc_asprintf(m,
202                 "Petitboot (" PACKAGE_VERSION ")");
203         m->scr.frame.rtitle = NULL;
204         m->scr.frame.help = talloc_strdup(m,
205                 _("Enter=accept, e=edit, n=new, x=exit, h=help"));
206         m->scr.frame.status = talloc_strdup(m, _("Welcome to Petitboot"));
207
208         i = pmenu_item_create(m, " ");
209         item_opts_off(i->nci, O_SELECTABLE);
210         pmenu_item_insert(m, i, 0);
211
212         i = pmenu_item_create(m, _("System information"));
213         i->on_execute = pmenu_sysinfo;
214         pmenu_item_insert(m, i, 1);
215
216         i = pmenu_item_create(m, _("System configuration"));
217         i->on_execute = pmenu_config;
218         pmenu_item_insert(m, i, 2);
219
220         i = pmenu_item_create(m, _("Rescan devices"));
221         i->on_execute = pmenu_reinit;
222         pmenu_item_insert(m, i, 3);
223
224         i = pmenu_item_create(m, _("Exit to shell"));
225         i->on_execute = pmenu_exit_cb;
226         pmenu_item_insert(m, i, 4);
227
228         result = pmenu_setup(m);
229
230         if (result) {
231                 pb_log("%s:%d: pmenu_setup failed: %s\n", __func__, __LINE__,
232                         strerror(errno));
233                 goto fail_setup;
234         }
235
236         m->help_title = _("main menu");
237         m->help_text = main_menu_help_text;
238
239         menu_opts_off(m->ncm, O_SHOWDESC);
240         set_menu_mark(m->ncm, " *");
241         set_current_item(m->ncm, i->nci);
242
243         return m;
244
245 fail_setup:
246         talloc_free(m);
247         return NULL;
248 }
249
250 static struct pb_cui pb;
251
252 static void sig_handler(int signum)
253 {
254         DBGS("%d\n", signum);
255
256         switch (signum) {
257         case SIGWINCH:
258                 if (pb.cui)
259                         cui_resize(pb.cui);
260                 break;
261         default:
262                 assert(0 && "unknown sig");
263                 /* fall through */
264         case SIGINT:
265         case SIGHUP:
266         case SIGTERM:
267                 if (pb.cui)
268                         cui_abort(pb.cui);
269                 break;
270         }
271 }
272
273 /**
274  * main - cui bootloader main routine.
275  */
276
277 int main(int argc, char *argv[])
278 {
279         static struct sigaction sa;
280         const char *log_filename;
281         int result;
282         int cui_result;
283         struct opts opts;
284         FILE *log;
285
286         result = opts_parse(&opts, argc, argv);
287
288         setlocale(LC_ALL, "");
289         bindtextdomain(PACKAGE, LOCALEDIR);
290         textdomain(PACKAGE);
291
292         if (result) {
293                 print_usage();
294                 return EXIT_FAILURE;
295         }
296
297         if (opts.show_help == opt_yes) {
298                 print_usage();
299                 return EXIT_SUCCESS;
300         }
301
302         if (opts.show_version == opt_yes) {
303                 print_version();
304                 return EXIT_SUCCESS;
305         }
306
307         if (opts.log_file)
308                 log_filename = opts.log_file;
309         else
310                 log_filename = default_log_filename();
311
312         log = stderr;
313         if (strcmp(log_filename, "-")) {
314                 log = fopen(log_filename, "a");
315
316                 if (!log)
317                         log = fopen("/dev/null", "a");
318         }
319
320         pb_log_init(log);
321
322         if (opts.verbose == opt_yes)
323                 pb_log_set_debug(true);
324
325         pb_log("--- petitboot-nc ---\n");
326
327         sa.sa_handler = sig_handler;
328         result = sigaction(SIGALRM, &sa, NULL);
329         result += sigaction(SIGHUP, &sa, NULL);
330         result += sigaction(SIGINT, &sa, NULL);
331         result += sigaction(SIGTERM, &sa, NULL);
332         result += sigaction(SIGWINCH, &sa, NULL);
333
334         if (result) {
335                 pb_log("%s sigaction failed.\n", __func__);
336                 return EXIT_FAILURE;
337         }
338
339         pb.cui = cui_init(&pb, NULL, opts.start_daemon);
340
341         if (!pb.cui)
342                 return EXIT_FAILURE;
343
344         pb.mm = pb_mm_init(&pb);
345
346         cui_result = cui_run(pb.cui, pb.mm, 0);
347
348         pmenu_delete(pb.mm);
349
350         talloc_free(pb.cui);
351
352         pb_log("--- end ---\n");
353
354         return cui_result ? EXIT_FAILURE : EXIT_SUCCESS;
355 }