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