aboutsummaryrefslogtreecommitdiff
path: root/sys/cmd/rc/lex.c
blob: ec9e94d33f2ea3c7e0616062ad62bd9bb800738c (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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
#include "rc.h"
#include "parse.h"

static int advance(void);
// -----------------------------------------------------------------------
// lexer

struct Lexer
{
    int c[2];
    ushort doprompt;
    char buf[BUFSIZ];
};

static struct Lexer lexer = { .c={0, EOF}, .doprompt=1 };

void
yyerror(char *msg)
{
    print(errio, "\nrc: ");

    if(lexer.buf[0] && lexer.buf[0]!='\n')
        print(errio, "@ %q: ", lexer.buf);

    print(errio, "%s\n", msg);
    flush(errio);

    while(lexer.c[0] !='\n' && lexer.c[0] != EOF)
        advance();
}

int
readc(void)
{
    int c;
    static int peek = EOF;

    if(peek!=EOF){
        c = peek;
        peek = EOF;
        return c;
    }
    if(shell->flag.eof)
        return EOF;

    if(!prompt(&lexer.doprompt))
        exit(1); // XXX: hack for signal handling right now...

    c = get(shell->cmd.io);

    lexer.doprompt = lexer.doprompt || c=='\n' || c==EOF;

    if(c==EOF)
        shell->flag.eof = 1;

    return c;
}

static
int
peekc(void)
{
    if(lexer.c[1] == EOF)
        lexer.c[1] = readc();

    return lexer.c[1];
}

static
int
advance(void)
{
    int c = peekc();
    lexer.c[0] = lexer.c[1], lexer.c[1] = EOF;

    return c;
}

static
void
skipws(void)
{
    int c;
    for(;;){
        c = peekc();
        if(c== ' ' || c == '\t')
            advance();
        else
            return;
    }
}

static
void
skipnl(void)
{
    int c;
    for(;;){
        c = peekc();
        if(c== ' ' || c == '\t' || c == '\n')
            advance();
        else
            return;
    }
}

static
int
nextis(int c)
{
    if(peekc()==c){
        advance();
        return 1;
    }
    return 0;
}

static
char *
putbyte(char *buf, int c)
{
    if(!buf)
        return buf;

    if(buf == arrend(lexer.buf)){
        fatal("lexer: out of buffer space");
        return nil;
    }
    *buf++ = c;
    return buf;
}

#define	onebyte(c)	     ((c&0x80)==0x00)
#define	twobyte(c)	     ((c&0xe0)==0xc0)
#define	threebyte(c)	 ((c&0xf0)==0xe0)
#define	fourbyte(c)	     ((c&0xf8)==0xf0)

static
char *
putrune(char *buf, int c)
{
    buf = putbyte(buf, c);
    if(onebyte(c))
        return buf;
    if(twobyte(c))
        return putbyte(buf,c);
    if(threebyte(c)){
        buf = putbyte(buf,c);
        return putbyte(buf,c);
    }
    if(fourbyte(c)){
        buf = putbyte(buf,c);
        buf = putbyte(buf,c);
        return putbyte(buf,c);
    }
    fatal("malformed utf8 stream");

    return nil;
}

// -----------------------------------------------------------------------
// exported functions

int
iswordchar(int c)
{
    return !strchr("\n \t#;&|^$=`'{}()<>", c) && c!=EOF;
}

int
yylex(void)
{
    int  c;
    Tree *node;
    char *w = lexer.buf;

    yylval.tree = nil;

    skipws();
    switch(c=advance()){
    case EOF:
        return EOF;
    case '&':
        lexer.buf[0] = '&';
        lexer.buf[1] = 0;
        return '&';

    default:
        ;
    }
    if(!iswordchar(c))
        return c;

    for(;;){
        w = putrune(w, c);
        c = peekc();
        if(!iswordchar(c))
            break;
        advance();
    }
    w[0] = 0;

    node = token(Tword, lexer.buf);

    yylval.tree = node;
    return node->type;
}