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