cmdline (merge-intel): don't display control character at the end of line
[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)
168 {
169         unsigned int i;
170         char tmp;
171
172         if (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         rdline_miniprintf(rdl, vt100_multi_left,
180                           CIRBUF_GET_LEN(&rdl->right));
181 }
182
183 void
184 rdline_redisplay(struct rdline *rdl)
185 {
186         unsigned int i;
187         char tmp;
188
189         rdline_puts(rdl, vt100_home);
190         for (i=0 ; i<rdl->prompt_size ; i++)
191                 rdl->write_char(rdl, rdl->prompt[i]);
192         CIRBUF_FOREACH(&rdl->left, i, tmp) {
193                 rdl->write_char(rdl, tmp);
194         }
195         display_right_buffer(rdl);
196 }
197
198 int
199 rdline_char_in(struct rdline *rdl, char c)
200 {
201         unsigned int i;
202         int cmd;
203         char tmp;
204 #ifndef NO_RDLINE_HISTORY
205         char *buf;
206 #endif
207
208         if (rdl->status == RDLINE_EXITED)
209                 return RDLINE_RES_EXITED;
210         if (rdl->status != RDLINE_RUNNING)
211                 return RDLINE_RES_NOT_RUNNING;
212
213         cmd = vt100_parser(&rdl->vt100, c);
214         if (cmd == -2)
215                 return RDLINE_RES_SUCCESS;
216
217         if (cmd >= 0) {
218                 switch (cmd) {
219                 case CMDLINE_KEY_CTRL_B:
220                 case CMDLINE_KEY_LEFT_ARR:
221                         if (CIRBUF_IS_EMPTY(&rdl->left))
222                                 break;
223                         tmp = cirbuf_get_tail(&rdl->left);
224                         cirbuf_del_tail(&rdl->left);
225                         cirbuf_add_head(&rdl->right, tmp);
226                         rdline_puts(rdl, vt100_left_arr);
227                         break;
228
229                 case CMDLINE_KEY_CTRL_F:
230                 case CMDLINE_KEY_RIGHT_ARR:
231                         if (CIRBUF_IS_EMPTY(&rdl->right))
232                                 break;
233                         tmp = cirbuf_get_head(&rdl->right);
234                         cirbuf_del_head(&rdl->right);
235                         cirbuf_add_tail(&rdl->left, tmp);
236                         rdline_puts(rdl, vt100_right_arr);
237                         break;
238
239                 case CMDLINE_KEY_WLEFT:
240                         while (! CIRBUF_IS_EMPTY(&rdl->left) &&
241                                (tmp = cirbuf_get_tail(&rdl->left)) &&
242                                isblank2(tmp)) {
243                                 rdline_puts(rdl, vt100_left_arr);
244                                 cirbuf_del_tail(&rdl->left);
245                                 cirbuf_add_head(&rdl->right, tmp);
246                         }
247                         while (! CIRBUF_IS_EMPTY(&rdl->left) &&
248                                (tmp = cirbuf_get_tail(&rdl->left)) &&
249                                !isblank2(tmp)) {
250                                 rdline_puts(rdl, vt100_left_arr);
251                                 cirbuf_del_tail(&rdl->left);
252                                 cirbuf_add_head(&rdl->right, tmp);
253                         }
254                         break;
255
256                 case CMDLINE_KEY_WRIGHT:
257                         while (! CIRBUF_IS_EMPTY(&rdl->right) &&
258                                (tmp = cirbuf_get_head(&rdl->right)) &&
259                                isblank2(tmp)) {
260                                 rdline_puts(rdl, vt100_right_arr);
261                                 cirbuf_del_head(&rdl->right);
262                                 cirbuf_add_tail(&rdl->left, tmp);
263                         }
264                         while (! CIRBUF_IS_EMPTY(&rdl->right) &&
265                                (tmp = cirbuf_get_head(&rdl->right)) &&
266                                !isblank2(tmp)) {
267                                 rdline_puts(rdl, vt100_right_arr);
268                                 cirbuf_del_head(&rdl->right);
269                                 cirbuf_add_tail(&rdl->left, tmp);
270                         }
271                         break;
272
273                 case CMDLINE_KEY_BKSPACE:
274                         if(!cirbuf_del_tail_safe(&rdl->left)) {
275                                 rdline_puts(rdl, vt100_bs);
276                                 display_right_buffer(rdl);
277                         }
278                         break;
279
280                 case CMDLINE_KEY_META_BKSPACE:
281                         while (! CIRBUF_IS_EMPTY(&rdl->left) && isblank2(cirbuf_get_tail(&rdl->left))) {
282                                 rdline_puts(rdl, vt100_bs);
283                                 cirbuf_del_tail(&rdl->left);
284                         }
285                         while (! CIRBUF_IS_EMPTY(&rdl->left) && !isblank2(cirbuf_get_tail(&rdl->left))) {
286                                 rdline_puts(rdl, vt100_bs);
287                                 cirbuf_del_tail(&rdl->left);
288                         }
289                         display_right_buffer(rdl);
290                         break;
291
292                 case CMDLINE_KEY_SUPPR:
293                 case CMDLINE_KEY_CTRL_D:
294                         if (cmd == CMDLINE_KEY_CTRL_D &&
295                             CIRBUF_IS_EMPTY(&rdl->left) &&
296                             CIRBUF_IS_EMPTY(&rdl->right)) {
297                                 return RDLINE_RES_EOF;
298                         }
299                         if (!cirbuf_del_head_safe(&rdl->right)) {
300                                 display_right_buffer(rdl);
301                         }
302                         break;
303
304                 case CMDLINE_KEY_CTRL_A:
305                         if (CIRBUF_IS_EMPTY(&rdl->left))
306                                 break;
307                         rdline_miniprintf(rdl, vt100_multi_left,
308                                             CIRBUF_GET_LEN(&rdl->left));
309                         while (! CIRBUF_IS_EMPTY(&rdl->left)) {
310                                 tmp = cirbuf_get_tail(&rdl->left);
311                                 cirbuf_del_tail(&rdl->left);
312                                 cirbuf_add_head(&rdl->right, tmp);
313                         }
314                         break;
315
316                 case CMDLINE_KEY_CTRL_E:
317                         if (CIRBUF_IS_EMPTY(&rdl->right))
318                                 break;
319                         rdline_miniprintf(rdl, vt100_multi_right,
320                                             CIRBUF_GET_LEN(&rdl->right));
321                         while (! CIRBUF_IS_EMPTY(&rdl->right)) {
322                                 tmp = cirbuf_get_head(&rdl->right);
323                                 cirbuf_del_head(&rdl->right);
324                                 cirbuf_add_tail(&rdl->left, tmp);
325                         }
326                         break;
327
328 #ifndef NO_RDLINE_KILL_BUF
329                 case CMDLINE_KEY_CTRL_K:
330                         cirbuf_get_buf_head(&rdl->right, rdl->kill_buf, RDLINE_BUF_SIZE);
331                         rdl->kill_size = CIRBUF_GET_LEN(&rdl->right);
332                         cirbuf_del_buf_head(&rdl->right, rdl->kill_size);
333                         rdline_puts(rdl, vt100_clear_right);
334                         break;
335
336                 case CMDLINE_KEY_CTRL_Y:
337                         i=0;
338                         while(CIRBUF_GET_LEN(&rdl->right) + CIRBUF_GET_LEN(&rdl->left) <
339                               RDLINE_BUF_SIZE &&
340                               i < rdl->kill_size) {
341                                 cirbuf_add_tail(&rdl->left, rdl->kill_buf[i]);
342                                 rdl->write_char(rdl, rdl->kill_buf[i]);
343                                 i++;
344                         }
345                         display_right_buffer(rdl);
346                         break;
347 #endif /* !NO_RDLINE_KILL_BUF */
348
349                 case CMDLINE_KEY_CTRL_C:
350                         rdline_puts(rdl, "\r\n");
351                         rdline_newline(rdl, rdl->prompt);
352                         break;
353
354                 case CMDLINE_KEY_CTRL_L:
355                         rdline_redisplay(rdl);
356                         break;
357
358                 case CMDLINE_KEY_TAB:
359                 case CMDLINE_KEY_HELP:
360                         cirbuf_align_left(&rdl->left);
361                         rdl->left_buf[CIRBUF_GET_LEN(&rdl->left)] = '\0';
362                         if (rdl->complete) {
363                                 char tmp_buf[BUFSIZ];
364                                 int complete_state;
365                                 int ret;
366                                 unsigned int tmp_size;
367
368                                 if (cmd == CMDLINE_KEY_TAB)
369                                         complete_state = 0;
370                                 else
371                                         complete_state = -1;
372
373                                 /* see in parse.h for help on complete() */
374                                 ret = rdl->complete(rdl, rdl->left_buf,
375                                                     tmp_buf, sizeof(tmp_buf),
376                                                     &complete_state);
377                                 /* no completion or error */
378                                 if (ret <= 0) {
379                                         return RDLINE_RES_COMPLETE;
380                                 }
381
382                                 tmp_size = strlen(tmp_buf);
383                                 /* add chars */
384                                 if (ret == RDLINE_RES_COMPLETE) {
385                                         i=0;
386                                         while(CIRBUF_GET_LEN(&rdl->right) + CIRBUF_GET_LEN(&rdl->left) <
387                                               RDLINE_BUF_SIZE &&
388                                               i < tmp_size) {
389                                                 cirbuf_add_tail(&rdl->left, tmp_buf[i]);
390                                                 rdl->write_char(rdl, tmp_buf[i]);
391                                                 i++;
392                                         }
393                                         display_right_buffer(rdl);
394                                         return RDLINE_RES_COMPLETE; /* ?? */
395                                 }
396
397                                 /* choice */
398                                 rdline_puts(rdl, "\r\n");
399                                 while (ret) {
400                                         rdl->write_char(rdl, ' ');
401                                         for (i=0 ; tmp_buf[i] ; i++)
402                                                 rdl->write_char(rdl, tmp_buf[i]);
403                                         rdline_puts(rdl, "\r\n");
404                                         ret = rdl->complete(rdl, rdl->left_buf,
405                                                             tmp_buf, sizeof(tmp_buf),
406                                                             &complete_state);
407                                 }
408
409                                 rdline_redisplay(rdl);
410                         }
411                         return RDLINE_RES_COMPLETE;
412
413                 case CMDLINE_KEY_RETURN:
414                 case CMDLINE_KEY_RETURN2:
415                         rdline_get_buffer(rdl);
416                         rdl->status = RDLINE_INIT;
417                         rdline_puts(rdl, "\r\n");
418 #ifndef NO_RDLINE_HISTORY
419                         if (rdl->history_cur_line != -1)
420                                 rdline_remove_first_history_item(rdl);
421 #endif
422
423                         if (rdl->validate)
424                                 rdl->validate(rdl, rdl->left_buf, CIRBUF_GET_LEN(&rdl->left)+2);
425                         /* user may have stopped rdline */
426                         if (rdl->status == RDLINE_EXITED)
427                                 return RDLINE_RES_EXITED;
428                         return RDLINE_RES_VALIDATED;
429
430 #ifndef NO_RDLINE_HISTORY
431                 case CMDLINE_KEY_UP_ARR:
432                         if (rdl->history_cur_line == 0) {
433                                 rdline_remove_first_history_item(rdl);
434                         }
435                         if (rdl->history_cur_line <= 0) {
436                                 rdline_add_history(rdl, rdline_get_buffer(rdl));
437                                 rdl->history_cur_line = 0;
438                         }
439
440                         buf = rdline_get_history_item(rdl, rdl->history_cur_line + 1);
441                         if (!buf)
442                                 break;
443
444                         rdl->history_cur_line ++;
445                         vt100_init(&rdl->vt100);
446                         cirbuf_init(&rdl->left, rdl->left_buf, 0, RDLINE_BUF_SIZE);
447                         cirbuf_init(&rdl->right, rdl->right_buf, 0, RDLINE_BUF_SIZE);
448                         cirbuf_add_buf_tail(&rdl->left, buf, strlen(buf));
449                         rdline_redisplay(rdl);
450                         break;
451
452                 case CMDLINE_KEY_DOWN_ARR:
453                         if (rdl->history_cur_line - 1 < 0)
454                                 break;
455
456                         rdl->history_cur_line --;
457                         buf = rdline_get_history_item(rdl, rdl->history_cur_line);
458                         if (!buf)
459                                 break;
460                         vt100_init(&rdl->vt100);
461                         cirbuf_init(&rdl->left, rdl->left_buf, 0, RDLINE_BUF_SIZE);
462                         cirbuf_init(&rdl->right, rdl->right_buf, 0, RDLINE_BUF_SIZE);
463                         cirbuf_add_buf_tail(&rdl->left, buf, strlen(buf));
464                         rdline_redisplay(rdl);
465
466                         break;
467 #endif /* !NO_RDLINE_HISTORY */
468
469
470                 default:
471                         break;
472                 }
473
474                 return RDLINE_RES_SUCCESS;
475         }
476
477         if (!isprint((int)c))
478                 return RDLINE_RES_SUCCESS;
479
480         /* standard chars */
481         if (CIRBUF_GET_LEN(&rdl->left) + CIRBUF_GET_LEN(&rdl->right) >= RDLINE_BUF_SIZE)
482                 return RDLINE_RES_SUCCESS;
483
484         if (cirbuf_add_tail_safe(&rdl->left, c))
485                 return RDLINE_RES_SUCCESS;
486
487         rdl->write_char(rdl, c);
488         display_right_buffer(rdl);
489
490         return RDLINE_RES_SUCCESS;
491 }
492
493
494 /* HISTORY */
495
496 #ifndef NO_RDLINE_HISTORY
497 static void
498 rdline_remove_old_history_item(struct rdline * rdl)
499 {
500         char tmp;
501
502         while (! CIRBUF_IS_EMPTY(&rdl->history) ) {
503                 tmp = cirbuf_get_head(&rdl->history);
504                 cirbuf_del_head(&rdl->history);
505                 if (!tmp)
506                         break;
507         }
508 }
509
510 static void
511 rdline_remove_first_history_item(struct rdline * rdl)
512 {
513         char tmp;
514
515         if ( CIRBUF_IS_EMPTY(&rdl->history) ) {
516                 return;
517         }
518         else {
519                 cirbuf_del_tail(&rdl->history);
520         }
521
522         while (! CIRBUF_IS_EMPTY(&rdl->history) ) {
523                 tmp = cirbuf_get_tail(&rdl->history);
524                 if (!tmp)
525                         break;
526                 cirbuf_del_tail(&rdl->history);
527         }
528 }
529
530 static unsigned int
531 rdline_get_history_size(struct rdline * rdl)
532 {
533         unsigned int i, tmp, ret=0;
534
535         CIRBUF_FOREACH(&rdl->history, i, tmp) {
536                 if (tmp == 0)
537                         ret ++;
538         }
539
540         return ret;
541 }
542
543 char *
544 rdline_get_history_item(struct rdline * rdl, unsigned int idx)
545 {
546         unsigned int len, i, tmp;
547
548         len = rdline_get_history_size(rdl);
549         if ( idx >= len ) {
550                 return NULL;
551         }
552
553         cirbuf_align_left(&rdl->history);
554
555         CIRBUF_FOREACH(&rdl->history, i, tmp) {
556                 if ( idx == len - 1) {
557                         return rdl->history_buf + i;
558                 }
559                 if (tmp == 0)
560                         len --;
561         }
562
563         return NULL;
564 }
565
566 int
567 rdline_add_history(struct rdline * rdl, const char * buf)
568 {
569         unsigned int len, i;
570
571         len = strlen(buf);
572         for (i=0; i<len ; i++) {
573                 if (buf[i] == '\n') {
574                         len = i;
575                         break;
576                 }
577         }
578
579         if ( len >= RDLINE_HISTORY_BUF_SIZE )
580                 return -1;
581
582         while ( len >= CIRBUF_GET_FREELEN(&rdl->history) ) {
583                 rdline_remove_old_history_item(rdl);
584         }
585
586         cirbuf_add_buf_tail(&rdl->history, buf, len);
587         cirbuf_add_tail(&rdl->history, 0);
588
589         return 0;
590 }
591
592 void
593 rdline_clear_history(struct rdline * rdl)
594 {
595         cirbuf_init(&rdl->history, rdl->history_buf, 0, RDLINE_HISTORY_BUF_SIZE);
596 }
597
598 #else /* !NO_RDLINE_HISTORY */
599
600 int rdline_add_history(struct rdline * rdl, const char * buf) {return -1;}
601 void rdline_clear_history(struct rdline * rdl) {}
602 char * rdline_get_history_item(struct rdline * rdl, unsigned int i) {return NULL;}
603
604
605 #endif /* !NO_RDLINE_HISTORY */
606
607
608 /* STATIC USEFUL FUNCS */
609
610 static void
611 rdline_puts(struct rdline * rdl, const char * buf)
612 {
613         char c;
614         while ( (c = *(buf++)) != '\0' ) {
615                 rdl->write_char(rdl, c);
616         }
617 }
618
619 /* a very very basic printf with one arg and one format 'u' */
620 static void
621 rdline_miniprintf(struct rdline *rdl, const char * buf, unsigned int val)
622 {
623         char c, started=0, div=100;
624
625         while ( (c=*(buf++)) ) {
626                 if (c != '%') {
627                         rdl->write_char(rdl, c);
628                         continue;
629                 }
630                 c = *(buf++);
631                 if (c != 'u') {
632                         rdl->write_char(rdl, '%');
633                         rdl->write_char(rdl, c);
634                         continue;
635                 }
636                 /* val is never more than 255 */
637                 while (div) {
638                         c = (char)(val / div);
639                         if (c || started) {
640                                 rdl->write_char(rdl, (char)(c+'0'));
641                                 started = 1;
642                         }
643                         val %= div;
644                         div /= 10;
645                 }
646         }
647 }
648