root / readline.c @ 71be0fc3
History | View | Annotate | Download (10.9 kB)
1 | 7e2515e8 | bellard | /*
|
---|---|---|---|
2 | 7e2515e8 | bellard | * QEMU readline utility
|
3 | 7e2515e8 | bellard | *
|
4 | 7e2515e8 | bellard | * Copyright (c) 2003-2004 Fabrice Bellard
|
5 | 7e2515e8 | bellard | *
|
6 | 7e2515e8 | bellard | * Permission is hereby granted, free of charge, to any person obtaining a copy
|
7 | 7e2515e8 | bellard | * of this software and associated documentation files (the "Software"), to deal
|
8 | 7e2515e8 | bellard | * in the Software without restriction, including without limitation the rights
|
9 | 7e2515e8 | bellard | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10 | 7e2515e8 | bellard | * copies of the Software, and to permit persons to whom the Software is
|
11 | 7e2515e8 | bellard | * furnished to do so, subject to the following conditions:
|
12 | 7e2515e8 | bellard | *
|
13 | 7e2515e8 | bellard | * The above copyright notice and this permission notice shall be included in
|
14 | 7e2515e8 | bellard | * all copies or substantial portions of the Software.
|
15 | 7e2515e8 | bellard | *
|
16 | 7e2515e8 | bellard | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17 | 7e2515e8 | bellard | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18 | 7e2515e8 | bellard | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
|
19 | 7e2515e8 | bellard | * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20 | 7e2515e8 | bellard | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21 | 7e2515e8 | bellard | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
22 | 7e2515e8 | bellard | * THE SOFTWARE.
|
23 | 7e2515e8 | bellard | */
|
24 | 7e2515e8 | bellard | #include "vl.h" |
25 | 7e2515e8 | bellard | |
26 | 7e2515e8 | bellard | #define TERM_CMD_BUF_SIZE 4095 |
27 | 7e2515e8 | bellard | #define TERM_MAX_CMDS 64 |
28 | 7e2515e8 | bellard | #define NB_COMPLETIONS_MAX 256 |
29 | 7e2515e8 | bellard | |
30 | 7e2515e8 | bellard | #define IS_NORM 0 |
31 | 7e2515e8 | bellard | #define IS_ESC 1 |
32 | 7e2515e8 | bellard | #define IS_CSI 2 |
33 | 7e2515e8 | bellard | |
34 | 7e2515e8 | bellard | #define printf do_not_use_printf
|
35 | 7e2515e8 | bellard | |
36 | 7e2515e8 | bellard | static char term_cmd_buf[TERM_CMD_BUF_SIZE + 1]; |
37 | 7e2515e8 | bellard | static int term_cmd_buf_index; |
38 | 7e2515e8 | bellard | static int term_cmd_buf_size; |
39 | 7e2515e8 | bellard | |
40 | 7e2515e8 | bellard | static char term_last_cmd_buf[TERM_CMD_BUF_SIZE + 1]; |
41 | 7e2515e8 | bellard | static int term_last_cmd_buf_index; |
42 | 7e2515e8 | bellard | static int term_last_cmd_buf_size; |
43 | 7e2515e8 | bellard | |
44 | 7e2515e8 | bellard | static int term_esc_state; |
45 | 7e2515e8 | bellard | static int term_esc_param; |
46 | 7e2515e8 | bellard | |
47 | 7e2515e8 | bellard | static char *term_history[TERM_MAX_CMDS]; |
48 | 7e2515e8 | bellard | static int term_hist_entry = -1; |
49 | 7e2515e8 | bellard | |
50 | 7e2515e8 | bellard | static int nb_completions; |
51 | 7e2515e8 | bellard | int completion_index;
|
52 | 7e2515e8 | bellard | static char *completions[NB_COMPLETIONS_MAX]; |
53 | 7e2515e8 | bellard | |
54 | 7e2515e8 | bellard | static ReadLineFunc *term_readline_func;
|
55 | 7e2515e8 | bellard | static int term_is_password; |
56 | 7e2515e8 | bellard | static char term_prompt[256]; |
57 | 7e2515e8 | bellard | static void *term_readline_opaque; |
58 | 7e2515e8 | bellard | |
59 | 7e2515e8 | bellard | static void term_show_prompt2(void) |
60 | 7e2515e8 | bellard | { |
61 | 7e2515e8 | bellard | term_printf("%s", term_prompt);
|
62 | 7e2515e8 | bellard | term_flush(); |
63 | 7e2515e8 | bellard | term_last_cmd_buf_index = 0;
|
64 | 7e2515e8 | bellard | term_last_cmd_buf_size = 0;
|
65 | 7e2515e8 | bellard | term_esc_state = IS_NORM; |
66 | 7e2515e8 | bellard | } |
67 | 7e2515e8 | bellard | |
68 | 7e2515e8 | bellard | static void term_show_prompt(void) |
69 | 7e2515e8 | bellard | { |
70 | 7e2515e8 | bellard | term_show_prompt2(); |
71 | 7e2515e8 | bellard | term_cmd_buf_index = 0;
|
72 | 7e2515e8 | bellard | term_cmd_buf_size = 0;
|
73 | 7e2515e8 | bellard | } |
74 | 7e2515e8 | bellard | |
75 | 7e2515e8 | bellard | /* update the displayed command line */
|
76 | 7e2515e8 | bellard | static void term_update(void) |
77 | 7e2515e8 | bellard | { |
78 | 7e2515e8 | bellard | int i, delta, len;
|
79 | 7e2515e8 | bellard | |
80 | 7e2515e8 | bellard | if (term_cmd_buf_size != term_last_cmd_buf_size ||
|
81 | 7e2515e8 | bellard | memcmp(term_cmd_buf, term_last_cmd_buf, term_cmd_buf_size) != 0) {
|
82 | 7e2515e8 | bellard | for(i = 0; i < term_last_cmd_buf_index; i++) { |
83 | 7e2515e8 | bellard | term_printf("\033[D");
|
84 | 7e2515e8 | bellard | } |
85 | 7e2515e8 | bellard | term_cmd_buf[term_cmd_buf_size] = '\0';
|
86 | 7e2515e8 | bellard | if (term_is_password) {
|
87 | 7e2515e8 | bellard | len = strlen(term_cmd_buf); |
88 | 7e2515e8 | bellard | for(i = 0; i < len; i++) |
89 | 7e2515e8 | bellard | term_printf("*");
|
90 | 7e2515e8 | bellard | } else {
|
91 | 7e2515e8 | bellard | term_printf("%s", term_cmd_buf);
|
92 | 7e2515e8 | bellard | } |
93 | 7e2515e8 | bellard | term_printf("\033[K");
|
94 | 7e2515e8 | bellard | memcpy(term_last_cmd_buf, term_cmd_buf, term_cmd_buf_size); |
95 | 7e2515e8 | bellard | term_last_cmd_buf_size = term_cmd_buf_size; |
96 | 7e2515e8 | bellard | term_last_cmd_buf_index = term_cmd_buf_size; |
97 | 7e2515e8 | bellard | } |
98 | 7e2515e8 | bellard | if (term_cmd_buf_index != term_last_cmd_buf_index) {
|
99 | 7e2515e8 | bellard | delta = term_cmd_buf_index - term_last_cmd_buf_index; |
100 | 7e2515e8 | bellard | if (delta > 0) { |
101 | 7e2515e8 | bellard | for(i = 0;i < delta; i++) { |
102 | 7e2515e8 | bellard | term_printf("\033[C");
|
103 | 7e2515e8 | bellard | } |
104 | 7e2515e8 | bellard | } else {
|
105 | 7e2515e8 | bellard | delta = -delta; |
106 | 7e2515e8 | bellard | for(i = 0;i < delta; i++) { |
107 | 7e2515e8 | bellard | term_printf("\033[D");
|
108 | 7e2515e8 | bellard | } |
109 | 7e2515e8 | bellard | } |
110 | 7e2515e8 | bellard | term_last_cmd_buf_index = term_cmd_buf_index; |
111 | 7e2515e8 | bellard | } |
112 | 7e2515e8 | bellard | term_flush(); |
113 | 7e2515e8 | bellard | } |
114 | 7e2515e8 | bellard | |
115 | 7e2515e8 | bellard | static void term_insert_char(int ch) |
116 | 7e2515e8 | bellard | { |
117 | 7e2515e8 | bellard | if (term_cmd_buf_index < TERM_CMD_BUF_SIZE) {
|
118 | 7e2515e8 | bellard | memmove(term_cmd_buf + term_cmd_buf_index + 1,
|
119 | 7e2515e8 | bellard | term_cmd_buf + term_cmd_buf_index, |
120 | 7e2515e8 | bellard | term_cmd_buf_size - term_cmd_buf_index); |
121 | 7e2515e8 | bellard | term_cmd_buf[term_cmd_buf_index] = ch; |
122 | 7e2515e8 | bellard | term_cmd_buf_size++; |
123 | 7e2515e8 | bellard | term_cmd_buf_index++; |
124 | 7e2515e8 | bellard | } |
125 | 7e2515e8 | bellard | } |
126 | 7e2515e8 | bellard | |
127 | 7e2515e8 | bellard | static void term_backward_char(void) |
128 | 7e2515e8 | bellard | { |
129 | 7e2515e8 | bellard | if (term_cmd_buf_index > 0) { |
130 | 7e2515e8 | bellard | term_cmd_buf_index--; |
131 | 7e2515e8 | bellard | } |
132 | 7e2515e8 | bellard | } |
133 | 7e2515e8 | bellard | |
134 | 7e2515e8 | bellard | static void term_forward_char(void) |
135 | 7e2515e8 | bellard | { |
136 | 7e2515e8 | bellard | if (term_cmd_buf_index < term_cmd_buf_size) {
|
137 | 7e2515e8 | bellard | term_cmd_buf_index++; |
138 | 7e2515e8 | bellard | } |
139 | 7e2515e8 | bellard | } |
140 | 7e2515e8 | bellard | |
141 | 7e2515e8 | bellard | static void term_delete_char(void) |
142 | 7e2515e8 | bellard | { |
143 | 7e2515e8 | bellard | if (term_cmd_buf_index < term_cmd_buf_size) {
|
144 | 7e2515e8 | bellard | memmove(term_cmd_buf + term_cmd_buf_index, |
145 | 7e2515e8 | bellard | term_cmd_buf + term_cmd_buf_index + 1,
|
146 | 7e2515e8 | bellard | term_cmd_buf_size - term_cmd_buf_index - 1);
|
147 | 7e2515e8 | bellard | term_cmd_buf_size--; |
148 | 7e2515e8 | bellard | } |
149 | 7e2515e8 | bellard | } |
150 | 7e2515e8 | bellard | |
151 | 7e2515e8 | bellard | static void term_backspace(void) |
152 | 7e2515e8 | bellard | { |
153 | 7e2515e8 | bellard | if (term_cmd_buf_index > 0) { |
154 | 7e2515e8 | bellard | term_backward_char(); |
155 | 7e2515e8 | bellard | term_delete_char(); |
156 | 7e2515e8 | bellard | } |
157 | 7e2515e8 | bellard | } |
158 | 7e2515e8 | bellard | |
159 | 7e2515e8 | bellard | static void term_bol(void) |
160 | 7e2515e8 | bellard | { |
161 | 7e2515e8 | bellard | term_cmd_buf_index = 0;
|
162 | 7e2515e8 | bellard | } |
163 | 7e2515e8 | bellard | |
164 | 7e2515e8 | bellard | static void term_eol(void) |
165 | 7e2515e8 | bellard | { |
166 | 7e2515e8 | bellard | term_cmd_buf_index = term_cmd_buf_size; |
167 | 7e2515e8 | bellard | } |
168 | 7e2515e8 | bellard | |
169 | 7e2515e8 | bellard | static void term_up_char(void) |
170 | 7e2515e8 | bellard | { |
171 | 7e2515e8 | bellard | int idx;
|
172 | 7e2515e8 | bellard | |
173 | 7e2515e8 | bellard | if (term_hist_entry == 0) |
174 | 7e2515e8 | bellard | return;
|
175 | 7e2515e8 | bellard | if (term_hist_entry == -1) { |
176 | 7e2515e8 | bellard | /* Find latest entry */
|
177 | 7e2515e8 | bellard | for (idx = 0; idx < TERM_MAX_CMDS; idx++) { |
178 | 7e2515e8 | bellard | if (term_history[idx] == NULL) |
179 | 7e2515e8 | bellard | break;
|
180 | 7e2515e8 | bellard | } |
181 | 7e2515e8 | bellard | term_hist_entry = idx; |
182 | 7e2515e8 | bellard | } |
183 | 7e2515e8 | bellard | term_hist_entry--; |
184 | 7e2515e8 | bellard | if (term_hist_entry >= 0) { |
185 | 7e2515e8 | bellard | pstrcpy(term_cmd_buf, sizeof(term_cmd_buf),
|
186 | 7e2515e8 | bellard | term_history[term_hist_entry]); |
187 | 7e2515e8 | bellard | term_cmd_buf_index = term_cmd_buf_size = strlen(term_cmd_buf); |
188 | 7e2515e8 | bellard | } |
189 | 7e2515e8 | bellard | } |
190 | 7e2515e8 | bellard | |
191 | 7e2515e8 | bellard | static void term_down_char(void) |
192 | 7e2515e8 | bellard | { |
193 | 7e2515e8 | bellard | if (term_hist_entry == TERM_MAX_CMDS - 1 || term_hist_entry == -1) |
194 | 7e2515e8 | bellard | return;
|
195 | 7e2515e8 | bellard | if (term_history[++term_hist_entry] != NULL) { |
196 | 7e2515e8 | bellard | pstrcpy(term_cmd_buf, sizeof(term_cmd_buf),
|
197 | 7e2515e8 | bellard | term_history[term_hist_entry]); |
198 | 7e2515e8 | bellard | } else {
|
199 | 7e2515e8 | bellard | term_hist_entry = -1;
|
200 | 7e2515e8 | bellard | } |
201 | 7e2515e8 | bellard | term_cmd_buf_index = term_cmd_buf_size = strlen(term_cmd_buf); |
202 | 7e2515e8 | bellard | } |
203 | 7e2515e8 | bellard | |
204 | 7e2515e8 | bellard | static void term_hist_add(const char *cmdline) |
205 | 7e2515e8 | bellard | { |
206 | 7e2515e8 | bellard | char *hist_entry, *new_entry;
|
207 | 7e2515e8 | bellard | int idx;
|
208 | 7e2515e8 | bellard | |
209 | 7e2515e8 | bellard | if (cmdline[0] == '\0') |
210 | 7e2515e8 | bellard | return;
|
211 | 7e2515e8 | bellard | new_entry = NULL;
|
212 | 7e2515e8 | bellard | if (term_hist_entry != -1) { |
213 | 7e2515e8 | bellard | /* We were editing an existing history entry: replace it */
|
214 | 7e2515e8 | bellard | hist_entry = term_history[term_hist_entry]; |
215 | 7e2515e8 | bellard | idx = term_hist_entry; |
216 | 7e2515e8 | bellard | if (strcmp(hist_entry, cmdline) == 0) { |
217 | 7e2515e8 | bellard | goto same_entry;
|
218 | 7e2515e8 | bellard | } |
219 | 7e2515e8 | bellard | } |
220 | 7e2515e8 | bellard | /* Search cmdline in history buffers */
|
221 | 7e2515e8 | bellard | for (idx = 0; idx < TERM_MAX_CMDS; idx++) { |
222 | 7e2515e8 | bellard | hist_entry = term_history[idx]; |
223 | 7e2515e8 | bellard | if (hist_entry == NULL) |
224 | 7e2515e8 | bellard | break;
|
225 | 7e2515e8 | bellard | if (strcmp(hist_entry, cmdline) == 0) { |
226 | 7e2515e8 | bellard | same_entry:
|
227 | 7e2515e8 | bellard | new_entry = hist_entry; |
228 | 7e2515e8 | bellard | /* Put this entry at the end of history */
|
229 | 7e2515e8 | bellard | memmove(&term_history[idx], &term_history[idx + 1],
|
230 | 7e2515e8 | bellard | &term_history[TERM_MAX_CMDS] - &term_history[idx + 1]);
|
231 | 7e2515e8 | bellard | term_history[TERM_MAX_CMDS - 1] = NULL; |
232 | 7e2515e8 | bellard | for (; idx < TERM_MAX_CMDS; idx++) {
|
233 | 7e2515e8 | bellard | if (term_history[idx] == NULL) |
234 | 7e2515e8 | bellard | break;
|
235 | 7e2515e8 | bellard | } |
236 | 7e2515e8 | bellard | break;
|
237 | 7e2515e8 | bellard | } |
238 | 7e2515e8 | bellard | } |
239 | 7e2515e8 | bellard | if (idx == TERM_MAX_CMDS) {
|
240 | 7e2515e8 | bellard | /* Need to get one free slot */
|
241 | 7e2515e8 | bellard | free(term_history[0]);
|
242 | 7e2515e8 | bellard | memcpy(term_history, &term_history[1],
|
243 | 7e2515e8 | bellard | &term_history[TERM_MAX_CMDS] - &term_history[1]);
|
244 | 7e2515e8 | bellard | term_history[TERM_MAX_CMDS - 1] = NULL; |
245 | 7e2515e8 | bellard | idx = TERM_MAX_CMDS - 1;
|
246 | 7e2515e8 | bellard | } |
247 | 7e2515e8 | bellard | if (new_entry == NULL) |
248 | 7e2515e8 | bellard | new_entry = strdup(cmdline); |
249 | 7e2515e8 | bellard | term_history[idx] = new_entry; |
250 | 7e2515e8 | bellard | term_hist_entry = -1;
|
251 | 7e2515e8 | bellard | } |
252 | 7e2515e8 | bellard | |
253 | 7e2515e8 | bellard | /* completion support */
|
254 | 7e2515e8 | bellard | |
255 | 7e2515e8 | bellard | void add_completion(const char *str) |
256 | 7e2515e8 | bellard | { |
257 | 7e2515e8 | bellard | if (nb_completions < NB_COMPLETIONS_MAX) {
|
258 | 7e2515e8 | bellard | completions[nb_completions++] = qemu_strdup(str); |
259 | 7e2515e8 | bellard | } |
260 | 7e2515e8 | bellard | } |
261 | 7e2515e8 | bellard | |
262 | 7e2515e8 | bellard | static void term_completion(void) |
263 | 7e2515e8 | bellard | { |
264 | 7e2515e8 | bellard | int len, i, j, max_width, nb_cols;
|
265 | 7e2515e8 | bellard | char *cmdline;
|
266 | 7e2515e8 | bellard | |
267 | 7e2515e8 | bellard | nb_completions = 0;
|
268 | 7e2515e8 | bellard | |
269 | 7e2515e8 | bellard | cmdline = qemu_malloc(term_cmd_buf_index + 1);
|
270 | 7e2515e8 | bellard | if (!cmdline)
|
271 | 7e2515e8 | bellard | return;
|
272 | 7e2515e8 | bellard | memcpy(cmdline, term_cmd_buf, term_cmd_buf_index); |
273 | 7e2515e8 | bellard | cmdline[term_cmd_buf_index] = '\0';
|
274 | 7e2515e8 | bellard | readline_find_completion(cmdline); |
275 | 7e2515e8 | bellard | qemu_free(cmdline); |
276 | 7e2515e8 | bellard | |
277 | 7e2515e8 | bellard | /* no completion found */
|
278 | 7e2515e8 | bellard | if (nb_completions <= 0) |
279 | 7e2515e8 | bellard | return;
|
280 | 7e2515e8 | bellard | if (nb_completions == 1) { |
281 | 7e2515e8 | bellard | len = strlen(completions[0]);
|
282 | 7e2515e8 | bellard | for(i = completion_index; i < len; i++) {
|
283 | 7e2515e8 | bellard | term_insert_char(completions[0][i]);
|
284 | 7e2515e8 | bellard | } |
285 | 7e2515e8 | bellard | /* extra space for next argument. XXX: make it more generic */
|
286 | 7e2515e8 | bellard | if (len > 0 && completions[0][len - 1] != '/') |
287 | 7e2515e8 | bellard | term_insert_char(' ');
|
288 | 7e2515e8 | bellard | } else {
|
289 | 7e2515e8 | bellard | term_printf("\n");
|
290 | 7e2515e8 | bellard | max_width = 0;
|
291 | 7e2515e8 | bellard | for(i = 0; i < nb_completions; i++) { |
292 | 7e2515e8 | bellard | len = strlen(completions[i]); |
293 | 7e2515e8 | bellard | if (len > max_width)
|
294 | 7e2515e8 | bellard | max_width = len; |
295 | 7e2515e8 | bellard | } |
296 | 7e2515e8 | bellard | max_width += 2;
|
297 | 7e2515e8 | bellard | if (max_width < 10) |
298 | 7e2515e8 | bellard | max_width = 10;
|
299 | 7e2515e8 | bellard | else if (max_width > 80) |
300 | 7e2515e8 | bellard | max_width = 80;
|
301 | 7e2515e8 | bellard | nb_cols = 80 / max_width;
|
302 | 7e2515e8 | bellard | j = 0;
|
303 | 7e2515e8 | bellard | for(i = 0; i < nb_completions; i++) { |
304 | 7e2515e8 | bellard | term_printf("%-*s", max_width, completions[i]);
|
305 | 7e2515e8 | bellard | if (++j == nb_cols || i == (nb_completions - 1)) { |
306 | 7e2515e8 | bellard | term_printf("\n");
|
307 | 7e2515e8 | bellard | j = 0;
|
308 | 7e2515e8 | bellard | } |
309 | 7e2515e8 | bellard | } |
310 | 7e2515e8 | bellard | term_show_prompt2(); |
311 | 7e2515e8 | bellard | } |
312 | 7e2515e8 | bellard | } |
313 | 7e2515e8 | bellard | |
314 | 7e2515e8 | bellard | /* return true if command handled */
|
315 | 7e2515e8 | bellard | void readline_handle_byte(int ch) |
316 | 7e2515e8 | bellard | { |
317 | 7e2515e8 | bellard | switch(term_esc_state) {
|
318 | 7e2515e8 | bellard | case IS_NORM:
|
319 | 7e2515e8 | bellard | switch(ch) {
|
320 | 7e2515e8 | bellard | case 1: |
321 | 7e2515e8 | bellard | term_bol(); |
322 | 7e2515e8 | bellard | break;
|
323 | 7e2515e8 | bellard | case 4: |
324 | 7e2515e8 | bellard | term_delete_char(); |
325 | 7e2515e8 | bellard | break;
|
326 | 7e2515e8 | bellard | case 5: |
327 | 7e2515e8 | bellard | term_eol(); |
328 | 7e2515e8 | bellard | break;
|
329 | 7e2515e8 | bellard | case 9: |
330 | 7e2515e8 | bellard | term_completion(); |
331 | 7e2515e8 | bellard | break;
|
332 | 7e2515e8 | bellard | case 10: |
333 | 7e2515e8 | bellard | case 13: |
334 | 7e2515e8 | bellard | term_cmd_buf[term_cmd_buf_size] = '\0';
|
335 | 7e2515e8 | bellard | if (!term_is_password)
|
336 | 7e2515e8 | bellard | term_hist_add(term_cmd_buf); |
337 | 7e2515e8 | bellard | term_printf("\n");
|
338 | 7e2515e8 | bellard | /* NOTE: readline_start can be called here */
|
339 | 7e2515e8 | bellard | term_readline_func(term_readline_opaque, term_cmd_buf); |
340 | 7e2515e8 | bellard | break;
|
341 | 7e2515e8 | bellard | case 27: |
342 | 7e2515e8 | bellard | term_esc_state = IS_ESC; |
343 | 7e2515e8 | bellard | break;
|
344 | 7e2515e8 | bellard | case 127: |
345 | 7e2515e8 | bellard | case 8: |
346 | 7e2515e8 | bellard | term_backspace(); |
347 | 7e2515e8 | bellard | break;
|
348 | 7e2515e8 | bellard | case 155: |
349 | 7e2515e8 | bellard | term_esc_state = IS_CSI; |
350 | 7e2515e8 | bellard | break;
|
351 | 7e2515e8 | bellard | default:
|
352 | 7e2515e8 | bellard | if (ch >= 32) { |
353 | 7e2515e8 | bellard | term_insert_char(ch); |
354 | 7e2515e8 | bellard | } |
355 | 7e2515e8 | bellard | break;
|
356 | 7e2515e8 | bellard | } |
357 | 7e2515e8 | bellard | break;
|
358 | 7e2515e8 | bellard | case IS_ESC:
|
359 | 7e2515e8 | bellard | if (ch == '[') { |
360 | 7e2515e8 | bellard | term_esc_state = IS_CSI; |
361 | 7e2515e8 | bellard | term_esc_param = 0;
|
362 | 7e2515e8 | bellard | } else {
|
363 | 7e2515e8 | bellard | term_esc_state = IS_NORM; |
364 | 7e2515e8 | bellard | } |
365 | 7e2515e8 | bellard | break;
|
366 | 7e2515e8 | bellard | case IS_CSI:
|
367 | 7e2515e8 | bellard | switch(ch) {
|
368 | 7e2515e8 | bellard | case 'A': |
369 | 7e2515e8 | bellard | case 'F': |
370 | 7e2515e8 | bellard | term_up_char(); |
371 | 7e2515e8 | bellard | break;
|
372 | 7e2515e8 | bellard | case 'B': |
373 | 7e2515e8 | bellard | case 'E': |
374 | 7e2515e8 | bellard | term_down_char(); |
375 | 7e2515e8 | bellard | break;
|
376 | 7e2515e8 | bellard | case 'D': |
377 | 7e2515e8 | bellard | term_backward_char(); |
378 | 7e2515e8 | bellard | break;
|
379 | 7e2515e8 | bellard | case 'C': |
380 | 7e2515e8 | bellard | term_forward_char(); |
381 | 7e2515e8 | bellard | break;
|
382 | 7e2515e8 | bellard | case '0' ... '9': |
383 | 7e2515e8 | bellard | term_esc_param = term_esc_param * 10 + (ch - '0'); |
384 | 7e2515e8 | bellard | goto the_end;
|
385 | 7e2515e8 | bellard | case '~': |
386 | 7e2515e8 | bellard | switch(term_esc_param) {
|
387 | 7e2515e8 | bellard | case 1: |
388 | 7e2515e8 | bellard | term_bol(); |
389 | 7e2515e8 | bellard | break;
|
390 | 7e2515e8 | bellard | case 3: |
391 | 7e2515e8 | bellard | term_delete_char(); |
392 | 7e2515e8 | bellard | break;
|
393 | 7e2515e8 | bellard | case 4: |
394 | 7e2515e8 | bellard | term_eol(); |
395 | 7e2515e8 | bellard | break;
|
396 | 7e2515e8 | bellard | } |
397 | 7e2515e8 | bellard | break;
|
398 | 7e2515e8 | bellard | default:
|
399 | 7e2515e8 | bellard | break;
|
400 | 7e2515e8 | bellard | } |
401 | 7e2515e8 | bellard | term_esc_state = IS_NORM; |
402 | 7e2515e8 | bellard | the_end:
|
403 | 7e2515e8 | bellard | break;
|
404 | 7e2515e8 | bellard | } |
405 | 7e2515e8 | bellard | term_update(); |
406 | 7e2515e8 | bellard | } |
407 | 7e2515e8 | bellard | |
408 | 7e2515e8 | bellard | void readline_start(const char *prompt, int is_password, |
409 | 7e2515e8 | bellard | ReadLineFunc *readline_func, void *opaque)
|
410 | 7e2515e8 | bellard | { |
411 | 7e2515e8 | bellard | pstrcpy(term_prompt, sizeof(term_prompt), prompt);
|
412 | 7e2515e8 | bellard | term_readline_func = readline_func; |
413 | 7e2515e8 | bellard | term_readline_opaque = opaque; |
414 | 7e2515e8 | bellard | term_is_password = is_password; |
415 | 7e2515e8 | bellard | term_show_prompt(); |
416 | 7e2515e8 | bellard | } |
417 | 7e2515e8 | bellard | |
418 | 7e2515e8 | bellard | const char *readline_get_history(unsigned int index) |
419 | 7e2515e8 | bellard | { |
420 | 7e2515e8 | bellard | if (index >= TERM_MAX_CMDS)
|
421 | 7e2515e8 | bellard | return NULL; |
422 | 7e2515e8 | bellard | return term_history[index];
|
423 | 7e2515e8 | bellard | } |
424 | 7e2515e8 | bellard |