]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/generic-main.c
7616788d6b7ea9dd583af198a982f24091794713
[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 /**
144  * pb_mm_init - Setup the main menu instance.
145  */
146
147 static struct pmenu *pb_mm_init(struct pb_cui *pb_cui)
148 {
149         int result;
150         struct pmenu *m;
151         struct pmenu_item *i;
152
153         m = pmenu_init(pb_cui->cui, 4, cui_on_exit);
154
155         if (!m) {
156                 pb_log("%s: failed\n", __func__);
157                 return NULL;
158         }
159
160         m->on_new = cui_item_new;
161
162         m->scr.frame.ltitle = talloc_asprintf(m,
163                 "Petitboot (" PACKAGE_VERSION ")");
164         m->scr.frame.rtitle = NULL;
165         m->scr.frame.help = talloc_strdup(m,
166                 "Enter=accept, e=edit, n=new, x=exit, h=help");
167         m->scr.frame.status = talloc_strdup(m, "Welcome to Petitboot");
168
169         i = pmenu_item_init(m, 0, " ");
170         item_opts_off(i->nci, O_SELECTABLE);
171         i = pmenu_item_init(m, 1, "System information");
172         i->on_execute = pmenu_sysinfo;
173         i = pmenu_item_init(m, 2, "System configuration");
174         i->on_execute = pmenu_config;
175         i = pmenu_item_init(m, 3, "Exit to shell");
176         i->on_execute = pmenu_exit_cb;
177
178         result = pmenu_setup(m);
179
180         if (result) {
181                 pb_log("%s:%d: pmenu_setup failed: %s\n", __func__, __LINE__,
182                         strerror(errno));
183                 goto fail_setup;
184         }
185
186         m->help_title = "main menu";
187         m->help_text = main_menu_help_text;
188
189         menu_opts_off(m->ncm, O_SHOWDESC);
190         set_menu_mark(m->ncm, " *");
191         set_current_item(m->ncm, i->nci);
192
193         return m;
194
195 fail_setup:
196         talloc_free(m);
197         return NULL;
198 }
199
200 static struct pb_cui pb;
201
202 static void sig_handler(int signum)
203 {
204         DBGS("%d\n", signum);
205
206         switch (signum) {
207         case SIGWINCH:
208                 if (pb.cui)
209                         cui_resize(pb.cui);
210                 break;
211         default:
212                 assert(0 && "unknown sig");
213                 /* fall through */
214         case SIGINT:
215         case SIGHUP:
216         case SIGTERM:
217                 if (pb.cui)
218                         cui_abort(pb.cui);
219                 break;
220         }
221 }
222
223 /**
224  * main - cui bootloader main routine.
225  */
226
227 int main(int argc, char *argv[])
228 {
229         static struct sigaction sa;
230         int result;
231         int cui_result;
232         struct opts opts;
233         FILE *log;
234
235         result = opts_parse(&opts, argc, argv);
236
237         if (result) {
238                 print_usage();
239                 return EXIT_FAILURE;
240         }
241
242         if (opts.show_help == opt_yes) {
243                 print_usage();
244                 return EXIT_SUCCESS;
245         }
246
247         if (opts.show_version == opt_yes) {
248                 print_version();
249                 return EXIT_SUCCESS;
250         }
251
252         log = stderr;
253         if (strcmp(opts.log_file, "-")) {
254                 log = fopen(opts.log_file, "a");
255
256                 if (!log)
257                         log = fopen("/dev/null", "a");
258         }
259
260         pb_log_init(log);
261
262         pb_log("--- petitboot-nc ---\n");
263
264         sa.sa_handler = sig_handler;
265         result = sigaction(SIGALRM, &sa, NULL);
266         result += sigaction(SIGHUP, &sa, NULL);
267         result += sigaction(SIGINT, &sa, NULL);
268         result += sigaction(SIGTERM, &sa, NULL);
269         result += sigaction(SIGWINCH, &sa, NULL);
270
271         if (result) {
272                 pb_log("%s sigaction failed.\n", __func__);
273                 return EXIT_FAILURE;
274         }
275
276         pb.cui = cui_init(&pb, NULL, opts.start_daemon);
277
278         if (!pb.cui)
279                 return EXIT_FAILURE;
280
281         pb.mm = pb_mm_init(&pb);
282
283         cui_result = cui_run(pb.cui, pb.mm, 0);
284
285         pmenu_delete(pb.mm);
286
287         talloc_free(pb.cui);
288
289         pb_log("--- end ---\n");
290
291         return cui_result ? EXIT_FAILURE : EXIT_SUCCESS;
292 }