38b0773393ea4329e84ac56f87f639f8fff2c20c
[libcmdline.git] / src / lib / cmdline_rdline.c
1 /*-
2  * Copyright (c) <2010>, Intel Corporation
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * - Redistributions of source code must retain the above copyright
10  *   notice, this list of conditions and the following disclaimer.
11  *
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
15  *   distribution.
16  *
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.
20  *
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.
33  */
34
35 /*
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:
40  *
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.
49  *
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.
60  */
61
62 #include <stdlib.h>
63 #include <stdio.h>
64 #include <stdint.h>
65 #include <string.h>
66 #include <stdarg.h>
67 #include <ctype.h>
68
69 #include "cmdline_cirbuf.h"
70 #include "cmdline_rdline.h"
71
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);
75
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 */
81
82
83 /* isblank() needs _XOPEN_SOURCE >= 600 || _ISOC99_SOURCE, so use our
84  * own. */
85 static int
86 isblank2(char c)
87 {
88         if (c == ' ' ||
89             c == '\t' )
90                 return 1;
91         return 0;
92 }
93
94 void
95 rdline_init(struct rdline *rdl,
96                  rdline_write_char_t *write_char,
97                  rdline_validate_t *validate,
98                  rdline_complete_t *complete)
99 {
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 */
108 }
109
110 void
111 rdline_newline(struct rdline *rdl, const char *prompt)
112 {
113         unsigned int i;
114
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);
118
119         if (prompt != rdl->prompt)
120                 memcpy(rdl->prompt, prompt, sizeof(rdl->prompt)-1);
121         rdl->prompt_size = strlen(prompt);
122
123         for (i=0 ; i<rdl->prompt_size ; i++)
124                 rdl->write_char(rdl, rdl->prompt[i]);
125         rdl->status = RDLINE_RUNNING;
126
127 #ifndef NO_RDLINE_HISTORY
128         rdl->history_cur_line = -1;
129 #endif /* !NO_RDLINE_HISTORY */
130 }
131
132 void
133 rdline_stop(struct rdline *rdl)
134 {
135         rdl->status = RDLINE_INIT;
136 }
137
138 void
139 rdline_quit(struct rdline *rdl)
140 {
141         rdl->status = RDLINE_EXITED;
142 }
143
144 void
145 rdline_restart(struct rdline *rdl)
146 {
147         rdl->status = RDLINE_RUNNING;
148 }
149
150 const char *
151 rdline_get_buffer(struct rdline *rdl)
152 {
153         unsigned int len_l, len_r;
154         cirbuf_align_left(&rdl->left);
155         cirbuf_align_left(&rdl->right);
156
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);
160
161         rdl->left_buf[len_l + len_r] = '\n';
162         rdl->left_buf[len_l + len_r + 1] = '\0';
163         return rdl->left_buf;
164 }
165
166 static void
167 display_right_buffer(struct rdline *rdl, int force)
168 {
169         unsigned int i;
170         char tmp;
171
172         if (!force && CIRBUF_IS_EMPTY(&rdl->right))
173                 return;
174
175         rdline_puts(rdl, vt100_clear_right);
176         CIRBUF_FOREACH(&rdl->right, i, tmp) {
177                 rdl->write_char(rdl, tmp);
178         }
179         if (!CIRBUF_IS_EMPTY(&rdl->right))
180                 rdline_miniprintf(rdl, vt100_multi_left,
181                                   CIRBUF_GET_LEN(&rdl->right));
182 }
183
184 void
185 rdline_redisplay(struct rdline *rdl)
186 {
187         unsigned int i;
188         char tmp;
189
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);
195         }
196         display_right_buffer(rdl, 1);
197 }
198
199 int
200 rdline_char_in(struct rdline *rdl, char c)
201 {
202         unsigned int i;
203         int cmd;
204         char tmp;
205 #ifndef NO_RDLINE_HISTORY
206         char *buf;
207 #endif
208
209         if (rdl->status == RDLINE_EXITED)
210                 return RDLINE_RES_EXITED;
211         if (rdl->status != RDLINE_RUNNING)
212                 return RDLINE_RES_NOT_RUNNING;
213
214         cmd = vt100_parser(&rdl->vt100, c);
215         if (cmd == -2)
216                 return RDLINE_RES_SUCCESS;
217
218         if (cmd >= 0) {
219                 switch (cmd) {
220                 case CMDLINE_KEY_CTRL_B:
221                 case CMDLINE_KEY_LEFT_ARR:
222                         if (CIRBUF_IS_EMPTY(&rdl->left))
223                                 break;
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);
228                         break;
229
230                 case CMDLINE_KEY_CTRL_F:
231                 case CMDLINE_KEY_RIGHT_ARR:
232                         if (CIRBUF_IS_EMPTY(&rdl->right))
233                                 break;
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);
238                         break;
239
240                 case CMDLINE_KEY_WLEFT:
241                         while (! CIRBUF_IS_EMPTY(&rdl->left) &&
242                                (tmp = cirbuf_get_tail(&rdl->left)) &&
243                                isblank2(tmp)) {
244                                 rdline_puts(rdl, vt100_left_arr);
245                                 cirbuf_del_tail(&rdl->left);
246                                 cirbuf_add_head(&rdl->right, tmp);
247                         }
248                         while (! CIRBUF_IS_EMPTY(&rdl->left) &&
249                                (tmp = cirbuf_get_tail(&rdl->left)) &&
250                                !isblank2(tmp)) {
251                                 rdline_puts(rdl, vt100_left_arr);
252                                 cirbuf_del_tail(&rdl->left);
253                                 cirbuf_add_head(&rdl->right, tmp);
254                         }
255                         break;
256
257                 case CMDLINE_KEY_WRIGHT:
258                         while (! CIRBUF_IS_EMPTY(&rdl->right) &&
259                                (tmp = cirbuf_get_head(&rdl->right)) &&
260                                isblank2(tmp)) {
261                                 rdline_puts(rdl, vt100_right_arr);
262                                 cirbuf_del_head(&rdl->right);
263                                 cirbuf_add_tail(&rdl->left, tmp);
264                         }
265                         while (! CIRBUF_IS_EMPTY(&rdl->right) &&
266                                (tmp = cirbuf_get_head(&rdl->right)) &&
267                                !isblank2(tmp)) {
268                                 rdline_puts(rdl, vt100_right_arr);
269                                 cirbuf_del_head(&rdl->right);
270                                 cirbuf_add_tail(&rdl->left, tmp);
271                         }
272                         break;
273
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);
278                         }
279                         break;
280
281                 case CMDLINE_KEY_META_BKSPACE:
282                         while (! CIRBUF_IS_EMPTY(&rdl->left) && isblank2(cirbuf_get_tail(&rdl->left))) {
283                                 rdline_puts(rdl, vt100_bs);
284                                 cirbuf_del_tail(&rdl->left);
285                         }
286                         while (! CIRBUF_IS_EMPTY(&rdl->left) && !isblank2(cirbuf_get_tail(&rdl->left))) {
287                                 rdline_puts(rdl, vt100_bs);
288                                 cirbuf_del_tail(&rdl->left);
289                         }
290                         display_right_buffer(rdl, 1);
291                         break;
292
293                 case CMDLINE_KEY_SUPPR:
294                 case CMDLINE_KEY_CTRL_D:
295                         if (cmd == CMDLINE_KEY_CTRL_D &&
296                             CIRBUF_IS_EMPTY(&rdl->left) &&
297                             CIRBUF_IS_EMPTY(&rdl->right)) {
298                                 return RDLINE_RES_EOF;
299                         }
300                         if (!cirbuf_del_head_safe(&rdl->right)) {
301                                 display_right_buffer(rdl, 1);
302                         }
303                         break;
304
305                 case CMDLINE_KEY_CTRL_A:
306                         if (CIRBUF_IS_EMPTY(&rdl->left))
307                                 break;
308                         rdline_miniprintf(rdl, vt100_multi_left,
309                                             CIRBUF_GET_LEN(&rdl->left));
310                         while (! CIRBUF_IS_EMPTY(&rdl->left)) {
311                                 tmp = cirbuf_get_tail(&rdl->left);
312                                 cirbuf_del_tail(&rdl->left);
313                                 cirbuf_add_head(&rdl->right, tmp);
314                         }
315                         break;
316
317                 case CMDLINE_KEY_CTRL_E:
318                         if (CIRBUF_IS_EMPTY(&rdl->right))
319                                 break;
320                         rdline_miniprintf(rdl, vt100_multi_right,
321                                             CIRBUF_GET_LEN(&rdl->right));
322                         while (! CIRBUF_IS_EMPTY(&rdl->right)) {
323                                 tmp = cirbuf_get_head(&rdl->right);
324                                 cirbuf_del_head(&rdl->right);
325                                 cirbuf_add_tail(&rdl->left, tmp);
326                         }
327                         break;
328
329 #ifndef NO_RDLINE_KILL_BUF
330                 case CMDLINE_KEY_CTRL_K:
331                         cirbuf_get_buf_head(&rdl->right, rdl->kill_buf, RDLINE_BUF_SIZE);
332                         rdl->kill_size = CIRBUF_GET_LEN(&rdl->right);
333                         cirbuf_del_buf_head(&rdl->right, rdl->kill_size);
334                         rdline_puts(rdl, vt100_clear_right);
335                         break;
336
337                 case CMDLINE_KEY_CTRL_Y:
338                         i=0;
339                         while(CIRBUF_GET_LEN(&rdl->right) + CIRBUF_GET_LEN(&rdl->left) <
340                               RDLINE_BUF_SIZE &&
341                               i < rdl->kill_size) {
342                                 cirbuf_add_tail(&rdl->left, rdl->kill_buf[i]);
343                                 rdl->write_char(rdl, rdl->kill_buf[i]);
344                                 i++;
345                         }
346                         display_right_buffer(rdl, 0);
347                         break;
348 #endif /* !NO_RDLINE_KILL_BUF */
349
350                 case CMDLINE_KEY_CTRL_C:
351                         rdline_puts(rdl, "\r\n");
352                         rdline_newline(rdl, rdl->prompt);
353                         break;
354
355                 case CMDLINE_KEY_CTRL_L:
356                         rdline_redisplay(rdl);
357                         break;
358
359                 case CMDLINE_KEY_TAB:
360                 case CMDLINE_KEY_HELP:
361                         cirbuf_align_left(&rdl->left);
362                         rdl->left_buf[CIRBUF_GET_LEN(&rdl->left)] = '\0';
363                         if (rdl->complete) {
364                                 char tmp_buf[BUFSIZ];
365                                 int complete_state;
366                                 int ret;
367                                 unsigned int tmp_size;
368
369                                 if (cmd == CMDLINE_KEY_TAB)
370                                         complete_state = 0;
371                                 else
372                                         complete_state = -1;
373
374                                 /* see in parse.h for help on complete() */
375                                 ret = rdl->complete(rdl, rdl->left_buf,
376                                                     tmp_buf, sizeof(tmp_buf),
377                                                     &complete_state);
378                                 /* no completion or error */
379                                 if (ret <= 0) {
380                                         return RDLINE_RES_COMPLETE;
381                                 }
382
383                                 tmp_size = strlen(tmp_buf);
384                                 /* add chars */
385                                 if (ret == RDLINE_RES_COMPLETE) {
386                                         i=0;
387                                         while(CIRBUF_GET_LEN(&rdl->right) + CIRBUF_GET_LEN(&rdl->left) <
388                                               RDLINE_BUF_SIZE &&
389                                               i < tmp_size) {
390                                                 cirbuf_add_tail(&rdl->left, tmp_buf[i]);
391                                                 rdl->write_char(rdl, tmp_buf[i]);
392                                                 i++;
393                                         }
394                                         display_right_buffer(rdl, 1);
395                                         return RDLINE_RES_COMPLETE; /* ?? */
396                                 }
397
398                                 /* choice */
399                                 rdline_puts(rdl, "\r\n");
400                                 while (ret) {
401                                         rdl->write_char(rdl, ' ');
402                                         for (i=0 ; tmp_buf[i] ; i++)
403                                                 rdl->write_char(rdl, tmp_buf[i]);
404                                         rdline_puts(rdl, "\r\n");
405                                         ret = rdl->complete(rdl, rdl->left_buf,
406                                                             tmp_buf, sizeof(tmp_buf),
407                                                             &complete_state);
408                                 }
409
410                                 rdline_redisplay(rdl);
411                         }
412                         return RDLINE_RES_COMPLETE;
413
414                 case CMDLINE_KEY_RETURN:
415                 case CMDLINE_KEY_RETURN2:
416                         rdline_get_buffer(rdl);
417                         rdl->status = RDLINE_INIT;
418                         rdline_puts(rdl, "\r\n");
419 #ifndef NO_RDLINE_HISTORY
420                         if (rdl->history_cur_line != -1)
421                                 rdline_remove_first_history_item(rdl);
422 #endif
423
424                         if (rdl->validate)
425                                 rdl->validate(rdl, rdl->left_buf, CIRBUF_GET_LEN(&rdl->left)+2);
426                         /* user may have stopped rdline */
427                         if (rdl->status == RDLINE_EXITED)
428                                 return RDLINE_RES_EXITED;
429                         return RDLINE_RES_VALIDATED;
430
431 #ifndef NO_RDLINE_HISTORY
432                 case CMDLINE_KEY_UP_ARR:
433                         if (rdl->history_cur_line == 0) {
434                                 rdline_remove_first_history_item(rdl);
435                         }
436                         if (rdl->history_cur_line <= 0) {
437                                 rdline_add_history(rdl, rdline_get_buffer(rdl));
438                                 rdl->history_cur_line = 0;
439                         }
440
441                         buf = rdline_get_history_item(rdl, rdl->history_cur_line + 1);
442                         if (!buf)
443                                 break;
444
445                         rdl->history_cur_line ++;
446                         vt100_init(&rdl->vt100);
447                         cirbuf_init(&rdl->left, rdl->left_buf, 0, RDLINE_BUF_SIZE);
448                         cirbuf_init(&rdl->right, rdl->right_buf, 0, RDLINE_BUF_SIZE);
449                         cirbuf_add_buf_tail(&rdl->left, buf, strlen(buf));
450                         rdline_redisplay(rdl);
451                         break;
452
453                 case CMDLINE_KEY_DOWN_ARR:
454                         if (rdl->history_cur_line - 1 < 0)
455                                 break;
456
457                         rdl->history_cur_line --;
458                         buf = rdline_get_history_item(rdl, rdl->history_cur_line);
459                         if (!buf)
460                                 break;
461                         vt100_init(&rdl->vt100);
462                         cirbuf_init(&rdl->left, rdl->left_buf, 0, RDLINE_BUF_SIZE);
463                         cirbuf_init(&rdl->right, rdl->right_buf, 0, RDLINE_BUF_SIZE);
464                         cirbuf_add_buf_tail(&rdl->left, buf, strlen(buf));
465                         rdline_redisplay(rdl);
466
467                         break;
468 #endif /* !NO_RDLINE_HISTORY */
469
470
471                 default:
472                         break;
473                 }
474
475                 return RDLINE_RES_SUCCESS;
476         }
477
478         if (!isprint((int)c))
479                 return RDLINE_RES_SUCCESS;
480
481         /* standard chars */
482         if (CIRBUF_GET_LEN(&rdl->left) + CIRBUF_GET_LEN(&rdl->right) >= RDLINE_BUF_SIZE)
483                 return RDLINE_RES_SUCCESS;
484
485         if (cirbuf_add_tail_safe(&rdl->left, c))
486                 return RDLINE_RES_SUCCESS;
487
488         rdl->write_char(rdl, c);
489         display_right_buffer(rdl, 0);
490
491         return RDLINE_RES_SUCCESS;
492 }
493
494
495 /* HISTORY */
496
497 #ifndef NO_RDLINE_HISTORY
498 static void
499 rdline_remove_old_history_item(struct rdline * rdl)
500 {
501         char tmp;
502
503         while (! CIRBUF_IS_EMPTY(&rdl->history) ) {
504                 tmp = cirbuf_get_head(&rdl->history);
505                 cirbuf_del_head(&rdl->history);
506                 if (!tmp)
507                         break;
508         }
509 }
510
511 static void
512 rdline_remove_first_history_item(struct rdline * rdl)
513 {
514         char tmp;
515
516         if ( CIRBUF_IS_EMPTY(&rdl->history) ) {
517                 return;
518         }
519         else {
520                 cirbuf_del_tail(&rdl->history);
521         }
522
523         while (! CIRBUF_IS_EMPTY(&rdl->history) ) {
524                 tmp = cirbuf_get_tail(&rdl->history);
525                 if (!tmp)
526                         break;
527                 cirbuf_del_tail(&rdl->history);
528         }
529 }
530
531 static unsigned int
532 rdline_get_history_size(struct rdline * rdl)
533 {
534         unsigned int i, tmp, ret=0;
535
536         CIRBUF_FOREACH(&rdl->history, i, tmp) {
537                 if (tmp == 0)
538                         ret ++;
539         }
540
541         return ret;
542 }
543
544 char *
545 rdline_get_history_item(struct rdline * rdl, unsigned int idx)
546 {
547         unsigned int len, i, tmp;
548
549         len = rdline_get_history_size(rdl);
550         if ( idx >= len ) {
551                 return NULL;
552         }
553
554         cirbuf_align_left(&rdl->history);
555
556         CIRBUF_FOREACH(&rdl->history, i, tmp) {
557                 if ( idx == len - 1) {
558                         return rdl->history_buf + i;
559                 }
560                 if (tmp == 0)
561                         len --;
562         }
563
564         return NULL;
565 }
566
567 int
568 rdline_add_history(struct rdline * rdl, const char * buf)
569 {
570         unsigned int len, i;
571
572         len = strlen(buf);
573         for (i=0; i<len ; i++) {
574                 if (buf[i] == '\n') {
575                         len = i;
576                         break;
577                 }
578         }
579
580         if ( len >= RDLINE_HISTORY_BUF_SIZE )
581                 return -1;
582
583         while ( len >= CIRBUF_GET_FREELEN(&rdl->history) ) {
584                 rdline_remove_old_history_item(rdl);
585         }
586
587         cirbuf_add_buf_tail(&rdl->history, buf, len);
588         cirbuf_add_tail(&rdl->history, 0);
589
590         return 0;
591 }
592
593 void
594 rdline_clear_history(struct rdline * rdl)
595 {
596         cirbuf_init(&rdl->history, rdl->history_buf, 0, RDLINE_HISTORY_BUF_SIZE);
597 }
598
599 #else /* !NO_RDLINE_HISTORY */
600
601 int rdline_add_history(struct rdline * rdl, const char * buf) {return -1;}
602 void rdline_clear_history(struct rdline * rdl) {}
603 char * rdline_get_history_item(struct rdline * rdl, unsigned int i) {return NULL;}
604
605
606 #endif /* !NO_RDLINE_HISTORY */
607
608
609 /* STATIC USEFUL FUNCS */
610
611 static void
612 rdline_puts(struct rdline * rdl, const char * buf)
613 {
614         char c;
615         while ( (c = *(buf++)) != '\0' ) {
616                 rdl->write_char(rdl, c);
617         }
618 }
619
620 /* a very very basic printf with one arg and one format 'u' */
621 static void
622 rdline_miniprintf(struct rdline *rdl, const char * buf, unsigned int val)
623 {
624         char c, started=0, div=100;
625
626         while ( (c=*(buf++)) ) {
627                 if (c != '%') {
628                         rdl->write_char(rdl, c);
629                         continue;
630                 }
631                 c = *(buf++);
632                 if (c != 'u') {
633                         rdl->write_char(rdl, '%');
634                         rdl->write_char(rdl, c);
635                         continue;
636                 }
637                 /* val is never more than 255 */
638                 while (div) {
639                         c = (char)(val / div);
640                         if (c || started) {
641                                 rdl->write_char(rdl, (char)(c+'0'));
642                                 started = 1;
643                         }
644                         val %= div;
645                         div /= 10;
646                 }
647         }
648 }
649