2 * Copyright (c) <2010>, Intel Corporation
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * - Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * - Neither the name of Intel Corporation nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
30 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
32 * OF THE POSSIBILITY OF SUCH DAMAGE.
36 * Copyright (c) 2009, Olivier MATZ <zer0@droids-corp.org>
37 * All rights reserved.
38 * Redistribution and use in source and binary forms, with or without
39 * modification, are permitted provided that the following conditions are met:
41 * * Redistributions of source code must retain the above copyright
42 * notice, this list of conditions and the following disclaimer.
43 * * Redistributions in binary form must reproduce the above copyright
44 * notice, this list of conditions and the following disclaimer in the
45 * documentation and/or other materials provided with the distribution.
46 * * Neither the name of the University of California, Berkeley nor the
47 * names of its contributors may be used to endorse or promote products
48 * derived from this software without specific prior written permission.
50 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND ANY
51 * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
52 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
53 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS BE LIABLE FOR ANY
54 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
55 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
56 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
57 * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
58 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
59 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
69 #include "cmdline_cirbuf.h"
70 #include "cmdline_rdline.h"
72 static void rdline_puts(struct rdline *rdl, const char *buf);
73 static void rdline_miniprintf(struct rdline *rdl,
74 const char *buf, unsigned int val);
76 #ifndef NO_RDLINE_HISTORY
77 static void rdline_remove_old_history_item(struct rdline *rdl);
78 static void rdline_remove_first_history_item(struct rdline *rdl);
79 static unsigned int rdline_get_history_size(struct rdline *rdl);
80 #endif /* !NO_RDLINE_HISTORY */
83 /* isblank() needs _XOPEN_SOURCE >= 600 || _ISOC99_SOURCE, so use our
95 rdline_init(struct rdline *rdl,
96 rdline_write_char_t *write_char,
97 rdline_validate_t *validate,
98 rdline_complete_t *complete)
100 memset(rdl, 0, sizeof(*rdl));
101 rdl->validate = validate;
102 rdl->complete = complete;
103 rdl->write_char = write_char;
104 rdl->status = RDLINE_INIT;
105 #ifndef NO_RDLINE_HISTORY
106 cirbuf_init(&rdl->history, rdl->history_buf, 0, RDLINE_HISTORY_BUF_SIZE);
107 #endif /* !NO_RDLINE_HISTORY */
111 rdline_newline(struct rdline *rdl, const char *prompt)
115 vt100_init(&rdl->vt100);
116 cirbuf_init(&rdl->left, rdl->left_buf, 0, RDLINE_BUF_SIZE);
117 cirbuf_init(&rdl->right, rdl->right_buf, 0, RDLINE_BUF_SIZE);
119 if (prompt != rdl->prompt)
120 memcpy(rdl->prompt, prompt, sizeof(rdl->prompt)-1);
121 rdl->prompt_size = strlen(prompt);
123 for (i=0 ; i<rdl->prompt_size ; i++)
124 rdl->write_char(rdl, rdl->prompt[i]);
125 rdl->status = RDLINE_RUNNING;
127 #ifndef NO_RDLINE_HISTORY
128 rdl->history_cur_line = -1;
129 #endif /* !NO_RDLINE_HISTORY */
133 rdline_stop(struct rdline *rdl)
135 rdl->status = RDLINE_INIT;
139 rdline_quit(struct rdline *rdl)
141 rdl->status = RDLINE_EXITED;
145 rdline_restart(struct rdline *rdl)
147 rdl->status = RDLINE_RUNNING;
151 rdline_get_buffer(struct rdline *rdl)
153 unsigned int len_l, len_r;
154 cirbuf_align_left(&rdl->left);
155 cirbuf_align_left(&rdl->right);
157 len_l = CIRBUF_GET_LEN(&rdl->left);
158 len_r = CIRBUF_GET_LEN(&rdl->right);
159 memcpy(rdl->left_buf+len_l, rdl->right_buf, len_r);
161 rdl->left_buf[len_l + len_r] = '\n';
162 rdl->left_buf[len_l + len_r + 1] = '\0';
163 return rdl->left_buf;
167 display_right_buffer(struct rdline *rdl, int force)
172 if (!force && CIRBUF_IS_EMPTY(&rdl->right))
175 rdline_puts(rdl, vt100_clear_right);
176 CIRBUF_FOREACH(&rdl->right, i, tmp) {
177 rdl->write_char(rdl, tmp);
179 if (!CIRBUF_IS_EMPTY(&rdl->right))
180 rdline_miniprintf(rdl, vt100_multi_left,
181 CIRBUF_GET_LEN(&rdl->right));
185 rdline_redisplay(struct rdline *rdl)
190 rdline_puts(rdl, vt100_home);
191 for (i=0 ; i<rdl->prompt_size ; i++)
192 rdl->write_char(rdl, rdl->prompt[i]);
193 CIRBUF_FOREACH(&rdl->left, i, tmp) {
194 rdl->write_char(rdl, tmp);
196 display_right_buffer(rdl, 1);
200 rdline_char_in(struct rdline *rdl, char c)
205 #ifndef NO_RDLINE_HISTORY
209 if (rdl->status == RDLINE_EXITED)
210 return RDLINE_RES_EXITED;
211 if (rdl->status != RDLINE_RUNNING)
212 return RDLINE_RES_NOT_RUNNING;
214 cmd = vt100_parser(&rdl->vt100, c);
216 return RDLINE_RES_SUCCESS;
220 case CMDLINE_KEY_CTRL_B:
221 case CMDLINE_KEY_LEFT_ARR:
222 if (CIRBUF_IS_EMPTY(&rdl->left))
224 tmp = cirbuf_get_tail(&rdl->left);
225 cirbuf_del_tail(&rdl->left);
226 cirbuf_add_head(&rdl->right, tmp);
227 rdline_puts(rdl, vt100_left_arr);
230 case CMDLINE_KEY_CTRL_F:
231 case CMDLINE_KEY_RIGHT_ARR:
232 if (CIRBUF_IS_EMPTY(&rdl->right))
234 tmp = cirbuf_get_head(&rdl->right);
235 cirbuf_del_head(&rdl->right);
236 cirbuf_add_tail(&rdl->left, tmp);
237 rdline_puts(rdl, vt100_right_arr);
240 case CMDLINE_KEY_WLEFT:
241 while (! CIRBUF_IS_EMPTY(&rdl->left) &&
242 (tmp = cirbuf_get_tail(&rdl->left)) &&
244 rdline_puts(rdl, vt100_left_arr);
245 cirbuf_del_tail(&rdl->left);
246 cirbuf_add_head(&rdl->right, tmp);
248 while (! CIRBUF_IS_EMPTY(&rdl->left) &&
249 (tmp = cirbuf_get_tail(&rdl->left)) &&
251 rdline_puts(rdl, vt100_left_arr);
252 cirbuf_del_tail(&rdl->left);
253 cirbuf_add_head(&rdl->right, tmp);
257 case CMDLINE_KEY_WRIGHT:
258 while (! CIRBUF_IS_EMPTY(&rdl->right) &&
259 (tmp = cirbuf_get_head(&rdl->right)) &&
261 rdline_puts(rdl, vt100_right_arr);
262 cirbuf_del_head(&rdl->right);
263 cirbuf_add_tail(&rdl->left, tmp);
265 while (! CIRBUF_IS_EMPTY(&rdl->right) &&
266 (tmp = cirbuf_get_head(&rdl->right)) &&
268 rdline_puts(rdl, vt100_right_arr);
269 cirbuf_del_head(&rdl->right);
270 cirbuf_add_tail(&rdl->left, tmp);
274 case CMDLINE_KEY_BKSPACE:
275 if(!cirbuf_del_tail_safe(&rdl->left)) {
276 rdline_puts(rdl, vt100_bs);
277 display_right_buffer(rdl, 1);
281 case CMDLINE_KEY_META_BKSPACE:
282 case CMDLINE_KEY_CTRL_W:
283 while (! CIRBUF_IS_EMPTY(&rdl->left) && isblank2(cirbuf_get_tail(&rdl->left))) {
284 rdline_puts(rdl, vt100_bs);
285 cirbuf_del_tail(&rdl->left);
287 while (! CIRBUF_IS_EMPTY(&rdl->left) && !isblank2(cirbuf_get_tail(&rdl->left))) {
288 rdline_puts(rdl, vt100_bs);
289 cirbuf_del_tail(&rdl->left);
291 display_right_buffer(rdl, 1);
294 case CMDLINE_KEY_META_D:
295 while (! CIRBUF_IS_EMPTY(&rdl->right) && isblank2(cirbuf_get_head(&rdl->right)))
296 cirbuf_del_head(&rdl->right);
297 while (! CIRBUF_IS_EMPTY(&rdl->right) && !isblank2(cirbuf_get_head(&rdl->right)))
298 cirbuf_del_head(&rdl->right);
299 display_right_buffer(rdl, 1);
302 case CMDLINE_KEY_SUPPR:
303 case CMDLINE_KEY_CTRL_D:
304 if (cmd == CMDLINE_KEY_CTRL_D &&
305 CIRBUF_IS_EMPTY(&rdl->left) &&
306 CIRBUF_IS_EMPTY(&rdl->right)) {
307 return RDLINE_RES_EOF;
309 if (!cirbuf_del_head_safe(&rdl->right)) {
310 display_right_buffer(rdl, 1);
314 case CMDLINE_KEY_CTRL_A:
315 if (CIRBUF_IS_EMPTY(&rdl->left))
317 rdline_miniprintf(rdl, vt100_multi_left,
318 CIRBUF_GET_LEN(&rdl->left));
319 while (! CIRBUF_IS_EMPTY(&rdl->left)) {
320 tmp = cirbuf_get_tail(&rdl->left);
321 cirbuf_del_tail(&rdl->left);
322 cirbuf_add_head(&rdl->right, tmp);
326 case CMDLINE_KEY_CTRL_E:
327 if (CIRBUF_IS_EMPTY(&rdl->right))
329 rdline_miniprintf(rdl, vt100_multi_right,
330 CIRBUF_GET_LEN(&rdl->right));
331 while (! CIRBUF_IS_EMPTY(&rdl->right)) {
332 tmp = cirbuf_get_head(&rdl->right);
333 cirbuf_del_head(&rdl->right);
334 cirbuf_add_tail(&rdl->left, tmp);
338 #ifndef NO_RDLINE_KILL_BUF
339 case CMDLINE_KEY_CTRL_K:
340 cirbuf_get_buf_head(&rdl->right, rdl->kill_buf, RDLINE_BUF_SIZE);
341 rdl->kill_size = CIRBUF_GET_LEN(&rdl->right);
342 cirbuf_del_buf_head(&rdl->right, rdl->kill_size);
343 rdline_puts(rdl, vt100_clear_right);
346 case CMDLINE_KEY_CTRL_Y:
348 while(CIRBUF_GET_LEN(&rdl->right) + CIRBUF_GET_LEN(&rdl->left) <
350 i < rdl->kill_size) {
351 cirbuf_add_tail(&rdl->left, rdl->kill_buf[i]);
352 rdl->write_char(rdl, rdl->kill_buf[i]);
355 display_right_buffer(rdl, 0);
357 #endif /* !NO_RDLINE_KILL_BUF */
359 case CMDLINE_KEY_CTRL_C:
360 rdline_puts(rdl, "\r\n");
361 rdline_newline(rdl, rdl->prompt);
364 case CMDLINE_KEY_CTRL_L:
365 rdline_redisplay(rdl);
368 case CMDLINE_KEY_TAB:
369 case CMDLINE_KEY_HELP:
370 cirbuf_align_left(&rdl->left);
371 rdl->left_buf[CIRBUF_GET_LEN(&rdl->left)] = '\0';
373 char tmp_buf[BUFSIZ];
376 unsigned int tmp_size;
378 if (cmd == CMDLINE_KEY_TAB)
383 /* see in parse.h for help on complete() */
384 ret = rdl->complete(rdl, rdl->left_buf,
385 tmp_buf, sizeof(tmp_buf),
387 /* no completion or error */
389 return RDLINE_RES_COMPLETE;
392 tmp_size = strlen(tmp_buf);
394 if (ret == RDLINE_RES_COMPLETE) {
396 while(CIRBUF_GET_LEN(&rdl->right) + CIRBUF_GET_LEN(&rdl->left) <
399 cirbuf_add_tail(&rdl->left, tmp_buf[i]);
400 rdl->write_char(rdl, tmp_buf[i]);
403 display_right_buffer(rdl, 1);
404 return RDLINE_RES_COMPLETE; /* ?? */
408 rdline_puts(rdl, "\r\n");
410 rdl->write_char(rdl, ' ');
411 for (i=0 ; tmp_buf[i] ; i++)
412 rdl->write_char(rdl, tmp_buf[i]);
413 rdline_puts(rdl, "\r\n");
414 ret = rdl->complete(rdl, rdl->left_buf,
415 tmp_buf, sizeof(tmp_buf),
419 rdline_redisplay(rdl);
421 return RDLINE_RES_COMPLETE;
423 case CMDLINE_KEY_RETURN:
424 case CMDLINE_KEY_RETURN2:
425 rdline_get_buffer(rdl);
426 rdl->status = RDLINE_INIT;
427 rdline_puts(rdl, "\r\n");
428 #ifndef NO_RDLINE_HISTORY
429 if (rdl->history_cur_line != -1)
430 rdline_remove_first_history_item(rdl);
434 rdl->validate(rdl, rdl->left_buf, CIRBUF_GET_LEN(&rdl->left)+2);
435 /* user may have stopped rdline */
436 if (rdl->status == RDLINE_EXITED)
437 return RDLINE_RES_EXITED;
438 return RDLINE_RES_VALIDATED;
440 #ifndef NO_RDLINE_HISTORY
441 case CMDLINE_KEY_UP_ARR:
442 case CMDLINE_KEY_CTRL_P:
443 if (rdl->history_cur_line == 0) {
444 rdline_remove_first_history_item(rdl);
446 if (rdl->history_cur_line <= 0) {
447 rdline_add_history(rdl, rdline_get_buffer(rdl));
448 rdl->history_cur_line = 0;
451 buf = rdline_get_history_item(rdl, rdl->history_cur_line + 1);
455 rdl->history_cur_line ++;
456 vt100_init(&rdl->vt100);
457 cirbuf_init(&rdl->left, rdl->left_buf, 0, RDLINE_BUF_SIZE);
458 cirbuf_init(&rdl->right, rdl->right_buf, 0, RDLINE_BUF_SIZE);
459 cirbuf_add_buf_tail(&rdl->left, buf, strlen(buf));
460 rdline_redisplay(rdl);
463 case CMDLINE_KEY_DOWN_ARR:
464 case CMDLINE_KEY_CTRL_N:
465 if (rdl->history_cur_line - 1 < 0)
468 rdl->history_cur_line --;
469 buf = rdline_get_history_item(rdl, rdl->history_cur_line);
472 vt100_init(&rdl->vt100);
473 cirbuf_init(&rdl->left, rdl->left_buf, 0, RDLINE_BUF_SIZE);
474 cirbuf_init(&rdl->right, rdl->right_buf, 0, RDLINE_BUF_SIZE);
475 cirbuf_add_buf_tail(&rdl->left, buf, strlen(buf));
476 rdline_redisplay(rdl);
479 #endif /* !NO_RDLINE_HISTORY */
486 return RDLINE_RES_SUCCESS;
489 if (!isprint((int)c))
490 return RDLINE_RES_SUCCESS;
493 if (CIRBUF_GET_LEN(&rdl->left) + CIRBUF_GET_LEN(&rdl->right) >= RDLINE_BUF_SIZE)
494 return RDLINE_RES_SUCCESS;
496 if (cirbuf_add_tail_safe(&rdl->left, c))
497 return RDLINE_RES_SUCCESS;
499 rdl->write_char(rdl, c);
500 display_right_buffer(rdl, 0);
502 return RDLINE_RES_SUCCESS;
508 #ifndef NO_RDLINE_HISTORY
510 rdline_remove_old_history_item(struct rdline * rdl)
514 while (! CIRBUF_IS_EMPTY(&rdl->history) ) {
515 tmp = cirbuf_get_head(&rdl->history);
516 cirbuf_del_head(&rdl->history);
523 rdline_remove_first_history_item(struct rdline * rdl)
527 if ( CIRBUF_IS_EMPTY(&rdl->history) ) {
531 cirbuf_del_tail(&rdl->history);
534 while (! CIRBUF_IS_EMPTY(&rdl->history) ) {
535 tmp = cirbuf_get_tail(&rdl->history);
538 cirbuf_del_tail(&rdl->history);
543 rdline_get_history_size(struct rdline * rdl)
545 unsigned int i, tmp, ret=0;
547 CIRBUF_FOREACH(&rdl->history, i, tmp) {
556 rdline_get_history_item(struct rdline * rdl, unsigned int idx)
558 unsigned int len, i, tmp;
560 len = rdline_get_history_size(rdl);
565 cirbuf_align_left(&rdl->history);
567 CIRBUF_FOREACH(&rdl->history, i, tmp) {
568 if ( idx == len - 1) {
569 return rdl->history_buf + i;
579 rdline_add_history(struct rdline * rdl, const char * buf)
584 for (i=0; i<len ; i++) {
585 if (buf[i] == '\n') {
591 if ( len >= RDLINE_HISTORY_BUF_SIZE )
594 while ( len >= CIRBUF_GET_FREELEN(&rdl->history) ) {
595 rdline_remove_old_history_item(rdl);
598 cirbuf_add_buf_tail(&rdl->history, buf, len);
599 cirbuf_add_tail(&rdl->history, 0);
605 rdline_clear_history(struct rdline * rdl)
607 cirbuf_init(&rdl->history, rdl->history_buf, 0, RDLINE_HISTORY_BUF_SIZE);
610 #else /* !NO_RDLINE_HISTORY */
612 int rdline_add_history(struct rdline * rdl, const char * buf) {return -1;}
613 void rdline_clear_history(struct rdline * rdl) {}
614 char * rdline_get_history_item(struct rdline * rdl, unsigned int i) {return NULL;}
617 #endif /* !NO_RDLINE_HISTORY */
620 /* STATIC USEFUL FUNCS */
623 rdline_puts(struct rdline * rdl, const char * buf)
626 while ( (c = *(buf++)) != '\0' ) {
627 rdl->write_char(rdl, c);
631 /* a very very basic printf with one arg and one format 'u' */
633 rdline_miniprintf(struct rdline *rdl, const char * buf, unsigned int val)
635 char c, started=0, div=100;
637 while ( (c=*(buf++)) ) {
639 rdl->write_char(rdl, c);
644 rdl->write_char(rdl, '%');
645 rdl->write_char(rdl, c);
648 /* val is never more than 255 */
650 c = (char)(val / div);
652 rdl->write_char(rdl, (char)(c+'0'));