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