summaryrefslogtreecommitdiff
path: root/sys/src/cmd/rc/tree.c
diff options
context:
space:
mode:
authorcinap_lenrek <cinap_lenrek@felloff.net>2021-12-31 15:27:10 +0000
committercinap_lenrek <cinap_lenrek@felloff.net>2021-12-31 15:27:10 +0000
commitb90036a062ca330ac5f667cd1ee503686cbe0b80 (patch)
treeab9538715188d5017843c6d94e2ee4c5e155448a /sys/src/cmd/rc/tree.c
parent855cf4326f5a07d7142c2d8918f5fa856d912b85 (diff)
rc: fix everything
Untangle the lexer and interpreter thread state. Fix the file and line number error reporting, getting rid of Xsrcfile instruction, as the whole code block can only come from a single file, stuff the source file in slot[1] of the code block instead. Remove limitations for globber (path element limits) and be more intelligent about handling globbing by inserting Xglob instruction only when needed and not run it over every Xsimple argument list. Remove fragile ndot magic and make it explicit by adding the -q flag to . builtin command. Add -b flag for full compilation. Make exitnext() smart, so we can speculate thru rcmain and avoid the fork(). Get rid of all print(2) format functions and use io instead. Improve the io library, adding rstr() to handle tokenization, which allows us to look ahead in the already read buffer for the terminators, avoiding alot of string copies. Auto indent pcmd(), to make line number reporting more usefull. Implement here documents properly, so they can work everywhere.
Diffstat (limited to 'sys/src/cmd/rc/tree.c')
-rw-r--r--sys/src/cmd/rc/tree.c91
1 files changed, 61 insertions, 30 deletions
diff --git a/sys/src/cmd/rc/tree.c b/sys/src/cmd/rc/tree.c
index ce9fd152a..da7157312 100644
--- a/sys/src/cmd/rc/tree.c
+++ b/sys/src/cmd/rc/tree.c
@@ -1,22 +1,30 @@
#include "rc.h"
-#include "exec.h"
#include "io.h"
#include "fns.h"
-tree *treenodes;
+
/*
* create and clear a new tree node, and add it
* to the node list.
*/
+static tree *treefree, *treenodes;
tree*
newtree(void)
{
- tree *t = new(tree);
+ tree *t;
+
+ t = treefree;
+ if(t==0)
+ t = new(tree);
+ else
+ treefree = t->next;
+ t->quoted = 0;
+ t->glob = 0;
t->iskw = 0;
t->str = 0;
t->child[0] = t->child[1] = t->child[2] = 0;
+ t->line = lex->line;
t->next = treenodes;
- t->line = runq->lexline;
treenodes = t;
return t;
}
@@ -24,12 +32,21 @@ newtree(void)
void
freenodes(void)
{
- tree *t, *u;
- for(t = treenodes;t;t = u){
- u = t->next;
- if(t->str)
+ tree *t;
+
+ t = treenodes;
+ while(t){
+ if(t->str){
free(t->str);
- free(t);
+ t->str = 0;
+ }
+ t->child[0] = t->child[1] = t->child[2] = 0;
+ if(t->next==0){
+ t->next = treefree;
+ treefree = treenodes;
+ break;
+ }
+ t = t->next;
}
treenodes = 0;
}
@@ -61,6 +78,13 @@ tree3(int type, tree *c0, tree *c1, tree *c2)
t->child[0] = c0;
t->child[1] = c1;
t->child[2] = c2;
+
+ if(c0)
+ t->line = c0->line;
+ else if(c1)
+ t->line = c1->line;
+ else if(c2)
+ t->line = c2->line;
return t;
}
@@ -98,22 +122,18 @@ epimung(tree *comp, tree *epi)
p->child[1] = comp;
return epi;
}
+
/*
* Add a SIMPLE node at the root of t and percolate all the redirections
* up to the root.
*/
-
tree*
simplemung(tree *t)
{
tree *u;
- struct io *s;
t = tree1(SIMPLE, t);
- s = openstr();
- pfmt(s, "%t", t);
- t->str = estrdup((char *)s->strp);
- closeio(s);
+ t->str = fnstr(t);
for(u = t->child[0];u->type==ARGLIST;u = u->child[0]){
if(u->child[1]->type==DUP
|| u->child[1]->type==REDIR){
@@ -125,25 +145,36 @@ simplemung(tree *t)
return t;
}
-tree*
-token(char *str, int type)
+char*
+fnstr(tree *t)
{
- tree *t = newtree();
+ io *f = openiostr();
+ pfmt(f, "%t", t);
+ return closeiostr(f);
+}
- t->type = type;
- t->str = strdup(str);
+tree*
+globprop(tree *t)
+{
+ tree *c0 = t->child[0];
+ tree *c1 = t->child[1];
+ if(t->glob==0){
+ if(c0->glob || c1->glob){
+ if(c0->glob)
+ c0->glob=-1;
+ if(c1->glob)
+ c1->glob=-1;
+ t->glob=1;
+ }
+ }
return t;
}
-void
-freetree(tree *p)
+tree*
+token(char *str, int type)
{
- if(p==0)
- return;
- freetree(p->child[0]);
- freetree(p->child[1]);
- freetree(p->child[2]);
- if(p->str)
- free(p->str);
- free(p);
+ tree *t = newtree();
+ t->str = estrdup(str);
+ t->type = type;
+ return t;
}