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
int delsize = hist_used - newsize;
56
for (i = 1; i <= delsize; i++)
58
int 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 (int 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 (int 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
grub_puts_ (N_("Possible things are:"));
195
if (type == GRUB_COMPLETION_TYPE_PARTITION)
197
grub_normal_print_device_info (item);
198
grub_errno = GRUB_ERR_NONE;
201
grub_printf (" %s", item);
206
unsigned xpos, ypos, ystart, width, height;
207
struct grub_term_output *term;
210
/* Get a command-line. If ESC is pushed, return zero,
211
otherwise return command line. */
212
/* FIXME: The dumb interface is not supported yet. */
214
grub_cmdline_get (const char *prompt)
216
grub_size_t lpos, llen;
219
grub_size_t max_len = 256;
222
auto void cl_insert (const grub_uint32_t *str);
223
auto void cl_delete (unsigned len);
224
auto inline void __attribute__ ((always_inline)) cl_print (struct cmdline_term *cl_term, int pos,
226
auto void cl_set_pos (struct cmdline_term *cl_term);
227
auto void cl_print_all (int pos, grub_uint32_t c);
228
auto void cl_set_pos_all (void);
229
auto void init_clterm (struct cmdline_term *cl_term_cur);
230
auto void init_clterm_all (void);
231
const char *prompt_translated = _(prompt);
232
struct cmdline_term *cl_terms;
236
void cl_set_pos (struct cmdline_term *cl_term)
238
cl_term->xpos = (plen + lpos) % (cl_term->width - 1);
239
cl_term->ypos = cl_term->ystart + (plen + lpos) / (cl_term->width - 1);
240
grub_term_gotoxy (cl_term->term, cl_term->xpos, cl_term->ypos);
243
void cl_set_pos_all ()
246
for (i = 0; i < nterms; i++)
247
cl_set_pos (&cl_terms[i]);
250
inline void __attribute__ ((always_inline)) cl_print (struct cmdline_term *cl_term, int pos, grub_uint32_t c)
254
for (p = buf + pos; p < buf + llen; p++)
257
grub_putcode (c, cl_term->term);
259
grub_putcode (*p, cl_term->term);
261
if (cl_term->xpos >= cl_term->width - 1)
264
if (cl_term->ypos >= (unsigned) (cl_term->height - 1))
268
grub_putcode ('\n', cl_term->term);
273
void cl_print_all (int pos, grub_uint32_t c)
276
for (i = 0; i < nterms; i++)
277
cl_print (&cl_terms[i], pos, c);
280
void cl_insert (const grub_uint32_t *str)
282
grub_size_t len = strlen_ucs4 (str);
284
if (len + llen >= max_len)
288
nbuf = grub_realloc (buf, sizeof (grub_uint32_t) * max_len);
294
grub_errno = GRUB_ERR_NONE;
299
if (len + llen < max_len)
301
grub_memmove (buf + lpos + len, buf + lpos,
302
(llen - lpos + 1) * sizeof (grub_uint32_t));
303
grub_memmove (buf + lpos, str, len * sizeof (grub_uint32_t));
307
cl_print_all (lpos, 0);
313
void cl_delete (unsigned len)
315
if (lpos + len <= llen)
317
grub_size_t saved_lpos = lpos;
321
cl_print_all (lpos, ' ');
325
grub_memmove (buf + lpos, buf + lpos + len,
326
sizeof (grub_uint32_t) * (llen - lpos + 1));
328
cl_print_all (lpos, 0);
333
void init_clterm (struct cmdline_term *cl_term_cur)
335
cl_term_cur->xpos = plen;
336
cl_term_cur->ypos = (grub_term_getxy (cl_term_cur->term) & 0xFF);
337
cl_term_cur->ystart = cl_term_cur->ypos;
338
cl_term_cur->width = grub_term_width (cl_term_cur->term);
339
cl_term_cur->height = grub_term_height (cl_term_cur->term);
342
void init_clterm_all (void)
345
for (i = 0; i < nterms; i++)
346
init_clterm (&cl_terms[i]);
349
buf = grub_malloc (max_len * sizeof (grub_uint32_t));
353
plen = grub_strlen (prompt_translated) + sizeof (" ") - 1;
358
grub_term_output_t term;
360
FOR_ACTIVE_TERM_OUTPUTS(term)
361
if ((grub_term_getxy (term) >> 8) != 0)
362
grub_putcode ('\n', term);
364
grub_printf ("%s ", prompt_translated);
365
grub_normal_reset_more ();
368
struct cmdline_term *cl_term_cur;
369
struct grub_term_output *cur;
371
FOR_ACTIVE_TERM_OUTPUTS(cur)
374
cl_terms = grub_malloc (sizeof (cl_terms[0]) * nterms);
377
cl_term_cur = cl_terms;
378
FOR_ACTIVE_TERM_OUTPUTS(cur)
380
cl_term_cur->term = cur;
381
init_clterm (cl_term_cur);
387
grub_history_add (buf, llen);
391
while ((key = grub_getkey ()) != '\n' && key != '\r')
395
case GRUB_TERM_CTRL | 'a':
396
case GRUB_TERM_KEY_HOME:
401
case GRUB_TERM_CTRL | 'b':
402
case GRUB_TERM_KEY_LEFT:
410
case GRUB_TERM_CTRL | 'e':
411
case GRUB_TERM_KEY_END:
416
case GRUB_TERM_CTRL | 'f':
417
case GRUB_TERM_KEY_RIGHT:
425
case GRUB_TERM_CTRL | 'i':
436
bufu8 = grub_ucs4_to_utf8_alloc (buf, lpos);
441
grub_errno = GRUB_ERR_NONE;
445
insertu8 = grub_normal_do_completion (bufu8, &restore,
449
grub_normal_reset_more ();
453
/* Restore the prompt. */
454
grub_printf ("\n%s ", prompt_translated);
461
grub_size_t insertlen;
463
grub_uint32_t *insert;
465
insertlen = grub_strlen (insertu8);
466
insert = grub_malloc ((insertlen + 1) * sizeof (grub_uint32_t));
469
grub_free (insertu8);
471
grub_errno = GRUB_ERR_NONE;
474
t = grub_utf8_to_ucs4 (insert, insertlen,
475
(grub_uint8_t *) insertu8,
479
if (insert[t-1] == ' ' && buf[lpos] == ' ')
493
grub_free (insertu8);
500
case GRUB_TERM_CTRL | 'k':
504
grub_free (kill_buf);
506
kill_buf = grub_malloc ((llen - lpos + 1)
507
* sizeof (grub_uint32_t));
511
grub_errno = GRUB_ERR_NONE;
515
grub_memcpy (kill_buf, buf + lpos,
516
(llen - lpos + 1) * sizeof (grub_uint32_t));
517
kill_buf[llen - lpos] = 0;
520
cl_delete (llen - lpos);
524
case GRUB_TERM_CTRL | 'n':
525
case GRUB_TERM_KEY_DOWN:
533
grub_history_replace (histpos, buf, llen);
538
hist = grub_history_get (histpos);
544
case GRUB_TERM_KEY_UP:
545
case GRUB_TERM_CTRL | 'p':
551
if (histpos < hist_used - 1)
553
grub_history_replace (histpos, buf, llen);
558
hist = grub_history_get (histpos);
564
case GRUB_TERM_CTRL | 'u':
567
grub_size_t n = lpos;
570
grub_free (kill_buf);
572
kill_buf = grub_malloc (n + 1);
576
grub_errno = GRUB_ERR_NONE;
580
grub_memcpy (kill_buf, buf, n);
590
case GRUB_TERM_CTRL | 'y':
592
cl_insert (kill_buf);
596
grub_free (cl_terms);
609
case GRUB_TERM_CTRL | 'd':
610
case GRUB_TERM_KEY_DC:
616
if (grub_isprint (key))
618
grub_uint32_t str[2];
633
/* Remove leading spaces. */
635
while (buf[lpos] == ' ')
639
if (strlen_ucs4 (buf) > 0)
641
grub_uint32_t empty[] = { 0 };
642
grub_history_replace (histpos, buf, llen);
643
grub_history_add (empty, 0);
646
ret = grub_ucs4_to_utf8_alloc (buf + lpos, llen - lpos + 1);
648
grub_free (cl_terms);