aboutsummaryrefslogtreecommitdiff
path: root/sys/cmd/rc/tree.c
blob: 14049e5b97341fb5c5de21dd8b5e1e34663f689b (plain)
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
#include "rc.h"

// -----------------------------------------------------------------------
// globals

static Tree *nodes;

// -----------------------------------------------------------------------
// exported funcs

Tree*
newtree(void)
{
    Tree *t;

    alloc(t);
    t->str = nil;
    t->child[0] = t->child[1] = t->child[2] = nil;
    t->redir.fd[0] = t->redir.fd[1] = t->redir.type = 0;

    t->link = nodes, nodes = t;
    return t;
}

void
freetree(Tree *t)
{
    if (!t)
        return;

    freetree(t->child[0]);
    freetree(t->child[1]);
    freetree(t->child[2]);

    if (t->str)
        efree(t->str);
    efree(t);
}

void
freenodes(void)
{
    Tree *t, *u;

    for (t = nodes;t;t = u) {
        u = t->link;
        if (t->str)
            efree(t->str);
        efree(t);
    }
    nodes = nil;
}

/* tree creation */
Tree*
tree3(int type, Tree *c0, Tree *c1, Tree *c2)
{
    Tree *t;

    t = newtree();
    t->type = type;
    t->child[0] = c0;
    t->child[1] = c1;
    t->child[2] = c2;

    return t;
}

Tree*
tree2(int type, Tree *c0, Tree *c1)
{
	return tree3(type, c0, c1, nil);
}

Tree*
tree1(int type, Tree *c0)
{
	return tree3(type, c0, nil, nil);
}

/* tree hang */
Tree*
hang1(Tree *p, Tree *c0)
{
    p->child[0] = c0;
    return p;
}

Tree*
hang2(Tree *p, Tree *c0, Tree *c1)
{
    p->child[0] = c0;
    p->child[1] = c1;
    return p;
}

Tree*
hang3(Tree *p, Tree *c0, Tree *c1, Tree *c2)
{
    p->child[0] = c0;
    p->child[1] = c1;
    p->child[2] = c2;
    return p;
}

/* hangs the cmd underneath the epilogue */
Tree*
epihang(Tree *c, Tree *epi)
{
    Tree *p;
    if(!epi)
        return c;
    for(p=epi;p->child[1];p = p->child[1])
        ;
    p->child[1] = c;
    return epi;
}

/* hangs tree t from a new simple node. percolates redirections to root */
Tree*
simplehang(Tree *t)
{
    Tree *u;
    t = tree1(Tsimple, t);
    for(u = t->child[0];u->type==Targs;u=u->child[0]) {
        if (u->child[1]->type==Tdup
        ||  u->child[1]->type==Tredir){
            u->child[1]->child[1] = t;
            t = u->child[1];
            u->child[1] = nil;
        }
    }
    return t;
}

Tree*
wordnode(char *w)
{
    Tree *t = newtree();
    t->type = Tword;
    t->str  = strdup(w);

    return t;
}