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>
23 #include <grub/types.h>
25 #include <grub/partition.h>
26 #include <grub/disk.h>
27 #include <grub/file.h>
30 static char *kill_buf
;
33 static char **hist_lines
= 0;
34 static int hist_pos
= 0;
35 static int hist_end
= 0;
36 static int hist_used
= 0;
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. */
47 /* Remove the lines that don't fit in the new buffer. */
48 if (newsize
< hist_used
)
51 int delsize
= hist_used
- newsize
;
54 for (i
= 1; i
<= delsize
; i
++)
56 int pos
= hist_end
- i
;
59 grub_free (old_hist_lines
[pos
]);
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 *));
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
);
90 /* Get the entry POS from the history where `0' is the newest
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. */
102 grub_history_add (char *s
)
104 /* Remove the oldest entry in the history to make room for a new
106 if (hist_used
+ 1 > hist_size
)
110 hist_end
= hist_size
+ hist_end
;
112 grub_free (hist_lines
[hist_end
]);
117 /* Move to the next position. */
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. */
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
);
136 grub_cmdline_run (int nested
)
138 grub_normal_init_page ();
142 [ Minimal BASH-like line editing is supported. For the first word, TAB\n\
143 lists possible command completions. Anywhere else TAB lists possible\n\
144 device/file completions.%s ]\n\n",
145 nested
? " ESC at any time exits." : "");
149 static char cmdline
[GRUB_MAX_CMDLINE
];
152 grub_errno
= GRUB_ERR_NONE
;
155 if (! grub_cmdline_get ("grub> ", cmdline
, sizeof (cmdline
), 0, 1)
162 grub_command_execute (cmdline
, 1);
166 /* A completion hook to print items. */
168 print_completion (const char *item
, grub_completion_type_t type
, int count
)
172 /* If this is the first time, print a label. */
177 case GRUB_COMPLETION_TYPE_COMMAND
:
180 case GRUB_COMPLETION_TYPE_DEVICE
:
183 case GRUB_COMPLETION_TYPE_FILE
:
186 case GRUB_COMPLETION_TYPE_PARTITION
:
189 case GRUB_COMPLETION_TYPE_ARGUMENT
:
197 grub_printf ("\nPossible %s are:\n", what
);
200 if (type
== GRUB_COMPLETION_TYPE_PARTITION
)
202 grub_normal_print_device_info (item
);
203 grub_errno
= GRUB_ERR_NONE
;
206 grub_printf (" %s", item
);
209 /* Get a command-line. If ECHO_CHAR is not zero, echo it instead of input
210 characters. If READLINE is non-zero, readline-like key bindings are
211 available. If ESC is pushed, return zero, otherwise return non-zero. */
212 /* FIXME: The dumb interface is not supported yet. */
214 grub_cmdline_get (const char *prompt
, char cmdline
[], unsigned max_len
,
215 int echo_char
, int readline
)
217 unsigned xpos
, ypos
, ystart
;
218 grub_size_t lpos
, llen
;
223 auto void cl_insert (const char *str
);
224 auto void cl_delete (unsigned len
);
225 auto void cl_print (int pos
, int c
);
226 auto void cl_set_pos (void);
228 void cl_set_pos (void)
230 xpos
= (plen
+ lpos
) % 79;
231 ypos
= ystart
+ (plen
+ lpos
) / 79;
232 grub_gotoxy (xpos
, ypos
);
237 void cl_print (int pos
, int c
)
241 for (p
= buf
+ pos
; *p
; p
++)
248 if (ypos
== (unsigned) (grub_getxy () & 0xFF))
261 void cl_insert (const char *str
)
263 grub_size_t len
= grub_strlen (str
);
265 if (len
+ llen
< max_len
)
267 grub_memmove (buf
+ lpos
+ len
, buf
+ lpos
, llen
- lpos
+ 1);
268 grub_memmove (buf
+ lpos
, str
, len
);
272 cl_print (lpos
- len
, echo_char
);
279 void cl_delete (unsigned len
)
281 if (lpos
+ len
<= llen
)
283 grub_size_t saved_lpos
= lpos
;
287 cl_print (lpos
, ' ');
291 grub_memmove (buf
+ lpos
, buf
+ lpos
+ len
, llen
- lpos
+ 1);
293 cl_print (lpos
, echo_char
);
300 plen
= grub_strlen (prompt
);
304 if ((grub_getxy () >> 8) != 0)
307 grub_printf (prompt
);
310 ystart
= ypos
= (grub_getxy () & 0xFF);
315 grub_history_add (buf
);
317 while ((key
= GRUB_TERM_ASCII_CHAR (grub_getkey ())) != '\n' && key
!= '\r')
349 case 9: /* Ctrl-i or TAB */
354 /* Backup the next character and make it 0 so it will
355 be easy to use string functions. */
356 char backup
= buf
[lpos
];
360 insert
= grub_normal_do_completion (buf
, &restore
,
362 /* Restore the original string. */
367 /* Restore the prompt. */
368 grub_printf ("\n%s%s", prompt
, buf
);
370 ystart
= ypos
= (grub_getxy () & 0xFF);
381 case 11: /* Ctrl-k */
385 grub_free (kill_buf
);
387 kill_buf
= grub_strdup (buf
+ lpos
);
388 grub_errno
= GRUB_ERR_NONE
;
390 cl_delete (llen
- lpos
);
394 case 14: /* Ctrl-n */
402 grub_history_replace (histpos
, buf
);
407 hist
= grub_history_get (histpos
);
412 case 16: /* Ctrl-p */
418 if (histpos
< hist_used
- 1)
420 grub_history_replace (histpos
, buf
);
425 hist
= grub_history_get (histpos
);
431 case 21: /* Ctrl-u */
434 grub_size_t n
= lpos
;
437 grub_free (kill_buf
);
439 kill_buf
= grub_malloc (n
+ 1);
440 grub_errno
= GRUB_ERR_NONE
;
443 grub_memcpy (kill_buf
, buf
, n
);
453 case 25: /* Ctrl-y */
455 cl_insert (kill_buf
);
481 if (grub_isprint (key
))
496 /* If ECHO_CHAR is NUL, remove leading spaces. */
499 while (buf
[lpos
] == ' ')
503 if (grub_strlen (buf
) > 0)
505 grub_history_replace (histpos
, buf
);
506 grub_history_add ("");
509 grub_memcpy (cmdline
, buf
+ lpos
, llen
- lpos
+ 1);