1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
|
#include "rc.h"
#include "io.h"
#include "fns.h"
/*
* create and clear a new tree node, and add it
* to the node list.
*/
static tree *treefree, *treenodes;
tree*
newtree(void)
{
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;
treenodes = t;
return t;
}
void
freenodes(void)
{
tree *t;
t = treenodes;
while(t){
if(t->str){
free(t->str);
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;
}
tree*
tree1(int type, tree *c0)
{
return tree3(type, c0, (tree *)0, (tree *)0);
}
tree*
tree2(int type, tree *c0, tree *c1)
{
return tree3(type, c0, c1, (tree *)0);
}
tree*
tree3(int type, tree *c0, tree *c1, tree *c2)
{
tree *t;
if(type==';'){
if(c0==0)
return c1;
if(c1==0)
return c0;
}
t = newtree();
t->type = type;
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;
}
tree*
mung1(tree *t, tree *c0)
{
t->child[0] = c0;
return t;
}
tree*
mung2(tree *t, tree *c0, tree *c1)
{
t->child[0] = c0;
t->child[1] = c1;
return t;
}
tree*
mung3(tree *t, tree *c0, tree *c1, tree *c2)
{
t->child[0] = c0;
t->child[1] = c1;
t->child[2] = c2;
return t;
}
tree*
epimung(tree *comp, tree *epi)
{
tree *p;
if(epi==0)
return comp;
for(p = epi;p->child[1];p = p->child[1]);
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;
t = tree1(SIMPLE, t);
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){
u->child[1]->child[1] = t;
t = u->child[1];
u->child[1] = 0;
}
}
return t;
}
char*
fnstr(tree *t)
{
io *f = openiostr();
pfmt(f, "%t", t);
return closeiostr(f);
}
tree*
globprop(tree *t)
{
tree *c0 = t->child[0];
tree *c1 = t->child[1];
if(c1==0){
while(c0 && c0->type==WORDS){
c1 = c0->child[1];
if(c1 && c1->glob){
c1->glob=2;
t->glob=1;
}
c0 = c0->child[0];
}
} else {
if(c0->glob){
c0->glob=2;
t->glob=1;
}
if(c1->glob){
c1->glob=2;
t->glob=1;
}
}
return t;
}
tree*
token(char *str, int type)
{
tree *t = newtree();
t->str = estrdup(str);
t->type = type;
return t;
}
|