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