96b00e895446b25cbdd88bf2eb12f416966cc00f
[akaros.git] / kern / src / readline.c
1 #ifdef __DEPUTY__
2 #pragma nodeputy
3 #endif
4
5 #include <stdio.h>
6 #include <error.h>
7
8 #define BUFLEN 1024
9 static char buf[BUFLEN];
10
11 char *
12 readline(const char *prompt)
13 {
14         int i, c, echoing;
15
16         if (prompt != NULL)
17                 cprintf("%s", prompt);
18
19         i = 0;
20         echoing = iscons(0);
21         while (1) {
22                 c = getchar();
23                 if (c < 0) {
24                         cprintf("read error: %e\n", c);
25                         return NULL;
26                 } else if (c >= ' ' && i < BUFLEN-1) {
27                         if (echoing)
28                                 cputchar(c);
29                         buf[i++] = c;
30                 } else if (c == '\b' && i > 0) {
31                         if (echoing)
32                                 cputchar(c);
33                         i--;
34                 } else if (c == '\n' || c == '\r') {
35                         if (echoing)
36                                 cputchar(c);
37                         buf[i] = 0;
38                         return buf;
39                 }
40         }
41 }
42