]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/generic-main.c
ui/ncurses: Use nc-widgets for boot editor
[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 #include "config.h"
22
23 #define _GNU_SOURCE
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 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 [-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 show_help;
64         const char *log_file;
65         enum opt_value start_daemon;
66         enum opt_value show_version;
67 };
68
69 /**
70  * opts_parse - Parse the command line options.
71  */
72
73 static int opts_parse(struct opts *opts, int argc, char *argv[])
74 {
75         static const struct option long_options[] = {
76                 {"help",         no_argument,       NULL, 'h'},
77                 {"log",          required_argument, NULL, 'l'},
78                 {"start-daemon", no_argument,       NULL, 's'},
79                 {"version",      no_argument,       NULL, 'V'},
80                 { NULL,          0,                 NULL, 0},
81         };
82         static const char short_options[] = "dhl:sV";
83         static const struct opts default_values = {
84                 .log_file = "/var/log/petitboot/petitboot-nc.log",
85         };
86
87         *opts = default_values;
88
89         while (1) {
90                 int c = getopt_long(argc, argv, short_options, long_options,
91                         NULL);
92
93                 if (c == EOF)
94                         break;
95
96                 switch (c) {
97                 case 'h':
98                         opts->show_help = opt_yes;
99                         break;
100                 case 'l':
101                         opts->log_file = optarg;
102                         break;
103                 case 's':
104                         opts->start_daemon = opt_yes;
105                         break;
106                 case 'V':
107                         opts->show_version = opt_yes;
108                         break;
109                 default:
110                         opts->show_help = opt_yes;
111                         return -1;
112                 }
113         }
114
115         return 0;
116 }
117
118 /**
119  * struct pb_cui - Main cui program instance.
120  * @mm: Main menu.
121  * @svm: Set video mode menu.
122  */
123
124 struct pb_cui {
125         struct pmenu *mm;
126         struct cui *cui;
127 };
128
129 static int pmenu_sysinfo(struct pmenu_item *item)
130 {
131         cui_show_sysinfo(cui_from_item(item));
132         return 0;
133 }
134
135 /**
136  * pb_mm_init - Setup the main menu instance.
137  */
138
139 static struct pmenu *pb_mm_init(struct pb_cui *pb_cui)
140 {
141         int result;
142         struct pmenu *m;
143         struct pmenu_item *i;
144
145         m = pmenu_init(pb_cui->cui, 3, cui_on_exit);
146
147         if (!m) {
148                 pb_log("%s: failed\n", __func__);
149                 return NULL;
150         }
151
152         m->on_new = cui_item_new;
153
154         m->scr.frame.ltitle = talloc_asprintf(m,
155                 "Petitboot (" PACKAGE_VERSION ")");
156         m->scr.frame.rtitle = NULL;
157         m->scr.frame.help = talloc_strdup(m,
158                 "Enter=accept, e=edit, n=new, x=exit");
159         m->scr.frame.status = talloc_strdup(m, "Welcome to Petitboot");
160
161         i = pmenu_item_init(m, 0, " ");
162         item_opts_off(i->nci, O_SELECTABLE);
163         i = pmenu_item_init(m, 1, "System information");
164         i->on_execute = pmenu_sysinfo;
165         i = pmenu_item_init(m, 2, "Exit to shell");
166         i->on_execute = pmenu_exit_cb;
167
168         result = pmenu_setup(m);
169
170         if (result) {
171                 pb_log("%s:%d: pmenu_setup failed: %s\n", __func__, __LINE__,
172                         strerror(errno));
173                 goto fail_setup;
174         }
175
176         menu_opts_off(m->ncm, O_SHOWDESC);
177         set_menu_mark(m->ncm, " *");
178         set_current_item(m->ncm, i->nci);
179
180         return m;
181
182 fail_setup:
183         talloc_free(m);
184         return NULL;
185 }
186
187 static struct pb_cui pb;
188
189 static void sig_handler(int signum)
190 {
191         DBGS("%d\n", signum);
192
193         switch (signum) {
194         case SIGWINCH:
195                 if (pb.cui)
196                         cui_resize(pb.cui);
197                 break;
198         default:
199                 assert(0 && "unknown sig");
200                 /* fall through */
201         case SIGINT:
202         case SIGHUP:
203         case SIGTERM:
204                 if (pb.cui)
205                         cui_abort(pb.cui);
206                 break;
207         }
208 }
209
210 /**
211  * main - cui bootloader main routine.
212  */
213
214 int main(int argc, char *argv[])
215 {
216         static struct sigaction sa;
217         int result;
218         int cui_result;
219         struct opts opts;
220         FILE *log;
221
222         result = opts_parse(&opts, argc, argv);
223
224         if (result) {
225                 print_usage();
226                 return EXIT_FAILURE;
227         }
228
229         if (opts.show_help == opt_yes) {
230                 print_usage();
231                 return EXIT_SUCCESS;
232         }
233
234         if (opts.show_version == opt_yes) {
235                 print_version();
236                 return EXIT_SUCCESS;
237         }
238
239         log = stderr;
240         if (strcmp(opts.log_file, "-")) {
241                 log = fopen(opts.log_file, "a");
242
243                 if (!log)
244                         log = fopen("/dev/null", "a");
245         }
246
247         pb_log_init(log);
248
249         pb_log("--- petitboot-nc ---\n");
250
251         sa.sa_handler = sig_handler;
252         result = sigaction(SIGALRM, &sa, NULL);
253         result += sigaction(SIGHUP, &sa, NULL);
254         result += sigaction(SIGINT, &sa, NULL);
255         result += sigaction(SIGTERM, &sa, NULL);
256         result += sigaction(SIGWINCH, &sa, NULL);
257
258         if (result) {
259                 pb_log("%s sigaction failed.\n", __func__);
260                 return EXIT_FAILURE;
261         }
262
263         pb.cui = cui_init(&pb, NULL, opts.start_daemon);
264
265         if (!pb.cui)
266                 return EXIT_FAILURE;
267
268         pb.mm = pb_mm_init(&pb);
269
270         cui_result = cui_run(pb.cui, pb.mm, 0);
271
272         pmenu_delete(pb.mm);
273
274         talloc_free(pb.cui);
275
276         pb_log("--- end ---\n");
277
278         return cui_result ? EXIT_FAILURE : EXIT_SUCCESS;
279 }