ref: 2b23d05d57743af57385cd42c0fd2d223b11d8c8
dir: /systemcmd.c/
#include <u.h>
#include <libc.h>
#include <thread.h>
#include <plumb.h>
#include "dat.h"
#include "fns.h"
char *syscmd_off(Session *, char *);
char *syscmd_ed(Session *, char *);
struct {
char *name;
char *(*fn)(Session *, char *);
} cmdtab[] = {
{ "off", syscmd_off },
{ "ed", syscmd_ed },
};
void
systemcmd(Session *s, char *cmd, int ctl)
{
char *out;
char *parts[2];
for(vlong i = strlen(cmd)-1; i >= 0; i--){
if(cmd[i] != ' ')
break;
else
cmd[i] = 0;
}
if(getfields(cmd, parts, 2, 1, " \n") == 1)
parts[1] = "";
char *(*fn)(Session *, char *) = nil;
for(int i = 0; i < nelem(cmdtab) && fn == nil; i++)
if(strcmp(cmdtab[i].name, parts[0]) == 0)
fn = cmdtab[i].fn;
if(fn != nil)
out = fn(s, parts[1]);
else
out = smprint("invalid system command: %s", parts[0]);
if(!ctl && out){
appendlog(s, out); /* Otherwise do something that makes read from the ctl file get the response... */
appendlog(s, "\n");
}
free(out);
}
char *
syscmd_off(Session *s, char *args)
{
if(strcmp(args, "") != 0)
return smprint("unexpected: %s\n", args);
/* TODO force the lpa script's 'cat cons' to stop. */
s->active = 0;
return smprint("bye bye :)");
}
char *
syscmd_ed(Session *s, char *name)
{
char *resp = nil;
int fd = plumbopen("send", OWRITE);
if(fd < 0)
return smprint("plumb failed: %r");
trim(name);
/* create the symbol */
sym(s->modules->modules[0]->symtab, name); /* TODO: fix this and the line below. Name and module should be parsed.. */
char *path = smprint("/mnt/lpa/%ulld/modules/main/%s", s->id, name);
if(plumbsendtext(fd, "lpa", "edit", "/", path) < 0)
resp = smprint("plumb failed: %r");
close(fd);
free(path);
return resp;
}