From 91c673b37782d4cd90e5cf9a8e4491723e6c04bf Mon Sep 17 00:00:00 2001 From: Nicholas Noll Date: Fri, 19 Jun 2020 14:44:54 -0700 Subject: fix: many small bug fixes with parser and lexer --- sys/cmd/rc/io.c | 80 +++++++++++++++++++++++++++++++-------------------------- 1 file changed, 44 insertions(+), 36 deletions(-) (limited to 'sys/cmd/rc/io.c') diff --git a/sys/cmd/rc/io.c b/sys/cmd/rc/io.c index 8cc2f5b..2fbe57f 100644 --- a/sys/cmd/rc/io.c +++ b/sys/cmd/rc/io.c @@ -1,10 +1,10 @@ #include "rc.h" -#undef bufsize #define c0 t->child[0] #define c1 t->child[1] #define c2 t->child[2] +#undef bufsize #define bufsize 512 #define strsize 100 @@ -169,6 +169,11 @@ pptr(Io *f, void *v) int n; uintptr p; + if (!v) { + pstr(f, ""); + return; + } + p = (uintptr)v; if ((sizeof(uintptr) == sizeof(uvlong)) && p >>32) for (n = 60; n >= 32; n-=4) @@ -252,49 +257,50 @@ pcmd(Io *f, Tree *t) return; switch(t->type){ - default: pfmt(f, "bad %d %p %p %p", t->type, c0, c1, c2); + default: pfmt(f, "bad<%d> %p %p %p", t->type, c0, c1, c2); break; - case '$': pfmt(f, "$%t", c0); + case Tdol: pfmt(f, "$%t", c0); break; - case '"': pfmt(f, "$\"%t", c0); + case Tquote: pfmt(f, "$\"%t", c0); break; - case '&': pfmt(f, "%t&", c0); + case Tand: pfmt(f, "%t&", c0); break; - case '^': pfmt(f, "%t^%t", c0, c1); + case Tcarot: pfmt(f, "%t^%t", c0, c1); break; - case '`': pfmt(f, "`%t", c0); + case Ttick: pfmt(f, "`%t", c0); break; - case Aandand: pfmt(f, "%t && %t", c0, c1); + case Tandand: pfmt(f, "%t && %t", c0, c1); break; - case Kbang: pfmt(f, "! %t", c0); + case Tbang: pfmt(f, "! %t", c0); break; - case Abrace: pfmt(f, "{%t}", c0); + case Tbrace: pfmt(f, "{%t}", c0); break; - case Acount: pfmt(f, "$#%t", c0); + case Tcount: pfmt(f, "$#%t", c0); break; - case Kfunc: pfmt(f, "fn %t %t", c0, c1); + case Tfunc: pfmt(f, "func %t %t", c0, c1); break; - case Kif: pfmt(f, "if%t%t", c0, c1); + case Tif: (c2) ? pfmt(f, "if%t%t else %t", c0, c1, c2): pfmt(f, "if%t%t", c0, c1); break; - case Kelse: pfmt(f, "else %t", c0); + case Toror: pfmt(f, "%t || %t", c0, c1); break; - case Aoror: pfmt(f, "%t || %t", c0, c1); + case Tpcmd: /* fallthrough */ + case Tparen: pfmt(f, "(%t)", c0); break; - case Aparen: pfmt(f, "(%t)", c0); + case Tsub: pfmt(f, "$%t(%t)", c0, c1); break; - case Asub: pfmt(f, "$%t(%t)", c0, c1); + case Tsimple: pfmt(f, "%t", c0); break; - case Asimple: pfmt(f, "%t", c0); + case Tsubshell: pfmt(f, "@ %t", c0); break; - case Ksubsh: pfmt(f, "@ %t", c0); + case Tswitch: pfmt(f, "switch %t %t", c0, c1); break; - case Kswitch: pfmt(f, "switch %t %t", c0, c1); + case Tcase: pfmt(f, "case %t:\n%t", c0, c1); break; - case Ktwiddle: pfmt(f, "~ %t %t", c0, c1); + case Ttwiddle: pfmt(f, "~ %t %t", c0, c1); break; - case Kwhile: pfmt(f, "while %t%t", c0, c1); + case Twhile: pfmt(f, "while %t%t", c0, c1); break; - case Aargs: + case Targs: if(c0==0) pfmt(f, "%t", c1); else if(c1==0) @@ -302,40 +308,41 @@ pcmd(Io *f, Tree *t) else pfmt(f, "%t %t", c0, c1); break; - case ';': - if(c0){ + case Tsemi: + if(c0) { if(c1) pfmt(f, "%t%c%t", c0, '\n', c1); - else pfmt(f, "%t", c0); - } - else pfmt(f, "%t", c1); + else + pfmt(f, "%t", c0); + } else + pfmt(f, "%t", c1); break; - case Awords: + case Twords: if(c0) pfmt(f, "%t ", c0); pfmt(f, "%t", c1); break; - case Kfor: + case Tfor: pfmt(f, "for(%t", c0); if(c1) pfmt(f, " in %t", c1); pfmt(f, ")%t", c2); break; - case Aword: + case Tword: if(t->quoted) pfmt(f, "%Q", t->str); else pdeglob(f, t->str); break; - case Adup: + case Tdup: if(t->redir.type==Rdupfd) pfmt(f, ">[%d=%d]", t->redir.fd[1], t->redir.fd[0]); /* yes, fd1, then fd0; read lex.c */ else pfmt(f, ">[%d=]", t->redir.fd[0]); pfmt(f, "%t", c1); break; - case Apipefd: - case Aredir: + case Tpipefd: + case Tredir: switch(t->redir.type){ case Rhere: pchr(f, '<'); @@ -359,12 +366,12 @@ pcmd(Io *f, Tree *t) if(c1) pfmt(f, " %t", c1); break; - case '=': + case Teq: pfmt(f, "%t=%t", c0, c1); if(c2) pfmt(f, " %t", c2); break; - case Apipe: + case Tpipe: pfmt(f, "%t|", c0); if(t->redir.fd[1]==0){ if(t->redir.fd[0]!=1) @@ -419,6 +426,7 @@ vpfmt(Io *f, char *fmt, va_list args) pval(f, va_arg(args, Word*)); break; } + if (--pfmtlev==0) flush(&f); } -- cgit v1.2.1