commit ae369f71acaee978b1d84f07380f6d206921bbad
parent 3a1c73c2e97f4a8ef071f096bdd90297aa8e4048
Author: Roberto E. Vargas Caballero <k0ga@shike2.com>
Date: Tue, 1 Sep 2020 22:21:56 +0200
devcons: Simplify cons commands
The functions that were serving the cons commands
had a prototype that was complicating the code of
every command.
Change-Id: I6c4410364afb36acb3737ac3ea64b65edc14b504
Diffstat:
1 file changed, 35 insertions(+), 53 deletions(-)
diff --git a/src/kernel/dev/devcons.c b/src/kernel/dev/devcons.c
@@ -25,6 +25,13 @@ enum Orootqid {
Qctl
};
+struct conscmd {
+ char *name;
+ int (*fn)(struct conscmd *, char *);
+};
+
+static struct conscmd cmds[];
+
static const Dirtab dirtab[] = {
{"raw", QID(CHFILE, 0, Qraw), 0, O_READ | O_WRITE},
{"ctl", QID(CHFILE, 0, Qctl), 0, O_READ | O_WRITE}
@@ -78,37 +85,27 @@ consstatus(void *buf, Chan *c, int n)
}
static int
-consaddin(char *buf, int n)
+consaddin(struct conscmd *cmd, char *s)
{
- if (n >= NAMESIZE) {
- errno = EINVAL;
- return -1;
- }
-
if (in) {
errno = ENOMEM;
return -1;
}
- in = namec(buf, O_READ);
- memcpy(inname, buf, n);
+ in = namec(s, O_READ);
+ strcpy(inname, s);
return 0;
}
static int
-consaddout(char *buf, int n)
+consaddout(struct conscmd *cmd, char *s)
{
int i;
- if (n >= NAMESIZE) {
- errno = ENAMETOOLONG;
- return -1;
- }
-
for (i = 0; i < CONSOUT; i++) {
if (!out[i]) {
- out[i] = namec(buf, O_WRITE);
- memcpy(outname[i], buf, n);
+ out[i] = namec(s, O_WRITE);
+ strcpy(outname[i], s);
return 0;
}
}
@@ -117,14 +114,9 @@ consaddout(char *buf, int n)
}
static int
-consdelin(char *buf, int n)
+consdelin(struct conscmd *cmd, char *s)
{
- if (n >= NAMESIZE) {
- errno = EINVAL;
- return -1;
- }
-
- if (!in || memcmp(buf, inname, n)) {
+ if (!in || strcmp(s, inname)) {
errno = ENOENT;
return -1;
}
@@ -136,17 +128,12 @@ consdelin(char *buf, int n)
}
static int
-consdelout(char *buf, int n)
+consdelout(struct conscmd *cmd, char *s)
{
int i;
- if (n >= NAMESIZE) {
- errno = EINVAL;
- return -1;
- }
-
for (i = 0; i < CONSOUT; i++) {
- if (out[i] && !memcmp(buf, outname[i], n)) {
+ if (out[i] && !strcmp(s, outname[i])) {
delchan(out[i]);
out[i] = NULL;
outname[i][0] = '\0';
@@ -158,11 +145,11 @@ consdelout(char *buf, int n)
}
static int
-consmode(char *arg, int n)
+consmode(struct conscmd *cmd, char *s)
{
- if (!strcmp(arg, "raw")) {
+ if (!strcmp(s, "raw")) {
mode = RAW;
- } else if(!strcmp(arg, "cooked")) {
+ } else if(!strcmp(s, "cooked")) {
mode = COOKED;
} else {
errno = EINVAL;
@@ -209,22 +196,8 @@ conswriteraw(char *buf, int n)
static int
conswrite(Chan *c, void *buf, int n)
{
+ struct conscmd *cmd;
char *tokens[2];
- int i;
- int (*func)(char *, int);
-
- struct conscmds {
- const char name[7];
- int (*func)(char *, int);
- };
-
- static const struct conscmds conscmds[] = {
- {"addin", consaddin},
- {"addout", consaddout},
- {"delin", consdelin},
- {"delout", consdelout},
- {"mode", consmode},
- };
switch (c->qid.path) {
case Qraw:
@@ -234,12 +207,12 @@ conswrite(Chan *c, void *buf, int n)
errno = EINVAL;
return -1;
}
- for (i = 0; i < NELEM(conscmds); i++) {
- if (!strcmp(tokens[0], conscmds[i].name)) {
- func = conscmds[i].func;
- return (*func)(tokens[1], strlen(tokens[1]));
- }
+
+ for (cmd = cmds; *cmd->name; ++cmd) {
+ if (!strcmp(tokens[0], cmd->name))
+ return (*cmd->fn)(cmd, tokens[1]);
}
+
errno = EINVAL;
return -1;
default:
@@ -389,6 +362,15 @@ consread(Chan *c, void *buf, int n)
}
}
+static struct conscmd cmds[] = {
+ {"addin", consaddin},
+ {"addout", consaddout},
+ {"delin", consdelin},
+ {"delout", consdelout},
+ {"mode", consmode},
+ {""}
+};
+
const Dev consdevtab = {
.id = 'c',
.stat = consstat,