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
);
135 /* A completion hook to print items. */
137 print_completion (const char *item
, grub_completion_type_t type
, int count
)
141 /* If this is the first time, print a label. */
146 case GRUB_COMPLETION_TYPE_COMMAND
:
149 case GRUB_COMPLETION_TYPE_DEVICE
:
152 case GRUB_COMPLETION_TYPE_FILE
:
155 case GRUB_COMPLETION_TYPE_PARTITION
:
158 case GRUB_COMPLETION_TYPE_ARGUMENT
:
166 grub_printf ("\nPossible %s are:\n", what
);
169 if (type
== GRUB_COMPLETION_TYPE_PARTITION
)
171 grub_normal_print_device_info (item
);
172 grub_errno
= GRUB_ERR_NONE
;
175 grub_printf (" %s", item
);
178 /* Get a command-line. If ECHO_CHAR is not zero, echo it instead of input
179 characters. If READLINE is non-zero, readline-like key bindings are
180 available. If ESC is pushed, return zero, otherwise return non-zero. */
181 /* FIXME: The dumb interface is not supported yet. */
183 grub_cmdline_get (const char *prompt
, char cmdline
[], unsigned max_len
,
184 int echo_char
, int readline
, int history
)
186 unsigned xpos
, ypos
, ystart
;
187 grub_size_t lpos
, llen
;
192 auto void cl_insert (const char *str
);
193 auto void cl_delete (unsigned len
);
194 auto void cl_print (int pos
, int c
);
195 auto void cl_set_pos (void);
197 void cl_set_pos (void)
199 xpos
= (plen
+ lpos
) % 79;
200 ypos
= ystart
+ (plen
+ lpos
) / 79;
201 grub_gotoxy (xpos
, ypos
);
206 void cl_print (int pos
, int c
)
210 for (p
= buf
+ pos
; *p
; p
++)
217 if (ypos
== (unsigned) (grub_getxy () & 0xFF))
230 void cl_insert (const char *str
)
232 grub_size_t len
= grub_strlen (str
);
234 if (len
+ llen
< max_len
)
236 grub_memmove (buf
+ lpos
+ len
, buf
+ lpos
, llen
- lpos
+ 1);
237 grub_memmove (buf
+ lpos
, str
, len
);
241 cl_print (lpos
- len
, echo_char
);
248 void cl_delete (unsigned len
)
250 if (lpos
+ len
<= llen
)
252 grub_size_t saved_lpos
= lpos
;
256 cl_print (lpos
, ' ');
260 grub_memmove (buf
+ lpos
, buf
+ lpos
+ len
, llen
- lpos
+ 1);
262 cl_print (lpos
, echo_char
);
269 plen
= grub_strlen (prompt
);
273 if ((grub_getxy () >> 8) != 0)
276 grub_printf ("%s", prompt
);
279 ystart
= ypos
= (grub_getxy () & 0xFF);
283 if (history
&& hist_used
== 0)
284 grub_history_add (buf
);
286 while ((key
= GRUB_TERM_ASCII_CHAR (grub_getkey ())) != '\n' && key
!= '\r')
318 case 9: /* Ctrl-i or TAB */
323 /* Backup the next character and make it 0 so it will
324 be easy to use string functions. */
325 char backup
= buf
[lpos
];
329 insert
= grub_normal_do_completion (buf
, &restore
,
331 /* Restore the original string. */
336 /* Restore the prompt. */
337 grub_printf ("\n%s%s", prompt
, buf
);
339 ystart
= ypos
= (grub_getxy () & 0xFF);
350 case 11: /* Ctrl-k */
354 grub_free (kill_buf
);
356 kill_buf
= grub_strdup (buf
+ lpos
);
357 grub_errno
= GRUB_ERR_NONE
;
359 cl_delete (llen
- lpos
);
363 case 14: /* Ctrl-n */
371 grub_history_replace (histpos
, buf
);
376 hist
= grub_history_get (histpos
);
381 case 16: /* Ctrl-p */
387 if (histpos
< hist_used
- 1)
389 grub_history_replace (histpos
, buf
);
394 hist
= grub_history_get (histpos
);
400 case 21: /* Ctrl-u */
403 grub_size_t n
= lpos
;
406 grub_free (kill_buf
);
408 kill_buf
= grub_malloc (n
+ 1);
409 grub_errno
= GRUB_ERR_NONE
;
412 grub_memcpy (kill_buf
, buf
, n
);
422 case 25: /* Ctrl-y */
424 cl_insert (kill_buf
);
450 if (grub_isprint (key
))
465 /* If ECHO_CHAR is NUL, remove leading spaces. */
468 while (buf
[lpos
] == ' ')
474 if (grub_strlen (buf
) > 0)
476 grub_history_replace (histpos
, buf
);
477 grub_history_add ("");
481 grub_memcpy (cmdline
, buf
+ lpos
, llen
- lpos
+ 1);