root / readline.c @ bfb811ad
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 | 5b0d2727 | aliguori | if (rs->hist_entry == -1) |
200 | 5b0d2727 | aliguori | return;
|
201 | 5b0d2727 | aliguori | if (rs->hist_entry < READLINE_MAX_CMDS - 1 && |
202 | 5b0d2727 | aliguori | rs->history[++rs->hist_entry] != NULL) {
|
203 | 4c36ba32 | aliguori | pstrcpy(rs->cmd_buf, sizeof(rs->cmd_buf),
|
204 | 4c36ba32 | aliguori | rs->history[rs->hist_entry]); |
205 | 7e2515e8 | bellard | } else {
|
206 | 5b0d2727 | aliguori | rs->cmd_buf[0] = 0; |
207 | 4c36ba32 | aliguori | rs->hist_entry = -1;
|
208 | 7e2515e8 | bellard | } |
209 | 4c36ba32 | aliguori | rs->cmd_buf_index = rs->cmd_buf_size = strlen(rs->cmd_buf); |
210 | 7e2515e8 | bellard | } |
211 | 7e2515e8 | bellard | |
212 | 4c36ba32 | aliguori | static void readline_hist_add(ReadLineState *rs, const char *cmdline) |
213 | 7e2515e8 | bellard | { |
214 | 7e2515e8 | bellard | char *hist_entry, *new_entry;
|
215 | 7e2515e8 | bellard | int idx;
|
216 | 7e2515e8 | bellard | |
217 | 7e2515e8 | bellard | if (cmdline[0] == '\0') |
218 | 7e2515e8 | bellard | return;
|
219 | 7e2515e8 | bellard | new_entry = NULL;
|
220 | 4c36ba32 | aliguori | if (rs->hist_entry != -1) { |
221 | 7e2515e8 | bellard | /* We were editing an existing history entry: replace it */
|
222 | 4c36ba32 | aliguori | hist_entry = rs->history[rs->hist_entry]; |
223 | 4c36ba32 | aliguori | idx = rs->hist_entry; |
224 | 7e2515e8 | bellard | if (strcmp(hist_entry, cmdline) == 0) { |
225 | 7e2515e8 | bellard | goto same_entry;
|
226 | 7e2515e8 | bellard | } |
227 | 7e2515e8 | bellard | } |
228 | 7e2515e8 | bellard | /* Search cmdline in history buffers */
|
229 | 4c36ba32 | aliguori | for (idx = 0; idx < READLINE_MAX_CMDS; idx++) { |
230 | 4c36ba32 | aliguori | hist_entry = rs->history[idx]; |
231 | 7e2515e8 | bellard | if (hist_entry == NULL) |
232 | 7e2515e8 | bellard | break;
|
233 | 7e2515e8 | bellard | if (strcmp(hist_entry, cmdline) == 0) { |
234 | 7e2515e8 | bellard | same_entry:
|
235 | 7e2515e8 | bellard | new_entry = hist_entry; |
236 | 7e2515e8 | bellard | /* Put this entry at the end of history */
|
237 | 4c36ba32 | aliguori | memmove(&rs->history[idx], &rs->history[idx + 1],
|
238 | 4c36ba32 | aliguori | (READLINE_MAX_CMDS - idx + 1) * sizeof(char *)); |
239 | 4c36ba32 | aliguori | rs->history[READLINE_MAX_CMDS - 1] = NULL; |
240 | 4c36ba32 | aliguori | for (; idx < READLINE_MAX_CMDS; idx++) {
|
241 | 4c36ba32 | aliguori | if (rs->history[idx] == NULL) |
242 | 7e2515e8 | bellard | break;
|
243 | 7e2515e8 | bellard | } |
244 | 7e2515e8 | bellard | break;
|
245 | 7e2515e8 | bellard | } |
246 | 7e2515e8 | bellard | } |
247 | 4c36ba32 | aliguori | if (idx == READLINE_MAX_CMDS) {
|
248 | 7e2515e8 | bellard | /* Need to get one free slot */
|
249 | 4c36ba32 | aliguori | free(rs->history[0]);
|
250 | 4c36ba32 | aliguori | memcpy(rs->history, &rs->history[1],
|
251 | 4c36ba32 | aliguori | (READLINE_MAX_CMDS - 1) * sizeof(char *)); |
252 | 4c36ba32 | aliguori | rs->history[READLINE_MAX_CMDS - 1] = NULL; |
253 | 4c36ba32 | aliguori | idx = READLINE_MAX_CMDS - 1;
|
254 | 7e2515e8 | bellard | } |
255 | 7e2515e8 | bellard | if (new_entry == NULL) |
256 | 7e2515e8 | bellard | new_entry = strdup(cmdline); |
257 | 4c36ba32 | aliguori | rs->history[idx] = new_entry; |
258 | 4c36ba32 | aliguori | rs->hist_entry = -1;
|
259 | 7e2515e8 | bellard | } |
260 | 7e2515e8 | bellard | |
261 | 7e2515e8 | bellard | /* completion support */
|
262 | 7e2515e8 | bellard | |
263 | 4c36ba32 | aliguori | void readline_add_completion(ReadLineState *rs, const char *str) |
264 | 7e2515e8 | bellard | { |
265 | 4c36ba32 | aliguori | if (rs->nb_completions < READLINE_MAX_COMPLETIONS) {
|
266 | 4c36ba32 | aliguori | rs->completions[rs->nb_completions++] = qemu_strdup(str); |
267 | 7e2515e8 | bellard | } |
268 | 7e2515e8 | bellard | } |
269 | 7e2515e8 | bellard | |
270 | 4c36ba32 | aliguori | void readline_set_completion_index(ReadLineState *rs, int index) |
271 | 376253ec | aliguori | { |
272 | 4c36ba32 | aliguori | rs->completion_index = index; |
273 | 376253ec | aliguori | } |
274 | 376253ec | aliguori | |
275 | 4c36ba32 | aliguori | static void readline_completion(ReadLineState *rs) |
276 | 7e2515e8 | bellard | { |
277 | 376253ec | aliguori | Monitor *mon = cur_mon; |
278 | b427c726 | ths | int len, i, j, max_width, nb_cols, max_prefix;
|
279 | 7e2515e8 | bellard | char *cmdline;
|
280 | 7e2515e8 | bellard | |
281 | 4c36ba32 | aliguori | rs->nb_completions = 0;
|
282 | 3b46e624 | ths | |
283 | 4c36ba32 | aliguori | cmdline = qemu_malloc(rs->cmd_buf_index + 1);
|
284 | 4c36ba32 | aliguori | memcpy(cmdline, rs->cmd_buf, rs->cmd_buf_index); |
285 | 4c36ba32 | aliguori | cmdline[rs->cmd_buf_index] = '\0';
|
286 | 4c36ba32 | aliguori | rs->completion_finder(cmdline); |
287 | 7e2515e8 | bellard | qemu_free(cmdline); |
288 | 7e2515e8 | bellard | |
289 | 7e2515e8 | bellard | /* no completion found */
|
290 | 4c36ba32 | aliguori | if (rs->nb_completions <= 0) |
291 | 7e2515e8 | bellard | return;
|
292 | 4c36ba32 | aliguori | if (rs->nb_completions == 1) { |
293 | 4c36ba32 | aliguori | len = strlen(rs->completions[0]);
|
294 | 4c36ba32 | aliguori | for(i = rs->completion_index; i < len; i++) {
|
295 | 4c36ba32 | aliguori | readline_insert_char(rs, rs->completions[0][i]);
|
296 | 7e2515e8 | bellard | } |
297 | 7e2515e8 | bellard | /* extra space for next argument. XXX: make it more generic */
|
298 | 4c36ba32 | aliguori | if (len > 0 && rs->completions[0][len - 1] != '/') |
299 | 4c36ba32 | aliguori | readline_insert_char(rs, ' ');
|
300 | 7e2515e8 | bellard | } else {
|
301 | 376253ec | aliguori | monitor_printf(mon, "\n");
|
302 | 7e2515e8 | bellard | max_width = 0;
|
303 | b427c726 | ths | max_prefix = 0;
|
304 | 4c36ba32 | aliguori | for(i = 0; i < rs->nb_completions; i++) { |
305 | 4c36ba32 | aliguori | len = strlen(rs->completions[i]); |
306 | b427c726 | ths | if (i==0) { |
307 | b427c726 | ths | max_prefix = len; |
308 | b427c726 | ths | } else {
|
309 | b427c726 | ths | if (len < max_prefix)
|
310 | b427c726 | ths | max_prefix = len; |
311 | b427c726 | ths | for(j=0; j<max_prefix; j++) { |
312 | 4c36ba32 | aliguori | if (rs->completions[i][j] != rs->completions[0][j]) |
313 | b427c726 | ths | max_prefix = j; |
314 | b427c726 | ths | } |
315 | b427c726 | ths | } |
316 | 7e2515e8 | bellard | if (len > max_width)
|
317 | 7e2515e8 | bellard | max_width = len; |
318 | 7e2515e8 | bellard | } |
319 | b427c726 | ths | if (max_prefix > 0) |
320 | 4c36ba32 | aliguori | for(i = rs->completion_index; i < max_prefix; i++) {
|
321 | 4c36ba32 | aliguori | readline_insert_char(rs, rs->completions[0][i]);
|
322 | b427c726 | ths | } |
323 | 7e2515e8 | bellard | max_width += 2;
|
324 | 7e2515e8 | bellard | if (max_width < 10) |
325 | 7e2515e8 | bellard | max_width = 10;
|
326 | 7e2515e8 | bellard | else if (max_width > 80) |
327 | 7e2515e8 | bellard | max_width = 80;
|
328 | 7e2515e8 | bellard | nb_cols = 80 / max_width;
|
329 | 7e2515e8 | bellard | j = 0;
|
330 | 4c36ba32 | aliguori | for(i = 0; i < rs->nb_completions; i++) { |
331 | 4c36ba32 | aliguori | monitor_printf(rs->mon, "%-*s", max_width, rs->completions[i]);
|
332 | 4c36ba32 | aliguori | if (++j == nb_cols || i == (rs->nb_completions - 1)) { |
333 | 4c36ba32 | aliguori | monitor_printf(rs->mon, "\n");
|
334 | 7e2515e8 | bellard | j = 0;
|
335 | 7e2515e8 | bellard | } |
336 | 7e2515e8 | bellard | } |
337 | 4c36ba32 | aliguori | readline_show_prompt(rs); |
338 | 7e2515e8 | bellard | } |
339 | 7e2515e8 | bellard | } |
340 | 7e2515e8 | bellard | |
341 | 7e2515e8 | bellard | /* return true if command handled */
|
342 | 4c36ba32 | aliguori | void readline_handle_byte(ReadLineState *rs, int ch) |
343 | 7e2515e8 | bellard | { |
344 | 4c36ba32 | aliguori | switch(rs->esc_state) {
|
345 | 7e2515e8 | bellard | case IS_NORM:
|
346 | 7e2515e8 | bellard | switch(ch) {
|
347 | 7e2515e8 | bellard | case 1: |
348 | 4c36ba32 | aliguori | readline_bol(rs); |
349 | 7e2515e8 | bellard | break;
|
350 | 7e2515e8 | bellard | case 4: |
351 | 4c36ba32 | aliguori | readline_delete_char(rs); |
352 | 7e2515e8 | bellard | break;
|
353 | 7e2515e8 | bellard | case 5: |
354 | 4c36ba32 | aliguori | readline_eol(rs); |
355 | 7e2515e8 | bellard | break;
|
356 | 7e2515e8 | bellard | case 9: |
357 | 4c36ba32 | aliguori | readline_completion(rs); |
358 | 7e2515e8 | bellard | break;
|
359 | 7e2515e8 | bellard | case 10: |
360 | 7e2515e8 | bellard | case 13: |
361 | 4c36ba32 | aliguori | rs->cmd_buf[rs->cmd_buf_size] = '\0';
|
362 | 4c36ba32 | aliguori | if (!rs->read_password)
|
363 | 4c36ba32 | aliguori | readline_hist_add(rs, rs->cmd_buf); |
364 | 4c36ba32 | aliguori | monitor_printf(rs->mon, "\n");
|
365 | 4c36ba32 | aliguori | rs->cmd_buf_index = 0;
|
366 | 4c36ba32 | aliguori | rs->cmd_buf_size = 0;
|
367 | 4c36ba32 | aliguori | rs->last_cmd_buf_index = 0;
|
368 | 4c36ba32 | aliguori | rs->last_cmd_buf_size = 0;
|
369 | 4c36ba32 | aliguori | rs->readline_func(rs->mon, rs->cmd_buf, rs->readline_opaque); |
370 | 7e2515e8 | bellard | break;
|
371 | 33fa11d4 | ths | case 23: |
372 | 33fa11d4 | ths | /* ^W */
|
373 | 4c36ba32 | aliguori | readline_backword(rs); |
374 | 33fa11d4 | ths | break;
|
375 | 7e2515e8 | bellard | case 27: |
376 | 4c36ba32 | aliguori | rs->esc_state = IS_ESC; |
377 | 7e2515e8 | bellard | break;
|
378 | 7e2515e8 | bellard | case 127: |
379 | 7e2515e8 | bellard | case 8: |
380 | 4c36ba32 | aliguori | readline_backspace(rs); |
381 | 7e2515e8 | bellard | break;
|
382 | 7e2515e8 | bellard | case 155: |
383 | 4c36ba32 | aliguori | rs->esc_state = IS_CSI; |
384 | 7e2515e8 | bellard | break;
|
385 | 7e2515e8 | bellard | default:
|
386 | 7e2515e8 | bellard | if (ch >= 32) { |
387 | 4c36ba32 | aliguori | readline_insert_char(rs, ch); |
388 | 7e2515e8 | bellard | } |
389 | 7e2515e8 | bellard | break;
|
390 | 7e2515e8 | bellard | } |
391 | 7e2515e8 | bellard | break;
|
392 | 7e2515e8 | bellard | case IS_ESC:
|
393 | 7e2515e8 | bellard | if (ch == '[') { |
394 | 4c36ba32 | aliguori | rs->esc_state = IS_CSI; |
395 | 4c36ba32 | aliguori | rs->esc_param = 0;
|
396 | 7e2515e8 | bellard | } else {
|
397 | 4c36ba32 | aliguori | rs->esc_state = IS_NORM; |
398 | 7e2515e8 | bellard | } |
399 | 7e2515e8 | bellard | break;
|
400 | 7e2515e8 | bellard | case IS_CSI:
|
401 | 7e2515e8 | bellard | switch(ch) {
|
402 | 7e2515e8 | bellard | case 'A': |
403 | 7e2515e8 | bellard | case 'F': |
404 | 4c36ba32 | aliguori | readline_up_char(rs); |
405 | 7e2515e8 | bellard | break;
|
406 | 7e2515e8 | bellard | case 'B': |
407 | 7e2515e8 | bellard | case 'E': |
408 | 4c36ba32 | aliguori | readline_down_char(rs); |
409 | 7e2515e8 | bellard | break;
|
410 | 7e2515e8 | bellard | case 'D': |
411 | 4c36ba32 | aliguori | readline_backward_char(rs); |
412 | 7e2515e8 | bellard | break;
|
413 | 7e2515e8 | bellard | case 'C': |
414 | 4c36ba32 | aliguori | readline_forward_char(rs); |
415 | 7e2515e8 | bellard | break;
|
416 | 7e2515e8 | bellard | case '0' ... '9': |
417 | 4c36ba32 | aliguori | rs->esc_param = rs->esc_param * 10 + (ch - '0'); |
418 | 7e2515e8 | bellard | goto the_end;
|
419 | 7e2515e8 | bellard | case '~': |
420 | 4c36ba32 | aliguori | switch(rs->esc_param) {
|
421 | 7e2515e8 | bellard | case 1: |
422 | 4c36ba32 | aliguori | readline_bol(rs); |
423 | 7e2515e8 | bellard | break;
|
424 | 7e2515e8 | bellard | case 3: |
425 | 4c36ba32 | aliguori | readline_delete_char(rs); |
426 | 7e2515e8 | bellard | break;
|
427 | 7e2515e8 | bellard | case 4: |
428 | 4c36ba32 | aliguori | readline_eol(rs); |
429 | 7e2515e8 | bellard | break;
|
430 | 7e2515e8 | bellard | } |
431 | 7e2515e8 | bellard | break;
|
432 | 7e2515e8 | bellard | default:
|
433 | 7e2515e8 | bellard | break;
|
434 | 7e2515e8 | bellard | } |
435 | 4c36ba32 | aliguori | rs->esc_state = IS_NORM; |
436 | 7e2515e8 | bellard | the_end:
|
437 | 7e2515e8 | bellard | break;
|
438 | 7e2515e8 | bellard | } |
439 | 4c36ba32 | aliguori | readline_update(rs); |
440 | 7e2515e8 | bellard | } |
441 | 7e2515e8 | bellard | |
442 | 4c36ba32 | aliguori | void readline_start(ReadLineState *rs, const char *prompt, int read_password, |
443 | 7e2515e8 | bellard | ReadLineFunc *readline_func, void *opaque)
|
444 | 7e2515e8 | bellard | { |
445 | 4c36ba32 | aliguori | pstrcpy(rs->prompt, sizeof(rs->prompt), prompt);
|
446 | 4c36ba32 | aliguori | rs->readline_func = readline_func; |
447 | 4c36ba32 | aliguori | rs->readline_opaque = opaque; |
448 | 4c36ba32 | aliguori | rs->read_password = read_password; |
449 | 2724b180 | aliguori | readline_restart(rs); |
450 | 2724b180 | aliguori | } |
451 | 2724b180 | aliguori | |
452 | 2724b180 | aliguori | void readline_restart(ReadLineState *rs)
|
453 | 2724b180 | aliguori | { |
454 | 4c36ba32 | aliguori | rs->cmd_buf_index = 0;
|
455 | 4c36ba32 | aliguori | rs->cmd_buf_size = 0;
|
456 | 7e2515e8 | bellard | } |
457 | 7e2515e8 | bellard | |
458 | 4c36ba32 | aliguori | const char *readline_get_history(ReadLineState *rs, unsigned int index) |
459 | 7e2515e8 | bellard | { |
460 | 4c36ba32 | aliguori | if (index >= READLINE_MAX_CMDS)
|
461 | 7e2515e8 | bellard | return NULL; |
462 | 4c36ba32 | aliguori | return rs->history[index];
|
463 | 4c36ba32 | aliguori | } |
464 | 4c36ba32 | aliguori | |
465 | 4c36ba32 | aliguori | ReadLineState *readline_init(Monitor *mon, |
466 | 4c36ba32 | aliguori | ReadLineCompletionFunc *completion_finder) |
467 | 4c36ba32 | aliguori | { |
468 | 4c36ba32 | aliguori | ReadLineState *rs = qemu_mallocz(sizeof(*rs));
|
469 | 4c36ba32 | aliguori | |
470 | 4c36ba32 | aliguori | rs->hist_entry = -1;
|
471 | 4c36ba32 | aliguori | rs->mon = mon; |
472 | 4c36ba32 | aliguori | rs->completion_finder = completion_finder; |
473 | 4c36ba32 | aliguori | |
474 | 4c36ba32 | aliguori | return rs;
|
475 | 7e2515e8 | bellard | } |