Merge branch 'makefile' into haiku
[grub2/phcoder.git] / normal / menu_entry.c
blobf7662ff75bebffa547cc57af06011092bf46c55f
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 2005,2006,2007,2008 Free Software Foundation, Inc.
5 * GRUB 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, either version 3 of the License, or
8 * (at your option) any later version.
10 * GRUB is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with GRUB. If not, see <http://www.gnu.org/licenses/>.
19 #include <grub/normal.h>
20 #include <grub/term.h>
21 #include <grub/misc.h>
22 #include <grub/mm.h>
23 #include <grub/loader.h>
24 #include <grub/command.h>
25 #include <grub/parser.h>
27 enum update_mode
29 NO_LINE,
30 SINGLE_LINE,
31 ALL_LINES
34 struct line
36 /* The line buffer. */
37 char *buf;
38 /* The length of the line. */
39 int len;
40 /* The maximum length of the line. */
41 int max_len;
44 struct screen
46 /* The array of lines. */
47 struct line *lines;
48 /* The number of lines. */
49 int num_lines;
50 /* The current column. */
51 int column;
52 /* The real column. */
53 int real_column;
54 /* The current line. */
55 int line;
56 /* The X coordinate. */
57 int x;
58 /* The Y coordinate. */
59 int y;
60 /* The kill buffer. */
61 char *killed_text;
62 /* The flag of a completion window. */
63 int completion_shown;
66 /* Used for storing completion items temporarily. */
67 static struct line completion_buffer;
69 /* Initialize a line. */
70 static int
71 init_line (struct line *linep)
73 linep->len = 0;
74 linep->max_len = 80; /* XXX */
75 linep->buf = grub_malloc (linep->max_len);
76 if (! linep->buf)
77 return 0;
79 return 1;
82 /* Allocate extra space if necessary. */
83 static int
84 ensure_space (struct line *linep, int extra)
86 if (linep->max_len < linep->len + extra)
88 linep->max_len = linep->len + extra + 80; /* XXX */
89 linep->buf = grub_realloc (linep->buf, linep->max_len + 1);
90 if (! linep->buf)
91 return 0;
94 return 1;
97 /* Return the number of lines occupied by this line on the screen. */
98 static int
99 get_logical_num_lines (struct line *linep)
101 return (linep->len / GRUB_TERM_ENTRY_WIDTH) + 1;
104 /* Print a line. */
105 static void
106 print_line (struct line *linep, int offset, int start, int y)
108 int i;
109 char *p;
111 grub_gotoxy (GRUB_TERM_LEFT_BORDER_X + GRUB_TERM_MARGIN + start + 1,
112 y + GRUB_TERM_FIRST_ENTRY_Y);
114 for (p = linep->buf + offset + start, i = start;
115 i < GRUB_TERM_ENTRY_WIDTH && offset + i < linep->len;
116 p++, i++)
117 grub_putchar (*p);
119 for (; i < GRUB_TERM_ENTRY_WIDTH; i++)
120 grub_putchar (' ');
122 if (linep->len >= offset + GRUB_TERM_ENTRY_WIDTH)
123 grub_putchar ('\\');
124 else
125 grub_putchar (' ');
128 /* Print an empty line. */
129 static void
130 print_empty_line (int y)
132 int i;
134 grub_gotoxy (GRUB_TERM_LEFT_BORDER_X + GRUB_TERM_MARGIN + 1,
135 y + GRUB_TERM_FIRST_ENTRY_Y);
137 for (i = 0; i < GRUB_TERM_ENTRY_WIDTH + 1; i++)
138 grub_putchar (' ');
141 /* Print an up arrow. */
142 static void
143 print_up (int flag)
145 grub_gotoxy (GRUB_TERM_LEFT_BORDER_X + GRUB_TERM_BORDER_WIDTH,
146 GRUB_TERM_FIRST_ENTRY_Y);
148 if (flag)
149 grub_putcode (GRUB_TERM_DISP_UP);
150 else
151 grub_putchar (' ');
154 /* Print a down arrow. */
155 static void
156 print_down (int flag)
158 grub_gotoxy (GRUB_TERM_LEFT_BORDER_X + GRUB_TERM_BORDER_WIDTH,
159 GRUB_TERM_TOP_BORDER_Y + GRUB_TERM_NUM_ENTRIES);
161 if (flag)
162 grub_putcode (GRUB_TERM_DISP_DOWN);
163 else
164 grub_putchar (' ');
167 /* Draw the lines of the screen SCREEN. */
168 static void
169 update_screen (struct screen *screen, int region_start, int region_column,
170 int up, int down, enum update_mode mode)
172 int up_flag = 0;
173 int down_flag = 0;
174 int y;
175 int i;
176 struct line *linep;
178 /* Check if scrolling is necessary. */
179 if (screen->y < 0 || screen->y >= GRUB_TERM_NUM_ENTRIES)
181 if (screen->y < 0)
182 screen->y = 0;
183 else
184 screen->y = GRUB_TERM_NUM_ENTRIES - 1;
186 region_start = 0;
187 region_column = 0;
188 up = 1;
189 down = 1;
190 mode = ALL_LINES;
193 if (mode != NO_LINE)
195 /* Draw lines. This code is tricky, because this must calculate logical
196 positions. */
197 y = screen->y - screen->column / GRUB_TERM_ENTRY_WIDTH;
198 i = screen->line;
199 linep = screen->lines + i;
200 while (y > 0)
202 i--;
203 linep--;
204 y -= get_logical_num_lines (linep);
207 if (y < 0 || i > 0)
208 up_flag = 1;
212 int column;
214 for (column = 0;
215 column <= linep->len && y < GRUB_TERM_NUM_ENTRIES;
216 column += GRUB_TERM_ENTRY_WIDTH, y++)
218 if (y < 0)
219 continue;
221 if (i == region_start)
223 if (region_column >= column
224 && region_column < column + GRUB_TERM_ENTRY_WIDTH)
225 print_line (linep, column, region_column - column, y);
226 else if (region_column < column)
227 print_line (linep, column, 0, y);
229 else if (i > region_start && mode == ALL_LINES)
230 print_line (linep, column, 0, y);
233 if (y == GRUB_TERM_NUM_ENTRIES)
235 if (column <= linep->len || i + 1 < screen->num_lines)
236 down_flag = 1;
239 linep++;
240 i++;
242 if (mode == ALL_LINES && i == screen->num_lines)
243 for (; y < GRUB_TERM_NUM_ENTRIES; y++)
244 print_empty_line (y);
247 while (y < GRUB_TERM_NUM_ENTRIES);
249 /* Draw up and down arrows. */
250 if (up)
251 print_up (up_flag);
252 if (down)
253 print_down (down_flag);
256 /* Place the cursor. */
257 grub_gotoxy (GRUB_TERM_LEFT_BORDER_X + GRUB_TERM_MARGIN + 1 + screen->x,
258 GRUB_TERM_FIRST_ENTRY_Y + screen->y);
260 grub_refresh ();
263 /* Insert the string S into the screen SCREEN. This updates the cursor
264 position and redraw the screen. Return zero if fails. */
265 static int
266 insert_string (struct screen *screen, char *s, int update)
268 int region_start = screen->num_lines;
269 int region_column = 0;
270 int down = 0;
271 enum update_mode mode = NO_LINE;
273 while (*s)
275 if (*s == '\n')
277 /* LF is special because it creates a new line. */
278 struct line *current_linep;
279 struct line *next_linep;
280 int size;
282 /* Make a new line. */
283 screen->num_lines++;
284 screen->lines = grub_realloc (screen->lines,
285 screen->num_lines
286 * sizeof (struct line));
287 if (! screen->lines)
288 return 0;
290 /* Scroll down. */
291 grub_memmove (screen->lines + screen->line + 2,
292 screen->lines + screen->line + 1,
293 ((screen->num_lines - screen->line - 2)
294 * sizeof (struct line)));
296 if (! init_line (screen->lines + screen->line + 1))
297 return 0;
299 /* Fold the line. */
300 current_linep = screen->lines + screen->line;
301 next_linep = current_linep + 1;
302 size = current_linep->len - screen->column;
304 if (! ensure_space (next_linep, size))
305 return 0;
307 grub_memmove (next_linep->buf,
308 current_linep->buf + screen->column,
309 size);
310 current_linep->len = screen->column;
311 next_linep->len = size;
313 /* Update a dirty region. */
314 if (region_start > screen->line)
316 region_start = screen->line;
317 region_column = screen->column;
320 mode = ALL_LINES;
321 down = 1; /* XXX not optimal. */
323 /* Move the cursor. */
324 screen->column = screen->real_column = 0;
325 screen->line++;
326 screen->x = 0;
327 screen->y++;
329 s++;
331 else
333 /* All but LF. */
334 char *p;
335 struct line *current_linep;
336 int size;
337 int orig_num, new_num;
339 /* Find a string delimited by LF. */
340 p = grub_strchr (s, '\n');
341 if (! p)
342 p = s + grub_strlen (s);
344 /* Insert the string. */
345 current_linep = screen->lines + screen->line;
346 size = p - s;
347 if (! ensure_space (current_linep, size))
348 return 0;
350 grub_memmove (current_linep->buf + screen->column + size,
351 current_linep->buf + screen->column,
352 current_linep->len - screen->column);
353 grub_memmove (current_linep->buf + screen->column,
355 size);
356 orig_num = get_logical_num_lines (current_linep);
357 current_linep->len += size;
358 new_num = get_logical_num_lines (current_linep);
360 /* Update the dirty region. */
361 if (region_start > screen->line)
363 region_start = screen->line;
364 region_column = screen->column;
367 if (orig_num != new_num)
369 mode = ALL_LINES;
370 down = 1; /* XXX not optimal. */
372 else if (mode != ALL_LINES)
373 mode = SINGLE_LINE;
375 /* Move the cursor. */
376 screen->column += size;
377 screen->real_column = screen->column;
378 screen->x += size;
379 screen->y += screen->x / GRUB_TERM_ENTRY_WIDTH;
380 screen->x %= GRUB_TERM_ENTRY_WIDTH;
382 s = p;
386 if (update)
387 update_screen (screen, region_start, region_column, 0, down, mode);
389 return 1;
392 /* Release the resource allocated for SCREEN. */
393 static void
394 destroy_screen (struct screen *screen)
396 int i;
398 if (screen->lines)
399 for (i = 0; i < screen->num_lines; i++)
401 struct line *linep = screen->lines + i;
403 if (linep)
404 grub_free (linep->buf);
407 grub_free (screen->killed_text);
408 grub_free (screen->lines);
409 grub_free (screen);
412 /* Make a new screen. */
413 static struct screen *
414 make_screen (grub_menu_entry_t entry)
416 struct screen *screen;
418 /* Initialize the screen. */
419 screen = grub_zalloc (sizeof (*screen));
420 if (! screen)
421 return 0;
423 screen->num_lines = 1;
424 screen->lines = grub_malloc (sizeof (struct line));
425 if (! screen->lines)
426 goto fail;
428 /* Initialize the first line which must be always present. */
429 if (! init_line (screen->lines))
430 goto fail;
432 insert_string (screen, (char *) entry->sourcecode, 0);
434 /* Reset the cursor position. */
435 screen->column = 0;
436 screen->real_column = 0;
437 screen->line = 0;
438 screen->x = 0;
439 screen->y = 0;
441 return screen;
443 fail:
444 destroy_screen (screen);
445 return 0;
448 static int
449 forward_char (struct screen *screen, int update)
451 struct line *linep;
453 linep = screen->lines + screen->line;
454 if (screen->column < linep->len)
456 screen->column++;
457 screen->x++;
458 if (screen->x == GRUB_TERM_ENTRY_WIDTH)
460 screen->x = 0;
461 screen->y++;
464 else if (screen->num_lines > screen->line + 1)
466 screen->column = 0;
467 screen->line++;
468 screen->x = 0;
469 screen->y++;
472 screen->real_column = screen->column;
474 if (update)
475 update_screen (screen, screen->num_lines, 0, 0, 0, NO_LINE);
476 return 1;
479 static int
480 backward_char (struct screen *screen, int update)
482 if (screen->column > 0)
484 screen->column--;
485 screen->x--;
486 if (screen->x == -1)
488 screen->x = GRUB_TERM_ENTRY_WIDTH - 1;
489 screen->y--;
492 else if (screen->line > 0)
494 struct line *linep;
496 screen->line--;
497 linep = screen->lines + screen->line;
498 screen->column = linep->len;
499 screen->x = screen->column % GRUB_TERM_ENTRY_WIDTH;
500 screen->y--;
503 screen->real_column = screen->column;
505 if (update)
506 update_screen (screen, screen->num_lines, 0, 0, 0, NO_LINE);
508 return 1;
511 static int
512 previous_line (struct screen *screen, int update)
514 if (screen->line > 0)
516 struct line *linep;
517 int dy;
519 /* How many physical lines from the current position
520 to the first physical line? */
521 dy = screen->column / GRUB_TERM_ENTRY_WIDTH;
523 screen->line--;
525 linep = screen->lines + screen->line;
526 if (linep->len < screen->real_column)
527 screen->column = linep->len;
528 else
529 screen->column = screen->real_column;
531 /* How many physical lines from the current position
532 to the last physical line? */
533 dy += (linep->len / GRUB_TERM_ENTRY_WIDTH
534 - screen->column / GRUB_TERM_ENTRY_WIDTH);
536 screen->y -= dy + 1;
537 screen->x = screen->column % GRUB_TERM_ENTRY_WIDTH;
539 else
541 screen->y -= screen->column / GRUB_TERM_ENTRY_WIDTH;
542 screen->column = 0;
543 screen->x = 0;
546 if (update)
547 update_screen (screen, screen->num_lines, 0, 0, 0, NO_LINE);
549 return 1;
552 static int
553 next_line (struct screen *screen, int update)
555 if (screen->line < screen->num_lines - 1)
557 struct line *linep;
558 int dy;
560 /* How many physical lines from the current position
561 to the last physical line? */
562 linep = screen->lines + screen->line;
563 dy = (linep->len / GRUB_TERM_ENTRY_WIDTH
564 - screen->column / GRUB_TERM_ENTRY_WIDTH);
566 screen->line++;
568 linep++;
569 if (linep->len < screen->real_column)
570 screen->column = linep->len;
571 else
572 screen->column = screen->real_column;
574 /* How many physical lines from the current position
575 to the first physical line? */
576 dy += screen->column / GRUB_TERM_ENTRY_WIDTH;
578 screen->y += dy + 1;
579 screen->x = screen->column % GRUB_TERM_ENTRY_WIDTH;
581 else
583 struct line *linep;
585 linep = screen->lines + screen->line;
586 screen->y += (linep->len / GRUB_TERM_ENTRY_WIDTH
587 - screen->column / GRUB_TERM_ENTRY_WIDTH);
588 screen->column = linep->len;
589 screen->x = screen->column % GRUB_TERM_ENTRY_WIDTH;
592 if (update)
593 update_screen (screen, screen->num_lines, 0, 0, 0, NO_LINE);
595 return 1;
598 static int
599 beginning_of_line (struct screen *screen, int update)
601 screen->y -= screen->column / GRUB_TERM_ENTRY_WIDTH;
602 screen->column = screen->real_column = 0;
603 screen->x = 0;
605 if (update)
606 update_screen (screen, screen->num_lines, 0, 0, 0, NO_LINE);
608 return 1;
611 static int
612 end_of_line (struct screen *screen, int update)
614 struct line *linep;
616 linep = screen->lines + screen->line;
617 screen->y += (linep->len / GRUB_TERM_ENTRY_WIDTH
618 - screen->column / GRUB_TERM_ENTRY_WIDTH);
619 screen->column = screen->real_column = linep->len;
620 screen->x = screen->column % GRUB_TERM_ENTRY_WIDTH;
622 if (update)
623 update_screen (screen, screen->num_lines, 0, 0, 0, NO_LINE);
625 return 1;
628 static int
629 delete_char (struct screen *screen, int update)
631 struct line *linep;
632 enum update_mode mode = NO_LINE;
633 int start = screen->num_lines;
634 int column = 0;
635 int down = 0;
637 linep = screen->lines + screen->line;
638 if (linep->len > screen->column)
640 int orig_num, new_num;
642 orig_num = get_logical_num_lines (linep);
644 grub_memmove (linep->buf + screen->column,
645 linep->buf + screen->column + 1,
646 linep->len - screen->column - 1);
647 linep->len--;
649 new_num = get_logical_num_lines (linep);
651 if (orig_num != new_num)
652 mode = ALL_LINES;
653 else
654 mode = SINGLE_LINE;
656 start = screen->line;
657 column = screen->column;
659 else if (screen->num_lines > screen->line + 1)
661 struct line *next_linep;
663 next_linep = linep + 1;
664 if (! ensure_space (linep, next_linep->len))
665 return 0;
667 grub_memmove (linep->buf + linep->len, next_linep->buf, next_linep->len);
668 linep->len += next_linep->len;
670 grub_free (next_linep->buf);
671 grub_memmove (next_linep,
672 next_linep + 1,
673 (screen->num_lines - screen->line - 2)
674 * sizeof (struct line));
675 screen->num_lines--;
677 mode = ALL_LINES;
678 start = screen->line;
679 column = screen->column;
680 down = 1;
683 screen->real_column = screen->column;
685 if (update)
686 update_screen (screen, start, column, 0, down, mode);
688 return 1;
691 static int
692 backward_delete_char (struct screen *screen, int update)
694 int saved_column;
695 int saved_line;
697 saved_column = screen->column;
698 saved_line = screen->line;
700 if (! backward_char (screen, 0))
701 return 0;
703 if (saved_column != screen->column || saved_line != screen->line)
704 if (! delete_char (screen, update))
705 return 0;
707 return 1;
710 static int
711 kill_line (struct screen *screen, int continuous, int update)
713 struct line *linep;
714 char *p;
715 int size;
716 int offset;
718 p = screen->killed_text;
719 if (! continuous && p)
720 p[0] = '\0';
722 linep = screen->lines + screen->line;
723 size = linep->len - screen->column;
725 if (p)
726 offset = grub_strlen (p);
727 else
728 offset = 0;
730 if (size > 0)
732 enum update_mode mode = SINGLE_LINE;
733 int down = 0;
734 int orig_num, new_num;
736 p = grub_realloc (p, offset + size + 1);
737 if (! p)
738 return 0;
740 grub_memmove (p + offset, linep->buf + screen->column, size);
741 p[offset + size - 1] = '\0';
743 screen->killed_text = p;
745 orig_num = get_logical_num_lines (linep);
746 linep->len = screen->column;
747 new_num = get_logical_num_lines (linep);
749 if (orig_num != new_num)
751 mode = ALL_LINES;
752 down = 1;
755 if (update)
756 update_screen (screen, screen->line, screen->column, 0, down, mode);
758 else if (screen->line + 1 < screen->num_lines)
760 p = grub_realloc (p, offset + 1 + 1);
761 if (! p)
762 return 0;
764 p[offset] = '\n';
765 p[offset + 1] = '\0';
767 screen->killed_text = p;
769 return delete_char (screen, update);
772 return 1;
775 static int
776 yank (struct screen *screen, int update)
778 if (screen->killed_text)
779 return insert_string (screen, screen->killed_text, update);
781 return 1;
784 static int
785 open_line (struct screen *screen, int update)
787 int saved_y = screen->y;
789 if (! insert_string (screen, "\n", 0))
790 return 0;
792 if (! backward_char (screen, 0))
793 return 0;
795 screen->y = saved_y;
797 if (update)
798 update_screen (screen, screen->line, screen->column, 0, 1, ALL_LINES);
800 return 1;
803 /* A completion hook to print items. */
804 static void
805 store_completion (const char *item, grub_completion_type_t type, int count)
807 char *p;
809 if (count == 0)
811 /* If this is the first time, print a label. */
812 const char *what;
814 switch (type)
816 case GRUB_COMPLETION_TYPE_COMMAND:
817 what = "commands";
818 break;
819 case GRUB_COMPLETION_TYPE_DEVICE:
820 what = "devices";
821 break;
822 case GRUB_COMPLETION_TYPE_FILE:
823 what = "files";
824 break;
825 case GRUB_COMPLETION_TYPE_PARTITION:
826 what = "partitions";
827 break;
828 case GRUB_COMPLETION_TYPE_ARGUMENT:
829 what = "arguments";
830 break;
831 default:
832 what = "things";
833 break;
836 grub_gotoxy (0, GRUB_TERM_HEIGHT - 3);
837 grub_printf (" Possible %s are:\n ", what);
840 /* Make sure that the completion buffer has enough room. */
841 if (completion_buffer.max_len < (completion_buffer.len
842 + (int) grub_strlen (item) + 1 + 1))
844 grub_size_t new_len;
846 new_len = completion_buffer.len + grub_strlen (item) + 80;
847 p = grub_realloc (completion_buffer.buf, new_len);
848 if (! p)
850 /* Possibly not fatal. */
851 grub_errno = GRUB_ERR_NONE;
852 return;
854 p[completion_buffer.len] = 0;
855 completion_buffer.buf = p;
856 completion_buffer.max_len = new_len;
859 p = completion_buffer.buf + completion_buffer.len;
860 if (completion_buffer.len != 0)
862 *p++ = ' ';
863 completion_buffer.len++;
865 grub_strcpy (p, item);
866 completion_buffer.len += grub_strlen (item);
869 static int
870 complete (struct screen *screen, int continuous, int update)
872 grub_uint16_t pos;
873 char saved_char;
874 struct line *linep;
875 int restore;
876 char *insert;
877 static int count = -1;
879 if (continuous)
880 count++;
881 else
882 count = 0;
884 pos = grub_getxy ();
885 grub_gotoxy (0, GRUB_TERM_HEIGHT - 3);
887 completion_buffer.buf = 0;
888 completion_buffer.len = 0;
889 completion_buffer.max_len = 0;
891 linep = screen->lines + screen->line;
892 saved_char = linep->buf[screen->column];
893 linep->buf[screen->column] = '\0';
895 insert = grub_normal_do_completion (linep->buf, &restore, store_completion);
897 linep->buf[screen->column] = saved_char;
899 if (restore)
901 char *p = completion_buffer.buf;
903 screen->completion_shown = 1;
905 if (p)
907 int num_sections = ((completion_buffer.len + GRUB_TERM_WIDTH - 8 - 1)
908 / (GRUB_TERM_WIDTH - 8));
909 char *endp;
911 p += (count % num_sections) * (GRUB_TERM_WIDTH - 8);
912 endp = p + (GRUB_TERM_WIDTH - 8);
914 if (p != completion_buffer.buf)
915 grub_putcode (GRUB_TERM_DISP_LEFT);
916 else
917 grub_putchar (' ');
919 while (*p && p < endp)
920 grub_putchar (*p++);
922 if (*p)
923 grub_putcode (GRUB_TERM_DISP_RIGHT);
927 grub_gotoxy (pos >> 8, pos & 0xFF);
929 if (insert)
931 insert_string (screen, insert, update);
932 count = -1;
933 grub_free (insert);
935 else if (update)
936 grub_refresh ();
938 grub_free (completion_buffer.buf);
939 return 1;
942 /* Clear displayed completions. */
943 static void
944 clear_completions (void)
946 grub_uint16_t pos;
947 int i, j;
949 pos = grub_getxy ();
950 grub_gotoxy (0, GRUB_TERM_HEIGHT - 3);
952 for (i = 0; i < 2; i++)
954 for (j = 0; j < GRUB_TERM_WIDTH - 1; j++)
955 grub_putchar (' ');
956 grub_putchar ('\n');
959 grub_gotoxy (pos >> 8, pos & 0xFF);
960 grub_refresh ();
963 /* Execute the command list in the screen SCREEN. */
964 static int
965 run (struct screen *screen)
967 int currline = 0;
968 char *nextline;
970 auto grub_err_t editor_getline (char **line, int cont);
971 grub_err_t editor_getline (char **line, int cont __attribute__ ((unused)))
973 struct line *linep = screen->lines + currline;
974 char *p;
976 if (currline > screen->num_lines)
978 *line = 0;
979 return 0;
982 /* Trim down space characters. */
983 for (p = linep->buf + linep->len - 1;
984 p >= linep->buf && grub_isspace (*p);
985 p--)
987 *++p = '\0';
989 linep->len = p - linep->buf;
990 for (p = linep->buf; grub_isspace (*p); p++)
992 *line = grub_strdup (p);
993 currline++;
994 return 0;
997 grub_cls ();
998 grub_printf (" Booting a command list\n\n");
1001 /* Execute the script, line for line. */
1002 while (currline < screen->num_lines)
1004 editor_getline (&nextline, 0);
1005 if (grub_parser_get_current ()->parse_line (nextline, editor_getline))
1006 break;
1009 if (grub_errno == GRUB_ERR_NONE && grub_loader_is_loaded ())
1010 /* Implicit execution of boot, only if something is loaded. */
1011 grub_command_execute ("boot", 0, 0);
1013 if (grub_errno != GRUB_ERR_NONE)
1015 grub_print_error ();
1016 grub_errno = GRUB_ERR_NONE;
1017 grub_wait_after_message ();
1020 return 1;
1023 /* Edit a menu entry with an Emacs-like interface. */
1024 void
1025 grub_menu_entry_run (grub_menu_entry_t entry)
1027 struct screen *screen;
1028 int prev_c;
1030 screen = make_screen (entry);
1031 if (! screen)
1032 return;
1034 refresh:
1035 /* Draw the screen. */
1036 grub_menu_init_page (0, 1);
1037 update_screen (screen, 0, 0, 1, 1, ALL_LINES);
1038 grub_setcursor (1);
1039 prev_c = '\0';
1041 while (1)
1043 int c = GRUB_TERM_ASCII_CHAR (grub_getkey ());
1045 if (screen->completion_shown)
1047 clear_completions ();
1048 screen->completion_shown = 0;
1051 switch (c)
1053 case 16: /* C-p */
1054 if (! previous_line (screen, 1))
1055 goto fail;
1056 break;
1058 case 14: /* C-n */
1059 if (! next_line (screen, 1))
1060 goto fail;
1061 break;
1063 case 6: /* C-f */
1064 if (! forward_char (screen, 1))
1065 goto fail;
1066 break;
1068 case 2: /* C-b */
1069 if (! backward_char (screen, 1))
1070 goto fail;
1071 break;
1073 case 1: /* C-a */
1074 if (! beginning_of_line (screen, 1))
1075 goto fail;
1076 break;
1078 case 5: /* C-e */
1079 if (! end_of_line (screen, 1))
1080 goto fail;
1081 break;
1083 case '\t': /* C-i */
1084 if (! complete (screen, prev_c == c, 1))
1085 goto fail;
1086 break;
1088 case 4: /* C-d */
1089 if (! delete_char (screen, 1))
1090 goto fail;
1091 break;
1093 case 8: /* C-h */
1094 if (! backward_delete_char (screen, 1))
1095 goto fail;
1096 break;
1098 case 11: /* C-k */
1099 if (! kill_line (screen, prev_c == c, 1))
1100 goto fail;
1101 break;
1103 case 21: /* C-u */
1104 /* FIXME: What behavior is good for this key? */
1105 break;
1107 case 25: /* C-y */
1108 if (! yank (screen, 1))
1109 goto fail;
1110 break;
1112 case 12: /* C-l */
1113 /* FIXME: centering. */
1114 goto refresh;
1116 case 15: /* C-o */
1117 if (! open_line (screen, 1))
1118 goto fail;
1119 break;
1121 case '\n':
1122 case '\r':
1123 if (! insert_string (screen, "\n", 1))
1124 goto fail;
1125 break;
1127 case '\e':
1128 destroy_screen (screen);
1129 return;
1131 case 3: /* C-c */
1132 grub_cmdline_run (1);
1133 goto refresh;
1135 case 24: /* C-x */
1136 if (! run (screen))
1137 goto fail;
1138 goto refresh;
1140 case 18: /* C-r */
1141 case 19: /* C-s */
1142 case 20: /* C-t */
1143 /* FIXME */
1144 break;
1146 default:
1147 if (grub_isprint (c))
1149 char buf[2];
1151 buf[0] = c;
1152 buf[1] = '\0';
1153 if (! insert_string (screen, buf, 1))
1154 goto fail;
1156 break;
1159 prev_c = c;
1162 fail:
1163 destroy_screen (screen);
1165 grub_cls ();
1166 grub_print_error ();
1167 grub_errno = GRUB_ERR_NONE;
1168 grub_printf ("\nPress any key to continue...");
1169 (void) grub_getkey ();