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.
70 #include "cmdline_cirbuf.h"
71 #include "cmdline_rdline.h"
72 #include "cmdline_parse.h"
74 #ifndef NO_RDLINE_HISTORY
75 static void rdline_remove_old_history_item(struct rdline *rdl);
76 static void rdline_remove_first_history_item(struct rdline *rdl);
77 static unsigned int rdline_get_history_size(struct rdline *rdl);
78 #endif /* !NO_RDLINE_HISTORY */
81 static int rdline_pager_next_page(struct rdline *rdl);
82 static void rdline_asyncpager_reset(struct rdline *rdl);
83 #endif /* !NO_PAGER */
86 /* isblank() needs _XOPEN_SOURCE >= 600 || _ISOC99_SOURCE, so use our
98 rdline_init(struct rdline *rdl,
99 int fd_in, int fd_out,
100 rdline_validate_t *validate,
101 rdline_complete_t *complete,
104 memset(rdl, 0, sizeof(*rdl));
106 rdl->fd_out = fd_out;
107 rdl->validate = validate;
108 rdl->complete = complete;
110 rdl->status = RDLINE_INIT;
111 #ifndef NO_RDLINE_HISTORY
112 cirbuf_init(&rdl->history, rdl->history_buf, 0, RDLINE_HISTORY_BUF_SIZE);
113 #endif /* !NO_RDLINE_HISTORY */
117 rdline_newline(struct rdline *rdl, const char *prompt)
119 vt100_init(&rdl->vt100);
120 cirbuf_init(&rdl->left, rdl->left_buf, 0, RDLINE_BUF_SIZE);
121 cirbuf_init(&rdl->right, rdl->right_buf, 0, RDLINE_BUF_SIZE);
123 /* if pointer is the same, don't copy it */
124 if (prompt != rdl->prompt)
125 snprintf(rdl->prompt, sizeof(rdl->prompt), "%s", prompt);
127 rdline_printf(rdl, "%s", rdl->prompt);
128 rdl->status = RDLINE_RUNNING;
130 #ifndef NO_RDLINE_HISTORY
131 rdl->history_cur_line = -1;
132 #endif /* !NO_RDLINE_HISTORY */
136 rdline_stop(struct rdline *rdl)
138 rdl->status = RDLINE_INIT;
142 rdline_quit(struct rdline *rdl)
144 rdl->status = RDLINE_EXITED;
148 rdline_restart(struct rdline *rdl)
150 rdl->status = RDLINE_RUNNING;
154 rdline_get_buffer(struct rdline *rdl)
156 unsigned int len_l, len_r;
157 cirbuf_align_left(&rdl->left);
158 cirbuf_align_left(&rdl->right);
160 len_l = CIRBUF_GET_LEN(&rdl->left);
161 len_r = CIRBUF_GET_LEN(&rdl->right);
162 memcpy(rdl->left_buf+len_l, rdl->right_buf, len_r);
164 rdl->left_buf[len_l + len_r] = '\0';
165 return rdl->left_buf;
169 display_right_buffer(struct rdline *rdl, int force)
174 if (!force && CIRBUF_IS_EMPTY(&rdl->right))
177 rdline_printf(rdl, vt100_clear_right);
178 CIRBUF_FOREACH(&rdl->right, i, tmp) {
179 rdline_printf(rdl, "%c", tmp);
181 if (!CIRBUF_IS_EMPTY(&rdl->right))
182 rdline_printf(rdl, vt100_multi_left,
183 CIRBUF_GET_LEN(&rdl->right));
187 rdline_redisplay(struct rdline *rdl)
192 rdline_printf(rdl, vt100_home);
193 rdline_printf(rdl, "%s", rdl->prompt);
194 CIRBUF_FOREACH(&rdl->left, i, tmp) {
195 rdline_printf(rdl, "%c", tmp);
197 display_right_buffer(rdl, 1);
201 rdline_parse_char(struct rdline *rdl, char c)
206 #ifndef NO_RDLINE_HISTORY
210 cmd = vt100_parser(&rdl->vt100, c);
211 if (cmd == VT100_NOT_COMPLETE)
212 return RDLINE_RES_SUCCESS;
215 /* display asynchrounous printf if any */
216 if (rdl->pager_buf != NULL) {
217 if (cmd == VT100_STD_CHAR && c == 'q') {
218 rdline_asyncpager_reset(rdl);
220 if (rdl->pager_buf == NULL)
221 return RDLINE_RES_VALIDATED;
224 if (rdline_pager_next_page(rdl) == 0) {
225 rdline_asyncpager_reset(rdl);
227 if (rdl->pager_buf == NULL)
228 return RDLINE_RES_VALIDATED;
230 /* async printf was called in cb() */
231 return RDLINE_RES_SUCCESS;
235 /* process control chars */
236 if (cmd != VT100_STD_CHAR) {
238 case CMDLINE_KEY_CTRL_B:
239 case CMDLINE_KEY_LEFT_ARR:
240 if (CIRBUF_IS_EMPTY(&rdl->left))
242 tmp = cirbuf_get_tail(&rdl->left);
243 cirbuf_del_tail(&rdl->left);
244 cirbuf_add_head(&rdl->right, tmp);
245 rdline_printf(rdl, vt100_left_arr);
248 case CMDLINE_KEY_CTRL_F:
249 case CMDLINE_KEY_RIGHT_ARR:
250 if (CIRBUF_IS_EMPTY(&rdl->right))
252 tmp = cirbuf_get_head(&rdl->right);
253 cirbuf_del_head(&rdl->right);
254 cirbuf_add_tail(&rdl->left, tmp);
255 rdline_printf(rdl, vt100_right_arr);
258 case CMDLINE_KEY_WLEFT:
259 while (! CIRBUF_IS_EMPTY(&rdl->left) &&
260 (tmp = cirbuf_get_tail(&rdl->left)) &&
262 rdline_printf(rdl, vt100_left_arr);
263 cirbuf_del_tail(&rdl->left);
264 cirbuf_add_head(&rdl->right, tmp);
266 while (! CIRBUF_IS_EMPTY(&rdl->left) &&
267 (tmp = cirbuf_get_tail(&rdl->left)) &&
269 rdline_printf(rdl, vt100_left_arr);
270 cirbuf_del_tail(&rdl->left);
271 cirbuf_add_head(&rdl->right, tmp);
275 case CMDLINE_KEY_WRIGHT:
276 while (! CIRBUF_IS_EMPTY(&rdl->right) &&
277 (tmp = cirbuf_get_head(&rdl->right)) &&
279 rdline_printf(rdl, vt100_right_arr);
280 cirbuf_del_head(&rdl->right);
281 cirbuf_add_tail(&rdl->left, tmp);
283 while (! CIRBUF_IS_EMPTY(&rdl->right) &&
284 (tmp = cirbuf_get_head(&rdl->right)) &&
286 rdline_printf(rdl, vt100_right_arr);
287 cirbuf_del_head(&rdl->right);
288 cirbuf_add_tail(&rdl->left, tmp);
292 case CMDLINE_KEY_BKSPACE:
293 if(!cirbuf_del_tail_safe(&rdl->left)) {
294 rdline_printf(rdl, vt100_bs);
295 display_right_buffer(rdl, 1);
299 case CMDLINE_KEY_META_BKSPACE:
300 case CMDLINE_KEY_CTRL_W:
301 while (! CIRBUF_IS_EMPTY(&rdl->left) &&
302 isblank2(cirbuf_get_tail(&rdl->left))) {
303 rdline_printf(rdl, vt100_bs);
304 cirbuf_del_tail(&rdl->left);
306 while (! CIRBUF_IS_EMPTY(&rdl->left) &&
307 !isblank2(cirbuf_get_tail(&rdl->left))) {
308 rdline_printf(rdl, vt100_bs);
309 cirbuf_del_tail(&rdl->left);
311 display_right_buffer(rdl, 1);
314 case CMDLINE_KEY_META_D:
315 while (! CIRBUF_IS_EMPTY(&rdl->right) &&
316 isblank2(cirbuf_get_head(&rdl->right)))
317 cirbuf_del_head(&rdl->right);
318 while (! CIRBUF_IS_EMPTY(&rdl->right) &&
319 !isblank2(cirbuf_get_head(&rdl->right)))
320 cirbuf_del_head(&rdl->right);
321 display_right_buffer(rdl, 1);
324 case CMDLINE_KEY_SUPPR:
325 case CMDLINE_KEY_CTRL_D:
326 if (cmd == CMDLINE_KEY_CTRL_D &&
327 CIRBUF_IS_EMPTY(&rdl->left) &&
328 CIRBUF_IS_EMPTY(&rdl->right)) {
329 return RDLINE_RES_EOF;
331 if (!cirbuf_del_head_safe(&rdl->right)) {
332 display_right_buffer(rdl, 1);
336 case CMDLINE_KEY_CTRL_A:
337 if (CIRBUF_IS_EMPTY(&rdl->left))
339 rdline_printf(rdl, vt100_multi_left,
340 CIRBUF_GET_LEN(&rdl->left));
341 while (! CIRBUF_IS_EMPTY(&rdl->left)) {
342 tmp = cirbuf_get_tail(&rdl->left);
343 cirbuf_del_tail(&rdl->left);
344 cirbuf_add_head(&rdl->right, tmp);
348 case CMDLINE_KEY_CTRL_E:
349 if (CIRBUF_IS_EMPTY(&rdl->right))
351 rdline_printf(rdl, vt100_multi_right,
352 CIRBUF_GET_LEN(&rdl->right));
353 while (! CIRBUF_IS_EMPTY(&rdl->right)) {
354 tmp = cirbuf_get_head(&rdl->right);
355 cirbuf_del_head(&rdl->right);
356 cirbuf_add_tail(&rdl->left, tmp);
360 #ifndef NO_RDLINE_KILL_BUF
361 case CMDLINE_KEY_CTRL_K:
362 cirbuf_get_buf_head(&rdl->right, rdl->kill_buf, RDLINE_BUF_SIZE);
363 rdl->kill_size = CIRBUF_GET_LEN(&rdl->right);
364 cirbuf_del_buf_head(&rdl->right, rdl->kill_size);
365 rdline_printf(rdl, vt100_clear_right);
368 case CMDLINE_KEY_CTRL_Y:
370 while(CIRBUF_GET_LEN(&rdl->right) + CIRBUF_GET_LEN(&rdl->left) <
372 i < rdl->kill_size) {
373 cirbuf_add_tail(&rdl->left, rdl->kill_buf[i]);
374 rdline_printf(rdl, "%c", rdl->kill_buf[i]);
377 display_right_buffer(rdl, 0);
379 #endif /* !NO_RDLINE_KILL_BUF */
381 case CMDLINE_KEY_CTRL_C:
382 rdline_printf(rdl, "\r\n");
383 rdline_newline(rdl, rdl->prompt);
386 case CMDLINE_KEY_CTRL_L:
387 rdline_redisplay(rdl);
390 case CMDLINE_KEY_HELP: {
391 if (rdl->help == NULL)
394 cirbuf_align_left(&rdl->left);
395 rdl->left_buf[CIRBUF_GET_LEN(&rdl->left)] = '\0';
396 rdline_printf(rdl, "\r\n");
397 rdl->help(rdl, rdl->left_buf, rdline_write, rdl);
399 if (rdl->pager_buf != NULL) {
401 return RDLINE_RES_SUCCESS;
404 rdline_redisplay(rdl);
408 case CMDLINE_KEY_TAB: {
409 char tmp_buf[CMDLINE_MAX_TOKEN_SIZE];
410 int ret; //, curline = 0;
411 unsigned int tmp_size;
413 if (rdl->complete == NULL)
416 cirbuf_align_left(&rdl->left);
417 rdl->left_buf[CIRBUF_GET_LEN(&rdl->left)] = '\0';
419 /* see in parse.h for help on complete() */
420 ret = rdl->complete(rdl, rdl->left_buf,
421 tmp_buf, sizeof(tmp_buf));
422 /* no completion or error */
423 if (ret == CMDLINE_COMPLETE_NONE ||
424 ret == CMDLINE_COMPLETE_MANY) {
425 cirbuf_align_left(&rdl->left);
426 rdl->left_buf[CIRBUF_GET_LEN(&rdl->left)] = '\0';
427 rdline_printf(rdl, "\r\n");
428 rdl->help(rdl, rdl->left_buf, rdline_write, rdl);
430 if (rdl->pager_buf != NULL) {
432 return RDLINE_RES_SUCCESS;
435 rdline_redisplay(rdl);
439 tmp_size = strlen(tmp_buf);
442 while(CIRBUF_GET_LEN(&rdl->right) + CIRBUF_GET_LEN(&rdl->left) <
445 cirbuf_add_tail(&rdl->left, tmp_buf[i]);
446 rdline_printf(rdl, "%c", tmp_buf[i]);
449 display_right_buffer(rdl, 1);
453 case CMDLINE_KEY_RETURN:
454 case CMDLINE_KEY_RETURN2:
455 cirbuf_align_left(&rdl->left);
456 rdl->left_buf[CIRBUF_GET_LEN(&rdl->left)] = '\n';
457 rdl->left_buf[CIRBUF_GET_LEN(&rdl->left) + 1] = '\0';
458 rdline_printf(rdl, "\r\n");
459 #ifndef NO_RDLINE_HISTORY
460 if (rdl->history_cur_line != -1)
461 rdline_remove_first_history_item(rdl);
465 rdl->validate(rdl, rdl->left_buf, CIRBUF_GET_LEN(&rdl->left)+2);
467 /* user may have stopped rdline */
468 if (rdl->status == RDLINE_EXITED) {
469 rdline_asyncpager_reset(rdl);
470 return RDLINE_RES_EXITED;
472 if (rdl->pager_buf != NULL) {
474 return RDLINE_RES_SUCCESS;
476 rdl->status = RDLINE_INIT;
478 if (rdl->status == RDLINE_EXITED)
479 return RDLINE_RES_EXITED;
481 return RDLINE_RES_VALIDATED;
483 #ifndef NO_RDLINE_HISTORY
484 case CMDLINE_KEY_UP_ARR:
485 case CMDLINE_KEY_CTRL_P:
486 if (rdl->history_cur_line == 0) {
487 rdline_remove_first_history_item(rdl);
489 if (rdl->history_cur_line <= 0) {
490 rdline_add_history(rdl, rdline_get_buffer(rdl));
491 rdl->history_cur_line = 0;
494 buf = rdline_get_history_item(rdl, rdl->history_cur_line + 1);
498 rdl->history_cur_line ++;
499 vt100_init(&rdl->vt100);
500 cirbuf_init(&rdl->left, rdl->left_buf, 0, RDLINE_BUF_SIZE);
501 cirbuf_init(&rdl->right, rdl->right_buf, 0, RDLINE_BUF_SIZE);
502 cirbuf_add_buf_tail(&rdl->left, buf, strlen(buf));
503 rdline_redisplay(rdl);
506 case CMDLINE_KEY_DOWN_ARR:
507 case CMDLINE_KEY_CTRL_N:
508 if (rdl->history_cur_line - 1 < 0)
511 rdl->history_cur_line --;
512 buf = rdline_get_history_item(rdl, rdl->history_cur_line);
515 vt100_init(&rdl->vt100);
516 cirbuf_init(&rdl->left, rdl->left_buf, 0, RDLINE_BUF_SIZE);
517 cirbuf_init(&rdl->right, rdl->right_buf, 0, RDLINE_BUF_SIZE);
518 cirbuf_add_buf_tail(&rdl->left, buf, strlen(buf));
519 rdline_redisplay(rdl);
522 #endif /* !NO_RDLINE_HISTORY */
529 return RDLINE_RES_SUCCESS;
532 if (!isprint((int)c))
533 return RDLINE_RES_SUCCESS;
536 if (CIRBUF_GET_LEN(&rdl->left) + CIRBUF_GET_LEN(&rdl->right) >= RDLINE_BUF_SIZE)
537 return RDLINE_RES_SUCCESS;
539 if (cirbuf_add_tail_safe(&rdl->left, c))
540 return RDLINE_RES_SUCCESS;
542 rdline_printf(rdl, "%c", c);
543 display_right_buffer(rdl, 0);
545 return RDLINE_RES_SUCCESS;
549 rdline_char_in(struct rdline *rdl, char c)
552 const char *history, *buffer;
554 if (rdl->status == RDLINE_EXITED)
555 return RDLINE_RES_EXITED;
556 if (rdl->status != RDLINE_RUNNING)
557 return RDLINE_RES_NOT_RUNNING;
559 ret = rdline_parse_char(rdl, c);
561 /* add line to history */
562 if (ret == RDLINE_RES_VALIDATED) {
563 buffer = rdline_get_buffer(rdl);
564 history = rdline_get_history_item(rdl, 0);
566 same = !strcmp(buffer, history);
568 if (strlen(buffer) >= 1 && same == 0)
569 rdline_add_history(rdl, buffer);
576 rdline(struct rdline *rdl, const char *prompt)
579 int ret = RDLINE_RES_NOT_RUNNING;
581 rdline_newline(rdl, prompt);
583 if (read(rdl->fd_in, &c, 1) < 0)
585 ret = rdline_char_in(rdl, c);
586 if (ret != RDLINE_RES_SUCCESS)
595 #ifndef NO_RDLINE_HISTORY
597 rdline_remove_old_history_item(struct rdline * rdl)
601 while (! CIRBUF_IS_EMPTY(&rdl->history) ) {
602 tmp = cirbuf_get_head(&rdl->history);
603 cirbuf_del_head(&rdl->history);
610 rdline_remove_first_history_item(struct rdline * rdl)
614 if ( CIRBUF_IS_EMPTY(&rdl->history) ) {
618 cirbuf_del_tail(&rdl->history);
621 while (! CIRBUF_IS_EMPTY(&rdl->history) ) {
622 tmp = cirbuf_get_tail(&rdl->history);
625 cirbuf_del_tail(&rdl->history);
630 rdline_get_history_size(struct rdline * rdl)
632 unsigned int i, tmp, ret=0;
634 CIRBUF_FOREACH(&rdl->history, i, tmp) {
643 rdline_get_history_item(struct rdline * rdl, unsigned int idx)
645 unsigned int len, i, tmp;
647 len = rdline_get_history_size(rdl);
652 cirbuf_align_left(&rdl->history);
654 CIRBUF_FOREACH(&rdl->history, i, tmp) {
655 if ( idx == len - 1) {
656 return rdl->history_buf + i;
666 rdline_add_history(struct rdline * rdl, const char * buf)
671 if (len >= RDLINE_HISTORY_BUF_SIZE)
674 while (len >= CIRBUF_GET_FREELEN(&rdl->history)) {
675 rdline_remove_old_history_item(rdl);
678 cirbuf_add_buf_tail(&rdl->history, buf, len);
679 cirbuf_add_tail(&rdl->history, 0);
685 rdline_clear_history(struct rdline * rdl)
687 cirbuf_init(&rdl->history, rdl->history_buf, 0, RDLINE_HISTORY_BUF_SIZE);
690 #else /* !NO_RDLINE_HISTORY */
692 int rdline_add_history(struct rdline * rdl, const char * buf)
697 void rdline_clear_history(struct rdline * rdl)
702 char * rdline_get_history_item(struct rdline * rdl, unsigned int i)
708 #endif /* !NO_RDLINE_HISTORY */
712 rdline_write(const struct rdline *rdl, void *buf, size_t count)
714 return write(rdl->fd_out, buf, count);
718 rdline_vprintf(const struct rdline *rdl, const char *fmt, va_list ap)
729 ret = vdprintf(rdl->fd_out, fmt, ap);
731 buf = malloc(BUFSIZ);
735 ret = vsnprintf(buf, BUFSIZ, fmt, ap);
738 write(rdl->fd_out, buf, (ret >= BUFSIZ) ? BUFSIZ : ret);
746 rdline_printf(const struct rdline *rdl, const char *fmt, ...)
752 ret = rdline_vprintf(rdl, fmt, ap);
759 /* reset pager state */
761 rdline_asyncpager_reset(struct rdline *rdl)
763 if (rdl->pager_buf) {
764 free(rdl->pager_buf);
765 rdl->pager_buf = NULL;
767 rdl->pager_lines = 0;
772 /* Return the offset of the i-th occurence of char c in string s. If
773 * there is less than i occurences, return -1 and fill i with the
776 strnchr(const char *s, char c, int *i)
779 const char *orig = s;
792 /* display a page of data from pager, return 0 if all is displayed */
794 rdline_pager_next_page(struct rdline *rdl)
796 int lines = RDLINE_MAX_LINES;
804 rdline_printf(rdl, vt100_home);
805 rdline_printf(rdl, vt100_clear_right);
809 /* we know that s is 0-terminated */
810 displen = strnchr(s, '\n', &lines);
811 rdl->pager_lines = lines;
813 /* we can display all the data */
815 write(rdl->fd_out, s, rdl->pager_len);
816 free(rdl->pager_buf);
817 rdl->pager_buf = NULL;
821 displen = displen + 1; /* include \n */
822 write(rdl->fd_out, s, displen);
823 rdl->pager_off += displen;
824 rdl->pager_len -= displen;
826 rdline_printf(rdl, "--- press a key to continue ---");
830 /* push data in pager */
832 rdline_pager_push(struct rdline *rdl, char *s, int len)
834 /* display as many lines as we can */
835 if (rdl->pager_lines < RDLINE_MAX_LINES) {
836 int lines = RDLINE_MAX_LINES - rdl->pager_lines;
839 /* we know that s is 0-terminated */
840 displen = strnchr(s, '\n', &lines);
841 rdl->pager_lines += lines;
843 /* we can display all the data */
845 write(rdl->fd_out, s, len);
848 displen = displen + 1; /* include \n */
849 write(rdl->fd_out, s, displen);
854 if (rdl->pager_buf == NULL) {
855 rdline_printf(rdl, "--- press a key to continue ---");
857 rdl->pager_buf = realloc(rdl->pager_buf, rdl->pager_len + len);
858 if (rdl->pager_buf == NULL) {
859 rdline_asyncpager_reset(rdl);
863 memcpy(rdl->pager_buf + rdl->pager_len, s, len);
864 rdl->pager_len += len;
868 /* XXX we should have a specific return value when displaying will be
869 * asynchronous, we may also have a callback */
871 rdline_asyncpager_printf(struct rdline *rdl, const char *fmt, ...)
880 buf = malloc(BUFSIZ);
885 n = vsnprintf(buf, BUFSIZ, fmt, ap);
891 rdline_pager_push(rdl, buf, n);
895 #endif /* !NO_PAGER */