jos/lib/readline.c
Anish Athalye 1a83673424 Lab 1
2018-08-30 15:17:20 -04:00

39 lines
627 B
C

#include <inc/stdio.h>
#include <inc/error.h>
#define BUFLEN 1024
static char buf[BUFLEN];
char *
readline(const char *prompt)
{
int i, c, echoing;
if (prompt != NULL)
cprintf("%s", prompt);
i = 0;
echoing = iscons(0);
while (1) {
c = getchar();
if (c < 0) {
cprintf("read error: %e\n", c);
return NULL;
} else if ((c == '\b' || c == '\x7f') && i > 0) {
if (echoing)
cputchar('\b');
i--;
} else if (c >= ' ' && i < BUFLEN-1) {
if (echoing)
cputchar(c);
buf[i++] = c;
} else if (c == '\n' || c == '\r') {
if (echoing)
cputchar('\n');
buf[i] = 0;
return buf;
}
}
}