]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/generic-main.c
2cfb9c319f634d2570662b350d3ef598e8a9056c
[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 <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 extern const char *main_menu_help_text;
40
41 static void print_version(void)
42 {
43         printf("petitboot-nc (" PACKAGE_NAME ") " PACKAGE_VERSION "\n");
44 }
45
46 static void print_usage(void)
47 {
48         print_version();
49         printf(
50 "Usage: petitboot-nc [-h, --help] [-l, --log log-file]\n"
51 "                    [-s, --start-daemon] [-V, --version]\n");
52 }
53
54 /**
55  * enum opt_value - Tri-state options variables.
56  */
57
58 enum opt_value {opt_undef = 0, opt_yes, opt_no};
59
60 /**
61  * struct opts - Values from command line options.
62  */
63
64 struct opts {
65         enum opt_value show_help;
66         const char *log_file;
67         enum opt_value start_daemon;
68         enum opt_value show_version;
69 };
70
71 /**
72  * opts_parse - Parse the command line options.
73  */
74
75 static int opts_parse(struct opts *opts, int argc, char *argv[])
76 {
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'},
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 'h':
100                         opts->show_help = opt_yes;
101                         break;
102                 case 'l':
103                         opts->log_file = optarg;
104                         break;
105                 case 's':
106                         opts->start_daemon = opt_yes;
107                         break;
108                 case 'V':
109                         opts->show_version = opt_yes;
110                         break;
111                 default:
112                         opts->show_help = opt_yes;
113                         return -1;
114                 }
115         }
116
117         return 0;
118 }
119
120 /**
121  * struct pb_cui - Main cui program instance.
122  * @mm: Main menu.
123  * @svm: Set video mode menu.
124  */
125
126 struct pb_cui {
127         struct pmenu *mm;
128         struct cui *cui;
129 };
130
131 static int pmenu_sysinfo(struct pmenu_item *item)
132 {
133         cui_show_sysinfo(cui_from_item(item));
134         return 0;
135 }
136
137 static int pmenu_config(struct pmenu_item *item)
138 {
139         cui_show_config(cui_from_item(item));
140         return 0;
141 }
142
143 static int pmenu_reinit(struct pmenu_item *item)
144 {
145         cui_send_reinit(cui_from_item(item));
146         return 0;
147 }
148
149 /**
150  * pb_mm_init - Setup the main menu instance.
151  */
152
153 static struct pmenu *pb_mm_init(struct pb_cui *pb_cui)
154 {
155         int result;
156         struct pmenu *m;
157         struct pmenu_item *i;
158
159         m = pmenu_init(pb_cui->cui, 5, cui_on_exit);
160
161         if (!m) {
162                 pb_log("%s: failed\n", __func__);
163                 return NULL;
164         }
165
166         m->on_new = cui_item_new;
167
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");
174
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;
185
186         result = pmenu_setup(m);
187
188         if (result) {
189                 pb_log("%s:%d: pmenu_setup failed: %s\n", __func__, __LINE__,
190                         strerror(errno));
191                 goto fail_setup;
192         }
193
194         m->help_title = "main menu";
195         m->help_text = main_menu_help_text;
196
197         menu_opts_off(m->ncm, O_SHOWDESC);
198         set_menu_mark(m->ncm, " *");
199         set_current_item(m->ncm, i->nci);
200
201         return m;
202
203 fail_setup:
204         talloc_free(m);
205         return NULL;
206 }
207
208 static struct pb_cui pb;
209
210 static void sig_handler(int signum)
211 {
212         DBGS("%d\n", signum);
213
214         switch (signum) {
215         case SIGWINCH:
216                 if (pb.cui)
217                         cui_resize(pb.cui);
218                 break;
219         default:
220                 assert(0 && "unknown sig");
221                 /* fall through */
222         case SIGINT:
223         case SIGHUP:
224         case SIGTERM:
225                 if (pb.cui)
226                         cui_abort(pb.cui);
227                 break;
228         }
229 }
230
231 /**
232  * main - cui bootloader main routine.
233  */
234
235 int main(int argc, char *argv[])
236 {
237         static struct sigaction sa;
238         int result;
239         int cui_result;
240         struct opts opts;
241         FILE *log;
242
243         result = opts_parse(&opts, argc, argv);
244
245         if (result) {
246                 print_usage();
247                 return EXIT_FAILURE;
248         }
249
250         if (opts.show_help == opt_yes) {
251                 print_usage();
252                 return EXIT_SUCCESS;
253         }
254
255         if (opts.show_version == opt_yes) {
256                 print_version();
257                 return EXIT_SUCCESS;
258         }
259
260         log = stderr;
261         if (strcmp(opts.log_file, "-")) {
262                 log = fopen(opts.log_file, "a");
263
264                 if (!log)
265                         log = fopen("/dev/null", "a");
266         }
267
268         pb_log_init(log);
269
270         pb_log("--- petitboot-nc ---\n");
271
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);
278
279         if (result) {
280                 pb_log("%s sigaction failed.\n", __func__);
281                 return EXIT_FAILURE;
282         }
283
284         pb.cui = cui_init(&pb, NULL, opts.start_daemon);
285
286         if (!pb.cui)
287                 return EXIT_FAILURE;
288
289         pb.mm = pb_mm_init(&pb);
290
291         cui_result = cui_run(pb.cui, pb.mm, 0);
292
293         pmenu_delete(pb.mm);
294
295         talloc_free(pb.cui);
296
297         pb_log("--- end ---\n");
298
299         return cui_result ? EXIT_FAILURE : EXIT_SUCCESS;
300 }