diff options
author | Alex AUVOLAT <alexis211@gmail.com> | 2012-05-20 20:20:39 +0200 |
---|---|---|
committer | Alex AUVOLAT <alexis211@gmail.com> | 2012-05-20 20:20:39 +0200 |
commit | 2e28873f23d2cf073fdddfecdc278b0f44b2789b (patch) | |
tree | 58e5ac230bdbe82def76751f4e7a2bb34ce01475 /src/user/lib/libc | |
parent | 7fde5e61bc7047433bfc1f47229b72d0ccd302e7 (diff) | |
download | TCE-2e28873f23d2cf073fdddfecdc278b0f44b2789b.tar.gz TCE-2e28873f23d2cf073fdddfecdc278b0f44b2789b.zip |
Corrected some bugs in readline && multitasking improvement.
Diffstat (limited to 'src/user/lib/libc')
-rw-r--r-- | src/user/lib/libc/std/readline.c | 107 |
1 files changed, 66 insertions, 41 deletions
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; } } |