blob: f1ac6e476940446f869bf1b354504628b8484ae5 [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 Hajnoczi0150cd82013-11-14 11:54:15 +010026#include "qemu/readline.h"
Markus Armbruster856dfd82019-05-23 16:35:06 +020027#include "qemu/ctype.h"
Veronia Bahaaf348b6d2016-03-20 19:16:19 +020028#include "qemu/cutils.h"
bellard7e2515e2004-08-01 21:52:19 +000029
bellard7e2515e2004-08-01 21:52:19 +000030#define IS_NORM 0
31#define IS_ESC 1
32#define IS_CSI 2
Kevin Wolfd34dc452013-05-14 12:15:54 +020033#define IS_SS3 3
bellard7e2515e2004-08-01 21:52:19 +000034
aliguori4c36ba32009-03-05 23:01:37 +000035void readline_show_prompt(ReadLineState *rs)
bellard7e2515e2004-08-01 21:52:19 +000036{
Stefan Hajnoczic60bf332013-11-14 11:54:14 +010037 rs->printf_func(rs->opaque, "%s", rs->prompt);
38 rs->flush_func(rs->opaque);
aliguori4c36ba32009-03-05 23:01:37 +000039 rs->last_cmd_buf_index = 0;
40 rs->last_cmd_buf_size = 0;
41 rs->esc_state = IS_NORM;
bellard7e2515e2004-08-01 21:52:19 +000042}
43
bellard7e2515e2004-08-01 21:52:19 +000044/* update the displayed command line */
aliguori4c36ba32009-03-05 23:01:37 +000045static void readline_update(ReadLineState *rs)
bellard7e2515e2004-08-01 21:52:19 +000046{
47 int i, delta, len;
48
aliguori4c36ba32009-03-05 23:01:37 +000049 if (rs->cmd_buf_size != rs->last_cmd_buf_size ||
50 memcmp(rs->cmd_buf, rs->last_cmd_buf, rs->cmd_buf_size) != 0) {
Jules Irenge2467f952019-04-01 03:44:05 +010051 for (i = 0; i < rs->last_cmd_buf_index; i++) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +010052 rs->printf_func(rs->opaque, "\033[D");
bellard7e2515e2004-08-01 21:52:19 +000053 }
aliguori4c36ba32009-03-05 23:01:37 +000054 rs->cmd_buf[rs->cmd_buf_size] = '\0';
55 if (rs->read_password) {
56 len = strlen(rs->cmd_buf);
Jules Irengee238a992019-03-30 11:21:42 +000057 for (i = 0; i < len; i++) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +010058 rs->printf_func(rs->opaque, "*");
Jules Irengee238a992019-03-30 11:21:42 +000059 }
bellard7e2515e2004-08-01 21:52:19 +000060 } else {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +010061 rs->printf_func(rs->opaque, "%s", rs->cmd_buf);
bellard7e2515e2004-08-01 21:52:19 +000062 }
Stefan Hajnoczic60bf332013-11-14 11:54:14 +010063 rs->printf_func(rs->opaque, "\033[K");
aliguori4c36ba32009-03-05 23:01:37 +000064 memcpy(rs->last_cmd_buf, rs->cmd_buf, rs->cmd_buf_size);
65 rs->last_cmd_buf_size = rs->cmd_buf_size;
66 rs->last_cmd_buf_index = rs->cmd_buf_size;
bellard7e2515e2004-08-01 21:52:19 +000067 }
aliguori4c36ba32009-03-05 23:01:37 +000068 if (rs->cmd_buf_index != rs->last_cmd_buf_index) {
69 delta = rs->cmd_buf_index - rs->last_cmd_buf_index;
bellard7e2515e2004-08-01 21:52:19 +000070 if (delta > 0) {
Jules Irenge2467f952019-04-01 03:44:05 +010071 for (i = 0; i < delta; i++) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +010072 rs->printf_func(rs->opaque, "\033[C");
bellard7e2515e2004-08-01 21:52:19 +000073 }
74 } else {
75 delta = -delta;
Jules Irenge2467f952019-04-01 03:44:05 +010076 for (i = 0; i < delta; i++) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +010077 rs->printf_func(rs->opaque, "\033[D");
bellard7e2515e2004-08-01 21:52:19 +000078 }
79 }
aliguori4c36ba32009-03-05 23:01:37 +000080 rs->last_cmd_buf_index = rs->cmd_buf_index;
bellard7e2515e2004-08-01 21:52:19 +000081 }
Stefan Hajnoczic60bf332013-11-14 11:54:14 +010082 rs->flush_func(rs->opaque);
bellard7e2515e2004-08-01 21:52:19 +000083}
84
aliguori4c36ba32009-03-05 23:01:37 +000085static void readline_insert_char(ReadLineState *rs, int ch)
bellard7e2515e2004-08-01 21:52:19 +000086{
aliguori4c36ba32009-03-05 23:01:37 +000087 if (rs->cmd_buf_index < READLINE_CMD_BUF_SIZE) {
88 memmove(rs->cmd_buf + rs->cmd_buf_index + 1,
89 rs->cmd_buf + rs->cmd_buf_index,
90 rs->cmd_buf_size - rs->cmd_buf_index);
91 rs->cmd_buf[rs->cmd_buf_index] = ch;
92 rs->cmd_buf_size++;
93 rs->cmd_buf_index++;
bellard7e2515e2004-08-01 21:52:19 +000094 }
95}
96
aliguori4c36ba32009-03-05 23:01:37 +000097static void readline_backward_char(ReadLineState *rs)
bellard7e2515e2004-08-01 21:52:19 +000098{
aliguori4c36ba32009-03-05 23:01:37 +000099 if (rs->cmd_buf_index > 0) {
100 rs->cmd_buf_index--;
bellard7e2515e2004-08-01 21:52:19 +0000101 }
102}
103
aliguori4c36ba32009-03-05 23:01:37 +0000104static void readline_forward_char(ReadLineState *rs)
bellard7e2515e2004-08-01 21:52:19 +0000105{
aliguori4c36ba32009-03-05 23:01:37 +0000106 if (rs->cmd_buf_index < rs->cmd_buf_size) {
107 rs->cmd_buf_index++;
bellard7e2515e2004-08-01 21:52:19 +0000108 }
109}
110
aliguori4c36ba32009-03-05 23:01:37 +0000111static void readline_delete_char(ReadLineState *rs)
bellard7e2515e2004-08-01 21:52:19 +0000112{
aliguori4c36ba32009-03-05 23:01:37 +0000113 if (rs->cmd_buf_index < rs->cmd_buf_size) {
114 memmove(rs->cmd_buf + rs->cmd_buf_index,
115 rs->cmd_buf + rs->cmd_buf_index + 1,
116 rs->cmd_buf_size - rs->cmd_buf_index - 1);
117 rs->cmd_buf_size--;
bellard7e2515e2004-08-01 21:52:19 +0000118 }
119}
120
aliguori4c36ba32009-03-05 23:01:37 +0000121static void readline_backspace(ReadLineState *rs)
bellard7e2515e2004-08-01 21:52:19 +0000122{
aliguori4c36ba32009-03-05 23:01:37 +0000123 if (rs->cmd_buf_index > 0) {
124 readline_backward_char(rs);
125 readline_delete_char(rs);
bellard7e2515e2004-08-01 21:52:19 +0000126 }
127}
128
aliguori4c36ba32009-03-05 23:01:37 +0000129static void readline_backword(ReadLineState *rs)
ths33fa11d2007-06-25 11:36:50 +0000130{
131 int start;
132
aliguori4c36ba32009-03-05 23:01:37 +0000133 if (rs->cmd_buf_index == 0 || rs->cmd_buf_index > rs->cmd_buf_size) {
ths33fa11d2007-06-25 11:36:50 +0000134 return;
135 }
136
aliguori4c36ba32009-03-05 23:01:37 +0000137 start = rs->cmd_buf_index - 1;
ths33fa11d2007-06-25 11:36:50 +0000138
139 /* find first word (backwards) */
140 while (start > 0) {
aliguori4c36ba32009-03-05 23:01:37 +0000141 if (!qemu_isspace(rs->cmd_buf[start])) {
ths33fa11d2007-06-25 11:36:50 +0000142 break;
143 }
144
145 --start;
146 }
147
148 /* find first space (backwards) */
149 while (start > 0) {
aliguori4c36ba32009-03-05 23:01:37 +0000150 if (qemu_isspace(rs->cmd_buf[start])) {
ths33fa11d2007-06-25 11:36:50 +0000151 ++start;
152 break;
153 }
154
155 --start;
156 }
157
158 /* remove word */
aliguori4c36ba32009-03-05 23:01:37 +0000159 if (start < rs->cmd_buf_index) {
160 memmove(rs->cmd_buf + start,
161 rs->cmd_buf + rs->cmd_buf_index,
162 rs->cmd_buf_size - rs->cmd_buf_index);
163 rs->cmd_buf_size -= rs->cmd_buf_index - start;
164 rs->cmd_buf_index = start;
ths33fa11d2007-06-25 11:36:50 +0000165 }
166}
167
aliguori4c36ba32009-03-05 23:01:37 +0000168static void readline_bol(ReadLineState *rs)
bellard7e2515e2004-08-01 21:52:19 +0000169{
aliguori4c36ba32009-03-05 23:01:37 +0000170 rs->cmd_buf_index = 0;
bellard7e2515e2004-08-01 21:52:19 +0000171}
172
aliguori4c36ba32009-03-05 23:01:37 +0000173static void readline_eol(ReadLineState *rs)
bellard7e2515e2004-08-01 21:52:19 +0000174{
aliguori4c36ba32009-03-05 23:01:37 +0000175 rs->cmd_buf_index = rs->cmd_buf_size;
bellard7e2515e2004-08-01 21:52:19 +0000176}
177
aliguori4c36ba32009-03-05 23:01:37 +0000178static void readline_up_char(ReadLineState *rs)
bellard7e2515e2004-08-01 21:52:19 +0000179{
180 int idx;
181
Jules Irengee238a992019-03-30 11:21:42 +0000182 if (rs->hist_entry == 0) {
Jules Irengec95d4a22019-04-01 03:44:06 +0100183 return;
Jules Irengee238a992019-03-30 11:21:42 +0000184 }
aliguori4c36ba32009-03-05 23:01:37 +0000185 if (rs->hist_entry == -1) {
Jules Irengec95d4a22019-04-01 03:44:06 +0100186 /* Find latest entry */
187 for (idx = 0; idx < READLINE_MAX_CMDS; idx++) {
Jules Irengee238a992019-03-30 11:21:42 +0000188 if (rs->history[idx] == NULL) {
Jules Irengec95d4a22019-04-01 03:44:06 +0100189 break;
Jules Irengee238a992019-03-30 11:21:42 +0000190 }
Jules Irengec95d4a22019-04-01 03:44:06 +0100191 }
192 rs->hist_entry = idx;
bellard7e2515e2004-08-01 21:52:19 +0000193 }
aliguori4c36ba32009-03-05 23:01:37 +0000194 rs->hist_entry--;
195 if (rs->hist_entry >= 0) {
Jules Irengec95d4a22019-04-01 03:44:06 +0100196 pstrcpy(rs->cmd_buf, sizeof(rs->cmd_buf),
aliguori4c36ba32009-03-05 23:01:37 +0000197 rs->history[rs->hist_entry]);
Jules Irengec95d4a22019-04-01 03:44:06 +0100198 rs->cmd_buf_index = rs->cmd_buf_size = strlen(rs->cmd_buf);
bellard7e2515e2004-08-01 21:52:19 +0000199 }
200}
201
aliguori4c36ba32009-03-05 23:01:37 +0000202static void readline_down_char(ReadLineState *rs)
bellard7e2515e2004-08-01 21:52:19 +0000203{
Jules Irengee238a992019-03-30 11:21:42 +0000204 if (rs->hist_entry == -1) {
aliguori5b0d2722009-03-13 15:02:28 +0000205 return;
Jules Irengee238a992019-03-30 11:21:42 +0000206 }
aliguori5b0d2722009-03-13 15:02:28 +0000207 if (rs->hist_entry < READLINE_MAX_CMDS - 1 &&
208 rs->history[++rs->hist_entry] != NULL) {
Jules Irengec95d4a22019-04-01 03:44:06 +0100209 pstrcpy(rs->cmd_buf, sizeof(rs->cmd_buf),
aliguori4c36ba32009-03-05 23:01:37 +0000210 rs->history[rs->hist_entry]);
bellard7e2515e2004-08-01 21:52:19 +0000211 } else {
aliguori5b0d2722009-03-13 15:02:28 +0000212 rs->cmd_buf[0] = 0;
Jules Irengec95d4a22019-04-01 03:44:06 +0100213 rs->hist_entry = -1;
bellard7e2515e2004-08-01 21:52:19 +0000214 }
aliguori4c36ba32009-03-05 23:01:37 +0000215 rs->cmd_buf_index = rs->cmd_buf_size = strlen(rs->cmd_buf);
bellard7e2515e2004-08-01 21:52:19 +0000216}
217
aliguori4c36ba32009-03-05 23:01:37 +0000218static void readline_hist_add(ReadLineState *rs, const char *cmdline)
bellard7e2515e2004-08-01 21:52:19 +0000219{
220 char *hist_entry, *new_entry;
221 int idx;
222
Jules Irengee238a992019-03-30 11:21:42 +0000223 if (cmdline[0] == '\0') {
Jules Irengec95d4a22019-04-01 03:44:06 +0100224 return;
Jules Irengee238a992019-03-30 11:21:42 +0000225 }
bellard7e2515e2004-08-01 21:52:19 +0000226 new_entry = NULL;
aliguori4c36ba32009-03-05 23:01:37 +0000227 if (rs->hist_entry != -1) {
Jules Irengec95d4a22019-04-01 03:44:06 +0100228 /* We were editing an existing history entry: replace it */
229 hist_entry = rs->history[rs->hist_entry];
230 idx = rs->hist_entry;
231 if (strcmp(hist_entry, cmdline) == 0) {
232 goto same_entry;
233 }
bellard7e2515e2004-08-01 21:52:19 +0000234 }
235 /* Search cmdline in history buffers */
aliguori4c36ba32009-03-05 23:01:37 +0000236 for (idx = 0; idx < READLINE_MAX_CMDS; idx++) {
Jules Irengec95d4a22019-04-01 03:44:06 +0100237 hist_entry = rs->history[idx];
Jules Irengee238a992019-03-30 11:21:42 +0000238 if (hist_entry == NULL) {
Jules Irengec95d4a22019-04-01 03:44:06 +0100239 break;
Jules Irengee238a992019-03-30 11:21:42 +0000240 }
Jules Irengec95d4a22019-04-01 03:44:06 +0100241 if (strcmp(hist_entry, cmdline) == 0) {
242 same_entry:
Alex Chen593621f2020-12-03 13:50:43 +0000243 if (idx == READLINE_MAX_CMDS - 1) {
244 return;
245 }
Jules Irengec95d4a22019-04-01 03:44:06 +0100246 new_entry = hist_entry;
247 /* Put this entry at the end of history */
248 memmove(&rs->history[idx], &rs->history[idx + 1],
249 (READLINE_MAX_CMDS - (idx + 1)) * sizeof(char *));
250 rs->history[READLINE_MAX_CMDS - 1] = NULL;
251 for (; idx < READLINE_MAX_CMDS; idx++) {
Jules Irengee238a992019-03-30 11:21:42 +0000252 if (rs->history[idx] == NULL) {
Jules Irengec95d4a22019-04-01 03:44:06 +0100253 break;
Jules Irengee238a992019-03-30 11:21:42 +0000254 }
Jules Irengec95d4a22019-04-01 03:44:06 +0100255 }
256 break;
257 }
bellard7e2515e2004-08-01 21:52:19 +0000258 }
aliguori4c36ba32009-03-05 23:01:37 +0000259 if (idx == READLINE_MAX_CMDS) {
Jules Irengec95d4a22019-04-01 03:44:06 +0100260 /* Need to get one free slot */
Markus Armbrusterc3baa5f2013-01-22 11:07:59 +0100261 g_free(rs->history[0]);
Jules Irengec95d4a22019-04-01 03:44:06 +0100262 memmove(rs->history, &rs->history[1],
263 (READLINE_MAX_CMDS - 1) * sizeof(char *));
264 rs->history[READLINE_MAX_CMDS - 1] = NULL;
265 idx = READLINE_MAX_CMDS - 1;
bellard7e2515e2004-08-01 21:52:19 +0000266 }
Jules Irengee238a992019-03-30 11:21:42 +0000267 if (new_entry == NULL) {
Markus Armbrusterc3baa5f2013-01-22 11:07:59 +0100268 new_entry = g_strdup(cmdline);
Jules Irengee238a992019-03-30 11:21:42 +0000269 }
aliguori4c36ba32009-03-05 23:01:37 +0000270 rs->history[idx] = new_entry;
271 rs->hist_entry = -1;
bellard7e2515e2004-08-01 21:52:19 +0000272}
273
274/* completion support */
275
aliguori4c36ba32009-03-05 23:01:37 +0000276void readline_add_completion(ReadLineState *rs, const char *str)
bellard7e2515e2004-08-01 21:52:19 +0000277{
aliguori4c36ba32009-03-05 23:01:37 +0000278 if (rs->nb_completions < READLINE_MAX_COMPLETIONS) {
Hani Benhabilese70871d2014-05-27 23:39:35 +0100279 int i;
280 for (i = 0; i < rs->nb_completions; i++) {
281 if (!strcmp(rs->completions[i], str)) {
282 return;
283 }
284 }
Anthony Liguori7267c092011-08-20 22:09:37 -0500285 rs->completions[rs->nb_completions++] = g_strdup(str);
bellard7e2515e2004-08-01 21:52:19 +0000286 }
287}
288
aliguori4c36ba32009-03-05 23:01:37 +0000289void readline_set_completion_index(ReadLineState *rs, int index)
aliguori376253e2009-03-05 23:01:23 +0000290{
aliguori4c36ba32009-03-05 23:01:37 +0000291 rs->completion_index = index;
aliguori376253e2009-03-05 23:01:23 +0000292}
293
Hani Benhabiles307b2f02014-05-01 15:26:42 +0100294static int completion_comp(const void *a, const void *b)
295{
296 return strcmp(*(const char **) a, *(const char **) b);
297}
298
aliguori4c36ba32009-03-05 23:01:37 +0000299static void readline_completion(ReadLineState *rs)
bellard7e2515e2004-08-01 21:52:19 +0000300{
thsb427c722007-09-25 14:45:23 +0000301 int len, i, j, max_width, nb_cols, max_prefix;
bellard7e2515e2004-08-01 21:52:19 +0000302 char *cmdline;
303
aliguori4c36ba32009-03-05 23:01:37 +0000304 rs->nb_completions = 0;
ths3b46e622007-09-17 08:09:54 +0000305
Michael Tokarev6ad7c322014-05-05 13:23:50 +0400306 cmdline = g_strndup(rs->cmd_buf, rs->cmd_buf_index);
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100307 rs->completion_finder(rs->opaque, cmdline);
Anthony Liguori7267c092011-08-20 22:09:37 -0500308 g_free(cmdline);
bellard7e2515e2004-08-01 21:52:19 +0000309
310 /* no completion found */
Jules Irengee238a992019-03-30 11:21:42 +0000311 if (rs->nb_completions <= 0) {
bellard7e2515e2004-08-01 21:52:19 +0000312 return;
Jules Irengee238a992019-03-30 11:21:42 +0000313 }
aliguori4c36ba32009-03-05 23:01:37 +0000314 if (rs->nb_completions == 1) {
315 len = strlen(rs->completions[0]);
Jules Irenge2467f952019-04-01 03:44:05 +0100316 for (i = rs->completion_index; i < len; i++) {
aliguori4c36ba32009-03-05 23:01:37 +0000317 readline_insert_char(rs, rs->completions[0][i]);
bellard7e2515e2004-08-01 21:52:19 +0000318 }
319 /* extra space for next argument. XXX: make it more generic */
Jules Irengee238a992019-03-30 11:21:42 +0000320 if (len > 0 && rs->completions[0][len - 1] != '/') {
aliguori4c36ba32009-03-05 23:01:37 +0000321 readline_insert_char(rs, ' ');
Jules Irengee238a992019-03-30 11:21:42 +0000322 }
bellard7e2515e2004-08-01 21:52:19 +0000323 } else {
Hani Benhabiles307b2f02014-05-01 15:26:42 +0100324 qsort(rs->completions, rs->nb_completions, sizeof(char *),
325 completion_comp);
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100326 rs->printf_func(rs->opaque, "\n");
bellard7e2515e2004-08-01 21:52:19 +0000327 max_width = 0;
Jules Irenge2467f952019-04-01 03:44:05 +0100328 max_prefix = 0;
329 for (i = 0; i < rs->nb_completions; i++) {
aliguori4c36ba32009-03-05 23:01:37 +0000330 len = strlen(rs->completions[i]);
Jules Irenge2467f952019-04-01 03:44:05 +0100331 if (i == 0) {
thsb427c722007-09-25 14:45:23 +0000332 max_prefix = len;
333 } else {
Jules Irengee238a992019-03-30 11:21:42 +0000334 if (len < max_prefix) {
thsb427c722007-09-25 14:45:23 +0000335 max_prefix = len;
Jules Irengee238a992019-03-30 11:21:42 +0000336 }
Jules Irenge2467f952019-04-01 03:44:05 +0100337 for (j = 0; j < max_prefix; j++) {
Jules Irengee238a992019-03-30 11:21:42 +0000338 if (rs->completions[i][j] != rs->completions[0][j]) {
thsb427c722007-09-25 14:45:23 +0000339 max_prefix = j;
Jules Irengee238a992019-03-30 11:21:42 +0000340 }
thsb427c722007-09-25 14:45:23 +0000341 }
342 }
Jules Irengee238a992019-03-30 11:21:42 +0000343 if (len > max_width) {
bellard7e2515e2004-08-01 21:52:19 +0000344 max_width = len;
Jules Irengee238a992019-03-30 11:21:42 +0000345 }
bellard7e2515e2004-08-01 21:52:19 +0000346 }
Jules Irenge2467f952019-04-01 03:44:05 +0100347 if (max_prefix > 0)
348 for (i = rs->completion_index; i < max_prefix; i++) {
aliguori4c36ba32009-03-05 23:01:37 +0000349 readline_insert_char(rs, rs->completions[0][i]);
thsb427c722007-09-25 14:45:23 +0000350 }
bellard7e2515e2004-08-01 21:52:19 +0000351 max_width += 2;
Jules Irengee238a992019-03-30 11:21:42 +0000352 if (max_width < 10) {
bellard7e2515e2004-08-01 21:52:19 +0000353 max_width = 10;
Jules Irengee238a992019-03-30 11:21:42 +0000354 } else if (max_width > 80) {
bellard7e2515e2004-08-01 21:52:19 +0000355 max_width = 80;
Jules Irengee238a992019-03-30 11:21:42 +0000356 }
bellard7e2515e2004-08-01 21:52:19 +0000357 nb_cols = 80 / max_width;
358 j = 0;
Jules Irenge2467f952019-04-01 03:44:05 +0100359 for (i = 0; i < rs->nb_completions; i++) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100360 rs->printf_func(rs->opaque, "%-*s", max_width, rs->completions[i]);
aliguori4c36ba32009-03-05 23:01:37 +0000361 if (++j == nb_cols || i == (rs->nb_completions - 1)) {
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100362 rs->printf_func(rs->opaque, "\n");
bellard7e2515e2004-08-01 21:52:19 +0000363 j = 0;
364 }
365 }
aliguori4c36ba32009-03-05 23:01:37 +0000366 readline_show_prompt(rs);
bellard7e2515e2004-08-01 21:52:19 +0000367 }
Stefan Weilfc9fa4b2012-05-14 09:47:20 +0000368 for (i = 0; i < rs->nb_completions; i++) {
369 g_free(rs->completions[i]);
370 }
bellard7e2515e2004-08-01 21:52:19 +0000371}
372
Hani Benhabiles075ccb62014-06-01 12:53:35 +0100373static void readline_clear_screen(ReadLineState *rs)
374{
375 rs->printf_func(rs->opaque, "\033[2J\033[1;1H");
376 readline_show_prompt(rs);
377}
378
bellard7e2515e2004-08-01 21:52:19 +0000379/* return true if command handled */
aliguori4c36ba32009-03-05 23:01:37 +0000380void readline_handle_byte(ReadLineState *rs, int ch)
bellard7e2515e2004-08-01 21:52:19 +0000381{
Jules Irenge2467f952019-04-01 03:44:05 +0100382 switch (rs->esc_state) {
bellard7e2515e2004-08-01 21:52:19 +0000383 case IS_NORM:
Jules Irenge2467f952019-04-01 03:44:05 +0100384 switch (ch) {
bellard7e2515e2004-08-01 21:52:19 +0000385 case 1:
aliguori4c36ba32009-03-05 23:01:37 +0000386 readline_bol(rs);
bellard7e2515e2004-08-01 21:52:19 +0000387 break;
388 case 4:
aliguori4c36ba32009-03-05 23:01:37 +0000389 readline_delete_char(rs);
bellard7e2515e2004-08-01 21:52:19 +0000390 break;
391 case 5:
aliguori4c36ba32009-03-05 23:01:37 +0000392 readline_eol(rs);
bellard7e2515e2004-08-01 21:52:19 +0000393 break;
394 case 9:
aliguori4c36ba32009-03-05 23:01:37 +0000395 readline_completion(rs);
bellard7e2515e2004-08-01 21:52:19 +0000396 break;
Hani Benhabiles075ccb62014-06-01 12:53:35 +0100397 case 12:
398 readline_clear_screen(rs);
399 break;
bellard7e2515e2004-08-01 21:52:19 +0000400 case 10:
401 case 13:
aliguori4c36ba32009-03-05 23:01:37 +0000402 rs->cmd_buf[rs->cmd_buf_size] = '\0';
Jules Irengee238a992019-03-30 11:21:42 +0000403 if (!rs->read_password) {
aliguori4c36ba32009-03-05 23:01:37 +0000404 readline_hist_add(rs, rs->cmd_buf);
Jules Irengee238a992019-03-30 11:21:42 +0000405 }
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100406 rs->printf_func(rs->opaque, "\n");
aliguori4c36ba32009-03-05 23:01:37 +0000407 rs->cmd_buf_index = 0;
408 rs->cmd_buf_size = 0;
409 rs->last_cmd_buf_index = 0;
410 rs->last_cmd_buf_size = 0;
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100411 rs->readline_func(rs->opaque, rs->cmd_buf, rs->readline_opaque);
bellard7e2515e2004-08-01 21:52:19 +0000412 break;
ths33fa11d2007-06-25 11:36:50 +0000413 case 23:
414 /* ^W */
aliguori4c36ba32009-03-05 23:01:37 +0000415 readline_backword(rs);
ths33fa11d2007-06-25 11:36:50 +0000416 break;
bellard7e2515e2004-08-01 21:52:19 +0000417 case 27:
aliguori4c36ba32009-03-05 23:01:37 +0000418 rs->esc_state = IS_ESC;
bellard7e2515e2004-08-01 21:52:19 +0000419 break;
420 case 127:
421 case 8:
aliguori4c36ba32009-03-05 23:01:37 +0000422 readline_backspace(rs);
bellard7e2515e2004-08-01 21:52:19 +0000423 break;
Jules Irengec95d4a22019-04-01 03:44:06 +0100424 case 155:
aliguori4c36ba32009-03-05 23:01:37 +0000425 rs->esc_state = IS_CSI;
Jules Irengec95d4a22019-04-01 03:44:06 +0100426 break;
bellard7e2515e2004-08-01 21:52:19 +0000427 default:
428 if (ch >= 32) {
aliguori4c36ba32009-03-05 23:01:37 +0000429 readline_insert_char(rs, ch);
bellard7e2515e2004-08-01 21:52:19 +0000430 }
431 break;
432 }
433 break;
434 case IS_ESC:
435 if (ch == '[') {
aliguori4c36ba32009-03-05 23:01:37 +0000436 rs->esc_state = IS_CSI;
437 rs->esc_param = 0;
Kevin Wolfd34dc452013-05-14 12:15:54 +0200438 } else if (ch == 'O') {
439 rs->esc_state = IS_SS3;
440 rs->esc_param = 0;
bellard7e2515e2004-08-01 21:52:19 +0000441 } else {
aliguori4c36ba32009-03-05 23:01:37 +0000442 rs->esc_state = IS_NORM;
bellard7e2515e2004-08-01 21:52:19 +0000443 }
444 break;
445 case IS_CSI:
Jules Irenge2467f952019-04-01 03:44:05 +0100446 switch (ch) {
Jules Irengec95d4a22019-04-01 03:44:06 +0100447 case 'A':
448 case 'F':
449 readline_up_char(rs);
450 break;
451 case 'B':
452 case 'E':
453 readline_down_char(rs);
454 break;
bellard7e2515e2004-08-01 21:52:19 +0000455 case 'D':
aliguori4c36ba32009-03-05 23:01:37 +0000456 readline_backward_char(rs);
bellard7e2515e2004-08-01 21:52:19 +0000457 break;
458 case 'C':
aliguori4c36ba32009-03-05 23:01:37 +0000459 readline_forward_char(rs);
bellard7e2515e2004-08-01 21:52:19 +0000460 break;
461 case '0' ... '9':
aliguori4c36ba32009-03-05 23:01:37 +0000462 rs->esc_param = rs->esc_param * 10 + (ch - '0');
bellard7e2515e2004-08-01 21:52:19 +0000463 goto the_end;
464 case '~':
Jules Irenge2467f952019-04-01 03:44:05 +0100465 switch (rs->esc_param) {
bellard7e2515e2004-08-01 21:52:19 +0000466 case 1:
aliguori4c36ba32009-03-05 23:01:37 +0000467 readline_bol(rs);
bellard7e2515e2004-08-01 21:52:19 +0000468 break;
469 case 3:
aliguori4c36ba32009-03-05 23:01:37 +0000470 readline_delete_char(rs);
bellard7e2515e2004-08-01 21:52:19 +0000471 break;
472 case 4:
aliguori4c36ba32009-03-05 23:01:37 +0000473 readline_eol(rs);
bellard7e2515e2004-08-01 21:52:19 +0000474 break;
475 }
476 break;
477 default:
478 break;
479 }
aliguori4c36ba32009-03-05 23:01:37 +0000480 rs->esc_state = IS_NORM;
bellard7e2515e2004-08-01 21:52:19 +0000481 the_end:
482 break;
Kevin Wolfd34dc452013-05-14 12:15:54 +0200483 case IS_SS3:
Jules Irenge2467f952019-04-01 03:44:05 +0100484 switch (ch) {
Kevin Wolfd34dc452013-05-14 12:15:54 +0200485 case 'F':
486 readline_eol(rs);
487 break;
488 case 'H':
489 readline_bol(rs);
490 break;
491 }
492 rs->esc_state = IS_NORM;
493 break;
bellard7e2515e2004-08-01 21:52:19 +0000494 }
aliguori4c36ba32009-03-05 23:01:37 +0000495 readline_update(rs);
bellard7e2515e2004-08-01 21:52:19 +0000496}
497
aliguori4c36ba32009-03-05 23:01:37 +0000498void readline_start(ReadLineState *rs, const char *prompt, int read_password,
bellard7e2515e2004-08-01 21:52:19 +0000499 ReadLineFunc *readline_func, void *opaque)
500{
aliguori4c36ba32009-03-05 23:01:37 +0000501 pstrcpy(rs->prompt, sizeof(rs->prompt), prompt);
502 rs->readline_func = readline_func;
503 rs->readline_opaque = opaque;
504 rs->read_password = read_password;
aliguori2724b182009-03-05 23:01:47 +0000505 readline_restart(rs);
506}
507
508void readline_restart(ReadLineState *rs)
509{
aliguori4c36ba32009-03-05 23:01:37 +0000510 rs->cmd_buf_index = 0;
511 rs->cmd_buf_size = 0;
bellard7e2515e2004-08-01 21:52:19 +0000512}
513
aliguori4c36ba32009-03-05 23:01:37 +0000514const char *readline_get_history(ReadLineState *rs, unsigned int index)
bellard7e2515e2004-08-01 21:52:19 +0000515{
Jules Irengee238a992019-03-30 11:21:42 +0000516 if (index >= READLINE_MAX_CMDS) {
bellard7e2515e2004-08-01 21:52:19 +0000517 return NULL;
Jules Irengee238a992019-03-30 11:21:42 +0000518 }
aliguori4c36ba32009-03-05 23:01:37 +0000519 return rs->history[index];
520}
521
Marc-André Lureaue5dc1a62018-01-04 17:05:15 +0100522void readline_free(ReadLineState *rs)
523{
524 int i;
525
526 if (!rs) {
527 return;
528 }
529 for (i = 0; i < READLINE_MAX_CMDS; i++) {
530 g_free(rs->history[i]);
531 }
Marc-André Lureaue5dc1a62018-01-04 17:05:15 +0100532 g_free(rs);
533}
534
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100535ReadLineState *readline_init(ReadLinePrintfFunc *printf_func,
536 ReadLineFlushFunc *flush_func,
537 void *opaque,
aliguori4c36ba32009-03-05 23:01:37 +0000538 ReadLineCompletionFunc *completion_finder)
539{
Marc-André Lureaue5dc1a62018-01-04 17:05:15 +0100540 ReadLineState *rs = g_new0(ReadLineState, 1);
aliguori4c36ba32009-03-05 23:01:37 +0000541
aliguori4c36ba32009-03-05 23:01:37 +0000542 rs->hist_entry = -1;
Stefan Hajnoczic60bf332013-11-14 11:54:14 +0100543 rs->opaque = opaque;
544 rs->printf_func = printf_func;
545 rs->flush_func = flush_func;
aliguori4c36ba32009-03-05 23:01:37 +0000546 rs->completion_finder = completion_finder;
547
548 return rs;
bellard7e2515e2004-08-01 21:52:19 +0000549}