2009-09-24 Robert Millan <rmh.grub@aybabtu.com>
[grub2/phcoder/solaris.git] / normal / cmdline.c
blob7a5b6ec84607246c5aeacfbf01bd4853675a0266
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 /* A completion hook to print items. */
136 static void
137 print_completion (const char *item, grub_completion_type_t type, int count)
139 if (count == 0)
141 /* If this is the first time, print a label. */
142 const char *what;
144 switch (type)
146 case GRUB_COMPLETION_TYPE_COMMAND:
147 what = "commands";
148 break;
149 case GRUB_COMPLETION_TYPE_DEVICE:
150 what = "devices";
151 break;
152 case GRUB_COMPLETION_TYPE_FILE:
153 what = "files";
154 break;
155 case GRUB_COMPLETION_TYPE_PARTITION:
156 what = "partitions";
157 break;
158 case GRUB_COMPLETION_TYPE_ARGUMENT:
159 what = "arguments";
160 break;
161 default:
162 what = "things";
163 break;
166 grub_printf ("\nPossible %s are:\n", what);
169 if (type == GRUB_COMPLETION_TYPE_PARTITION)
171 grub_normal_print_device_info (item);
172 grub_errno = GRUB_ERR_NONE;
174 else
175 grub_printf (" %s", item);
178 /* Get a command-line. If ECHO_CHAR is not zero, echo it instead of input
179 characters. If READLINE is non-zero, readline-like key bindings are
180 available. If ESC is pushed, return zero, otherwise return non-zero. */
181 /* FIXME: The dumb interface is not supported yet. */
183 grub_cmdline_get (const char *prompt, char cmdline[], unsigned max_len,
184 int echo_char, int readline, int history)
186 unsigned xpos, ypos, ystart;
187 grub_size_t lpos, llen;
188 grub_size_t plen;
189 char buf[max_len];
190 int key;
191 int histpos = 0;
192 auto void cl_insert (const char *str);
193 auto void cl_delete (unsigned len);
194 auto void cl_print (int pos, int c);
195 auto void cl_set_pos (void);
197 void cl_set_pos (void)
199 xpos = (plen + lpos) % 79;
200 ypos = ystart + (plen + lpos) / 79;
201 grub_gotoxy (xpos, ypos);
203 grub_refresh ();
206 void cl_print (int pos, int c)
208 char *p;
210 for (p = buf + pos; *p; p++)
212 if (xpos++ > 78)
214 grub_putchar ('\n');
216 xpos = 1;
217 if (ypos == (unsigned) (grub_getxy () & 0xFF))
218 ystart--;
219 else
220 ypos++;
223 if (c)
224 grub_putchar (c);
225 else
226 grub_putchar (*p);
230 void cl_insert (const char *str)
232 grub_size_t len = grub_strlen (str);
234 if (len + llen < max_len)
236 grub_memmove (buf + lpos + len, buf + lpos, llen - lpos + 1);
237 grub_memmove (buf + lpos, str, len);
239 llen += len;
240 lpos += len;
241 cl_print (lpos - len, echo_char);
242 cl_set_pos ();
245 grub_refresh ();
248 void cl_delete (unsigned len)
250 if (lpos + len <= llen)
252 grub_size_t saved_lpos = lpos;
254 lpos = llen - len;
255 cl_set_pos ();
256 cl_print (lpos, ' ');
257 lpos = saved_lpos;
258 cl_set_pos ();
260 grub_memmove (buf + lpos, buf + lpos + len, llen - lpos + 1);
261 llen -= len;
262 cl_print (lpos, echo_char);
263 cl_set_pos ();
266 grub_refresh ();
269 plen = grub_strlen (prompt);
270 lpos = llen = 0;
271 buf[0] = '\0';
273 if ((grub_getxy () >> 8) != 0)
274 grub_putchar ('\n');
276 grub_printf ("%s", prompt);
278 xpos = plen;
279 ystart = ypos = (grub_getxy () & 0xFF);
281 cl_insert (cmdline);
283 if (history && hist_used == 0)
284 grub_history_add (buf);
286 while ((key = GRUB_TERM_ASCII_CHAR (grub_getkey ())) != '\n' && key != '\r')
288 if (readline)
290 switch (key)
292 case 1: /* Ctrl-a */
293 lpos = 0;
294 cl_set_pos ();
295 break;
297 case 2: /* Ctrl-b */
298 if (lpos > 0)
300 lpos--;
301 cl_set_pos ();
303 break;
305 case 5: /* Ctrl-e */
306 lpos = llen;
307 cl_set_pos ();
308 break;
310 case 6: /* Ctrl-f */
311 if (lpos < llen)
313 lpos++;
314 cl_set_pos ();
316 break;
318 case 9: /* Ctrl-i or TAB */
320 char *insert;
321 int restore;
323 /* Backup the next character and make it 0 so it will
324 be easy to use string functions. */
325 char backup = buf[lpos];
326 buf[lpos] = '\0';
329 insert = grub_normal_do_completion (buf, &restore,
330 print_completion);
331 /* Restore the original string. */
332 buf[lpos] = backup;
334 if (restore)
336 /* Restore the prompt. */
337 grub_printf ("\n%s%s", prompt, buf);
338 xpos = plen;
339 ystart = ypos = (grub_getxy () & 0xFF);
342 if (insert)
344 cl_insert (insert);
345 grub_free (insert);
348 break;
350 case 11: /* Ctrl-k */
351 if (lpos < llen)
353 if (kill_buf)
354 grub_free (kill_buf);
356 kill_buf = grub_strdup (buf + lpos);
357 grub_errno = GRUB_ERR_NONE;
359 cl_delete (llen - lpos);
361 break;
363 case 14: /* Ctrl-n */
365 char *hist;
367 lpos = 0;
369 if (histpos > 0)
371 grub_history_replace (histpos, buf);
372 histpos--;
375 cl_delete (llen);
376 hist = grub_history_get (histpos);
377 cl_insert (hist);
379 break;
381 case 16: /* Ctrl-p */
383 char *hist;
385 lpos = 0;
387 if (histpos < hist_used - 1)
389 grub_history_replace (histpos, buf);
390 histpos++;
393 cl_delete (llen);
394 hist = grub_history_get (histpos);
396 cl_insert (hist);
398 break;
400 case 21: /* Ctrl-u */
401 if (lpos > 0)
403 grub_size_t n = lpos;
405 if (kill_buf)
406 grub_free (kill_buf);
408 kill_buf = grub_malloc (n + 1);
409 grub_errno = GRUB_ERR_NONE;
410 if (kill_buf)
412 grub_memcpy (kill_buf, buf, n);
413 kill_buf[n] = '\0';
416 lpos = 0;
417 cl_set_pos ();
418 cl_delete (n);
420 break;
422 case 25: /* Ctrl-y */
423 if (kill_buf)
424 cl_insert (kill_buf);
425 break;
429 switch (key)
431 case '\e':
432 return 0;
434 case '\b':
435 if (lpos > 0)
437 lpos--;
438 cl_set_pos ();
440 else
441 break;
442 /* fall through */
444 case 4: /* Ctrl-d */
445 if (lpos < llen)
446 cl_delete (1);
447 break;
449 default:
450 if (grub_isprint (key))
452 char str[2];
454 str[0] = key;
455 str[1] = '\0';
456 cl_insert (str);
458 break;
462 grub_putchar ('\n');
463 grub_refresh ();
465 /* If ECHO_CHAR is NUL, remove leading spaces. */
466 lpos = 0;
467 if (! echo_char)
468 while (buf[lpos] == ' ')
469 lpos++;
471 if (history)
473 histpos = 0;
474 if (grub_strlen (buf) > 0)
476 grub_history_replace (histpos, buf);
477 grub_history_add ("");
481 grub_memcpy (cmdline, buf + lpos, llen - lpos + 1);
483 return 1;