9os

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

commit a18a0a49e712bc4f95b5e5ff80cd804f74fadb28
parent ae369f71acaee978b1d84f07380f6d206921bbad
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date:   Wed,  2 Sep 2020 06:51:00 +0200

devcons: Implement echo ctl command

Cooked mode was used for editing and echo, while they
are different things and they must be different commands.
This commit is also used to apply cosmetic changes.

Change-Id: If4024a402298ef76cc797c1b40cc8e27385cefe8

Diffstat:
Msrc/kernel/dev/devcons.c | 145+++++++++++++++++++++++++++++++++++++++++++++----------------------------------
1 file changed, 82 insertions(+), 63 deletions(-)

diff --git a/src/kernel/dev/devcons.c b/src/kernel/dev/devcons.c @@ -14,9 +14,6 @@ #define NAMESIZE 15 #define CONSSTATUS 128 -#define RAW 0 -#define COOKED 1 - #define CTRL(x) ((x) & 0x1f) enum Orootqid { @@ -45,7 +42,7 @@ static char outname[CONSOUT][NAMESIZE]; static char buffer[LINELEN]; static int head; static int tail; -static int mode; +static int cookedf, echof; static int conswalk(Chan *c, const char *name) @@ -72,10 +69,12 @@ consstatus(void *buf, Chan *c, int n) inname); for (i = 0; i < CONSOUT; i++) { - if (out[i]) - len += ksnprint(tmp + len, sizeof(tmp) - len, + if (out[i]) { + len += ksnprint(tmp + len, + sizeof(tmp) - len, "addout %s\n", outname[i]); + } } if (len == sizeof(tmp)) @@ -85,6 +84,48 @@ consstatus(void *buf, Chan *c, int n) } static int +conswriteraw(char *buf, int n) +{ + int i, idx, r; + char wrtbuf[LINELEN]; + Chan **pp; + + idx = 0; + for (i = 0; i < n; i++) { + if (idx + 2 > sizeof(wrtbuf)) + break; + if (buf[i] == '\n') + wrtbuf[idx++] = '\r'; + wrtbuf[idx++] = buf[i]; + } + + r = 0; + for (pp = out; pp < &out[CONSOUT]; pp++) { + int w; + Chan *p; + + if ((p = *pp) == NULL) + continue; + w = devtab[p->type]->write(p, wrtbuf, idx); + if (w < 0) { + r = -1; + } else if (w != idx) { + errno = EIO; + r = -1; + } + } + return r < 0 ? r : idx; +} + +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) { @@ -147,50 +188,27 @@ consdelout(struct conscmd *cmd, char *s) static int consmode(struct conscmd *cmd, char *s) { - if (!strcmp(s, "raw")) { - mode = RAW; - } else if(!strcmp(s, "cooked")) { - mode = COOKED; + int mode; + + if (!strcmp(s, "on")) { + mode = 1; + } else if(!strcmp(s, "off")) { + mode = 0; } else { errno = EINVAL; return -1; } - return 0; -} - -static int -conswriteraw(char *buf, int n) -{ - int i, idx, r; - char wrtbuf[LINELEN]; - Chan **pp; - - idx = 0; - for (i = 0; i < n; i++) { - if (idx + 2 > sizeof(wrtbuf)) - break; - if (buf[i] == '\n') - wrtbuf[idx++] = '\r'; - wrtbuf[idx++] = buf[i]; + if (!strcmp(cmd->name, "cooked")) { + cookedf = mode; + } else if (!strcmp(cmd->name, "echo")) { + echof = mode; + } else { + errno = EINVAL; + return -1; } - r = 0; - for (pp = out; pp < &out[CONSOUT]; pp++) { - int w; - Chan *p; - - if ((p = *pp) == NULL) - continue; - w = devtab[p->type]->write(p, wrtbuf, idx); - if (w < 0) { - r = -1; - } else if (w != idx) { - errno = EIO; - r = -1; - } - } - return r < 0 ? r : idx; + return 0; } static int @@ -221,7 +239,7 @@ conswrite(Chan *c, void *buf, int n) } static void -conserase(Chan *c, int n) +erase(int n) { int i; char editingchar; @@ -231,24 +249,24 @@ conserase(Chan *c, int n) editingchar = '\b'; for (i = 0; i < n; i++) - conswrite(c, &editingchar, 1); + echo(&editingchar, 1); editingchar = ' '; for (i = 0; i < n; i++) - conswrite(c, &editingchar, 1); + echo(&editingchar, 1); editingchar = '\b'; for (i = 0; i < n; i++) - conswrite(c, &editingchar, 1); + echo(&editingchar, 1); } static int consreadediting(Chan *c) { int i; - char editingchar; + char ch; - if (mode == RAW) - return 0; + if (!cookedf) + return echo(&buffer[head], 1); /* * it relies on the fact that head is incremented in the @@ -261,23 +279,23 @@ consreadediting(Chan *c) case '\b': case 127: /* DEL */ head--; - conserase(c, 1); + erase(1); head -= head >= 0; return 0; case CTRL('L'): - editingchar = '\r'; - conswrite(c, &editingchar, 1); - conswrite(c, buffer, head); + ch = '\r'; + echo(&ch, 1); + echo(buffer, head); head--; return 0; case CTRL('U'): - editingchar = '\r'; - conswrite(c, &editingchar, 1); - editingchar = ' '; + ch = '\r'; + echo(&ch, 1); + ch = ' '; for (i = 0; i < head; i++) - conswrite(c, &editingchar, 1); - editingchar = '\r'; - conswrite(c, &editingchar, 1); + echo(&ch, 1); + ch = '\r'; + echo(&ch, 1); head = -1; return 0; case CTRL('W'): @@ -287,13 +305,13 @@ consreadediting(Chan *c) i++; while (head - i >= 0 && buffer[head - i] != ' ') i++; - conserase(c, i); + erase(i); head -= i; return 0; case '\r': buffer[head] = '\n'; default: - return conswrite(c, &buffer[head], 1); + return echo(&buffer[head], 1); } } @@ -367,7 +385,8 @@ static struct conscmd cmds[] = { {"addout", consaddout}, {"delin", consdelin}, {"delout", consdelout}, - {"mode", consmode}, + {"cooked", consmode}, + {"echo", consmode}, {""} };