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