ref: b5aa12ceca7ee1532a457189b8e304c59e15f711
parent: 01098d2f2f2ad421a77beba1793b3e71ed97d0a2
author: Michael Misch <michaelmisch1985@gmail.com>
date: Fri Aug 16 01:58:11 EDT 2024
Fix formatting issues on input/ctl message parsing
--- a/alt.h
+++ b/alt.h
@@ -26,7 +26,7 @@
// TODO: Move data to the stack
MaxBuflen = 128,
MaxDatalen = 1024,
- CmdSize = MaxBuflen * 2 + 1,
+ CmdSize = MaxBuflen * 2 + 1 + MaxDatalen,
};
struct Buffer
--- a/client.c
+++ b/client.c
@@ -318,17 +318,13 @@
Buffer *b;
Clfid *f;
Cmd cmd;
- char *s, t[MaxDatalen], path[1024];
+ char path[1024];
f = r->fid->aux;
-
- s = mallocz(r->ifcall.count, 1);
- memmove(s, r->ifcall.data, r->ifcall.count);
-
+ memset(&cmd, 0, sizeof(Cmd));
switch(f->level){case Qctl:
- memset(&cmd, 0, CmdSize);
- convS2C(&cmd, s, r->ifcall.count);
+ convS2C(&cmd, r->ifcall.data, r->ifcall.count);
switch(cmd.type){case BufferCmd:
b = bufferSearch(root, cmd.buffer);
@@ -354,7 +350,8 @@
f->cl->showmarkdown = !f->cl->showmarkdown;
goto Out;
default:
- send(root->cmds, &cmd);
+ sendp(root->cmds, &cmd);
+ sendp(root->cmds, nil);
goto Out;
}
case Qinput:
@@ -363,13 +360,14 @@
respond(r, "No buffer selected");
return;
}
- // Cut off the newline
- memset(t, 0, MaxDatalen);
- snprint(t, sizeof(t), "input %s\n%s", f->cl->current->name, r->ifcall.data);
- send(root->input, t);
+ cmd.type = InputCmd;
+ strcpy(cmd.buffer, f->cl->current->name);
+ cmd.data = strdup(r->ifcall.data);
+ cmd.data[r->ifcall.count] = 0;
+ sendp(root->cmds, &cmd);
+ sendp(root->cmds, nil);
}
Out:
- free(s);
respond(r, nil);
return;
}
--- a/cmd.c
+++ b/cmd.c
@@ -15,7 +15,7 @@
c = va_arg(fp->args, Cmd*);
switch(c->type){case ServiceCmd:
- if(c->data != nil)
+ if(strlen(c->data) > 0)
snprint(s, sizeof(s), "%s %s\n%s", c->svccmd, c->buffer, c->data);
else
snprint(s, sizeof(s), "%s %s", c->svccmd, c->buffer);
--- a/convS2C.c
+++ b/convS2C.c
@@ -39,6 +39,7 @@
parse_cmd(struct state *s)
{int n;
+ char *first;
n = 0;
for(;;){@@ -87,7 +88,8 @@
s->cmd.type = MarkdownCmd;
else {s->cmd.type = ServiceCmd;
- strncpy(s->cmd.svccmd, s->base, n);
+ first = strtok(s->base, " ");
+ sprint(s->cmd.svccmd, first);
}
s->size -= n;
n++;
--- a/service.c
+++ b/service.c
@@ -82,7 +82,6 @@
// NOTE: If you're sending more commands than this before they are processed, up this number
// But also it might be time to question your design, because commands really should not be taking long
svc->cmds = chancreate(sizeof(Cmd*), 8);
- svc->input = chancreate(sizeof(char*), 8);
svc->base = bufferCreate(svc->cmds, svc->input);
svc->isInitialized = 0;
@@ -291,21 +290,14 @@
svcread(Req *r)
{- Cmd cmd;
- char input[MaxDatalen];
+ Cmd *cmd;
+
char buf[CmdSize];
Svcfid *f;
f = r->fid->aux;
memset(buf, 0, CmdSize);
- Alt a[] = {- [CmdRead] = { nil, &cmd, CHANRCV },- [InputRead] = { nil, input, CHANRCV },- };
- a[CmdRead].c = f->svc->cmds;
- a[InputRead].c = f->svc->input;
-
switch(f->level){case Qsroot:
dirread9p(r, rootgen, nil);
@@ -324,20 +316,12 @@
} else {// Wait for any data/command from the client
srvrelease(fs);
- memset(&cmd, 0, CmdSize);
- switch(alt(a)) {- case CmdRead:
- if(cmd.type != FlushCmd){- snprint(buf, sizeof(buf), "%C\n data", &cmd);
- readstr(r, buf);
-
- }
- break;
- case InputRead:
- readstr(r, input);
- break;
- }
+ cmd = recvp(f->svc->cmds);
srvacquire(fs);
+ if(cmd){+ snprint(buf, sizeof(buf), "%C", cmd);
+ readstr(r, buf);
+ }
respond(r, nil);
}
return;
--
⑨