blob: e94c97521b51c9776d8ac95010ae1c7fdc22a86d [file] [log] [blame]
bellard7e2515e2004-08-01 21:52:19 +00001/*
2 * QEMU readline utility
ths5fafdf22007-09-16 21:08:06 +00003 *
bellard7e2515e2004-08-01 21:52:19 +00004 * Copyright (c) 2003-2004 Fabrice Bellard
ths5fafdf22007-09-16 21:08:06 +00005 *
bellard7e2515e2004-08-01 21:52:19 +00006 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22 * THE SOFTWARE.
23 */
Stefan Hajnoczic60bf332013-11-14 11:54:14 +010024
Peter Maydellaafd7582016-01-29 17:49:55 +000025#include "qemu/osdep.h"
Stefan Hajnoczic60bf332013-11-14 11:54:14 +010026#include "qemu-common.h"
Stefan Hajnoczi0150cd82013-11-14 11:54:15 +010027#include "qemu/readline.h"
bellard7e2515e2004-08-01 21:52:19 +000028
bellard7e2515e2004-08-01 21:52:19 +000029#define IS_NORM 0
30#define IS_ESC 1
31#define IS_CSI 2
Kevin Wolfd34dc452013-05-14 12:15:54 +020032#define IS_SS3 3
bellard7e2515e2004-08-01 21:52:19 +000033
aliguori4c36ba32009-03-05 23:01:37 +000034void readline_show_prompt(ReadLineState *rs)
bellard7e2515e2004-08-01 21:52:19 +000035{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +010036 rs->printf_func(rs->opaque, "%s", rs->prompt);
37 rs->flush_func(rs->opaque);
aliguori4c36ba32009-03-05 23:01:37 +000038 rs->last_cmd_buf_index = 0;
39 rs->last_cmd_buf_size = 0;
40 rs->esc_state = IS_NORM;
bellard7e2515e2004-08-01 21:52:19 +000041}
42
bellard7e2515e2004-08-01 21:52:19 +000043/* update the displayed command line */
aliguori4c36ba32009-03-05 23:01:37 +000044static void readline_update(ReadLineState *rs)
bellard7e2515e2004-08-01 21:52:19 +000045{
46 int i, delta, len;
47
aliguori4c36ba32009-03-05 23:01:37 +000048 if (rs->cmd_buf_size != rs->last_cmd_buf_size ||
49 memcmp(rs->cmd_buf, rs->last_cmd_buf, rs->cmd_buf_size) != 0) {
50 for(i = 0; i < rs->last_cmd_buf_index; i++) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +010051 rs->printf_func(rs->opaque, "\033[D");
bellard7e2515e2004-08-01 21:52:19 +000052 }
aliguori4c36ba32009-03-05 23:01:37 +000053 rs->cmd_buf[rs->cmd_buf_size] = '\0';
54 if (rs->read_password) {
55 len = strlen(rs->cmd_buf);
bellard7e2515e2004-08-01 21:52:19 +000056 for(i = 0; i < len; i++)
Stefan Hajnoczic60bf332013-11-14 11:54:14 +010057 rs->printf_func(rs->opaque, "*");
bellard7e2515e2004-08-01 21:52:19 +000058 } else {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +010059 rs->printf_func(rs->opaque, "%s", rs->cmd_buf);
bellard7e2515e2004-08-01 21:52:19 +000060 }
Stefan Hajnoczic60bf332013-11-14 11:54:14 +010061 rs->printf_func(rs->opaque, "\033[K");
aliguori4c36ba32009-03-05 23:01:37 +000062 memcpy(rs->last_cmd_buf, rs->cmd_buf, rs->cmd_buf_size);
63 rs->last_cmd_buf_size = rs->cmd_buf_size;
64 rs->last_cmd_buf_index = rs->cmd_buf_size;
bellard7e2515e2004-08-01 21:52:19 +000065 }
aliguori4c36ba32009-03-05 23:01:37 +000066 if (rs->cmd_buf_index != rs->last_cmd_buf_index) {
67 delta = rs->cmd_buf_index - rs->last_cmd_buf_index;
bellard7e2515e2004-08-01 21:52:19 +000068 if (delta > 0) {
69 for(i = 0;i < delta; i++) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +010070 rs->printf_func(rs->opaque, "\033[C");
bellard7e2515e2004-08-01 21:52:19 +000071 }
72 } else {
73 delta = -delta;
74 for(i = 0;i < delta; i++) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +010075 rs->printf_func(rs->opaque, "\033[D");
bellard7e2515e2004-08-01 21:52:19 +000076 }
77 }
aliguori4c36ba32009-03-05 23:01:37 +000078 rs->last_cmd_buf_index = rs->cmd_buf_index;
bellard7e2515e2004-08-01 21:52:19 +000079 }
Stefan Hajnoczic60bf332013-11-14 11:54:14 +010080 rs->flush_func(rs->opaque);
bellard7e2515e2004-08-01 21:52:19 +000081}
82
aliguori4c36ba32009-03-05 23:01:37 +000083static void readline_insert_char(ReadLineState *rs, int ch)
bellard7e2515e2004-08-01 21:52:19 +000084{
aliguori4c36ba32009-03-05 23:01:37 +000085 if (rs->cmd_buf_index < READLINE_CMD_BUF_SIZE) {
86 memmove(rs->cmd_buf + rs->cmd_buf_index + 1,
87 rs->cmd_buf + rs->cmd_buf_index,
88 rs->cmd_buf_size - rs->cmd_buf_index);
89 rs->cmd_buf[rs->cmd_buf_index] = ch;
90 rs->cmd_buf_size++;
91 rs->cmd_buf_index++;
bellard7e2515e2004-08-01 21:52:19 +000092 }
93}
94
aliguori4c36ba32009-03-05 23:01:37 +000095static void readline_backward_char(ReadLineState *rs)
bellard7e2515e2004-08-01 21:52:19 +000096{
aliguori4c36ba32009-03-05 23:01:37 +000097 if (rs->cmd_buf_index > 0) {
98 rs->cmd_buf_index--;
bellard7e2515e2004-08-01 21:52:19 +000099 }
100}
101
aliguori4c36ba32009-03-05 23:01:37 +0000102static void readline_forward_char(ReadLineState *rs)
bellard7e2515e2004-08-01 21:52:19 +0000103{
aliguori4c36ba32009-03-05 23:01:37 +0000104 if (rs->cmd_buf_index < rs->cmd_buf_size) {
105 rs->cmd_buf_index++;
bellard7e2515e2004-08-01 21:52:19 +0000106 }
107}
108
aliguori4c36ba32009-03-05 23:01:37 +0000109static void readline_delete_char(ReadLineState *rs)
bellard7e2515e2004-08-01 21:52:19 +0000110{
aliguori4c36ba32009-03-05 23:01:37 +0000111 if (rs->cmd_buf_index < rs->cmd_buf_size) {
112 memmove(rs->cmd_buf + rs->cmd_buf_index,
113 rs->cmd_buf + rs->cmd_buf_index + 1,
114 rs->cmd_buf_size - rs->cmd_buf_index - 1);
115 rs->cmd_buf_size--;
bellard7e2515e2004-08-01 21:52:19 +0000116 }
117}
118
aliguori4c36ba32009-03-05 23:01:37 +0000119static void readline_backspace(ReadLineState *rs)
bellard7e2515e2004-08-01 21:52:19 +0000120{
aliguori4c36ba32009-03-05 23:01:37 +0000121 if (rs->cmd_buf_index > 0) {
122 readline_backward_char(rs);
123 readline_delete_char(rs);
bellard7e2515e2004-08-01 21:52:19 +0000124 }
125}
126
aliguori4c36ba32009-03-05 23:01:37 +0000127static void readline_backword(ReadLineState *rs)
ths33fa11d2007-06-25 11:36:50 +0000128{
129 int start;
130
aliguori4c36ba32009-03-05 23:01:37 +0000131 if (rs->cmd_buf_index == 0 || rs->cmd_buf_index > rs->cmd_buf_size) {
ths33fa11d2007-06-25 11:36:50 +0000132 return;
133 }
134
aliguori4c36ba32009-03-05 23:01:37 +0000135 start = rs->cmd_buf_index - 1;
ths33fa11d2007-06-25 11:36:50 +0000136
137 /* find first word (backwards) */
138 while (start > 0) {
aliguori4c36ba32009-03-05 23:01:37 +0000139 if (!qemu_isspace(rs->cmd_buf[start])) {
ths33fa11d2007-06-25 11:36:50 +0000140 break;
141 }
142
143 --start;
144 }
145
146 /* find first space (backwards) */
147 while (start > 0) {
aliguori4c36ba32009-03-05 23:01:37 +0000148 if (qemu_isspace(rs->cmd_buf[start])) {
ths33fa11d2007-06-25 11:36:50 +0000149 ++start;
150 break;
151 }
152
153 --start;
154 }
155
156 /* remove word */
aliguori4c36ba32009-03-05 23:01:37 +0000157 if (start < rs->cmd_buf_index) {
158 memmove(rs->cmd_buf + start,
159 rs->cmd_buf + rs->cmd_buf_index,
160 rs->cmd_buf_size - rs->cmd_buf_index);
161 rs->cmd_buf_size -= rs->cmd_buf_index - start;
162 rs->cmd_buf_index = start;
ths33fa11d2007-06-25 11:36:50 +0000163 }
164}
165
aliguori4c36ba32009-03-05 23:01:37 +0000166static void readline_bol(ReadLineState *rs)
bellard7e2515e2004-08-01 21:52:19 +0000167{
aliguori4c36ba32009-03-05 23:01:37 +0000168 rs->cmd_buf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000169}
170
aliguori4c36ba32009-03-05 23:01:37 +0000171static void readline_eol(ReadLineState *rs)
bellard7e2515e2004-08-01 21:52:19 +0000172{
aliguori4c36ba32009-03-05 23:01:37 +0000173 rs->cmd_buf_index = rs->cmd_buf_size;
bellard7e2515e2004-08-01 21:52:19 +0000174}
175
aliguori4c36ba32009-03-05 23:01:37 +0000176static void readline_up_char(ReadLineState *rs)
bellard7e2515e2004-08-01 21:52:19 +0000177{
178 int idx;
179
aliguori4c36ba32009-03-05 23:01:37 +0000180 if (rs->hist_entry == 0)
bellard7e2515e2004-08-01 21:52:19 +0000181 return;
aliguori4c36ba32009-03-05 23:01:37 +0000182 if (rs->hist_entry == -1) {
bellard7e2515e2004-08-01 21:52:19 +0000183 /* Find latest entry */
aliguori4c36ba32009-03-05 23:01:37 +0000184 for (idx = 0; idx < READLINE_MAX_CMDS; idx++) {
185 if (rs->history[idx] == NULL)
bellard7e2515e2004-08-01 21:52:19 +0000186 break;
187 }
aliguori4c36ba32009-03-05 23:01:37 +0000188 rs->hist_entry = idx;
bellard7e2515e2004-08-01 21:52:19 +0000189 }
aliguori4c36ba32009-03-05 23:01:37 +0000190 rs->hist_entry--;
191 if (rs->hist_entry >= 0) {
192 pstrcpy(rs->cmd_buf, sizeof(rs->cmd_buf),
193 rs->history[rs->hist_entry]);
194 rs->cmd_buf_index = rs->cmd_buf_size = strlen(rs->cmd_buf);
bellard7e2515e2004-08-01 21:52:19 +0000195 }
196}
197
aliguori4c36ba32009-03-05 23:01:37 +0000198static void readline_down_char(ReadLineState *rs)
bellard7e2515e2004-08-01 21:52:19 +0000199{
aliguori5b0d2722009-03-13 15:02:28 +0000200 if (rs->hist_entry == -1)
201 return;
202 if (rs->hist_entry < READLINE_MAX_CMDS - 1 &&
203 rs->history[++rs->hist_entry] != NULL) {
aliguori4c36ba32009-03-05 23:01:37 +0000204 pstrcpy(rs->cmd_buf, sizeof(rs->cmd_buf),
205 rs->history[rs->hist_entry]);
bellard7e2515e2004-08-01 21:52:19 +0000206 } else {
aliguori5b0d2722009-03-13 15:02:28 +0000207 rs->cmd_buf[0] = 0;
aliguori4c36ba32009-03-05 23:01:37 +0000208 rs->hist_entry = -1;
bellard7e2515e2004-08-01 21:52:19 +0000209 }
aliguori4c36ba32009-03-05 23:01:37 +0000210 rs->cmd_buf_index = rs->cmd_buf_size = strlen(rs->cmd_buf);
bellard7e2515e2004-08-01 21:52:19 +0000211}
212
aliguori4c36ba32009-03-05 23:01:37 +0000213static void readline_hist_add(ReadLineState *rs, const char *cmdline)
bellard7e2515e2004-08-01 21:52:19 +0000214{
215 char *hist_entry, *new_entry;
216 int idx;
217
218 if (cmdline[0] == '\0')
219 return;
220 new_entry = NULL;
aliguori4c36ba32009-03-05 23:01:37 +0000221 if (rs->hist_entry != -1) {
bellard7e2515e2004-08-01 21:52:19 +0000222 /* We were editing an existing history entry: replace it */
aliguori4c36ba32009-03-05 23:01:37 +0000223 hist_entry = rs->history[rs->hist_entry];
224 idx = rs->hist_entry;
bellard7e2515e2004-08-01 21:52:19 +0000225 if (strcmp(hist_entry, cmdline) == 0) {
226 goto same_entry;
227 }
228 }
229 /* Search cmdline in history buffers */
aliguori4c36ba32009-03-05 23:01:37 +0000230 for (idx = 0; idx < READLINE_MAX_CMDS; idx++) {
231 hist_entry = rs->history[idx];
bellard7e2515e2004-08-01 21:52:19 +0000232 if (hist_entry == NULL)
233 break;
234 if (strcmp(hist_entry, cmdline) == 0) {
235 same_entry:
236 new_entry = hist_entry;
237 /* Put this entry at the end of history */
aliguori4c36ba32009-03-05 23:01:37 +0000238 memmove(&rs->history[idx], &rs->history[idx + 1],
Markus Armbruster8af42882011-11-04 11:10:01 +0100239 (READLINE_MAX_CMDS - (idx + 1)) * sizeof(char *));
aliguori4c36ba32009-03-05 23:01:37 +0000240 rs->history[READLINE_MAX_CMDS - 1] = NULL;
241 for (; idx < READLINE_MAX_CMDS; idx++) {
242 if (rs->history[idx] == NULL)
bellard7e2515e2004-08-01 21:52:19 +0000243 break;
244 }
245 break;
246 }
247 }
aliguori4c36ba32009-03-05 23:01:37 +0000248 if (idx == READLINE_MAX_CMDS) {
bellard7e2515e2004-08-01 21:52:19 +0000249 /* Need to get one free slot */
Markus Armbrusterc3baa5f2013-01-22 11:07:59 +0100250 g_free(rs->history[0]);
Nickolai Zeldovich7682e852013-01-07 15:38:39 -0500251 memmove(rs->history, &rs->history[1],
252 (READLINE_MAX_CMDS - 1) * sizeof(char *));
aliguori4c36ba32009-03-05 23:01:37 +0000253 rs->history[READLINE_MAX_CMDS - 1] = NULL;
254 idx = READLINE_MAX_CMDS - 1;
bellard7e2515e2004-08-01 21:52:19 +0000255 }
256 if (new_entry == NULL)
Markus Armbrusterc3baa5f2013-01-22 11:07:59 +0100257 new_entry = g_strdup(cmdline);
aliguori4c36ba32009-03-05 23:01:37 +0000258 rs->history[idx] = new_entry;
259 rs->hist_entry = -1;
bellard7e2515e2004-08-01 21:52:19 +0000260}
261
262/* completion support */
263
aliguori4c36ba32009-03-05 23:01:37 +0000264void readline_add_completion(ReadLineState *rs, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000265{
aliguori4c36ba32009-03-05 23:01:37 +0000266 if (rs->nb_completions < READLINE_MAX_COMPLETIONS) {
Hani Benhabilese70871d2014-05-27 23:39:35 +0100267 int i;
268 for (i = 0; i < rs->nb_completions; i++) {
269 if (!strcmp(rs->completions[i], str)) {
270 return;
271 }
272 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500273 rs->completions[rs->nb_completions++] = g_strdup(str);
bellard7e2515e2004-08-01 21:52:19 +0000274 }
275}
276
aliguori4c36ba32009-03-05 23:01:37 +0000277void readline_set_completion_index(ReadLineState *rs, int index)
aliguori376253e2009-03-05 23:01:23 +0000278{
aliguori4c36ba32009-03-05 23:01:37 +0000279 rs->completion_index = index;
aliguori376253e2009-03-05 23:01:23 +0000280}
281
Hani Benhabiles307b2f02014-05-01 15:26:42 +0100282static int completion_comp(const void *a, const void *b)
283{
284 return strcmp(*(const char **) a, *(const char **) b);
285}
286
aliguori4c36ba32009-03-05 23:01:37 +0000287static void readline_completion(ReadLineState *rs)
bellard7e2515e2004-08-01 21:52:19 +0000288{
thsb427c722007-09-25 14:45:23 +0000289 int len, i, j, max_width, nb_cols, max_prefix;
bellard7e2515e2004-08-01 21:52:19 +0000290 char *cmdline;
291
aliguori4c36ba32009-03-05 23:01:37 +0000292 rs->nb_completions = 0;
ths3b46e622007-09-17 08:09:54 +0000293
Michael Tokarev6ad7c322014-05-05 13:23:50 +0400294 cmdline = g_strndup(rs->cmd_buf, rs->cmd_buf_index);
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100295 rs->completion_finder(rs->opaque, cmdline);
Anthony Liguori7267c092011-08-20 22:09:37 -0500296 g_free(cmdline);
bellard7e2515e2004-08-01 21:52:19 +0000297
298 /* no completion found */
aliguori4c36ba32009-03-05 23:01:37 +0000299 if (rs->nb_completions <= 0)
bellard7e2515e2004-08-01 21:52:19 +0000300 return;
aliguori4c36ba32009-03-05 23:01:37 +0000301 if (rs->nb_completions == 1) {
302 len = strlen(rs->completions[0]);
303 for(i = rs->completion_index; i < len; i++) {
304 readline_insert_char(rs, rs->completions[0][i]);
bellard7e2515e2004-08-01 21:52:19 +0000305 }
306 /* extra space for next argument. XXX: make it more generic */
aliguori4c36ba32009-03-05 23:01:37 +0000307 if (len > 0 && rs->completions[0][len - 1] != '/')
308 readline_insert_char(rs, ' ');
bellard7e2515e2004-08-01 21:52:19 +0000309 } else {
Hani Benhabiles307b2f02014-05-01 15:26:42 +0100310 qsort(rs->completions, rs->nb_completions, sizeof(char *),
311 completion_comp);
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100312 rs->printf_func(rs->opaque, "\n");
bellard7e2515e2004-08-01 21:52:19 +0000313 max_width = 0;
thsb427c722007-09-25 14:45:23 +0000314 max_prefix = 0;
aliguori4c36ba32009-03-05 23:01:37 +0000315 for(i = 0; i < rs->nb_completions; i++) {
316 len = strlen(rs->completions[i]);
thsb427c722007-09-25 14:45:23 +0000317 if (i==0) {
318 max_prefix = len;
319 } else {
320 if (len < max_prefix)
321 max_prefix = len;
322 for(j=0; j<max_prefix; j++) {
aliguori4c36ba32009-03-05 23:01:37 +0000323 if (rs->completions[i][j] != rs->completions[0][j])
thsb427c722007-09-25 14:45:23 +0000324 max_prefix = j;
325 }
326 }
bellard7e2515e2004-08-01 21:52:19 +0000327 if (len > max_width)
328 max_width = len;
329 }
thsb427c722007-09-25 14:45:23 +0000330 if (max_prefix > 0)
aliguori4c36ba32009-03-05 23:01:37 +0000331 for(i = rs->completion_index; i < max_prefix; i++) {
332 readline_insert_char(rs, rs->completions[0][i]);
thsb427c722007-09-25 14:45:23 +0000333 }
bellard7e2515e2004-08-01 21:52:19 +0000334 max_width += 2;
335 if (max_width < 10)
336 max_width = 10;
337 else if (max_width > 80)
338 max_width = 80;
339 nb_cols = 80 / max_width;
340 j = 0;
aliguori4c36ba32009-03-05 23:01:37 +0000341 for(i = 0; i < rs->nb_completions; i++) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100342 rs->printf_func(rs->opaque, "%-*s", max_width, rs->completions[i]);
aliguori4c36ba32009-03-05 23:01:37 +0000343 if (++j == nb_cols || i == (rs->nb_completions - 1)) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100344 rs->printf_func(rs->opaque, "\n");
bellard7e2515e2004-08-01 21:52:19 +0000345 j = 0;
346 }
347 }
aliguori4c36ba32009-03-05 23:01:37 +0000348 readline_show_prompt(rs);
bellard7e2515e2004-08-01 21:52:19 +0000349 }
Stefan Weilfc9fa4b2012-05-14 09:47:20 +0000350 for (i = 0; i < rs->nb_completions; i++) {
351 g_free(rs->completions[i]);
352 }
bellard7e2515e2004-08-01 21:52:19 +0000353}
354
Hani Benhabiles075ccb62014-06-01 12:53:35 +0100355static void readline_clear_screen(ReadLineState *rs)
356{
357 rs->printf_func(rs->opaque, "\033[2J\033[1;1H");
358 readline_show_prompt(rs);
359}
360
bellard7e2515e2004-08-01 21:52:19 +0000361/* return true if command handled */
aliguori4c36ba32009-03-05 23:01:37 +0000362void readline_handle_byte(ReadLineState *rs, int ch)
bellard7e2515e2004-08-01 21:52:19 +0000363{
aliguori4c36ba32009-03-05 23:01:37 +0000364 switch(rs->esc_state) {
bellard7e2515e2004-08-01 21:52:19 +0000365 case IS_NORM:
366 switch(ch) {
367 case 1:
aliguori4c36ba32009-03-05 23:01:37 +0000368 readline_bol(rs);
bellard7e2515e2004-08-01 21:52:19 +0000369 break;
370 case 4:
aliguori4c36ba32009-03-05 23:01:37 +0000371 readline_delete_char(rs);
bellard7e2515e2004-08-01 21:52:19 +0000372 break;
373 case 5:
aliguori4c36ba32009-03-05 23:01:37 +0000374 readline_eol(rs);
bellard7e2515e2004-08-01 21:52:19 +0000375 break;
376 case 9:
aliguori4c36ba32009-03-05 23:01:37 +0000377 readline_completion(rs);
bellard7e2515e2004-08-01 21:52:19 +0000378 break;
Hani Benhabiles075ccb62014-06-01 12:53:35 +0100379 case 12:
380 readline_clear_screen(rs);
381 break;
bellard7e2515e2004-08-01 21:52:19 +0000382 case 10:
383 case 13:
aliguori4c36ba32009-03-05 23:01:37 +0000384 rs->cmd_buf[rs->cmd_buf_size] = '\0';
385 if (!rs->read_password)
386 readline_hist_add(rs, rs->cmd_buf);
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100387 rs->printf_func(rs->opaque, "\n");
aliguori4c36ba32009-03-05 23:01:37 +0000388 rs->cmd_buf_index = 0;
389 rs->cmd_buf_size = 0;
390 rs->last_cmd_buf_index = 0;
391 rs->last_cmd_buf_size = 0;
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100392 rs->readline_func(rs->opaque, rs->cmd_buf, rs->readline_opaque);
bellard7e2515e2004-08-01 21:52:19 +0000393 break;
ths33fa11d2007-06-25 11:36:50 +0000394 case 23:
395 /* ^W */
aliguori4c36ba32009-03-05 23:01:37 +0000396 readline_backword(rs);
ths33fa11d2007-06-25 11:36:50 +0000397 break;
bellard7e2515e2004-08-01 21:52:19 +0000398 case 27:
aliguori4c36ba32009-03-05 23:01:37 +0000399 rs->esc_state = IS_ESC;
bellard7e2515e2004-08-01 21:52:19 +0000400 break;
401 case 127:
402 case 8:
aliguori4c36ba32009-03-05 23:01:37 +0000403 readline_backspace(rs);
bellard7e2515e2004-08-01 21:52:19 +0000404 break;
405 case 155:
aliguori4c36ba32009-03-05 23:01:37 +0000406 rs->esc_state = IS_CSI;
bellard7e2515e2004-08-01 21:52:19 +0000407 break;
408 default:
409 if (ch >= 32) {
aliguori4c36ba32009-03-05 23:01:37 +0000410 readline_insert_char(rs, ch);
bellard7e2515e2004-08-01 21:52:19 +0000411 }
412 break;
413 }
414 break;
415 case IS_ESC:
416 if (ch == '[') {
aliguori4c36ba32009-03-05 23:01:37 +0000417 rs->esc_state = IS_CSI;
418 rs->esc_param = 0;
Kevin Wolfd34dc452013-05-14 12:15:54 +0200419 } else if (ch == 'O') {
420 rs->esc_state = IS_SS3;
421 rs->esc_param = 0;
bellard7e2515e2004-08-01 21:52:19 +0000422 } else {
aliguori4c36ba32009-03-05 23:01:37 +0000423 rs->esc_state = IS_NORM;
bellard7e2515e2004-08-01 21:52:19 +0000424 }
425 break;
426 case IS_CSI:
427 switch(ch) {
428 case 'A':
429 case 'F':
aliguori4c36ba32009-03-05 23:01:37 +0000430 readline_up_char(rs);
bellard7e2515e2004-08-01 21:52:19 +0000431 break;
432 case 'B':
433 case 'E':
aliguori4c36ba32009-03-05 23:01:37 +0000434 readline_down_char(rs);
bellard7e2515e2004-08-01 21:52:19 +0000435 break;
436 case 'D':
aliguori4c36ba32009-03-05 23:01:37 +0000437 readline_backward_char(rs);
bellard7e2515e2004-08-01 21:52:19 +0000438 break;
439 case 'C':
aliguori4c36ba32009-03-05 23:01:37 +0000440 readline_forward_char(rs);
bellard7e2515e2004-08-01 21:52:19 +0000441 break;
442 case '0' ... '9':
aliguori4c36ba32009-03-05 23:01:37 +0000443 rs->esc_param = rs->esc_param * 10 + (ch - '0');
bellard7e2515e2004-08-01 21:52:19 +0000444 goto the_end;
445 case '~':
aliguori4c36ba32009-03-05 23:01:37 +0000446 switch(rs->esc_param) {
bellard7e2515e2004-08-01 21:52:19 +0000447 case 1:
aliguori4c36ba32009-03-05 23:01:37 +0000448 readline_bol(rs);
bellard7e2515e2004-08-01 21:52:19 +0000449 break;
450 case 3:
aliguori4c36ba32009-03-05 23:01:37 +0000451 readline_delete_char(rs);
bellard7e2515e2004-08-01 21:52:19 +0000452 break;
453 case 4:
aliguori4c36ba32009-03-05 23:01:37 +0000454 readline_eol(rs);
bellard7e2515e2004-08-01 21:52:19 +0000455 break;
456 }
457 break;
458 default:
459 break;
460 }
aliguori4c36ba32009-03-05 23:01:37 +0000461 rs->esc_state = IS_NORM;
bellard7e2515e2004-08-01 21:52:19 +0000462 the_end:
463 break;
Kevin Wolfd34dc452013-05-14 12:15:54 +0200464 case IS_SS3:
465 switch(ch) {
466 case 'F':
467 readline_eol(rs);
468 break;
469 case 'H':
470 readline_bol(rs);
471 break;
472 }
473 rs->esc_state = IS_NORM;
474 break;
bellard7e2515e2004-08-01 21:52:19 +0000475 }
aliguori4c36ba32009-03-05 23:01:37 +0000476 readline_update(rs);
bellard7e2515e2004-08-01 21:52:19 +0000477}
478
aliguori4c36ba32009-03-05 23:01:37 +0000479void readline_start(ReadLineState *rs, const char *prompt, int read_password,
bellard7e2515e2004-08-01 21:52:19 +0000480 ReadLineFunc *readline_func, void *opaque)
481{
aliguori4c36ba32009-03-05 23:01:37 +0000482 pstrcpy(rs->prompt, sizeof(rs->prompt), prompt);
483 rs->readline_func = readline_func;
484 rs->readline_opaque = opaque;
485 rs->read_password = read_password;
aliguori2724b182009-03-05 23:01:47 +0000486 readline_restart(rs);
487}
488
489void readline_restart(ReadLineState *rs)
490{
aliguori4c36ba32009-03-05 23:01:37 +0000491 rs->cmd_buf_index = 0;
492 rs->cmd_buf_size = 0;
bellard7e2515e2004-08-01 21:52:19 +0000493}
494
aliguori4c36ba32009-03-05 23:01:37 +0000495const char *readline_get_history(ReadLineState *rs, unsigned int index)
bellard7e2515e2004-08-01 21:52:19 +0000496{
aliguori4c36ba32009-03-05 23:01:37 +0000497 if (index >= READLINE_MAX_CMDS)
bellard7e2515e2004-08-01 21:52:19 +0000498 return NULL;
aliguori4c36ba32009-03-05 23:01:37 +0000499 return rs->history[index];
500}
501
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100502ReadLineState *readline_init(ReadLinePrintfFunc *printf_func,
503 ReadLineFlushFunc *flush_func,
504 void *opaque,
aliguori4c36ba32009-03-05 23:01:37 +0000505 ReadLineCompletionFunc *completion_finder)
506{
Anthony Liguori7267c092011-08-20 22:09:37 -0500507 ReadLineState *rs = g_malloc0(sizeof(*rs));
aliguori4c36ba32009-03-05 23:01:37 +0000508
aliguori4c36ba32009-03-05 23:01:37 +0000509 rs->hist_entry = -1;
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100510 rs->opaque = opaque;
511 rs->printf_func = printf_func;
512 rs->flush_func = flush_func;
aliguori4c36ba32009-03-05 23:01:37 +0000513 rs->completion_finder = completion_finder;
514
515 return rs;
bellard7e2515e2004-08-01 21:52:19 +0000516}