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 /* user ask to exit pager */
218 if (cmd == VT100_STD_CHAR && c == 'q') {
219 rdline_asyncpager_reset(rdl);
220 if (rdl->pager_cb != NULL) {
221 rdl->pager_cb(rdl, rdl->pager_arg);
222 rdl->pager_cb = NULL;
224 rdline_redisplay(rdl);
225 return RDLINE_RES_SUCCESS;
228 /* last page is displayed */
229 else if (rdline_pager_next_page(rdl) == 0) {
230 rdline_asyncpager_reset(rdl);
231 if (rdl->pager_cb != NULL) {
232 rdl->pager_cb(rdl, rdl->pager_arg);
233 rdl->pager_cb = NULL;
235 rdline_redisplay(rdl);
238 /* pages remain, async printf was called in cb() */
239 return RDLINE_RES_SUCCESS;
243 /* process control chars */
244 if (cmd != VT100_STD_CHAR) {
246 case CMDLINE_KEY_CTRL_B:
247 case CMDLINE_KEY_LEFT_ARR:
248 if (CIRBUF_IS_EMPTY(&rdl->left))
250 tmp = cirbuf_get_tail(&rdl->left);
251 cirbuf_del_tail(&rdl->left);
252 cirbuf_add_head(&rdl->right, tmp);
253 rdline_printf(rdl, vt100_left_arr);
256 case CMDLINE_KEY_CTRL_F:
257 case CMDLINE_KEY_RIGHT_ARR:
258 if (CIRBUF_IS_EMPTY(&rdl->right))
260 tmp = cirbuf_get_head(&rdl->right);
261 cirbuf_del_head(&rdl->right);
262 cirbuf_add_tail(&rdl->left, tmp);
263 rdline_printf(rdl, vt100_right_arr);
266 case CMDLINE_KEY_WLEFT:
267 while (! CIRBUF_IS_EMPTY(&rdl->left) &&
268 (tmp = cirbuf_get_tail(&rdl->left)) &&
270 rdline_printf(rdl, vt100_left_arr);
271 cirbuf_del_tail(&rdl->left);
272 cirbuf_add_head(&rdl->right, tmp);
274 while (! CIRBUF_IS_EMPTY(&rdl->left) &&
275 (tmp = cirbuf_get_tail(&rdl->left)) &&
277 rdline_printf(rdl, vt100_left_arr);
278 cirbuf_del_tail(&rdl->left);
279 cirbuf_add_head(&rdl->right, tmp);
283 case CMDLINE_KEY_WRIGHT:
284 while (! CIRBUF_IS_EMPTY(&rdl->right) &&
285 (tmp = cirbuf_get_head(&rdl->right)) &&
287 rdline_printf(rdl, vt100_right_arr);
288 cirbuf_del_head(&rdl->right);
289 cirbuf_add_tail(&rdl->left, tmp);
291 while (! CIRBUF_IS_EMPTY(&rdl->right) &&
292 (tmp = cirbuf_get_head(&rdl->right)) &&
294 rdline_printf(rdl, vt100_right_arr);
295 cirbuf_del_head(&rdl->right);
296 cirbuf_add_tail(&rdl->left, tmp);
300 case CMDLINE_KEY_BKSPACE:
301 if(!cirbuf_del_tail_safe(&rdl->left)) {
302 rdline_printf(rdl, vt100_bs);
303 display_right_buffer(rdl, 1);
307 case CMDLINE_KEY_META_BKSPACE:
308 case CMDLINE_KEY_CTRL_W:
309 while (! CIRBUF_IS_EMPTY(&rdl->left) &&
310 isblank2(cirbuf_get_tail(&rdl->left))) {
311 rdline_printf(rdl, vt100_bs);
312 cirbuf_del_tail(&rdl->left);
314 while (! CIRBUF_IS_EMPTY(&rdl->left) &&
315 !isblank2(cirbuf_get_tail(&rdl->left))) {
316 rdline_printf(rdl, vt100_bs);
317 cirbuf_del_tail(&rdl->left);
319 display_right_buffer(rdl, 1);
322 case CMDLINE_KEY_META_D:
323 while (! CIRBUF_IS_EMPTY(&rdl->right) &&
324 isblank2(cirbuf_get_head(&rdl->right)))
325 cirbuf_del_head(&rdl->right);
326 while (! CIRBUF_IS_EMPTY(&rdl->right) &&
327 !isblank2(cirbuf_get_head(&rdl->right)))
328 cirbuf_del_head(&rdl->right);
329 display_right_buffer(rdl, 1);
332 case CMDLINE_KEY_SUPPR:
333 case CMDLINE_KEY_CTRL_D:
334 if (cmd == CMDLINE_KEY_CTRL_D &&
335 CIRBUF_IS_EMPTY(&rdl->left) &&
336 CIRBUF_IS_EMPTY(&rdl->right)) {
337 return RDLINE_RES_EOF;
339 if (!cirbuf_del_head_safe(&rdl->right)) {
340 display_right_buffer(rdl, 1);
344 case CMDLINE_KEY_CTRL_A:
345 if (CIRBUF_IS_EMPTY(&rdl->left))
347 rdline_printf(rdl, vt100_multi_left,
348 CIRBUF_GET_LEN(&rdl->left));
349 while (! CIRBUF_IS_EMPTY(&rdl->left)) {
350 tmp = cirbuf_get_tail(&rdl->left);
351 cirbuf_del_tail(&rdl->left);
352 cirbuf_add_head(&rdl->right, tmp);
356 case CMDLINE_KEY_CTRL_E:
357 if (CIRBUF_IS_EMPTY(&rdl->right))
359 rdline_printf(rdl, vt100_multi_right,
360 CIRBUF_GET_LEN(&rdl->right));
361 while (! CIRBUF_IS_EMPTY(&rdl->right)) {
362 tmp = cirbuf_get_head(&rdl->right);
363 cirbuf_del_head(&rdl->right);
364 cirbuf_add_tail(&rdl->left, tmp);
368 #ifndef NO_RDLINE_KILL_BUF
369 case CMDLINE_KEY_CTRL_K:
370 cirbuf_get_buf_head(&rdl->right, rdl->kill_buf, RDLINE_BUF_SIZE);
371 rdl->kill_size = CIRBUF_GET_LEN(&rdl->right);
372 cirbuf_del_buf_head(&rdl->right, rdl->kill_size);
373 rdline_printf(rdl, vt100_clear_right);
376 case CMDLINE_KEY_CTRL_Y:
378 while(CIRBUF_GET_LEN(&rdl->right) + CIRBUF_GET_LEN(&rdl->left) <
380 i < rdl->kill_size) {
381 cirbuf_add_tail(&rdl->left, rdl->kill_buf[i]);
382 rdline_printf(rdl, "%c", rdl->kill_buf[i]);
385 display_right_buffer(rdl, 0);
387 #endif /* !NO_RDLINE_KILL_BUF */
389 case CMDLINE_KEY_CTRL_C:
390 rdline_printf(rdl, "\r\n");
391 rdline_newline(rdl, rdl->prompt);
394 case CMDLINE_KEY_CTRL_L:
395 rdline_redisplay(rdl);
398 case CMDLINE_KEY_HELP: {
399 if (rdl->help == NULL)
402 cirbuf_align_left(&rdl->left);
403 rdl->left_buf[CIRBUF_GET_LEN(&rdl->left)] = '\0';
404 rdline_printf(rdl, "\r\n");
406 rdl->help(rdl, rdl->left_buf, rdline_asyncpager_write, rdl);
407 if (rdl->pager_buf != NULL)
408 return RDLINE_RES_SUCCESS;
410 rdline_asyncpager_reset(rdl);
412 rdl->help(rdl, rdl->left_buf, rdline_write, rdl);
414 rdline_redisplay(rdl);
418 case CMDLINE_KEY_TAB: {
419 char tmp_buf[CMDLINE_MAX_TOKEN_SIZE];
421 unsigned int tmp_size;
423 if (rdl->complete == NULL)
426 cirbuf_align_left(&rdl->left);
427 rdl->left_buf[CIRBUF_GET_LEN(&rdl->left)] = '\0';
429 /* see in parse.h for help on complete() */
430 ret = rdl->complete(rdl, rdl->left_buf,
431 tmp_buf, sizeof(tmp_buf));
432 /* no completion or error */
433 if (ret == CMDLINE_COMPLETE_NONE ||
434 ret == CMDLINE_COMPLETE_MANY) {
435 cirbuf_align_left(&rdl->left);
436 rdl->left_buf[CIRBUF_GET_LEN(&rdl->left)] = '\0';
437 rdline_printf(rdl, "\r\n");
439 rdl->help(rdl, rdl->left_buf, rdline_asyncpager_write,
441 if (rdl->pager_buf != NULL)
442 return RDLINE_RES_SUCCESS;
444 rdline_asyncpager_reset(rdl);
446 rdl->help(rdl, rdl->left_buf, rdline_write, rdl);
448 rdline_redisplay(rdl);
452 tmp_size = strlen(tmp_buf);
455 while(CIRBUF_GET_LEN(&rdl->right) + CIRBUF_GET_LEN(&rdl->left) <
458 cirbuf_add_tail(&rdl->left, tmp_buf[i]);
459 rdline_printf(rdl, "%c", tmp_buf[i]);
462 display_right_buffer(rdl, 1);
466 case CMDLINE_KEY_RETURN:
467 case CMDLINE_KEY_RETURN2: {
469 while (!CIRBUF_IS_EMPTY(&rdl->right) &&
470 (tmp = cirbuf_get_head(&rdl->right))) {
471 cirbuf_del_head(&rdl->right);
472 cirbuf_add_tail(&rdl->left, tmp);
474 cirbuf_align_left(&rdl->left);
475 rdl->left_buf[CIRBUF_GET_LEN(&rdl->left)] = '\n';
476 rdl->left_buf[CIRBUF_GET_LEN(&rdl->left) + 1] = '\0';
477 rdline_printf(rdl, "\r\n");
478 #ifndef NO_RDLINE_HISTORY
479 if (rdl->history_cur_line != -1)
480 rdline_remove_first_history_item(rdl);
484 rdl->validate(rdl, rdl->left_buf, CIRBUF_GET_LEN(&rdl->left)+2);
486 /* user may have stopped rdline */
487 if (rdl->status == RDLINE_EXITED) {
488 rdline_asyncpager_reset(rdl);
489 return RDLINE_RES_EXITED;
491 if (rdl->pager_buf != NULL)
492 return RDLINE_RES_SUCCESS;
494 rdline_asyncpager_reset(rdl);
496 rdl->status = RDLINE_INIT;
498 if (rdl->status == RDLINE_EXITED)
499 return RDLINE_RES_EXITED;
501 return RDLINE_RES_VALIDATED;
503 #ifndef NO_RDLINE_HISTORY
504 case CMDLINE_KEY_UP_ARR:
505 case CMDLINE_KEY_CTRL_P:
506 if (rdl->history_cur_line == 0) {
507 rdline_remove_first_history_item(rdl);
509 if (rdl->history_cur_line <= 0) {
510 rdline_add_history(rdl, rdline_get_buffer(rdl));
511 rdl->history_cur_line = 0;
514 buf = rdline_get_history_item(rdl, rdl->history_cur_line + 1);
518 rdl->history_cur_line ++;
519 vt100_init(&rdl->vt100);
520 cirbuf_init(&rdl->left, rdl->left_buf, 0, RDLINE_BUF_SIZE);
521 cirbuf_init(&rdl->right, rdl->right_buf, 0, RDLINE_BUF_SIZE);
522 cirbuf_add_buf_tail(&rdl->left, buf, strlen(buf));
523 rdline_redisplay(rdl);
526 case CMDLINE_KEY_DOWN_ARR:
527 case CMDLINE_KEY_CTRL_N:
528 if (rdl->history_cur_line - 1 < 0)
531 rdl->history_cur_line --;
532 buf = rdline_get_history_item(rdl, rdl->history_cur_line);
535 vt100_init(&rdl->vt100);
536 cirbuf_init(&rdl->left, rdl->left_buf, 0, RDLINE_BUF_SIZE);
537 cirbuf_init(&rdl->right, rdl->right_buf, 0, RDLINE_BUF_SIZE);
538 cirbuf_add_buf_tail(&rdl->left, buf, strlen(buf));
539 rdline_redisplay(rdl);
542 #endif /* !NO_RDLINE_HISTORY */
548 return RDLINE_RES_SUCCESS;
551 if (!isprint((int)c))
552 return RDLINE_RES_SUCCESS;
555 if (CIRBUF_GET_LEN(&rdl->left) + CIRBUF_GET_LEN(&rdl->right) >= RDLINE_BUF_SIZE)
556 return RDLINE_RES_SUCCESS;
558 if (cirbuf_add_tail_safe(&rdl->left, c))
559 return RDLINE_RES_SUCCESS;
561 rdline_printf(rdl, "%c", c);
562 display_right_buffer(rdl, 0);
564 return RDLINE_RES_SUCCESS;
568 rdline_char_in(struct rdline *rdl, char c)
571 const char *history, *buffer;
573 if (rdl->status == RDLINE_EXITED)
574 return RDLINE_RES_EXITED;
575 if (rdl->status != RDLINE_RUNNING)
576 return RDLINE_RES_NOT_RUNNING;
578 ret = rdline_parse_char(rdl, c);
580 /* add line to history */
581 if (ret == RDLINE_RES_VALIDATED) {
582 buffer = rdline_get_buffer(rdl);
583 history = rdline_get_history_item(rdl, 0);
585 same = !strcmp(buffer, history);
587 if (strlen(buffer) >= 1 && same == 0)
588 rdline_add_history(rdl, buffer);
595 rdline(struct rdline *rdl, const char *prompt)
598 int ret = RDLINE_RES_NOT_RUNNING;
600 rdline_newline(rdl, prompt);
602 if (read(rdl->fd_in, &c, 1) < 0)
604 ret = rdline_char_in(rdl, c);
605 if (ret != RDLINE_RES_SUCCESS)
614 #ifndef NO_RDLINE_HISTORY
616 rdline_remove_old_history_item(struct rdline * rdl)
620 while (! CIRBUF_IS_EMPTY(&rdl->history) ) {
621 tmp = cirbuf_get_head(&rdl->history);
622 cirbuf_del_head(&rdl->history);
629 rdline_remove_first_history_item(struct rdline * rdl)
633 if ( CIRBUF_IS_EMPTY(&rdl->history) ) {
637 cirbuf_del_tail(&rdl->history);
640 while (! CIRBUF_IS_EMPTY(&rdl->history) ) {
641 tmp = cirbuf_get_tail(&rdl->history);
644 cirbuf_del_tail(&rdl->history);
649 rdline_get_history_size(struct rdline * rdl)
651 unsigned int i, tmp, ret=0;
653 CIRBUF_FOREACH(&rdl->history, i, tmp) {
662 rdline_get_history_item(struct rdline * rdl, unsigned int idx)
664 unsigned int len, i, tmp;
666 len = rdline_get_history_size(rdl);
671 cirbuf_align_left(&rdl->history);
673 CIRBUF_FOREACH(&rdl->history, i, tmp) {
674 if ( idx == len - 1) {
675 return rdl->history_buf + i;
685 rdline_add_history(struct rdline * rdl, const char * buf)
690 if (len >= RDLINE_HISTORY_BUF_SIZE)
693 while (len >= CIRBUF_GET_FREELEN(&rdl->history)) {
694 rdline_remove_old_history_item(rdl);
697 cirbuf_add_buf_tail(&rdl->history, buf, len);
698 cirbuf_add_tail(&rdl->history, 0);
704 rdline_clear_history(struct rdline * rdl)
706 cirbuf_init(&rdl->history, rdl->history_buf, 0, RDLINE_HISTORY_BUF_SIZE);
709 #else /* !NO_RDLINE_HISTORY */
711 int rdline_add_history(struct rdline * rdl, const char * buf)
716 void rdline_clear_history(struct rdline * rdl)
721 char * rdline_get_history_item(struct rdline * rdl, unsigned int i)
727 #endif /* !NO_RDLINE_HISTORY */
731 rdline_write(const struct rdline *rdl, void *buf, size_t count)
733 return write(rdl->fd_out, buf, count);
737 rdline_vprintf(const struct rdline *rdl, const char *fmt, va_list ap)
748 ret = vdprintf(rdl->fd_out, fmt, ap);
750 buf = malloc(BUFSIZ);
754 ret = vsnprintf(buf, BUFSIZ, fmt, ap);
757 write(rdl->fd_out, buf, (ret >= BUFSIZ) ? BUFSIZ : ret);
765 rdline_printf(const struct rdline *rdl, const char *fmt, ...)
771 ret = rdline_vprintf(rdl, fmt, ap);
778 /* reset pager state */
780 rdline_asyncpager_reset(struct rdline *rdl)
782 if (rdl->pager_buf) {
783 free(rdl->pager_buf);
784 rdl->pager_buf = NULL;
786 rdl->pager_lines = 0;
791 /* Return the offset of the i-th occurence of char c in string s. If
792 * there is less than i occurences, return -1 and fill i with the
795 strnchr(const char *s, char c, int *i)
798 const char *orig = s;
811 /* display a page of data from pager, return 0 if all is displayed */
813 rdline_pager_next_page(struct rdline *rdl)
815 int lines = RDLINE_MAX_LINES;
823 rdline_printf(rdl, vt100_home);
824 rdline_printf(rdl, vt100_clear_right);
828 /* we know that s is 0-terminated */
829 displen = strnchr(s, '\n', &lines);
830 rdl->pager_lines = lines;
832 /* we can display all the data */
834 write(rdl->fd_out, s, rdl->pager_len);
835 free(rdl->pager_buf);
836 rdl->pager_buf = NULL;
840 displen = displen + 1; /* include \n */
841 write(rdl->fd_out, s, displen);
842 rdl->pager_off += displen;
843 rdl->pager_len -= displen;
845 rdline_printf(rdl, "--- press a key to continue ---");
849 /* push data in pager */
851 rdline_asyncpager_write(struct rdline *rdl, void *buf, size_t len)
855 /* display as many lines as we can */
856 if (rdl->pager_lines < RDLINE_MAX_LINES) {
857 int lines = RDLINE_MAX_LINES - rdl->pager_lines;
860 /* we know that s is 0-terminated */
861 displen = strnchr(s, '\n', &lines);
862 rdl->pager_lines += lines;
864 /* we can display all the data */
866 write(rdl->fd_out, s, len);
869 displen = displen + 1; /* include \n */
870 write(rdl->fd_out, s, displen);
875 if (rdl->pager_buf == NULL) {
876 rdline_printf(rdl, "--- press a key to continue ---");
878 rdl->pager_buf = realloc(rdl->pager_buf, rdl->pager_len + len);
879 if (rdl->pager_buf == NULL) {
880 rdline_asyncpager_reset(rdl);
884 memcpy(rdl->pager_buf + rdl->pager_len, s, len);
885 rdl->pager_len += len;
889 /* Print data asynchronously (using pager if needed) */
891 rdline_asyncpager_printf(struct rdline *rdl, const char *fmt, ...)
900 buf = malloc(BUFSIZ);
905 n = vsnprintf(buf, BUFSIZ, fmt, ap);
911 rdline_asyncpager_write(rdl, buf, n);
916 int rdline_asyncpager_set_cb(struct rdline *rdl, rdline_asyncpager_cb_t *cb,
919 if (rdl->pager_buf == NULL)
923 rdl->pager_arg = arg;
926 #endif /* !NO_PAGER */