aboutsummaryrefslogtreecommitdiff
path: root/sys/cmd/rc/word.c
blob: 84ff40cf9c12214e61d845722188829233a0462b (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
#include "rc.h"

void
pushlist(void)
{
    List *ls;

    alloc(ls);
    ls->words = nil;
    ls->link  = shell->stack, shell->stack = ls; 
}

void
freelist(Word *w)
{
    Word *it;
    while (w) {
        it = w->link;
        efree(w->word);
        efree(w);
        w  = it;
    }
}

void
poplist(void)
{
    List *ls = shell->stack;
    if (!ls) 
        panicf("shell stack underflow");

    freelist(ls->words);
    shell->stack = ls->link;
    efree(ls);
}

int
count(Word *w)
{
    int n;
    for (n=0; w; n++)
        w = w->link;
    return n;
}

Word*
newword(char *w, Word *link)
{
    Word *wd;
    
    alloc(wd);
    wd->word = strdup(w);
    wd->link = link;

    return wd;
}

void
pushword(char *w)
{
    if (shell->stack == nil)
        panicf("no active stack");
    shell->stack->words = newword(w, shell->stack->words);
}