Catch negative FDs
[akaros.git] / kern / src / readline.c
1 #ifdef __SHARC__
2 #pragma nosharc
3 #endif
4
5 #include <error.h>
6 #include <stdio.h>
7
8 #define BUFLEN 1024
9 // zra: used only by monitor.c.
10 static char RACY (RO NT buf)[BUFLEN];
11
12 char *
13 readline(const char *prompt, ...)
14 {
15         int i, c, echoing;
16         va_list ap;
17
18         va_start(ap, prompt);
19         if (prompt != NULL)
20                 vcprintf(prompt, ap);
21         va_end(ap);
22
23         i = 0;
24         echoing = iscons(0);
25         while (1) {
26                 c = getchar();
27                 if (c < 0) {
28                         cprintf("read error: %e\n", c);
29                         return NULL;
30                 } else if (c >= ' ' && i < BUFLEN-1) {
31                         if (echoing)
32                                 cputchar(c);
33                         buf[i++] = c;
34                 } else if (c == '\b' && i > 0) {
35                         if (echoing)
36                                 cputchar(c);
37                         i--;
38                 } else if (c == '\n' || c == '\r') {
39                         if (echoing)
40                                 cputchar(c);
41                         buf[i] = 0;
42                         return buf;
43                 }
44         }
45 }
46