From 4650064aa757c217fa72f8819a2cf67c689bcdef Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Mon, 30 Mar 2020 09:54:45 -0400 Subject: acme: scale window bodies on resize, not including tag space This avoids reopening collapsed windows after a large vertical resize. --- src/cmd/acme/cols.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/cmd/acme/cols.c b/src/cmd/acme/cols.c index 63c33ac1..63247a84 100644 --- a/src/cmd/acme/cols.c +++ b/src/cmd/acme/cols.c @@ -232,7 +232,7 @@ colmousebut(Column *c) void colresize(Column *c, Rectangle r) { - int i; + int i, old, new; Rectangle r1, r2; Window *w; @@ -245,6 +245,8 @@ colresize(Column *c, Rectangle r) r1.max.y += Border; draw(screen, r1, display->black, nil, ZP); r1.max.y = r.max.y; + new = Dy(r) - c->nw*(Border + font->height); + old = Dy(c->r) - c->nw*(Border + font->height); for(i=0; inw; i++){ w = c->w[i]; w->maxlines = 0; @@ -252,8 +254,8 @@ colresize(Column *c, Rectangle r) r1.max.y = r.max.y; else{ r1.max.y = r1.min.y; - if(Dy(c->r) != 0){ - r1.max.y += (Dy(w->r)+Border)*Dy(r)/Dy(c->r); + if(new > 0 && old > 0 && Dy(w->r) > Border+font->height){ + r1.max.y += (Dy(w->r)-Border-font->height)*new/old + Border + font->height; } } r1.max.y = max(r1.max.y, r1.min.y + Border+font->height); -- cgit v1.2.3 From 47d4646eebac34c0b94951cfcf1b81ed2ca513e1 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Mon, 4 May 2020 18:34:19 -0400 Subject: rc: add recursive descent parser MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit The old yacc-based parser is available with the -Y flag, which will probably be removed at some point. The new -D flag dumps a parse tree of the input, without executing it. This allows comparing the output of rc -D and rc -DY on different scripts to see that the two parsers behave the same. The rc paper ends by saying: It is remarkable that in the four most recent editions of the UNIX system programmer’s manual the Bourne shell grammar described in the manual page does not admit the command who|wc. This is surely an oversight, but it suggests something darker: nobody really knows what the Bourne shell’s grammar is. Even examination of the source code is little help. The parser is implemented by recursive descent, but the routines corresponding to the syntactic categories all have a flag argument that subtly changes their operation depending on the context. Rc’s parser is implemented using yacc, so I can say precisely what the grammar is. The new recursive descent parser here has no such flags. It is a straightforward translation of the yacc. The new parser will make it easier to handle free carats in more generality as well as potentially allow the use of unquoted = as a word character. Going through this exercise has highlighted a few dark corners here as well. For example, I was surprised to find that x >f | y >f x | y are different commands (the latter redirects y's output). It is similarly surprising that a=b x | y sets a during the execution of y. It is also a bit counter-intuitive x | y | z x | if(c) y | z are not both 3-phase pipelines. These are certainly not things we should change, but they are not entirely obvious from the man page description, undercutting the quoted claim a bit. On the other hand, who | wc is clearly accepted by the grammar in the manual page, and the new parser still handles that test case. --- src/cmd/rc/checkparse | 14 ++ src/cmd/rc/code.c | 10 + src/cmd/rc/exec.c | 4 +- src/cmd/rc/fns.h | 1 + src/cmd/rc/io.c | 5 +- src/cmd/rc/io.h | 1 + src/cmd/rc/lex.c | 7 +- src/cmd/rc/mkfile | 1 + src/cmd/rc/parse.c | 532 ++++++++++++++++++++++++++++++++++++++++++++++++++ src/cmd/rc/pcmd.c | 115 +++++++++++ src/cmd/rc/simple.c | 1 + src/cmd/rc/syn.y | 10 +- src/cmd/rc/test.rc | 38 ++++ 13 files changed, 730 insertions(+), 9 deletions(-) create mode 100755 src/cmd/rc/checkparse create mode 100644 src/cmd/rc/parse.c create mode 100644 src/cmd/rc/test.rc (limited to 'src') diff --git a/src/cmd/rc/checkparse b/src/cmd/rc/checkparse new file mode 100755 index 00000000..cccc4058 --- /dev/null +++ b/src/cmd/rc/checkparse @@ -0,0 +1,14 @@ +#!/bin/bash + +files="$@" +if [ $# = 0 ]; then + files=$(echo ./test.rc; + grep -l '^#!/usr/local/plan9/bin/rc' /usr/local/plan9/bin/{*,*/*} 2>/dev/null; + grep -l '^#!/bin/rc' $HOME/pub/plan9/rc/bin/{*,*/*} 2>/dev/null) +fi + +for i in $files +do + echo '#' $i + diff <(./o.rc -DY $i 2>&1) <(./o.rc -D $i 2>&1) +done diff --git a/src/cmd/rc/code.c b/src/cmd/rc/code.c index eeaa3ed8..208476ae 100644 --- a/src/cmd/rc/code.c +++ b/src/cmd/rc/code.c @@ -41,6 +41,16 @@ stuffdot(int a) int compile(tree *t) { + if(flag['D']) { + struct io *s; + s = openstr(); + pfmt(s, "compile: %u\n", t); + write(2, s->strp, strlen(s->strp)); + closeio(s); + if(eflagok) // made it out of rcmain - stop executing commands, just print them + t = nil; + } + ncode = 100; codebuf = (code *)emalloc(ncode*sizeof codebuf[0]); codep = 0; diff --git a/src/cmd/rc/exec.c b/src/cmd/rc/exec.c index 172acfa3..0320976a 100644 --- a/src/cmd/rc/exec.c +++ b/src/cmd/rc/exec.c @@ -139,7 +139,7 @@ main(int argc, char *argv[]) /* needed for rcmain later */ putenv("PLAN9", unsharp("#9")); - argc = getflags(argc, argv, "SsrdiIlxepvVc:1m:1[command]", 1); + argc = getflags(argc, argv, "DSYsrdiIlxepvVc:1m:1[command]", 1); if(argc==-1) usage("[file [arg ...]]"); if(argv[0][0]=='-') @@ -901,7 +901,7 @@ Xrdcmds(void) promptstr="% "; } Noerror(); - if(yyparse()){ + if((flag['Y'] ? yyparse : parse)()){ if(!p->iflag || p->eof && !Eintr()){ if(p->cmdfile) efree(p->cmdfile); diff --git a/src/cmd/rc/fns.h b/src/cmd/rc/fns.h index c4d201dc..5721d617 100644 --- a/src/cmd/rc/fns.h +++ b/src/cmd/rc/fns.h @@ -65,3 +65,4 @@ int wordchr(int); void yyerror(char*); int yylex(void); int yyparse(void); +int parse(void); diff --git a/src/cmd/rc/io.c b/src/cmd/rc/io.c index bb8af4ab..c2e9d7b4 100644 --- a/src/cmd/rc/io.c +++ b/src/cmd/rc/io.c @@ -44,7 +44,10 @@ pfmt(io *f, char *fmt, ...) pstr(f, va_arg(ap, char *)); break; case 't': - pcmd(f, va_arg(ap, struct tree *)); + pcmd(f, va_arg(ap, tree *)); + break; + case 'u': + pcmdu(f, va_arg(ap, tree *)); break; case 'v': pval(f, va_arg(ap, struct word *)); diff --git a/src/cmd/rc/io.h b/src/cmd/rc/io.h index 21cc6b8e..6c75cc5b 100644 --- a/src/cmd/rc/io.h +++ b/src/cmd/rc/io.h @@ -25,6 +25,7 @@ void pquo(io*, char*); void pwrd(io*, char*); void pstr(io*, char*); void pcmd(io*, tree*); +void pcmdu(io*, tree*); void pval(io*, word*); void pfnc(io*, thread*); void pfmt(io*, char*, ...); diff --git a/src/cmd/rc/lex.c b/src/cmd/rc/lex.c index 973ddee8..0bdbbb4b 100644 --- a/src/cmd/rc/lex.c +++ b/src/cmd/rc/lex.c @@ -188,7 +188,7 @@ yylex(void) { int c, d = nextc(); char *w = tok; - struct tree *t; + tree *t; yylval.tree = 0; /* * Embarassing sneakiness: if the last token read was a quoted or unquoted @@ -331,6 +331,11 @@ yylex(void) yylval.tree = t; if(t->type==PIPE) skipnl(); + if(t->type==REDIR) { + skipwhite(); + if(nextc() == '{') + t->type = REDIRW; + } return t->type; case '\'': lastdol = 0; diff --git a/src/cmd/rc/mkfile b/src/cmd/rc/mkfile index 79b407f8..c1e77aef 100644 --- a/src/cmd/rc/mkfile +++ b/src/cmd/rc/mkfile @@ -10,6 +10,7 @@ OFILES=\ here.$O\ io.$O\ lex.$O\ + parse.$O\ pcmd.$O\ pfnc.$O\ simple.$O\ diff --git a/src/cmd/rc/parse.c b/src/cmd/rc/parse.c new file mode 100644 index 00000000..6071bdff --- /dev/null +++ b/src/cmd/rc/parse.c @@ -0,0 +1,532 @@ +#include "rc.h" +#include "io.h" +#include "fns.h" + +static tree* body(int tok, int *ptok); +static tree* brace(int tok); +static tree* cmd(int tok, int *ptok); +static tree* cmd2(int tok, int *ptok); +static tree* cmd3(int tok, int *ptok); +static tree* cmd4(int tok, int *ptok); +static tree* cmds(int tok, int *ptok, int nlok); +static tree* epilog(int tok, int *ptok); +static int iswordtok(int tok); +static tree* line(int tok, int *ptok); +static tree* paren(int tok); +static tree* yyredir(int tok, int *ptok); +static tree* yyword(int tok, int *ptok); +static tree* word1(int tok, int *ptok); +static tree* words(int tok, int *ptok); + +static jmp_buf yyjmp; + +static void +syntax(int tok) +{ + char buf[100]; + snprint(buf, sizeof buf, "syntax error %d", tok); + yyerror(buf); + longjmp(yyjmp, 1); +} + +int +parse(void) +{ + tree *t; + int tok; + + if(setjmp(yyjmp)) + return 1; + + // rc: { return 1;} + // | line '\n' {return !compile($1);} + + tok = yylex(); + if(tok == EOF) + return 1; + t = line(tok, &tok); + if(tok != '\n') + yyerror("missing newline at end of line"); + yylval.tree = t; + return !compile(t); +} + +static tree* +line(int tok, int *ptok) +{ + return cmds(tok, ptok, 0); +} + +static tree* +body(int tok, int *ptok) +{ + return cmds(tok, ptok, 1); +} + +static tree* +cmds(int tok, int *ptok, int nlok) +{ + tree *t, **last, *t2; + + // line: cmd + // | cmdsa line {$$=tree2(';', $1, $2);} + // cmdsa: cmd ';' + // | cmd '&' {$$=tree1('&', $1);} + + // body: cmd + // | cmdsan body {$$=tree2(';', $1, $2);} + // cmdsan: cmdsa + // | cmd '\n' + + t = nil; + last = nil; + for(;;) { + t2 = cmd(tok, &tok); + if(tok == '&') + t2 = tree1('&', t2); + if(t2 != nil) { + // slot into list t + if(last == nil) { + t = t2; + last = &t; + } else { + *last = tree2(';', *last, t2); + last = &(*last)->child[1]; + } + } + if(tok != ';' && tok != '&' && (!nlok || tok != '\n')) + break; + tok = yylex(); + } + *ptok = tok; + return t; +} + +static tree* +brace(int tok) +{ + tree *t; + + // brace: '{' body '}' {$$=tree1(BRACE, $2);} + + if(tok != '{') + syntax(tok); + t = body(yylex(), &tok); + if(tok != '}') + syntax(tok); + return tree1(BRACE, t); +} + +static tree* +paren(int tok) +{ + tree *t; + + // paren: '(' body ')' {$$=tree1(PCMD, $2);} + + if(tok != '(') + syntax(tok); + t = body(yylex(), &tok); + if(tok != ')') + syntax(tok); + return tree1(PCMD, t); +} + +static tree* +epilog(int tok, int *ptok) +{ + tree *t, *r; + + // epilog: {$$=0;} + // | redir epilog {$$=mung2($1, $1->child[0], $2);} + + if(tok != REDIR && tok != DUP) { + *ptok = tok; + return nil; + } + + r = yyredir(tok, &tok); + t = epilog(tok, &tok); + *ptok = tok; + return mung2(r, r->child[0], t); +} + +static tree* +yyredir(int tok, int *ptok) +{ + tree *r, *w; + + // redir: REDIR word {$$=mung1($1, $1->rtype==HERE?heredoc($2):$2);} + // | DUP + + switch(tok) { + default: + syntax(tok); + case DUP: + r = yylval.tree; + *ptok = yylex(); + break; + case REDIR: + r = yylval.tree; + w = yyword(yylex(), ptok); + r = mung1(r, r->rtype==HERE?heredoc(w):w); + break; + } + return r; +} + +static tree* +cmd(int tok, int *ptok) +{ + tree *t1, *t2, *t3, *t4; + + switch(tok) { + default: + return cmd2(tok, ptok); + + case IF: + // | IF paren {skipnl();} cmd {$$=mung2($1, $2, $4);} + // | IF NOT {skipnl();} cmd {$$=mung1($2, $4);} + t1 = yylval.tree; + tok = yylex(); + if(tok == NOT) { + t1 = yylval.tree; + skipnl(); + t2 = cmd(yylex(), ptok); + return mung1(t1, t2); + } + t2 = paren(tok); + skipnl(); + t3 = cmd(yylex(), ptok); + return mung2(t1, t2, t3); + + case FOR: + // | FOR '(' word IN words ')' {skipnl();} cmd + // {$$=mung3($1, $3, $5 ? $5 : tree1(PAREN, $5), $8);} + // | FOR '(' word ')' {skipnl();} cmd + // {$$=mung3($1, $3, (tree *)0, $6);} + t1 = yylval.tree; + tok = yylex(); + if(tok != '(') + syntax(tok); + t2 = yyword(yylex(), &tok); + switch(tok) { + default: + syntax(tok); + case ')': + t3 = nil; + break; + case IN: + t3 = words(yylex(), &tok); + if(t3 == nil) + t3 = tree1(PAREN, nil); + if(tok != ')') + syntax(tok); + break; + } + skipnl(); + t4 = cmd(yylex(), ptok); + return mung3(t1, t2, t3, t4); + + case WHILE: + // | WHILE paren {skipnl();} cmd + // {$$=mung2($1, $2, $4);} + t1 = yylval.tree; + t2 = paren(yylex()); + skipnl(); + t3 = cmd(yylex(), ptok); + return mung2(t1, t2, t3); + + case SWITCH: + // | SWITCH word {skipnl();} brace + // {$$=tree2(SWITCH, $2, $4);} + t1 = yyword(yylex(), &tok); + while(tok == '\n') + tok = yylex(); + t2 = brace(tok); + *ptok = yylex(); + return tree2(SWITCH, t1, t2); + } +} + +static tree* +cmd2(int tok, int *ptok) +{ + int op; + tree *t1, *t2; + + // | cmd ANDAND cmd {$$=tree2(ANDAND, $1, $3);} + // | cmd OROR cmd {$$=tree2(OROR, $1, $3);} + + t1 = cmd3(tok, &tok); + while(tok == ANDAND || tok == OROR) { + op = tok; + t2 = cmd3(yylex(), &tok); + t1 = tree2(op, t1, t2); + } + *ptok = tok; + return t1; +} + +static tree* +cmd3(int tok, int *ptok) +{ + tree *t1, *t2, *t3; + + // | cmd PIPE cmd {$$=mung2($2, $1, $3);} + t1 = cmd4(tok, &tok); + while(tok == PIPE) { + t2 = yylval.tree; + t3 = cmd4(yylex(), &tok); + t1 = mung2(t2, t1, t3); + } + *ptok = tok; + return t1; +} + +static tree* +cmd4(int tok, int *ptok) +{ + tree *t1, *t2, *t3; + + switch(tok) { + case ';': + case '&': + case '\n': + *ptok = tok; + return nil; + + case IF: + case FOR: + case SWITCH: + case WHILE: + // Note: cmd: a && for(x) y && b is a && {for (x) {y && b}}. + return cmd(tok, ptok); + + case FN: + // | FN words brace {$$=tree2(FN, $2, $3);} + // | FN words {$$=tree1(FN, $2);} + t1 = words(yylex(), &tok); + if(tok != '{') { + *ptok = tok; + return tree1(FN, t1); + } + t2 = brace(tok); + *ptok = yylex(); + return tree2(FN, t1, t2); + + case TWIDDLE: + // | TWIDDLE word words {$$=mung2($1, $2, $3);} + t1 = yylval.tree; + t2 = yyword(yylex(), &tok); + t3 = words(tok, ptok); + return mung2(t1, t2, t3); + + case BANG: + case SUBSHELL: + // | BANG cmd {$$=mung1($1, $2);} + // | SUBSHELL cmd {$$=mung1($1, $2);} + // Note: cmd3: ! x | y is !{x | y} not {!x} | y. + t1 = yylval.tree; + return mung1(t1, cmd3(yylex(), ptok)); + + case REDIR: + case DUP: + // | redir cmd %prec BANG {$$=mung2($1, $1->child[0], $2);} + // Note: cmd3: {>x echo a | tr a-z A-Z} writes A to x. + t1 = yyredir(tok, &tok); + t2 = cmd3(tok, ptok); + return mung2(t1, t1->child[0], t2); + + case '{': + // | brace epilog {$$=epimung($1, $2);} + t1 = brace(tok); + tok = yylex(); + t2 = epilog(tok, ptok); + return epimung(t1, t2); + } + + if(!iswordtok(tok)) { + *ptok = tok; + return nil; + } + + // cmd: ... + // | simple {$$=simplemung($1);} + // | assign cmd %prec BANG {$$=mung3($1, $1->child[0], $1->child[1], $2);} + // assign: first '=' word {$$=tree2('=', $1, $3);} + // Note: first is same as word except for disallowing all the leading keywords, + // but all those keywords have been picked off in the switch above. + // Except NOT, but disallowing that in yacc was likely a mistake anyway: + // there's no ambiguity in not=1 or not x y z. + t1 = yyword(tok, &tok); + if(tok == '=') { + // assignment + // Note: cmd3: {x=1 true | echo $x} echoes 1. + t1 = tree2('=', t1, yyword(yylex(), &tok)); + t2 = cmd3(tok, ptok); + return mung3(t1, t1->child[0], t1->child[1], t2); + } + + // simple: first + // | simple word {$$=tree2(ARGLIST, $1, $2);} + // | simple redir {$$=tree2(ARGLIST, $1, $2);} + for(;;) { + if(tok == REDIR || tok == DUP) { + t1 = tree2(ARGLIST, t1, yyredir(tok, &tok)); + } else if(iswordtok(tok)) { + t1 = tree2(ARGLIST, t1, yyword(tok, &tok)); + } else { + break; + } + } + *ptok = tok; + return simplemung(t1); +} + +static tree* +words(int tok, int *ptok) +{ + tree *t; + + // words: {$$=(tree*)0;} + // | words word {$$=tree2(WORDS, $1, $2);} + + t = nil; + while(iswordtok(tok)) + t = tree2(WORDS, t, yyword(tok, &tok)); + *ptok = tok; + return t; +} + +static tree* +yyword(int tok, int *ptok) +{ + tree *t; + + // word: keyword {lastword=1; $1->type=WORD;} + // | comword + // | word '^' word {$$=tree2('^', $1, $3);} + // comword: '$' word {$$=tree1('$', $2);} + // | '$' word SUB words ')' {$$=tree2(SUB, $2, $4);} + // | '"' word {$$=tree1('"', $2);} + // | COUNT word {$$=tree1(COUNT, $2);} + // | WORD + // | '`' brace {$$=tree1('`', $2);} + // | '(' words ')' {$$=tree1(PAREN, $2);} + // | REDIR brace {$$=mung1($1, $2); $$->type=PIPEFD;} + // keyword: FOR|IN|WHILE|IF|NOT|TWIDDLE|BANG|SUBSHELL|SWITCH|FN + // + // factored into: + // + // word: word1 + // | word '^' word1 + // + // word1: keyword | comword + + t = word1(tok, &tok); + while(tok == '^') + t = tree2('^', t, word1(yylex(), &tok)); + *ptok = tok; + return t; +} + +static tree* +word1(int tok, int *ptok) +{ + tree *w, *sub, *t; + + switch(tok) { + default: + syntax(tok); + + case WORD: + case FOR: + case IN: + case WHILE: + case IF: + case NOT: + case TWIDDLE: + case BANG: + case SUBSHELL: + case SWITCH: + case FN: + // | WORD + // keyword: FOR|IN|WHILE|IF|NOT|TWIDDLE|BANG|SUBSHELL|SWITCH|FN + t = yylval.tree; + t->type = WORD; + lastword = 1; + *ptok = yylex(); + return t; + + case '$': + // comword: '$' word1 {$$=tree1('$', $2);} + // | '$' word1 SUB words ')' {$$=tree2(SUB, $2, $4);} + w = word1(yylex(), &tok); + if(tok == SUB) { + sub = words(yylex(), &tok); + if(tok != ')') + syntax(tok); + *ptok = yylex(); + return tree2(SUB, w, sub); + } + *ptok = tok; + return tree1('$', w); + + case '"': + // | '"' word1 {$$=tree1('"', $2);} + return tree1('"', word1(yylex(), ptok)); + + case COUNT: + // | COUNT word1 {$$=tree1(COUNT, $2);} + return tree1(COUNT, word1(yylex(), ptok)); + + case '`': + // | '`' brace {$$=tree1('`', $2);} + t = tree1('`', brace(yylex())); + *ptok = yylex(); + return t; + + case '(': + // | '(' words ')' {$$=tree1(PAREN, $2);} + t = tree1(PAREN, words(yylex(), &tok)); + if(tok != ')') + syntax(tok); + *ptok = yylex(); + return t; + + case REDIRW: + // | REDIRW brace {$$=mung1($1, $2); $$->type=PIPEFD;} + t = yylval.tree; + t = mung1(t, brace(yylex())); + t->type = PIPEFD; + *ptok = yylex(); + return t; + } +} + +static int +iswordtok(int tok) +{ + switch(tok) { + case FOR: + case IN: + case WHILE: + case IF: + case NOT: + case TWIDDLE: + case BANG: + case SUBSHELL: + case SWITCH: + case FN: + case '$': + case '"': + case COUNT: + case WORD: + case '`': + case '(': + case REDIRW: + return 1; + } + return 0; +} diff --git a/src/cmd/rc/pcmd.c b/src/cmd/rc/pcmd.c index 8caf60a2..26bd883b 100644 --- a/src/cmd/rc/pcmd.c +++ b/src/cmd/rc/pcmd.c @@ -145,3 +145,118 @@ pcmd(io *f, tree *t) break; } } + +void +pcmdu(io *f, tree *t) /* unambiguous */ +{ + if(t==0) { + pfmt(f, ""); + return; + } + + switch(t->type){ + default: pfmt(f, "(bad %d %p %p %p)", t->type, c0, c1, c2); + break; + case '$': pfmt(f, "($ %u)", c0); + break; + case '"': pfmt(f, "($\" %u)", c0); + break; + case '&': pfmt(f, "(& %u)", c0); + break; + case '^': pfmt(f, "(^ %u %u)", c0, c1); + break; + case '`': pfmt(f, "(` %u)", c0); + break; + case ANDAND: pfmt(f, "(&& %u %u)", c0, c1); + break; + case BANG: pfmt(f, "(! %u)", c0); + break; + case BRACE: pfmt(f, "(brace %u)", c0); + break; + case COUNT: pfmt(f, "($# %u)", c0); + break; + case FN: pfmt(f, "(fn %u %u)", c0, c1); + break; + case IF: pfmt(f, "(if %u %u)", c0, c1); + break; + case NOT: pfmt(f, "(if not %u)", c0); + break; + case OROR: pfmt(f, "(|| %u %u)", c0, c1); + break; + case PCMD: + case PAREN: pfmt(f, "(paren %u)", c0); + break; + case SUB: pfmt(f, "($sub %u %u)", c0, c1); + break; + case SIMPLE: pfmt(f, "(simple %u)", c0); + break; + case SUBSHELL: pfmt(f, "(@ %u)", c0); + break; + case SWITCH: pfmt(f, "(switch %u %u)", c0, c1); + break; + case TWIDDLE: pfmt(f, "(~ %u %u)", c0, c1); + break; + case WHILE: pfmt(f, "(while %u %u)", c0, c1); + break; + case ARGLIST: + pfmt(f, "(arglist %u %u)", c0, c1); + break; + case ';': + pfmt(f, "(; %u %u)", c0, c1); + break; + case WORDS: + pfmt(f, "(words %u %u)", c0, c1); + break; + case FOR: + pfmt(f, "(for %u %u %u)", c0, c1, c2); + break; + case WORD: + if(t->quoted) + pfmt(f, "%Q", t->str); + else pdeglob(f, t->str); + break; + case DUP: + if(t->rtype==DUPFD) + pfmt(f, "(>[%d=%d]", t->fd1, t->fd0); /* yes, fd1, then fd0; read lex.c */ + else + pfmt(f, "(>[%d=]", t->fd0); /*)*/ + pfmt(f, " %u)", c1); + break; + case PIPEFD: + case REDIR: + pfmt(f, "("); + switch(t->rtype){ + case HERE: + pchr(f, '<'); + case READ: + case RDWR: + pchr(f, '<'); + if(t->rtype==RDWR) + pchr(f, '>'); + if(t->fd0!=0) + pfmt(f, "[%d]", t->fd0); + break; + case APPEND: + pchr(f, '>'); + case WRITE: + pchr(f, '>'); + if(t->fd0!=1) + pfmt(f, "[%d]", t->fd0); + break; + } + pfmt(f, "%u %u)", c0, c1); + break; + case '=': + pfmt(f, "(%u=%u %u)", c0, c1, c2); + break; + case PIPE: + pfmt(f, "(|"); + if(t->fd1==0){ + if(t->fd0!=1) + pfmt(f, "[%d]", t->fd0); + } + else pfmt(f, "[%d=%d]", t->fd0, t->fd1); + pfmt(f, " %u %u", c0, c1); + break; + } +} diff --git a/src/cmd/rc/simple.c b/src/cmd/rc/simple.c index d587227a..a7d78f6f 100644 --- a/src/cmd/rc/simple.c +++ b/src/cmd/rc/simple.c @@ -322,6 +322,7 @@ execdot(void) static int first = 1; char file[512]; word *path; + if(first){ dotcmds[0].i = 1; dotcmds[1].f = Xmark; diff --git a/src/cmd/rc/syn.y b/src/cmd/rc/syn.y index c7de3531..5c98ef80 100644 --- a/src/cmd/rc/syn.y +++ b/src/cmd/rc/syn.y @@ -1,5 +1,5 @@ %term FOR IN WHILE IF NOT TWIDDLE BANG SUBSHELL SWITCH FN -%term WORD REDIR DUP PIPE SUB +%term WORD REDIR REDIRW DUP PIPE SUB %term SIMPLE ARGLIST WORDS BRACE PAREN PCMD PIPEFD /* not used in syntax */ /* operator priorities -- lowest first */ %left IF WHILE FOR SWITCH ')' NOT @@ -19,7 +19,7 @@ %type line paren brace body cmdsa cmdsan assign epilog redir %type cmd simple first word comword keyword words %type NOT FOR IN WHILE IF TWIDDLE BANG SUBSHELL SWITCH FN -%type WORD REDIR DUP PIPE +%type WORD REDIR REDIRW DUP PIPE %% rc: { return 1;} | line '\n' {return !compile($1);} @@ -45,7 +45,7 @@ cmd: {$$=0;} | IF NOT {skipnl();} cmd {$$=mung1($2, $4);} | FOR '(' word IN words ')' {skipnl();} cmd /* - * if ``words'' is nil, we need a tree element to distinguish between + * if ``words'' is nil, we need a tree element to distinguish between * for(i in ) and for(i), the former being a loop over the empty set * and the latter being the implicit argument loop. so if $5 is nil * (the empty set), we represent it as "()". don't parenthesize non-nil @@ -73,7 +73,7 @@ cmd: {$$=0;} simple: first | simple word {$$=tree2(ARGLIST, $1, $2);} | simple redir {$$=tree2(ARGLIST, $1, $2);} -first: comword +first: comword | first '^' word {$$=tree2('^', $1, $3);} word: keyword {lastword=1; $1->type=WORD;} | comword @@ -85,7 +85,7 @@ comword: '$' word {$$=tree1('$', $2);} | WORD | '`' brace {$$=tree1('`', $2);} | '(' words ')' {$$=tree1(PAREN, $2);} -| REDIR brace {$$=mung1($1, $2); $$->type=PIPEFD;} +| REDIRW brace {$$=mung1($1, $2); $$->type=PIPEFD;} keyword: FOR|IN|WHILE|IF|NOT|TWIDDLE|BANG|SUBSHELL|SWITCH|FN words: {$$=(struct tree*)0;} | words word {$$=tree2(WORDS, $1, $2);} diff --git a/src/cmd/rc/test.rc b/src/cmd/rc/test.rc new file mode 100644 index 00000000..8cb11e0f --- /dev/null +++ b/src/cmd/rc/test.rc @@ -0,0 +1,38 @@ +# test for parser + +{a; b; c} +x=y a && b || c +x=y a | b | c +x=y for(i) a | b +>x for(i) a | b +>x a || b && c +a >x || b && c +a | for(i) b | c +fn x {y; z} | b && c +if (x) y +if not z +`{} >x >[1=2]y >[3=] z Date: Mon, 4 May 2020 22:52:27 -0400 Subject: rc: move newline handling into parser --- src/cmd/rc/checkparse | 6 ++++-- src/cmd/rc/lex.c | 24 ++++++++++++++++-------- src/cmd/rc/parse.c | 27 +++++++++++++++------------ src/cmd/rc/pcmd.c | 5 ++++- src/cmd/rc/syn.y | 2 +- src/cmd/rc/test.rc | 27 +++++++++++++++++++++++++++ 6 files changed, 67 insertions(+), 24 deletions(-) (limited to 'src') diff --git a/src/cmd/rc/checkparse b/src/cmd/rc/checkparse index cccc4058..1ff84667 100755 --- a/src/cmd/rc/checkparse +++ b/src/cmd/rc/checkparse @@ -9,6 +9,8 @@ fi for i in $files do - echo '#' $i - diff <(./o.rc -DY $i 2>&1) <(./o.rc -D $i 2>&1) + if ! diff <(./o.rc -DY $i 2>&1) <(./o.rc -D $i 2>&1); then + echo '#' $i + exit 1 + fi done diff --git a/src/cmd/rc/lex.c b/src/cmd/rc/lex.c index 0bdbbb4b..58338479 100644 --- a/src/cmd/rc/lex.c +++ b/src/cmd/rc/lex.c @@ -102,15 +102,17 @@ pprompt(void) doprompt = 0; } -void +int skipwhite(void) { - int c; + int c, skipped; + skipped = 0; for(;;){ c = nextc(); /* Why did this used to be if(!inquote && c=='#') ?? */ if(c=='#'){ incomm = 1; + skipped = 1; for(;;){ c = nextc(); if(c=='\n' || c==EOF) { @@ -120,9 +122,12 @@ skipwhite(void) advance(); } } - if(c==' ' || c=='\t') + if(c==' ' || c=='\t') { + skipped = 1; advance(); - else return; + } + else + return skipped; } } @@ -210,7 +215,8 @@ yylex(void) } } inquote = 0; - skipwhite(); + if(skipwhite() && flag['Z']) + return SP; switch(c = advance()){ case EOF: lastdol = 0; @@ -231,7 +237,8 @@ yylex(void) case '&': lastdol = 0; if(nextis('&')){ - skipnl(); + if(flag['Y']) + skipnl(); strcpy(tok, "&&"); return ANDAND; } @@ -240,7 +247,8 @@ yylex(void) case '|': lastdol = 0; if(nextis(c)){ - skipnl(); + if(flag['Y']) + skipnl(); strcpy(tok, "||"); return OROR; } @@ -329,7 +337,7 @@ yylex(void) } *w='\0'; yylval.tree = t; - if(t->type==PIPE) + if(t->type==PIPE && flag['Y']) skipnl(); if(t->type==REDIR) { skipwhite(); diff --git a/src/cmd/rc/parse.c b/src/cmd/rc/parse.c index 6071bdff..466b7da2 100644 --- a/src/cmd/rc/parse.c +++ b/src/cmd/rc/parse.c @@ -20,6 +20,14 @@ static tree* words(int tok, int *ptok); static jmp_buf yyjmp; +static int +dropnl(int tok) +{ + while(tok == '\n') + tok = yylex(); + return tok; +} + static void syntax(int tok) { @@ -191,13 +199,11 @@ cmd(int tok, int *ptok) tok = yylex(); if(tok == NOT) { t1 = yylval.tree; - skipnl(); - t2 = cmd(yylex(), ptok); + t2 = cmd(dropnl(yylex()), ptok); return mung1(t1, t2); } t2 = paren(tok); - skipnl(); - t3 = cmd(yylex(), ptok); + t3 = cmd(dropnl(yylex()), ptok); return mung2(t1, t2, t3); case FOR: @@ -224,8 +230,7 @@ cmd(int tok, int *ptok) syntax(tok); break; } - skipnl(); - t4 = cmd(yylex(), ptok); + t4 = cmd(dropnl(yylex()), ptok); return mung3(t1, t2, t3, t4); case WHILE: @@ -233,16 +238,14 @@ cmd(int tok, int *ptok) // {$$=mung2($1, $2, $4);} t1 = yylval.tree; t2 = paren(yylex()); - skipnl(); - t3 = cmd(yylex(), ptok); + t3 = cmd(dropnl(yylex()), ptok); return mung2(t1, t2, t3); case SWITCH: // | SWITCH word {skipnl();} brace // {$$=tree2(SWITCH, $2, $4);} t1 = yyword(yylex(), &tok); - while(tok == '\n') - tok = yylex(); + tok = dropnl(tok); // doesn't work in yacc grammar but works here! t2 = brace(tok); *ptok = yylex(); return tree2(SWITCH, t1, t2); @@ -261,7 +264,7 @@ cmd2(int tok, int *ptok) t1 = cmd3(tok, &tok); while(tok == ANDAND || tok == OROR) { op = tok; - t2 = cmd3(yylex(), &tok); + t2 = cmd3(dropnl(yylex()), &tok); t1 = tree2(op, t1, t2); } *ptok = tok; @@ -277,7 +280,7 @@ cmd3(int tok, int *ptok) t1 = cmd4(tok, &tok); while(tok == PIPE) { t2 = yylval.tree; - t3 = cmd4(yylex(), &tok); + t3 = cmd4(dropnl(yylex()), &tok); t1 = mung2(t2, t1, t3); } *ptok = tok; diff --git a/src/cmd/rc/pcmd.c b/src/cmd/rc/pcmd.c index 26bd883b..cae84737 100644 --- a/src/cmd/rc/pcmd.c +++ b/src/cmd/rc/pcmd.c @@ -244,7 +244,10 @@ pcmdu(io *f, tree *t) /* unambiguous */ pfmt(f, "[%d]", t->fd0); break; } - pfmt(f, "%u %u)", c0, c1); + if(t->rtype == HERE) + pfmt(f, "HERE %u)", c1); + else + pfmt(f, "%u %u)", c0, c1); break; case '=': pfmt(f, "(%u=%u %u)", c0, c1, c2); diff --git a/src/cmd/rc/syn.y b/src/cmd/rc/syn.y index 5c98ef80..e3decd41 100644 --- a/src/cmd/rc/syn.y +++ b/src/cmd/rc/syn.y @@ -1,4 +1,4 @@ -%term FOR IN WHILE IF NOT TWIDDLE BANG SUBSHELL SWITCH FN +%term FOR IN WHILE IF NOT TWIDDLE BANG SUBSHELL SWITCH FN SP %term WORD REDIR REDIRW DUP PIPE SUB %term SIMPLE ARGLIST WORDS BRACE PAREN PCMD PIPEFD /* not used in syntax */ /* operator priorities -- lowest first */ diff --git a/src/cmd/rc/test.rc b/src/cmd/rc/test.rc index 8cb11e0f..5c658132 100644 --- a/src/cmd/rc/test.rc +++ b/src/cmd/rc/test.rc @@ -36,3 +36,30 @@ $#$x x for in while if not ~ ! @ switch fn x not$y a;b;c +if(x) +y +if(x) +{ +y +} +if not +z +for(x) +y +for(x in y) +z +while(x) +y +# yacc doesn't accept a newline before the brace +# even though the rule is written as if it would +switch x { +} +switch (x) { +} +z +x && +y +x || +y +x | +y -- cgit v1.2.3 From 7d6a248f2c68d70f58387afc69e73e695c3d940c Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Mon, 4 May 2020 23:20:08 -0400 Subject: rc: move free carat handling into parser This fixes at least one shell script (printfont) that expected 'x'`{y}'z' to mean 'x'^`{y}^'z' as it now does. Before it meant: 'x'^`{y} 'z' One surprise is that adjacent lists get a free carat: (x y z)(1 2 3) is (x1 y2 z3) This doesn't affect any rc script in Plan 9 or plan9port. --- src/cmd/rc/lex.c | 6 +- src/cmd/rc/parse.c | 158 +++++++++++++++++++++++++++++------------------------ src/cmd/rc/syn.y | 2 +- src/cmd/rc/test.rc | 11 ++++ 4 files changed, 103 insertions(+), 74 deletions(-) (limited to 'src') diff --git a/src/cmd/rc/lex.c b/src/cmd/rc/lex.c index 58338479..48bd70de 100644 --- a/src/cmd/rc/lex.c +++ b/src/cmd/rc/lex.c @@ -202,7 +202,7 @@ yylex(void) * if the next character is the first character of a simple or compound word, * we insert a `^' before it. */ - if(lastword){ + if(lastword && flag['Y']){ lastword = 0; if(d=='('){ advance(); @@ -215,8 +215,8 @@ yylex(void) } } inquote = 0; - if(skipwhite() && flag['Z']) - return SP; + if(skipwhite() && !flag['Y']) + return ' '; switch(c = advance()){ case EOF: lastdol = 0; diff --git a/src/cmd/rc/parse.c b/src/cmd/rc/parse.c index 466b7da2..11be951b 100644 --- a/src/cmd/rc/parse.c +++ b/src/cmd/rc/parse.c @@ -23,7 +23,15 @@ static jmp_buf yyjmp; static int dropnl(int tok) { - while(tok == '\n') + while(tok == ' ' || tok == '\n') + tok = yylex(); + return tok; +} + +static int +dropsp(int tok) +{ + while(tok == ' ') tok = yylex(); return tok; } @@ -49,7 +57,7 @@ parse(void) // rc: { return 1;} // | line '\n' {return !compile($1);} - tok = yylex(); + tok = dropsp(yylex()); if(tok == EOF) return 1; t = line(tok, &tok); @@ -117,6 +125,7 @@ brace(int tok) // brace: '{' body '}' {$$=tree1(BRACE, $2);} + tok = dropsp(tok); if(tok != '{') syntax(tok); t = body(yylex(), &tok); @@ -132,6 +141,7 @@ paren(int tok) // paren: '(' body ')' {$$=tree1(PCMD, $2);} + tok = dropsp(tok); if(tok != '(') syntax(tok); t = body(yylex(), &tok); @@ -172,11 +182,12 @@ yyredir(int tok, int *ptok) syntax(tok); case DUP: r = yylval.tree; - *ptok = yylex(); + *ptok = dropsp(yylex()); break; case REDIR: r = yylval.tree; - w = yyword(yylex(), ptok); + w = yyword(yylex(), &tok); + *ptok = dropsp(tok); r = mung1(r, r->rtype==HERE?heredoc(w):w); break; } @@ -186,17 +197,67 @@ yyredir(int tok, int *ptok) static tree* cmd(int tok, int *ptok) { - tree *t1, *t2, *t3, *t4; - + tok = dropsp(tok); switch(tok) { default: return cmd2(tok, ptok); + } +} + +static tree* +cmd2(int tok, int *ptok) +{ + int op; + tree *t1, *t2; + + // | cmd ANDAND cmd {$$=tree2(ANDAND, $1, $3);} + // | cmd OROR cmd {$$=tree2(OROR, $1, $3);} + + t1 = cmd3(tok, &tok); + while(tok == ANDAND || tok == OROR) { + op = tok; + t2 = cmd3(dropnl(yylex()), &tok); + t1 = tree2(op, t1, t2); + } + *ptok = tok; + return t1; +} + +static tree* +cmd3(int tok, int *ptok) +{ + tree *t1, *t2, *t3; + + // | cmd PIPE cmd {$$=mung2($2, $1, $3);} + t1 = cmd4(tok, &tok); + while(tok == PIPE) { + t2 = yylval.tree; + t3 = cmd4(dropnl(yylex()), &tok); + t1 = mung2(t2, t1, t3); + } + *ptok = tok; + return t1; +} + +static tree* +cmd4(int tok, int *ptok) +{ + tree *t1, *t2, *t3, *t4; + + tok = dropsp(tok); + switch(tok) { + case ';': + case '&': + case '\n': + *ptok = tok; + return nil; + case IF: // | IF paren {skipnl();} cmd {$$=mung2($1, $2, $4);} // | IF NOT {skipnl();} cmd {$$=mung1($2, $4);} t1 = yylval.tree; - tok = yylex(); + tok = dropsp(yylex()); if(tok == NOT) { t1 = yylval.tree; t2 = cmd(dropnl(yylex()), ptok); @@ -212,7 +273,7 @@ cmd(int tok, int *ptok) // | FOR '(' word ')' {skipnl();} cmd // {$$=mung3($1, $3, (tree *)0, $6);} t1 = yylval.tree; - tok = yylex(); + tok = dropsp(yylex()); if(tok != '(') syntax(tok); t2 = yyword(yylex(), &tok); @@ -247,62 +308,8 @@ cmd(int tok, int *ptok) t1 = yyword(yylex(), &tok); tok = dropnl(tok); // doesn't work in yacc grammar but works here! t2 = brace(tok); - *ptok = yylex(); + *ptok = dropsp(yylex()); return tree2(SWITCH, t1, t2); - } -} - -static tree* -cmd2(int tok, int *ptok) -{ - int op; - tree *t1, *t2; - - // | cmd ANDAND cmd {$$=tree2(ANDAND, $1, $3);} - // | cmd OROR cmd {$$=tree2(OROR, $1, $3);} - - t1 = cmd3(tok, &tok); - while(tok == ANDAND || tok == OROR) { - op = tok; - t2 = cmd3(dropnl(yylex()), &tok); - t1 = tree2(op, t1, t2); - } - *ptok = tok; - return t1; -} - -static tree* -cmd3(int tok, int *ptok) -{ - tree *t1, *t2, *t3; - - // | cmd PIPE cmd {$$=mung2($2, $1, $3);} - t1 = cmd4(tok, &tok); - while(tok == PIPE) { - t2 = yylval.tree; - t3 = cmd4(dropnl(yylex()), &tok); - t1 = mung2(t2, t1, t3); - } - *ptok = tok; - return t1; -} - -static tree* -cmd4(int tok, int *ptok) -{ - tree *t1, *t2, *t3; - - switch(tok) { - case ';': - case '&': - case '\n': - *ptok = tok; - return nil; - - case IF: - case FOR: - case SWITCH: - case WHILE: // Note: cmd: a && for(x) y && b is a && {for (x) {y && b}}. return cmd(tok, ptok); @@ -315,7 +322,7 @@ cmd4(int tok, int *ptok) return tree1(FN, t1); } t2 = brace(tok); - *ptok = yylex(); + *ptok = dropsp(yylex()); return tree2(FN, t1, t2); case TWIDDLE: @@ -344,7 +351,7 @@ cmd4(int tok, int *ptok) case '{': // | brace epilog {$$=epimung($1, $2);} t1 = brace(tok); - tok = yylex(); + tok = dropsp(yylex()); t2 = epilog(tok, ptok); return epimung(t1, t2); } @@ -396,6 +403,7 @@ words(int tok, int *ptok) // | words word {$$=tree2(WORDS, $1, $2);} t = nil; + tok = dropsp(tok); while(iswordtok(tok)) t = tree2(WORDS, t, yyword(tok, &tok)); *ptok = tok; @@ -428,9 +436,19 @@ yyword(int tok, int *ptok) // word1: keyword | comword t = word1(tok, &tok); - while(tok == '^') - t = tree2('^', t, word1(yylex(), &tok)); - *ptok = tok; + for(;;) { + if(iswordtok(tok)) { + t = tree2('^', t, word1(tok, &tok)); + continue; + } + tok = dropsp(tok); + if(tok == '^') { + t = tree2('^', t, word1(yylex(), &tok)); + continue; + } + break; + } + *ptok = dropsp(tok); return t; } @@ -439,6 +457,7 @@ word1(int tok, int *ptok) { tree *w, *sub, *t; + tok = dropsp(tok); switch(tok) { default: syntax(tok); @@ -458,7 +477,6 @@ word1(int tok, int *ptok) // keyword: FOR|IN|WHILE|IF|NOT|TWIDDLE|BANG|SUBSHELL|SWITCH|FN t = yylval.tree; t->type = WORD; - lastword = 1; *ptok = yylex(); return t; @@ -466,7 +484,7 @@ word1(int tok, int *ptok) // comword: '$' word1 {$$=tree1('$', $2);} // | '$' word1 SUB words ')' {$$=tree2(SUB, $2, $4);} w = word1(yylex(), &tok); - if(tok == SUB) { + if(tok == '(') { sub = words(yylex(), &tok); if(tok != ')') syntax(tok); diff --git a/src/cmd/rc/syn.y b/src/cmd/rc/syn.y index e3decd41..5c98ef80 100644 --- a/src/cmd/rc/syn.y +++ b/src/cmd/rc/syn.y @@ -1,4 +1,4 @@ -%term FOR IN WHILE IF NOT TWIDDLE BANG SUBSHELL SWITCH FN SP +%term FOR IN WHILE IF NOT TWIDDLE BANG SUBSHELL SWITCH FN %term WORD REDIR REDIRW DUP PIPE SUB %term SIMPLE ARGLIST WORDS BRACE PAREN PCMD PIPEFD /* not used in syntax */ /* operator priorities -- lowest first */ diff --git a/src/cmd/rc/test.rc b/src/cmd/rc/test.rc index 5c658132..7a83ad17 100644 --- a/src/cmd/rc/test.rc +++ b/src/cmd/rc/test.rc @@ -1,5 +1,9 @@ # test for parser +a +a b +a|b +a | b {a; b; c} x=y a && b || c x=y a | b | c @@ -63,3 +67,10 @@ x || y x | y +switch x {y} && z +switch x {} | y + +OPTIONS=$OPTIONS' /axescount '^`{echo $1 | sed s/-a//}^' def' + +# bug in old printfont script - expected more free carats +# OPTIONS=$OPTIONS' /axescount '`{echo $1 | sed s/-a//}' def' -- cgit v1.2.3 From ff74f7cdda7b08da6fe7c8bbcca990305fd6b547 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Mon, 4 May 2020 23:31:59 -0400 Subject: rc: allow unquoted = in command arguments dd fans rejoice! Also helps with commands like go test -run=x. --- src/cmd/rc/parse.c | 28 ++++++++++++++++++---------- src/cmd/rc/test.rc | 9 +++++++++ 2 files changed, 27 insertions(+), 10 deletions(-) (limited to 'src') diff --git a/src/cmd/rc/parse.c b/src/cmd/rc/parse.c index 11be951b..a46931dc 100644 --- a/src/cmd/rc/parse.c +++ b/src/cmd/rc/parse.c @@ -14,7 +14,7 @@ static int iswordtok(int tok); static tree* line(int tok, int *ptok); static tree* paren(int tok); static tree* yyredir(int tok, int *ptok); -static tree* yyword(int tok, int *ptok); +static tree* yyword(int tok, int *ptok, int eqok); static tree* word1(int tok, int *ptok); static tree* words(int tok, int *ptok); @@ -186,7 +186,7 @@ yyredir(int tok, int *ptok) break; case REDIR: r = yylval.tree; - w = yyword(yylex(), &tok); + w = yyword(yylex(), &tok, 1); *ptok = dropsp(tok); r = mung1(r, r->rtype==HERE?heredoc(w):w); break; @@ -276,7 +276,7 @@ cmd4(int tok, int *ptok) tok = dropsp(yylex()); if(tok != '(') syntax(tok); - t2 = yyword(yylex(), &tok); + t2 = yyword(yylex(), &tok, 1); switch(tok) { default: syntax(tok); @@ -305,7 +305,7 @@ cmd4(int tok, int *ptok) case SWITCH: // | SWITCH word {skipnl();} brace // {$$=tree2(SWITCH, $2, $4);} - t1 = yyword(yylex(), &tok); + t1 = yyword(yylex(), &tok, 1); tok = dropnl(tok); // doesn't work in yacc grammar but works here! t2 = brace(tok); *ptok = dropsp(yylex()); @@ -328,7 +328,7 @@ cmd4(int tok, int *ptok) case TWIDDLE: // | TWIDDLE word words {$$=mung2($1, $2, $3);} t1 = yylval.tree; - t2 = yyword(yylex(), &tok); + t2 = yyword(yylex(), &tok, 1); t3 = words(tok, ptok); return mung2(t1, t2, t3); @@ -369,11 +369,11 @@ cmd4(int tok, int *ptok) // but all those keywords have been picked off in the switch above. // Except NOT, but disallowing that in yacc was likely a mistake anyway: // there's no ambiguity in not=1 or not x y z. - t1 = yyword(tok, &tok); + t1 = yyword(tok, &tok, 0); if(tok == '=') { // assignment // Note: cmd3: {x=1 true | echo $x} echoes 1. - t1 = tree2('=', t1, yyword(yylex(), &tok)); + t1 = tree2('=', t1, yyword(yylex(), &tok, 1)); t2 = cmd3(tok, ptok); return mung3(t1, t1->child[0], t1->child[1], t2); } @@ -385,7 +385,7 @@ cmd4(int tok, int *ptok) if(tok == REDIR || tok == DUP) { t1 = tree2(ARGLIST, t1, yyredir(tok, &tok)); } else if(iswordtok(tok)) { - t1 = tree2(ARGLIST, t1, yyword(tok, &tok)); + t1 = tree2(ARGLIST, t1, yyword(tok, &tok, 1)); } else { break; } @@ -405,13 +405,13 @@ words(int tok, int *ptok) t = nil; tok = dropsp(tok); while(iswordtok(tok)) - t = tree2(WORDS, t, yyword(tok, &tok)); + t = tree2(WORDS, t, yyword(tok, &tok, 1)); *ptok = tok; return t; } static tree* -yyword(int tok, int *ptok) +yyword(int tok, int *ptok, int eqok) { tree *t; @@ -436,6 +436,8 @@ yyword(int tok, int *ptok) // word1: keyword | comword t = word1(tok, &tok); + if(tok == '=' && !eqok) + goto out; for(;;) { if(iswordtok(tok)) { t = tree2('^', t, word1(tok, &tok)); @@ -448,6 +450,7 @@ yyword(int tok, int *ptok) } break; } +out: *ptok = dropsp(tok); return t; } @@ -480,6 +483,10 @@ word1(int tok, int *ptok) *ptok = yylex(); return t; + case '=': + *ptok = yylex(); + return token("=", WORD); + case '$': // comword: '$' word1 {$$=tree1('$', $2);} // | '$' word1 SUB words ')' {$$=tree2(SUB, $2, $4);} @@ -547,6 +554,7 @@ iswordtok(int tok) case '`': case '(': case REDIRW: + case '=': return 1; } return 0; diff --git a/src/cmd/rc/test.rc b/src/cmd/rc/test.rc index 7a83ad17..f667b840 100644 --- a/src/cmd/rc/test.rc +++ b/src/cmd/rc/test.rc @@ -74,3 +74,12 @@ OPTIONS=$OPTIONS' /axescount '^`{echo $1 | sed s/-a//}^' def' # bug in old printfont script - expected more free carats # OPTIONS=$OPTIONS' /axescount '`{echo $1 | sed s/-a//}' def' + +(x) = y +x=y +x = y + +# works now! +# x y=z +# x =y +# x -flag=y -- cgit v1.2.3 From 601e07b63653d0fed91594ebba261b733d017653 Mon Sep 17 00:00:00 2001 From: Russ Cox Date: Tue, 5 May 2020 08:29:45 -0400 Subject: rc: clean up parser levels, disallow free carats on lists --- src/cmd/rc/checkparse | 13 ++++++++++--- src/cmd/rc/lex.c | 2 +- src/cmd/rc/parse.c | 45 ++++++++++++++++++--------------------------- src/cmd/rc/test.rc | 8 ++++++++ 4 files changed, 37 insertions(+), 31 deletions(-) (limited to 'src') diff --git a/src/cmd/rc/checkparse b/src/cmd/rc/checkparse index 1ff84667..0a1472b2 100755 --- a/src/cmd/rc/checkparse +++ b/src/cmd/rc/checkparse @@ -1,16 +1,23 @@ #!/bin/bash +aflag=false +if [ "$1" = -a ]; then + aflag=true + shift +fi + files="$@" if [ $# = 0 ]; then files=$(echo ./test.rc; grep -l '^#!/usr/local/plan9/bin/rc' /usr/local/plan9/bin/{*,*/*} 2>/dev/null; - grep -l '^#!/bin/rc' $HOME/pub/plan9/rc/bin/{*,*/*} 2>/dev/null) + grep -R -l '^#!/bin/rc' $HOME/pub/plan9 | egrep -v 'plan9/(lib/(oui|pci)|sys/man|sys/lib/man|sys/lib/scsicodes)' 2>/dev/null) fi for i in $files do if ! diff <(./o.rc -DY $i 2>&1) <(./o.rc -D $i 2>&1); then - echo '#' $i - exit 1 + echo '^^^' $i + ! $aflag && exit 1 fi done + diff --git a/src/cmd/rc/lex.c b/src/cmd/rc/lex.c index 48bd70de..e4410c00 100644 --- a/src/cmd/rc/lex.c +++ b/src/cmd/rc/lex.c @@ -206,7 +206,7 @@ yylex(void) lastword = 0; if(d=='('){ advance(); - strcpy(tok, "( [SUB]"); + strcpy(tok, "("); return SUB; } if(wordchr(d) || d=='\'' || d=='`' || d=='$' || d=='"'){ diff --git a/src/cmd/rc/parse.c b/src/cmd/rc/parse.c index a46931dc..dd102190 100644 --- a/src/cmd/rc/parse.c +++ b/src/cmd/rc/parse.c @@ -7,7 +7,6 @@ static tree* brace(int tok); static tree* cmd(int tok, int *ptok); static tree* cmd2(int tok, int *ptok); static tree* cmd3(int tok, int *ptok); -static tree* cmd4(int tok, int *ptok); static tree* cmds(int tok, int *ptok, int nlok); static tree* epilog(int tok, int *ptok); static int iswordtok(int tok); @@ -39,9 +38,8 @@ dropsp(int tok) static void syntax(int tok) { - char buf[100]; - snprint(buf, sizeof buf, "syntax error %d", tok); - yyerror(buf); + USED(tok); + yyerror("syntax error"); longjmp(yyjmp, 1); } @@ -196,17 +194,6 @@ yyredir(int tok, int *ptok) static tree* cmd(int tok, int *ptok) -{ - tok = dropsp(tok); - switch(tok) { - default: - return cmd2(tok, ptok); - - } -} - -static tree* -cmd2(int tok, int *ptok) { int op; tree *t1, *t2; @@ -214,10 +201,11 @@ cmd2(int tok, int *ptok) // | cmd ANDAND cmd {$$=tree2(ANDAND, $1, $3);} // | cmd OROR cmd {$$=tree2(OROR, $1, $3);} - t1 = cmd3(tok, &tok); + tok = dropsp(tok); + t1 = cmd2(tok, &tok); while(tok == ANDAND || tok == OROR) { op = tok; - t2 = cmd3(dropnl(yylex()), &tok); + t2 = cmd2(dropnl(yylex()), &tok); t1 = tree2(op, t1, t2); } *ptok = tok; @@ -225,15 +213,15 @@ cmd2(int tok, int *ptok) } static tree* -cmd3(int tok, int *ptok) +cmd2(int tok, int *ptok) { tree *t1, *t2, *t3; // | cmd PIPE cmd {$$=mung2($2, $1, $3);} - t1 = cmd4(tok, &tok); + t1 = cmd3(tok, &tok); while(tok == PIPE) { t2 = yylval.tree; - t3 = cmd4(dropnl(yylex()), &tok); + t3 = cmd3(dropnl(yylex()), &tok); t1 = mung2(t2, t1, t3); } *ptok = tok; @@ -241,7 +229,7 @@ cmd3(int tok, int *ptok) } static tree* -cmd4(int tok, int *ptok) +cmd3(int tok, int *ptok) { tree *t1, *t2, *t3, *t4; @@ -336,16 +324,16 @@ cmd4(int tok, int *ptok) case SUBSHELL: // | BANG cmd {$$=mung1($1, $2);} // | SUBSHELL cmd {$$=mung1($1, $2);} - // Note: cmd3: ! x | y is !{x | y} not {!x} | y. + // Note: cmd2: ! x | y is !{x | y} not {!x} | y. t1 = yylval.tree; - return mung1(t1, cmd3(yylex(), ptok)); + return mung1(t1, cmd2(yylex(), ptok)); case REDIR: case DUP: // | redir cmd %prec BANG {$$=mung2($1, $1->child[0], $2);} - // Note: cmd3: {>x echo a | tr a-z A-Z} writes A to x. + // Note: cmd2: {>x echo a | tr a-z A-Z} writes A to x. t1 = yyredir(tok, &tok); - t2 = cmd3(tok, ptok); + t2 = cmd2(tok, ptok); return mung2(t1, t1->child[0], t2); case '{': @@ -372,9 +360,9 @@ cmd4(int tok, int *ptok) t1 = yyword(tok, &tok, 0); if(tok == '=') { // assignment - // Note: cmd3: {x=1 true | echo $x} echoes 1. + // Note: cmd2: {x=1 true | echo $x} echoes 1. t1 = tree2('=', t1, yyword(yylex(), &tok, 1)); - t2 = cmd3(tok, ptok); + t2 = cmd2(tok, ptok); return mung3(t1, t1->child[0], t1->child[1], t2); } @@ -440,6 +428,9 @@ yyword(int tok, int *ptok, int eqok) goto out; for(;;) { if(iswordtok(tok)) { + // No free carats around parens. + if(t->type == PAREN || tok == '(') + syntax(tok); t = tree2('^', t, word1(tok, &tok)); continue; } diff --git a/src/cmd/rc/test.rc b/src/cmd/rc/test.rc index f667b840..4a33d87c 100644 --- a/src/cmd/rc/test.rc +++ b/src/cmd/rc/test.rc @@ -83,3 +83,11 @@ x = y # x y=z # x =y # x -flag=y + +>z x | y + +# rejected now, was like parens were spaces before. +# echo Formatting Venti arenas and indices (this takes a while). + + +# echo $STATLINE(1)^$STATLINE(3)' '$STATLINE(2)' '$STATLINE(4)' '$LSLINE(6) -- cgit v1.2.3