commit 3d3bcd3c9df90978c9b328cb8851434d23c541e5
parent 03aa7bbfe7d4fd1527052f7145a8827db6832a68
Author: Pavel Renev <an2qzavok@gmail.com>
Date: Sun, 9 May 2021 17:10:51 +0000
steal runcmd from vt(1)
Diffstat:
M | devfs.c | | | 15 | ++++++++++++--- |
M | richterm.c | | | 69 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----- |
2 files changed, 76 insertions(+), 8 deletions(-)
diff --git a/devfs.c b/devfs.c
@@ -9,6 +9,12 @@
void
devfs_read(Req *r)
{
+ Devfsctl *dctl;
+ int kv;
+ dctl = r->fid->file->aux;
+ recv(dctl->rc, &kv);
+ r->ofcall.count = 1; //sizeof(int);
+ memcpy(r->ofcall.data, &kv, 1);
respond(r, nil);
}
@@ -41,11 +47,14 @@ initdevfs(void)
dctl = mallocz(sizeof(Devfsctl), 1);
dctl->wc = chancreate(sizeof(Object), 0);
- srv.tree = alloctree(nil, nil, 0600, nil);
+ dctl->rc = chancreate(sizeof(int), 1024);
+ srv.tree = alloctree("richterm", "richterm", DMDIR|0555, nil);
if (srv.tree == nil)
return nil;
- if (createfile(srv.tree->root, "cons", nil, 0600, dctl) == nil)
+ if (createfile(srv.tree->root, "cons", "richterm", 0666, dctl) == nil)
+ return nil;
+ if (createfile(srv.tree->root, "consctl", "richterm", 0666, dctl) == nil)
return nil;
- threadpostmountsrv(&srv, "rtdev", nil, 0);
+ threadpostmountsrv(&srv, nil, "/dev", MBEFORE);
return dctl;
}
diff --git a/richterm.c b/richterm.c
@@ -25,29 +25,78 @@ struct Fonts {
int count;
};
+int hostpid = -1;
+Channel *pidchan;
+
+Devfsctl *dctl;
+
Fonts fonts;
Page generatepage(Rectangle, Rich *);
Font* getfont(Fonts *, char *);
void addfont(Fonts *, Font *);
+void shutdown(void);
+void send_interrupt(void);
+void runcmd(void *args);
+
+void
+runcmd(void *args)
+{
+ char **argv = args;
+ char *cmd;
+
+ rfork(RFNAMEG);
+
+ if ((dctl = initdevfs()) == nil)
+ sysfatal("initdevfs failed: %r");
+
+ rfork(RFFDG);
+ close(0);
+ open("/dev/cons", OREAD);
+ close(1);
+ open("/dev/cons", OWRITE);
+ dup(1, 2);
+
+ cmd = nil;
+ while (*argv != nil) {
+ if (cmd == nil) cmd = strdup(*argv);
+ else cmd = smprint("%s %q", cmd, *argv);
+ argv++;
+ }
+
+ procexecl(pidchan, "/bin/rc", "rcX", cmd == nil ? nil : "-c", cmd, nil);
+ sysfatal("%r");
+}
+
+void
+shutdown(void)
+{
+ send_interrupt();
+ threadexitsall(nil);
+}
+
+void
+send_interrupt(void)
+{
+ if(hostpid > 0)
+ postnote(PNGROUP, hostpid, "interrupt");
+}
void
usage(void)
{
fprint(2, "usage: %s [-D] [cmd]\n", argv0);
- threadexitsall("usage");
+ exits("usage");
}
void
threadmain(int argc, char **argv)
{
- Channel *oc;
Object ov;
Rich rich;
int i;
Mousectl *mctl;
Keyboardctl *kctl;
- Devfsctl *dctl;
int rv[2];
Mouse mv;
Rune kv;
@@ -59,6 +108,15 @@ threadmain(int argc, char **argv)
usage();
} ARGEND
+
+ if(rfork(RFENVG) < 0)
+ sysfatal("rfork: %r");
+ atexit(shutdown);
+
+ pidchan = chancreate(sizeof(int), 0);
+ proccreate(runcmd, argv, 16 * 1024);
+ hostpid = recvul(pidchan);
+
if (initdraw(0, 0, "richterm") < 0)
sysfatal("%s: %r", argv0);
@@ -102,7 +160,7 @@ threadmain(int argc, char **argv)
if ((kctl = initkeyboard(nil)) == nil)
sysfatal("%s: %r", argv0);
- if ((dctl = initdevfs()) == nil) sysfatal("initdevfs failed: %r");
+ // if ((dctl = initdevfs()) == nil) sysfatal("initdevfs failed: %r");
// init /mnt fs for exposing internals
// launch a subprocess from cmd passed on args
@@ -131,7 +189,8 @@ threadmain(int argc, char **argv)
flushimage(display, 1);
break;
case KBD:
- if (kv == 0x7f) threadexitsall(nil);
+ if (kv == 0x7f) shutdown();
+ nbsend(dctl->rc, &kv);
break;
case DEVFSWRITE:
rich.count++;