9os

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 01df3e6dcb31efe1959d9debeaca6013512df849
parent a18a0a49e712bc4f95b5e5ff80cd804f74fadb28
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Tue,  8 Sep 2020 10:04:59 +0200

devcons: Improve quality of the code

Change-Id: I12385387724257babdba5c2be1bdd917922417e7

Diffstat:
Msrc/kernel/dev/devcons.c | 107++++++++++++++++++++++++++++++++++++++-----------------------------------------
1 file changed, 52 insertions(+), 55 deletions(-)

diff --git a/src/kernel/dev/devcons.c b/src/kernel/dev/devcons.c @@ -15,6 +15,7 @@ #define CONSSTATUS 128 #define CTRL(x) ((x) & 0x1f) +#define DEL 127 enum Orootqid { Qconsfs, @@ -118,14 +119,6 @@ conswriteraw(char *buf, int n) } static int -echo(void *buf, int n) -{ - if (!echof) - return n; - return conswriteraw(buf, n); -} - -static int consaddin(struct conscmd *cmd, char *s) { if (in) { @@ -238,85 +231,92 @@ conswrite(Chan *c, void *buf, int n) } } +static int +echo(char *buf, int n) +{ + if (echof) { + if (conswriteraw(buf, n) < 0) + return -1; + } + return buf[n-1]; +} + static void erase(int n) { int i; - char editingchar; + char ch; - if (head + 1 < n) - n = head + 1; + if (head < n) + n = head; - editingchar = '\b'; + ch = '\b'; for (i = 0; i < n; i++) - echo(&editingchar, 1); - editingchar = ' '; + echo(&ch, 1); + ch = ' '; for (i = 0; i < n; i++) - echo(&editingchar, 1); - editingchar = '\b'; + echo(&ch, 1); + ch = '\b'; for (i = 0; i < n; i++) - echo(&editingchar, 1); + echo(&ch, 1); + + head -= n; } static int -consreadediting(Chan *c) +wordsize(void) { - int i; - char ch; + int i = 1; + + while (head - i > 0 && buffer[head - i] == ' ') + i++; + while (head - i > 0 && buffer[head - i] != ' ') + i++; + + return 1; +} +static int +edit(char key) +{ + char ch; - if (!cookedf) - return echo(&buffer[head], 1); + if (!cookedf) { + buffer[head++] = key; + return echo(&key, 1); + } - /* - * it relies on the fact that head is incremented in the - * calling function after exiting this one. For example, head is - * briefly equal to -1 when backspace is hit on an empty line. - */ - switch (buffer[head]) { + switch (key) { case '\0': - return -1; + return 0; case '\b': - case 127: /* DEL */ - head--; + case DEL: erase(1); - head -= head >= 0; return 0; case CTRL('L'): ch = '\r'; echo(&ch, 1); - echo(buffer, head); - head--; + echo(buffer, head-1); return 0; case CTRL('U'): ch = '\r'; echo(&ch, 1); - ch = ' '; - for (i = 0; i < head; i++) - echo(&ch, 1); - ch = '\r'; + erase(head); echo(&ch, 1); - head = -1; return 0; case CTRL('W'): - i = 0; - head--; - while (head - i >= 0 && buffer[head - i] == ' ') - i++; - while (head - i >= 0 && buffer[head - i] != ' ') - i++; - erase(i); - head -= i; + erase(wordsize()); return 0; case '\r': - buffer[head] = '\n'; + key = '\n'; default: - return echo(&buffer[head], 1); + buffer[head++] = key; + return echo(&key, 1); } } static int -fillbuffer(Chan *c) +fillbuffer(void) { char ch; @@ -325,16 +325,13 @@ fillbuffer(Chan *c) ch = EOF; break; } - buffer[head] = ch; - consreadediting(c); - if (buffer[head++] == '\n') + if (edit(ch) == '\n') break; } if (ch == EOF && head == 0) { errno = EIO; return -1; } - buffer[head] = '\0'; return 0; } @@ -368,7 +365,7 @@ consread(Chan *c, void *buf, int n) return -1; } if (head == 0) { - if (fillbuffer(c) < 0) + if (fillbuffer() < 0) return -1; }