2 * GRUB -- GRand Unified Bootloader
3 * Copyright (C) 1999,2000,2001,2002,2003,2004,2005 Free Software Foundation, Inc.
5 * This program 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 2 of the License, or
8 * (at your option) any later version.
10 * This program 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 this program; if not, write to the Free Software
17 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20 #include <grub/normal.h>
21 #include <grub/misc.h>
22 #include <grub/term.h>
24 #include <grub/types.h>
26 #include <grub/partition.h>
27 #include <grub/disk.h>
28 #include <grub/file.h>
31 static char *kill_buf
;
34 static char **hist_lines
= 0;
35 static int hist_pos
= 0;
36 static int hist_end
= 0;
37 static int hist_used
= 0;
40 grub_set_history (int newsize
)
42 char **old_hist_lines
= hist_lines
;
43 hist_lines
= grub_malloc (sizeof (char *) * newsize
);
45 /* Copy the old lines into the new buffer. */
48 /* Remove the lines that don't fit in the new buffer. */
49 if (newsize
< hist_used
)
52 int delsize
= hist_used
- newsize
;
55 for (i
= 1; i
<= delsize
; i
++)
57 int pos
= hist_end
- i
;
60 grub_free (old_hist_lines
[pos
]);
65 hist_end
+= hist_size
;
68 if (hist_pos
< hist_end
)
69 grub_memmove (hist_lines
, old_hist_lines
+ hist_pos
,
70 (hist_end
- hist_pos
) * sizeof (char *));
73 /* Copy the older part. */
74 grub_memmove (hist_lines
, old_hist_lines
+ hist_pos
,
75 (hist_size
- hist_pos
) * sizeof (char *));
77 /* Copy the newer part. */
78 grub_memmove (hist_lines
+ hist_size
- hist_pos
, old_hist_lines
,
79 hist_end
* sizeof (char *));
83 grub_free (old_hist_lines
);
91 /* Get the entry POS from the history where `0' is the newest
94 grub_history_get (int pos
)
96 pos
= (hist_pos
+ pos
) % hist_size
;
97 return hist_lines
[pos
];
101 /* Insert a new history line S on the top of the history. */
103 grub_history_add (char *s
)
105 /* Remove the oldest entry in the history to make room for a new
107 if (hist_used
+ 1 > hist_size
)
111 hist_end
= hist_size
+ hist_end
;
113 grub_free (hist_lines
[hist_end
]);
118 /* Move to the next position. */
121 hist_pos
= hist_size
+ hist_pos
;
123 /* Insert into history. */
124 hist_lines
[hist_pos
] = grub_strdup (s
);
127 /* Replace the history entry on position POS with the string S. */
129 grub_history_replace (int pos
, char *s
)
131 pos
= (hist_pos
+ pos
) % hist_size
;
132 grub_free (hist_lines
[pos
]);
133 hist_lines
[pos
] = grub_strdup (s
);
137 grub_cmdline_run (int nested
)
139 grub_normal_init_page ();
143 [ Minimal BASH-like line editing is supported. For the first word, TAB\n\
144 lists possible command completions. Anywhere else TAB lists possible\n\
145 device/file completions.%s ]\n\n",
146 nested
? " ESC at any time exits." : "");
150 static char cmdline
[GRUB_MAX_CMDLINE
];
153 grub_errno
= GRUB_ERR_NONE
;
156 if (! grub_cmdline_get ("grub> ", cmdline
, sizeof (cmdline
), 0, 1)
163 grub_command_execute (cmdline
, 1);
167 /* A completion hook to print items. */
169 print_completion (const char *item
, grub_completion_type_t type
, int count
)
173 /* If this is the first time, print a label. */
178 case GRUB_COMPLETION_TYPE_COMMAND
:
181 case GRUB_COMPLETION_TYPE_DEVICE
:
184 case GRUB_COMPLETION_TYPE_FILE
:
187 case GRUB_COMPLETION_TYPE_PARTITION
:
190 case GRUB_COMPLETION_TYPE_ARGUMENT
:
198 grub_printf ("\nPossible %s are:\n", what
);
201 if (type
== GRUB_COMPLETION_TYPE_PARTITION
)
203 grub_normal_print_device_info (item
);
204 grub_errno
= GRUB_ERR_NONE
;
207 grub_printf (" %s", item
);
210 /* Get a command-line. If ECHO_CHAR is not zero, echo it instead of input
211 characters. If READLINE is non-zero, readline-like key bindings are
212 available. If ESC is pushed, return zero, otherwise return non-zero. */
213 /* FIXME: The dumb interface is not supported yet. */
215 grub_cmdline_get (const char *prompt
, char cmdline
[], unsigned max_len
,
216 int echo_char
, int readline
)
218 unsigned xpos
, ypos
, ystart
;
219 grub_size_t lpos
, llen
;
224 auto void cl_insert (const char *str
);
225 auto void cl_delete (unsigned len
);
226 auto void cl_print (int pos
, int c
);
227 auto void cl_set_pos (void);
229 void cl_set_pos (void)
231 xpos
= (plen
+ lpos
) % 79;
232 ypos
= ystart
+ (plen
+ lpos
) / 79;
233 grub_gotoxy (xpos
, ypos
);
236 void cl_print (int pos
, int c
)
240 for (p
= buf
+ pos
; *p
; p
++)
247 if (ypos
== (unsigned) (grub_getxy () & 0xFF))
260 void cl_insert (const char *str
)
262 grub_size_t len
= grub_strlen (str
);
264 if (len
+ llen
< max_len
)
266 grub_memmove (buf
+ lpos
+ len
, buf
+ lpos
, llen
- lpos
+ 1);
267 grub_memmove (buf
+ lpos
, str
, len
);
271 cl_print (lpos
- len
, echo_char
);
276 void cl_delete (unsigned len
)
278 if (lpos
+ len
<= llen
)
280 grub_size_t saved_lpos
= lpos
;
284 cl_print (lpos
, ' ');
288 grub_memmove (buf
+ lpos
, buf
+ lpos
+ len
, llen
- lpos
+ 1);
290 cl_print (lpos
, echo_char
);
295 plen
= grub_strlen (prompt
);
299 if ((grub_getxy () >> 8) != 0)
302 grub_printf (prompt
);
305 ystart
= ypos
= (grub_getxy () & 0xFF);
310 grub_history_add (buf
);
312 while ((key
= GRUB_TERM_ASCII_CHAR (grub_getkey ())) != '\n' && key
!= '\r')
344 case 9: /* Ctrl-i or TAB */
349 /* Backup the next character and make it 0 so it will
350 be easy to use string functions. */
351 char backup
= buf
[lpos
];
355 insert
= grub_normal_do_completion (buf
, &restore
,
357 /* Restore the original string. */
362 /* Restore the prompt. */
363 grub_printf ("\n%s%s", prompt
, buf
);
365 ystart
= ypos
= (grub_getxy () & 0xFF);
376 case 11: /* Ctrl-k */
380 grub_free (kill_buf
);
382 kill_buf
= grub_strdup (buf
+ lpos
);
383 grub_errno
= GRUB_ERR_NONE
;
385 cl_delete (llen
- lpos
);
389 case 14: /* Ctrl-n */
397 grub_history_replace (histpos
, buf
);
402 hist
= grub_history_get (histpos
);
407 case 16: /* Ctrl-p */
413 if (histpos
< hist_used
- 1)
415 grub_history_replace (histpos
, buf
);
420 hist
= grub_history_get (histpos
);
426 case 21: /* Ctrl-u */
429 grub_size_t n
= lpos
;
432 grub_free (kill_buf
);
434 kill_buf
= grub_malloc (n
+ 1);
435 grub_errno
= GRUB_ERR_NONE
;
438 grub_memcpy (kill_buf
, buf
, n
);
448 case 25: /* Ctrl-y */
450 cl_insert (kill_buf
);
476 if (grub_isprint (key
))
491 /* If ECHO_CHAR is NUL, remove leading spaces. */
494 while (buf
[lpos
] == ' ')
498 if (grub_strlen (buf
) > 0)
500 grub_history_replace (histpos
, buf
);
501 grub_history_add ("");
504 grub_memcpy (cmdline
, buf
+ lpos
, llen
- lpos
+ 1);