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