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