]>
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 | */ | |
c60bf339 | 24 | |
aafd7584 | 25 | #include "qemu/osdep.h" |
0150cd81 | 26 | #include "qemu/readline.h" |
856dfd8a | 27 | #include "qemu/ctype.h" |
f348b6d1 | 28 | #include "qemu/cutils.h" |
7e2515e8 | 29 | |
7e2515e8 FB |
30 | #define IS_NORM 0 |
31 | #define IS_ESC 1 | |
32 | #define IS_CSI 2 | |
d34dc45d | 33 | #define IS_SS3 3 |
7e2515e8 | 34 | |
4c36ba32 | 35 | void readline_show_prompt(ReadLineState *rs) |
7e2515e8 | 36 | { |
c60bf339 SH |
37 | rs->printf_func(rs->opaque, "%s", rs->prompt); |
38 | rs->flush_func(rs->opaque); | |
4c36ba32 AL |
39 | rs->last_cmd_buf_index = 0; |
40 | rs->last_cmd_buf_size = 0; | |
41 | rs->esc_state = IS_NORM; | |
7e2515e8 FB |
42 | } |
43 | ||
7e2515e8 | 44 | /* update the displayed command line */ |
4c36ba32 | 45 | static void readline_update(ReadLineState *rs) |
7e2515e8 FB |
46 | { |
47 | int i, delta, len; | |
48 | ||
4c36ba32 AL |
49 | if (rs->cmd_buf_size != rs->last_cmd_buf_size || |
50 | memcmp(rs->cmd_buf, rs->last_cmd_buf, rs->cmd_buf_size) != 0) { | |
2467f95a | 51 | for (i = 0; i < rs->last_cmd_buf_index; i++) { |
c60bf339 | 52 | rs->printf_func(rs->opaque, "\033[D"); |
7e2515e8 | 53 | } |
4c36ba32 AL |
54 | rs->cmd_buf[rs->cmd_buf_size] = '\0'; |
55 | if (rs->read_password) { | |
56 | len = strlen(rs->cmd_buf); | |
e238a99e | 57 | for (i = 0; i < len; i++) { |
c60bf339 | 58 | rs->printf_func(rs->opaque, "*"); |
e238a99e | 59 | } |
7e2515e8 | 60 | } else { |
c60bf339 | 61 | rs->printf_func(rs->opaque, "%s", rs->cmd_buf); |
7e2515e8 | 62 | } |
c60bf339 | 63 | rs->printf_func(rs->opaque, "\033[K"); |
4c36ba32 AL |
64 | 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; | |
7e2515e8 | 67 | } |
4c36ba32 AL |
68 | if (rs->cmd_buf_index != rs->last_cmd_buf_index) { |
69 | delta = rs->cmd_buf_index - rs->last_cmd_buf_index; | |
7e2515e8 | 70 | if (delta > 0) { |
2467f95a | 71 | for (i = 0; i < delta; i++) { |
c60bf339 | 72 | rs->printf_func(rs->opaque, "\033[C"); |
7e2515e8 FB |
73 | } |
74 | } else { | |
75 | delta = -delta; | |
2467f95a | 76 | for (i = 0; i < delta; i++) { |
c60bf339 | 77 | rs->printf_func(rs->opaque, "\033[D"); |
7e2515e8 FB |
78 | } |
79 | } | |
4c36ba32 | 80 | rs->last_cmd_buf_index = rs->cmd_buf_index; |
7e2515e8 | 81 | } |
c60bf339 | 82 | rs->flush_func(rs->opaque); |
7e2515e8 FB |
83 | } |
84 | ||
4c36ba32 | 85 | static void readline_insert_char(ReadLineState *rs, int ch) |
7e2515e8 | 86 | { |
4c36ba32 AL |
87 | 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++; | |
7e2515e8 FB |
94 | } |
95 | } | |
96 | ||
4c36ba32 | 97 | static void readline_backward_char(ReadLineState *rs) |
7e2515e8 | 98 | { |
4c36ba32 AL |
99 | if (rs->cmd_buf_index > 0) { |
100 | rs->cmd_buf_index--; | |
7e2515e8 FB |
101 | } |
102 | } | |
103 | ||
4c36ba32 | 104 | static void readline_forward_char(ReadLineState *rs) |
7e2515e8 | 105 | { |
4c36ba32 AL |
106 | if (rs->cmd_buf_index < rs->cmd_buf_size) { |
107 | rs->cmd_buf_index++; | |
7e2515e8 FB |
108 | } |
109 | } | |
110 | ||
4c36ba32 | 111 | static void readline_delete_char(ReadLineState *rs) |
7e2515e8 | 112 | { |
4c36ba32 AL |
113 | 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--; | |
7e2515e8 FB |
118 | } |
119 | } | |
120 | ||
4c36ba32 | 121 | static void readline_backspace(ReadLineState *rs) |
7e2515e8 | 122 | { |
4c36ba32 AL |
123 | if (rs->cmd_buf_index > 0) { |
124 | readline_backward_char(rs); | |
125 | readline_delete_char(rs); | |
7e2515e8 FB |
126 | } |
127 | } | |
128 | ||
4c36ba32 | 129 | static void readline_backword(ReadLineState *rs) |
33fa11d4 TS |
130 | { |
131 | int start; | |
132 | ||
4c36ba32 | 133 | if (rs->cmd_buf_index == 0 || rs->cmd_buf_index > rs->cmd_buf_size) { |
33fa11d4 TS |
134 | return; |
135 | } | |
136 | ||
4c36ba32 | 137 | start = rs->cmd_buf_index - 1; |
33fa11d4 TS |
138 | |
139 | /* find first word (backwards) */ | |
140 | while (start > 0) { | |
4c36ba32 | 141 | if (!qemu_isspace(rs->cmd_buf[start])) { |
33fa11d4 TS |
142 | break; |
143 | } | |
144 | ||
145 | --start; | |
146 | } | |
147 | ||
148 | /* find first space (backwards) */ | |
149 | while (start > 0) { | |
4c36ba32 | 150 | if (qemu_isspace(rs->cmd_buf[start])) { |
33fa11d4 TS |
151 | ++start; |
152 | break; | |
153 | } | |
154 | ||
155 | --start; | |
156 | } | |
157 | ||
158 | /* remove word */ | |
4c36ba32 AL |
159 | 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; | |
33fa11d4 TS |
165 | } |
166 | } | |
167 | ||
4c36ba32 | 168 | static void readline_bol(ReadLineState *rs) |
7e2515e8 | 169 | { |
4c36ba32 | 170 | rs->cmd_buf_index = 0; |
7e2515e8 FB |
171 | } |
172 | ||
4c36ba32 | 173 | static void readline_eol(ReadLineState *rs) |
7e2515e8 | 174 | { |
4c36ba32 | 175 | rs->cmd_buf_index = rs->cmd_buf_size; |
7e2515e8 FB |
176 | } |
177 | ||
4c36ba32 | 178 | static void readline_up_char(ReadLineState *rs) |
7e2515e8 FB |
179 | { |
180 | int idx; | |
181 | ||
e238a99e | 182 | if (rs->hist_entry == 0) { |
c95d4a29 | 183 | return; |
e238a99e | 184 | } |
4c36ba32 | 185 | if (rs->hist_entry == -1) { |
c95d4a29 JI |
186 | /* Find latest entry */ |
187 | for (idx = 0; idx < READLINE_MAX_CMDS; idx++) { | |
e238a99e | 188 | if (rs->history[idx] == NULL) { |
c95d4a29 | 189 | break; |
e238a99e | 190 | } |
c95d4a29 JI |
191 | } |
192 | rs->hist_entry = idx; | |
7e2515e8 | 193 | } |
4c36ba32 AL |
194 | rs->hist_entry--; |
195 | if (rs->hist_entry >= 0) { | |
c95d4a29 | 196 | pstrcpy(rs->cmd_buf, sizeof(rs->cmd_buf), |
4c36ba32 | 197 | rs->history[rs->hist_entry]); |
c95d4a29 | 198 | rs->cmd_buf_index = rs->cmd_buf_size = strlen(rs->cmd_buf); |
7e2515e8 FB |
199 | } |
200 | } | |
201 | ||
4c36ba32 | 202 | static void readline_down_char(ReadLineState *rs) |
7e2515e8 | 203 | { |
e238a99e | 204 | if (rs->hist_entry == -1) { |
5b0d2727 | 205 | return; |
e238a99e | 206 | } |
5b0d2727 AL |
207 | if (rs->hist_entry < READLINE_MAX_CMDS - 1 && |
208 | rs->history[++rs->hist_entry] != NULL) { | |
c95d4a29 | 209 | pstrcpy(rs->cmd_buf, sizeof(rs->cmd_buf), |
4c36ba32 | 210 | rs->history[rs->hist_entry]); |
7e2515e8 | 211 | } else { |
5b0d2727 | 212 | rs->cmd_buf[0] = 0; |
c95d4a29 | 213 | rs->hist_entry = -1; |
7e2515e8 | 214 | } |
4c36ba32 | 215 | rs->cmd_buf_index = rs->cmd_buf_size = strlen(rs->cmd_buf); |
7e2515e8 FB |
216 | } |
217 | ||
4c36ba32 | 218 | static void readline_hist_add(ReadLineState *rs, const char *cmdline) |
7e2515e8 FB |
219 | { |
220 | char *hist_entry, *new_entry; | |
221 | int idx; | |
222 | ||
e238a99e | 223 | if (cmdline[0] == '\0') { |
c95d4a29 | 224 | return; |
e238a99e | 225 | } |
7e2515e8 | 226 | new_entry = NULL; |
4c36ba32 | 227 | if (rs->hist_entry != -1) { |
c95d4a29 JI |
228 | /* 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 | } | |
7e2515e8 FB |
234 | } |
235 | /* Search cmdline in history buffers */ | |
4c36ba32 | 236 | for (idx = 0; idx < READLINE_MAX_CMDS; idx++) { |
c95d4a29 | 237 | hist_entry = rs->history[idx]; |
e238a99e | 238 | if (hist_entry == NULL) { |
c95d4a29 | 239 | break; |
e238a99e | 240 | } |
c95d4a29 JI |
241 | if (strcmp(hist_entry, cmdline) == 0) { |
242 | same_entry: | |
593621f3 AC |
243 | if (idx == READLINE_MAX_CMDS - 1) { |
244 | return; | |
245 | } | |
c95d4a29 JI |
246 | 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++) { | |
e238a99e | 252 | if (rs->history[idx] == NULL) { |
c95d4a29 | 253 | break; |
e238a99e | 254 | } |
c95d4a29 JI |
255 | } |
256 | break; | |
257 | } | |
7e2515e8 | 258 | } |
4c36ba32 | 259 | if (idx == READLINE_MAX_CMDS) { |
c95d4a29 | 260 | /* Need to get one free slot */ |
c3baa5f9 | 261 | g_free(rs->history[0]); |
c95d4a29 JI |
262 | 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; | |
7e2515e8 | 266 | } |
e238a99e | 267 | if (new_entry == NULL) { |
c3baa5f9 | 268 | new_entry = g_strdup(cmdline); |
e238a99e | 269 | } |
4c36ba32 AL |
270 | rs->history[idx] = new_entry; |
271 | rs->hist_entry = -1; | |
7e2515e8 FB |
272 | } |
273 | ||
274 | /* completion support */ | |
275 | ||
4c36ba32 | 276 | void readline_add_completion(ReadLineState *rs, const char *str) |
7e2515e8 | 277 | { |
4c36ba32 | 278 | if (rs->nb_completions < READLINE_MAX_COMPLETIONS) { |
e70871d8 HB |
279 | int i; |
280 | for (i = 0; i < rs->nb_completions; i++) { | |
281 | if (!strcmp(rs->completions[i], str)) { | |
282 | return; | |
283 | } | |
284 | } | |
7267c094 | 285 | rs->completions[rs->nb_completions++] = g_strdup(str); |
7e2515e8 FB |
286 | } |
287 | } | |
288 | ||
4c36ba32 | 289 | void readline_set_completion_index(ReadLineState *rs, int index) |
376253ec | 290 | { |
4c36ba32 | 291 | rs->completion_index = index; |
376253ec AL |
292 | } |
293 | ||
307b2f01 HB |
294 | static int completion_comp(const void *a, const void *b) |
295 | { | |
296 | return strcmp(*(const char **) a, *(const char **) b); | |
297 | } | |
298 | ||
4c36ba32 | 299 | static void readline_completion(ReadLineState *rs) |
7e2515e8 | 300 | { |
b427c726 | 301 | int len, i, j, max_width, nb_cols, max_prefix; |
7e2515e8 FB |
302 | char *cmdline; |
303 | ||
4c36ba32 | 304 | rs->nb_completions = 0; |
3b46e624 | 305 | |
6ad7c326 | 306 | cmdline = g_strndup(rs->cmd_buf, rs->cmd_buf_index); |
c60bf339 | 307 | rs->completion_finder(rs->opaque, cmdline); |
7267c094 | 308 | g_free(cmdline); |
7e2515e8 FB |
309 | |
310 | /* no completion found */ | |
e238a99e | 311 | if (rs->nb_completions <= 0) { |
7e2515e8 | 312 | return; |
e238a99e | 313 | } |
4c36ba32 AL |
314 | if (rs->nb_completions == 1) { |
315 | len = strlen(rs->completions[0]); | |
2467f95a | 316 | for (i = rs->completion_index; i < len; i++) { |
4c36ba32 | 317 | readline_insert_char(rs, rs->completions[0][i]); |
7e2515e8 FB |
318 | } |
319 | /* extra space for next argument. XXX: make it more generic */ | |
e238a99e | 320 | if (len > 0 && rs->completions[0][len - 1] != '/') { |
4c36ba32 | 321 | readline_insert_char(rs, ' '); |
e238a99e | 322 | } |
7e2515e8 | 323 | } else { |
307b2f01 HB |
324 | qsort(rs->completions, rs->nb_completions, sizeof(char *), |
325 | completion_comp); | |
c60bf339 | 326 | rs->printf_func(rs->opaque, "\n"); |
7e2515e8 | 327 | max_width = 0; |
2467f95a JI |
328 | max_prefix = 0; |
329 | for (i = 0; i < rs->nb_completions; i++) { | |
4c36ba32 | 330 | len = strlen(rs->completions[i]); |
2467f95a | 331 | if (i == 0) { |
b427c726 TS |
332 | max_prefix = len; |
333 | } else { | |
e238a99e | 334 | if (len < max_prefix) { |
b427c726 | 335 | max_prefix = len; |
e238a99e | 336 | } |
2467f95a | 337 | for (j = 0; j < max_prefix; j++) { |
e238a99e | 338 | if (rs->completions[i][j] != rs->completions[0][j]) { |
b427c726 | 339 | max_prefix = j; |
e238a99e | 340 | } |
b427c726 TS |
341 | } |
342 | } | |
e238a99e | 343 | if (len > max_width) { |
7e2515e8 | 344 | max_width = len; |
e238a99e | 345 | } |
7e2515e8 | 346 | } |
2467f95a JI |
347 | if (max_prefix > 0) |
348 | for (i = rs->completion_index; i < max_prefix; i++) { | |
4c36ba32 | 349 | readline_insert_char(rs, rs->completions[0][i]); |
b427c726 | 350 | } |
7e2515e8 | 351 | max_width += 2; |
e238a99e | 352 | if (max_width < 10) { |
7e2515e8 | 353 | max_width = 10; |
e238a99e | 354 | } else if (max_width > 80) { |
7e2515e8 | 355 | max_width = 80; |
e238a99e | 356 | } |
7e2515e8 FB |
357 | nb_cols = 80 / max_width; |
358 | j = 0; | |
2467f95a | 359 | for (i = 0; i < rs->nb_completions; i++) { |
c60bf339 | 360 | rs->printf_func(rs->opaque, "%-*s", max_width, rs->completions[i]); |
4c36ba32 | 361 | if (++j == nb_cols || i == (rs->nb_completions - 1)) { |
c60bf339 | 362 | rs->printf_func(rs->opaque, "\n"); |
7e2515e8 FB |
363 | j = 0; |
364 | } | |
365 | } | |
4c36ba32 | 366 | readline_show_prompt(rs); |
7e2515e8 | 367 | } |
fc9fa4bd SW |
368 | for (i = 0; i < rs->nb_completions; i++) { |
369 | g_free(rs->completions[i]); | |
370 | } | |
7e2515e8 FB |
371 | } |
372 | ||
075ccb6c HB |
373 | static 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 | ||
7e2515e8 | 379 | /* return true if command handled */ |
4c36ba32 | 380 | void readline_handle_byte(ReadLineState *rs, int ch) |
7e2515e8 | 381 | { |
2467f95a | 382 | switch (rs->esc_state) { |
7e2515e8 | 383 | case IS_NORM: |
2467f95a | 384 | switch (ch) { |
7e2515e8 | 385 | case 1: |
4c36ba32 | 386 | readline_bol(rs); |
7e2515e8 FB |
387 | break; |
388 | case 4: | |
4c36ba32 | 389 | readline_delete_char(rs); |
7e2515e8 FB |
390 | break; |
391 | case 5: | |
4c36ba32 | 392 | readline_eol(rs); |
7e2515e8 FB |
393 | break; |
394 | case 9: | |
4c36ba32 | 395 | readline_completion(rs); |
7e2515e8 | 396 | break; |
075ccb6c HB |
397 | case 12: |
398 | readline_clear_screen(rs); | |
399 | break; | |
7e2515e8 FB |
400 | case 10: |
401 | case 13: | |
4c36ba32 | 402 | rs->cmd_buf[rs->cmd_buf_size] = '\0'; |
e238a99e | 403 | if (!rs->read_password) { |
4c36ba32 | 404 | readline_hist_add(rs, rs->cmd_buf); |
e238a99e | 405 | } |
c60bf339 | 406 | rs->printf_func(rs->opaque, "\n"); |
4c36ba32 AL |
407 | 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; | |
c60bf339 | 411 | rs->readline_func(rs->opaque, rs->cmd_buf, rs->readline_opaque); |
7e2515e8 | 412 | break; |
33fa11d4 TS |
413 | case 23: |
414 | /* ^W */ | |
4c36ba32 | 415 | readline_backword(rs); |
33fa11d4 | 416 | break; |
7e2515e8 | 417 | case 27: |
4c36ba32 | 418 | rs->esc_state = IS_ESC; |
7e2515e8 FB |
419 | break; |
420 | case 127: | |
421 | case 8: | |
4c36ba32 | 422 | readline_backspace(rs); |
7e2515e8 | 423 | break; |
c95d4a29 | 424 | case 155: |
4c36ba32 | 425 | rs->esc_state = IS_CSI; |
c95d4a29 | 426 | break; |
7e2515e8 FB |
427 | default: |
428 | if (ch >= 32) { | |
4c36ba32 | 429 | readline_insert_char(rs, ch); |
7e2515e8 FB |
430 | } |
431 | break; | |
432 | } | |
433 | break; | |
434 | case IS_ESC: | |
435 | if (ch == '[') { | |
4c36ba32 AL |
436 | rs->esc_state = IS_CSI; |
437 | rs->esc_param = 0; | |
d34dc45d KW |
438 | } else if (ch == 'O') { |
439 | rs->esc_state = IS_SS3; | |
440 | rs->esc_param = 0; | |
7e2515e8 | 441 | } else { |
4c36ba32 | 442 | rs->esc_state = IS_NORM; |
7e2515e8 FB |
443 | } |
444 | break; | |
445 | case IS_CSI: | |
2467f95a | 446 | switch (ch) { |
c95d4a29 JI |
447 | 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; | |
7e2515e8 | 455 | case 'D': |
4c36ba32 | 456 | readline_backward_char(rs); |
7e2515e8 FB |
457 | break; |
458 | case 'C': | |
4c36ba32 | 459 | readline_forward_char(rs); |
7e2515e8 FB |
460 | break; |
461 | case '0' ... '9': | |
4c36ba32 | 462 | rs->esc_param = rs->esc_param * 10 + (ch - '0'); |
7e2515e8 FB |
463 | goto the_end; |
464 | case '~': | |
2467f95a | 465 | switch (rs->esc_param) { |
7e2515e8 | 466 | case 1: |
4c36ba32 | 467 | readline_bol(rs); |
7e2515e8 FB |
468 | break; |
469 | case 3: | |
4c36ba32 | 470 | readline_delete_char(rs); |
7e2515e8 FB |
471 | break; |
472 | case 4: | |
4c36ba32 | 473 | readline_eol(rs); |
7e2515e8 FB |
474 | break; |
475 | } | |
476 | break; | |
477 | default: | |
478 | break; | |
479 | } | |
4c36ba32 | 480 | rs->esc_state = IS_NORM; |
7e2515e8 FB |
481 | the_end: |
482 | break; | |
d34dc45d | 483 | case IS_SS3: |
2467f95a | 484 | switch (ch) { |
d34dc45d KW |
485 | 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; | |
7e2515e8 | 494 | } |
4c36ba32 | 495 | readline_update(rs); |
7e2515e8 FB |
496 | } |
497 | ||
4c36ba32 | 498 | void readline_start(ReadLineState *rs, const char *prompt, int read_password, |
7e2515e8 FB |
499 | ReadLineFunc *readline_func, void *opaque) |
500 | { | |
4c36ba32 AL |
501 | pstrcpy(rs->prompt, sizeof(rs->prompt), prompt); |
502 | rs->readline_func = readline_func; | |
503 | rs->readline_opaque = opaque; | |
504 | rs->read_password = read_password; | |
2724b180 AL |
505 | readline_restart(rs); |
506 | } | |
507 | ||
508 | void readline_restart(ReadLineState *rs) | |
509 | { | |
4c36ba32 AL |
510 | rs->cmd_buf_index = 0; |
511 | rs->cmd_buf_size = 0; | |
7e2515e8 FB |
512 | } |
513 | ||
4c36ba32 | 514 | const char *readline_get_history(ReadLineState *rs, unsigned int index) |
7e2515e8 | 515 | { |
e238a99e | 516 | if (index >= READLINE_MAX_CMDS) { |
7e2515e8 | 517 | return NULL; |
e238a99e | 518 | } |
4c36ba32 AL |
519 | return rs->history[index]; |
520 | } | |
521 | ||
e5dc1a6c MAL |
522 | void 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 | } | |
e5dc1a6c MAL |
532 | g_free(rs); |
533 | } | |
534 | ||
c60bf339 SH |
535 | ReadLineState *readline_init(ReadLinePrintfFunc *printf_func, |
536 | ReadLineFlushFunc *flush_func, | |
537 | void *opaque, | |
4c36ba32 AL |
538 | ReadLineCompletionFunc *completion_finder) |
539 | { | |
e5dc1a6c | 540 | ReadLineState *rs = g_new0(ReadLineState, 1); |
4c36ba32 | 541 | |
4c36ba32 | 542 | rs->hist_entry = -1; |
c60bf339 SH |
543 | rs->opaque = opaque; |
544 | rs->printf_func = printf_func; | |
545 | rs->flush_func = flush_func; | |
4c36ba32 AL |
546 | rs->completion_finder = completion_finder; |
547 | ||
548 | return rs; | |
7e2515e8 | 549 | } |