2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 1999,2000,2001,2002,2003,2004,2005,2007,2009 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>
23 #include <grub/types.h>
25 #include <grub/partition.h>
26 #include <grub/disk.h>
27 #include <grub/file.h>
29 #include <grub/i18n.h>
30 #include <grub/charset.h>
32 static grub_uint32_t *kill_buf;
35 static grub_uint32_t **hist_lines = 0;
36 static int hist_pos = 0;
37 static int hist_end = 0;
38 static int hist_used = 0;
41 grub_set_history (int newsize)
43 grub_uint32_t **old_hist_lines = hist_lines;
44 hist_lines = grub_malloc (sizeof (grub_uint32_t *) * newsize);
46 /* Copy the old lines into the new buffer. */
49 /* Remove the lines that don't fit in the new buffer. */
50 if (newsize < hist_used)
53 grub_size_t delsize = hist_used - newsize;
56 for (i = 1; i < delsize + 1; i++)
58 grub_ssize_t pos = hist_end - i;
61 grub_free (old_hist_lines[pos]);
66 hist_end += hist_size;
69 if (hist_pos < hist_end)
70 grub_memmove (hist_lines, old_hist_lines + hist_pos,
71 (hist_end - hist_pos) * sizeof (grub_uint32_t *));
74 /* Copy the older part. */
75 grub_memmove (hist_lines, old_hist_lines + hist_pos,
76 (hist_size - hist_pos) * sizeof (grub_uint32_t *));
78 /* Copy the newer part. */
79 grub_memmove (hist_lines + hist_size - hist_pos, old_hist_lines,
80 hist_end * sizeof (grub_uint32_t *));
84 grub_free (old_hist_lines);
92 /* Get the entry POS from the history where `0' is the newest
94 static grub_uint32_t *
95 grub_history_get (unsigned pos)
97 pos = (hist_pos + pos) % hist_size;
98 return hist_lines[pos];
102 strlen_ucs4 (const grub_uint32_t *s)
104 const grub_uint32_t *p = s;
112 /* Replace the history entry on position POS with the string S. */
114 grub_history_set (int pos, grub_uint32_t *s, grub_size_t len)
116 grub_free (hist_lines[pos]);
117 hist_lines[pos] = grub_malloc ((len + 1) * sizeof (grub_uint32_t));
118 if (!hist_lines[pos])
121 grub_errno = GRUB_ERR_NONE;
124 grub_memcpy (hist_lines[pos], s, len * sizeof (grub_uint32_t));
125 hist_lines[pos][len] = 0;
128 /* Insert a new history line S on the top of the history. */
130 grub_history_add (grub_uint32_t *s, grub_size_t len)
132 /* Remove the oldest entry in the history to make room for a new
134 if (hist_used + 1 > hist_size)
138 hist_end = hist_size + hist_end;
140 grub_free (hist_lines[hist_end]);
145 /* Move to the next position. */
148 hist_pos = hist_size + hist_pos;
150 /* Insert into history. */
151 hist_lines[hist_pos] = NULL;
152 grub_history_set (hist_pos, s, len);
155 /* Replace the history entry on position POS with the string S. */
157 grub_history_replace (unsigned pos, grub_uint32_t *s, grub_size_t len)
159 grub_history_set ((hist_pos + pos) % hist_size, s, len);
162 /* A completion hook to print items. */
164 print_completion (const char *item, grub_completion_type_t type, int count)
168 /* If this is the first time, print a label. */
173 case GRUB_COMPLETION_TYPE_COMMAND:
174 grub_puts_ (N_("Possible commands are:"));
176 case GRUB_COMPLETION_TYPE_DEVICE:
177 grub_puts_ (N_("Possible devices are:"));
179 case GRUB_COMPLETION_TYPE_FILE:
180 grub_puts_ (N_("Possible files are:"));
182 case GRUB_COMPLETION_TYPE_PARTITION:
183 grub_puts_ (N_("Possible partitions are:"));
185 case GRUB_COMPLETION_TYPE_ARGUMENT:
186 grub_puts_ (N_("Possible arguments are:"));
189 /* TRANSLATORS: this message is used if none of above matches.
190 This shouldn't happen but please use the general term for
191 "thing" or "object". */
192 grub_puts_ (N_("Possible things are:"));
198 if (type == GRUB_COMPLETION_TYPE_PARTITION)
200 grub_normal_print_device_info (item);
201 grub_errno = GRUB_ERR_NONE;
204 grub_printf (" %s", item);
209 struct grub_term_coordinate pos;
210 unsigned ystart, width, height;
212 struct grub_term_output *term;
216 cl_set_pos (struct cmdline_term *cl_term, grub_size_t lpos)
218 cl_term->pos.x = (cl_term->prompt_len + lpos) % cl_term->width;
219 cl_term->pos.y = cl_term->ystart
220 + (cl_term->prompt_len + lpos) / cl_term->width;
221 grub_term_gotoxy (cl_term->term, cl_term->pos);
225 cl_set_pos_all (struct cmdline_term *cl_terms, unsigned nterms,
229 for (i = 0; i < nterms; i++)
230 cl_set_pos (&cl_terms[i], lpos);
233 static inline void __attribute__ ((always_inline))
234 cl_print (struct cmdline_term *cl_term, grub_uint32_t c,
235 grub_uint32_t *start, grub_uint32_t *end)
239 for (p = start; p < end; p++)
242 grub_putcode (c, cl_term->term);
244 grub_putcode (*p, cl_term->term);
246 if (cl_term->pos.x >= cl_term->width - 1)
249 if (cl_term->pos.y >= (unsigned) (cl_term->height - 1))
253 grub_putcode ('\n', cl_term->term);
259 cl_print_all (struct cmdline_term *cl_terms, unsigned nterms,
260 grub_uint32_t c, grub_uint32_t *start, grub_uint32_t *end)
263 for (i = 0; i < nterms; i++)
264 cl_print (&cl_terms[i], c, start, end);
268 init_clterm (struct cmdline_term *cl_term_cur)
270 cl_term_cur->pos.x = cl_term_cur->prompt_len;
271 cl_term_cur->pos.y = grub_term_getxy (cl_term_cur->term).y;
272 cl_term_cur->ystart = cl_term_cur->pos.y;
273 cl_term_cur->width = grub_term_width (cl_term_cur->term);
274 cl_term_cur->height = grub_term_height (cl_term_cur->term);
279 cl_delete (struct cmdline_term *cl_terms, unsigned nterms,
281 grub_size_t lpos, grub_size_t *llen, unsigned len)
283 if (lpos + len <= (*llen))
285 cl_set_pos_all (cl_terms, nterms, (*llen) - len);
286 cl_print_all (cl_terms, nterms, ' ', buf + (*llen) - len, buf + (*llen));
288 cl_set_pos_all (cl_terms, nterms, lpos);
290 grub_memmove (buf + lpos, buf + lpos + len,
291 sizeof (grub_uint32_t) * ((*llen) - lpos + 1));
293 cl_print_all (cl_terms, nterms, 0, buf + lpos, buf + (*llen));
294 cl_set_pos_all (cl_terms, nterms, lpos);
300 cl_insert (struct cmdline_term *cl_terms, unsigned nterms,
301 grub_size_t *lpos, grub_size_t *llen,
302 grub_size_t *max_len, grub_uint32_t **buf,
303 const grub_uint32_t *str)
305 grub_size_t len = strlen_ucs4 (str);
307 if (len + (*llen) >= (*max_len))
311 nbuf = grub_realloc ((*buf), sizeof (grub_uint32_t) * (*max_len));
317 grub_errno = GRUB_ERR_NONE;
322 if (len + (*llen) < (*max_len))
324 grub_memmove ((*buf) + (*lpos) + len, (*buf) + (*lpos),
325 ((*llen) - (*lpos) + 1) * sizeof (grub_uint32_t));
326 grub_memmove ((*buf) + (*lpos), str, len * sizeof (grub_uint32_t));
329 cl_set_pos_all (cl_terms, nterms, (*lpos));
330 cl_print_all (cl_terms, nterms, 0, *buf + (*lpos), *buf + (*llen));
332 cl_set_pos_all (cl_terms, nterms, (*lpos));
337 /* Get a command-line. If ESC is pushed, return zero,
338 otherwise return command line. */
339 /* FIXME: The dumb interface is not supported yet. */
341 grub_cmdline_get (const char *prompt_translated)
343 grub_size_t lpos, llen;
345 grub_size_t max_len = 256;
348 struct cmdline_term *cl_terms;
352 buf = grub_malloc (max_len * sizeof (grub_uint32_t));
360 grub_term_output_t term;
362 FOR_ACTIVE_TERM_OUTPUTS(term)
363 if ((grub_term_getxy (term).x) != 0)
364 grub_putcode ('\n', term);
366 grub_xputs (prompt_translated);
368 grub_normal_reset_more ();
371 struct cmdline_term *cl_term_cur;
372 struct grub_term_output *cur;
373 grub_uint32_t *unicode_msg;
374 grub_size_t msg_len = grub_strlen (prompt_translated) + 3;
377 FOR_ACTIVE_TERM_OUTPUTS(cur)
380 cl_terms = grub_malloc (sizeof (cl_terms[0]) * nterms);
386 cl_term_cur = cl_terms;
388 unicode_msg = grub_malloc (msg_len * sizeof (grub_uint32_t));
392 grub_free (cl_terms);
395 msg_len = grub_utf8_to_ucs4 (unicode_msg, msg_len - 1,
396 (grub_uint8_t *) prompt_translated, -1, 0);
397 unicode_msg[msg_len++] = ' ';
399 FOR_ACTIVE_TERM_OUTPUTS(cur)
401 cl_term_cur->term = cur;
402 cl_term_cur->prompt_len = grub_getstringwidth (unicode_msg,
403 unicode_msg + msg_len,
405 init_clterm (cl_term_cur);
408 grub_free (unicode_msg);
412 grub_history_add (buf, llen);
416 while ((key = grub_getkey ()) != '\n' && key != '\r')
420 case GRUB_TERM_CTRL | 'a':
421 case GRUB_TERM_KEY_HOME:
423 cl_set_pos_all (cl_terms, nterms, lpos);
426 case GRUB_TERM_CTRL | 'b':
427 case GRUB_TERM_KEY_LEFT:
431 cl_set_pos_all (cl_terms, nterms, lpos);
435 case GRUB_TERM_CTRL | 'e':
436 case GRUB_TERM_KEY_END:
438 cl_set_pos_all (cl_terms, nterms, lpos);
441 case GRUB_TERM_CTRL | 'f':
442 case GRUB_TERM_KEY_RIGHT:
446 cl_set_pos_all (cl_terms, nterms, lpos);
450 case GRUB_TERM_CTRL | 'i':
461 bufu8 = grub_ucs4_to_utf8_alloc (buf, lpos);
466 grub_errno = GRUB_ERR_NONE;
470 insertu8 = grub_normal_do_completion (bufu8, &restore,
474 grub_normal_reset_more ();
480 /* Restore the prompt. */
482 grub_xputs (prompt_translated);
485 for (i = 0; i < nterms; i++)
486 init_clterm (&cl_terms[i]);
488 cl_print_all (cl_terms, nterms, 0, buf, buf + llen);
493 grub_size_t insertlen;
495 grub_uint32_t *insert;
497 insertlen = grub_strlen (insertu8);
498 insert = grub_malloc ((insertlen + 1) * sizeof (grub_uint32_t));
501 grub_free (insertu8);
503 grub_errno = GRUB_ERR_NONE;
506 t = grub_utf8_to_ucs4 (insert, insertlen,
507 (grub_uint8_t *) insertu8,
511 if (insert[t-1] == ' ' && buf[lpos] == ' ')
515 cl_insert (cl_terms, nterms, &lpos, &llen, &max_len, &buf, insert);
521 cl_insert (cl_terms, nterms, &lpos, &llen, &max_len, &buf, insert);
525 grub_free (insertu8);
528 cl_set_pos_all (cl_terms, nterms, lpos);
532 case GRUB_TERM_CTRL | 'k':
535 grub_free (kill_buf);
537 kill_buf = grub_malloc ((llen - lpos + 1)
538 * sizeof (grub_uint32_t));
542 grub_errno = GRUB_ERR_NONE;
546 grub_memcpy (kill_buf, buf + lpos,
547 (llen - lpos + 1) * sizeof (grub_uint32_t));
548 kill_buf[llen - lpos] = 0;
551 cl_delete (cl_terms, nterms,
552 buf, lpos, &llen, llen - lpos);
556 case GRUB_TERM_CTRL | 'n':
557 case GRUB_TERM_KEY_DOWN:
565 grub_history_replace (histpos, buf, llen);
569 cl_delete (cl_terms, nterms,
570 buf, lpos, &llen, llen);
571 hist = grub_history_get (histpos);
572 cl_insert (cl_terms, nterms, &lpos, &llen, &max_len, &buf, hist);
577 case GRUB_TERM_KEY_UP:
578 case GRUB_TERM_CTRL | 'p':
584 if (histpos < hist_used - 1)
586 grub_history_replace (histpos, buf, llen);
590 cl_delete (cl_terms, nterms,
591 buf, lpos, &llen, llen);
592 hist = grub_history_get (histpos);
594 cl_insert (cl_terms, nterms, &lpos, &llen, &max_len, &buf, hist);
598 case GRUB_TERM_CTRL | 'u':
601 grub_size_t n = lpos;
603 grub_free (kill_buf);
605 kill_buf = grub_malloc ((n + 1) * sizeof(grub_uint32_t));
609 grub_errno = GRUB_ERR_NONE;
613 grub_memcpy (kill_buf, buf, n * sizeof(grub_uint32_t));
618 cl_set_pos_all (cl_terms, nterms, lpos);
619 cl_delete (cl_terms, nterms,
620 buf, lpos, &llen, n);
624 case GRUB_TERM_CTRL | 'y':
626 cl_insert (cl_terms, nterms, &lpos, &llen, &max_len, &buf, kill_buf);
630 grub_free (cl_terms);
638 cl_set_pos_all (cl_terms, nterms, lpos);
644 case GRUB_TERM_CTRL | 'd':
645 case GRUB_TERM_KEY_DC:
647 cl_delete (cl_terms, nterms,
648 buf, lpos, &llen, 1);
652 if (grub_isprint (key))
654 grub_uint32_t str[2];
658 cl_insert (cl_terms, nterms, &lpos, &llen, &max_len, &buf, str);
670 if (strlen_ucs4 (buf) > 0)
672 grub_uint32_t empty[] = { 0 };
673 grub_history_replace (histpos, buf, llen);
674 grub_history_add (empty, 0);
677 ret = grub_ucs4_to_utf8_alloc (buf, llen + 1);
679 grub_free (cl_terms);