]>
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 | ||
52f50b1e MA |
289 | void readline_add_completion_of(ReadLineState *rs, |
290 | const char *pfx, const char *str) | |
291 | { | |
292 | if (!strncmp(str, pfx, strlen(pfx))) { | |
293 | readline_add_completion(rs, str); | |
294 | } | |
295 | } | |
296 | ||
4c36ba32 | 297 | void readline_set_completion_index(ReadLineState *rs, int index) |
376253ec | 298 | { |
4c36ba32 | 299 | rs->completion_index = index; |
376253ec AL |
300 | } |
301 | ||
307b2f01 HB |
302 | static int completion_comp(const void *a, const void *b) |
303 | { | |
304 | return strcmp(*(const char **) a, *(const char **) b); | |
305 | } | |
306 | ||
4c36ba32 | 307 | static void readline_completion(ReadLineState *rs) |
7e2515e8 | 308 | { |
b427c726 | 309 | int len, i, j, max_width, nb_cols, max_prefix; |
7e2515e8 FB |
310 | char *cmdline; |
311 | ||
4c36ba32 | 312 | rs->nb_completions = 0; |
3b46e624 | 313 | |
6ad7c326 | 314 | cmdline = g_strndup(rs->cmd_buf, rs->cmd_buf_index); |
c60bf339 | 315 | rs->completion_finder(rs->opaque, cmdline); |
7267c094 | 316 | g_free(cmdline); |
7e2515e8 FB |
317 | |
318 | /* no completion found */ | |
e238a99e | 319 | if (rs->nb_completions <= 0) { |
7e2515e8 | 320 | return; |
e238a99e | 321 | } |
4c36ba32 AL |
322 | if (rs->nb_completions == 1) { |
323 | len = strlen(rs->completions[0]); | |
2467f95a | 324 | for (i = rs->completion_index; i < len; i++) { |
4c36ba32 | 325 | readline_insert_char(rs, rs->completions[0][i]); |
7e2515e8 FB |
326 | } |
327 | /* extra space for next argument. XXX: make it more generic */ | |
e238a99e | 328 | if (len > 0 && rs->completions[0][len - 1] != '/') { |
4c36ba32 | 329 | readline_insert_char(rs, ' '); |
e238a99e | 330 | } |
7e2515e8 | 331 | } else { |
307b2f01 HB |
332 | qsort(rs->completions, rs->nb_completions, sizeof(char *), |
333 | completion_comp); | |
c60bf339 | 334 | rs->printf_func(rs->opaque, "\n"); |
7e2515e8 | 335 | max_width = 0; |
2467f95a JI |
336 | max_prefix = 0; |
337 | for (i = 0; i < rs->nb_completions; i++) { | |
4c36ba32 | 338 | len = strlen(rs->completions[i]); |
2467f95a | 339 | if (i == 0) { |
b427c726 TS |
340 | max_prefix = len; |
341 | } else { | |
e238a99e | 342 | if (len < max_prefix) { |
b427c726 | 343 | max_prefix = len; |
e238a99e | 344 | } |
2467f95a | 345 | for (j = 0; j < max_prefix; j++) { |
e238a99e | 346 | if (rs->completions[i][j] != rs->completions[0][j]) { |
b427c726 | 347 | max_prefix = j; |
e238a99e | 348 | } |
b427c726 TS |
349 | } |
350 | } | |
e238a99e | 351 | if (len > max_width) { |
7e2515e8 | 352 | max_width = len; |
e238a99e | 353 | } |
7e2515e8 | 354 | } |
2467f95a JI |
355 | if (max_prefix > 0) |
356 | for (i = rs->completion_index; i < max_prefix; i++) { | |
4c36ba32 | 357 | readline_insert_char(rs, rs->completions[0][i]); |
b427c726 | 358 | } |
7e2515e8 | 359 | max_width += 2; |
e238a99e | 360 | if (max_width < 10) { |
7e2515e8 | 361 | max_width = 10; |
e238a99e | 362 | } else if (max_width > 80) { |
7e2515e8 | 363 | max_width = 80; |
e238a99e | 364 | } |
7e2515e8 FB |
365 | nb_cols = 80 / max_width; |
366 | j = 0; | |
2467f95a | 367 | for (i = 0; i < rs->nb_completions; i++) { |
c60bf339 | 368 | rs->printf_func(rs->opaque, "%-*s", max_width, rs->completions[i]); |
4c36ba32 | 369 | if (++j == nb_cols || i == (rs->nb_completions - 1)) { |
c60bf339 | 370 | rs->printf_func(rs->opaque, "\n"); |
7e2515e8 FB |
371 | j = 0; |
372 | } | |
373 | } | |
4c36ba32 | 374 | readline_show_prompt(rs); |
7e2515e8 | 375 | } |
fc9fa4bd SW |
376 | for (i = 0; i < rs->nb_completions; i++) { |
377 | g_free(rs->completions[i]); | |
378 | } | |
7e2515e8 FB |
379 | } |
380 | ||
075ccb6c HB |
381 | static void readline_clear_screen(ReadLineState *rs) |
382 | { | |
383 | rs->printf_func(rs->opaque, "\033[2J\033[1;1H"); | |
384 | readline_show_prompt(rs); | |
385 | } | |
386 | ||
7e2515e8 | 387 | /* return true if command handled */ |
4c36ba32 | 388 | void readline_handle_byte(ReadLineState *rs, int ch) |
7e2515e8 | 389 | { |
2467f95a | 390 | switch (rs->esc_state) { |
7e2515e8 | 391 | case IS_NORM: |
2467f95a | 392 | switch (ch) { |
7e2515e8 | 393 | case 1: |
4c36ba32 | 394 | readline_bol(rs); |
7e2515e8 FB |
395 | break; |
396 | case 4: | |
4c36ba32 | 397 | readline_delete_char(rs); |
7e2515e8 FB |
398 | break; |
399 | case 5: | |
4c36ba32 | 400 | readline_eol(rs); |
7e2515e8 FB |
401 | break; |
402 | case 9: | |
4c36ba32 | 403 | readline_completion(rs); |
7e2515e8 | 404 | break; |
075ccb6c HB |
405 | case 12: |
406 | readline_clear_screen(rs); | |
407 | break; | |
7e2515e8 FB |
408 | case 10: |
409 | case 13: | |
4c36ba32 | 410 | rs->cmd_buf[rs->cmd_buf_size] = '\0'; |
e238a99e | 411 | if (!rs->read_password) { |
4c36ba32 | 412 | readline_hist_add(rs, rs->cmd_buf); |
e238a99e | 413 | } |
c60bf339 | 414 | rs->printf_func(rs->opaque, "\n"); |
4c36ba32 AL |
415 | rs->cmd_buf_index = 0; |
416 | rs->cmd_buf_size = 0; | |
417 | rs->last_cmd_buf_index = 0; | |
418 | rs->last_cmd_buf_size = 0; | |
c60bf339 | 419 | rs->readline_func(rs->opaque, rs->cmd_buf, rs->readline_opaque); |
7e2515e8 | 420 | break; |
33fa11d4 TS |
421 | case 23: |
422 | /* ^W */ | |
4c36ba32 | 423 | readline_backword(rs); |
33fa11d4 | 424 | break; |
7e2515e8 | 425 | case 27: |
4c36ba32 | 426 | rs->esc_state = IS_ESC; |
7e2515e8 FB |
427 | break; |
428 | case 127: | |
429 | case 8: | |
4c36ba32 | 430 | readline_backspace(rs); |
7e2515e8 | 431 | break; |
c95d4a29 | 432 | case 155: |
4c36ba32 | 433 | rs->esc_state = IS_CSI; |
c95d4a29 | 434 | break; |
7e2515e8 FB |
435 | default: |
436 | if (ch >= 32) { | |
4c36ba32 | 437 | readline_insert_char(rs, ch); |
7e2515e8 FB |
438 | } |
439 | break; | |
440 | } | |
441 | break; | |
442 | case IS_ESC: | |
443 | if (ch == '[') { | |
4c36ba32 AL |
444 | rs->esc_state = IS_CSI; |
445 | rs->esc_param = 0; | |
d34dc45d KW |
446 | } else if (ch == 'O') { |
447 | rs->esc_state = IS_SS3; | |
448 | rs->esc_param = 0; | |
7e2515e8 | 449 | } else { |
4c36ba32 | 450 | rs->esc_state = IS_NORM; |
7e2515e8 FB |
451 | } |
452 | break; | |
453 | case IS_CSI: | |
2467f95a | 454 | switch (ch) { |
c95d4a29 JI |
455 | case 'A': |
456 | case 'F': | |
457 | readline_up_char(rs); | |
458 | break; | |
459 | case 'B': | |
460 | case 'E': | |
461 | readline_down_char(rs); | |
462 | break; | |
7e2515e8 | 463 | case 'D': |
4c36ba32 | 464 | readline_backward_char(rs); |
7e2515e8 FB |
465 | break; |
466 | case 'C': | |
4c36ba32 | 467 | readline_forward_char(rs); |
7e2515e8 FB |
468 | break; |
469 | case '0' ... '9': | |
4c36ba32 | 470 | rs->esc_param = rs->esc_param * 10 + (ch - '0'); |
7e2515e8 FB |
471 | goto the_end; |
472 | case '~': | |
2467f95a | 473 | switch (rs->esc_param) { |
7e2515e8 | 474 | case 1: |
4c36ba32 | 475 | readline_bol(rs); |
7e2515e8 FB |
476 | break; |
477 | case 3: | |
4c36ba32 | 478 | readline_delete_char(rs); |
7e2515e8 FB |
479 | break; |
480 | case 4: | |
4c36ba32 | 481 | readline_eol(rs); |
7e2515e8 FB |
482 | break; |
483 | } | |
484 | break; | |
485 | default: | |
486 | break; | |
487 | } | |
4c36ba32 | 488 | rs->esc_state = IS_NORM; |
7e2515e8 FB |
489 | the_end: |
490 | break; | |
d34dc45d | 491 | case IS_SS3: |
2467f95a | 492 | switch (ch) { |
d34dc45d KW |
493 | case 'F': |
494 | readline_eol(rs); | |
495 | break; | |
496 | case 'H': | |
497 | readline_bol(rs); | |
498 | break; | |
499 | } | |
500 | rs->esc_state = IS_NORM; | |
501 | break; | |
7e2515e8 | 502 | } |
4c36ba32 | 503 | readline_update(rs); |
7e2515e8 FB |
504 | } |
505 | ||
4c36ba32 | 506 | void readline_start(ReadLineState *rs, const char *prompt, int read_password, |
7e2515e8 FB |
507 | ReadLineFunc *readline_func, void *opaque) |
508 | { | |
4c36ba32 AL |
509 | pstrcpy(rs->prompt, sizeof(rs->prompt), prompt); |
510 | rs->readline_func = readline_func; | |
511 | rs->readline_opaque = opaque; | |
512 | rs->read_password = read_password; | |
2724b180 AL |
513 | readline_restart(rs); |
514 | } | |
515 | ||
516 | void readline_restart(ReadLineState *rs) | |
517 | { | |
4c36ba32 AL |
518 | rs->cmd_buf_index = 0; |
519 | rs->cmd_buf_size = 0; | |
7e2515e8 FB |
520 | } |
521 | ||
4c36ba32 | 522 | const char *readline_get_history(ReadLineState *rs, unsigned int index) |
7e2515e8 | 523 | { |
e238a99e | 524 | if (index >= READLINE_MAX_CMDS) { |
7e2515e8 | 525 | return NULL; |
e238a99e | 526 | } |
4c36ba32 AL |
527 | return rs->history[index]; |
528 | } | |
529 | ||
e5dc1a6c MAL |
530 | void readline_free(ReadLineState *rs) |
531 | { | |
532 | int i; | |
533 | ||
534 | if (!rs) { | |
535 | return; | |
536 | } | |
537 | for (i = 0; i < READLINE_MAX_CMDS; i++) { | |
538 | g_free(rs->history[i]); | |
539 | } | |
e5dc1a6c MAL |
540 | g_free(rs); |
541 | } | |
542 | ||
c60bf339 SH |
543 | ReadLineState *readline_init(ReadLinePrintfFunc *printf_func, |
544 | ReadLineFlushFunc *flush_func, | |
545 | void *opaque, | |
4c36ba32 AL |
546 | ReadLineCompletionFunc *completion_finder) |
547 | { | |
e5dc1a6c | 548 | ReadLineState *rs = g_new0(ReadLineState, 1); |
4c36ba32 | 549 | |
4c36ba32 | 550 | rs->hist_entry = -1; |
c60bf339 SH |
551 | rs->opaque = opaque; |
552 | rs->printf_func = printf_func; | |
553 | rs->flush_func = flush_func; | |
4c36ba32 AL |
554 | rs->completion_finder = completion_finder; |
555 | ||
556 | return rs; | |
7e2515e8 | 557 | } |