ref: d75e8f47d40918903dbf715afcbf1472ed58ae4a
parent: 81acb5be5b025979ca6a1cd4abad0634964119de
author: Sigrid Haflínudóttir <ftrvxmtrx@gmail.com>
date: Fri Feb 21 09:29:54 EST 2020
plan9: read stdin as a stream of commands
--- a/plan9.c
+++ b/plan9.c
@@ -809,6 +809,33 @@
}
static void
+stdinproc(void *)
+{
+ char buf[256];
+ int n, i;
+
+ threadsetname("stdinproc");
+
+ for (;;) {
+ if ((n = read(0, buf, sizeof(buf)-1)) <= 0)
+ break;
+ for (i = 0; i < n; i++) {
+ if (buf[i] == '\r' || buf[i] == '\n' || buf[i] == 0) {
+ buf[i] = 0;
+ while (buf[i+1] == '\n')
+ i++;
+ command(buf);
+ n -= i;
+ memmove(buf, buf+i+1, n);
+ i = 0;
+ }
+ }
+ }
+
+ threadexits(nil);
+}
+
+static void
kbdproc(void *k)
{
char buf[128], buf2[128], *s;
@@ -819,13 +846,13 @@
threadsetname("kbdproc");
if ((kfd = open("/dev/kbd", OREAD)) < 0)
- sysfatal("can't open kbd: %r");
+ sysfatal("/dev/kbd: %r");
kchan = k;
buf2[0] = 0;
buf2[1] = 0;
buf[0] = 0;
- for(;;){
+ for (;;) {
if (buf[0] != 0) {
n = strlen(buf)+1;
memmove(buf, buf+n, sizeof(buf)-n);
@@ -842,13 +869,12 @@
case 'c':
if (chartorune(&r, buf+1) > 0 && r != Runeerror)
nbsend(cchan, &r);
- /* no break */
default:
continue;
case 'k':
s = buf+1;
- while (*s){
+ while (*s) {
s += chartorune(&r, s);
if (utfrune(buf2+1, r) == nil) {
key.down = 1;
@@ -862,7 +888,7 @@
s = buf2+1;
while (*s) {
s += chartorune(&r, s);
- if(utfrune(buf+1, r) == nil) {
+ if (utfrune(buf+1, r) == nil) {
key.down = 0;
key.rune = r;
nbsend(kchan, &key);
@@ -872,6 +898,8 @@
}
strcpy(buf2, buf);
}
+
+ threadexits(nil);
}
static void
@@ -1007,6 +1035,8 @@
proccreate(kbdproc, a[Ckey].c, mainstacksize);
kctl.c = cchan;
+
+ proccreate(stdinproc, nil, mainstacksize);
for (n = 0; n < Numcolors; n++) {
if (inverse)