]>
Commit | Line | Data |
---|---|---|
7e2515e8 FB |
1 | /* |
2 | * QEMU readline utility | |
5fafdf24 | 3 | * |
7e2515e8 | 4 | * Copyright (c) 2003-2004 Fabrice Bellard |
5fafdf24 | 5 | * |
7e2515e8 FB |
6 | * 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 | */ | |
376253ec AL |
24 | #include "readline.h" |
25 | #include "monitor.h" | |
7e2515e8 | 26 | |
7e2515e8 FB |
27 | #define IS_NORM 0 |
28 | #define IS_ESC 1 | |
29 | #define IS_CSI 2 | |
30 | ||
bc5b6004 | 31 | #ifdef printf |
32 | #undef printf | |
33 | #endif | |
34 | ||
7e2515e8 FB |
35 | #define printf do_not_use_printf |
36 | ||
4c36ba32 | 37 | void readline_show_prompt(ReadLineState *rs) |
7e2515e8 | 38 | { |
4c36ba32 AL |
39 | monitor_printf(rs->mon, "%s", rs->prompt); |
40 | monitor_flush(rs->mon); | |
41 | rs->last_cmd_buf_index = 0; | |
42 | rs->last_cmd_buf_size = 0; | |
43 | rs->esc_state = IS_NORM; | |
7e2515e8 FB |
44 | } |
45 | ||
7e2515e8 | 46 | /* update the displayed command line */ |
4c36ba32 | 47 | static void readline_update(ReadLineState *rs) |
7e2515e8 FB |
48 | { |
49 | int i, delta, len; | |
50 | ||
4c36ba32 AL |
51 | if (rs->cmd_buf_size != rs->last_cmd_buf_size || |
52 | memcmp(rs->cmd_buf, rs->last_cmd_buf, rs->cmd_buf_size) != 0) { | |
53 | for(i = 0; i < rs->last_cmd_buf_index; i++) { | |
54 | monitor_printf(rs->mon, "\033[D"); | |
7e2515e8 | 55 | } |
4c36ba32 AL |
56 | rs->cmd_buf[rs->cmd_buf_size] = '\0'; |
57 | if (rs->read_password) { | |
58 | len = strlen(rs->cmd_buf); | |
7e2515e8 | 59 | for(i = 0; i < len; i++) |
4c36ba32 | 60 | monitor_printf(rs->mon, "*"); |
7e2515e8 | 61 | } else { |
4c36ba32 | 62 | monitor_printf(rs->mon, "%s", rs->cmd_buf); |
7e2515e8 | 63 | } |
4c36ba32 AL |
64 | monitor_printf(rs->mon, "\033[K"); |
65 | memcpy(rs->last_cmd_buf, rs->cmd_buf, rs->cmd_buf_size); | |
66 | rs->last_cmd_buf_size = rs->cmd_buf_size; | |
67 | rs->last_cmd_buf_index = rs->cmd_buf_size; | |
7e2515e8 | 68 | } |
4c36ba32 AL |
69 | if (rs->cmd_buf_index != rs->last_cmd_buf_index) { |
70 | delta = rs->cmd_buf_index - rs->last_cmd_buf_index; | |
7e2515e8 FB |
71 | if (delta > 0) { |
72 | for(i = 0;i < delta; i++) { | |
4c36ba32 | 73 | monitor_printf(rs->mon, "\033[C"); |
7e2515e8 FB |
74 | } |
75 | } else { | |
76 | delta = -delta; | |
77 | for(i = 0;i < delta; i++) { | |
4c36ba32 | 78 | monitor_printf(rs->mon, "\033[D"); |
7e2515e8 FB |
79 | } |
80 | } | |
4c36ba32 | 81 | rs->last_cmd_buf_index = rs->cmd_buf_index; |
7e2515e8 | 82 | } |
4c36ba32 | 83 | monitor_flush(rs->mon); |
7e2515e8 FB |
84 | } |
85 | ||
4c36ba32 | 86 | static void readline_insert_char(ReadLineState *rs, int ch) |
7e2515e8 | 87 | { |
4c36ba32 AL |
88 | if (rs->cmd_buf_index < READLINE_CMD_BUF_SIZE) { |
89 | memmove(rs->cmd_buf + rs->cmd_buf_index + 1, | |
90 | rs->cmd_buf + rs->cmd_buf_index, | |
91 | rs->cmd_buf_size - rs->cmd_buf_index); | |
92 | rs->cmd_buf[rs->cmd_buf_index] = ch; | |
93 | rs->cmd_buf_size++; | |
94 | rs->cmd_buf_index++; | |
7e2515e8 FB |
95 | } |
96 | } | |
97 | ||
4c36ba32 | 98 | static void readline_backward_char(ReadLineState *rs) |
7e2515e8 | 99 | { |
4c36ba32 AL |
100 | if (rs->cmd_buf_index > 0) { |
101 | rs->cmd_buf_index--; | |
7e2515e8 FB |
102 | } |
103 | } | |
104 | ||
4c36ba32 | 105 | static void readline_forward_char(ReadLineState *rs) |
7e2515e8 | 106 | { |
4c36ba32 AL |
107 | if (rs->cmd_buf_index < rs->cmd_buf_size) { |
108 | rs->cmd_buf_index++; | |
7e2515e8 FB |
109 | } |
110 | } | |
111 | ||
4c36ba32 | 112 | static void readline_delete_char(ReadLineState *rs) |
7e2515e8 | 113 | { |
4c36ba32 AL |
114 | if (rs->cmd_buf_index < rs->cmd_buf_size) { |
115 | memmove(rs->cmd_buf + rs->cmd_buf_index, | |
116 | rs->cmd_buf + rs->cmd_buf_index + 1, | |
117 | rs->cmd_buf_size - rs->cmd_buf_index - 1); | |
118 | rs->cmd_buf_size--; | |
7e2515e8 FB |
119 | } |
120 | } | |
121 | ||
4c36ba32 | 122 | static void readline_backspace(ReadLineState *rs) |
7e2515e8 | 123 | { |
4c36ba32 AL |
124 | if (rs->cmd_buf_index > 0) { |
125 | readline_backward_char(rs); | |
126 | readline_delete_char(rs); | |
7e2515e8 FB |
127 | } |
128 | } | |
129 | ||
4c36ba32 | 130 | static void readline_backword(ReadLineState *rs) |
33fa11d4 TS |
131 | { |
132 | int start; | |
133 | ||
4c36ba32 | 134 | if (rs->cmd_buf_index == 0 || rs->cmd_buf_index > rs->cmd_buf_size) { |
33fa11d4 TS |
135 | return; |
136 | } | |
137 | ||
4c36ba32 | 138 | start = rs->cmd_buf_index - 1; |
33fa11d4 TS |
139 | |
140 | /* find first word (backwards) */ | |
141 | while (start > 0) { | |
4c36ba32 | 142 | if (!qemu_isspace(rs->cmd_buf[start])) { |
33fa11d4 TS |
143 | break; |
144 | } | |
145 | ||
146 | --start; | |
147 | } | |
148 | ||
149 | /* find first space (backwards) */ | |
150 | while (start > 0) { | |
4c36ba32 | 151 | if (qemu_isspace(rs->cmd_buf[start])) { |
33fa11d4 TS |
152 | ++start; |
153 | break; | |
154 | } | |
155 | ||
156 | --start; | |
157 | } | |
158 | ||
159 | /* remove word */ | |
4c36ba32 AL |
160 | if (start < rs->cmd_buf_index) { |
161 | memmove(rs->cmd_buf + start, | |
162 | rs->cmd_buf + rs->cmd_buf_index, | |
163 | rs->cmd_buf_size - rs->cmd_buf_index); | |
164 | rs->cmd_buf_size -= rs->cmd_buf_index - start; | |
165 | rs->cmd_buf_index = start; | |
33fa11d4 TS |
166 | } |
167 | } | |
168 | ||
4c36ba32 | 169 | static void readline_bol(ReadLineState *rs) |
7e2515e8 | 170 | { |
4c36ba32 | 171 | rs->cmd_buf_index = 0; |
7e2515e8 FB |
172 | } |
173 | ||
4c36ba32 | 174 | static void readline_eol(ReadLineState *rs) |
7e2515e8 | 175 | { |
4c36ba32 | 176 | rs->cmd_buf_index = rs->cmd_buf_size; |
7e2515e8 FB |
177 | } |
178 | ||
4c36ba32 | 179 | static void readline_up_char(ReadLineState *rs) |
7e2515e8 FB |
180 | { |
181 | int idx; | |
182 | ||
4c36ba32 | 183 | if (rs->hist_entry == 0) |
7e2515e8 | 184 | return; |
4c36ba32 | 185 | if (rs->hist_entry == -1) { |
7e2515e8 | 186 | /* Find latest entry */ |
4c36ba32 AL |
187 | for (idx = 0; idx < READLINE_MAX_CMDS; idx++) { |
188 | if (rs->history[idx] == NULL) | |
7e2515e8 FB |
189 | break; |
190 | } | |
4c36ba32 | 191 | rs->hist_entry = idx; |
7e2515e8 | 192 | } |
4c36ba32 AL |
193 | rs->hist_entry--; |
194 | if (rs->hist_entry >= 0) { | |
195 | pstrcpy(rs->cmd_buf, sizeof(rs->cmd_buf), | |
196 | rs->history[rs->hist_entry]); | |
197 | rs->cmd_buf_index = rs->cmd_buf_size = strlen(rs->cmd_buf); | |
7e2515e8 FB |
198 | } |
199 | } | |
200 | ||
4c36ba32 | 201 | static void readline_down_char(ReadLineState *rs) |
7e2515e8 | 202 | { |
5b0d2727 AL |
203 | if (rs->hist_entry == -1) |
204 | return; | |
205 | if (rs->hist_entry < READLINE_MAX_CMDS - 1 && | |
206 | rs->history[++rs->hist_entry] != NULL) { | |
4c36ba32 AL |
207 | pstrcpy(rs->cmd_buf, sizeof(rs->cmd_buf), |
208 | rs->history[rs->hist_entry]); | |
7e2515e8 | 209 | } else { |
5b0d2727 | 210 | rs->cmd_buf[0] = 0; |
4c36ba32 | 211 | rs->hist_entry = -1; |
7e2515e8 | 212 | } |
4c36ba32 | 213 | rs->cmd_buf_index = rs->cmd_buf_size = strlen(rs->cmd_buf); |
7e2515e8 FB |
214 | } |
215 | ||
4c36ba32 | 216 | static void readline_hist_add(ReadLineState *rs, const char *cmdline) |
7e2515e8 FB |
217 | { |
218 | char *hist_entry, *new_entry; | |
219 | int idx; | |
220 | ||
221 | if (cmdline[0] == '\0') | |
222 | return; | |
223 | new_entry = NULL; | |
4c36ba32 | 224 | if (rs->hist_entry != -1) { |
7e2515e8 | 225 | /* We were editing an existing history entry: replace it */ |
4c36ba32 AL |
226 | hist_entry = rs->history[rs->hist_entry]; |
227 | idx = rs->hist_entry; | |
7e2515e8 FB |
228 | if (strcmp(hist_entry, cmdline) == 0) { |
229 | goto same_entry; | |
230 | } | |
231 | } | |
232 | /* Search cmdline in history buffers */ | |
4c36ba32 AL |
233 | for (idx = 0; idx < READLINE_MAX_CMDS; idx++) { |
234 | hist_entry = rs->history[idx]; | |
7e2515e8 FB |
235 | if (hist_entry == NULL) |
236 | break; | |
237 | if (strcmp(hist_entry, cmdline) == 0) { | |
238 | same_entry: | |
239 | new_entry = hist_entry; | |
240 | /* Put this entry at the end of history */ | |
4c36ba32 AL |
241 | memmove(&rs->history[idx], &rs->history[idx + 1], |
242 | (READLINE_MAX_CMDS - idx + 1) * sizeof(char *)); | |
243 | rs->history[READLINE_MAX_CMDS - 1] = NULL; | |
244 | for (; idx < READLINE_MAX_CMDS; idx++) { | |
245 | if (rs->history[idx] == NULL) | |
7e2515e8 FB |
246 | break; |
247 | } | |
248 | break; | |
249 | } | |
250 | } | |
4c36ba32 | 251 | if (idx == READLINE_MAX_CMDS) { |
7e2515e8 | 252 | /* Need to get one free slot */ |
4c36ba32 AL |
253 | free(rs->history[0]); |
254 | memcpy(rs->history, &rs->history[1], | |
255 | (READLINE_MAX_CMDS - 1) * sizeof(char *)); | |
256 | rs->history[READLINE_MAX_CMDS - 1] = NULL; | |
257 | idx = READLINE_MAX_CMDS - 1; | |
7e2515e8 FB |
258 | } |
259 | if (new_entry == NULL) | |
260 | new_entry = strdup(cmdline); | |
4c36ba32 AL |
261 | rs->history[idx] = new_entry; |
262 | rs->hist_entry = -1; | |
7e2515e8 FB |
263 | } |
264 | ||
265 | /* completion support */ | |
266 | ||
4c36ba32 | 267 | void readline_add_completion(ReadLineState *rs, const char *str) |
7e2515e8 | 268 | { |
4c36ba32 AL |
269 | if (rs->nb_completions < READLINE_MAX_COMPLETIONS) { |
270 | rs->completions[rs->nb_completions++] = qemu_strdup(str); | |
7e2515e8 FB |
271 | } |
272 | } | |
273 | ||
4c36ba32 | 274 | void readline_set_completion_index(ReadLineState *rs, int index) |
376253ec | 275 | { |
4c36ba32 | 276 | rs->completion_index = index; |
376253ec AL |
277 | } |
278 | ||
4c36ba32 | 279 | static void readline_completion(ReadLineState *rs) |
7e2515e8 | 280 | { |
376253ec | 281 | Monitor *mon = cur_mon; |
b427c726 | 282 | int len, i, j, max_width, nb_cols, max_prefix; |
7e2515e8 FB |
283 | char *cmdline; |
284 | ||
4c36ba32 | 285 | rs->nb_completions = 0; |
3b46e624 | 286 | |
4c36ba32 AL |
287 | cmdline = qemu_malloc(rs->cmd_buf_index + 1); |
288 | memcpy(cmdline, rs->cmd_buf, rs->cmd_buf_index); | |
289 | cmdline[rs->cmd_buf_index] = '\0'; | |
290 | rs->completion_finder(cmdline); | |
7e2515e8 FB |
291 | qemu_free(cmdline); |
292 | ||
293 | /* no completion found */ | |
4c36ba32 | 294 | if (rs->nb_completions <= 0) |
7e2515e8 | 295 | return; |
4c36ba32 AL |
296 | if (rs->nb_completions == 1) { |
297 | len = strlen(rs->completions[0]); | |
298 | for(i = rs->completion_index; i < len; i++) { | |
299 | readline_insert_char(rs, rs->completions[0][i]); | |
7e2515e8 FB |
300 | } |
301 | /* extra space for next argument. XXX: make it more generic */ | |
4c36ba32 AL |
302 | if (len > 0 && rs->completions[0][len - 1] != '/') |
303 | readline_insert_char(rs, ' '); | |
7e2515e8 | 304 | } else { |
376253ec | 305 | monitor_printf(mon, "\n"); |
7e2515e8 | 306 | max_width = 0; |
b427c726 | 307 | max_prefix = 0; |
4c36ba32 AL |
308 | for(i = 0; i < rs->nb_completions; i++) { |
309 | len = strlen(rs->completions[i]); | |
b427c726 TS |
310 | if (i==0) { |
311 | max_prefix = len; | |
312 | } else { | |
313 | if (len < max_prefix) | |
314 | max_prefix = len; | |
315 | for(j=0; j<max_prefix; j++) { | |
4c36ba32 | 316 | if (rs->completions[i][j] != rs->completions[0][j]) |
b427c726 TS |
317 | max_prefix = j; |
318 | } | |
319 | } | |
7e2515e8 FB |
320 | if (len > max_width) |
321 | max_width = len; | |
322 | } | |
b427c726 | 323 | if (max_prefix > 0) |
4c36ba32 AL |
324 | for(i = rs->completion_index; i < max_prefix; i++) { |
325 | readline_insert_char(rs, rs->completions[0][i]); | |
b427c726 | 326 | } |
7e2515e8 FB |
327 | max_width += 2; |
328 | if (max_width < 10) | |
329 | max_width = 10; | |
330 | else if (max_width > 80) | |
331 | max_width = 80; | |
332 | nb_cols = 80 / max_width; | |
333 | j = 0; | |
4c36ba32 AL |
334 | for(i = 0; i < rs->nb_completions; i++) { |
335 | monitor_printf(rs->mon, "%-*s", max_width, rs->completions[i]); | |
336 | if (++j == nb_cols || i == (rs->nb_completions - 1)) { | |
337 | monitor_printf(rs->mon, "\n"); | |
7e2515e8 FB |
338 | j = 0; |
339 | } | |
340 | } | |
4c36ba32 | 341 | readline_show_prompt(rs); |
7e2515e8 FB |
342 | } |
343 | } | |
344 | ||
345 | /* return true if command handled */ | |
4c36ba32 | 346 | void readline_handle_byte(ReadLineState *rs, int ch) |
7e2515e8 | 347 | { |
4c36ba32 | 348 | switch(rs->esc_state) { |
7e2515e8 FB |
349 | case IS_NORM: |
350 | switch(ch) { | |
351 | case 1: | |
4c36ba32 | 352 | readline_bol(rs); |
7e2515e8 FB |
353 | break; |
354 | case 4: | |
4c36ba32 | 355 | readline_delete_char(rs); |
7e2515e8 FB |
356 | break; |
357 | case 5: | |
4c36ba32 | 358 | readline_eol(rs); |
7e2515e8 FB |
359 | break; |
360 | case 9: | |
4c36ba32 | 361 | readline_completion(rs); |
7e2515e8 FB |
362 | break; |
363 | case 10: | |
364 | case 13: | |
4c36ba32 AL |
365 | rs->cmd_buf[rs->cmd_buf_size] = '\0'; |
366 | if (!rs->read_password) | |
367 | readline_hist_add(rs, rs->cmd_buf); | |
368 | monitor_printf(rs->mon, "\n"); | |
369 | rs->cmd_buf_index = 0; | |
370 | rs->cmd_buf_size = 0; | |
371 | rs->last_cmd_buf_index = 0; | |
372 | rs->last_cmd_buf_size = 0; | |
373 | rs->readline_func(rs->mon, rs->cmd_buf, rs->readline_opaque); | |
7e2515e8 | 374 | break; |
33fa11d4 TS |
375 | case 23: |
376 | /* ^W */ | |
4c36ba32 | 377 | readline_backword(rs); |
33fa11d4 | 378 | break; |
7e2515e8 | 379 | case 27: |
4c36ba32 | 380 | rs->esc_state = IS_ESC; |
7e2515e8 FB |
381 | break; |
382 | case 127: | |
383 | case 8: | |
4c36ba32 | 384 | readline_backspace(rs); |
7e2515e8 FB |
385 | break; |
386 | case 155: | |
4c36ba32 | 387 | rs->esc_state = IS_CSI; |
7e2515e8 FB |
388 | break; |
389 | default: | |
390 | if (ch >= 32) { | |
4c36ba32 | 391 | readline_insert_char(rs, ch); |
7e2515e8 FB |
392 | } |
393 | break; | |
394 | } | |
395 | break; | |
396 | case IS_ESC: | |
397 | if (ch == '[') { | |
4c36ba32 AL |
398 | rs->esc_state = IS_CSI; |
399 | rs->esc_param = 0; | |
7e2515e8 | 400 | } else { |
4c36ba32 | 401 | rs->esc_state = IS_NORM; |
7e2515e8 FB |
402 | } |
403 | break; | |
404 | case IS_CSI: | |
405 | switch(ch) { | |
406 | case 'A': | |
407 | case 'F': | |
4c36ba32 | 408 | readline_up_char(rs); |
7e2515e8 FB |
409 | break; |
410 | case 'B': | |
411 | case 'E': | |
4c36ba32 | 412 | readline_down_char(rs); |
7e2515e8 FB |
413 | break; |
414 | case 'D': | |
4c36ba32 | 415 | readline_backward_char(rs); |
7e2515e8 FB |
416 | break; |
417 | case 'C': | |
4c36ba32 | 418 | readline_forward_char(rs); |
7e2515e8 FB |
419 | break; |
420 | case '0' ... '9': | |
4c36ba32 | 421 | rs->esc_param = rs->esc_param * 10 + (ch - '0'); |
7e2515e8 FB |
422 | goto the_end; |
423 | case '~': | |
4c36ba32 | 424 | switch(rs->esc_param) { |
7e2515e8 | 425 | case 1: |
4c36ba32 | 426 | readline_bol(rs); |
7e2515e8 FB |
427 | break; |
428 | case 3: | |
4c36ba32 | 429 | readline_delete_char(rs); |
7e2515e8 FB |
430 | break; |
431 | case 4: | |
4c36ba32 | 432 | readline_eol(rs); |
7e2515e8 FB |
433 | break; |
434 | } | |
435 | break; | |
436 | default: | |
437 | break; | |
438 | } | |
4c36ba32 | 439 | rs->esc_state = IS_NORM; |
7e2515e8 FB |
440 | the_end: |
441 | break; | |
442 | } | |
4c36ba32 | 443 | readline_update(rs); |
7e2515e8 FB |
444 | } |
445 | ||
4c36ba32 | 446 | void readline_start(ReadLineState *rs, const char *prompt, int read_password, |
7e2515e8 FB |
447 | ReadLineFunc *readline_func, void *opaque) |
448 | { | |
4c36ba32 AL |
449 | pstrcpy(rs->prompt, sizeof(rs->prompt), prompt); |
450 | rs->readline_func = readline_func; | |
451 | rs->readline_opaque = opaque; | |
452 | rs->read_password = read_password; | |
2724b180 AL |
453 | readline_restart(rs); |
454 | } | |
455 | ||
456 | void readline_restart(ReadLineState *rs) | |
457 | { | |
4c36ba32 AL |
458 | rs->cmd_buf_index = 0; |
459 | rs->cmd_buf_size = 0; | |
7e2515e8 FB |
460 | } |
461 | ||
4c36ba32 | 462 | const char *readline_get_history(ReadLineState *rs, unsigned int index) |
7e2515e8 | 463 | { |
4c36ba32 | 464 | if (index >= READLINE_MAX_CMDS) |
7e2515e8 | 465 | return NULL; |
4c36ba32 AL |
466 | return rs->history[index]; |
467 | } | |
468 | ||
469 | ReadLineState *readline_init(Monitor *mon, | |
470 | ReadLineCompletionFunc *completion_finder) | |
471 | { | |
472 | ReadLineState *rs = qemu_mallocz(sizeof(*rs)); | |
473 | ||
4c36ba32 AL |
474 | rs->hist_entry = -1; |
475 | rs->mon = mon; | |
476 | rs->completion_finder = completion_finder; | |
477 | ||
478 | return rs; | |
7e2515e8 | 479 | } |