xref: /inferno-os/utils/rcsh/tree.c (revision 74a4d8c26dd3c1e9febcb717cfd6cb6512991a7a)
1*74a4d8c2SCharles.Forsyth #include "rc.h"
2*74a4d8c2SCharles.Forsyth #include "y.tab.h"
3*74a4d8c2SCharles.Forsyth 
4*74a4d8c2SCharles.Forsyth Tree *Treenodes;
5*74a4d8c2SCharles.Forsyth 
6*74a4d8c2SCharles.Forsyth /*
7*74a4d8c2SCharles.Forsyth  * create and clear a new Tree node, and add it
8*74a4d8c2SCharles.Forsyth  * to the node list.
9*74a4d8c2SCharles.Forsyth  */
10*74a4d8c2SCharles.Forsyth Tree *
newtree(void)11*74a4d8c2SCharles.Forsyth newtree(void)
12*74a4d8c2SCharles.Forsyth {
13*74a4d8c2SCharles.Forsyth 	Tree *t=new(Tree);
14*74a4d8c2SCharles.Forsyth 	t->iskw=0;
15*74a4d8c2SCharles.Forsyth 	t->str=0;
16*74a4d8c2SCharles.Forsyth 	t->child[0]=t->child[1]=t->child[2]=0;
17*74a4d8c2SCharles.Forsyth 	t->next=Treenodes;
18*74a4d8c2SCharles.Forsyth 	Treenodes=t;
19*74a4d8c2SCharles.Forsyth 	return t;
20*74a4d8c2SCharles.Forsyth }
21*74a4d8c2SCharles.Forsyth 
22*74a4d8c2SCharles.Forsyth void
freenodes(void)23*74a4d8c2SCharles.Forsyth freenodes(void)
24*74a4d8c2SCharles.Forsyth {
25*74a4d8c2SCharles.Forsyth 	Tree *t, *u;
26*74a4d8c2SCharles.Forsyth 	for(t=Treenodes;t;t=u){
27*74a4d8c2SCharles.Forsyth 		u=t->next;
28*74a4d8c2SCharles.Forsyth 		if(t->str)
29*74a4d8c2SCharles.Forsyth 			free(t->str);
30*74a4d8c2SCharles.Forsyth 		free(t);
31*74a4d8c2SCharles.Forsyth 	}
32*74a4d8c2SCharles.Forsyth 	Treenodes=0;
33*74a4d8c2SCharles.Forsyth }
34*74a4d8c2SCharles.Forsyth 
35*74a4d8c2SCharles.Forsyth Tree *
tree1(int type,Tree * c0)36*74a4d8c2SCharles.Forsyth tree1(int type, Tree *c0)
37*74a4d8c2SCharles.Forsyth {
38*74a4d8c2SCharles.Forsyth 	return tree3(type, c0, 0, 0);
39*74a4d8c2SCharles.Forsyth }
40*74a4d8c2SCharles.Forsyth 
41*74a4d8c2SCharles.Forsyth Tree *
tree2(int type,Tree * c0,Tree * c1)42*74a4d8c2SCharles.Forsyth tree2(int type, Tree *c0, Tree *c1)
43*74a4d8c2SCharles.Forsyth {
44*74a4d8c2SCharles.Forsyth 	return tree3(type, c0, c1, 0);
45*74a4d8c2SCharles.Forsyth }
46*74a4d8c2SCharles.Forsyth 
47*74a4d8c2SCharles.Forsyth Tree *
tree3(int type,Tree * c0,Tree * c1,Tree * c2)48*74a4d8c2SCharles.Forsyth tree3(int type, Tree *c0, Tree *c1, Tree *c2)
49*74a4d8c2SCharles.Forsyth {
50*74a4d8c2SCharles.Forsyth 	Tree *t;
51*74a4d8c2SCharles.Forsyth 	if(type==';'){
52*74a4d8c2SCharles.Forsyth 		if(c0==0) return c1;
53*74a4d8c2SCharles.Forsyth 		if(c1==0) return c0;
54*74a4d8c2SCharles.Forsyth 	}
55*74a4d8c2SCharles.Forsyth 	t=newtree();
56*74a4d8c2SCharles.Forsyth 	t->type=type;
57*74a4d8c2SCharles.Forsyth 	t->child[0]=c0;
58*74a4d8c2SCharles.Forsyth 	t->child[1]=c1;
59*74a4d8c2SCharles.Forsyth 	t->child[2]=c2;
60*74a4d8c2SCharles.Forsyth 	return t;
61*74a4d8c2SCharles.Forsyth }
62*74a4d8c2SCharles.Forsyth 
63*74a4d8c2SCharles.Forsyth Tree *
mung1(Tree * t,Tree * c0)64*74a4d8c2SCharles.Forsyth mung1(Tree *t, Tree *c0)
65*74a4d8c2SCharles.Forsyth {
66*74a4d8c2SCharles.Forsyth 	t->child[0]=c0;
67*74a4d8c2SCharles.Forsyth 	return t;
68*74a4d8c2SCharles.Forsyth }
69*74a4d8c2SCharles.Forsyth 
70*74a4d8c2SCharles.Forsyth Tree *
mung2(Tree * t,Tree * c0,Tree * c1)71*74a4d8c2SCharles.Forsyth mung2(Tree *t, Tree *c0, Tree *c1)
72*74a4d8c2SCharles.Forsyth {
73*74a4d8c2SCharles.Forsyth 	t->child[0]=c0;
74*74a4d8c2SCharles.Forsyth 	t->child[1]=c1;
75*74a4d8c2SCharles.Forsyth 	return t;
76*74a4d8c2SCharles.Forsyth }
77*74a4d8c2SCharles.Forsyth 
78*74a4d8c2SCharles.Forsyth Tree *
mung3(Tree * t,Tree * c0,Tree * c1,Tree * c2)79*74a4d8c2SCharles.Forsyth mung3(Tree *t, Tree *c0, Tree *c1, Tree *c2)
80*74a4d8c2SCharles.Forsyth {
81*74a4d8c2SCharles.Forsyth 	t->child[0]=c0;
82*74a4d8c2SCharles.Forsyth 	t->child[1]=c1;
83*74a4d8c2SCharles.Forsyth 	t->child[2]=c2;
84*74a4d8c2SCharles.Forsyth 	return t;
85*74a4d8c2SCharles.Forsyth }
86*74a4d8c2SCharles.Forsyth 
87*74a4d8c2SCharles.Forsyth Tree *
epimung(Tree * comp,Tree * epi)88*74a4d8c2SCharles.Forsyth epimung(Tree *comp, Tree *epi)
89*74a4d8c2SCharles.Forsyth {
90*74a4d8c2SCharles.Forsyth 	Tree *p;
91*74a4d8c2SCharles.Forsyth 	if(epi==0) return comp;
92*74a4d8c2SCharles.Forsyth 	for(p=epi;p->child[1];p=p->child[1]);
93*74a4d8c2SCharles.Forsyth 	p->child[1]=comp;
94*74a4d8c2SCharles.Forsyth 	return epi;
95*74a4d8c2SCharles.Forsyth }
96*74a4d8c2SCharles.Forsyth 
97*74a4d8c2SCharles.Forsyth /*
98*74a4d8c2SCharles.Forsyth  * Add a SIMPLE node at the root of t and percolate all the redirections
99*74a4d8c2SCharles.Forsyth  * up to the root.
100*74a4d8c2SCharles.Forsyth  */
101*74a4d8c2SCharles.Forsyth Tree *
simplemung(Tree * t)102*74a4d8c2SCharles.Forsyth simplemung(Tree *t)
103*74a4d8c2SCharles.Forsyth {
104*74a4d8c2SCharles.Forsyth 	Tree *u;
105*74a4d8c2SCharles.Forsyth 	Io *s;
106*74a4d8c2SCharles.Forsyth 	t=tree1(SIMPLE, t);
107*74a4d8c2SCharles.Forsyth 	s=openstr();
108*74a4d8c2SCharles.Forsyth 	pfmt(s, "%t", t);
109*74a4d8c2SCharles.Forsyth 	t->str=strdup(s->strp);
110*74a4d8c2SCharles.Forsyth 	closeio(s);
111*74a4d8c2SCharles.Forsyth 	for(u=t->child[0];u->type==ARGLIST;u=u->child[0]){
112*74a4d8c2SCharles.Forsyth 		if(u->child[1]->type==DUP
113*74a4d8c2SCharles.Forsyth 		|| u->child[1]->type==REDIR){
114*74a4d8c2SCharles.Forsyth 			u->child[1]->child[1]=t;
115*74a4d8c2SCharles.Forsyth 			t=u->child[1];
116*74a4d8c2SCharles.Forsyth 			u->child[1]=0;
117*74a4d8c2SCharles.Forsyth 		}
118*74a4d8c2SCharles.Forsyth 	}
119*74a4d8c2SCharles.Forsyth 	return t;
120*74a4d8c2SCharles.Forsyth }
121*74a4d8c2SCharles.Forsyth 
122*74a4d8c2SCharles.Forsyth Tree *
token(char * str,int type)123*74a4d8c2SCharles.Forsyth token(char *str, int type)
124*74a4d8c2SCharles.Forsyth {
125*74a4d8c2SCharles.Forsyth 	Tree *t=newtree();
126*74a4d8c2SCharles.Forsyth 	t->type=type;
127*74a4d8c2SCharles.Forsyth 	t->str=strdup(str);
128*74a4d8c2SCharles.Forsyth 	return t;
129*74a4d8c2SCharles.Forsyth }
130*74a4d8c2SCharles.Forsyth 
131*74a4d8c2SCharles.Forsyth void
freetree(Tree * p)132*74a4d8c2SCharles.Forsyth freetree(Tree *p)
133*74a4d8c2SCharles.Forsyth {
134*74a4d8c2SCharles.Forsyth 	if(p==0) return;
135*74a4d8c2SCharles.Forsyth 	freetree(p->child[0]);
136*74a4d8c2SCharles.Forsyth 	freetree(p->child[1]);
137*74a4d8c2SCharles.Forsyth 	freetree(p->child[2]);
138*74a4d8c2SCharles.Forsyth 	if(p->str) free(p->str);
139*74a4d8c2SCharles.Forsyth 	free((char *)p);
140*74a4d8c2SCharles.Forsyth }
141