]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/generic-main.c
ui/ncurses: Fix warning from unnecessary if statement
[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 #define _GNU_SOURCE
26 #include <errno.h>
27 #include <getopt.h>
28 #include <signal.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/time.h>
32
33 #include "log/log.h"
34 #include "talloc/talloc.h"
35 #include "waiter/waiter.h"
36 #include "ui/common/discover-client.h"
37 #include "nc-cui.h"
38
39 static void print_version(void)
40 {
41         printf("petitboot-nc (" PACKAGE_NAME ") " PACKAGE_VERSION "\n");
42 }
43
44 static void print_usage(void)
45 {
46         print_version();
47         printf(
48 "Usage: petitboot-nc [-d, --dry-run] [-h, --help] [-l, --log log-file]\n"
49 "                    [-s, --start-daemon] [-V, --version]\n");
50 }
51
52 /**
53  * enum opt_value - Tri-state options variables.
54  */
55
56 enum opt_value {opt_undef = 0, opt_yes, opt_no};
57
58 /**
59  * struct opts - Values from command line options.
60  */
61
62 struct opts {
63         enum opt_value dry_run;
64         enum opt_value show_help;
65         const char *log_file;
66         enum opt_value start_daemon;
67         enum opt_value show_version;
68 };
69
70 /**
71  * opts_parse - Parse the command line options.
72  */
73
74 static int opts_parse(struct opts *opts, int argc, char *argv[])
75 {
76         static const struct option long_options[] = {
77                 {"dry-run",      no_argument,       NULL, 'd'},
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'},
82                 { NULL,          0,                 NULL, 0},
83         };
84         static const char short_options[] = "dhl:sV";
85         static const struct opts default_values = {
86                 .log_file = "/var/log/petitboot/petitboot-nc.log",
87         };
88
89         *opts = default_values;
90
91         while (1) {
92                 int c = getopt_long(argc, argv, short_options, long_options,
93                         NULL);
94
95                 if (c == EOF)
96                         break;
97
98                 switch (c) {
99                 case 'd':
100                         opts->dry_run = opt_yes;
101                         break;
102                 case 'h':
103                         opts->show_help = opt_yes;
104                         break;
105                 case 'l':
106                         opts->log_file = optarg;
107                         break;
108                 case 's':
109                         opts->start_daemon = opt_yes;
110                         break;
111                 case 'V':
112                         opts->show_version = opt_yes;
113                         break;
114                 default:
115                         opts->show_help = opt_yes;
116                         return -1;
117                 }
118         }
119
120         return 0;
121 }
122
123 /**
124  * struct pb_cui - Main cui program instance.
125  * @mm: Main menu.
126  * @svm: Set video mode menu.
127  */
128
129 struct pb_cui {
130         struct pmenu *mm;
131         struct cui *cui;
132 };
133
134 static struct pb_cui *pb_from_cui(struct cui *cui)
135 {
136         struct pb_cui *pb;
137
138         assert(cui->c_sig == pb_cui_sig);
139         pb = cui->platform_info;
140         assert(pb->cui->c_sig == pb_cui_sig);
141         return pb;
142 }
143
144 /**
145  * pb_kexec_cb - The kexec callback.
146  */
147
148 static int pb_kexec_cb(struct cui *cui, struct cui_opt_data *cod)
149 {
150         struct pb_cui *pb = pb_from_cui(cui);
151
152         pb_log("%s: %s\n", __func__, cod->name);
153
154         assert(pb->cui->current == &pb->cui->main->scr);
155
156         return pb_run_kexec(cod->kd, pb->cui->dry_run);
157 }
158
159 /**
160  * pb_mm_init - Setup the main menu instance.
161  */
162
163 static struct pmenu *pb_mm_init(struct pb_cui *pb_cui)
164 {
165         int result;
166         struct pmenu *m;
167         struct pmenu_item *i;
168
169         m = pmenu_init(pb_cui->cui, 1, cui_on_exit);
170
171         if (!m) {
172                 pb_log("%s: failed\n", __func__);
173                 return NULL;
174         }
175
176         m->on_open = cui_on_open;
177
178         m->scr.frame.title = talloc_asprintf(m,
179                 "Petitboot (" PACKAGE_VERSION ")%s",
180                 (pb_cui->cui->dry_run ? " (dry-run)" : ""));
181         m->scr.frame.help = talloc_strdup(m,
182                 "ESC=exit, Enter=accept, e=edit, o=open");
183         m->scr.frame.status = talloc_strdup(m, "Welcome to Petitboot");
184
185         i = pmenu_item_init(m, 0, "Exit to Shell");
186         i->on_execute = pmenu_exit_cb;
187
188         result = pmenu_setup(m);
189
190         if (result) {
191                 pb_log("%s:%d: pmenu_setup failed: %s\n", __func__, __LINE__,
192                         strerror(errno));
193                 goto fail_setup;
194         }
195
196         menu_opts_off(m->ncm, O_SHOWDESC);
197         set_menu_mark(m->ncm, " *");
198         set_current_item(m->ncm, i->nci);
199
200         return m;
201
202 fail_setup:
203         talloc_free(m);
204         return NULL;
205 }
206
207 static struct pb_cui pb;
208
209 static void sig_handler(int signum)
210 {
211         DBGS("%d\n", signum);
212
213         switch (signum) {
214         case SIGALRM:
215                 if (pb.cui)
216                         ui_timer_sigalrm(&pb.cui->timer);
217                 break;
218         case SIGWINCH:
219                 if (pb.cui)
220                         cui_resize(pb.cui);
221                 break;
222         default:
223                 assert(0 && "unknown sig");
224                 /* fall through */
225         case SIGINT:
226         case SIGHUP:
227         case SIGTERM:
228                 if (pb.cui)
229                         cui_abort(pb.cui);
230                 break;
231         }
232 }
233
234 /**
235  * main - cui bootloader main routine.
236  */
237
238 int main(int argc, char *argv[])
239 {
240         static struct sigaction sa;
241         int result;
242         int cui_result;
243         struct opts opts;
244
245         result = opts_parse(&opts, argc, argv);
246
247         if (result) {
248                 print_usage();
249                 return EXIT_FAILURE;
250         }
251
252         if (opts.show_help == opt_yes) {
253                 print_usage();
254                 return EXIT_SUCCESS;
255         }
256
257         if (opts.show_version == opt_yes) {
258                 print_version();
259                 return EXIT_SUCCESS;
260         }
261
262         if (strcmp(opts.log_file, "-")) {
263                 FILE *log = fopen(opts.log_file, "a");
264
265                 assert(log);
266                 pb_log_set_stream(log);
267         } else
268                 pb_log_set_stream(stderr);
269
270 #if defined(DEBUG)
271         pb_log_always_flush(1);
272 #endif
273
274         pb_log("--- petitboot-nc ---\n");
275
276         sa.sa_handler = sig_handler;
277         result = sigaction(SIGALRM, &sa, NULL);
278         result += sigaction(SIGHUP, &sa, NULL);
279         result += sigaction(SIGINT, &sa, NULL);
280         result += sigaction(SIGTERM, &sa, NULL);
281         result += sigaction(SIGWINCH, &sa, NULL);
282
283         if (result) {
284                 pb_log("%s sigaction failed.\n", __func__);
285                 return EXIT_FAILURE;
286         }
287
288         pb.cui = cui_init(&pb, pb_kexec_cb, NULL, opts.start_daemon,
289                 opts.dry_run);
290
291         if (!pb.cui)
292                 return EXIT_FAILURE;
293
294         pb.mm = pb_mm_init(&pb);
295         ui_timer_disable(&pb.cui->timer);
296
297         cui_result = cui_run(pb.cui, pb.mm, 0);
298
299         pmenu_delete(pb.mm);
300
301         talloc_free(pb.cui);
302
303         pb_log("--- end ---\n");
304
305         return cui_result ? EXIT_FAILURE : EXIT_SUCCESS;
306 }