From 2e28873f23d2cf073fdddfecdc278b0f44b2789b Mon Sep 17 00:00:00 2001 From: Alex AUVOLAT Date: Sun, 20 May 2012 20:20:39 +0200 Subject: Corrected some bugs in readline && multitasking improvement. --- src/user/lib/libc/std/readline.c | 107 ++++++++++++++++++++++++--------------- 1 file changed, 66 insertions(+), 41 deletions(-) (limited to 'src/user') diff --git a/src/user/lib/libc/std/readline.c b/src/user/lib/libc/std/readline.c index ab13189..5cd9119 100644 --- a/src/user/lib/libc/std/readline.c +++ b/src/user/lib/libc/std/readline.c @@ -81,50 +81,57 @@ char *freadline(FILE f, readline_history *h) { int l = read(f, 0, 16, buf); if (l < 0) return 0; - if (buf[0] == 27) { - if (buf[1] == '[') { - if (buf[2] == 'A') { // up - if (lid > 0) { - lid--; - str = h->str[lid]; - len = strlen(str); - cur = str + len; + char *in = buf; + while (in < buf + l) { + if (*in == 27) { + in++; + if (*in == '[') { + in++; + if (*in == 'A') { // up + if (lid > 0) { + lid--; + str = h->str[lid]; + len = strlen(str); + cur = str + len; + } + } else if (*in == 'B') { // down + if (lid < h->n - 1) { + lid++; + str = h->str[lid]; + len = strlen(str); + cur = str + len; + } + } else if (*in == 'C') { // forward (right) + if (cur < str + len) cur++; + } else if (*in == 'D') { // back (left) + if (cur > str) cur--; } - } else if (buf[2] == 'B') { // down - if (lid < h->n - 1) { - lid++; - str = h->str[lid]; - len = strlen(str); - cur = str + len; - } - } else if (buf[2] == 'C') { // forward (right) - if (cur < str + len) cur++; - } else if (buf[2] == 'D') { // back (left) - if (cur > str) cur--; + } else { + // ALT sequence - ignore + in++; + } + } else if (*in == '\n') { + finished = 1; + cur = str + len; + } else if (*in == '\b') { + if (cur > str) { + cur--; + len--; + char* c; + for (c = cur; *c != 0; c++) c[0] = c[1]; + } + } else if (*in == '\t') { + // ignore tabs -- todo: tab completion???? haha + } else if (*in >= ' ') { + if (l < 255) { + char* c; + for (c = cur + len; c >= cur; c--) c[1] = c[0]; + cur[0] = *in; + cur++; + len++; } - } else { - // ALT sequence - ignore - } - } else if (buf[0] == '\n') { - finished = 1; - cur = str + len; - } else if (buf[0] == '\b') { - if (cur > str) { - cur--; - len--; - char* c; - for (c = cur; *c != 0; c++) c[0] = c[1]; - } - } else if (buf[0] == '\t') { - // ignore tabs -- todo: tab completion???? haha - } else if (buf[0] >= ' ') { - if (l < 255) { - char* c; - for (c = cur + len; c >= cur; c--) c[1] = c[0]; - cur[0] = buf[0]; - cur++; - len++; } + in++; } // ASSUMPTION : everything will fit on one line... @@ -136,7 +143,25 @@ char *freadline(FILE f, readline_history *h) { if (finished) { fprintf(f, "\n"); + + if (h->str[h->n-1] != str) h->n--; + + i = h->n - 1; + while (h->str[i] != str) { + if (i == 0) { + fprint(f, "READLINE FAIL"); + return 0; + } + i--; + } + while (i < h->n - 1) { + h->str[i] = h->str[i+1]; + i++; + } + h->str[i] = str; + if (h->str[h->n-1][0] == 0) h->n--; + return str; } } -- cgit v1.2.3