Makefile support for sparc-ros-gcc
[akaros.git] / user / apps / parlib / readline.c
1 #include <unistd.h>
2 #include <stdio.h>
3 #include <fcntl.h>
4 #include <arch/kbdreg.h>
5
6 char* readline(const char * prompt)
7 {
8         int i, read_amt;
9         char c;
10         #define BUFLEN 256
11         static char buf[BUFLEN];
12
13         if (prompt != NULL)
14                 printf("%s", prompt); fflush(stdout);
15
16         i = 0;
17         while (1) {
18                 read_amt = read(STDIN_FILENO, &c, 1);
19                 if (read_amt < 0) {
20                         printf("read error: %d\n",read_amt);
21                         return NULL;
22                 } else if (c >= ' ' && i < BUFLEN-1) {
23                         write(STDOUT_FILENO, &c, 1);
24                         buf[i++] = c;
25                 } else if (c == '\b' && i > 0) {
26                         write(STDOUT_FILENO, &c, 1);
27                         i--;
28                 } else if (c == '\n' || c == '\r') {
29                         write(STDOUT_FILENO, &c, 1);
30                         buf[i] = '\0';
31                         return buf;
32                 }
33         }
34 }
35