aboutsummaryrefslogtreecommitdiff
path: root/src/cmd/rc
diff options
context:
space:
mode:
Diffstat (limited to 'src/cmd/rc')
-rw-r--r--src/cmd/rc/code.c277
-rw-r--r--src/cmd/rc/exec.c1267
-rw-r--r--src/cmd/rc/exec.h47
-rw-r--r--src/cmd/rc/input.c1679
-rw-r--r--src/cmd/rc/io.c437
-rw-r--r--src/cmd/rc/job.c91
-rw-r--r--src/cmd/rc/lex.c394
-rw-r--r--src/cmd/rc/main.c66
-rw-r--r--src/cmd/rc/parse.c2059
-rw-r--r--src/cmd/rc/parse.h141
-rw-r--r--src/cmd/rc/prompt.c36
-rw-r--r--src/cmd/rc/rc.h263
-rw-r--r--src/cmd/rc/rules.mk32
-rw-r--r--src/cmd/rc/syntax.y147
-rw-r--r--src/cmd/rc/sys.c137
-rw-r--r--src/cmd/rc/tree.c111
-rw-r--r--src/cmd/rc/util.c65
-rw-r--r--src/cmd/rc/var.c336
-rw-r--r--src/cmd/rc/wait.c247
19 files changed, 7832 insertions, 0 deletions
diff --git a/src/cmd/rc/code.c b/src/cmd/rc/code.c
new file mode 100644
index 0000000..786f284
--- /dev/null
+++ b/src/cmd/rc/code.c
@@ -0,0 +1,277 @@
+#include "rc.h"
+#include "parse.h"
+#include "exec.h"
+
+// -----------------------------------------------------------------------
+// types
+
+struct Interpreter
+{
+ int i, cap;
+ Code *code;
+};
+
+Code *compiled = nil;
+static struct Interpreter interpreter;
+#define emiti(x) ((void)(interpreter.i != interpreter.cap || grow()), interpreter.code[interpreter.i].i = (x), interpreter.i++)
+#define emitf(x) ((void)(interpreter.i != interpreter.cap || grow()), interpreter.code[interpreter.i].f = (x), interpreter.i++)
+#define emits(x) ((void)(interpreter.i != interpreter.cap || grow()), interpreter.code[interpreter.i].s = (x), interpreter.i++)
+
+static
+int
+grow(void)
+{
+ interpreter.cap += 100;
+ interpreter.code = erealloc(interpreter.code, sizeof(*interpreter.code)*interpreter.cap);
+ memset(interpreter.code+interpreter.cap-100, 0, 100*sizeof(*interpreter.code));
+
+ return 0;
+}
+
+static
+void
+storepc(int a)
+{
+ if(interpreter.i <= a || a < 0)
+ fatal("bad address %d in interpreter", a);
+
+ interpreter.code[a].i = interpreter.i;
+}
+
+static
+void
+walk(Tree *node)
+{
+ Tree *n;
+ int addr1, addr2;
+
+ if(!node)
+ return;
+
+ switch(node->type){
+ default:
+ print(shell.err, "bad type %d in interpreter walk\n", node->type);
+ fatal("crashing\n");
+ break;
+
+ case '$':
+ emitf(Xmark);
+ walk(node->child[0]);
+ emitf(Xdollar);
+ break;
+
+ case Tcount:
+ emitf(Xmark);
+ walk(node->child[0]);
+ emitf(Xcount);
+ break;
+
+ case Tjoin:
+ emitf(Xmark);
+ walk(node->child[0]);
+ emitf(Xjoin);
+ break;
+
+ case Tindex:
+ emitf(Xmark);
+ walk(node->child[1]);
+ emitf(Xmark);
+ walk(node->child[0]);
+ emitf(Xindex);
+ break;
+
+ case ';':
+ walk(node->child[0]);
+ walk(node->child[1]);
+ break;
+
+ case '^':
+ emitf(Xmark);
+ walk(node->child[1]);
+ emitf(Xmark);
+ walk(node->child[0]);
+ emitf(Xconcatenate);
+ break;
+
+ case Tandand:
+ walk(node->child[0]);
+ emitf(Xtrue);
+ addr1 = emiti(0);
+ walk(node->child[1]);
+ storepc(addr1);
+ break;
+
+ case Toror:
+ walk(node->child[0]);
+ emitf(Xfalse);
+ addr1 = emiti(0);
+ walk(node->child[1]);
+ storepc(addr1);
+ break;
+
+ case Targs:
+ walk(node->child[1]);
+ walk(node->child[0]);
+ break;
+
+ case Tparen: case Tblock:
+ walk(node->child[0]);
+ break;
+
+ case Tbasic:
+ emitf(Xmark);
+ walk(node->child[0]);
+ emitf(Xbasic);
+ break;
+
+ case Tbang:
+ walk(node->child[0]);
+ emitf(Xbang);
+
+ case Tword:
+ emitf(Xword);
+ emits(strdup(node->str));
+ break;
+
+ case Twords:
+ walk(node->child[1]);
+ walk(node->child[0]);
+ break;
+
+ case '=':
+ for(n=node; node && node->type == '='; node = node->child[2])
+ ;
+ if(node){
+ for(node=n; node->type=='='; node = node->child[2]){
+ emitf(Xmark);
+ walk(node->child[1]);
+ emitf(Xmark);
+ walk(node->child[0]);
+ emitf(Xlocal);
+ }
+ walk(node);
+ for(node=n; node->type=='='; node = node->child[2])
+ emitf(Xunlocal);
+ }else{
+ for(node=n; node; node=node->child[2]){
+ emitf(Xmark);
+ walk(node->child[1]);
+ emitf(Xmark);
+ walk(node->child[0]);
+ emitf(Xassign);
+ }
+ }
+ node = n;
+ break;
+ /* control structures */
+ case Twhile:
+ addr1 = interpreter.i; // head of loop
+ walk(node->child[0]);
+ if(addr1 == interpreter.i)
+ fatal("TODO");
+ emitf(Xtrue);
+ addr2 = emiti(0); // goto end of loop
+
+ walk(node->child[1]);
+ emitf(Xgoto);
+ emiti(addr1); // goto top of loop
+ storepc(addr2);
+ break;
+
+ case Tfor:
+ emitf(Xmark);
+ if(node->child[1]){ // for( x in X )
+ walk(node->child[1]);
+ // emitf(Xglob)
+ }else{ // for(X)
+ fatal("TODO");
+ }
+ emitf(Xmark); // null initial value for Xlocal
+ emitf(Xmark);
+ walk(node->child[0]);
+ emitf(Xlocal);
+
+ addr1 = emitf(Xfor);
+ addr2 = emiti(0);
+
+ walk(node->child[2]);
+ emitf(Xgoto);
+ emiti(addr1);
+ storepc(addr2);
+ emitf(Xunlocal);
+ break;
+
+ /* forks */
+ case '&':
+ emitf(Xasync);
+ addr1 = emiti(0);
+ walk(node->child[0]);
+ emitf(Xexit);
+ storepc(addr1);
+ break;
+
+ case Tsubshell:
+ emitf(Xsubshell);
+ addr1 = emiti(0);
+ walk(node->child[0]);
+ emitf(Xexit);
+ storepc(addr1);
+ break;
+
+ case Tpipe:
+ emitf(Xpipe);
+
+ emiti(node->redir.fd[0]);
+ emiti(node->redir.fd[1]);
+ addr1 = emiti(0);
+ addr2 = emiti(0);
+
+ walk(node->child[0]);
+ emitf(Xexit);
+ storepc(addr1);
+
+ walk(node->child[1]);
+ emitf(Xreturn);
+ storepc(addr2);
+
+ emitf(Xpipewait);
+
+ break;
+ }
+}
+
+// -----------------------------------------------------------------------
+// main exports
+
+void
+freecode(Code *c)
+{
+ if(--c[0].i!=0)
+ return;
+ efree(c);
+}
+
+Code *
+copycode(Code *c)
+{
+ c[0].i++;
+ return c;
+}
+
+int
+compile(Tree *node)
+{
+ flush(shell.err);
+
+ interpreter.i = 0;
+ interpreter.code = emalloc(100*sizeof(*interpreter.code));
+ emiti(0); // reference count: no thread owns code yet
+
+ walk(node);
+
+ emitf(Xreturn);
+ emitf(nil);
+
+ compiled = interpreter.code;
+ return 0;
+}
diff --git a/src/cmd/rc/exec.c b/src/cmd/rc/exec.c
new file mode 100644
index 0000000..5baaf1a
--- /dev/null
+++ b/src/cmd/rc/exec.c
@@ -0,0 +1,1267 @@
+#include "rc.h"
+#include "exec.h"
+
+#include <sys/wait.h>
+
+int yyparse(void);
+
+struct Builtin{
+ char *name;
+ void (*func)(void);
+};
+
+struct State {
+ int async;
+};
+
+static struct State state;
+
+// -----------------------------------------------------------------------
+// globals
+
+static Word nullpath = { .str="", .link=nil };
+
+struct Builtin builtin[]={
+ {"cd", xcd},
+ {".", xdot},
+ {"echo", xecho},
+ {"exit", xexit},
+ {"fg", xfg},
+ {"jobs", xjob},
+ 0,
+};
+
+// -----------------------------------------------------------------------
+// internal
+
+/* words and lists */
+
+static
+void
+pushword(char *str)
+{
+ if(!runner->args)
+ fatal("attempt to push on empty argument stack\n");
+
+ runner->args->word = makeword(str, runner->args->word);
+}
+
+static
+void
+popword(void)
+{
+ Word *w;
+ if(!runner->args)
+ fatal("tried to pop word on empty argument stack\n");
+
+ w = runner->args->word;
+ if(!w)
+ fatal("tried to pop word but nothing there\n");
+
+ runner->args->word = w->link;
+ efree(w->str);
+ efree(w);
+}
+
+static
+Word*
+copywords(Word *a, Word *tail)
+{
+ Word *v = nil, **end;
+
+ for(end=&v; a; a = a->link,end=&(*end)->link)
+ *end = makeword(a->str, nil);
+ *end = tail;
+
+ return v;
+}
+
+static
+void
+freewords(Word *w)
+{
+ Word *n;
+ while(w){
+ efree(w->str);
+ n = w->link;
+ efree(w);
+ w = n;
+ }
+}
+
+static
+void
+freelist(Word *w)
+{
+ Word *n;
+ while(w){
+ n = w->link;
+ efree(w->str);
+ efree(w);
+ w = n;
+ }
+
+}
+
+static
+void
+pushlist(void)
+{
+ List *stack = emalloc(sizeof(*stack));
+
+ stack->word = nil;
+ stack->link = runner->args;
+
+ runner->args = stack;
+}
+
+static
+void
+poplist(void)
+{
+ List *stack = runner->args;
+ if(!stack)
+ fatal("attempted to pop an empty argument stack\n");
+
+ freelist(stack->word);
+ runner->args = stack->link;
+ efree(stack);
+}
+
+/* system interop */
+static
+Word*
+path(char *w)
+{
+ Word *path;
+
+ if(strncmp(w, "/", 1)==0
+ || strncmp(w, "./", 2)==0
+ || strncmp(w, "../", 3)==0
+ || (path = var("path")->val)==0)
+ path=&nullpath;
+
+ return path;
+}
+
+static inline
+void
+undoredirs(void)
+{
+ while(runner->redir.end != runner->redir.start)
+ Xpopredir();
+}
+
+static inline
+int
+exitsnext(void)
+{
+ Code *c = &runner->code.exe[runner->code.i];
+ while(c->f == Xpopredir)
+ c++;
+
+ return c->f == Xexit;
+}
+
+static inline
+void
+defaultsignal(void)
+{
+ signal(SIGINT, SIG_DFL);
+ signal(SIGQUIT, SIG_DFL);
+ signal(SIGTSTP, SIG_DFL);
+ signal(SIGTTIN, SIG_DFL);
+ signal(SIGTTOU, SIG_DFL);
+ signal(SIGCHLD, SIG_DFL);
+}
+
+static inline
+void
+setpid(Thread *job, int pid)
+{
+ job->pid = pid;
+ if(job->pgid <= 0){
+ job->pgid = pid;
+ addjob(job);
+ }
+
+ setpgid(pid, job->pgid);
+}
+
+/* fork/execute helpers */
+
+static inline
+void
+initchild(Thread *job, int fg)
+{
+ int pid = getpid();
+ setpid(job, pid);
+
+ if(job->flag.user){
+ if(fg)
+ tcsetpgrp(0, job->pgid);
+ else
+ job->flag.user = 0;
+ defaultsignal();
+ }
+
+ clearwait(job);
+}
+
+static inline
+void
+initparent(Thread *job, int pid, int fg)
+{
+ setpid(job, pid);
+
+ if(job->flag.user){
+ if(!fg){
+ tcsetpgrp(0, job->pgid);
+ job->flag.user = 0;
+ }
+ }
+ addwait(job, pid);
+}
+
+static
+void
+xx(void)
+{
+ popword(); // "exec"
+ if(!runner->args->word){
+ Xerror("empty argument list");
+ return;
+ }
+
+ redirect(runner->redir.end);
+ execute(runner->args->word, path(runner->args->word->str));
+ poplist();
+}
+
+static
+int
+xforkx(void)
+{
+ int n, pid;
+
+ switch(pid=fork()){
+ case -1:
+ Xerror("try again\n");
+ return -1;
+ case 0: // child
+ initchild(runner, 1);
+
+ pushword("exec");
+ xx();
+
+ exit(2); // NOTE: unreachable: xx does not return
+ default: // parent
+ initparent(runner, pid, 0);
+
+ return pid;
+ }
+}
+
+/* redirections */
+void
+pushredir(int type, int from, int to)
+{
+ Redir *r = emalloc(sizeof(*r));
+
+ r->type = type;
+ r->from = from;
+ r->to = to;
+
+ r->link = runner->redir.end, runner->redir.end = r;
+}
+
+/* byte code */
+static
+void
+run(Code *c, int pc, Var *local, int inherit)
+{
+ Thread *new = emalloc(sizeof(*new));
+
+ new->code.i = pc;
+ new->code.exe = copycode(c);
+
+ new->cmd.path = nil;
+ new->cmd.io = nil;
+
+ new->args = nil;
+ new->local = local;
+
+ new->flag.eof = 0;
+ if(runner){
+ new->pid = runner->pid;
+ new->flag.user = runner->flag.user;
+ new->redir.end = new->redir.start = runner->redir.end;
+ }else{
+ new->pid = shell.pid;
+ new->flag.user = shell.interactive;
+ new->redir.end = new->redir.start = nil;
+ }
+
+ new->wait.status = 0;
+ new->wait.len = 0;
+ new->wait.cap = 0;
+ new->wait.on = nil;
+
+ new->status = 0;
+ if(inherit)
+ new->pgid = runner->pgid;
+ else
+ new->pgid = -1;
+
+ new->line = 0;
+ new->caller = runner;
+ new->link = nil;
+
+ runner = new;
+}
+
+// -----------------------------------------------------------------------
+// exported builtins
+
+// XXX: find a better place for these
+Word*
+makeword(char *str, Word *link)
+{
+ Word *w = emalloc(sizeof(*w));
+
+ w->str = strdup(str);
+ w->link = link;
+
+ return w;
+}
+
+void
+freeword(Word *word)
+{
+ Word *n;
+
+ while(word){
+ efree(word->str);
+ n = word->link;
+ efree(word);
+ word = n;
+ }
+}
+
+int
+count(Word *w)
+{
+ int n;
+ for(n = 0; w; n++)
+ w = w->link;
+ return n;
+}
+
+// -----------------------------------------------------------------------
+// builtins
+
+void
+xecho(void)
+{
+ int fd;
+ Word *arg;
+ char *b, *s, buf[128];
+
+ fd = mapfd(1);
+ b = buf;
+
+ popword(); // echo
+
+ // TODO: controllable flags here
+ arg = runner->args->word;
+printword:
+ s = arg->str;
+ while(*s){
+ *b++ = *s++;
+ if(b == arrend(buf)-2) // always have 2 bytes available
+ write(fd, buf, arrlen(buf)-2), b = buf;
+ }
+
+ arg = arg->link;
+ if(arg){
+ *b++ = ' ';
+ goto printword;
+ }else{
+ *b++ = '\n';
+ *b++ = 0;
+ /* fallthrough */
+ }
+ write(fd, buf, b-buf);
+
+ poplist();
+}
+
+void
+xexit(void)
+{
+ Word *arg;
+
+ popword(); // exit
+ arg = runner->args->word;
+ switch(count(arg)){
+ default:
+ print(shell.err, "invalid number of arguments to exit, exiting anyways\n");
+ case 0:
+ Xexit();
+ }
+ /* unreachable */
+}
+
+void
+xcd(void)
+{
+ Word *arg;
+ Word *cdpath;
+ char dir[512];
+
+ popword(); // cd
+
+ arg = runner->args->word;
+ switch(count(arg)){
+ default:
+ print(shell.err, "usage: cd [directory]\n");
+ break;
+ case 0:
+ arg = var("home")->val;
+ if(count(arg) >= 1){
+ if(chdir(arg->str) < 0)
+ print(shell.err, "failed cd: %s\n", strerror(errno));
+ }else{
+ print(shell.err, "ambiguous cd: $home empty\n");
+ }
+ break;
+
+ case 1:
+ // TODO: add cdpath
+ cdpath = &nullpath;
+ for(; cdpath; cdpath = cdpath->link){
+ strcpy(dir, cdpath->str);
+ if(dir[0])
+ strcat(dir,"/");
+ strcat(dir, arg->str);
+ if(chdir(dir) < 0){
+ print(shell.err, "failed cd %s: %s\n", dir, strerror(errno));
+ }
+ break;
+ }
+ break;
+ }
+
+ poplist();
+}
+
+static Code dotcmd[14] =
+{
+ [0] = {.i = 0},
+ [1] = {.f = Xmark},
+ [2] = {.f = Xword},
+ [3] = {.s = "0"},
+ [4] = {.f = Xlocal},
+ [5] = {.f = Xmark},
+ [6] = {.f = Xword},
+ [7] = {.s = "*"},
+ [8] = {.f = Xlocal},
+ [9] = {.f = Xreadcmd},
+ [10] = {.f = Xunlocal},
+ [11] = {.f = Xunlocal},
+ [12] = {.f = Xreturn},
+};
+
+void
+xdot(void)
+{
+ Word *p;
+ List *argv;
+ char *base;
+ int fd, iflag = 0;
+ Thread *old;
+ char file[512];
+
+ popword(); // "."
+#if 0
+ if(proc->args->word && strcmp(proc->args->word->str, "-i")==0){
+ iflag = 1;
+ popword();
+ }
+#endif
+ /* get input file */
+ if(!runner->args->word){
+ Xerror("usage: . [-i] file [arg ...]\n");
+ return;
+ }
+
+ base = strdup(runner->args->word->str);
+ popword();
+ for(fd=-1, p=path(base); p; p = p->link){
+ strcpy(file, p->str);
+
+ if(file[0])
+ strcat(file, "/");
+ strcat(file, base);
+
+ if((fd = open(file, 0))>=0)
+ break;
+ }
+
+ if(fd<0){
+ print(shell.err, "failed open: %s: ", base);
+ return;
+ }
+ /* set up for a new command loop */
+ old = runner; // store pointer to old code
+ run(dotcmd, 1, nil, 0);
+
+ /* operations on new command stack */
+ pushredir(Rclose, fd, 0);
+ runner->cmd.path = base;
+ runner->cmd.io = openfd(fd);
+
+ /* push $* value */
+ pushlist();
+ runner->args->word = old->args->word;
+
+ /* free caller's copy of $* */
+ argv = old->args;
+ old->args = argv->link;
+ efree(argv);
+
+ /* push $0 value */
+ pushlist();
+ pushword(base);
+ //ndot++;
+}
+
+void
+xjob(void)
+{
+ int i;
+ Thread *job;
+
+ for(i=0, job = shell.jobs; job; job = job->link, i++)
+ report(job,i);
+
+ poplist();
+}
+
+void
+xfg(void)
+{
+ int i;
+ Thread *job, *old;
+
+ popword(); // fg
+
+ /* get input job id */
+ if(!runner->args->word){
+ print(shell.err, "usage: fg [pid|\%num]\n");
+ poplist();
+ return;
+ }
+
+ i = atoi(runner->args->word->str);
+ popword(); // [pid|num]
+
+ for(job=shell.jobs; i > 0; job=job->link, --i)
+ ;
+
+ poplist(); // this goes here?
+
+ wakeup(job);
+ job->caller = runner, runner = job; // XXX: can this leave zombies?
+ foreground(job, 1);
+}
+
+void
+xboot(int argc, char *argv[])
+{
+ int i;
+ Code bootstrap[32];
+ char num[12];
+
+ i = 0;
+ bootstrap[i++].i = 1;
+ bootstrap[i++].f = Xmark;
+ bootstrap[i++].f = Xword;
+ bootstrap[i++].s="*";
+ bootstrap[i++].f = Xassign;
+ bootstrap[i++].f = Xmark;
+ bootstrap[i++].f = Xmark;
+ bootstrap[i++].f = Xword;
+ bootstrap[i++].s="*";
+ bootstrap[i++].f = Xdollar;
+ bootstrap[i++].f = Xword;
+ bootstrap[i++].s = "/dev/stdin";
+ bootstrap[i++].f = Xword;
+ bootstrap[i++].s=".";
+ bootstrap[i++].f = Xbasic;
+ bootstrap[i++].f = Xexit;
+ bootstrap[i].i = 0;
+
+ run(bootstrap, 1, nil, 0);
+ runner->pid = runner->pgid = shell.pid;
+ pushlist(); // prime bootstrap argv
+
+ argv0 = strdup(argv[0]);
+ for(i = argc-1; i > 0; --i)
+ pushword(argv[i]);
+
+ /* main interpreter loop */
+ for(;;){
+ runner->code.i++;
+ (*runner->code.exe[runner->code.i-1].f)();
+ }
+}
+
+// -----------------------------------------------------------------------
+// exported interpreter bytecode
+
+void
+Xmark(void)
+{
+ pushlist();
+}
+
+void
+Xword(void)
+{
+ pushword(runner->code.exe[runner->code.i++].s);
+}
+
+void
+Xtrue(void)
+{
+ if(!runner->status){
+ assert(runner->wait.status == Pdone);
+ runner->code.i++;
+ deljob(runner);
+ runner->pgid = -1;
+ }else
+ runner->code.i = runner->code.exe[runner->code.i].i;
+}
+
+void
+Xfalse(void)
+{
+ if(runner->status){
+ assert(runner->wait.status == Pdone);
+ runner->code.i++;
+ deljob(runner);
+ runner->pgid = -1;
+ } else
+ runner->code.i = runner->code.exe[runner->code.i].i;
+}
+
+void
+Xgoto(void)
+{
+ runner->code.i = runner->code.exe[runner->code.i].i;
+}
+
+void
+Xfor(void)
+{
+ if(!runner->args->word){
+ poplist();
+ runner->code.i = runner->code.exe[runner->code.i].i;
+ }else{
+ freelist(runner->local->val);
+
+ runner->local->val = runner->args->word;
+ runner->local->new = 1;
+ runner->args->word = runner->args->word->link;
+
+ runner->local->val->link = nil;
+ runner->code.i++;
+ }
+
+}
+
+static
+Word*
+catlist(Word *l, Word *r, Word *tail)
+{
+ Word *w;
+ char *buf;
+
+ if(l->link || r->link)
+ tail = catlist( (!l->link)?l:l->link, (!r->link)?r:r->link, tail);
+
+ buf = emalloc(strlen(l->str)+strlen(r->str)+1);
+ strcpy(buf, l->str);
+ strcat(buf, r->str);
+
+ w = makeword(buf, tail);
+ efree(buf);
+
+ return w;
+}
+
+void
+Xconcatenate(void)
+{
+ int rn, ln;
+ Word *l = runner->args->word;
+ Word *r = runner->args->link->word;
+ Word *w = runner->args->link->link->word;
+
+ ln = count(l), rn = count(r);
+ if(ln != 0 || rn != 0) {
+ if(ln == 0 || rn == 0){
+ Xerror("null list in concatenation\n");
+ return;
+ }
+ if(ln != 1 && rn != 1 && ln != rn) {
+ Xerror("mismatched list lengths in concatenation\n");
+ return;
+ }
+ w = catlist(l, r, w);
+ }
+
+ poplist();
+ poplist();
+ runner->args->word = w;
+}
+
+void
+Xdollar(void)
+{
+ int n;
+ char *s, *t;
+ Word *a, *star;
+
+ if(count(runner->args->word)!=1){
+ Xerror("variable name not singleton!\n");
+ return;
+ }
+ s = runner->args->word->str;
+ // deglob(s);
+ n = 0;
+
+ for(t = s;'0'<=*t && *t<='9';t++)
+ n = n*10+*t-'0';
+
+ a = runner->args->link->word;
+
+ if(n==0 || *t)
+ a = copywords(var(s)->val, a);
+ else{
+ star = var("*")->val;
+ if(star && 1<=n && n<=count(star)){
+ while(--n)
+ star = star->link;
+
+ a = makeword(star->str, a);
+ }
+ }
+
+ poplist();
+ runner->args->word = a;
+}
+
+static
+Word*
+cpwords(Word *array, Word *tail, int n)
+{
+ Word *cp, **end;
+
+ cp = nil, end = &cp;
+ while(n-- > 0){
+ *end = makeword(array->str, nil);
+ end = &(*end)->link;
+ array = array->link;
+ }
+ *end = tail;
+
+ return cp;
+}
+
+
+static
+Word*
+getindex(Word *array, int len, Word *index, Word *tail)
+{
+ char *s;
+ int n, m;
+ if(!index)
+ return tail;
+
+ tail = getindex(array, len, index->link, tail);
+
+ s = index->str;
+ //deglob(s)
+
+ m = 0, n = 0;
+ while('0' <= *s && *s <= '9')
+ n = 10*n + (*s++ - '0');
+ if(*s == '-'){
+ if(*++s == 0)
+ m = len - n;
+ else{
+ while('0' <= *s && *s <= '9')
+ m = 10*m + (*s++ - '0');
+ m -= n;
+ }
+ }
+
+ if(n<1 || n > len || m < 0)
+ return tail;
+ if(n+m > len)
+ m = len-n;
+ while(--n > 0)
+ array = array->link;
+ return cpwords(array, tail, m+1);
+}
+
+void
+Xindex(void)
+{
+ char *s;
+ Word *val, *ret;
+
+ if(count(runner->args->word) != 1){
+ Xerror("variable name not a singleton");
+ return;
+ }
+ s = runner->args->word->str;
+ //deglob(s)
+ val = var(s)->val;
+ poplist();
+
+ ret = runner->args->link->word; // pointer to next stack frame
+ ret = getindex(val, count(val), runner->args->word, ret);
+ poplist();
+
+ // push result back on stack
+ runner->args->word = ret;
+}
+
+void
+Xjoin(void)
+{
+ int n;
+ char *s;
+ Word *arg, *elt;
+
+ if(count(runner->args->word) != 1){
+ Xerror("variable name is not singleton\n");
+ return;
+ }
+
+ s = runner->args->word->str;
+ // deglob(s)
+
+ arg = var(s)->val;
+ poplist();
+
+ n = count(arg);
+ if(n==0){
+ pushword("");
+ return;
+ }
+
+ for(elt = arg; elt; elt=elt->link)
+ n += strlen(elt->str);
+
+ s = emalloc(n);
+ if(arg){
+ strcpy(s, arg->str);
+ for(elt = arg->link; elt; elt = elt->link){
+ strcat(s, " ");
+ strcat(s, elt->str);
+ }
+ }else
+ s[0] = 0;
+
+ pushword(s);
+ efree(s);
+}
+
+void
+Xassign(void)
+{
+ Var *v;
+
+ if(count(runner->args->word)!=1){
+ Xerror("variable name not singleton!\n");
+ return;
+ }
+ //deglob(runq->argv->words->word);
+ v = var(runner->args->word->str);
+ poplist();
+
+ //globlist();
+ freewords(v->val);
+ v->val = runner->args->word;
+ v->new = 1;
+ if(v->update)
+ v->update(v);
+
+ runner->args->word = nil;
+ poplist();
+}
+
+void
+Xreadcmd(void)
+{
+ Thread *root;
+ Word *prompt;
+
+ flush(shell.err);
+ root = runner;
+
+ resetprompt();
+
+ if(yyparse()){
+ // resource cleanup?
+ if(runner->flag.eof)
+ Xreturn();
+ else
+ --root->code.i;
+ }else{
+ --root->code.i; /* re-execute Xreadcmd after codebuf runs */
+ run(compiled, 1, root->local, 0);
+ }
+
+ killzombies();
+ freeparsetree();
+}
+
+void
+Xlocal(void)
+{
+ if(count(runner->args->word)!=1){
+ Xerror("variable name must be singleton\n");
+ return;
+ }
+ //deglob(shell->args->word->str);
+
+ runner->local = makevar(strdup(runner->args->word->str), runner->local);
+ runner->local->val = copywords(runner->args->link->word, nil);
+ runner->local->new = 1;
+
+ poplist();
+ poplist();
+}
+
+void
+Xunlocal(void)
+{
+ Var *v = runner->local, *hide;
+ if(!v)
+ fatal("Xunlocal: no locals!\n", 0);
+
+ runner->local = v->link;
+ hide = var(v->name);
+ hide->new = 1;
+
+ efree(v->name);
+ freewords(v->val);
+ efree(v);
+}
+
+void
+Xasync(void)
+{
+ int pid;
+ /*
+ int null = open("/dev/null", 0);
+ if(!null){
+ Xerror("can not open /dev/null\n");
+ return;
+ }
+ */
+
+ switch(pid=fork()){
+ case -1:
+ // close(null);
+ Xerror("fork failed: try again");
+ break;
+
+ case 0: // child in background
+ initchild(runner,0);
+ /* pushredir(Ropen, null, 0); */
+
+ run(runner->code.exe, runner->code.i+1, runner->local, 0);
+ runner->caller = nil;
+ runner->flag.user = 0;
+ break;
+
+ default: // parent in foreground
+ initparent(runner,pid,1);
+ // close(null);
+
+ runner->code.i = runner->code.exe[runner->code.i].i; /* jump to end of async command */
+ /* don't wait: continue running */
+ }
+}
+
+void
+Xsubshell(void)
+{
+ int pid, user;
+
+ user = runner->flag.user;
+ switch(pid=fork()){
+ case -1:
+ Xerror("fork failed: try again");
+ break;
+
+ case 0: // child
+ initchild(runner, 1);
+ run(runner->code.exe, runner->code.i+1, runner->local, 1);
+ runner->caller = nil;
+ break;
+
+ default: // parent
+ initparent(runner, pid, 0); // relinquish control
+ waitfor(runner, pid); // wait until child finishes
+ if(user){
+ tcsetpgrp(0, shell.pid);
+ runner->flag.user = 1; // take control
+ }
+
+ runner->code.i = runner->code.exe[runner->code.i].i; // jump to end of subshell command and continue execution
+ }
+}
+
+void
+Xpipewait(void)
+{
+ foreground(runner, 0);
+}
+
+void
+Xpipe(void)
+{
+ Thread *orig;
+ int pc, pid, lfd, rfd, pfd[2];
+
+ orig = runner;
+ pc = orig->code.i;
+ lfd = orig->code.exe[pc++].i;
+ rfd = orig->code.exe[pc++].i;
+
+ if(pipe(pfd)<0){
+ Xerror("can't get pipe\n");
+ return;
+ }
+
+ switch(pid=fork()){
+ case -1:
+ Xerror("try again");
+ break;
+ case 0: // child
+ initchild(runner,1);
+
+ /* child 0 (writer) forked process */
+ run(runner->code.exe, pc+2, runner->local, 1);
+ runner->caller = nil;
+
+ close(pfd[0]);
+ pushredir(Ropen, pfd[1], lfd);
+ break;
+
+ default: // parent
+ initparent(runner,pid,0);
+
+ /* child 1 (reader) subprocess*/
+ run(runner->code.exe, runner->code.exe[pc].i, runner->local, 1);
+
+ close(pfd[1]);
+ pushredir(Ropen, pfd[0], rfd);
+
+ orig->code.i = orig->code.exe[pc+1].i;
+ break;
+ }
+}
+
+void
+Xbasic(void)
+{
+ Var *v;
+ Word *arg;
+ int pid, status;
+ struct Builtin *b;
+
+ arg = runner->args->word;
+ if(!arg){
+ Xerror("empty argument list\n");
+ return;
+ }
+
+ v = var(arg->str);
+ if(v->func){
+ return;
+ }
+
+ // see if it matches a builtin
+ for(b = builtin; b->name; b++){
+ if(strcmp(b->name, arg->str)==0){
+ b->func();
+ return;
+ }
+ }
+
+ /* if we are here then it's an external command */
+ if(exitsnext()){ // if we exit immediately, no need to fork
+ pushword("exec");
+ xx();
+ Xexit();
+ }
+
+ // run the external command
+ if((pid = xforkx()) < 0) {
+ Xerror("try again");
+ return;
+ }
+
+ poplist();
+ foreground(runner, 0); // waits for child
+}
+
+void
+Xcount(void)
+{
+ Word *arg;
+ char *str, num[12];
+
+ if(count(runner->args->word) != 1){
+ Xerror("variable name not a singleton\n");
+ return;
+ }
+
+ str = runner->args->word->str;
+ arg = var(str)->val;
+ poplist();
+
+ itoa(num, count(arg));
+ pushword(num);
+}
+
+void
+Xflat(void)
+{
+ int len;
+ char *str;
+ Word *arg, *a;
+
+ if(count(runner->args->word)!=1){
+ Xerror("variable name is not a singleton\n");
+ return;
+ }
+
+ str = runner->args->word->str;
+ arg = var(str)->val;
+ poplist();
+
+ len = count(arg);
+ if(!len){
+ pushword("");
+ return;
+ }
+
+ for(a=arg; a; a=a->link)
+ len += strlen(a->str);
+
+ str = emalloc(len);
+ if(arg){
+ strcpy(str, arg->str);
+ for(a = arg->link; a; a = a->link){
+ strcat(str," ");
+ strcat(str,a->str);
+ }
+ }else
+ str[0] = 0;
+
+ pushword(str);
+ efree(str);
+}
+
+void
+Xbang(void)
+{
+ if(runner->status)
+ runner->status = 0;
+ else
+ runner->status = 1;
+}
+
+void
+Xpopredir(void)
+{
+ Redir *r = runner->redir.end;
+ if(!r)
+ fatal("attempted to pop a nil redir\n");
+
+ runner->redir.end = runner->redir.end->link;
+ if(r->type==Ropen)
+ close(r->from);
+
+ efree(r);
+}
+
+void
+Xreturn(void)
+{
+ Thread *curr = runner;
+
+ switch(curr->wait.status){
+ /*
+ * If our job is still running or suspended we must:
+ * 1. move program one step back to rerun Xreturn upon recall
+ * 2. return to our calling thread
+ * 3. don't free!
+ */
+ case Prun:
+ report(curr, 0);
+ curr->flag.user = 0;
+ case Pstop:
+ curr->code.i--;
+ runner = curr->caller;
+ curr->caller = nil; // detach job
+ return;
+ /*
+ * If our job has finished:
+ * 1. remove from our list
+ * 2. continue to clean up its memory
+ */
+ case Pdone:
+ deljob(curr);
+ /* fallthrough */
+ default:
+ ;
+ }
+
+ undoredirs();
+
+ while(curr->args)
+ poplist();
+ freecode(curr->code.exe);
+ efree(curr->wait.on);
+
+ runner = curr->caller;
+ efree(curr);
+ if(!runner)
+ exit(0);
+}
+
+void
+Xexit(void)
+{
+ exit(runner->status);
+}
+
+void
+Xerror(char *msg)
+{
+ print(shell.err, "rc: %s", msg);
+ flush(shell.err);
+ while(!runner->flag.user)
+ Xreturn();
+}
+
diff --git a/src/cmd/rc/exec.h b/src/cmd/rc/exec.h
new file mode 100644
index 0000000..a3a6ae9
--- /dev/null
+++ b/src/cmd/rc/exec.h
@@ -0,0 +1,47 @@
+#pragma once
+
+/*
+ * opcode routines
+ * arguments on stack (...)
+ * arguments in line [...]
+ * code in line with jump around {...}
+ */
+
+void Xmark(void); // Xmark marks stack location for word
+void Xindex(void); // Xindex
+void Xlocal(void); // Xlocal(name,val) create local variable, assign value
+void Xunlocal(void); // Xunlocal delete local variable
+void Xdollar(void); // Xdollar(name) get value of name
+void Xtrue(void); // Xtrue{...} execute {} if true
+void Xfalse(void); // Xfalse{...} execute {} if false
+void Xgoto(void); // Xgoto[addr] goto address
+void Xfor(void); // Xfor(var, list){... Xreturn}
+void Xreadcmd(void); //
+void Xassign(void);
+void Xbang(void);
+void Xasync(void);
+void Xbasic(void); // Xbasic(args) run command and wait for result
+void Xsubshell(void);
+void Xword(void);
+void Xjoin(void);
+void Xconcatenate(void);
+void Xcount(void);
+void Xflat(void);
+void Xpipe(void);
+void Xpipewait(void);
+void Xpopredir(void);
+
+void Xreturn(void);
+void Xexit(void);
+
+void Xerror(char*);
+
+/* builtin commands */
+void xcd(void);
+void xdot(void);
+void xecho(void);
+void xexit(void);
+void xfg(void);
+void xjob(void);
+
+void xboot(int argc, char *argv[]);
diff --git a/src/cmd/rc/input.c b/src/cmd/rc/input.c
new file mode 100644
index 0000000..cc2383d
--- /dev/null
+++ b/src/cmd/rc/input.c
@@ -0,0 +1,1679 @@
+#include "rc.h"
+
+#include <termios.h>
+#include <sys/ioctl.h>
+
+/* don't change order of these without modifying matrix */
+enum
+{
+ NonPrintable,
+ Alnum,
+ Punctuation,
+ Space
+};
+
+static int ascii[256] =
+{
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 3, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 2,
+ 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 1,
+ 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 2, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+ 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+};
+
+struct Mode
+{
+ ushort raw : 1;
+ ushort multiline : 1;
+ ushort mask : 1;
+ ushort defer : 1;
+ struct {
+ ushort on : 1;
+ ushort insert : 1;
+ } vi ;
+};
+
+/*
+ * the structure represents the state during line editing.
+ * we pass this state to functions implementing specific editing functionalities
+ */
+struct TerminalState
+{
+ int ifd; /* terminal stdin file descriptor. */
+ int ofd; /* terminal stdout file descriptor. */
+
+ struct{
+ char *s; /* raw UTF-8 bytes */
+ int len; /* number of bytes in prompt */
+ int size; /* number of (printed) runes in prompt */
+ } prompt;
+
+ struct{
+ intptr cap; /* capacity of edit buffer */
+ intptr len; /* current number of bytes stored */
+ intptr pos; /* position within edit buffer */
+ char *buf;
+ } edit; /* edit buffer */
+
+ struct{
+ intptr cap; /* number of columns in terminal */
+ intptr len; /* current edited line length (in runes) */
+ intptr pos; /* current cursor position (in runes) */
+ intptr old; /* previous refresh cursor position (in runes) */
+ } cursor;
+
+ struct{
+ intptr cap;
+ intptr len;
+ char *buf;
+ } yank; /* yank buffer */
+
+ intptr maxrows; /* maximum num of rows used so far (multiline mode) */
+ intptr history; /* index of history we are currently editing */
+};
+
+/*
+ * line history (circular buffer)
+ */
+struct History
+{
+ char **bot, **top, *entry[1024];
+};
+
+/* globals */
+static struct Mode mode;
+static struct History history;
+static struct termios originalterm;
+
+enum
+{
+ KeyNil = 0, /* nil */
+ KeyCtrlA = 1, /* Ctrl+a */
+ KeyCtrlB = 2, /* Ctrl-b */
+ KeyCtrlC = 3, /* Ctrl-c */
+ KeyCtrlD = 4, /* Ctrl-d */
+ KeyCtrlE = 5, /* Ctrl-e */
+ KeyCtrlF = 6, /* Ctrl-f */
+ KeyCtrlH = 8, /* Ctrl-h */
+ KeyTab = 9, /* Tab */
+ KeyCtrlK = 11, /* Ctrl+k */
+ KeyCtrlL = 12, /* Ctrl+l */
+ KeyEnter = 13, /* Enter */
+ KeyCtrlN = 14, /* Ctrl-n */
+ KeyCtrlP = 16, /* Ctrl-p */
+ KeyCtrlT = 20, /* Ctrl-t */
+ KeyCtrlU = 21, /* Ctrl+u */
+ KeyCtrlW = 23, /* Ctrl+w */
+ KeyEsc = 27, /* Escape */
+ KeyBackspace = 127 /* Backspace */
+};
+
+static void doatexit(void);
+
+/* vi operations */
+typedef struct
+{
+ intptr buffer;
+ intptr cursor;
+} Position;
+
+typedef Position (*Noun)(struct TerminalState*, int);
+typedef void (*Verb)(struct TerminalState*, Position);
+
+static
+int
+runetype(rune r)
+{
+ if(r<128)
+ return ascii[r];
+ if(utf8·isspace(r))
+ return Space;
+ if(utf8·isdigit(r) || utf8·isalpha(r))
+ return Alnum;
+ if(utf8·ispunct(r))
+ return Punctuation;
+
+ return NonPrintable;
+}
+
+static
+void
+normalcursor(int fd)
+{
+ write(fd,"\e[2 q",5);
+}
+
+static
+void
+insertcursor(int fd)
+{
+ write(fd,"\e[6 q",5);
+}
+
+/* raw mode: 1960 magic shit. */
+static
+int
+enterraw(int fd)
+{
+ struct termios raw;
+
+ if(!shell.interactive)
+ goto fatal;
+
+ if(!mode.defer){
+ atexit(doatexit);
+ mode.defer = 1;
+ }
+ if(tcgetattr(fd,&originalterm) == -1)
+ goto fatal;
+
+ raw = originalterm; /* modify the original mode */
+
+ /* input modes: no break, no CR to NL, no parity check, no strip char,
+ * no start/stop output control. */
+ raw.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON);
+ /* output modes - disable post processing */
+ raw.c_oflag &= ~(OPOST);
+ /* control modes - set 8 bit chars */
+ raw.c_cflag |= (CS8);
+ /* local modes - choing off, canonical off, no extended functions,
+ * no signal chars (^Z,^C) */
+ raw.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG);
+ /* control chars - set return condition: min number of bytes and timer.
+ * We want read to return every single byte, without timeout. */
+ raw.c_cc[VMIN] = 1; raw.c_cc[VTIME] = 0; /* 1 byte, no timer */
+
+ /* put terminal in raw mode after flushing */
+ if(tcsetattr(fd,TCSAFLUSH,&raw) < 0)
+ goto fatal;
+
+ mode.raw = 1;
+ return 1;
+
+fatal:
+ errno = ENOTTY;
+ return 0;
+}
+
+static
+void
+exitraw(int fd)
+{
+ /* don't even check the return value as it's too late. */
+ if(mode.raw && tcsetattr(fd,TCSAFLUSH,&originalterm) != -1)
+ mode.raw = 0;
+}
+
+/* use the esc [6n escape sequence to query the horizontal cursor position
+ * and return it. on error -1 is returned, on success the position of the
+ * cursor. */
+static
+int
+cursorposition(int ifd, int ofd)
+{
+ char buf[32];
+ int cols, rows;
+ unsigned int i = 0;
+
+ /* Report cursor location */
+ if(write(ofd, "\x1b[6n", 4) != 4)
+ return -1;
+
+ /* Read the response: ESC [ rows ; cols R */
+ while(i < sizeof(buf)-1) {
+ if(read(ifd,buf+i,1) != 1)
+ break;
+ if(buf[i] == 'R')
+ break;
+ i++;
+ }
+ buf[i] = '\0';
+
+ /* Parse it. */
+ if(buf[0] != KeyEsc || buf[1] != '[')
+ return -1;
+ if(sscanf(buf+2,"%d;%d",&rows,&cols) != 2)
+ return -1;
+
+ return cols;
+}
+
+/* try to get the number of columns in the current terminal, or assume 80 if it fails. */
+static
+int
+columns(int ifd, int ofd)
+{
+ struct winsize ws;
+
+ if(ioctl(1, TIOCGWINSZ, &ws) == -1 || ws.ws_col == 0){
+ /* ioctl() failed. Try to query the terminal itself. */
+ int start, cols;
+
+ /* Get the initial position so we can restore it later. */
+ start = cursorposition(ifd,ofd);
+ if(start == -1)
+ goto failed;
+
+ /* Go to right margin and get position. */
+ if(write(ofd,"\x1b[999C",6) != 6)
+ goto failed;
+ cols = cursorposition(ifd,ofd);
+ if(cols == -1)
+ goto failed;
+
+ /* Restore position. */
+ if(cols > start){
+ char esc[32];
+ snprintf(esc,32,"\x1b[%dD",cols-start);
+ if(write(ofd,esc,strlen(esc)) == -1)
+ ;
+ }
+ return cols;
+ }else
+ return ws.ws_col;
+
+failed:
+ return 80;
+}
+
+static
+void
+clear(void)
+{
+ if(write(1,"\x1b[H\x1b[2J",7) <= 0)
+ ;
+}
+
+/* beep: used for completion when there is nothing to complete or when all
+ * the choices were already shown. */
+static
+void
+beep(void)
+{
+ fprintf(stderr, "\x7");
+ fflush(stderr);
+}
+
+// -----------------------------------------------------------------------
+// command history
+
+void
+inithistory(void)
+{
+ history.bot = history.top = history.entry;
+}
+
+int
+addhistory(char *line)
+{
+ char *copy;
+
+ copy = strdup(line);
+ if(!copy)
+ return 0;
+
+ *history.top++ = copy;
+ if(history.top == arrend(history.entry))
+ history.top = history.entry;
+
+ if(history.top == history.bot){
+ efree(history.bot);
+ history.bot++;
+ }
+
+ return 1;
+}
+
+static
+void
+pophistory(void)
+{
+ if(--history.top < history.entry)
+ history.top = arrend(history.entry)-1;
+ efree(*history.top);
+}
+
+static void refreshline(struct TerminalState *);
+
+static
+char **
+currenthistory(struct TerminalState *term, intptr *size)
+{
+ char **entry;
+ intptr len, head;
+
+ if(history.top > history.bot){
+ len = history.top - history.bot;
+ entry = history.top - term->history - 1;
+ }else if(history.top < history.bot){
+ len = (arrend(history.entry) - history.bot) + (history.top - history.entry);
+ if((head=history.top - history.entry) < term->history)
+ entry = arrend(history.entry) - head;
+ else
+ entry = history.top - term->history - 1;
+ }else
+ return nil;
+
+ *size = len;
+ return entry;
+}
+
+static
+void
+usehistory(struct TerminalState *term, int d)
+{
+ rune r;
+ intptr w, len;
+ char *b, *e, **entry;
+
+ if(!(entry = currenthistory(term, &len)))
+ return;
+
+ efree(*entry);
+ *entry = strdup(term->edit.buf);
+
+ term->history += d;
+ if(term->history < 0){
+ term->history = 0;
+ return;
+ }else if(term->history >= len){
+ term->history = len - 1;
+ return;
+ }
+ entry = currenthistory(term, &len);
+
+ strncpy(term->edit.buf, *entry, term->edit.cap);
+ term->edit.buf[term->edit.cap-1] = 0;
+
+ /* update cursor/buffer positions */
+ term->edit.len = term->edit.pos = strlen(term->edit.buf);
+ for(w=0, b=term->edit.buf, e=term->edit.buf+term->edit.len; b < e; ){
+ b += utf8·decode(b, &r);
+ w += utf8·runewidth(r);
+ }
+ term->cursor.len = term->cursor.pos = w;
+
+ refreshline(term);
+}
+
+// -----------------------------------------------------------------------
+// line editing
+
+/*
+ * we define a very simple "append buffer" structure, that is an heap
+ * allocated string where we can append to. this is useful in order to
+ * write all the escape sequences in a buffer and flush them to the standard
+ * output in a single call, to avoid flickering effects.
+ */
+
+struct Buffer
+{
+ int len;
+ char *b;
+};
+
+static
+void
+initbuffer(struct Buffer *ab)
+{
+ ab->b = nil;
+ ab->len = 0;
+}
+
+static
+void
+append(struct Buffer *ab, const char *s, int len)
+{
+ char *new = realloc(ab->b,ab->len+len);
+
+ if (new == nil) return;
+ memcpy(new+ab->len,s,len);
+ ab->b = new;
+ ab->len += len;
+}
+
+static
+void
+freebuffer(struct Buffer *ab)
+{
+ free(ab->b);
+}
+
+/* single line low level line refresh.
+ *
+ * rewrite the currently edited line accordingly to the buffer content,
+ * cursor position, and number of columns of the terminal. */
+static
+void
+refreshsingleline(struct TerminalState *term)
+{
+ char esc[64];
+ struct Buffer ab;
+
+ int n, w;
+ rune r;
+ int fd = term->ofd;
+ intptr off = term->prompt.size;
+ char *buf = term->edit.buf;
+ intptr len = term->edit.len;
+ intptr pos = term->cursor.pos;
+ intptr col = term->cursor.len;
+
+ while((off+pos) >= term->cursor.cap){
+ n = utf8·decode(buf, &r);
+ w = utf8·runewidth(r);
+
+ buf+=n, len-=n;
+ pos-=w, col-=w;
+ }
+
+ assert(buf <= term->edit.buf + len);
+
+ while(off+col > term->cursor.cap){
+ n = utf8·decodeprev(buf+len-1, &r);
+ w = utf8·runewidth(r);
+
+ len-=n, col-=w;
+ }
+ assert(len >= 0);
+
+ initbuffer(&ab); // TODO: do we need so much malloc pressure?
+
+ /* move cursor to left edge */
+ snprintf(esc,64,"\r");
+ append(&ab,"\r",1);
+
+ /* write the prompt and the current buffer content */
+ append(&ab, term->prompt.s, term->prompt.len);
+
+ if(mode.mask == 1)
+ while(len--)
+ append(&ab,"*",1);
+ else
+ append(&ab,buf,len);
+
+ snprintf(esc,64,"\x1b[0K"); // erase to right
+ append(&ab,esc,strlen(esc));
+
+ snprintf(esc,64,"\r\x1b[%dC", (int)(off+pos)); // move cursor to original position
+ append(&ab,esc,strlen(esc));
+
+ if(write(fd,ab.b,ab.len) == -1) /* can't recover from write error. */
+ ;
+
+ freebuffer(&ab);
+}
+
+/* multi line low level line refresh.
+ *
+ * Rewrite the currently edited line accordingly to the buffer content,
+ * cursor position, and number of columns of the terminal. */
+static
+void
+refreshmultilines(struct TerminalState *term)
+{
+#if 0
+ char esc[64];
+ int plen = term->plen;
+ int rows = (plen+term->len+term->cols-1)/term->cols; /* rows used by current buf. */
+ int rpos = (plen+term->oldpos+term->cols)/term->cols; /* cursor relative row. */
+ int rpos2; /* rpos after refresh. */
+ int col; /* colum position, zero-based. */
+ int i;
+ int old_rows = term->maxrows;
+ int fd = term->ofd, j;
+ struct Buffer ab;
+
+ /* Update maxrows if needed. */
+ if(rows > (int)term->maxrows)
+ term->maxrows = rows;
+
+ /* First step: clear all the lines used before. To do so start by
+ * going to the last row. */
+ initbuffer(&ab);
+ if(old_rows-rpos > 0){
+ snprintf(esc,64,"\x1b[%dB", old_rows-rpos);
+ append(&ab,esc,strlen(esc));
+ }
+
+ /* Now for every row clear it, go up. */
+ for(j = 0; j < old_rows-1; j++){
+ snprintf(esc,64,"\r\x1b[0K\x1b[1A");
+ append(&ab,esc,strlen(esc));
+ }
+
+ /* clean the top line. */
+ snprintf(esc,64,"\r\x1b[0K");
+ append(&ab,esc,strlen(esc));
+
+ /* Write the prompt and the current buffer content */
+ append(&ab,term->prompt,strlen(term->prompt));
+ if(mode.mask == 1){
+ for(i = 0; i < term->len; i++) append(&ab,"*",1);
+ }else
+ append(&ab,term->buf,term->len);
+
+ /* If we are at the very end of the screen with our prompt, we need to
+ * emit a newline and move the prompt to the first column. */
+ if(term->pos && term->pos == term->len && (term->pos+plen) % term->cols == 0) {
+ append(&ab,"\n",1);
+ snprintf(esc,64,"\r");
+ append(&ab,esc,strlen(esc));
+ rows++;
+ if(rows > (int)term->maxrows)
+ term->maxrows = rows;
+ }
+
+ /* Move cursor to right position. */
+ rpos2 = (plen+term->pos+term->cols)/term->cols; /* current cursor relative row. */
+
+ /* Go up till we reach the expected positon. */
+ if(rows-rpos2 > 0){
+ snprintf(esc,64,"\x1b[%dA", rows-rpos2);
+ append(&ab,esc,strlen(esc));
+ }
+
+ /* Set column. */
+ col = (plen+(int)term->pos) % (int)term->cols;
+ if(col)
+ snprintf(esc,64,"\r\x1b[%dC", col);
+ else
+ snprintf(esc,64,"\r");
+ append(&ab,esc,strlen(esc));
+
+ term->oldpos = term->pos;
+
+ if(write(fd,ab.b,ab.len) == -1) /* Can't recover from write error. */
+ ;
+
+ freebuffer(&ab);
+#endif
+}
+
+/* Calls the two low level functions refreshSingleLine() or
+ * refreshMultiLine() according to the selected mode. */
+static
+void
+refreshline(struct TerminalState *term)
+{
+ if(mode.multiline)
+ refreshmultilines(term);
+ else
+ refreshsingleline(term);
+}
+
+/* insert the rune 'c' at cursor current position.
+ * on error writing to the terminal -1 is returned, otherwise 0. */
+int
+insertrune(struct TerminalState *term, int n, char *c)
+{
+ int w;
+ rune r;
+
+ utf8·decode(c, &r);
+ w = utf8·runewidth(r);
+
+ if(term->edit.len + n <= term->edit.cap){
+ if(term->edit.pos == term->edit.len){
+ memcpy(term->edit.buf+term->edit.pos, c, n);
+
+ term->edit.pos += n, term->edit.len += n;
+ term->cursor.pos += w, term->cursor.len += w;
+
+ term->edit.buf[term->edit.len] = '\0';
+
+ if(!mode.multiline && ((term->prompt.size+term->cursor.pos+n) <= term->cursor.cap)){
+ if(mode.mask){
+ c = "*";
+ n = 1;
+ }
+ if(write(term->ofd, c, n) == -1)
+ return 0;
+ }
+ refreshline(term);
+ }else{
+ memmove(term->edit.buf+term->edit.pos+n, term->edit.buf+term->edit.pos, term->edit.len-term->edit.pos);
+ memcpy(term->edit.buf+term->edit.pos, c, n);
+
+ term->edit.pos += n, term->edit.len += n;
+ term->cursor.pos += w, term->cursor.len += w;
+
+ term->edit.buf[term->edit.len] = '\0';
+ refreshline(term);
+ }
+ }
+
+ return 1;
+}
+
+int
+insertbytes(struct TerminalState *term, int len, char *buf)
+{
+ int nr;
+ if(term->edit.len + len > term->edit.cap){
+ len = term->edit.cap - term->edit.len;
+ buf[len] = 0;
+ }
+ nr = utf8·len(buf);
+
+ if(term->edit.pos == term->cursor.len){
+ memcpy(term->edit.buf+term->edit.len, buf, len);
+
+ term->edit.pos += len, term->edit.len += len;
+ term->cursor.pos += nr, term->cursor.len += nr;
+
+ // XXX: transfer the modeline here?
+ term->edit.buf[term->edit.len] = '\0';
+ refreshline(term);
+ }else{
+ memmove(term->edit.buf+term->edit.pos+len,term->edit.buf+term->edit.pos,term->edit.len-term->edit.pos);
+ memcpy(term->edit.buf+term->edit.pos, buf, len);
+
+ term->edit.pos += len, term->edit.len += len;
+ term->cursor.pos += nr, term->cursor.len += nr;
+
+ term->edit.buf[term->edit.len] = '\0';
+ refreshline(term);
+ }
+
+ return 1;
+}
+
+// -----------------------------------------------------------------------
+// vi functionality
+
+/* modes */
+
+static
+void
+normalmode(int fd)
+{
+ mode.vi.insert = 0;
+ normalcursor(fd);
+}
+
+static
+void
+insertmode(int fd)
+{
+ mode.vi.insert = 1;
+ insertcursor(fd);
+}
+
+/* actions */
+
+static
+void
+move(struct TerminalState *term, Position to)
+{
+ if(to.buffer != term->edit.pos){
+ term->edit.pos = to.buffer;
+ term->cursor.pos = to.cursor;
+ refreshline(term);
+ }
+}
+
+static
+void
+yank(struct TerminalState *term, Position to)
+{
+ intptr len, off;
+
+ if(to.buffer == term->edit.pos)
+ return; // noop
+
+ if(to.buffer > term->edit.pos){
+ len = to.buffer - term->edit.pos;
+ off = term->edit.pos;
+ }else{
+ len = term->edit.pos - to.buffer;
+ off = to.buffer;
+ }
+
+ if(term->yank.cap < len+1){
+ efree(term->yank.buf);
+ term->yank.cap = len+1;
+ term->yank.buf = emalloc(len+1);
+ }
+ term->yank.len = len;
+ memcpy(term->yank.buf, term->edit.buf+off, len);
+ term->yank.buf[len] = 0;
+}
+
+static
+void
+delete(struct TerminalState *term, Position to)
+{
+ intptr diff;
+
+ // delete characters in front of us (exclusive)
+ if(to.buffer > term->edit.pos){
+ diff = to.buffer - term->edit.pos;
+ memmove(term->edit.buf+term->edit.pos, term->edit.buf+to.buffer, term->edit.len-to.buffer+1);
+ term->edit.len -= diff;
+
+ diff = to.cursor - term->cursor.pos;
+ goto refresh;
+ }
+
+ // delete characters behind us
+ if(to.buffer < term->edit.pos){
+ diff = term->edit.pos - to.buffer;
+ memmove(term->edit.buf+to.buffer, term->edit.buf+term->edit.pos, term->edit.len-term->edit.pos+1);
+ term->edit.pos = to.buffer;
+ term->edit.len -= diff;
+
+ diff = term->cursor.pos - to.cursor;
+ term->cursor.pos = to.cursor;
+ goto refresh;
+ }
+ // do nothing
+ return;
+
+refresh:
+ term->cursor.len -= diff;
+ refreshline(term);
+}
+/* movements */
+
+#define CURRENT(term) (Position){ .buffer=(term)->edit.pos, .cursor=(term)->cursor.pos };
+
+// move cursor to the left n boxes
+static
+Position
+left(struct TerminalState *term, int n)
+{
+ rune r;
+ int w, d;
+ Position pos = CURRENT(term);
+ char *buf = term->edit.buf + term->edit.pos;
+
+ d = 0;
+ while(n > 0 && buf > term->edit.buf){
+ buf -= utf8·decodeprev(buf-1, &r);
+
+ w = utf8·runewidth(r);
+ n -= w;
+ d += w;
+ }
+
+ pos.cursor = MAX(pos.cursor-d, 0);
+ pos.buffer = MAX(buf-term->edit.buf, 0);
+ return pos;
+}
+
+// move cursor to the right n boxes
+static
+Position
+right(struct TerminalState *term, int n)
+{
+ rune r;
+ int w, d;
+ Position pos = CURRENT(term);
+
+ char *buf = term->edit.buf + term->edit.pos;
+ char *end = term->edit.buf + term->edit.len;
+
+ d = 0;
+ while(n > 0 && buf < end){
+ buf += utf8·decode(buf, &r);
+
+ w = utf8·runewidth(r);
+ n -= w;
+ d += w;
+ }
+
+ pos.cursor = MIN(pos.cursor+d, term->cursor.len);
+ pos.buffer = MIN(buf-term->edit.buf, term->edit.len);
+ return pos;
+}
+
+static
+Position
+prevword(struct TerminalState *term, int n)
+{
+ rune r;
+ int c, w, b, d;
+ Position pos = CURRENT(term);
+
+ char *buf = term->edit.buf + term->edit.pos;
+
+ d = 0;
+ while(n-- > 0 && buf > term->edit.buf){
+ eatspace:
+ b = utf8·decodeprev(buf-1, &r);
+ w = utf8·runewidth(r);
+ if((c=runetype(r)) == Space){
+ buf -= b;
+ d += w;
+
+ if(buf <= term->edit.buf)
+ break;
+
+ goto eatspace;
+ }
+
+ eatword:
+ if(runetype(r) == c){
+ buf -= b;
+ d += w;
+
+ if(buf <= term->edit.buf)
+ break;
+
+ b = utf8·decodeprev(buf-1, &r);
+ w = utf8·runewidth(r);
+
+ goto eatword;
+ }
+ }
+
+ pos.cursor = MAX(pos.cursor-d, 0);
+ pos.buffer = MAX(buf-term->edit.buf, 0);
+ return pos;
+}
+
+static
+Position
+nextword(struct TerminalState *term, int n)
+{
+ rune r;
+ int c, b, w, d;
+ Position pos = CURRENT(term);
+
+ char *buf = term->edit.buf + term->edit.pos;
+ char *end = term->edit.buf + term->edit.len;
+
+ d = 0;
+ while(n-- > 0 && buf < end){
+ b = utf8·decode(buf, &r);
+ w = utf8·runewidth(r);
+ c = runetype(r);
+ eatword:
+ if(runetype(r) == c){
+ buf += b;
+ d += w;
+
+ if(buf >= end)
+ break;
+
+ b = utf8·decode(buf, &r);
+ w = utf8·runewidth(r);
+ goto eatword;
+ }
+ eatspace:
+ while((c=runetype(r)) == Space){
+ buf += b;
+ d += w;
+
+ if(buf >= end)
+ break;
+
+ b = utf8·decode(buf, &r);
+ w = utf8·runewidth(r);
+ goto eatspace;
+ }
+ }
+
+ pos.cursor = MIN(pos.cursor+d, term->cursor.len);
+ pos.buffer = MIN(buf-term->edit.buf, term->edit.len);
+ return pos;
+}
+
+
+static
+Position
+prevWord(struct TerminalState *term, int n)
+{
+ rune r;
+ int c, w, b, d;
+ Position pos = CURRENT(term);
+
+ char *buf = term->edit.buf + term->edit.pos;
+
+ d = 0;
+ while(n-- > 0 && buf > term->edit.buf){
+ eatspace:
+ b = utf8·decodeprev(buf-1, &r);
+ w = utf8·runewidth(r);
+ if((c=runetype(r)) == Space){
+ buf -= b;
+ d += w;
+
+ if(buf <= term->edit.buf)
+ break;
+
+ goto eatspace;
+ }
+
+ eatword:
+ if((c=runetype(r)) != Space){
+ buf -= b;
+ d += w;
+
+ if(buf <= term->edit.buf)
+ break;
+
+ b = utf8·decodeprev(buf-1, &r);
+ w = utf8·runewidth(r);
+
+ goto eatword;
+ }
+ }
+
+ pos.cursor = MAX(pos.cursor-d, 0);
+ pos.buffer = MAX(buf-term->edit.buf, 0);
+ return pos;
+}
+
+static
+Position
+nextWord(struct TerminalState *term, int n)
+{
+ rune r;
+ int b, w, d;
+ Position pos = CURRENT(term);
+
+ char *buf = term->edit.buf + term->edit.pos;
+ char *end = term->edit.buf + term->edit.len;
+
+ d = 0;
+ while(n-- > 0 && buf < end){
+ eatword:
+ b = utf8·decode(buf, &r);
+ w = utf8·runewidth(r);
+ if(runetype(r) != Space){
+ buf += b;
+ d += w;
+
+ if(buf > end)
+ break;
+
+ goto eatword;
+ }
+
+ eatspace:
+ if(runetype(r) == Space){
+ buf += b;
+ d += w;
+
+ if(buf > end)
+ break;
+
+ b = utf8·decode(buf, &r);
+ w = utf8·runewidth(r);
+
+ goto eatspace;
+ }
+ }
+
+ pos.cursor = MIN(pos.cursor+d, term->cursor.len);
+ pos.buffer = MIN(buf-term->edit.buf, term->edit.len);
+ return pos;
+}
+
+static
+Position
+nextend(struct TerminalState *term, int n)
+{
+ rune r;
+ int c, b, w, d;
+ Position pos = CURRENT(term);
+
+ char *buf = term->edit.buf + term->edit.pos;
+ char *end = term->edit.buf + term->edit.len;
+
+ d = 0;
+ while(n-- > 0 && buf+1 < end){
+ eatspace:
+ b = utf8·decode(buf+1, &r);
+ w = utf8·runewidth(r);
+ while((c=runetype(r)) == Space){
+ buf += b;
+ d += w;
+
+ if(buf+1 >= end)
+ break;
+
+ goto eatspace;
+ }
+ eatword:
+ if(runetype(r) == c){
+ buf += b;
+ d += w;
+
+ if(buf+1 >= end)
+ break;
+
+ b = utf8·decode(buf+1, &r);
+ w = utf8·runewidth(r);
+ goto eatword;
+ }
+ }
+
+ pos.cursor = MIN(pos.cursor+d, term->cursor.len);
+ pos.buffer = MIN(buf-term->edit.buf, term->edit.len);
+ return pos;
+}
+
+static
+Position
+nextEnd(struct TerminalState *term, int n)
+{
+ rune r;
+ int b, w, d;
+ Position pos = CURRENT(term);
+
+ char *buf = term->edit.buf + term->edit.pos;
+ char *end = term->edit.buf + term->edit.len;
+
+ d = 0;
+ while(n-- > 0 && buf+1 < end){
+ eatspace:
+ b = utf8·decode(buf+1, &r);
+ w = utf8·runewidth(r);
+ if(runetype(r) == Space){
+ buf += b;
+ d += w;
+
+ if(buf+1 > end)
+ break;
+
+ goto eatspace;
+ }
+
+ eatword:
+ if(runetype(r) != Space){
+ buf += b;
+ d += w;
+
+ if(buf+1 > end)
+ break;
+
+ b = utf8·decode(buf+1, &r);
+ w = utf8·runewidth(r);
+
+ goto eatword;
+ }
+ }
+
+ pos.cursor = MIN(pos.cursor+d, term->cursor.len);
+ pos.buffer = MIN(buf-term->edit.buf, term->edit.len);
+ return pos;
+}
+
+#define HOME(term) (Position){0}
+#define END(term) (Position){(term)->edit.len, (term)->cursor.len}
+
+static
+int
+vi(struct TerminalState *term, char c)
+{
+ int n = 1;
+ Verb verb = move;
+
+action:
+ switch(c){
+ /* # of repeats */
+ case '1': case '2': case '3':
+ case '4': case '5': case '6':
+ case '7': case '8': case '9':
+ n = 0;
+ while('0' <= c && c <= '9'){
+ n = 10*n + (c-'0');
+ if(read(term->ifd, &c, 1)<1)
+ return -1;
+ }
+ goto action;
+
+ /* composable actions */
+ case 'l': verb(term, right(term, n)); break;
+ case 'h': verb(term, left(term, n)); break;
+ case '0': verb(term, HOME(term)); break;
+ case '$': verb(term, END(term)); break;
+ case 'b': verb(term, prevword(term,n)); break;
+ case 'B': verb(term, prevWord(term,n)); break;
+ case 'w': verb(term, nextword(term,n)); break;
+ case 'W': verb(term, nextWord(term,n)); break;
+ case 'e': verb(term, nextend(term,n)); break;
+ case 'E': verb(term, nextEnd(term,n)); break;
+
+ /* verb switches */
+ case 'd': // delete
+ verb = delete;
+ if(read(term->ifd, &c, 1)<1)
+ return -1;
+ /* special cases */
+ switch(c){
+ case 'd':
+ move(term, HOME(term));
+ delete(term, END(term));
+ return 0;
+ default:
+ goto action;
+ }
+ case 'y': // yank
+ verb = yank;
+ if(read(term->ifd, &c, 1)<1)
+ return -1;
+ /* special cases */
+ switch(c){
+ case 'y':
+ if(term->yank.cap < term->edit.len+1){
+ efree(term->yank.buf);
+ term->yank.len = term->edit.len;
+ term->yank.cap = term->edit.len+1;
+ term->yank.buf = emalloc(term->yank.cap);
+ }
+ memcpy(term->yank.buf, term->edit.buf, term->edit.len+1);
+ break;
+ default:
+ goto action;
+ }
+ break;
+
+ case 'p': // put
+ insertbytes(term, term->yank.len, term->yank.buf);
+ refreshline(term);
+ return 0;
+
+ /* special cases
+ * sadly I don't know a better way than to have these checks for move
+ * the vi language doesn't fully compose
+ */
+ case 'i': insertmode:
+ if(verb != move) goto unrecognized;
+ insertmode(term->ofd);
+ break;
+
+ case 'I':
+ if(verb != move) goto unrecognized;
+ move(term, HOME(term));
+ goto insertmode;
+
+ case 'a':
+ if(verb != move) goto unrecognized;
+ if(term->edit.pos < term->edit.len){
+ term->edit.pos++;
+ refreshline(term);
+ }
+ goto insertmode;
+
+ case 'A':
+ if(verb != move) goto unrecognized;
+ move(term, END(term));
+ goto insertmode;
+
+ case 'x':
+ if(verb != move) goto unrecognized;
+ delete(term, right(term, 1));
+ break;
+
+ case 'X':
+ if(verb != move) goto unrecognized;
+ delete(term, left(term, 1));
+ break;
+
+ case 'r':
+ if(verb != move) goto unrecognized;
+ if(read(term->ifd, &c, 1)<1)
+ return -1;
+ if(c < ' ')
+ break;
+ term->edit.buf[term->edit.pos] = c;
+ refreshline(term);
+ break;
+
+ // TODO: replace mode?
+
+ case 'c':
+ if(verb != move) goto unrecognized;
+ insertmode(term->ofd);
+ verb = delete;
+ if(read(term->ifd, &c, 1)<1)
+ return -1;
+ goto action;
+
+ case 'C':
+ if(verb != move) goto unrecognized;
+ insertmode(term->ofd);
+ goto deleteln;
+
+ case 'D':
+ if(verb != move) goto unrecognized;
+ deleteln:
+ term->edit.len = term->edit.pos;
+ term->edit.buf[term->edit.pos] = 0;
+ refreshline(term);
+ break;
+
+ default: unrecognized:
+ beep();
+ break;
+ }
+
+ return 0;
+}
+#undef END
+
+#define END(term) (Position){(term).edit.len, (term).cursor.len}
+
+static
+int
+size(char *s)
+{
+ rune c;
+ int n, len = 0;;
+ while((c=*s)){
+ if(c == '\033'){
+ n = 1;
+ esccode:
+ c = s[n];
+ if(!c) // we hit end of string in the middle of parsing an escape code!
+ return len;
+ if(c == 'm'){
+ s += n + 1;
+ continue; // outer loop
+ }
+ n++;
+ goto esccode;
+ }
+ n = utf8·decode(s, &c);
+ s += n;
+ len += utf8·runewidth(c);
+ }
+ return len;
+}
+
+/* this function is the core of the line editing capability of linenoise.
+ * it expects 'fd' to be already in "raw mode" so that every key pressed
+ * will be returned asap to read().
+ *
+ * the resulting string is put into 'buf' when the user type enter, or
+ * when ctrl+d is typed.
+ *
+ * the function returns the length of the current buffer. */
+static
+int
+interact(int ifd, int ofd, char *buf, intptr len, char *prompt)
+{
+ int n, aux;
+ char esc[3];
+ char c[UTFmax+1] = { 0 };
+ rune r;
+
+ struct TerminalState term;
+ /*
+ * populate the state that we pass to functions implementing
+ * specific editing functionalities
+ */
+ term.ifd = ifd;
+ term.ofd = ofd;
+
+ term.edit.buf = buf;
+ term.edit.cap = len;
+ term.edit.len = 0;
+ term.edit.pos = 0;
+
+ term.prompt.s = prompt;
+ term.prompt.len = strlen(prompt);
+ term.prompt.size = size(prompt);
+
+ term.cursor.pos = 0;
+ term.cursor.len = 0;
+ term.cursor.cap = columns(ifd, ofd);
+
+ term.maxrows = 0;
+ term.history = 0;
+
+ term.yank.buf = nil;
+ term.yank.cap = term.yank.len = 0;
+
+ /* buffer starts empty. */
+ term.edit.buf[0] = '\0';
+ term.edit.cap--; /* make sure there is always space for the nulterm */
+
+ /* push current (empty) command onto history stack */
+ addhistory("");
+
+ if(write(term.ofd,prompt,term.prompt.len) == -1)
+ return -1;
+
+ for(;;){
+ n = read(term.ifd,c,1);
+ if(n <= 0)
+ goto finish;
+
+ /* partition input by rune */
+ if(utf8·onebyte(c[0])){
+ r = c[0];
+ }else if(utf8·twobyte(c[0])){
+ n = read(term.ifd,c+1,1);
+ if(n < 1 || (n=utf8·decode(c, &r)) != 2)
+ goto finish;
+ }else if(utf8·threebyte(c[0])){
+ n = read(term.ifd,c+1,2);
+ if(n < 2 || (n=utf8·decode(c, &r)) != 3)
+ goto finish;
+ }else if(utf8·fourbyte(c[0])){
+ n = read(term.ifd,c+1,3);
+ if(n < 3 || (n=utf8·decode(c, &r)) != 4)
+ goto finish;
+ }else
+ goto finish;
+
+ switch(r){
+ case KeyEnter:
+ pophistory();
+ if(mode.multiline)
+ move(&term, END(term));
+ goto finish;
+
+ case KeyCtrlC:
+ errno = EAGAIN;
+ return -1;
+
+ case KeyBackspace:
+ case KeyCtrlH:
+ delete(&term, left(&term, 1));
+ break;
+
+ case KeyCtrlD:
+ if(term.edit.len > 0)
+ delete(&term, right(&term, 1));
+ break;
+
+ case KeyCtrlT:
+ if(term.edit.pos > 0 && term.edit.pos < term.edit.len){
+ aux = buf[term.edit.pos-1];
+
+ buf[term.edit.pos-1] = buf[term.edit.pos];
+ buf[term.edit.pos] = aux;
+
+ if(term.edit.pos != term.edit.len-1)
+ term.edit.pos++;
+
+ refreshline(&term);
+ }
+ break;
+
+ case KeyCtrlB:
+ move(&term, left(&term, 1));
+ break;
+
+ case KeyCtrlF: /* ctrl-f */
+ move(&term, right(&term, 1));
+ break;
+
+ case KeyCtrlP: /* ctrl-p */
+ usehistory(&term, +1);
+ break;
+
+ case KeyCtrlN: /* ctrl-n */
+ usehistory(&term, -1);
+ break;
+
+ case KeyEsc: /* escape sequence */
+ /*
+ * try to read two bytes representing the escape sequence.
+ * if we read less than 2 and we are in vi mode, interpret as command
+ *
+ * NOTE: we could do a timed read here
+ */
+ switch(read(term.ifd,esc,2)){
+ case 0:
+ if(mode.vi.on){
+ if(mode.vi.insert){
+ normalmode(term.ofd);
+ if(term.edit.pos > 0){
+ --term.edit.pos;
+ refreshline(&term);
+ }
+ continue;
+ }
+ }
+ case 1:
+ if(mode.vi.on){
+ if(mode.vi.insert){
+ normalmode(term.ofd);
+ if(vi(&term,esc[0]) < 0){
+ term.edit.len = -1;
+ goto finish;
+ }
+ continue;
+ }
+ }
+ default: // 2
+ ;
+ }
+
+ /* ESC [ sequences. */
+ if(esc[0] == '['){
+ if(0 <= esc[1] && esc[1] <= '9'){
+ /* extended escape, read additional byte. */
+ if(read(term.ifd,esc+2,1) == -1)
+ break;
+
+ if(esc[2] == '~'){
+ switch(esc[1]){
+ case '3': /* delete key. */
+ delete(&term, left(&term,1));
+ break;
+ }
+ }
+ }else{
+ switch(esc[1]) {
+ case 'A': /* up */
+ usehistory(&term, +1);
+ break;
+ case 'B': /* down */
+ usehistory(&term, -1);
+ break;
+ case 'C': /* right */
+ move(&term, right(&term, 1));
+ break;
+ case 'D': /* left */
+ move(&term, left(&term, 1));
+ break;
+ case 'H': /* home */
+ move(&term, HOME(term));
+ break;
+ case 'F': /* end*/
+ move(&term, END(term));
+ break;
+ }
+ }
+ }
+ /* ESC O sequences. */
+ else if(esc[0] == 'O'){
+ switch(esc[1]) {
+ case 'H': /* home */
+ move(&term, HOME(term));
+ break;
+ case 'F': /* end*/
+ move(&term, END(term));
+ break;
+ }
+ }
+ break;
+
+ default:
+ if(mode.vi.on && !mode.vi.insert && n == 1){
+ if(vi(&term,c[0]) < 0){
+ term.edit.len = -1;
+ goto finish;
+ }
+ }else if(!insertrune(&term,n,c)){
+ term.edit.len = -1;
+ goto finish;
+ }
+
+ break;
+
+ case KeyCtrlU: /* Ctrl+u, delete the whole line. */
+ buf[0] = '\0';
+ term.edit.pos = term.edit.len = 0;
+ term.cursor.pos = term.cursor.len = 0;
+ refreshline(&term);
+ break;
+
+ case KeyCtrlK: /* Ctrl+k, delete from current to end of line. */
+ buf[term.edit.pos] = '\0';
+ term.edit.len = term.edit.pos;
+ term.cursor.len = term.cursor.pos;
+ refreshline(&term);
+ break;
+
+ case KeyCtrlA: /* Ctrl+a, go to the start of the line */
+ move(&term, HOME(term));
+ break;
+
+ case KeyCtrlE: /* ctrl+e, go to the end of the line */
+ move(&term, END(term));
+ break;
+
+ case KeyCtrlL: /* ctrl+term, clear screen */
+ clear();
+ refreshline(&term);
+ break;
+
+ case KeyCtrlW: /* ctrl+w, delete previous word */
+ delete(&term, prevword(&term,1));
+ break;
+ }
+ }
+finish:
+ efree(term.yank.buf);
+ return term.edit.len;
+}
+
+/*
+ * this special mode is used by linenoise in order to print scan codes
+ * on screen for debugging / development purposes. It is implemented
+ * by the linenoise_example program using the --keycodes option.
+ */
+void
+printkeycode(void)
+{
+ int n;
+ char c, quit[4];
+
+ printf("entering debugging mode. printing key codes.\n"
+ "press keys to see scan codes. type 'quit' at any time to exit.\n");
+
+ if(!enterraw(0))
+ return;
+
+ memset(quit,' ',4);
+
+ for(;;){
+ n = read(0,&c,1);
+ if(n <= 0)
+ continue;
+ memmove(quit,quit+1,sizeof(quit)-1); // shift string to left
+ quit[arrlen(quit)-1] = c; /* Insert current char on the right. */
+
+ if(memcmp(quit,"quit",sizeof(quit)) == 0)
+ break;
+
+ printf("'%c' %02x (%d) (type quit to exit)\n", isprint(c) ? c : '?', (int)c, (int)c);
+ printf("\r"); /* go to left edge manually, we are in raw mode. */
+ fflush(stdout);
+ }
+ exitraw(0);
+}
+
+/*
+ * this function calls the line editing function edit() using the stdin set in raw mode
+ */
+static
+int
+raw(char *buf, intptr len, char *prompt)
+{
+ int n;
+
+ if(!len){
+ errno = EINVAL;
+ return -1;
+ }
+
+ // XXX: should we not hardcode stdin and stdout fd?
+ if(!enterraw(0)) return -1;
+ n = interact(0, 1, buf, len, prompt);
+ exitraw(0);
+
+ return n;
+}
+
+/*
+ * called when readline() is called with the standard
+ * input file descriptor not attached to a TTY. For example when the
+ * program is called in pipe or with a file redirected to its standard input
+ * in this case, we want to be able to return the line regardless of its length
+ */
+static
+int
+notty(void)
+{
+ int c;
+
+ for(;;){
+ c = fgetc(stdin);
+ put(&runner->cmd.io, c);
+ }
+}
+
+void
+enablevi(void)
+{
+ mode.vi.on = 1;
+ insertmode(1);
+}
+
+/*
+ * The high level function that is the main API.
+ * This function checks if the terminal has basic capabilities and later
+ * either calls the line editing function or uses dummy fgets() so that
+ * you will be able to type something even in the most desperate of the
+ * conditions.
+ */
+int
+readline(char *prompt)
+{
+ int n;
+
+ // reset the command buffer
+ runner->cmd.io->e = runner->cmd.io->b = runner->cmd.io->buf;
+
+ if(!shell.interactive)
+ return notty();
+
+ if((n = raw(runner->cmd.io->e, runner->cmd.io->cap-1, prompt)) == -1)
+ return 0;
+ runner->cmd.io->e += n;
+
+ /* insert a newline character at the end */
+ put(&runner->cmd.io, '\n');
+
+ return 1;
+}
+
+/* At exit we'll try to fix the terminal to the initial conditions. */
+static
+void
+doatexit(void)
+{
+ exitraw(0);
+ normalcursor(1);
+}
diff --git a/src/cmd/rc/io.c b/src/cmd/rc/io.c
new file mode 100644
index 0000000..dc81c2e
--- /dev/null
+++ b/src/cmd/rc/io.c
@@ -0,0 +1,437 @@
+#include "rc.h"
+#include "parse.h"
+
+#define CAP0 512
+
+Io*
+openfd(int fd)
+{
+ Io *io = emalloc(sizeof(*io) + CAP0);
+
+ io->fd = fd;
+ io->cap = CAP0;
+ io->b = io->e = io->buf;
+ io->s = nil;
+
+ return io;
+}
+
+Io*
+openstr(void)
+{
+ char *s;
+ Io *io = emalloc(sizeof(*io) + CAP0);
+
+ io->fd = -1;
+ io->cap = CAP0;
+ io->b = io->s = emalloc(101);
+ io->e = io->b+100;
+
+ for(s = io->b; s<=io->e; s++)
+ *s=0;
+
+ return io;
+}
+
+#if 0
+/*
+ * open a corebuffer to read. EOF occurs after reading len characters from buf
+ */
+
+Io*
+opencore(char *s, int len)
+{
+ Io *io = emalloc(sizeof(*io));
+ char *buf = emalloc(len);
+ io->fd = -1 /*open("/dev/null", 0)*/;
+ io->b = io->s = buf;
+ io->e = buf+len;
+ memcpy(buf, s, len);
+
+ return io;
+}
+#endif
+
+void
+iorewind(Io *io)
+{
+ if(io->fd==-1)
+ io->b = io->s;
+ else{
+ io->b = io->e = io->buf;
+ lseek(io->fd, 0L, 0);
+ }
+}
+
+void
+terminate(Io *io)
+{
+ if(io->fd>=0)
+ close(io->fd);
+ if(io->s)
+ efree(io->s);
+
+ efree((char *)io);
+}
+
+static
+int
+refill(Io *io)
+{
+ int n;
+
+ if(io->fd==-1 || (n = read(io->fd, io->buf, io->cap))<=0)
+ return EOF;
+
+ io->b = io->buf;
+ io->e = io->buf+n;
+
+ return *io->b++&0xff;
+}
+
+
+void
+flush(Io *io)
+{
+ int n;
+ char *s;
+
+ if(io->s){
+ n = io->e-io->s;
+ io->s = realloc(io->s, n+101);
+ if(io->s==0)
+ panicf("Can't realloc %d bytes in flush!", n+101);
+ io->b = io->s+n;
+ io->e = io->b+100;
+ for(s = io->b;s<=io->e;s++) *s='\0';
+ }else{
+ n = io->b-io->buf;
+ if(n && write(io->fd, io->buf, n) < 0)
+ write(3, "write error\n", 12);
+ io->b = io->buf;
+ io->e = io->buf + io->cap;
+ }
+}
+
+
+static
+void
+printchar(Io *io, int c)
+{
+ if(io->b==io->e)
+ flush(io);
+
+ *io->b++=c;
+}
+
+void
+printquote(Io *io, char *s)
+{
+ printchar(io, '\'');
+ for(;*s;s++)
+ if(*s=='\'')
+ print(io, "''");
+ else printchar(io, *s);
+ printchar(io, '\'');
+}
+
+void
+printstr(Io *io, char *s)
+{
+ if(s==0)
+ s="(null)";
+ while(*s) printchar(io, *s++);
+}
+
+void
+printword(Io *io, char *s)
+{
+ char *t;
+
+ for(t = s;*t;t++)
+ if(!iswordchar(*t))
+ break;
+
+ if(t==s || *t)
+ printquote(io, s);
+ else
+ printstr(io, s);
+}
+
+void
+printptr(Io *io, void *v)
+{
+ int n;
+ uintptr p;
+
+ p = (uintptr)v;
+ if(sizeof(uintptr) == sizeof(uvlong) && p>>32)
+ for(n = 60;n>=32;n-=4) printchar(io, "0123456789ABCDEF"[(p>>n)&0xF]);
+
+ for(n = 28;n>=0;n-=4) printchar(io, "0123456789ABCDEF"[(p>>n)&0xF]);
+}
+
+static
+void
+printint(Io *io, int n)
+{
+ if(n<0){
+ if(n!=INT_MIN){
+ printchar(io, '-');
+ printint(io, -n);
+ return;
+ }
+ /* n is two's complement minimum integer */
+ n = -(INT_MIN+1);
+ printchar(io, '-');
+ printint(io, n/10);
+ printchar(io, n%10+'1');
+ return;
+ }
+ if(n>9)
+ printint(io, n/10);
+ printchar(io, n%10+'0');
+}
+
+static
+void
+printoct(Io *io, unsigned n)
+{
+ if(n>7)
+ printoct(io, n>>3);
+ printchar(io, (n&7)+'0');
+}
+
+static
+void
+printval(Io *io, Word *a)
+{
+ if(a){
+ while(a->link && a->link->str){
+ printword(io, a->str);
+ printchar(io, ' ');
+ a = a->link;
+ }
+ printword(io, a->str);
+ }
+}
+
+#define C0 t->child[0]
+#define C1 t->child[1]
+#define C2 t->child[2]
+
+static
+void
+printtree(Io *io, Tree *t)
+{
+ if(!t)
+ return;
+
+ switch(t->type){
+ default: print(io, "bad(%d)[%p %p %p]", t->type, C0, C1, C2); break;
+ case '$': print(io,"$%t",C0); break;
+ case '&': print(io,"%t&",C0); break;
+ case '^': print(io,"%t^%t",C0,C1); break;
+ case '`': print(io,"`%t",C0); break;
+
+ case Tbasic: print(io, "%t", C0); break;
+ case Tbang: print(io, "!%t", C0); break;
+ case Tblock: print(io, "{%t}", C0); break;
+ case Tcount: print(io, "$#%t", C0); break;
+ case Tparen: print(io, "(%t)", C0); break;
+ case Tjoin: print(io,"$\"%t",C0); break;
+ case Tindex: print(io, "%t(%t)",C0); break;
+ case Tsubshell: print(io, "@ %t",C0); break;
+ //case Ttwiddle: print(io, "~ %t %t", C0, C1); break;
+
+ case Toror:
+ case Tandand:
+
+ case Targs:
+ if(!C0)
+ print(io, "%t", C1);
+ else if(!C1)
+ print(io, "%t", C0);
+ else
+ print(io, "%t %t", C0, C1);
+ break;
+
+ case ';':
+ if(C0){
+ if(C1)
+ print(io, "%t;%t", C0, C1);
+ else
+ print(io, "%t", C0);
+ }else
+ print(io, "%t", C1);
+ break;
+
+ case Twords:
+ if(C0)
+ print(io, "%t", C0);
+ print(io, "%t", C1);
+
+ case Tword:
+ if(t->quoted)
+ print(io, "%Q", t->str);
+ print(io, "%q", t->str);
+ break;
+
+ case '=':
+ print(io, "%t=%t", C0, C1);
+ if(C2)
+ print(io, " %t", C2);
+ break;
+
+ case Tdup:
+ if(t->redir.type == Rdupfd)
+ print(io, ">[%d=%d]", t->redir.fd[1], t->redir.fd[0]);
+ else
+ print(io, ">[%d=]", t->redir.fd[0]);
+ print(io, "%t", C1);
+ break;
+
+ case Tredir:
+ switch(t->redir.type){
+ case Rhere:
+ printchar(io, '<');
+ case Rread:
+ printchar(io, '<');
+ goto readfd;
+ case Rrdwr:
+ printchar(io, '<');
+ printchar(io, '>');
+ readfd:
+ if(t->redir.fd[0]!=0)
+ print(io, "[%d]", t->redir.fd[0]);
+ break;
+ case Rappend:
+ printchar(io, '>');
+ goto writefd;
+ case Rwrite:
+ printchar(io, '>');
+ printchar(io, '>');
+ writefd:
+ if(t->redir.fd[0]!=1)
+ print(io, "[%d]", t->redir.fd[0]);
+ break;
+ }
+ print(io, "%t", C0);
+ if(C1)
+ print(io, " %t", C1);
+ break;
+
+ case Tpipe:
+ print(io, "%t|", C0);
+ if(t->redir.fd[1]==0){
+ if(t->redir.fd[0]!=1)
+ print(io, "[%d]", t->redir.fd[0]);
+ }
+ else
+ print(io, "[%d=%d]", t->redir.fd[0], t->redir.fd[1]);
+ print(io, "%t", C1);
+ break;
+ }
+}
+
+#undef C0
+#undef C1
+#undef C2
+
+// -----------------------------------------------------------------------
+// exports
+
+/* readers */
+int
+get(Io *io)
+{
+ if(io->b==io->e)
+ return refill(io);
+
+ return *io->b++ & 0xFF;
+}
+
+/* writers */
+int
+put(Io **iop, char c)
+{
+ int nb, ne, nc;
+ Io *io = *iop;
+ char *e = io->b + io->cap;
+
+ if(io->e == e){
+ nb = io->b - io->buf;
+ ne = io->e - io->buf;
+ nc = 2*io->cap;
+
+ if(!(io = erealloc(io, sizeof(*io)+nc)))
+ return 0;
+
+ io->b = io->buf + nb;
+ io->e = io->buf + ne;
+ io->cap = nc;
+
+ *iop = io;
+ }
+
+ *io->e++ = c;
+ return 1;
+}
+
+/* printers */
+static int pfmtnest;
+
+void
+print(Io *io, char *fmt, ...)
+{
+ va_list args;
+ char err[ERRMAX];
+
+ va_start(args, fmt);
+ pfmtnest++;
+
+ for(;*fmt;fmt++)
+ if(*fmt!='%')
+ printchar(io, *fmt);
+ else
+ switch(*++fmt){
+ case '\0':
+ va_end(args);
+ return;
+ case 'c':
+ printchar(io, va_arg(args, int));
+ break;
+ case 'd':
+ printint(io, va_arg(args, int));
+ break;
+ case 'o':
+ printoct(io, va_arg(args, unsigned));
+ break;
+ case 'p':
+ printptr(io, va_arg(args, void*));
+ break;
+ case 'Q':
+ printquote(io, va_arg(args, char *));
+ break;
+ case 'q':
+ printword(io, va_arg(args, char *));
+ break;
+ case 's':
+ printstr(io, va_arg(args, char *));
+ break;
+ case 't':
+ printtree(io, va_arg(args, struct Tree *));
+ break;
+ case 'v':
+ printval(io, va_arg(args, struct Word *));
+ break;
+ default:
+ printchar(io, *fmt);
+ break;
+ }
+
+ va_end(args);
+
+ if(--pfmtnest==0)
+ flush(io);
+}
diff --git a/src/cmd/rc/job.c b/src/cmd/rc/job.c
new file mode 100644
index 0000000..1587951
--- /dev/null
+++ b/src/cmd/rc/job.c
@@ -0,0 +1,91 @@
+#include "rc.h"
+
+#include <signal.h>
+#include <termios.h>
+
+// -----------------------------------------------------------------------
+// exports
+
+Thread *
+getjob(int pid, int *index)
+{
+ int i;
+ Thread *job;
+ for(i=0,job=shell.jobs; job && job->pid != pid; i++, job=job->link)
+ ;
+
+ return job;
+}
+
+void
+report(Thread *job, int index)
+{
+ switch(job->wait.status){
+ case Pdone:
+ print(shell.err, "job %d [%d]: done\n", index, job->pid);
+ break;
+ case Pstop:
+ print(shell.err, "job %d [%d]: suspended\n", index, job->pid);
+ break;
+ case Pagain:
+ print(shell.err, "job %d [%d]: continued\n", index, job->pid);
+ break;
+ case Prun:
+ print(shell.err, "job %d [%d]: running\n", index, job->pid);
+ break;
+ default:
+ fatal("bad wait status: %d\n", job->wait.status);
+ }
+}
+
+void
+wakeup(Thread *job)
+{
+ int i;
+ job->wait.status = Prun;
+ for(i=0; i < job->wait.len; i++){
+ if(job->wait.on[i].status == Pstop)
+ job->wait.on[i].status = Prun;
+ }
+
+ tcsetpgrp(0, job->pgid);
+}
+
+void
+foreground(Thread *job, int now)
+{
+ Thread *caller = job->caller;
+ if(now){
+ if(kill(-job->pgid, SIGCONT) < 0)
+ perror("kill[SIGCONT]");
+ }
+
+ waitall(job);
+ /*
+ * reset state if we have a caller
+ * otherwise we will exit anyways
+ */
+ if(caller && caller->flag.user){
+ tcsetpgrp(0, caller->pid);
+ job->flag.user = 1;
+ }
+}
+
+void
+addjob(Thread *job)
+{
+ job->link = shell.jobs;
+ shell.jobs = job;
+ job->wait.status = Prun;
+}
+
+void
+deljob(Thread *job)
+{
+ Thread **jp;
+
+ for(jp = &shell.jobs; *jp && *jp != job; jp = &(*jp)->link)
+ ;
+
+ *jp = job->link;
+}
diff --git a/src/cmd/rc/lex.c b/src/cmd/rc/lex.c
new file mode 100644
index 0000000..9ca2453
--- /dev/null
+++ b/src/cmd/rc/lex.c
@@ -0,0 +1,394 @@
+#include "rc.h"
+#include "parse.h"
+
+static int advance(void);
+
+// -----------------------------------------------------------------------
+// lexer
+
+struct Lexer
+{
+ int c[2];
+ ushort doprompt;
+ ushort hadword;
+ ushort haddollar;
+ ushort inquote;
+ char buf[BUFSIZ];
+};
+
+static struct Lexer lexer = { .c={0, EOF}, .doprompt=1 };
+
+#define put1(b) lexer.buf[0] = (b), lexer.buf[1] = 0;
+#define put2(b0,b1) lexer.buf[0] = (b0), lexer.buf[1] = (b1), lexer.buf[2] = 0;
+#define put3(b0,b1,b2) lexer.buf[0] = (b0), lexer.buf[1] = (b1), lexer.buf[2] = b2, lexer.buf[3] = 0;
+
+void
+yyerror(const char *msg)
+{
+ print(shell.err, "rc:%d: ", runner->line);
+
+ if(lexer.buf[0] && lexer.buf[0]!='\n')
+ print(shell.err, "%q: ", lexer.buf);
+
+ print(shell.err, "%s\n", msg);
+ flush(shell.err);
+
+ lexer.hadword = 0;
+ lexer.haddollar = 0;
+
+ /* consume remaining tokens */
+ while(lexer.c[0] !='\n' && lexer.c[0] != EOF)
+ advance();
+}
+
+int
+readc(void)
+{
+ int c;
+ static int peek = EOF;
+
+ if(peek!=EOF){
+ c = peek;
+ peek = EOF;
+ return c;
+ }
+
+ if(runner->flag.eof)
+ return EOF;
+
+ if(!prompt(&lexer.doprompt))
+ exit(1); // XXX: hack for signal handling right now...
+
+ c = get(runner->cmd.io);
+ lexer.doprompt = lexer.doprompt || c=='\n' || c==EOF;
+
+ if(c==EOF)
+ runner->flag.eof = 1;
+
+ return c;
+}
+
+static
+int
+peekc(void)
+{
+ if(lexer.c[1] == EOF)
+ lexer.c[1] = readc();
+
+ return lexer.c[1];
+}
+
+static
+int
+advance(void)
+{
+ int c = peekc();
+ lexer.c[0] = lexer.c[1], lexer.c[1] = EOF;
+
+ return c;
+}
+
+static
+void
+skipws(void)
+{
+ int c;
+ for(;;){
+ c = peekc();
+ if(c== ' ' || c == '\t')
+ advance();
+ else
+ return;
+ }
+}
+
+static
+void
+skipnl(void)
+{
+ int c;
+ for(;;){
+ c = peekc();
+ if(c== ' ' || c == '\t' || c == '\n')
+ advance();
+ else
+ return;
+ }
+}
+
+static
+int
+nextis(int c)
+{
+ if(peekc()==c){
+ advance();
+ return 1;
+ }
+ return 0;
+}
+
+static
+char *
+putbyte(char *buf, int c)
+{
+ if(!buf)
+ return buf;
+
+ if(buf == arrend(lexer.buf)){
+ fatal("lexer: out of buffer space");
+ return nil;
+ }
+ *buf++ = c;
+ return buf;
+}
+
+static
+char *
+putrune(char *buf, int c)
+{
+ buf = putbyte(buf, c);
+ if(utf8·onebyte(c))
+ return buf;
+ if(utf8·twobyte(c))
+ return putbyte(buf,advance());
+ if(utf8·threebyte(c)){
+ buf = putbyte(buf,advance());
+ return putbyte(buf,advance());
+ }
+ if(utf8·fourbyte(c)){
+ buf = putbyte(buf,advance());
+ buf = putbyte(buf,advance());
+ return putbyte(buf,advance());
+ }
+ fatal("malformed utf8 stream");
+
+ return nil;
+}
+
+// -----------------------------------------------------------------------
+// exported functions
+
+// TODO: turn into static tables
+int
+iswordchar(int c)
+{
+ return !strchr("\n \t#;&|^$=`'{}()<>", c) && c!=EOF;
+}
+
+int
+isidentchar(int c)
+{
+ return c>' ' && !strchr("!\"#$%&'()+,-./:;<=>?@[\\]^`{|}~", c);
+}
+
+int
+yylex(void)
+{
+ int c, d = peekc();
+ Tree *node;
+ char *w = lexer.buf;
+
+ yylval.tree = nil;
+
+ /* inject tokens */
+ if(lexer.hadword){
+ lexer.hadword = 0;
+ if(d=='('){
+ advance();
+ strcpy(lexer.buf, "( [Tindex]");
+ return Tindex;
+ }
+ if(iswordchar(d) || d=='\'' || d=='`' || d=='$' || d=='"'){
+ strcpy(lexer.buf, "^");
+ return '^';
+ }
+ }
+
+ lexer.inquote = 0;
+
+ skipws();
+ switch(c=advance()){
+ case EOF:
+ lexer.haddollar = 0;
+ put3('E','O','F');
+ return EOF;
+
+ case '$':
+ lexer.haddollar = 1;
+ if(nextis('#')){
+ put2('$','#');
+ return Tcount;
+ }
+ if(nextis('^')){
+ put2('$','^');
+ return Tjoin;
+ }
+ put1('$');
+ return '$';
+
+ case '@':
+ lexer.haddollar = 0;
+ put1('@');
+ return Tsubshell;
+
+ case '!':
+ lexer.haddollar = 0;
+ put1('!');
+ return Tbang;
+
+ case '&':
+ lexer.haddollar = 0;
+ if(nextis('&')){
+ put2('&','&');
+ return Tandand;
+ }
+ put1('&');
+ return '&';
+
+ case '|':
+ lexer.haddollar = 0;
+ if(nextis('|')){
+ put2('|','|');
+ return Toror;
+ }
+ node = maketree();
+ *w++ = '|';
+
+ node->type = Tpipe;
+ node->redir.fd[0] = 1;
+ node->redir.fd[1] = 0;
+ goto redir;
+
+ case '>':
+ lexer.haddollar = 0;
+ node = maketree();
+ *w++ = '>';
+ node->type = Tredir;
+
+ if(nextis('>')){
+ node->redir.type = Rappend;
+ *w++ = '>';
+ }else
+ node->redir.type = Rwrite;
+ node->redir.fd[0] = 1;
+ goto redir;
+
+ case '<':
+ lexer.haddollar = 0;
+ node = maketree();
+ *w++ = '<';
+ node->type = Tredir;
+
+ if(nextis('<')){
+ node->redir.type = Rhere;
+ *w++ = '<';
+ }else if(nextis('>')){
+ node->redir.type = Rrdwr;
+ *w++ = '>';
+ }else{
+ node->redir.type = Rread;
+ }
+ node->redir.fd[0] = 0;
+ /* fallthrough */
+ redir:
+ if(nextis('[')){
+ *w++='[';
+ c = advance();
+ *w++ = c;
+ if(c < '0' || '9' < c){
+ badredir:
+ *w = 0;
+ yyerror(node->type == Tpipe ? "pipe syntax" : "redirection syntax");
+ return EOF;
+ }
+ node->redir.fd[0] = 0;
+ do{
+ node->redir.fd[0] = 10*node->redir.fd[0]+(c-'0');
+ *w++ = c;
+ c = advance();
+ }while('0'<=c && c<='9');
+
+ if(c == '='){
+ *w++ = '=';
+ if(node->type==Tredir)
+ node->type = Tdup;
+ c = advance();
+ }
+ if(c < '0' || '9' < c){
+ if(node->type == Tpipe)
+ goto badredir;
+ node->redir.type = Rclose;
+ }else{
+ node->redir.type = Rdupfd;
+ node->redir.fd[1] = node->redir.fd[0];
+ node->redir.fd[0] = 0;
+ do{
+ node->redir.fd[0] = 10*node->redir.fd[0]+(c-'0');
+ *w++ = c;
+ c = advance();
+ }while('0'<=c && c<='9');
+ }
+ if(c != ']' || (node->type == Tdup && (node->redir.type = Rhere || node->redir.type == Rappend)))
+ goto badredir;
+ *w++ = ']';
+ }
+ *w++ = 0;
+ yylval.tree = node;
+
+ return node->type;
+
+ case '\'':
+ lexer.hadword = 1;
+ lexer.inquote = 1;
+ lexer.haddollar = 0;
+ for(;;){
+ c = advance();
+ if(c==EOF)
+ break;
+
+ if(c=='\''){
+ if(peekc()!='\'')
+ break;
+ advance();
+ }
+ w = putrune(w, c);
+ }
+ if(w)
+ *w = 0;
+ node = token(Tword, lexer.buf);
+ node->quoted = 1;
+ return node->type;
+
+ default:
+ ;
+ }
+ if(!iswordchar(c)){
+ put1(c);
+ lexer.haddollar = 0;
+ return c;
+ }
+
+ for(;;){
+ w = putrune(w, c);
+ c = peekc();
+ if(lexer.haddollar ? !isidentchar(c) : !iswordchar(c))
+ break;
+ advance();
+ }
+
+ lexer.hadword = 1;
+ lexer.haddollar = 0;
+ if(w)
+ *w = 0;
+
+ node = token(Tword, lexer.buf);
+ if((c=iskeyword(lexer.buf))){
+ node->type = c;
+ lexer.hadword = 0;
+ }
+
+ node->quoted = 0;
+
+ yylval.tree = node;
+ return node->type;
+}
diff --git a/src/cmd/rc/main.c b/src/cmd/rc/main.c
new file mode 100644
index 0000000..2c0aa42
--- /dev/null
+++ b/src/cmd/rc/main.c
@@ -0,0 +1,66 @@
+#include "rc.h"
+#include "parse.h"
+#include "exec.h"
+
+#include <signal.h>
+#include <termios.h>
+
+// -----------------------------------------------------------------------
+// globals
+
+Thread *runner = nil;
+Shell shell = { 0 };
+
+// -----------------------------------------------------------------------
+// functions
+
+void
+initshell(void)
+{
+ if((shell.interactive=isatty(0))){
+ while(tcgetpgrp(0) != (shell.pid = getpgrp()))
+ kill(-shell.pid, SIGTTIN);
+
+ /* ignore job control signals */
+ signal(SIGINT, SIG_IGN);
+ signal(SIGQUIT, SIG_IGN);
+ signal(SIGTSTP, SIG_IGN);
+ signal(SIGTTIN, SIG_IGN);
+ signal(SIGTTOU, SIG_IGN);
+ /*
+ * NOTE: if SIGCHLD is set to SIG_IGN then
+ * 1. children that terminate do not become zombies
+ * 2. call a to wait() will block until all children have terminated
+ * 3. the call to wait will fail with errno == ECHILD
+ * see for discussion:
+ * https://stackoverflow.com/questions/1608017/no-child-process-error-from-waitpid-when-waiting-for-process-group
+ */
+ // signal(SIGCHLD, SIG_IGN);
+
+ /* take control */
+ shell.pid = getpid();
+ if(setpgid(shell.pid, shell.pid)<0)
+ fatal("could not put shell in its own process group");
+
+ tcsetpgrp(shell.pid, shell.pid);
+ }
+}
+
+// -----------------------------------------------------------------------
+// main point of entry
+
+int
+main(int argc, char *argv[])
+{
+ shell.err = openfd(2);
+
+ initenv();
+ initpath();
+ initkeywords();
+ initshell();
+ inithistory();
+
+ enablevi();
+ xboot(argc, argv);
+ /* unreachable */
+}
diff --git a/src/cmd/rc/parse.c b/src/cmd/rc/parse.c
new file mode 100644
index 0000000..1b29d41
--- /dev/null
+++ b/src/cmd/rc/parse.c
@@ -0,0 +1,2059 @@
+/* A Bison parser, made by GNU Bison 3.8.2. */
+
+/* Bison implementation for Yacc-like parsers in C
+
+ Copyright (C) 1984, 1989-1990, 2000-2015, 2018-2021 Free Software Foundation,
+ Inc.
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>. */
+
+/* As a special exception, you may create a larger work that contains
+ part or all of the Bison parser skeleton and distribute that work
+ under terms of your choice, so long as that work isn't itself a
+ parser generator using the skeleton or a modified version thereof
+ as a parser skeleton. Alternatively, if you modify or redistribute
+ the parser skeleton itself, you may (at your option) remove this
+ special exception, which will cause the skeleton and the resulting
+ Bison output files to be licensed under the GNU General Public
+ License without this special exception.
+
+ This special exception was added by the Free Software Foundation in
+ version 2.2 of Bison. */
+
+/* C LALR(1) parser skeleton written by Richard Stallman, by
+ simplifying the original so-called "semantic" parser. */
+
+/* DO NOT RELY ON FEATURES THAT ARE NOT DOCUMENTED in the manual,
+ especially those whose name start with YY_ or yy_. They are
+ private implementation details that can be changed or removed. */
+
+/* All symbols defined below should begin with yy or YY, to avoid
+ infringing on user name space. This should be done even for local
+ variables, as they might otherwise be expanded by user macros.
+ There are some unavoidable exceptions within include files to
+ define necessary library symbols; they are noted "INFRINGES ON
+ USER NAME SPACE" below. */
+
+/* Identify Bison output, and Bison version. */
+#define YYBISON 30802
+
+/* Bison version string. */
+#define YYBISON_VERSION "3.8.2"
+
+/* Skeleton name. */
+#define YYSKELETON_NAME "yacc.c"
+
+/* Pure parsers. */
+#define YYPURE 0
+
+/* Push parsers. */
+#define YYPUSH 0
+
+/* Pull parsers. */
+#define YYPULL 1
+
+
+
+
+/* First part of user prologue. */
+#line 7 "sys/cmd/rc/syntax.y"
+
+ #include "rc.h"
+
+ int yylex(void);
+ void yyerror(const char *);
+
+#line 78 "sys/cmd/rc/parse.c"
+
+# ifndef YY_CAST
+# ifdef __cplusplus
+# define YY_CAST(Type, Val) static_cast<Type> (Val)
+# define YY_REINTERPRET_CAST(Type, Val) reinterpret_cast<Type> (Val)
+# else
+# define YY_CAST(Type, Val) ((Type) (Val))
+# define YY_REINTERPRET_CAST(Type, Val) ((Type) (Val))
+# endif
+# endif
+# ifndef YY_NULLPTR
+# if defined __cplusplus
+# if 201103L <= __cplusplus
+# define YY_NULLPTR nullptr
+# else
+# define YY_NULLPTR 0
+# endif
+# else
+# define YY_NULLPTR ((void*)0)
+# endif
+# endif
+
+#include "parse.h"
+/* Symbol kind. */
+enum yysymbol_kind_t
+{
+ YYSYMBOL_YYEMPTY = -2,
+ YYSYMBOL_YYEOF = 0, /* "end of file" */
+ YYSYMBOL_YYerror = 1, /* error */
+ YYSYMBOL_YYUNDEF = 2, /* "invalid token" */
+ YYSYMBOL_Tfor = 3, /* Tfor */
+ YYSYMBOL_Tin = 4, /* Tin */
+ YYSYMBOL_Twhile = 5, /* Twhile */
+ YYSYMBOL_Tif = 6, /* Tif */
+ YYSYMBOL_Telse = 7, /* Telse */
+ YYSYMBOL_Tswitch = 8, /* Tswitch */
+ YYSYMBOL_Tcase = 9, /* Tcase */
+ YYSYMBOL_Tcasebody = 10, /* Tcasebody */
+ YYSYMBOL_Ttwiddle = 11, /* Ttwiddle */
+ YYSYMBOL_Tbang = 12, /* Tbang */
+ YYSYMBOL_Tsubshell = 13, /* Tsubshell */
+ YYSYMBOL_Tfunc = 14, /* Tfunc */
+ YYSYMBOL_Tredir = 15, /* Tredir */
+ YYSYMBOL_Tdup = 16, /* Tdup */
+ YYSYMBOL_Tpipe = 17, /* Tpipe */
+ YYSYMBOL_Tindex = 18, /* Tindex */
+ YYSYMBOL_Tbasic = 19, /* Tbasic */
+ YYSYMBOL_Targs = 20, /* Targs */
+ YYSYMBOL_Tword = 21, /* Tword */
+ YYSYMBOL_Twords = 22, /* Twords */
+ YYSYMBOL_Tparen = 23, /* Tparen */
+ YYSYMBOL_Tblock = 24, /* Tblock */
+ YYSYMBOL_25_ = 25, /* ')' */
+ YYSYMBOL_Tandand = 26, /* Tandand */
+ YYSYMBOL_Toror = 27, /* Toror */
+ YYSYMBOL_28_n_ = 28, /* '\n' */
+ YYSYMBOL_29_ = 29, /* '^' */
+ YYSYMBOL_30_ = 30, /* '$' */
+ YYSYMBOL_Tcount = 31, /* Tcount */
+ YYSYMBOL_Tjoin = 32, /* Tjoin */
+ YYSYMBOL_33_ = 33, /* '(' */
+ YYSYMBOL_34_ = 34, /* '{' */
+ YYSYMBOL_35_ = 35, /* '}' */
+ YYSYMBOL_36_ = 36, /* ';' */
+ YYSYMBOL_37_ = 37, /* '&' */
+ YYSYMBOL_38_ = 38, /* '=' */
+ YYSYMBOL_39_ = 39, /* '`' */
+ YYSYMBOL_YYACCEPT = 40, /* $accept */
+ YYSYMBOL_rc = 41, /* rc */
+ YYSYMBOL_line = 42, /* line */
+ YYSYMBOL_body = 43, /* body */
+ YYSYMBOL_paren = 44, /* paren */
+ YYSYMBOL_block = 45, /* block */
+ YYSYMBOL_cmds = 46, /* cmds */
+ YYSYMBOL_cmdsln = 47, /* cmdsln */
+ YYSYMBOL_ifbody = 48, /* ifbody */
+ YYSYMBOL_case = 49, /* case */
+ YYSYMBOL_casebody = 50, /* casebody */
+ YYSYMBOL_assign = 51, /* assign */
+ YYSYMBOL_redir = 52, /* redir */
+ YYSYMBOL_epilog = 53, /* epilog */
+ YYSYMBOL_cmd = 54, /* cmd */
+ YYSYMBOL_basic = 55, /* basic */
+ YYSYMBOL_atom = 56, /* atom */
+ YYSYMBOL_word = 57, /* word */
+ YYSYMBOL_executable = 58, /* executable */
+ YYSYMBOL_nonkeyword = 59, /* nonkeyword */
+ YYSYMBOL_keyword = 60, /* keyword */
+ YYSYMBOL_words = 61, /* words */
+ YYSYMBOL_wordsnl = 62, /* wordsnl */
+ YYSYMBOL_nl = 63 /* nl */
+};
+typedef enum yysymbol_kind_t yysymbol_kind_t;
+
+
+
+
+#ifdef short
+# undef short
+#endif
+
+/* On compilers that do not define __PTRDIFF_MAX__ etc., make sure
+ <limits.h> and (if available) <stdint.h> are included
+ so that the code can choose integer types of a good width. */
+
+#ifndef __PTRDIFF_MAX__
+# include <limits.h> /* INFRINGES ON USER NAME SPACE */
+# if defined __STDC_VERSION__ && 199901 <= __STDC_VERSION__
+# include <stdint.h> /* INFRINGES ON USER NAME SPACE */
+# define YY_STDINT_H
+# endif
+#endif
+
+/* Narrow types that promote to a signed type and that can represent a
+ signed or unsigned integer of at least N bits. In tables they can
+ save space and decrease cache pressure. Promoting to a signed type
+ helps avoid bugs in integer arithmetic. */
+
+#ifdef __INT_LEAST8_MAX__
+typedef __INT_LEAST8_TYPE__ yytype_int8;
+#elif defined YY_STDINT_H
+typedef int_least8_t yytype_int8;
+#else
+typedef signed char yytype_int8;
+#endif
+
+#ifdef __INT_LEAST16_MAX__
+typedef __INT_LEAST16_TYPE__ yytype_int16;
+#elif defined YY_STDINT_H
+typedef int_least16_t yytype_int16;
+#else
+typedef short yytype_int16;
+#endif
+
+/* Work around bug in HP-UX 11.23, which defines these macros
+ incorrectly for preprocessor constants. This workaround can likely
+ be removed in 2023, as HPE has promised support for HP-UX 11.23
+ (aka HP-UX 11i v2) only through the end of 2022; see Table 2 of
+ <https://h20195.www2.hpe.com/V2/getpdf.aspx/4AA4-7673ENW.pdf>. */
+#ifdef __hpux
+# undef UINT_LEAST8_MAX
+# undef UINT_LEAST16_MAX
+# define UINT_LEAST8_MAX 255
+# define UINT_LEAST16_MAX 65535
+#endif
+
+#if defined __UINT_LEAST8_MAX__ && __UINT_LEAST8_MAX__ <= __INT_MAX__
+typedef __UINT_LEAST8_TYPE__ yytype_uint8;
+#elif (!defined __UINT_LEAST8_MAX__ && defined YY_STDINT_H \
+ && UINT_LEAST8_MAX <= INT_MAX)
+typedef uint_least8_t yytype_uint8;
+#elif !defined __UINT_LEAST8_MAX__ && UCHAR_MAX <= INT_MAX
+typedef unsigned char yytype_uint8;
+#else
+typedef short yytype_uint8;
+#endif
+
+#if defined __UINT_LEAST16_MAX__ && __UINT_LEAST16_MAX__ <= __INT_MAX__
+typedef __UINT_LEAST16_TYPE__ yytype_uint16;
+#elif (!defined __UINT_LEAST16_MAX__ && defined YY_STDINT_H \
+ && UINT_LEAST16_MAX <= INT_MAX)
+typedef uint_least16_t yytype_uint16;
+#elif !defined __UINT_LEAST16_MAX__ && USHRT_MAX <= INT_MAX
+typedef unsigned short yytype_uint16;
+#else
+typedef int yytype_uint16;
+#endif
+
+#ifndef YYPTRDIFF_T
+# if defined __PTRDIFF_TYPE__ && defined __PTRDIFF_MAX__
+# define YYPTRDIFF_T __PTRDIFF_TYPE__
+# define YYPTRDIFF_MAXIMUM __PTRDIFF_MAX__
+# elif defined PTRDIFF_MAX
+# ifndef ptrdiff_t
+# include <stddef.h> /* INFRINGES ON USER NAME SPACE */
+# endif
+# define YYPTRDIFF_T ptrdiff_t
+# define YYPTRDIFF_MAXIMUM PTRDIFF_MAX
+# else
+# define YYPTRDIFF_T long
+# define YYPTRDIFF_MAXIMUM LONG_MAX
+# endif
+#endif
+
+#ifndef YYSIZE_T
+# ifdef __SIZE_TYPE__
+# define YYSIZE_T __SIZE_TYPE__
+# elif defined size_t
+# define YYSIZE_T size_t
+# elif defined __STDC_VERSION__ && 199901 <= __STDC_VERSION__
+# include <stddef.h> /* INFRINGES ON USER NAME SPACE */
+# define YYSIZE_T size_t
+# else
+# define YYSIZE_T unsigned
+# endif
+#endif
+
+#define YYSIZE_MAXIMUM \
+ YY_CAST (YYPTRDIFF_T, \
+ (YYPTRDIFF_MAXIMUM < YY_CAST (YYSIZE_T, -1) \
+ ? YYPTRDIFF_MAXIMUM \
+ : YY_CAST (YYSIZE_T, -1)))
+
+#define YYSIZEOF(X) YY_CAST (YYPTRDIFF_T, sizeof (X))
+
+
+/* Stored state numbers (used for stacks). */
+typedef yytype_uint8 yy_state_t;
+
+/* State numbers in computations. */
+typedef int yy_state_fast_t;
+
+#ifndef YY_
+# if defined YYENABLE_NLS && YYENABLE_NLS
+# if ENABLE_NLS
+# include <libintl.h> /* INFRINGES ON USER NAME SPACE */
+# define YY_(Msgid) dgettext ("bison-runtime", Msgid)
+# endif
+# endif
+# ifndef YY_
+# define YY_(Msgid) Msgid
+# endif
+#endif
+
+
+#ifndef YY_ATTRIBUTE_PURE
+# if defined __GNUC__ && 2 < __GNUC__ + (96 <= __GNUC_MINOR__)
+# define YY_ATTRIBUTE_PURE __attribute__ ((__pure__))
+# else
+# define YY_ATTRIBUTE_PURE
+# endif
+#endif
+
+#ifndef YY_ATTRIBUTE_UNUSED
+# if defined __GNUC__ && 2 < __GNUC__ + (7 <= __GNUC_MINOR__)
+# define YY_ATTRIBUTE_UNUSED __attribute__ ((__unused__))
+# else
+# define YY_ATTRIBUTE_UNUSED
+# endif
+#endif
+
+/* Suppress unused-variable warnings by "using" E. */
+#if ! defined lint || defined __GNUC__
+# define YY_USE(E) ((void) (E))
+#else
+# define YY_USE(E) /* empty */
+#endif
+
+/* Suppress an incorrect diagnostic about yylval being uninitialized. */
+#if defined __GNUC__ && ! defined __ICC && 406 <= __GNUC__ * 100 + __GNUC_MINOR__
+# if __GNUC__ * 100 + __GNUC_MINOR__ < 407
+# define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN \
+ _Pragma ("GCC diagnostic push") \
+ _Pragma ("GCC diagnostic ignored \"-Wuninitialized\"")
+# else
+# define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN \
+ _Pragma ("GCC diagnostic push") \
+ _Pragma ("GCC diagnostic ignored \"-Wuninitialized\"") \
+ _Pragma ("GCC diagnostic ignored \"-Wmaybe-uninitialized\"")
+# endif
+# define YY_IGNORE_MAYBE_UNINITIALIZED_END \
+ _Pragma ("GCC diagnostic pop")
+#else
+# define YY_INITIAL_VALUE(Value) Value
+#endif
+#ifndef YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
+# define YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
+# define YY_IGNORE_MAYBE_UNINITIALIZED_END
+#endif
+#ifndef YY_INITIAL_VALUE
+# define YY_INITIAL_VALUE(Value) /* Nothing. */
+#endif
+
+#if defined __cplusplus && defined __GNUC__ && ! defined __ICC && 6 <= __GNUC__
+# define YY_IGNORE_USELESS_CAST_BEGIN \
+ _Pragma ("GCC diagnostic push") \
+ _Pragma ("GCC diagnostic ignored \"-Wuseless-cast\"")
+# define YY_IGNORE_USELESS_CAST_END \
+ _Pragma ("GCC diagnostic pop")
+#endif
+#ifndef YY_IGNORE_USELESS_CAST_BEGIN
+# define YY_IGNORE_USELESS_CAST_BEGIN
+# define YY_IGNORE_USELESS_CAST_END
+#endif
+
+
+#define YY_ASSERT(E) ((void) (0 && (E)))
+
+#if 1
+
+/* The parser invokes alloca or malloc; define the necessary symbols. */
+
+# ifdef YYSTACK_USE_ALLOCA
+# if YYSTACK_USE_ALLOCA
+# ifdef __GNUC__
+# define YYSTACK_ALLOC __builtin_alloca
+# elif defined __BUILTIN_VA_ARG_INCR
+# include <alloca.h> /* INFRINGES ON USER NAME SPACE */
+# elif defined _AIX
+# define YYSTACK_ALLOC __alloca
+# elif defined _MSC_VER
+# include <malloc.h> /* INFRINGES ON USER NAME SPACE */
+# define alloca _alloca
+# else
+# define YYSTACK_ALLOC alloca
+# if ! defined _ALLOCA_H && ! defined EXIT_SUCCESS
+# include <stdlib.h> /* INFRINGES ON USER NAME SPACE */
+ /* Use EXIT_SUCCESS as a witness for stdlib.h. */
+# ifndef EXIT_SUCCESS
+# define EXIT_SUCCESS 0
+# endif
+# endif
+# endif
+# endif
+# endif
+
+# ifdef YYSTACK_ALLOC
+ /* Pacify GCC's 'empty if-body' warning. */
+# define YYSTACK_FREE(Ptr) do { /* empty */; } while (0)
+# ifndef YYSTACK_ALLOC_MAXIMUM
+ /* The OS might guarantee only one guard page at the bottom of the stack,
+ and a page size can be as small as 4096 bytes. So we cannot safely
+ invoke alloca (N) if N exceeds 4096. Use a slightly smaller number
+ to allow for a few compiler-allocated temporary stack slots. */
+# define YYSTACK_ALLOC_MAXIMUM 4032 /* reasonable circa 2006 */
+# endif
+# else
+# define YYSTACK_ALLOC YYMALLOC
+# define YYSTACK_FREE YYFREE
+# ifndef YYSTACK_ALLOC_MAXIMUM
+# define YYSTACK_ALLOC_MAXIMUM YYSIZE_MAXIMUM
+# endif
+# if (defined __cplusplus && ! defined EXIT_SUCCESS \
+ && ! ((defined YYMALLOC || defined malloc) \
+ && (defined YYFREE || defined free)))
+# include <stdlib.h> /* INFRINGES ON USER NAME SPACE */
+# ifndef EXIT_SUCCESS
+# define EXIT_SUCCESS 0
+# endif
+# endif
+# ifndef YYMALLOC
+# define YYMALLOC malloc
+# if ! defined malloc && ! defined EXIT_SUCCESS
+void *malloc (YYSIZE_T); /* INFRINGES ON USER NAME SPACE */
+# endif
+# endif
+# ifndef YYFREE
+# define YYFREE free
+# if ! defined free && ! defined EXIT_SUCCESS
+void free (void *); /* INFRINGES ON USER NAME SPACE */
+# endif
+# endif
+# endif
+#endif /* 1 */
+
+#if (! defined yyoverflow \
+ && (! defined __cplusplus \
+ || (defined YYSTYPE_IS_TRIVIAL && YYSTYPE_IS_TRIVIAL)))
+
+/* A type that is properly aligned for any stack member. */
+union yyalloc
+{
+ yy_state_t yyss_alloc;
+ YYSTYPE yyvs_alloc;
+};
+
+/* The size of the maximum gap between one aligned stack and the next. */
+# define YYSTACK_GAP_MAXIMUM (YYSIZEOF (union yyalloc) - 1)
+
+/* The size of an array large to enough to hold all stacks, each with
+ N elements. */
+# define YYSTACK_BYTES(N) \
+ ((N) * (YYSIZEOF (yy_state_t) + YYSIZEOF (YYSTYPE)) \
+ + YYSTACK_GAP_MAXIMUM)
+
+# define YYCOPY_NEEDED 1
+
+/* Relocate STACK from its old location to the new one. The
+ local variables YYSIZE and YYSTACKSIZE give the old and new number of
+ elements in the stack, and YYPTR gives the new location of the
+ stack. Advance YYPTR to a properly aligned location for the next
+ stack. */
+# define YYSTACK_RELOCATE(Stack_alloc, Stack) \
+ do \
+ { \
+ YYPTRDIFF_T yynewbytes; \
+ YYCOPY (&yyptr->Stack_alloc, Stack, yysize); \
+ Stack = &yyptr->Stack_alloc; \
+ yynewbytes = yystacksize * YYSIZEOF (*Stack) + YYSTACK_GAP_MAXIMUM; \
+ yyptr += yynewbytes / YYSIZEOF (*yyptr); \
+ } \
+ while (0)
+
+#endif
+
+#if defined YYCOPY_NEEDED && YYCOPY_NEEDED
+/* Copy COUNT objects from SRC to DST. The source and destination do
+ not overlap. */
+# ifndef YYCOPY
+# if defined __GNUC__ && 1 < __GNUC__
+# define YYCOPY(Dst, Src, Count) \
+ __builtin_memcpy (Dst, Src, YY_CAST (YYSIZE_T, (Count)) * sizeof (*(Src)))
+# else
+# define YYCOPY(Dst, Src, Count) \
+ do \
+ { \
+ YYPTRDIFF_T yyi; \
+ for (yyi = 0; yyi < (Count); yyi++) \
+ (Dst)[yyi] = (Src)[yyi]; \
+ } \
+ while (0)
+# endif
+# endif
+#endif /* !YYCOPY_NEEDED */
+
+/* YYFINAL -- State number of the termination state. */
+#define YYFINAL 56
+/* YYLAST -- Last index in YYTABLE. */
+#define YYLAST 478
+
+/* YYNTOKENS -- Number of terminals. */
+#define YYNTOKENS 40
+/* YYNNTS -- Number of nonterminals. */
+#define YYNNTS 24
+/* YYNRULES -- Number of rules. */
+#define YYNRULES 73
+/* YYNSTATES -- Number of states. */
+#define YYNSTATES 129
+
+/* YYMAXUTOK -- Last valid token kind. */
+#define YYMAXUTOK 283
+
+
+/* YYTRANSLATE(TOKEN-NUM) -- Symbol number corresponding to TOKEN-NUM
+ as returned by yylex, with out-of-bounds checking. */
+#define YYTRANSLATE(YYX) \
+ (0 <= (YYX) && (YYX) <= YYMAXUTOK \
+ ? YY_CAST (yysymbol_kind_t, yytranslate[YYX]) \
+ : YYSYMBOL_YYUNDEF)
+
+/* YYTRANSLATE[TOKEN-NUM] -- Symbol number corresponding to TOKEN-NUM
+ as returned by yylex. */
+static const yytype_int8 yytranslate[] =
+{
+ 0, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+ 28, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+ 2, 2, 2, 2, 2, 2, 30, 2, 37, 2,
+ 33, 25, 2, 2, 2, 2, 2, 2, 2, 2,
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 36,
+ 2, 38, 2, 2, 2, 2, 2, 2, 2, 2,
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+ 2, 2, 2, 2, 29, 2, 39, 2, 2, 2,
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+ 2, 2, 2, 34, 2, 35, 2, 2, 2, 2,
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+ 2, 2, 2, 2, 2, 2, 2, 2, 2, 2,
+ 2, 2, 2, 2, 2, 2, 1, 2, 3, 4,
+ 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
+ 15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
+ 26, 27, 31, 32
+};
+
+#if YYDEBUG
+/* YYRLINE[YYN] -- Source line where rule number YYN was defined. */
+static const yytype_uint8 yyrline[] =
+{
+ 0, 38, 38, 39, 42, 43, 46, 47, 50, 53,
+ 56, 57, 60, 61, 64, 65, 68, 69, 72, 73,
+ 74, 77, 80, 81, 84, 85, 88, 89, 90, 91,
+ 92, 93, 94, 95, 96, 97, 98, 99, 100, 101,
+ 102, 105, 106, 107, 110, 111, 114, 115, 118, 119,
+ 122, 123, 124, 125, 126, 127, 128, 132, 132, 132,
+ 132, 132, 132, 132, 132, 132, 132, 135, 136, 139,
+ 140, 141, 143, 145
+};
+#endif
+
+/** Accessing symbol of state STATE. */
+#define YY_ACCESSING_SYMBOL(State) YY_CAST (yysymbol_kind_t, yystos[State])
+
+#if 1
+/* The user-facing name of the symbol whose (internal) number is
+ YYSYMBOL. No bounds checking. */
+static const char *yysymbol_name (yysymbol_kind_t yysymbol) YY_ATTRIBUTE_UNUSED;
+
+/* YYTNAME[SYMBOL-NUM] -- String name of the symbol SYMBOL-NUM.
+ First, the terminals, then, starting at YYNTOKENS, nonterminals. */
+static const char *const yytname[] =
+{
+ "\"end of file\"", "error", "\"invalid token\"", "Tfor", "Tin",
+ "Twhile", "Tif", "Telse", "Tswitch", "Tcase", "Tcasebody", "Ttwiddle",
+ "Tbang", "Tsubshell", "Tfunc", "Tredir", "Tdup", "Tpipe", "Tindex",
+ "Tbasic", "Targs", "Tword", "Twords", "Tparen", "Tblock", "')'",
+ "Tandand", "Toror", "'\\n'", "'^'", "'$'", "Tcount", "Tjoin", "'('",
+ "'{'", "'}'", "';'", "'&'", "'='", "'`'", "$accept", "rc", "line",
+ "body", "paren", "block", "cmds", "cmdsln", "ifbody", "case", "casebody",
+ "assign", "redir", "epilog", "cmd", "basic", "atom", "word",
+ "executable", "nonkeyword", "keyword", "words", "wordsnl", "nl", YY_NULLPTR
+};
+
+static const char *
+yysymbol_name (yysymbol_kind_t yysymbol)
+{
+ return yytname[yysymbol];
+}
+#endif
+
+#define YYPACT_NINF (-82)
+
+#define yypact_value_is_default(Yyn) \
+ ((Yyn) == YYPACT_NINF)
+
+#define YYTABLE_NINF (-3)
+
+#define yytable_value_is_error(Yyn) \
+ 0
+
+/* YYPACT[STATE-NUM] -- Index in YYTABLE of the portion describing
+ STATE-NUM. */
+static const yytype_int16 yypact[] =
+{
+ 121, -17, -2, -2, 5, 439, 439, 343, -82, -82,
+ 343, 343, 343, -82, 439, -23, 45, 32, 11, 439,
+ 439, 439, 13, 158, -14, -82, 343, 439, -82, -82,
+ 343, 30, 30, -82, -82, -82, -82, -82, -82, -82,
+ -82, -82, -82, -82, 34, -82, -82, 47, -82, -82,
+ 195, 41, -82, 439, 54, -82, -82, -82, 11, -82,
+ -82, 30, 30, -82, -82, -82, -82, -82, -82, 34,
+ 343, 343, 19, 44, 375, 375, 4, 343, -82, -82,
+ -82, 34, -82, -82, -82, -82, 375, 375, 375, -82,
+ 34, -82, -82, -82, -82, 29, 77, -82, 29, -82,
+ -82, 269, -82, 30, 30, 306, 375, -82, 25, -82,
+ 34, -82, 29, 375, 407, 375, 29, -82, 407, 407,
+ 48, 54, 29, 232, -82, -82, -82, -82, -82
+};
+
+/* YYDEFACT[STATE-NUM] -- Default reduction number in state STATE-NUM.
+ Performed when YYTABLE does not specify something else to do. Zero
+ means the default is an error. */
+static const yytype_int8 yydefact[] =
+{
+ 26, 0, 0, 0, 0, 26, 26, 0, 22, 50,
+ 0, 0, 0, 69, 26, 0, 0, 0, 24, 26,
+ 26, 26, 4, 27, 41, 48, 0, 26, 72, 72,
+ 0, 34, 35, 57, 58, 59, 60, 61, 62, 63,
+ 64, 65, 66, 46, 23, 44, 45, 51, 54, 55,
+ 0, 0, 12, 26, 6, 56, 1, 3, 24, 28,
+ 5, 33, 32, 72, 72, 72, 10, 11, 43, 42,
+ 0, 0, 0, 0, 26, 26, 0, 0, 67, 53,
+ 70, 71, 9, 7, 13, 25, 26, 26, 26, 49,
+ 21, 67, 72, 8, 73, 38, 24, 39, 14, 72,
+ 47, 0, 29, 30, 31, 0, 26, 72, 0, 52,
+ 68, 72, 36, 26, 26, 26, 15, 67, 26, 26,
+ 0, 18, 37, 0, 20, 19, 40, 17, 16
+};
+
+/* YYPGOTO[NTERM-NUM]. */
+static const yytype_int8 yypgoto[] =
+{
+ -82, -82, 75, -19, 93, -11, 18, -52, -82, -82,
+ -21, -82, -1, 42, 0, -82, -9, 28, -82, 2,
+ -82, -81, -82, -22
+};
+
+/* YYDEFGOTO[NTERM-NUM]. */
+static const yytype_int8 yydefgoto[] =
+{
+ 0, 16, 17, 51, 28, 18, 52, 53, 97, 119,
+ 120, 20, 21, 59, 54, 23, 43, 110, 24, 25,
+ 46, 101, 50, 74
+};
+
+/* YYTABLE[YYPACT[STATE-NUM]] -- What to do in state STATE-NUM. If
+ positive, shift that token. If negative, reduce the rule whose
+ number is the opposite. If YYTABLE_NINF, syntax error. */
+static const yytype_int16 yytable[] =
+{
+ 22, 47, 48, 49, 55, 31, 32, 75, 73, 45,
+ 105, 14, 45, 45, 45, 70, 26, 58, 19, 22,
+ 61, 62, 68, 91, 71, 45, 7, 8, 45, 99,
+ 63, 27, 45, 77, 83, 44, 123, 19, 30, 64,
+ 65, 86, 87, 88, 92, 56, 63, 63, 77, 66,
+ 67, 69, 45, 94, 72, 64, 65, 58, 76, 114,
+ 57, 89, 118, 77, 96, 78, 118, 118, 100, 93,
+ 106, 63, 45, 45, 95, 98, 82, 108, 81, 45,
+ 64, 65, 84, 126, 107, 113, 102, 103, 104, 115,
+ 66, 67, 7, 8, 60, 58, 29, 124, 125, 90,
+ 85, 0, 0, 45, 0, 0, 112, 45, 0, 0,
+ 0, 0, 0, 116, 121, 122, 0, 0, 121, 121,
+ 0, -2, 0, 0, 1, 45, 2, 3, 0, 4,
+ 0, 0, 0, 5, 6, 0, 7, 8, 0, 0,
+ 0, 0, 9, 0, 0, 0, 0, 0, 0, 0,
+ 0, 10, 11, 12, 13, 14, 0, 0, 0, 0,
+ 15, 33, 34, 35, 36, 37, 38, 39, 0, 0,
+ 40, 41, 42, 7, 8, 0, 0, 0, 0, 9,
+ 0, 0, 0, 0, 0, 0, 0, 0, 10, 11,
+ 12, 13, 0, 0, 0, 0, 0, 15, 33, 34,
+ 35, 36, 37, 38, 39, 0, 0, 40, 41, 42,
+ 0, 0, 0, 0, 0, 0, 9, 0, 0, 0,
+ 79, 0, 0, 80, 0, 10, 11, 12, 13, 0,
+ 0, 0, 0, 0, 15, 33, 34, 35, 36, 37,
+ 38, 39, 0, 0, 40, 41, 42, 0, 0, 0,
+ 0, 0, 0, 9, 0, 0, 0, 0, 0, 0,
+ 127, 0, 10, 11, 12, 13, 0, 0, 128, 0,
+ 0, 15, 33, 34, 35, 36, 37, 38, 39, 0,
+ 0, 40, 41, 42, 0, 0, 0, 0, 0, 0,
+ 9, 0, 0, 0, 109, 0, 0, 0, 0, 10,
+ 11, 12, 13, 0, 0, 0, 0, 0, 15, 33,
+ 34, 35, 36, 37, 38, 39, 0, 0, 40, 41,
+ 42, 0, 0, 0, 0, 0, 0, 9, 0, 0,
+ 0, 111, 0, 0, 0, 0, 10, 11, 12, 13,
+ 0, 0, 0, 0, 0, 15, 33, 34, 35, 36,
+ 37, 38, 39, 0, 0, 40, 41, 42, 0, 0,
+ 0, 0, 0, 0, 9, 0, 0, 0, 0, 0,
+ 0, 0, 0, 10, 11, 12, 13, 0, 1, 0,
+ 2, 3, 15, 4, 0, 0, 0, 5, 6, 0,
+ 7, 8, 0, 0, 0, 0, 9, 0, 0, 0,
+ 0, 0, 0, 94, 0, 10, 11, 12, 13, 14,
+ 1, 0, 2, 3, 15, 4, 117, 0, 0, 5,
+ 6, 0, 7, 8, 0, 0, 0, 0, 9, 0,
+ 0, 0, 0, 0, 0, 0, 0, 10, 11, 12,
+ 13, 14, 1, 0, 2, 3, 15, 4, 0, 0,
+ 0, 5, 6, 0, 7, 8, 0, 0, 0, 0,
+ 9, 0, 0, 0, 0, 0, 0, 0, 0, 10,
+ 11, 12, 13, 14, 0, 0, 0, 0, 15
+};
+
+static const yytype_int8 yycheck[] =
+{
+ 0, 10, 11, 12, 15, 5, 6, 29, 27, 7,
+ 91, 34, 10, 11, 12, 29, 33, 18, 0, 19,
+ 20, 21, 23, 4, 38, 23, 15, 16, 26, 25,
+ 17, 33, 30, 29, 53, 7, 117, 19, 33, 26,
+ 27, 63, 64, 65, 25, 0, 17, 17, 29, 36,
+ 37, 23, 50, 28, 26, 26, 27, 58, 30, 34,
+ 28, 70, 114, 29, 75, 18, 118, 119, 77, 25,
+ 92, 17, 70, 71, 74, 75, 35, 99, 50, 77,
+ 26, 27, 28, 35, 7, 107, 86, 87, 88, 111,
+ 36, 37, 15, 16, 19, 96, 3, 118, 119, 71,
+ 58, -1, -1, 101, -1, -1, 106, 105, -1, -1,
+ -1, -1, -1, 113, 114, 115, -1, -1, 118, 119,
+ -1, 0, -1, -1, 3, 123, 5, 6, -1, 8,
+ -1, -1, -1, 12, 13, -1, 15, 16, -1, -1,
+ -1, -1, 21, -1, -1, -1, -1, -1, -1, -1,
+ -1, 30, 31, 32, 33, 34, -1, -1, -1, -1,
+ 39, 3, 4, 5, 6, 7, 8, 9, -1, -1,
+ 12, 13, 14, 15, 16, -1, -1, -1, -1, 21,
+ -1, -1, -1, -1, -1, -1, -1, -1, 30, 31,
+ 32, 33, -1, -1, -1, -1, -1, 39, 3, 4,
+ 5, 6, 7, 8, 9, -1, -1, 12, 13, 14,
+ -1, -1, -1, -1, -1, -1, 21, -1, -1, -1,
+ 25, -1, -1, 28, -1, 30, 31, 32, 33, -1,
+ -1, -1, -1, -1, 39, 3, 4, 5, 6, 7,
+ 8, 9, -1, -1, 12, 13, 14, -1, -1, -1,
+ -1, -1, -1, 21, -1, -1, -1, -1, -1, -1,
+ 28, -1, 30, 31, 32, 33, -1, -1, 36, -1,
+ -1, 39, 3, 4, 5, 6, 7, 8, 9, -1,
+ -1, 12, 13, 14, -1, -1, -1, -1, -1, -1,
+ 21, -1, -1, -1, 25, -1, -1, -1, -1, 30,
+ 31, 32, 33, -1, -1, -1, -1, -1, 39, 3,
+ 4, 5, 6, 7, 8, 9, -1, -1, 12, 13,
+ 14, -1, -1, -1, -1, -1, -1, 21, -1, -1,
+ -1, 25, -1, -1, -1, -1, 30, 31, 32, 33,
+ -1, -1, -1, -1, -1, 39, 3, 4, 5, 6,
+ 7, 8, 9, -1, -1, 12, 13, 14, -1, -1,
+ -1, -1, -1, -1, 21, -1, -1, -1, -1, -1,
+ -1, -1, -1, 30, 31, 32, 33, -1, 3, -1,
+ 5, 6, 39, 8, -1, -1, -1, 12, 13, -1,
+ 15, 16, -1, -1, -1, -1, 21, -1, -1, -1,
+ -1, -1, -1, 28, -1, 30, 31, 32, 33, 34,
+ 3, -1, 5, 6, 39, 8, 9, -1, -1, 12,
+ 13, -1, 15, 16, -1, -1, -1, -1, 21, -1,
+ -1, -1, -1, -1, -1, -1, -1, 30, 31, 32,
+ 33, 34, 3, -1, 5, 6, 39, 8, -1, -1,
+ -1, 12, 13, -1, 15, 16, -1, -1, -1, -1,
+ 21, -1, -1, -1, -1, -1, -1, -1, -1, 30,
+ 31, 32, 33, 34, -1, -1, -1, -1, 39
+};
+
+/* YYSTOS[STATE-NUM] -- The symbol kind of the accessing symbol of
+ state STATE-NUM. */
+static const yytype_int8 yystos[] =
+{
+ 0, 3, 5, 6, 8, 12, 13, 15, 16, 21,
+ 30, 31, 32, 33, 34, 39, 41, 42, 45, 46,
+ 51, 52, 54, 55, 58, 59, 33, 33, 44, 44,
+ 33, 54, 54, 3, 4, 5, 6, 7, 8, 9,
+ 12, 13, 14, 56, 57, 59, 60, 56, 56, 56,
+ 62, 43, 46, 47, 54, 45, 0, 28, 52, 53,
+ 42, 54, 54, 17, 26, 27, 36, 37, 52, 57,
+ 29, 38, 57, 43, 63, 63, 57, 29, 18, 25,
+ 28, 57, 35, 43, 28, 53, 63, 63, 63, 56,
+ 57, 4, 25, 25, 28, 54, 45, 48, 54, 25,
+ 56, 61, 54, 54, 54, 61, 63, 7, 63, 25,
+ 57, 25, 54, 63, 34, 63, 54, 9, 47, 49,
+ 50, 54, 54, 61, 50, 50, 35, 28, 36
+};
+
+/* YYR1[RULE-NUM] -- Symbol kind of the left-hand side of rule RULE-NUM. */
+static const yytype_int8 yyr1[] =
+{
+ 0, 40, 41, 41, 42, 42, 43, 43, 44, 45,
+ 46, 46, 47, 47, 48, 48, 49, 49, 50, 50,
+ 50, 51, 52, 52, 53, 53, 54, 54, 54, 54,
+ 54, 54, 54, 54, 54, 54, 54, 54, 54, 54,
+ 54, 55, 55, 55, 56, 56, 57, 57, 58, 58,
+ 59, 59, 59, 59, 59, 59, 59, 60, 60, 60,
+ 60, 60, 60, 60, 60, 60, 60, 61, 61, 62,
+ 62, 62, 63, 63
+};
+
+/* YYR2[RULE-NUM] -- Number of symbols on the right-hand side of rule RULE-NUM. */
+static const yytype_int8 yyr2[] =
+{
+ 0, 2, 0, 2, 1, 2, 1, 2, 3, 3,
+ 2, 2, 1, 2, 1, 4, 3, 3, 1, 2,
+ 2, 3, 1, 2, 0, 2, 0, 1, 2, 4,
+ 4, 4, 2, 2, 2, 2, 6, 8, 4, 4,
+ 8, 1, 2, 2, 1, 1, 1, 3, 1, 3,
+ 1, 2, 5, 3, 2, 2, 2, 1, 1, 1,
+ 1, 1, 1, 1, 1, 1, 1, 0, 2, 0,
+ 2, 2, 0, 2
+};
+
+
+enum { YYENOMEM = -2 };
+
+#define yyerrok (yyerrstatus = 0)
+#define yyclearin (yychar = YYEMPTY)
+
+#define YYACCEPT goto yyacceptlab
+#define YYABORT goto yyabortlab
+#define YYERROR goto yyerrorlab
+#define YYNOMEM goto yyexhaustedlab
+
+
+#define YYRECOVERING() (!!yyerrstatus)
+
+#define YYBACKUP(Token, Value) \
+ do \
+ if (yychar == YYEMPTY) \
+ { \
+ yychar = (Token); \
+ yylval = (Value); \
+ YYPOPSTACK (yylen); \
+ yystate = *yyssp; \
+ goto yybackup; \
+ } \
+ else \
+ { \
+ yyerror (YY_("syntax error: cannot back up")); \
+ YYERROR; \
+ } \
+ while (0)
+
+/* Backward compatibility with an undocumented macro.
+ Use YYerror or YYUNDEF. */
+#define YYERRCODE YYUNDEF
+
+
+/* Enable debugging if requested. */
+#if YYDEBUG
+
+# ifndef YYFPRINTF
+# include <stdio.h> /* INFRINGES ON USER NAME SPACE */
+# define YYFPRINTF fprintf
+# endif
+
+# define YYDPRINTF(Args) \
+do { \
+ if (yydebug) \
+ YYFPRINTF Args; \
+} while (0)
+
+
+
+
+# define YY_SYMBOL_PRINT(Title, Kind, Value, Location) \
+do { \
+ if (yydebug) \
+ { \
+ YYFPRINTF (stderr, "%s ", Title); \
+ yy_symbol_print (stderr, \
+ Kind, Value); \
+ YYFPRINTF (stderr, "\n"); \
+ } \
+} while (0)
+
+
+/*-----------------------------------.
+| Print this symbol's value on YYO. |
+`-----------------------------------*/
+
+static void
+yy_symbol_value_print (FILE *yyo,
+ yysymbol_kind_t yykind, YYSTYPE const * const yyvaluep)
+{
+ FILE *yyoutput = yyo;
+ YY_USE (yyoutput);
+ if (!yyvaluep)
+ return;
+ YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
+ YY_USE (yykind);
+ YY_IGNORE_MAYBE_UNINITIALIZED_END
+}
+
+
+/*---------------------------.
+| Print this symbol on YYO. |
+`---------------------------*/
+
+static void
+yy_symbol_print (FILE *yyo,
+ yysymbol_kind_t yykind, YYSTYPE const * const yyvaluep)
+{
+ YYFPRINTF (yyo, "%s %s (",
+ yykind < YYNTOKENS ? "token" : "nterm", yysymbol_name (yykind));
+
+ yy_symbol_value_print (yyo, yykind, yyvaluep);
+ YYFPRINTF (yyo, ")");
+}
+
+/*------------------------------------------------------------------.
+| yy_stack_print -- Print the state stack from its BOTTOM up to its |
+| TOP (included). |
+`------------------------------------------------------------------*/
+
+static void
+yy_stack_print (yy_state_t *yybottom, yy_state_t *yytop)
+{
+ YYFPRINTF (stderr, "Stack now");
+ for (; yybottom <= yytop; yybottom++)
+ {
+ int yybot = *yybottom;
+ YYFPRINTF (stderr, " %d", yybot);
+ }
+ YYFPRINTF (stderr, "\n");
+}
+
+# define YY_STACK_PRINT(Bottom, Top) \
+do { \
+ if (yydebug) \
+ yy_stack_print ((Bottom), (Top)); \
+} while (0)
+
+
+/*------------------------------------------------.
+| Report that the YYRULE is going to be reduced. |
+`------------------------------------------------*/
+
+static void
+yy_reduce_print (yy_state_t *yyssp, YYSTYPE *yyvsp,
+ int yyrule)
+{
+ int yylno = yyrline[yyrule];
+ int yynrhs = yyr2[yyrule];
+ int yyi;
+ YYFPRINTF (stderr, "Reducing stack by rule %d (line %d):\n",
+ yyrule - 1, yylno);
+ /* The symbols being reduced. */
+ for (yyi = 0; yyi < yynrhs; yyi++)
+ {
+ YYFPRINTF (stderr, " $%d = ", yyi + 1);
+ yy_symbol_print (stderr,
+ YY_ACCESSING_SYMBOL (+yyssp[yyi + 1 - yynrhs]),
+ &yyvsp[(yyi + 1) - (yynrhs)]);
+ YYFPRINTF (stderr, "\n");
+ }
+}
+
+# define YY_REDUCE_PRINT(Rule) \
+do { \
+ if (yydebug) \
+ yy_reduce_print (yyssp, yyvsp, Rule); \
+} while (0)
+
+/* Nonzero means print parse trace. It is left uninitialized so that
+ multiple parsers can coexist. */
+int yydebug;
+#else /* !YYDEBUG */
+# define YYDPRINTF(Args) ((void) 0)
+# define YY_SYMBOL_PRINT(Title, Kind, Value, Location)
+# define YY_STACK_PRINT(Bottom, Top)
+# define YY_REDUCE_PRINT(Rule)
+#endif /* !YYDEBUG */
+
+
+/* YYINITDEPTH -- initial size of the parser's stacks. */
+#ifndef YYINITDEPTH
+# define YYINITDEPTH 200
+#endif
+
+/* YYMAXDEPTH -- maximum size the stacks can grow to (effective only
+ if the built-in stack extension method is used).
+
+ Do not make this value too large; the results are undefined if
+ YYSTACK_ALLOC_MAXIMUM < YYSTACK_BYTES (YYMAXDEPTH)
+ evaluated with infinite-precision integer arithmetic. */
+
+#ifndef YYMAXDEPTH
+# define YYMAXDEPTH 10000
+#endif
+
+
+/* Context of a parse error. */
+typedef struct
+{
+ yy_state_t *yyssp;
+ yysymbol_kind_t yytoken;
+} yypcontext_t;
+
+/* Put in YYARG at most YYARGN of the expected tokens given the
+ current YYCTX, and return the number of tokens stored in YYARG. If
+ YYARG is null, return the number of expected tokens (guaranteed to
+ be less than YYNTOKENS). Return YYENOMEM on memory exhaustion.
+ Return 0 if there are more than YYARGN expected tokens, yet fill
+ YYARG up to YYARGN. */
+static int
+yypcontext_expected_tokens (const yypcontext_t *yyctx,
+ yysymbol_kind_t yyarg[], int yyargn)
+{
+ /* Actual size of YYARG. */
+ int yycount = 0;
+ int yyn = yypact[+*yyctx->yyssp];
+ if (!yypact_value_is_default (yyn))
+ {
+ /* Start YYX at -YYN if negative to avoid negative indexes in
+ YYCHECK. In other words, skip the first -YYN actions for
+ this state because they are default actions. */
+ int yyxbegin = yyn < 0 ? -yyn : 0;
+ /* Stay within bounds of both yycheck and yytname. */
+ int yychecklim = YYLAST - yyn + 1;
+ int yyxend = yychecklim < YYNTOKENS ? yychecklim : YYNTOKENS;
+ int yyx;
+ for (yyx = yyxbegin; yyx < yyxend; ++yyx)
+ if (yycheck[yyx + yyn] == yyx && yyx != YYSYMBOL_YYerror
+ && !yytable_value_is_error (yytable[yyx + yyn]))
+ {
+ if (!yyarg)
+ ++yycount;
+ else if (yycount == yyargn)
+ return 0;
+ else
+ yyarg[yycount++] = YY_CAST (yysymbol_kind_t, yyx);
+ }
+ }
+ if (yyarg && yycount == 0 && 0 < yyargn)
+ yyarg[0] = YYSYMBOL_YYEMPTY;
+ return yycount;
+}
+
+
+
+
+#ifndef yystrlen
+# if defined __GLIBC__ && defined _STRING_H
+# define yystrlen(S) (YY_CAST (YYPTRDIFF_T, strlen (S)))
+# else
+/* Return the length of YYSTR. */
+static YYPTRDIFF_T
+yystrlen (const char *yystr)
+{
+ YYPTRDIFF_T yylen;
+ for (yylen = 0; yystr[yylen]; yylen++)
+ continue;
+ return yylen;
+}
+# endif
+#endif
+
+#ifndef yystpcpy
+# if defined __GLIBC__ && defined _STRING_H && defined _GNU_SOURCE
+# define yystpcpy stpcpy
+# else
+/* Copy YYSRC to YYDEST, returning the address of the terminating '\0' in
+ YYDEST. */
+static char *
+yystpcpy (char *yydest, const char *yysrc)
+{
+ char *yyd = yydest;
+ const char *yys = yysrc;
+
+ while ((*yyd++ = *yys++) != '\0')
+ continue;
+
+ return yyd - 1;
+}
+# endif
+#endif
+
+#ifndef yytnamerr
+/* Copy to YYRES the contents of YYSTR after stripping away unnecessary
+ quotes and backslashes, so that it's suitable for yyerror. The
+ heuristic is that double-quoting is unnecessary unless the string
+ contains an apostrophe, a comma, or backslash (other than
+ backslash-backslash). YYSTR is taken from yytname. If YYRES is
+ null, do not copy; instead, return the length of what the result
+ would have been. */
+static YYPTRDIFF_T
+yytnamerr (char *yyres, const char *yystr)
+{
+ if (*yystr == '"')
+ {
+ YYPTRDIFF_T yyn = 0;
+ char const *yyp = yystr;
+ for (;;)
+ switch (*++yyp)
+ {
+ case '\'':
+ case ',':
+ goto do_not_strip_quotes;
+
+ case '\\':
+ if (*++yyp != '\\')
+ goto do_not_strip_quotes;
+ else
+ goto append;
+
+ append:
+ default:
+ if (yyres)
+ yyres[yyn] = *yyp;
+ yyn++;
+ break;
+
+ case '"':
+ if (yyres)
+ yyres[yyn] = '\0';
+ return yyn;
+ }
+ do_not_strip_quotes: ;
+ }
+
+ if (yyres)
+ return yystpcpy (yyres, yystr) - yyres;
+ else
+ return yystrlen (yystr);
+}
+#endif
+
+
+static int
+yy_syntax_error_arguments (const yypcontext_t *yyctx,
+ yysymbol_kind_t yyarg[], int yyargn)
+{
+ /* Actual size of YYARG. */
+ int yycount = 0;
+ /* There are many possibilities here to consider:
+ - If this state is a consistent state with a default action, then
+ the only way this function was invoked is if the default action
+ is an error action. In that case, don't check for expected
+ tokens because there are none.
+ - The only way there can be no lookahead present (in yychar) is if
+ this state is a consistent state with a default action. Thus,
+ detecting the absence of a lookahead is sufficient to determine
+ that there is no unexpected or expected token to report. In that
+ case, just report a simple "syntax error".
+ - Don't assume there isn't a lookahead just because this state is a
+ consistent state with a default action. There might have been a
+ previous inconsistent state, consistent state with a non-default
+ action, or user semantic action that manipulated yychar.
+ - Of course, the expected token list depends on states to have
+ correct lookahead information, and it depends on the parser not
+ to perform extra reductions after fetching a lookahead from the
+ scanner and before detecting a syntax error. Thus, state merging
+ (from LALR or IELR) and default reductions corrupt the expected
+ token list. However, the list is correct for canonical LR with
+ one exception: it will still contain any token that will not be
+ accepted due to an error action in a later state.
+ */
+ if (yyctx->yytoken != YYSYMBOL_YYEMPTY)
+ {
+ int yyn;
+ if (yyarg)
+ yyarg[yycount] = yyctx->yytoken;
+ ++yycount;
+ yyn = yypcontext_expected_tokens (yyctx,
+ yyarg ? yyarg + 1 : yyarg, yyargn - 1);
+ if (yyn == YYENOMEM)
+ return YYENOMEM;
+ else
+ yycount += yyn;
+ }
+ return yycount;
+}
+
+/* Copy into *YYMSG, which is of size *YYMSG_ALLOC, an error message
+ about the unexpected token YYTOKEN for the state stack whose top is
+ YYSSP.
+
+ Return 0 if *YYMSG was successfully written. Return -1 if *YYMSG is
+ not large enough to hold the message. In that case, also set
+ *YYMSG_ALLOC to the required number of bytes. Return YYENOMEM if the
+ required number of bytes is too large to store. */
+static int
+yysyntax_error (YYPTRDIFF_T *yymsg_alloc, char **yymsg,
+ const yypcontext_t *yyctx)
+{
+ enum { YYARGS_MAX = 5 };
+ /* Internationalized format string. */
+ const char *yyformat = YY_NULLPTR;
+ /* Arguments of yyformat: reported tokens (one for the "unexpected",
+ one per "expected"). */
+ yysymbol_kind_t yyarg[YYARGS_MAX];
+ /* Cumulated lengths of YYARG. */
+ YYPTRDIFF_T yysize = 0;
+
+ /* Actual size of YYARG. */
+ int yycount = yy_syntax_error_arguments (yyctx, yyarg, YYARGS_MAX);
+ if (yycount == YYENOMEM)
+ return YYENOMEM;
+
+ switch (yycount)
+ {
+#define YYCASE_(N, S) \
+ case N: \
+ yyformat = S; \
+ break
+ default: /* Avoid compiler warnings. */
+ YYCASE_(0, YY_("syntax error"));
+ YYCASE_(1, YY_("syntax error, unexpected %s"));
+ YYCASE_(2, YY_("syntax error, unexpected %s, expecting %s"));
+ YYCASE_(3, YY_("syntax error, unexpected %s, expecting %s or %s"));
+ YYCASE_(4, YY_("syntax error, unexpected %s, expecting %s or %s or %s"));
+ YYCASE_(5, YY_("syntax error, unexpected %s, expecting %s or %s or %s or %s"));
+#undef YYCASE_
+ }
+
+ /* Compute error message size. Don't count the "%s"s, but reserve
+ room for the terminator. */
+ yysize = yystrlen (yyformat) - 2 * yycount + 1;
+ {
+ int yyi;
+ for (yyi = 0; yyi < yycount; ++yyi)
+ {
+ YYPTRDIFF_T yysize1
+ = yysize + yytnamerr (YY_NULLPTR, yytname[yyarg[yyi]]);
+ if (yysize <= yysize1 && yysize1 <= YYSTACK_ALLOC_MAXIMUM)
+ yysize = yysize1;
+ else
+ return YYENOMEM;
+ }
+ }
+
+ if (*yymsg_alloc < yysize)
+ {
+ *yymsg_alloc = 2 * yysize;
+ if (! (yysize <= *yymsg_alloc
+ && *yymsg_alloc <= YYSTACK_ALLOC_MAXIMUM))
+ *yymsg_alloc = YYSTACK_ALLOC_MAXIMUM;
+ return -1;
+ }
+
+ /* Avoid sprintf, as that infringes on the user's name space.
+ Don't have undefined behavior even if the translation
+ produced a string with the wrong number of "%s"s. */
+ {
+ char *yyp = *yymsg;
+ int yyi = 0;
+ while ((*yyp = *yyformat) != '\0')
+ if (*yyp == '%' && yyformat[1] == 's' && yyi < yycount)
+ {
+ yyp += yytnamerr (yyp, yytname[yyarg[yyi++]]);
+ yyformat += 2;
+ }
+ else
+ {
+ ++yyp;
+ ++yyformat;
+ }
+ }
+ return 0;
+}
+
+
+/*-----------------------------------------------.
+| Release the memory associated to this symbol. |
+`-----------------------------------------------*/
+
+static void
+yydestruct (const char *yymsg,
+ yysymbol_kind_t yykind, YYSTYPE *yyvaluep)
+{
+ YY_USE (yyvaluep);
+ if (!yymsg)
+ yymsg = "Deleting";
+ YY_SYMBOL_PRINT (yymsg, yykind, yyvaluep, yylocationp);
+
+ YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
+ YY_USE (yykind);
+ YY_IGNORE_MAYBE_UNINITIALIZED_END
+}
+
+
+/* Lookahead token kind. */
+int yychar;
+
+/* The semantic value of the lookahead symbol. */
+YYSTYPE yylval;
+/* Number of syntax errors so far. */
+int yynerrs;
+
+
+
+
+/*----------.
+| yyparse. |
+`----------*/
+
+int
+yyparse (void)
+{
+ yy_state_fast_t yystate = 0;
+ /* Number of tokens to shift before error messages enabled. */
+ int yyerrstatus = 0;
+
+ /* Refer to the stacks through separate pointers, to allow yyoverflow
+ to reallocate them elsewhere. */
+
+ /* Their size. */
+ YYPTRDIFF_T yystacksize = YYINITDEPTH;
+
+ /* The state stack: array, bottom, top. */
+ yy_state_t yyssa[YYINITDEPTH];
+ yy_state_t *yyss = yyssa;
+ yy_state_t *yyssp = yyss;
+
+ /* The semantic value stack: array, bottom, top. */
+ YYSTYPE yyvsa[YYINITDEPTH];
+ YYSTYPE *yyvs = yyvsa;
+ YYSTYPE *yyvsp = yyvs;
+
+ int yyn;
+ /* The return value of yyparse. */
+ int yyresult;
+ /* Lookahead symbol kind. */
+ yysymbol_kind_t yytoken = YYSYMBOL_YYEMPTY;
+ /* The variables used to return semantic value and location from the
+ action routines. */
+ YYSTYPE yyval;
+
+ /* Buffer for error messages, and its allocated size. */
+ char yymsgbuf[128];
+ char *yymsg = yymsgbuf;
+ YYPTRDIFF_T yymsg_alloc = sizeof yymsgbuf;
+
+#define YYPOPSTACK(N) (yyvsp -= (N), yyssp -= (N))
+
+ /* The number of symbols on the RHS of the reduced rule.
+ Keep to zero when no symbol should be popped. */
+ int yylen = 0;
+
+ YYDPRINTF ((stderr, "Starting parse\n"));
+
+ yychar = YYEMPTY; /* Cause a token to be read. */
+
+ goto yysetstate;
+
+
+/*------------------------------------------------------------.
+| yynewstate -- push a new state, which is found in yystate. |
+`------------------------------------------------------------*/
+yynewstate:
+ /* In all cases, when you get here, the value and location stacks
+ have just been pushed. So pushing a state here evens the stacks. */
+ yyssp++;
+
+
+/*--------------------------------------------------------------------.
+| yysetstate -- set current state (the top of the stack) to yystate. |
+`--------------------------------------------------------------------*/
+yysetstate:
+ YYDPRINTF ((stderr, "Entering state %d\n", yystate));
+ YY_ASSERT (0 <= yystate && yystate < YYNSTATES);
+ YY_IGNORE_USELESS_CAST_BEGIN
+ *yyssp = YY_CAST (yy_state_t, yystate);
+ YY_IGNORE_USELESS_CAST_END
+ YY_STACK_PRINT (yyss, yyssp);
+
+ if (yyss + yystacksize - 1 <= yyssp)
+#if !defined yyoverflow && !defined YYSTACK_RELOCATE
+ YYNOMEM;
+#else
+ {
+ /* Get the current used size of the three stacks, in elements. */
+ YYPTRDIFF_T yysize = yyssp - yyss + 1;
+
+# if defined yyoverflow
+ {
+ /* Give user a chance to reallocate the stack. Use copies of
+ these so that the &'s don't force the real ones into
+ memory. */
+ yy_state_t *yyss1 = yyss;
+ YYSTYPE *yyvs1 = yyvs;
+
+ /* Each stack pointer address is followed by the size of the
+ data in use in that stack, in bytes. This used to be a
+ conditional around just the two extra args, but that might
+ be undefined if yyoverflow is a macro. */
+ yyoverflow (YY_("memory exhausted"),
+ &yyss1, yysize * YYSIZEOF (*yyssp),
+ &yyvs1, yysize * YYSIZEOF (*yyvsp),
+ &yystacksize);
+ yyss = yyss1;
+ yyvs = yyvs1;
+ }
+# else /* defined YYSTACK_RELOCATE */
+ /* Extend the stack our own way. */
+ if (YYMAXDEPTH <= yystacksize)
+ YYNOMEM;
+ yystacksize *= 2;
+ if (YYMAXDEPTH < yystacksize)
+ yystacksize = YYMAXDEPTH;
+
+ {
+ yy_state_t *yyss1 = yyss;
+ union yyalloc *yyptr =
+ YY_CAST (union yyalloc *,
+ YYSTACK_ALLOC (YY_CAST (YYSIZE_T, YYSTACK_BYTES (yystacksize))));
+ if (! yyptr)
+ YYNOMEM;
+ YYSTACK_RELOCATE (yyss_alloc, yyss);
+ YYSTACK_RELOCATE (yyvs_alloc, yyvs);
+# undef YYSTACK_RELOCATE
+ if (yyss1 != yyssa)
+ YYSTACK_FREE (yyss1);
+ }
+# endif
+
+ yyssp = yyss + yysize - 1;
+ yyvsp = yyvs + yysize - 1;
+
+ YY_IGNORE_USELESS_CAST_BEGIN
+ YYDPRINTF ((stderr, "Stack size increased to %ld\n",
+ YY_CAST (long, yystacksize)));
+ YY_IGNORE_USELESS_CAST_END
+
+ if (yyss + yystacksize - 1 <= yyssp)
+ YYABORT;
+ }
+#endif /* !defined yyoverflow && !defined YYSTACK_RELOCATE */
+
+
+ if (yystate == YYFINAL)
+ YYACCEPT;
+
+ goto yybackup;
+
+
+/*-----------.
+| yybackup. |
+`-----------*/
+yybackup:
+ /* Do appropriate processing given the current state. Read a
+ lookahead token if we need one and don't already have one. */
+
+ /* First try to decide what to do without reference to lookahead token. */
+ yyn = yypact[yystate];
+ if (yypact_value_is_default (yyn))
+ goto yydefault;
+
+ /* Not known => get a lookahead token if don't already have one. */
+
+ /* YYCHAR is either empty, or end-of-input, or a valid lookahead. */
+ if (yychar == YYEMPTY)
+ {
+ YYDPRINTF ((stderr, "Reading a token\n"));
+ yychar = yylex ();
+ }
+
+ if (yychar <= YYEOF)
+ {
+ yychar = YYEOF;
+ yytoken = YYSYMBOL_YYEOF;
+ YYDPRINTF ((stderr, "Now at end of input.\n"));
+ }
+ else if (yychar == YYerror)
+ {
+ /* The scanner already issued an error message, process directly
+ to error recovery. But do not keep the error token as
+ lookahead, it is too special and may lead us to an endless
+ loop in error recovery. */
+ yychar = YYUNDEF;
+ yytoken = YYSYMBOL_YYerror;
+ goto yyerrlab1;
+ }
+ else
+ {
+ yytoken = YYTRANSLATE (yychar);
+ YY_SYMBOL_PRINT ("Next token is", yytoken, &yylval, &yylloc);
+ }
+
+ /* If the proper action on seeing token YYTOKEN is to reduce or to
+ detect an error, take that action. */
+ yyn += yytoken;
+ if (yyn < 0 || YYLAST < yyn || yycheck[yyn] != yytoken)
+ goto yydefault;
+ yyn = yytable[yyn];
+ if (yyn <= 0)
+ {
+ if (yytable_value_is_error (yyn))
+ goto yyerrlab;
+ yyn = -yyn;
+ goto yyreduce;
+ }
+
+ /* Count tokens shifted since error; after three, turn off error
+ status. */
+ if (yyerrstatus)
+ yyerrstatus--;
+
+ /* Shift the lookahead token. */
+ YY_SYMBOL_PRINT ("Shifting", yytoken, &yylval, &yylloc);
+ yystate = yyn;
+ YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
+ *++yyvsp = yylval;
+ YY_IGNORE_MAYBE_UNINITIALIZED_END
+
+ /* Discard the shifted token. */
+ yychar = YYEMPTY;
+ goto yynewstate;
+
+
+/*-----------------------------------------------------------.
+| yydefault -- do the default action for the current state. |
+`-----------------------------------------------------------*/
+yydefault:
+ yyn = yydefact[yystate];
+ if (yyn == 0)
+ goto yyerrlab;
+ goto yyreduce;
+
+
+/*-----------------------------.
+| yyreduce -- do a reduction. |
+`-----------------------------*/
+yyreduce:
+ /* yyn is the number of a rule to reduce with. */
+ yylen = yyr2[yyn];
+
+ /* If YYLEN is nonzero, implement the default value of the action:
+ '$$ = $1'.
+
+ Otherwise, the following line sets YYVAL to garbage.
+ This behavior is undocumented and Bison
+ users should not rely upon it. Assigning to YYVAL
+ unconditionally makes the parser a bit smaller, and it avoids a
+ GCC warning that YYVAL may be used uninitialized. */
+ yyval = yyvsp[1-yylen];
+
+
+ YY_REDUCE_PRINT (yyn);
+ switch (yyn)
+ {
+ case 2: /* rc: %empty */
+#line 38 "sys/cmd/rc/syntax.y"
+ { return 0; }
+#line 1549 "sys/cmd/rc/parse.c"
+ break;
+
+ case 3: /* rc: line '\n' */
+#line 39 "sys/cmd/rc/syntax.y"
+ { return compile((yyvsp[-1].tree)); }
+#line 1555 "sys/cmd/rc/parse.c"
+ break;
+
+ case 5: /* line: cmds line */
+#line 43 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = maketree2(';', (yyvsp[-1].tree), (yyvsp[0].tree)); }
+#line 1561 "sys/cmd/rc/parse.c"
+ break;
+
+ case 7: /* body: cmdsln body */
+#line 47 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = maketree2(';', (yyvsp[-1].tree), (yyvsp[0].tree)); }
+#line 1567 "sys/cmd/rc/parse.c"
+ break;
+
+ case 8: /* paren: '(' body ')' */
+#line 50 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = maketree1(Tparen, (yyvsp[-1].tree)); }
+#line 1573 "sys/cmd/rc/parse.c"
+ break;
+
+ case 9: /* block: '{' body '}' */
+#line 53 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = maketree1(Tblock, (yyvsp[-1].tree)); }
+#line 1579 "sys/cmd/rc/parse.c"
+ break;
+
+ case 11: /* cmds: cmd '&' */
+#line 57 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = maketree1('&', (yyvsp[-1].tree)); }
+#line 1585 "sys/cmd/rc/parse.c"
+ break;
+
+ case 14: /* ifbody: cmd */
+#line 64 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = maketree2(Tif, nil, (yyvsp[0].tree)); }
+#line 1591 "sys/cmd/rc/parse.c"
+ break;
+
+ case 15: /* ifbody: block Telse nl cmd */
+#line 65 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = maketree3(Tif, nil, (yyvsp[-3].tree), (yyvsp[-2].tree)); }
+#line 1597 "sys/cmd/rc/parse.c"
+ break;
+
+ case 16: /* case: Tcase words ';' */
+#line 68 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = hangchild1((yyvsp[-2].tree), (yyvsp[-1].tree), 0); }
+#line 1603 "sys/cmd/rc/parse.c"
+ break;
+
+ case 17: /* case: Tcase words '\n' */
+#line 69 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = hangchild1((yyvsp[-2].tree), (yyvsp[-1].tree), 0); }
+#line 1609 "sys/cmd/rc/parse.c"
+ break;
+
+ case 18: /* casebody: cmd */
+#line 72 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = maketree2(Tcasebody, (yyvsp[0].tree), nil); }
+#line 1615 "sys/cmd/rc/parse.c"
+ break;
+
+ case 19: /* casebody: case casebody */
+#line 73 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = maketree2(Tcasebody, (yyvsp[-1].tree), (yyvsp[0].tree)); }
+#line 1621 "sys/cmd/rc/parse.c"
+ break;
+
+ case 20: /* casebody: cmdsln casebody */
+#line 74 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = maketree2(Tcasebody, (yyvsp[-1].tree), (yyvsp[0].tree)); }
+#line 1627 "sys/cmd/rc/parse.c"
+ break;
+
+ case 21: /* assign: executable '=' word */
+#line 77 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = maketree2('=', (yyvsp[-2].tree), (yyvsp[0].tree)); }
+#line 1633 "sys/cmd/rc/parse.c"
+ break;
+
+ case 23: /* redir: Tredir word */
+#line 81 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = hangchild1((yyvsp[-1].tree), (yyvsp[0].tree), 0); }
+#line 1639 "sys/cmd/rc/parse.c"
+ break;
+
+ case 24: /* epilog: %empty */
+#line 84 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = nil; }
+#line 1645 "sys/cmd/rc/parse.c"
+ break;
+
+ case 25: /* epilog: redir epilog */
+#line 85 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = hangchild1((yyvsp[-1].tree), (yyvsp[0].tree), 1); }
+#line 1651 "sys/cmd/rc/parse.c"
+ break;
+
+ case 26: /* cmd: %empty */
+#line 88 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = nil; }
+#line 1657 "sys/cmd/rc/parse.c"
+ break;
+
+ case 27: /* cmd: basic */
+#line 89 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = maketree1(Tbasic, (yyvsp[0].tree)); }
+#line 1663 "sys/cmd/rc/parse.c"
+ break;
+
+ case 28: /* cmd: block epilog */
+#line 90 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = hangepilog((yyvsp[-1].tree), (yyvsp[0].tree)); }
+#line 1669 "sys/cmd/rc/parse.c"
+ break;
+
+ case 29: /* cmd: cmd Tpipe nl cmd */
+#line 91 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = hangchild2((yyvsp[-2].tree), (yyvsp[-3].tree), 0, (yyvsp[0].tree), 1); }
+#line 1675 "sys/cmd/rc/parse.c"
+ break;
+
+ case 30: /* cmd: cmd Tandand nl cmd */
+#line 92 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = maketree2(Tandand, (yyvsp[-3].tree), (yyvsp[0].tree)); }
+#line 1681 "sys/cmd/rc/parse.c"
+ break;
+
+ case 31: /* cmd: cmd Toror nl cmd */
+#line 93 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = maketree2(Toror, (yyvsp[-3].tree), (yyvsp[0].tree)); }
+#line 1687 "sys/cmd/rc/parse.c"
+ break;
+
+ case 32: /* cmd: redir cmd */
+#line 94 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = hangchild1((yyvsp[-1].tree), (yyvsp[0].tree), 1); }
+#line 1693 "sys/cmd/rc/parse.c"
+ break;
+
+ case 33: /* cmd: assign cmd */
+#line 95 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = hangchild1((yyvsp[-1].tree), (yyvsp[0].tree), 2); }
+#line 1699 "sys/cmd/rc/parse.c"
+ break;
+
+ case 34: /* cmd: Tbang cmd */
+#line 96 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = maketree1(Tbang, (yyvsp[0].tree)); }
+#line 1705 "sys/cmd/rc/parse.c"
+ break;
+
+ case 35: /* cmd: Tsubshell cmd */
+#line 97 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = maketree1(Tsubshell, (yyvsp[0].tree)); }
+#line 1711 "sys/cmd/rc/parse.c"
+ break;
+
+ case 36: /* cmd: Tfor '(' word ')' nl cmd */
+#line 98 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = hangchild3((yyvsp[-5].tree), (yyvsp[-3].tree), nil, (yyvsp[0].tree)); }
+#line 1717 "sys/cmd/rc/parse.c"
+ break;
+
+ case 37: /* cmd: Tfor '(' word Tin words ')' nl cmd */
+#line 99 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = hangchild3((yyvsp[-7].tree), (yyvsp[-5].tree), (yyvsp[-3].tree), (yyvsp[0].tree)); }
+#line 1723 "sys/cmd/rc/parse.c"
+ break;
+
+ case 38: /* cmd: Twhile paren nl cmd */
+#line 100 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = hangchild2((yyvsp[-3].tree), (yyvsp[-2].tree), 0, (yyvsp[0].tree), 1); }
+#line 1729 "sys/cmd/rc/parse.c"
+ break;
+
+ case 39: /* cmd: Tif paren nl ifbody */
+#line 101 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = hangchild1((yyvsp[-2].tree), (yyvsp[-3].tree), 0); }
+#line 1735 "sys/cmd/rc/parse.c"
+ break;
+
+ case 40: /* cmd: Tswitch '(' word ')' nl '{' casebody '}' */
+#line 102 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = hangchild2((yyvsp[-7].tree), (yyvsp[-5].tree), 0, (yyvsp[-1].tree), 1); }
+#line 1741 "sys/cmd/rc/parse.c"
+ break;
+
+ case 42: /* basic: basic word */
+#line 106 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = maketree2(Targs, (yyvsp[-1].tree), (yyvsp[0].tree)); }
+#line 1747 "sys/cmd/rc/parse.c"
+ break;
+
+ case 43: /* basic: basic redir */
+#line 107 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = maketree2(Targs, (yyvsp[-1].tree), (yyvsp[0].tree)); }
+#line 1753 "sys/cmd/rc/parse.c"
+ break;
+
+ case 45: /* atom: keyword */
+#line 111 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = maketree1(Tword, (yyvsp[0].tree)); }
+#line 1759 "sys/cmd/rc/parse.c"
+ break;
+
+ case 47: /* word: word '^' atom */
+#line 115 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = maketree2('^', (yyvsp[-2].tree), (yyvsp[0].tree)); }
+#line 1765 "sys/cmd/rc/parse.c"
+ break;
+
+ case 49: /* executable: executable '^' atom */
+#line 119 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = maketree2('^', (yyvsp[-2].tree), (yyvsp[0].tree)); }
+#line 1771 "sys/cmd/rc/parse.c"
+ break;
+
+ case 51: /* nonkeyword: '$' atom */
+#line 123 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = maketree1('$', (yyvsp[0].tree)); }
+#line 1777 "sys/cmd/rc/parse.c"
+ break;
+
+ case 52: /* nonkeyword: '$' atom Tindex words ')' */
+#line 124 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = maketree2(Tindex, (yyvsp[-3].tree), (yyvsp[-1].tree)); }
+#line 1783 "sys/cmd/rc/parse.c"
+ break;
+
+ case 53: /* nonkeyword: '(' wordsnl ')' */
+#line 125 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = (yyvsp[-1].tree); }
+#line 1789 "sys/cmd/rc/parse.c"
+ break;
+
+ case 54: /* nonkeyword: Tcount atom */
+#line 126 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = maketree1(Tcount, (yyvsp[0].tree)); }
+#line 1795 "sys/cmd/rc/parse.c"
+ break;
+
+ case 55: /* nonkeyword: Tjoin atom */
+#line 127 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = maketree1(Tjoin, (yyvsp[0].tree)); }
+#line 1801 "sys/cmd/rc/parse.c"
+ break;
+
+ case 56: /* nonkeyword: '`' block */
+#line 128 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = maketree1('`', (yyvsp[0].tree)); }
+#line 1807 "sys/cmd/rc/parse.c"
+ break;
+
+ case 67: /* words: %empty */
+#line 135 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = nil; }
+#line 1813 "sys/cmd/rc/parse.c"
+ break;
+
+ case 68: /* words: words word */
+#line 136 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = maketree2(Twords, (yyvsp[-1].tree), (yyvsp[0].tree)); }
+#line 1819 "sys/cmd/rc/parse.c"
+ break;
+
+ case 69: /* wordsnl: %empty */
+#line 139 "sys/cmd/rc/syntax.y"
+ { (yyval.tree) = nil; }
+#line 1825 "sys/cmd/rc/parse.c"
+ break;
+
+ case 71: /* wordsnl: wordsnl word */
+#line 141 "sys/cmd/rc/syntax.y"
+ {(yyval.tree) = (!(yyvsp[-1].tree)) ? ((!(yyvsp[0].tree)) ? nil : (yyvsp[0].tree)) : ((!(yyvsp[0].tree)) ? (yyvsp[-1].tree) : maketree2(Twords, (yyvsp[-1].tree), (yyvsp[0].tree))); }
+#line 1831 "sys/cmd/rc/parse.c"
+ break;
+
+
+#line 1835 "sys/cmd/rc/parse.c"
+
+ default: break;
+ }
+ /* User semantic actions sometimes alter yychar, and that requires
+ that yytoken be updated with the new translation. We take the
+ approach of translating immediately before every use of yytoken.
+ One alternative is translating here after every semantic action,
+ but that translation would be missed if the semantic action invokes
+ YYABORT, YYACCEPT, or YYERROR immediately after altering yychar or
+ if it invokes YYBACKUP. In the case of YYABORT or YYACCEPT, an
+ incorrect destructor might then be invoked immediately. In the
+ case of YYERROR or YYBACKUP, subsequent parser actions might lead
+ to an incorrect destructor call or verbose syntax error message
+ before the lookahead is translated. */
+ YY_SYMBOL_PRINT ("-> $$ =", YY_CAST (yysymbol_kind_t, yyr1[yyn]), &yyval, &yyloc);
+
+ YYPOPSTACK (yylen);
+ yylen = 0;
+
+ *++yyvsp = yyval;
+
+ /* Now 'shift' the result of the reduction. Determine what state
+ that goes to, based on the state we popped back to and the rule
+ number reduced by. */
+ {
+ const int yylhs = yyr1[yyn] - YYNTOKENS;
+ const int yyi = yypgoto[yylhs] + *yyssp;
+ yystate = (0 <= yyi && yyi <= YYLAST && yycheck[yyi] == *yyssp
+ ? yytable[yyi]
+ : yydefgoto[yylhs]);
+ }
+
+ goto yynewstate;
+
+
+/*--------------------------------------.
+| yyerrlab -- here on detecting error. |
+`--------------------------------------*/
+yyerrlab:
+ /* Make sure we have latest lookahead translation. See comments at
+ user semantic actions for why this is necessary. */
+ yytoken = yychar == YYEMPTY ? YYSYMBOL_YYEMPTY : YYTRANSLATE (yychar);
+ /* If not already recovering from an error, report this error. */
+ if (!yyerrstatus)
+ {
+ ++yynerrs;
+ {
+ yypcontext_t yyctx
+ = {yyssp, yytoken};
+ char const *yymsgp = YY_("syntax error");
+ int yysyntax_error_status;
+ yysyntax_error_status = yysyntax_error (&yymsg_alloc, &yymsg, &yyctx);
+ if (yysyntax_error_status == 0)
+ yymsgp = yymsg;
+ else if (yysyntax_error_status == -1)
+ {
+ if (yymsg != yymsgbuf)
+ YYSTACK_FREE (yymsg);
+ yymsg = YY_CAST (char *,
+ YYSTACK_ALLOC (YY_CAST (YYSIZE_T, yymsg_alloc)));
+ if (yymsg)
+ {
+ yysyntax_error_status
+ = yysyntax_error (&yymsg_alloc, &yymsg, &yyctx);
+ yymsgp = yymsg;
+ }
+ else
+ {
+ yymsg = yymsgbuf;
+ yymsg_alloc = sizeof yymsgbuf;
+ yysyntax_error_status = YYENOMEM;
+ }
+ }
+ yyerror (yymsgp);
+ if (yysyntax_error_status == YYENOMEM)
+ YYNOMEM;
+ }
+ }
+
+ if (yyerrstatus == 3)
+ {
+ /* If just tried and failed to reuse lookahead token after an
+ error, discard it. */
+
+ if (yychar <= YYEOF)
+ {
+ /* Return failure if at end of input. */
+ if (yychar == YYEOF)
+ YYABORT;
+ }
+ else
+ {
+ yydestruct ("Error: discarding",
+ yytoken, &yylval);
+ yychar = YYEMPTY;
+ }
+ }
+
+ /* Else will try to reuse lookahead token after shifting the error
+ token. */
+ goto yyerrlab1;
+
+
+/*---------------------------------------------------.
+| yyerrorlab -- error raised explicitly by YYERROR. |
+`---------------------------------------------------*/
+yyerrorlab:
+ /* Pacify compilers when the user code never invokes YYERROR and the
+ label yyerrorlab therefore never appears in user code. */
+ if (0)
+ YYERROR;
+ ++yynerrs;
+
+ /* Do not reclaim the symbols of the rule whose action triggered
+ this YYERROR. */
+ YYPOPSTACK (yylen);
+ yylen = 0;
+ YY_STACK_PRINT (yyss, yyssp);
+ yystate = *yyssp;
+ goto yyerrlab1;
+
+
+/*-------------------------------------------------------------.
+| yyerrlab1 -- common code for both syntax error and YYERROR. |
+`-------------------------------------------------------------*/
+yyerrlab1:
+ yyerrstatus = 3; /* Each real token shifted decrements this. */
+
+ /* Pop stack until we find a state that shifts the error token. */
+ for (;;)
+ {
+ yyn = yypact[yystate];
+ if (!yypact_value_is_default (yyn))
+ {
+ yyn += YYSYMBOL_YYerror;
+ if (0 <= yyn && yyn <= YYLAST && yycheck[yyn] == YYSYMBOL_YYerror)
+ {
+ yyn = yytable[yyn];
+ if (0 < yyn)
+ break;
+ }
+ }
+
+ /* Pop the current state because it cannot handle the error token. */
+ if (yyssp == yyss)
+ YYABORT;
+
+
+ yydestruct ("Error: popping",
+ YY_ACCESSING_SYMBOL (yystate), yyvsp);
+ YYPOPSTACK (1);
+ yystate = *yyssp;
+ YY_STACK_PRINT (yyss, yyssp);
+ }
+
+ YY_IGNORE_MAYBE_UNINITIALIZED_BEGIN
+ *++yyvsp = yylval;
+ YY_IGNORE_MAYBE_UNINITIALIZED_END
+
+
+ /* Shift the error token. */
+ YY_SYMBOL_PRINT ("Shifting", YY_ACCESSING_SYMBOL (yyn), yyvsp, yylsp);
+
+ yystate = yyn;
+ goto yynewstate;
+
+
+/*-------------------------------------.
+| yyacceptlab -- YYACCEPT comes here. |
+`-------------------------------------*/
+yyacceptlab:
+ yyresult = 0;
+ goto yyreturnlab;
+
+
+/*-----------------------------------.
+| yyabortlab -- YYABORT comes here. |
+`-----------------------------------*/
+yyabortlab:
+ yyresult = 1;
+ goto yyreturnlab;
+
+
+/*-----------------------------------------------------------.
+| yyexhaustedlab -- YYNOMEM (memory exhaustion) comes here. |
+`-----------------------------------------------------------*/
+yyexhaustedlab:
+ yyerror (YY_("memory exhausted"));
+ yyresult = 2;
+ goto yyreturnlab;
+
+
+/*----------------------------------------------------------.
+| yyreturnlab -- parsing is finished, clean up and return. |
+`----------------------------------------------------------*/
+yyreturnlab:
+ if (yychar != YYEMPTY)
+ {
+ /* Make sure we have latest lookahead translation. See comments at
+ user semantic actions for why this is necessary. */
+ yytoken = YYTRANSLATE (yychar);
+ yydestruct ("Cleanup: discarding lookahead",
+ yytoken, &yylval);
+ }
+ /* Do not reclaim the symbols of the rule whose action triggered
+ this YYABORT or YYACCEPT. */
+ YYPOPSTACK (yylen);
+ YY_STACK_PRINT (yyss, yyssp);
+ while (yyssp != yyss)
+ {
+ yydestruct ("Cleanup: popping",
+ YY_ACCESSING_SYMBOL (+*yyssp), yyvsp);
+ YYPOPSTACK (1);
+ }
+#ifndef yyoverflow
+ if (yyss != yyssa)
+ YYSTACK_FREE (yyss);
+#endif
+ if (yymsg != yymsgbuf)
+ YYSTACK_FREE (yymsg);
+ return yyresult;
+}
+
+#line 147 "sys/cmd/rc/syntax.y"
+
diff --git a/src/cmd/rc/parse.h b/src/cmd/rc/parse.h
new file mode 100644
index 0000000..64ee07b
--- /dev/null
+++ b/src/cmd/rc/parse.h
@@ -0,0 +1,141 @@
+/* A Bison parser, made by GNU Bison 3.8.2. */
+
+/* Bison interface for Yacc-like parsers in C
+
+ Copyright (C) 1984, 1989-1990, 2000-2015, 2018-2021 Free Software Foundation,
+ Inc.
+
+ This program is free software: you can redistribute it and/or modify
+ it under the terms of the GNU General Public License as published by
+ the Free Software Foundation, either version 3 of the License, or
+ (at your option) any later version.
+
+ This program is distributed in the hope that it will be useful,
+ but WITHOUT ANY WARRANTY; without even the implied warranty of
+ MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ GNU General Public License for more details.
+
+ You should have received a copy of the GNU General Public License
+ along with this program. If not, see <https://www.gnu.org/licenses/>. */
+
+/* As a special exception, you may create a larger work that contains
+ part or all of the Bison parser skeleton and distribute that work
+ under terms of your choice, so long as that work isn't itself a
+ parser generator using the skeleton or a modified version thereof
+ as a parser skeleton. Alternatively, if you modify or redistribute
+ the parser skeleton itself, you may (at your option) remove this
+ special exception, which will cause the skeleton and the resulting
+ Bison output files to be licensed under the GNU General Public
+ License without this special exception.
+
+ This special exception was added by the Free Software Foundation in
+ version 2.2 of Bison. */
+
+/* DO NOT RELY ON FEATURES THAT ARE NOT DOCUMENTED in the manual,
+ especially those whose name start with YY_ or yy_. They are
+ private implementation details that can be changed or removed. */
+
+#ifndef YY_YY_SYS_CMD_RC_PARSE_H_INCLUDED
+# define YY_YY_SYS_CMD_RC_PARSE_H_INCLUDED
+/* Debug traces. */
+#ifndef YYDEBUG
+# define YYDEBUG 0
+#endif
+#if YYDEBUG
+extern int yydebug;
+#endif
+
+/* Token kinds. */
+#ifndef YYTOKENTYPE
+# define YYTOKENTYPE
+ enum yytokentype
+ {
+ YYEMPTY = -2,
+ YYEOF = 0, /* "end of file" */
+ YYerror = 256, /* error */
+ YYUNDEF = 257, /* "invalid token" */
+ Tfor = 258, /* Tfor */
+ Tin = 259, /* Tin */
+ Twhile = 260, /* Twhile */
+ Tif = 261, /* Tif */
+ Telse = 262, /* Telse */
+ Tswitch = 263, /* Tswitch */
+ Tcase = 264, /* Tcase */
+ Tcasebody = 265, /* Tcasebody */
+ Ttwiddle = 266, /* Ttwiddle */
+ Tbang = 267, /* Tbang */
+ Tsubshell = 268, /* Tsubshell */
+ Tfunc = 269, /* Tfunc */
+ Tredir = 270, /* Tredir */
+ Tdup = 271, /* Tdup */
+ Tpipe = 272, /* Tpipe */
+ Tindex = 273, /* Tindex */
+ Tbasic = 274, /* Tbasic */
+ Targs = 275, /* Targs */
+ Tword = 276, /* Tword */
+ Twords = 277, /* Twords */
+ Tparen = 278, /* Tparen */
+ Tblock = 279, /* Tblock */
+ Tandand = 280, /* Tandand */
+ Toror = 281, /* Toror */
+ Tcount = 282, /* Tcount */
+ Tjoin = 283 /* Tjoin */
+ };
+ typedef enum yytokentype yytoken_kind_t;
+#endif
+/* Token kinds. */
+#define YYEMPTY -2
+#define YYEOF 0
+#define YYerror 256
+#define YYUNDEF 257
+#define Tfor 258
+#define Tin 259
+#define Twhile 260
+#define Tif 261
+#define Telse 262
+#define Tswitch 263
+#define Tcase 264
+#define Tcasebody 265
+#define Ttwiddle 266
+#define Tbang 267
+#define Tsubshell 268
+#define Tfunc 269
+#define Tredir 270
+#define Tdup 271
+#define Tpipe 272
+#define Tindex 273
+#define Tbasic 274
+#define Targs 275
+#define Tword 276
+#define Twords 277
+#define Tparen 278
+#define Tblock 279
+#define Tandand 280
+#define Toror 281
+#define Tcount 282
+#define Tjoin 283
+
+/* Value type. */
+#if ! defined YYSTYPE && ! defined YYSTYPE_IS_DECLARED
+union YYSTYPE
+{
+#line 24 "sys/cmd/rc/syntax.y"
+
+ struct Tree *tree;
+
+#line 127 "sys/cmd/rc/parse.h"
+
+};
+typedef union YYSTYPE YYSTYPE;
+# define YYSTYPE_IS_TRIVIAL 1
+# define YYSTYPE_IS_DECLARED 1
+#endif
+
+
+extern YYSTYPE yylval;
+
+
+int yyparse (void);
+
+
+#endif /* !YY_YY_SYS_CMD_RC_PARSE_H_INCLUDED */
diff --git a/src/cmd/rc/prompt.c b/src/cmd/rc/prompt.c
new file mode 100644
index 0000000..1122d54
--- /dev/null
+++ b/src/cmd/rc/prompt.c
@@ -0,0 +1,36 @@
+#include "rc.h"
+
+/* static char promptbuf[7] = {'>', ' ', 0, ' ' , ' ', ' ', 0}; */
+static char *base= "\x1b[1;31m" ">" "\x1b[0;0m" " ", *promptstr;
+
+void
+resetprompt(void)
+{
+ promptstr = base;
+}
+
+int
+prompt(ushort *flag)
+{
+ int f = *flag;
+
+ if(f){
+ if(!readline(promptstr)){
+ runner->flag.eof = 1;
+ return 0;
+ }
+ if(runner->cmd.io->e[-1] == '\n'){
+ runner->cmd.io->e[-1] = 0;
+ addhistory(runner->cmd.io->b);
+ runner->cmd.io->e[-1] = '\n';
+ }
+
+ write(mapfd(0), "\n\r", 2);
+ promptstr = " ";
+
+ runner->line++;
+ *flag = 0;
+ }
+
+ return 1;
+}
diff --git a/src/cmd/rc/rc.h b/src/cmd/rc/rc.h
new file mode 100644
index 0000000..9b415fc
--- /dev/null
+++ b/src/cmd/rc/rc.h
@@ -0,0 +1,263 @@
+
+#include <u.h>
+#include <base.h>
+#include <libutf.h>
+
+// -----------------------------------------------------------------------
+// types
+
+typedef struct Io Io;
+typedef struct Var Var;
+typedef struct Word Word;
+typedef struct List List;
+typedef struct Tree Tree;
+typedef struct Redir Redir;
+typedef union Code Code;
+typedef struct Thread Thread;
+typedef struct Shell Shell;
+
+struct Io
+{
+ int fd, cap;
+ char *s;
+ char *b, *e, buf[];
+};
+
+enum
+{
+ Rappend,
+ Rwrite,
+ Rread,
+ Rhere,
+ Rdupfd,
+ Ropen,
+ Rclose,
+ Rrdwr
+};
+
+struct Tree
+{
+ int type;
+ union{
+ struct {
+ ushort quoted;
+ char *str; // Tword
+ };
+ struct {
+ ushort type; // Tpipe, Tredir, Tdup
+ int fd[2];
+ } redir;
+ };
+ Tree *child[3];
+ Tree *link;
+};
+
+struct Word
+{
+ char *str;
+ Word *link;
+};
+
+struct List
+{
+ Word *word;
+ List *link;
+};
+
+/*
+ * the first word of any code vector is a reference count
+ * always create a new reference to a code vector by calling copycode()
+ * always call freecode() when deleting a reference
+ */
+union Code
+{
+ int i;
+ void (*f)(void);
+ char *s;
+};
+
+struct Var
+{
+ char *name;
+ Word *val;
+ short new : 1;
+ short newfunc : 1;
+ Code *func;
+ void (*update)(Var *);
+ Var *link;
+};
+
+struct Redir
+{
+ char type; /* what to do */
+ short from, to; /* what to do it to */
+ struct Redir *link; /* what else to do (reverse order) */
+};
+
+enum
+{
+ Pnil,
+ Prun,
+ Pstop,
+ Psig,
+ Pagain,
+ Pdone,
+};
+
+struct WaitItem
+{
+ int pid;
+ ushort status;
+};
+
+struct Thread
+{
+ struct {
+ int i;
+ Code *exe;
+ } code; // execution stack
+ struct {
+ Io *io;
+ char *path;
+ } cmd; // command input
+
+ List *args; // argument stack
+ Var *local; // local variables
+ struct {
+ Redir *start;
+ Redir *end;
+ } redir; // list of redirections
+
+ struct {
+ ushort user : 1;
+ ushort eof : 1;
+ } flag;
+
+ struct {
+ ushort status;
+ int len, cap;
+ struct WaitItem *on;
+ } wait;
+
+ int pid, pgid, status;
+ long line;
+
+ Thread *caller; // process we return to
+ Thread *link; // next job
+};
+
+struct Shell
+{
+ int pid;
+ Io *err;
+ int status;
+ int interactive;
+ Thread *jobs;
+};
+
+// -----------------------------------------------------------------------
+// globals
+
+extern Shell shell;
+extern Thread *runner;
+extern Code *compiled;
+
+// -----------------------------------------------------------------------
+// functions
+
+/* util.c */
+void itoa(char*, long i);
+void fatal(char *, ...);
+void *emalloc(uintptr);
+void *erealloc(void*, uintptr);
+void efree(void*);
+
+/* input.c */
+int readline(char *);
+void enablevi(void);
+
+void inithistory(void);
+int addhistory(char *);
+
+/* prompt.c */
+void resetprompt(void);
+int prompt(ushort *);
+
+/* io.c */
+Io *openfd(int fd);
+Io *openstr(void);
+void terminate(Io *io);
+
+int get(Io *);
+int put(Io **, char);
+
+void flush(Io *io);
+void print(Io *, char *, ...);
+
+/* lex.c */
+int iswordchar(int c);
+int yylex(void);
+
+/* tree.c */
+Tree *maketree(void);
+Tree *maketree1(int, Tree*);
+Tree *maketree2(int, Tree*, Tree*);
+Tree *maketree3(int, Tree*, Tree*, Tree*);
+
+Tree *token(int, char *);
+Tree *hangchild1(Tree *, Tree *, int);
+Tree *hangchild2(Tree *, Tree *, int, Tree *, int);
+Tree *hangchild3(Tree *, Tree *, Tree *, Tree *);
+Tree *hangepilog(Tree *, Tree*);
+
+void freeparsetree(void);
+
+/* sys.c */
+void initenv(void);
+void redirect(struct Redir *);
+void execute(Word *, Word*);
+int mapfd(int fd);
+
+/* wait.c */
+void addwait(Thread *, int);
+void delwait(Thread *, int);
+void clearwait(Thread*);
+
+int waitall(Thread *);
+int waitfor(Thread *, int);
+
+void killzombies(void);
+
+/* job.c */
+Thread *getjob(int, int*);
+void addjob(Thread *);
+void deljob(Thread *);
+void wakeup(Thread *);
+void report(Thread *, int);
+
+void foreground(Thread *, int);
+void background(Thread *, int);
+
+/* exec.c */
+// XXX: odd place for this
+int count(Word *);
+Word *makeword(char *str, Word *link);
+void freeword(Word *w);
+
+/* var.c */
+Var *var(char*);
+Var *definevar(char*, Var *);
+Var *globalvar(char*);
+Var *makevar(char *name, Var *link);
+void setvar(char *, Word *);
+int iskeyword(char *);
+
+void initpath(void);
+void initkeywords(void);
+
+char **mkenv(void);
+
+/* code.c */
+int compile(Tree *);
+Code *copycode(Code *c);
+void freecode(Code *c);
diff --git a/src/cmd/rc/rules.mk b/src/cmd/rc/rules.mk
new file mode 100644
index 0000000..76837fc
--- /dev/null
+++ b/src/cmd/rc/rules.mk
@@ -0,0 +1,32 @@
+include share/push.mk
+# Iterate through subdirectory tree
+
+# local sources
+SRCS_$(d):=\
+ $(d)/util.c\
+ $(d)/input.c\
+ $(d)/prompt.c\
+ $(d)/io.c\
+ $(d)/lex.c\
+ $(d)/parse.c\
+ $(d)/tree.c\
+ $(d)/code.c\
+ $(d)/var.c\
+ $(d)/sys.c\
+ $(d)/wait.c\
+ $(d)/job.c\
+ $(d)/exec.c\
+ $(d)/main.c
+
+# local targets
+BINS_$(d) := $(d)/rc
+
+include share/paths.mk
+$(d)/parse.h $(d)/parse.c: $(d)/syntax.y
+ yacc --header=$(<D)/parse.h --output=$(<D)/parse.c $(<)
+
+# local rules
+$(BINS_$(d)): $(OBJS_$(d)) $(OBJ_DIR)/libutf/libutf.a $(OBJ_DIR)/base/base.a $(d)/parse.h
+ $(COMPLINK)
+
+include share/pop.mk
diff --git a/src/cmd/rc/syntax.y b/src/cmd/rc/syntax.y
new file mode 100644
index 0000000..0bdc776
--- /dev/null
+++ b/src/cmd/rc/syntax.y
@@ -0,0 +1,147 @@
+%token Tfor Tin Twhile Tif Telse Tswitch Tcase Tcasebody Ttwiddle Tbang Tsubshell Tfunc
+%token Tredir Tdup Tpipe Tindex
+%token Tbasic Targs Tword Twords Tparen Tblock
+
+%define parse.error verbose
+
+%{
+ #include "rc.h"
+
+ int yylex(void);
+ void yyerror(const char *);
+%}
+
+/* operator precendence: lowest first */
+%left Tif Tfor Tswitch Tcase ')' Twhile Telse
+%left Tandand Toror '\n'
+%left Tbang Tsubshell
+%left Tpipe;
+%left '^';
+%right '$' Tcount Tjoin
+%left Tindex
+
+/* semantic types */
+%union{
+ struct Tree *tree;
+}
+%type<tree> line cmds cmdsln body paren block ifbody casebody case assign epilog redir;
+%type<tree> cmd basic executable nonkeyword keyword word words wordsnl atom;
+%type<tree> Tfor Tin Twhile Tif Telse Tswitch Tcase Ttwiddle Tbang Tsubshell Tfunc;
+%type<tree> Tword Tredir Tpipe Tdup;
+
+/* grammar */
+
+%start rc
+
+%%
+rc:
+/*empty*/ { return 0; }
+| line '\n' { return compile($1); }
+
+line:
+ cmd
+| cmds line { $$ = maketree2(';', $1, $2); }
+
+body:
+ cmd
+| cmdsln body { $$ = maketree2(';', $1, $2); }
+
+paren:
+ '(' body ')' { $$ = maketree1(Tparen, $2); }
+
+block:
+ '{' body '}' { $$ = maketree1(Tblock, $2); }
+
+cmds:
+ cmd ';'
+| cmd '&' { $$ = maketree1('&', $1); }
+
+cmdsln:
+ cmds
+| cmd '\n'
+
+ifbody:
+ cmd %prec Telse { $$ = maketree2(Tif, nil, $1); }
+| block Telse nl cmd { $$ = maketree3(Tif, nil, $1, $2); }
+
+case:
+ Tcase words ';' { $$ = hangchild1($1, $2, 0); }
+| Tcase words '\n' { $$ = hangchild1($1, $2, 0); }
+
+casebody:
+ cmd { $$ = maketree2(Tcasebody, $1, nil); }
+| case casebody { $$ = maketree2(Tcasebody, $1, $2); }
+| cmdsln casebody { $$ = maketree2(Tcasebody, $1, $2); }
+
+assign:
+ executable '=' word { $$ = maketree2('=', $1, $3); }
+
+redir:
+ Tdup
+| Tredir word { $$ = hangchild1($1, $2, 0); }
+
+epilog:
+/* empty */ { $$ = nil; }
+| redir epilog { $$ = hangchild1($1, $2, 1); }
+
+cmd:
+/* empty */ %prec Twhile { $$ = nil; }
+| basic { $$ = maketree1(Tbasic, $1); }
+| block epilog { $$ = hangepilog($1, $2); }
+| cmd Tpipe nl cmd { $$ = hangchild2($2, $1, 0, $4, 1); }
+| cmd Tandand nl cmd { $$ = maketree2(Tandand, $1, $4); }
+| cmd Toror nl cmd { $$ = maketree2(Toror, $1, $4); }
+| redir cmd %prec Tbang { $$ = hangchild1($1, $2, 1); }
+| assign cmd %prec Tbang { $$ = hangchild1($1, $2, 2); }
+| Tbang cmd { $$ = maketree1(Tbang, $2); }
+| Tsubshell cmd { $$ = maketree1(Tsubshell, $2); }
+| Tfor '(' word ')' nl cmd { $$ = hangchild3($1, $3, nil, $6); }
+| Tfor '(' word Tin words ')' nl cmd { $$ = hangchild3($1, $3, $5, $8); }
+| Twhile paren nl cmd { $$ = hangchild2($1, $2, 0, $4, 1); }
+| Tif paren nl ifbody { $$ = hangchild1($2, $1, 0); }
+| Tswitch '(' word ')' nl '{' casebody '}' { $$ = hangchild2($1, $3, 0, $7, 1); }
+
+basic:
+ executable
+| basic word { $$ = maketree2(Targs, $1, $2); }
+| basic redir { $$ = maketree2(Targs, $1, $2); }
+
+atom:
+ nonkeyword
+| keyword { $$ = maketree1(Tword, $1); }
+
+word:
+ atom
+| word '^' atom { $$ = maketree2('^', $1, $3); }
+
+executable:
+ nonkeyword
+| executable '^' atom { $$ = maketree2('^', $1, $3); }
+
+nonkeyword:
+ Tword
+| '$' atom { $$ = maketree1('$', $2); }
+| '$' atom Tindex words ')' { $$ = maketree2(Tindex, $2, $4); }
+| '(' wordsnl ')' { $$ = $2; }
+| Tcount atom { $$ = maketree1(Tcount, $2); }
+| Tjoin atom { $$ = maketree1(Tjoin, $2); }
+| '`' block { $$ = maketree1('`', $2); }
+//| Tredir block { $$ = hangchild1($1, $2, 0); $$->type = Tpipefd; }
+
+keyword:
+ Tfor|Tin|Twhile|Tif|Telse|Tswitch|Tcase|Tbang|Tsubshell|Tfunc
+
+words:
+/* empty */ { $$ = nil; }
+| words word { $$ = maketree2(Twords, $1, $2); }
+
+wordsnl:
+/* empty */ { $$ = nil; }
+| wordsnl '\n' /* empty */
+| wordsnl word {$$ = (!$1) ? ((!$2) ? nil : $2) : ((!$2) ? $1 : maketree2(Twords, $1, $2)); }
+
+nl:
+/*empty*/
+| nl '\n'
+
+%%
diff --git a/src/cmd/rc/sys.c b/src/cmd/rc/sys.c
new file mode 100644
index 0000000..807359d
--- /dev/null
+++ b/src/cmd/rc/sys.c
@@ -0,0 +1,137 @@
+#include "rc.h"
+
+// -----------------------------------------------------------------------
+// internal
+
+static
+char**
+mkargv(Word *args)
+{
+ char **argv=emalloc((count(args)+2)*sizeof(char *));
+ char **argp=argv+1; /* leave one at front for runcoms */
+
+ for(;args;args=args->link)
+ *argp++=args->str;
+ *argp=nil;
+
+ return argv;
+}
+
+static
+Word*
+envval(char *s)
+{
+ Word *v;
+ char *t, c;
+
+ for(t=s; *t && *t!='\1'; t++)
+ ;
+
+ c = *t;
+ *t = '\0';
+
+ v = makeword(s, (c=='\0') ? nil : envval(t+1));
+ *t=c;
+
+ return v;
+}
+
+// -----------------------------------------------------------------------
+// exported
+
+void
+initenv(void)
+{
+ extern char **environ;
+
+ char *s;
+ char **env;
+
+ for(env=environ; *env; env++) {
+ for(s=*env; *s && *s != '(' && *s != '='; s++)
+ ;
+ switch(*s){
+ case '\0':
+ break;
+ case '(': /* ignore functions */
+ break;
+ case '=':
+ *s = '\0';
+ setvar(*env, envval(s+1));
+ *s = '=';
+ break;
+ }
+ }
+}
+
+void
+execute(Word *cmd, Word *path)
+{
+ int nc;
+ char **argv = mkargv(cmd);
+ char **env = mkenv();
+ char file[1024];
+
+ for(; path; path=path->link){
+ nc = strlen(path->str);
+ if(nc < arrlen(file)){
+ strcpy(file, path->str);
+ if(file[0]){
+ strcat(file, "/");
+ nc++;
+ }
+ if(nc+strlen(argv[1]) < arrlen(file)){
+ strcat(file, argv[1]);
+ execve(file, argv+1, env);
+ }else
+ fatal("command name too long");
+ }
+ }
+ print(shell.err, "could not execute command: %s\n", argv[1]);
+ efree(argv);
+}
+
+void
+redirect(Redir *r)
+{
+ if(r){
+ redirect(r->link);
+ switch(r->type){
+ case Ropen:
+ if(r->from != r->to){
+ dup2(r->from, r->to);
+ close(r->from);
+ }
+ break;
+ case Rdupfd:
+ dup2(r->from, r->to); // TODO: error checking
+ break;
+ case Rclose:
+ close(r->from);
+ break;
+ default:
+ fatal("unrecognized redirection type %d\n", r->type);
+ }
+ }
+}
+
+int
+mapfd(int fd)
+{
+ Redir *r;
+ for(r = runner->redir.end; r; r = r->link){
+ switch(r->type){
+ case Rclose:
+ if(r->from == fd)
+ fd = -1;
+ break;
+ case Rdupfd:
+ case Ropen:
+ if(r->to == fd)
+ fd = r->from;
+ break;
+ }
+ }
+
+ return fd;
+}
diff --git a/src/cmd/rc/tree.c b/src/cmd/rc/tree.c
new file mode 100644
index 0000000..2c65041
--- /dev/null
+++ b/src/cmd/rc/tree.c
@@ -0,0 +1,111 @@
+#include "rc.h"
+#include "parse.h"
+
+static Tree *nodes;
+
+Tree*
+maketree(void)
+{
+ Tree *node = emalloc(sizeof(*node));
+
+ node->link = nodes;
+ nodes = node;
+ return node;
+}
+
+void
+freeparsetree(void)
+{
+ Tree *t, *nt;
+ for(t = nodes; t; t = nt) {
+ nt = t->link;
+ if(t->type == Tword && t->str)
+ efree(t->str);
+ efree(t);
+ }
+ nodes = nil;
+}
+
+Tree*
+maketree1(int type, Tree *c0)
+{
+ return maketree2(type, c0, nil);
+}
+
+Tree*
+maketree2(int type, Tree *c0, Tree *c1)
+{
+ return maketree3(type, c0, c1, nil);
+}
+
+Tree*
+maketree3(int type, Tree *c0, Tree *c1, Tree *c2)
+{
+ Tree *node = maketree();
+
+ node->type = type;
+ node->child[0] = c0;
+ node->child[1] = c1;
+ node->child[2] = c2;
+
+ return node;
+}
+
+Tree*
+hangchild1(Tree *node, Tree *c, int i)
+{
+ node->child[i] = c;
+
+ return node;
+}
+
+Tree*
+hangchild2(Tree *node, Tree *c1, int i1, Tree *c2, int i2)
+{
+ node->child[i1] = c1;
+ node->child[i2] = c2;
+
+ return node;
+}
+
+Tree*
+hangchild3(Tree *node, Tree *c0, Tree *c1, Tree *c2)
+{
+ node->child[0] = c0;
+ node->child[1] = c1;
+ node->child[2] = c2;
+
+ return node;
+}
+
+Tree*
+hangepilog(Tree *cmd, Tree *epi)
+{
+ Tree *p;
+ if(!epi)
+ return cmd;
+ for(p = epi; p->child[1]; p = p->child[1])
+ ;
+
+ p->child[1] = cmd;
+ return epi;
+}
+
+Tree*
+token(int type, char *s)
+{
+ Tree *node = maketree();
+
+ node->type = type;
+ node->str = strdup(s);
+
+ return node;
+}
+
+/*
+Tree*
+basic(Tree *node)
+{
+ return maketree1(Tbasic, node);
+}
+*/
diff --git a/src/cmd/rc/util.c b/src/cmd/rc/util.c
new file mode 100644
index 0000000..b0be788
--- /dev/null
+++ b/src/cmd/rc/util.c
@@ -0,0 +1,65 @@
+#include "rc.h"
+
+void
+fatal(char *msg, ...)
+{
+ va_list args;
+ vfprintf(stderr, msg, args);
+ va_end(args);
+
+ abort();
+}
+
+void*
+emalloc(uintptr n)
+{
+ void *p;
+ if(!(p = malloc(n)))
+ fatal("out of memory: can't allocate %d bytes", n);
+
+ memset(p, 0, n);
+ return p;
+}
+
+void*
+erealloc(void *p, uintptr n)
+{
+ void *r;
+ if(!(r = realloc(p,n)))
+ fatal("out of memory: can't reallocate %d bytes", n);
+
+ return r;
+}
+
+void
+efree(void *p)
+{
+ if(p)
+ free(p);
+ // TODO: log the double free
+}
+
+
+char *bp;
+
+static
+void
+iacvt(int n)
+{
+ if(n<0){
+ *bp++='-';
+ n=-n; /* doesn't work for n==-inf */
+ }
+ if(n/10)
+ iacvt(n/10);
+
+ *bp++=n%10+'0';
+}
+
+void
+itoa(char *s, long n)
+{
+ bp = s;
+ iacvt(n);
+ *bp='\0';
+}
diff --git a/src/cmd/rc/var.c b/src/cmd/rc/var.c
new file mode 100644
index 0000000..3e9635f
--- /dev/null
+++ b/src/cmd/rc/var.c
@@ -0,0 +1,336 @@
+#include "rc.h"
+#include "parse.h"
+
+// TODO: string interning
+
+// -----------------------------------------------------------------------
+// globals
+
+struct Keyword
+{
+ char *name;
+ int type;
+};
+
+static Var *globals[512];
+static struct Keyword keywords[100]; // sparse map means less hits
+
+// -----------------------------------------------------------------------
+// internals
+
+static
+int
+hash(char *s, int len)
+{
+ int h =0, i = 1;
+ while(*s)
+ h += *s++*i++;
+
+ h %= len;
+ return h < 0 ? h+len : h;
+}
+
+static
+void
+·setvar(char *name, Word *val, int call)
+{
+ Var *v = var(name);
+ freeword(v->val);
+
+ v->val = val;
+ v->new = 1; // this never turns off?
+
+ if(call && v->update)
+ v->update(v);
+}
+
+static
+char*
+list2strcolon(Word *words)
+{
+ char *value, *s, *t;
+ int len = 0;
+ Word *ap;
+ for(ap = words;ap;ap = ap->link)
+ len+=1+strlen(ap->str);
+
+ value = emalloc(len+1);
+
+ s = value;
+ for(ap = words; ap; ap = ap->link){
+ for(t = ap->str;*t;) *s++=*t++;
+ *s++=':';
+ }
+
+ if(s==value)
+ *s='\0';
+ else
+ s[-1]='\0';
+
+ return value;
+}
+
+static
+void
+littlepath(Var *v)
+{
+ /* convert $path to $PATH */
+ char *p;
+ Word *w;
+
+ p = list2strcolon(v->val);
+ w = emalloc(sizeof(*w));
+ w->str = p;
+ w->link = nil;
+
+ ·setvar("PATH", w, 1);
+}
+
+static
+void
+bigpath(Var *v)
+{
+ /* convert $PATH to $path */
+ char *p, *q;
+ Word **l, *w;
+
+ if(v->val == nil){
+ ·setvar("path", nil, 0);
+ return;
+ }
+
+ p = v->val->str;
+ w = nil;
+ l = &w;
+
+ /* Doesn't handle escaped colon nonsense. */
+ if(p[0] == 0)
+ p = nil;
+
+ while(p){
+ q = strchr(p, ':');
+ if(q)
+ *q = 0;
+
+ *l = makeword(p[0] ? p : ".", nil);
+ l = &(*l)->link;
+
+ if(q){
+ *q = ':';
+ p = q+1;
+ }else
+ p = nil;
+ }
+ ·setvar("path", w, 0);
+}
+
+// -----------------------------------------------------------------------
+// exports
+
+Var*
+makevar(char *name, Var *link)
+{
+ Var *v = emalloc(sizeof(*v));
+
+ v->name = name;
+ v->val = 0;
+ v->new = 0;
+ v->newfunc = 0;
+ v->link = link;
+ v->func = nil;
+ v->update = nil;
+
+ return v;
+}
+
+void
+setvar(char *name, Word *val)
+{
+ ·setvar(name, val, 1);
+}
+
+Var*
+definevar(char *name, Var *link)
+{
+ Var *v = emalloc(sizeof(*v));
+
+ v->name = name;
+ v->val = 0;
+ v->link = link;
+
+ return v;
+}
+
+Var*
+globalvar(char *name)
+{
+ int h;
+ Var *v;
+
+ h = hash(name, arrlen(globals));
+
+ if(strcmp(name,"PATH")==0){
+ flush(shell.err);
+ }
+
+ for(v = globals[h]; v; v = v->link){
+ if(strcmp(v->name, name) == 0){
+ return v;
+ }
+ }
+
+ return globals[h] = definevar(strdup(name), globals[h]);
+}
+
+Var*
+var(char *name)
+{
+ Var *v;
+ if(runner){
+ for(v = runner->local; v; v=v->link)
+ if(strcmp(v->name, name) == 0)
+ return v;
+ }
+ return globalvar(name);
+}
+
+static
+int
+cmpenv(const void *a, const void *b)
+{
+ return strcmp(*(char**)a, *(char**)b);
+}
+
+char**
+mkenv(void)
+{
+ char **env, **ep, *p, *q;
+ Var **h, *v;
+ Word *a;
+ int nvar=0, nchr=0, sep;
+
+#define BODY \
+ if((v==var(v->name)) && v->val){ \
+ nvar++; \
+ nchr+=strlen(v->name)+1; \
+ for(a=v->val;a;a=a->link) \
+ nchr+=strlen(a->str)+1; \
+ }
+
+ for(v= runner->local; v; v=v->link){
+ BODY
+ }
+ for(h=globals; h!=arrend(globals); h++){
+ for(v = *h; v; v=v->link){
+ BODY
+ }
+ }
+
+#undef BODY
+
+ env=emalloc((nvar+1)*sizeof(*env)+nchr);
+ ep=env;
+ p=(char *)&env[nvar+1];
+
+#define BODY \
+ if((v==var(v->name)) && v->val){ \
+ *ep++=p; \
+ q=v->name; \
+ while(*q) \
+ *p++=*q++; \
+ sep='='; \
+ for(a=v->val;a;a=a->link){ \
+ *p++=sep; \
+ sep='\1'; \
+ q=a->str; \
+ while(*q) \
+ *p++=*q++; \
+ } \
+ *p++='\0'; \
+ }
+
+ for(v=runner->local; v; v=v->link){
+ BODY
+ }
+ for(h=globals; h!=arrend(globals); h++){
+ for(v = *h; v; v=v->link){
+ BODY
+ }
+ }
+#undef BODY
+
+ *ep=0;
+
+ qsort((char *)env, nvar, sizeof ep[0], cmpenv);
+ return env;
+}
+
+void
+initpath(void)
+{
+ Var *v;
+
+ v = globalvar("path");
+ v->update = littlepath;
+
+ v = globalvar("PATH");
+ v->update = bigpath;
+
+ flush(shell.err);
+ bigpath(v);
+}
+
+#define KEYWORDS \
+ KEYWORD("for", Tfor) \
+ KEYWORD("in", Tin) \
+ KEYWORD("while", Twhile) \
+ KEYWORD("if", Tif) \
+ KEYWORD("else", Telse) \
+ KEYWORD("switch", Tswitch) \
+ KEYWORD("case", Tcase) \
+ KEYWORD("!", Tbang) \
+ KEYWORD("@", Tsubshell) \
+ KEYWORD("func", Tfunc)
+
+void
+initkeywords(void)
+{
+ int i, s, j, h;
+#define KEYWORD(a, b) a,
+ static char *name[] = { KEYWORDS };
+#undef KEYWORD
+#define KEYWORD(a, b) b,
+ static int type[] = { KEYWORDS };
+#undef KEYWORD
+
+ for(i = 0; i < arrlen(type); i++){
+ h = hash(name[i], arrlen(keywords));
+ for(s=0; s < arrlen(keywords); s++){
+ j = (h + s) % arrlen(keywords);
+ if(!keywords[j].type || strcmp(keywords[j].name, name[i]) == 0){
+ keywords[j].name = name[i];
+ keywords[j].type = type[i];
+ goto nextkeyword;
+ }
+ }
+ nextkeyword:;
+ }
+}
+
+int
+iskeyword(char *word)
+{
+ int i, s, h;
+
+ h = hash(word, arrlen(keywords));
+ for(s = 0; s < arrlen(keywords); s++){
+ i = (h + s) % arrlen(keywords);
+ if(!keywords[i].type)
+ return 0;
+ if(strcmp(keywords[i].name, word) == 0)
+ return keywords[i].type;
+ }
+ return 0;
+}
+
+#undef KEYWORDS
diff --git a/src/cmd/rc/wait.c b/src/cmd/rc/wait.c
new file mode 100644
index 0000000..911601c
--- /dev/null
+++ b/src/cmd/rc/wait.c
@@ -0,0 +1,247 @@
+#include "rc.h"
+
+#include <sys/wait.h>
+
+// -----------------------------------------------------------------------
+// globals
+
+struct WaitMsg
+{
+ int pid;
+ int type;
+ ulong time[3];
+ int status;
+};
+
+// -----------------------------------------------------------------------
+// internal
+
+static
+int
+await(int pid4, int opt, struct WaitMsg *msg)
+{
+ int pid, status, core;
+ struct rusage ru;
+ ulong u, s;
+
+ /* event loop */
+ for(;;){
+ if((pid = wait4(pid4, &status, opt, &ru)) <= 0){
+ if(errno == ECHILD){
+ msg->pid = -1;
+ return 1;
+ }
+ msg->pid = 0;
+ perror("failed wait4");
+ return 0;
+ }
+
+ u = ru.ru_utime.tv_sec*1000+((ru.ru_utime.tv_usec+500)/1000);
+ s = ru.ru_stime.tv_sec*1000+((ru.ru_stime.tv_usec+500)/1000);
+
+ if(WIFEXITED(status)){
+ msg->pid = pid;
+ msg->time[0] = u;
+ msg->time[1] = s;
+ msg->time[2] = u+s;
+ msg->status = WEXITSTATUS(status);
+ msg->type = Pdone;
+
+ return 1;
+ }
+
+ if(WIFSIGNALED(status)){
+ msg->pid = pid;
+ msg->time[0] = u;
+ msg->time[1] = s;
+ msg->time[2] = u+s;
+ msg->status = WTERMSIG(status);
+ msg->type = Psig;
+
+ return 1;
+ }
+
+ if(WIFSTOPPED(status)){
+ msg->pid = pid;
+ msg->time[0] = u;
+ msg->time[1] = s;
+ msg->time[2] = u+s;
+ msg->status = WSTOPSIG(status);
+ msg->type = Pstop;
+
+ return 1;
+ }
+ }
+}
+
+static
+int
+shouldwait(Thread *job)
+{
+ int i;
+
+ for(i=0; i<job->wait.len; i++){
+ if(job->wait.on[i].status == Prun)
+ return 1;
+ }
+
+ return 0;
+}
+
+static inline
+void
+notify(Thread *job, struct WaitMsg msg)
+{
+ int i;
+ for(i=0; i < job->wait.len; i++){
+ if(job->wait.on[i].pid == msg.pid){
+ job->status = msg.status;
+ switch(msg.type){
+ case Pstop:
+ print(shell.err, "%d: suspended\n", msg.pid);
+ job->wait.status = Pstop;
+ job->wait.on[i].status = Pstop;
+ break;
+
+ case Psig:
+ print(shell.err, "%d: terminated by signal %d\n", msg.pid, msg.status);
+ /* fallthrough */
+ case Pdone:
+ job->wait.on[i].status = Pdone;
+ delwait(job, msg.pid);
+ if(!job->wait.len)
+ job->wait.status = Pdone;
+ break;
+
+ default:
+ fatal("%d: unrecognized message type %d\n", msg.pid, msg.type);
+ }
+ break;
+ }
+ }
+}
+
+// -----------------------------------------------------------------------
+// exported
+
+void
+clearwait(Thread *job)
+{
+ job->wait.len = 0;
+}
+
+int
+havewait(Thread *job, int pid)
+{
+ int i;
+
+ for(i=0; i<job->wait.len; i++)
+ if(job->wait.on[i].pid == pid)
+ return 1;
+ return 0;
+}
+
+void
+addwait(Thread *job, int pid)
+{
+ if(job->wait.len == job->wait.cap){
+ job->wait.cap = job->wait.cap + 2;
+ job->wait.on = erealloc(job->wait.on, job->wait.cap*sizeof(*job->wait.on));
+ }
+
+ job->wait.on[job->wait.len++] = (struct WaitItem){.pid=pid, .status=Prun};
+}
+
+void
+delwait(Thread *job, int pid)
+{
+ int r, w;
+
+ for(r=w=0; r < job->wait.len; r++){
+ if(job->wait.on[r].pid != pid)
+ job->wait.on[w++].pid = job->wait.on[r].pid;
+ }
+ job->wait.len = w;
+}
+
+int
+waitall(Thread *job)
+{
+ int i;
+ Thread *t;
+ struct WaitMsg msg;
+
+ while(shouldwait(job) && await(-job->pgid, WUNTRACED, &msg)){
+ switch(msg.pid){
+ case 0: // error
+ perror("wait job");
+ return 0;
+ case -1: // no children: assume they have exited
+ job->wait.status = Pdone;
+ clearwait(job);
+ return 1;
+ default:
+ ;
+ }
+
+ notify(job, msg);
+ }
+ return 1;
+}
+
+int
+waitfor(Thread *job, int pid)
+{
+ int i;
+ Thread *t;
+ struct WaitMsg msg;
+
+ while(shouldwait(job) && await(-job->pgid, WUNTRACED, &msg)){
+ switch(msg.pid){
+ case 0: // error
+ perror("wait for");
+ return 0;
+ case -1: // no children: assume they have exited
+ job->wait.status = Pdone;
+ clearwait(job);
+ return 1;
+ default:
+ ;
+ }
+
+ notify(job, msg);
+ /* allow for an early exit */
+ if(msg.pid == pid)
+ return 1;
+ }
+ return 1;
+
+}
+
+void
+killzombies(void)
+{
+ Thread *job;
+ int index, status, pid;
+
+ while((pid=waitpid(-1, &status, WNOHANG))>0){
+ print(shell.err, "found zombie pid %d\n", pid);
+ flush(shell.err);
+
+ job = getjob(pid, &index);
+ if(!job)
+ perror("invalid pid");
+
+ if(WIFEXITED(status))
+ job->wait.status = Pdone;
+ if(WIFSTOPPED(status))
+ job->wait.status = Pstop;
+ if(WIFCONTINUED(status))
+ job->wait.status = Pagain;
+
+ if(job->wait.status == Pdone){
+ report(job,index);
+ deljob(job);
+ }
+ }
+}