]> git.ozlabs.org Git - petitboot/blob - ui/ncurses/nc-boot-editor.c
ui: pb_kexec_data -> pb_boot_data
[petitboot] / ui / ncurses / nc-boot-editor.c
1 /*
2  *  Copyright (C) 2009 Sony Computer Entertainment Inc.
3  *  Copyright 2009 Sony Corp.
4  *
5  *  This program is free software; you can redistribute it and/or modify
6  *  it under the terms of the GNU General Public License as published by
7  *  the Free Software Foundation; version 2 of the License.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program; if not, write to the Free Software
16  *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17  */
18
19 #define _GNU_SOURCE
20
21 #include <assert.h>
22 #include <string.h>
23
24 #include "log/log.h"
25 #include "talloc/talloc.h"
26 #include "nc-boot-editor.h"
27
28 static struct boot_editor *boot_editor_from_scr(struct nc_scr *scr)
29 {
30         struct boot_editor *boot_editor;
31
32         assert(scr->sig == pb_boot_editor_sig);
33         boot_editor = (struct boot_editor *)
34                 ((char *)scr - (size_t)&((struct boot_editor *)0)->scr);
35         assert(boot_editor->scr.sig == pb_boot_editor_sig);
36         return boot_editor;
37 }
38
39 static struct boot_editor *boot_editor_from_arg(void *arg)
40 {
41         struct boot_editor *boot_editor = arg;
42
43         assert(boot_editor->scr.sig == pb_boot_editor_sig);
44         return boot_editor;
45 }
46
47 /**
48  * boot_editor_move_cursor - Move the cursor, setting correct attributes.
49  * @req: An ncurses request or char to send to form_driver().
50  */
51
52 static int boot_editor_move_cursor(struct boot_editor *boot_editor, int req)
53 {
54         int result;
55
56         wchgat(boot_editor->scr.sub_ncw, 1,
57                         boot_editor_attr_field_selected, 0, 0);
58         result = form_driver(boot_editor->ncf, req);
59         wchgat(boot_editor->scr.sub_ncw, 1, boot_editor->attr_cursor, 0, 0);
60         wrefresh(boot_editor->scr.main_ncw);
61         return result;
62 }
63
64 /**
65  * boot_editor_insert_mode_set - Set the insert mode.
66  */
67
68 static void boot_editor_insert_mode_set(struct boot_editor *boot_editor,
69                 int req)
70 {
71         switch (req) {
72         case REQ_INS_MODE:
73                 boot_editor->attr_cursor = boot_editor_attr_cursor_ins;
74                 break;
75         case REQ_OVL_MODE:
76                 boot_editor->attr_cursor = boot_editor_attr_cursor_ovl;
77                 break;
78         default:
79                 assert(0 && "bad req");
80                 break;
81         }
82         boot_editor_move_cursor(boot_editor, req);
83 }
84
85 /**
86  * boot_editor_insert_mode_tog - Toggle the insert mode.
87  */
88
89 static void boot_editor_insert_mode_tog(struct boot_editor *boot_editor)
90 {
91         if (boot_editor->attr_cursor == boot_editor_attr_cursor_ins)
92                 boot_editor_insert_mode_set(boot_editor, REQ_OVL_MODE);
93         else
94                 boot_editor_insert_mode_set(boot_editor, REQ_INS_MODE);
95 }
96
97 /**
98  * boot_editor_move_field - Move selected field, setting correct attributes.
99  * @req: An ncurses request to send to form_driver().
100  */
101
102 static int boot_editor_move_field(struct boot_editor *boot_editor, int req)
103 {
104         int result;
105
106         set_field_back(current_field(boot_editor->ncf),
107                         boot_editor_attr_field_normal);
108
109         result = form_driver(boot_editor->ncf, req);
110
111         set_field_back(current_field(boot_editor->ncf),
112                         boot_editor_attr_field_selected);
113
114         boot_editor_move_cursor(boot_editor, REQ_END_FIELD);
115         return result;
116 }
117
118 static int boot_editor_post(struct nc_scr *scr)
119 {
120         struct boot_editor *boot_editor = boot_editor_from_scr(scr);
121
122         post_form(boot_editor->ncf);
123
124         nc_scr_frame_draw(scr);
125         boot_editor_move_field(boot_editor, REQ_FIRST_FIELD);
126         boot_editor_move_field(boot_editor, REQ_END_FIELD);
127         boot_editor_insert_mode_set(boot_editor, REQ_INS_MODE);
128
129         redrawwin(boot_editor->scr.main_ncw);
130         wrefresh(boot_editor->scr.main_ncw);
131
132         return 0;
133 }
134
135 static int boot_editor_unpost(struct nc_scr *scr)
136 {
137         return unpost_form(boot_editor_from_scr(scr)->ncf);
138 }
139
140 static void boot_editor_resize(struct nc_scr *scr)
141 {
142         /* FIXME: forms can't be resized, need to recreate here */
143         boot_editor_unpost(scr);
144         boot_editor_post(scr);
145 }
146
147 /**
148  * boot_editor_chomp - Eat leading and trailing WS.
149  */
150
151 static char *boot_editor_chomp(char *s)
152 {
153         char *start;
154         char *end;
155         char *const s_end = s + strlen(s);
156
157         for (; s < s_end; s++)
158                 if (*s != ' ' && *s != '\t')
159                         break;
160
161         start = end = s;
162
163         for (; s < s_end; s++)
164                 if (*s != ' ' && *s != '\t')
165                         end = s;
166         *(end + 1) = 0;
167         return start;
168 }
169
170 static struct pb_boot_data *boot_editor_prepare_data(
171                 struct boot_editor *boot_editor)
172 {
173         struct pb_boot_data *bd;
174         char *s;
175
176         bd = talloc(boot_editor, struct pb_boot_data);
177
178         if (!bd)
179                 return NULL;
180
181         s = boot_editor_chomp(field_buffer(boot_editor->fields[0], 0));
182         bd->image = *s ? talloc_strdup(bd, s) : NULL;
183
184         s = boot_editor_chomp(field_buffer(boot_editor->fields[1], 0));
185         bd->initrd = *s ? talloc_strdup(bd, s) : NULL;
186
187         s = boot_editor_chomp(field_buffer(boot_editor->fields[2], 0));
188         bd->args = *s ? talloc_strdup(bd, s) : NULL;
189
190         return bd;
191 }
192
193 /**
194  * boot_editor_process_key - Process a user keystroke.
195  *
196  * Called from the cui via the scr:process_key method.
197  */
198
199 static void boot_editor_process_key(struct nc_scr *scr)
200 {
201         struct boot_editor *boot_editor = boot_editor_from_scr(scr);
202         struct pb_boot_data *bd;
203
204         while (1) {
205                 int c = getch();
206
207                 if (c == ERR)
208                         return;
209
210                 /* DBGS("%d (%o)\n", c, c); */
211
212                 switch (c) {
213                 default:
214                         boot_editor_move_cursor(boot_editor, c);
215                         break;
216
217                 /* hot keys */
218                 case 27: /* ESC */
219                         boot_editor->on_exit(boot_editor,
220                                         boot_editor_cancel, NULL);
221                         nc_flush_keys();
222                         return;
223                 case '\n':
224                 case '\r':
225                         form_driver(boot_editor->ncf, REQ_VALIDATION);
226                         bd = boot_editor_prepare_data(boot_editor);
227                         boot_editor->on_exit(boot_editor,
228                                         boot_editor_update, bd);
229                         nc_flush_keys();
230                         return;
231
232                 /* insert mode */
233                 case KEY_IC:
234                         boot_editor_insert_mode_tog(boot_editor);
235                         break;
236
237                 /* form nav */
238                 case KEY_PPAGE:
239                         boot_editor_move_field(boot_editor, REQ_FIRST_FIELD);
240                         break;
241                 case KEY_NPAGE:
242                         boot_editor_move_field(boot_editor, REQ_LAST_FIELD);
243                         break;
244                 case KEY_DOWN:
245                         boot_editor_move_field(boot_editor, REQ_NEXT_FIELD);
246                         break;
247                 case KEY_UP:
248                         boot_editor_move_field(boot_editor, REQ_PREV_FIELD);
249                         break;
250
251                 /* field nav */
252                 case KEY_HOME:
253                         boot_editor_move_cursor(boot_editor, REQ_BEG_FIELD);
254                         break;
255                 case KEY_END:
256                         boot_editor_move_cursor(boot_editor, REQ_END_FIELD);
257                         break;
258                 case KEY_LEFT:
259                         boot_editor_move_cursor(boot_editor, REQ_LEFT_CHAR);
260                         break;
261                 case KEY_RIGHT:
262                         boot_editor_move_cursor(boot_editor, REQ_RIGHT_CHAR);
263                         break;
264                 case KEY_BACKSPACE:
265                         if (boot_editor_move_cursor(boot_editor, REQ_LEFT_CHAR)
266                                         == E_OK)
267                                 boot_editor_move_cursor(boot_editor,
268                                                 REQ_DEL_CHAR);
269                         break;
270                 case KEY_DC:
271                         boot_editor_move_cursor(boot_editor, REQ_DEL_CHAR);
272                         break;
273                 }
274         }
275 }
276
277 /**
278  * boot_editor_destructor - The talloc destructor for a boot_editor.
279  */
280
281 static int boot_editor_destructor(void *arg)
282 {
283         struct boot_editor *boot_editor = boot_editor_from_arg(arg);
284         FIELD **f;
285
286         for (f = boot_editor->fields; *f; f++)
287                 free_field(*f);
288
289         free_form(boot_editor->ncf);
290         boot_editor->scr.sig = pb_removed_sig;
291
292         return 0;
293 }
294
295 static FIELD *boot_editor_setup_field(unsigned int y, unsigned int x, char *str)
296 {
297         FIELD *f;
298
299         f = new_field(1, COLS - 1 - x, y, x, 0, 0);
300         field_opts_off(f, O_STATIC | O_WRAP);
301         set_max_field(f, 256);
302         set_field_buffer(f, 0, str);
303         set_field_status(f, 0);
304         return f;
305 }
306
307 static FIELD *boot_editor_setup_label(unsigned int y, unsigned int x, char *str)
308 {
309         FIELD *f;
310
311         f = new_field(1, strlen(str), y, x, 0, 0);
312         field_opts_off(f, O_ACTIVE);
313         set_field_buffer(f, 0, str);
314         return f;
315 }
316
317 struct boot_editor *boot_editor_init(void *ui_ctx,
318                 const struct pb_boot_data *bd,
319                 void (*on_exit)(struct boot_editor *,
320                                 enum boot_editor_result,
321                                 struct pb_boot_data *))
322 {
323         struct boot_editor *boot_editor;
324
325         pb_log("%s: image:  '%s'\n", __func__, bd->image);
326         pb_log("%s: initrd: '%s'\n", __func__, bd->initrd);
327         pb_log("%s: args:   '%s'\n", __func__, bd->args);
328
329         assert(on_exit);
330
331         boot_editor = talloc_zero(ui_ctx, struct boot_editor);
332
333         if (!boot_editor)
334                 return NULL;
335
336         talloc_set_destructor(boot_editor, boot_editor_destructor);
337
338         nc_scr_init(&boot_editor->scr, pb_boot_editor_sig, 0,
339                         ui_ctx, boot_editor_process_key,
340                 boot_editor_post, boot_editor_unpost, boot_editor_resize);
341
342         boot_editor->scr.frame.title = talloc_strdup(boot_editor,
343                         "Petitboot Option Editor");
344         boot_editor->scr.frame.help = talloc_strdup(boot_editor,
345                         "ESC=cancel, Enter=accept");
346
347         boot_editor->on_exit = on_exit;
348
349         boot_editor->fields = talloc_array(boot_editor, FIELD *, 7);
350
351         boot_editor->fields[0] = boot_editor_setup_field(0, 9, bd->image);
352         boot_editor->fields[1] = boot_editor_setup_field(1, 9, bd->initrd);
353         boot_editor->fields[2] = boot_editor_setup_field(2, 9, bd->args);
354         boot_editor->fields[3] = boot_editor_setup_label(0, 1, "image:");
355         boot_editor->fields[4] = boot_editor_setup_label(1, 1, "initrd:");
356         boot_editor->fields[5] = boot_editor_setup_label(2, 1, "args:");
357         boot_editor->fields[6] = NULL;
358
359         boot_editor->ncf = new_form(boot_editor->fields);
360
361         set_form_win(boot_editor->ncf, boot_editor->scr.main_ncw);
362         set_form_sub(boot_editor->ncf, boot_editor->scr.sub_ncw);
363
364         return boot_editor;
365 }