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