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 (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 /* 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 unsigned xpos
, ypos
, ystart
, width
, height
;
211 struct grub_term_output
*term
;
214 /* Get a command-line. If ESC is pushed, return zero,
215 otherwise return command line. */
216 /* FIXME: The dumb interface is not supported yet. */
218 grub_cmdline_get (const char *prompt_translated
)
220 grub_size_t lpos
, llen
;
222 grub_size_t max_len
= 256;
225 auto void cl_insert (const grub_uint32_t
*str
);
226 auto void cl_delete (unsigned len
);
227 auto inline void __attribute__ ((always_inline
)) cl_print (struct cmdline_term
*cl_term
, int pos
,
229 auto void cl_set_pos (struct cmdline_term
*cl_term
);
230 auto void cl_print_all (int pos
, grub_uint32_t c
);
231 auto void cl_set_pos_all (void);
232 auto void init_clterm (struct cmdline_term
*cl_term_cur
);
233 auto void init_clterm_all (void);
234 struct cmdline_term
*cl_terms
;
238 void cl_set_pos (struct cmdline_term
*cl_term
)
240 cl_term
->xpos
= (cl_term
->prompt_len
+ lpos
) % (cl_term
->width
- 1);
241 cl_term
->ypos
= cl_term
->ystart
242 + (cl_term
->prompt_len
+ lpos
) / (cl_term
->width
- 1);
243 grub_term_gotoxy (cl_term
->term
, cl_term
->xpos
, cl_term
->ypos
);
246 void cl_set_pos_all (void)
249 for (i
= 0; i
< nterms
; i
++)
250 cl_set_pos (&cl_terms
[i
]);
253 inline void __attribute__ ((always_inline
)) cl_print (struct cmdline_term
*cl_term
, int pos
, grub_uint32_t c
)
257 for (p
= buf
+ pos
; p
< buf
+ llen
; p
++)
260 grub_putcode (c
, cl_term
->term
);
262 grub_putcode (*p
, cl_term
->term
);
264 if (cl_term
->xpos
>= cl_term
->width
- 1)
267 if (cl_term
->ypos
>= (unsigned) (cl_term
->height
- 1))
271 grub_putcode ('\n', cl_term
->term
);
276 void cl_print_all (int pos
, grub_uint32_t c
)
279 for (i
= 0; i
< nterms
; i
++)
280 cl_print (&cl_terms
[i
], pos
, c
);
283 void cl_insert (const grub_uint32_t
*str
)
285 grub_size_t len
= strlen_ucs4 (str
);
287 if (len
+ llen
>= max_len
)
291 nbuf
= grub_realloc (buf
, sizeof (grub_uint32_t
) * max_len
);
297 grub_errno
= GRUB_ERR_NONE
;
302 if (len
+ llen
< max_len
)
304 grub_memmove (buf
+ lpos
+ len
, buf
+ lpos
,
305 (llen
- lpos
+ 1) * sizeof (grub_uint32_t
));
306 grub_memmove (buf
+ lpos
, str
, len
* sizeof (grub_uint32_t
));
310 cl_print_all (lpos
, 0);
316 void cl_delete (unsigned len
)
318 if (lpos
+ len
<= llen
)
320 grub_size_t saved_lpos
= lpos
;
324 cl_print_all (lpos
, ' ');
328 grub_memmove (buf
+ lpos
, buf
+ lpos
+ len
,
329 sizeof (grub_uint32_t
) * (llen
- lpos
+ 1));
331 cl_print_all (lpos
, 0);
336 void init_clterm (struct cmdline_term
*cl_term_cur
)
338 cl_term_cur
->xpos
= cl_term_cur
->prompt_len
;
339 cl_term_cur
->ypos
= (grub_term_getxy (cl_term_cur
->term
) & 0xFF);
340 cl_term_cur
->ystart
= cl_term_cur
->ypos
;
341 cl_term_cur
->width
= grub_term_width (cl_term_cur
->term
);
342 cl_term_cur
->height
= grub_term_height (cl_term_cur
->term
);
345 void init_clterm_all (void)
348 for (i
= 0; i
< nterms
; i
++)
349 init_clterm (&cl_terms
[i
]);
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
) >> 8) != 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
);
383 cl_term_cur
= cl_terms
;
385 unicode_msg
= grub_malloc (msg_len
* sizeof (grub_uint32_t
));
388 msg_len
= grub_utf8_to_ucs4 (unicode_msg
, msg_len
- 1,
389 (grub_uint8_t
*) prompt_translated
, -1, 0);
390 unicode_msg
[msg_len
++] = ' ';
392 FOR_ACTIVE_TERM_OUTPUTS(cur
)
394 cl_term_cur
->term
= cur
;
395 cl_term_cur
->prompt_len
= grub_getstringwidth (unicode_msg
,
396 unicode_msg
+ msg_len
,
398 init_clterm (cl_term_cur
);
401 grub_free (unicode_msg
);
405 grub_history_add (buf
, llen
);
409 while ((key
= grub_getkey ()) != '\n' && key
!= '\r')
413 case GRUB_TERM_CTRL
| 'a':
414 case GRUB_TERM_KEY_HOME
:
419 case GRUB_TERM_CTRL
| 'b':
420 case GRUB_TERM_KEY_LEFT
:
428 case GRUB_TERM_CTRL
| 'e':
429 case GRUB_TERM_KEY_END
:
434 case GRUB_TERM_CTRL
| 'f':
435 case GRUB_TERM_KEY_RIGHT
:
443 case GRUB_TERM_CTRL
| 'i':
454 bufu8
= grub_ucs4_to_utf8_alloc (buf
, lpos
);
459 grub_errno
= GRUB_ERR_NONE
;
463 insertu8
= grub_normal_do_completion (bufu8
, &restore
,
467 grub_normal_reset_more ();
471 /* Restore the prompt. */
473 grub_xputs (prompt_translated
);
481 grub_size_t insertlen
;
483 grub_uint32_t
*insert
;
485 insertlen
= grub_strlen (insertu8
);
486 insert
= grub_malloc ((insertlen
+ 1) * sizeof (grub_uint32_t
));
489 grub_free (insertu8
);
491 grub_errno
= GRUB_ERR_NONE
;
494 t
= grub_utf8_to_ucs4 (insert
, insertlen
,
495 (grub_uint8_t
*) insertu8
,
499 if (insert
[t
-1] == ' ' && buf
[lpos
] == ' ')
513 grub_free (insertu8
);
520 case GRUB_TERM_CTRL
| 'k':
523 grub_free (kill_buf
);
525 kill_buf
= grub_malloc ((llen
- lpos
+ 1)
526 * sizeof (grub_uint32_t
));
530 grub_errno
= GRUB_ERR_NONE
;
534 grub_memcpy (kill_buf
, buf
+ lpos
,
535 (llen
- lpos
+ 1) * sizeof (grub_uint32_t
));
536 kill_buf
[llen
- lpos
] = 0;
539 cl_delete (llen
- lpos
);
543 case GRUB_TERM_CTRL
| 'n':
544 case GRUB_TERM_KEY_DOWN
:
552 grub_history_replace (histpos
, buf
, llen
);
557 hist
= grub_history_get (histpos
);
563 case GRUB_TERM_KEY_UP
:
564 case GRUB_TERM_CTRL
| 'p':
570 if (histpos
< hist_used
- 1)
572 grub_history_replace (histpos
, buf
, llen
);
577 hist
= grub_history_get (histpos
);
583 case GRUB_TERM_CTRL
| 'u':
586 grub_size_t n
= lpos
;
588 grub_free (kill_buf
);
590 kill_buf
= grub_malloc (n
+ 1);
594 grub_errno
= GRUB_ERR_NONE
;
598 grub_memcpy (kill_buf
, buf
, n
);
608 case GRUB_TERM_CTRL
| 'y':
610 cl_insert (kill_buf
);
614 grub_free (cl_terms
);
627 case GRUB_TERM_CTRL
| 'd':
628 case GRUB_TERM_KEY_DC
:
634 if (grub_isprint (key
))
636 grub_uint32_t str
[2];
652 if (strlen_ucs4 (buf
) > 0)
654 grub_uint32_t empty
[] = { 0 };
655 grub_history_replace (histpos
, buf
, llen
);
656 grub_history_add (empty
, 0);
659 ret
= grub_ucs4_to_utf8_alloc (buf
, llen
+ 1);
661 grub_free (cl_terms
);