make the linux-ppc packags be in synch with other platforms
[tangerine.git] / arch / common / boot / grub2 / normal / cmdline.c
blob8ca83f6bbdde549a9c2d39e9ca6bdbb39fc9aa98
1 /*
2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2007 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/misc.h>
21 #include <grub/term.h>
22 #include <grub/err.h>
23 #include <grub/types.h>
24 #include <grub/mm.h>
25 #include <grub/partition.h>
26 #include <grub/disk.h>
27 #include <grub/file.h>
28 #include <grub/env.h>
30 static char *kill_buf;
32 static int hist_size;
33 static char **hist_lines = 0;
34 static int hist_pos = 0;
35 static int hist_end = 0;
36 static int hist_used = 0;
38 grub_err_t
39 grub_set_history (int newsize)
41 char **old_hist_lines = hist_lines;
42 hist_lines = grub_malloc (sizeof (char *) * newsize);
44 /* Copy the old lines into the new buffer. */
45 if (old_hist_lines)
47 /* Remove the lines that don't fit in the new buffer. */
48 if (newsize < hist_used)
50 int i;
51 int delsize = hist_used - newsize;
52 hist_used = newsize;
54 for (i = 1; i <= delsize; i++)
56 int pos = hist_end - i;
57 if (pos < 0)
58 pos += hist_size;
59 grub_free (old_hist_lines[pos]);
62 hist_end -= delsize;
63 if (hist_end < 0)
64 hist_end += hist_size;
67 if (hist_pos < hist_end)
68 grub_memmove (hist_lines, old_hist_lines + hist_pos,
69 (hist_end - hist_pos) * sizeof (char *));
70 else if (hist_used)
72 /* Copy the older part. */
73 grub_memmove (hist_lines, old_hist_lines + hist_pos,
74 (hist_size - hist_pos) * sizeof (char *));
76 /* Copy the newer part. */
77 grub_memmove (hist_lines + hist_size - hist_pos, old_hist_lines,
78 hist_end * sizeof (char *));
82 grub_free (old_hist_lines);
84 hist_size = newsize;
85 hist_pos = 0;
86 hist_end = hist_used;
87 return 0;
90 /* Get the entry POS from the history where `0' is the newest
91 entry. */
92 static char *
93 grub_history_get (int pos)
95 pos = (hist_pos + pos) % hist_size;
96 return hist_lines[pos];
100 /* Insert a new history line S on the top of the history. */
101 static void
102 grub_history_add (char *s)
104 /* Remove the oldest entry in the history to make room for a new
105 entry. */
106 if (hist_used + 1 > hist_size)
108 hist_end--;
109 if (hist_end < 0)
110 hist_end = hist_size + hist_end;
112 grub_free (hist_lines[hist_end]);
114 else
115 hist_used++;
117 /* Move to the next position. */
118 hist_pos--;
119 if (hist_pos < 0)
120 hist_pos = hist_size + hist_pos;
122 /* Insert into history. */
123 hist_lines[hist_pos] = grub_strdup (s);
126 /* Replace the history entry on position POS with the string S. */
127 static void
128 grub_history_replace (int pos, char *s)
130 pos = (hist_pos + pos) % hist_size;
131 grub_free (hist_lines[pos]);
132 hist_lines[pos] = grub_strdup (s);
135 void
136 grub_cmdline_run (int nested)
138 grub_normal_init_page ();
139 grub_setcursor (1);
141 grub_printf ("\
142 [ Minimal BASH-like line editing is supported. For the first word, TAB\n\
143 lists possible command completions. Anywhere else TAB lists possible\n\
144 device/file completions.%s ]\n\n",
145 nested ? " ESC at any time exits." : "");
147 while (1)
149 static char cmdline[GRUB_MAX_CMDLINE];
151 grub_print_error ();
152 grub_errno = GRUB_ERR_NONE;
153 cmdline[0] = '\0';
155 if (! grub_cmdline_get ("grub> ", cmdline, sizeof (cmdline), 0, 1)
156 && nested)
157 return;
159 if (! *cmdline)
160 continue;
162 grub_command_execute (cmdline, 1);
166 /* A completion hook to print items. */
167 static void
168 print_completion (const char *item, grub_completion_type_t type, int count)
170 if (count == 0)
172 /* If this is the first time, print a label. */
173 const char *what;
175 switch (type)
177 case GRUB_COMPLETION_TYPE_COMMAND:
178 what = "commands";
179 break;
180 case GRUB_COMPLETION_TYPE_DEVICE:
181 what = "devices";
182 break;
183 case GRUB_COMPLETION_TYPE_FILE:
184 what = "files";
185 break;
186 case GRUB_COMPLETION_TYPE_PARTITION:
187 what = "partitions";
188 break;
189 case GRUB_COMPLETION_TYPE_ARGUMENT:
190 what = "arguments";
191 break;
192 default:
193 what = "things";
194 break;
197 grub_printf ("\nPossible %s are:\n", what);
200 if (type == GRUB_COMPLETION_TYPE_PARTITION)
202 grub_normal_print_device_info (item);
203 grub_errno = GRUB_ERR_NONE;
205 else
206 grub_printf (" %s", item);
209 /* Get a command-line. If ECHO_CHAR is not zero, echo it instead of input
210 characters. If READLINE is non-zero, readline-like key bindings are
211 available. If ESC is pushed, return zero, otherwise return non-zero. */
212 /* FIXME: The dumb interface is not supported yet. */
214 grub_cmdline_get (const char *prompt, char cmdline[], unsigned max_len,
215 int echo_char, int readline)
217 unsigned xpos, ypos, ystart;
218 grub_size_t lpos, llen;
219 grub_size_t plen;
220 char buf[max_len];
221 int key;
222 int histpos = 0;
223 auto void cl_insert (const char *str);
224 auto void cl_delete (unsigned len);
225 auto void cl_print (int pos, int c);
226 auto void cl_set_pos (void);
228 void cl_set_pos (void)
230 xpos = (plen + lpos) % 79;
231 ypos = ystart + (plen + lpos) / 79;
232 grub_gotoxy (xpos, ypos);
234 grub_refresh ();
237 void cl_print (int pos, int c)
239 char *p;
241 for (p = buf + pos; *p; p++)
243 if (xpos++ > 78)
245 grub_putchar ('\n');
247 xpos = 1;
248 if (ypos == (unsigned) (grub_getxy () & 0xFF))
249 ystart--;
250 else
251 ypos++;
254 if (c)
255 grub_putchar (c);
256 else
257 grub_putchar (*p);
261 void cl_insert (const char *str)
263 grub_size_t len = grub_strlen (str);
265 if (len + llen < max_len)
267 grub_memmove (buf + lpos + len, buf + lpos, llen - lpos + 1);
268 grub_memmove (buf + lpos, str, len);
270 llen += len;
271 lpos += len;
272 cl_print (lpos - len, echo_char);
273 cl_set_pos ();
276 grub_refresh ();
279 void cl_delete (unsigned len)
281 if (lpos + len <= llen)
283 grub_size_t saved_lpos = lpos;
285 lpos = llen - len;
286 cl_set_pos ();
287 cl_print (lpos, ' ');
288 lpos = saved_lpos;
289 cl_set_pos ();
291 grub_memmove (buf + lpos, buf + lpos + len, llen - lpos + 1);
292 llen -= len;
293 cl_print (lpos, echo_char);
294 cl_set_pos ();
297 grub_refresh ();
300 plen = grub_strlen (prompt);
301 lpos = llen = 0;
302 buf[0] = '\0';
304 if ((grub_getxy () >> 8) != 0)
305 grub_putchar ('\n');
307 grub_printf (prompt);
309 xpos = plen;
310 ystart = ypos = (grub_getxy () & 0xFF);
312 cl_insert (cmdline);
314 if (hist_used == 0)
315 grub_history_add (buf);
317 while ((key = GRUB_TERM_ASCII_CHAR (grub_getkey ())) != '\n' && key != '\r')
319 if (readline)
321 switch (key)
323 case 1: /* Ctrl-a */
324 lpos = 0;
325 cl_set_pos ();
326 break;
328 case 2: /* Ctrl-b */
329 if (lpos > 0)
331 lpos--;
332 cl_set_pos ();
334 break;
336 case 5: /* Ctrl-e */
337 lpos = llen;
338 cl_set_pos ();
339 break;
341 case 6: /* Ctrl-f */
342 if (lpos < llen)
344 lpos++;
345 cl_set_pos ();
347 break;
349 case 9: /* Ctrl-i or TAB */
351 char *insert;
352 int restore;
354 /* Backup the next character and make it 0 so it will
355 be easy to use string functions. */
356 char backup = buf[lpos];
357 buf[lpos] = '\0';
360 insert = grub_normal_do_completion (buf, &restore,
361 print_completion);
362 /* Restore the original string. */
363 buf[lpos] = backup;
365 if (restore)
367 /* Restore the prompt. */
368 grub_printf ("\n%s%s", prompt, buf);
369 xpos = plen;
370 ystart = ypos = (grub_getxy () & 0xFF);
373 if (insert)
375 cl_insert (insert);
376 grub_free (insert);
379 break;
381 case 11: /* Ctrl-k */
382 if (lpos < llen)
384 if (kill_buf)
385 grub_free (kill_buf);
387 kill_buf = grub_strdup (buf + lpos);
388 grub_errno = GRUB_ERR_NONE;
390 cl_delete (llen - lpos);
392 break;
394 case 14: /* Ctrl-n */
396 char *hist;
398 lpos = 0;
400 if (histpos > 0)
402 grub_history_replace (histpos, buf);
403 histpos--;
406 cl_delete (llen);
407 hist = grub_history_get (histpos);
408 cl_insert (hist);
410 break;
412 case 16: /* Ctrl-p */
414 char *hist;
416 lpos = 0;
418 if (histpos < hist_used - 1)
420 grub_history_replace (histpos, buf);
421 histpos++;
424 cl_delete (llen);
425 hist = grub_history_get (histpos);
427 cl_insert (hist);
429 break;
431 case 21: /* Ctrl-u */
432 if (lpos > 0)
434 grub_size_t n = lpos;
436 if (kill_buf)
437 grub_free (kill_buf);
439 kill_buf = grub_malloc (n + 1);
440 grub_errno = GRUB_ERR_NONE;
441 if (kill_buf)
443 grub_memcpy (kill_buf, buf, n);
444 kill_buf[n] = '\0';
447 lpos = 0;
448 cl_set_pos ();
449 cl_delete (n);
451 break;
453 case 25: /* Ctrl-y */
454 if (kill_buf)
455 cl_insert (kill_buf);
456 break;
460 switch (key)
462 case '\e':
463 return 0;
465 case '\b':
466 if (lpos > 0)
468 lpos--;
469 cl_set_pos ();
471 else
472 break;
473 /* fall through */
475 case 4: /* Ctrl-d */
476 if (lpos < llen)
477 cl_delete (1);
478 break;
480 default:
481 if (grub_isprint (key))
483 char str[2];
485 str[0] = key;
486 str[1] = '\0';
487 cl_insert (str);
489 break;
493 grub_putchar ('\n');
494 grub_refresh ();
496 /* If ECHO_CHAR is NUL, remove leading spaces. */
497 lpos = 0;
498 if (! echo_char)
499 while (buf[lpos] == ' ')
500 lpos++;
502 histpos = 0;
503 if (grub_strlen (buf) > 0)
505 grub_history_replace (histpos, buf);
506 grub_history_add ("");
509 grub_memcpy (cmdline, buf + lpos, llen - lpos + 1);
511 return 1;