aboutsummaryrefslogtreecommitdiff
path: root/sys/cmd/rc/rc.h
blob: ad46a7796a3abbefceafceaace733abb458f07d8 (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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
#pragma once

#include <u.h>
#include <libn.h>

#include <errno.h>
#include <sys/wait.h>

#define alloc(ptr)       ptr = emalloc(sizeof(*ptr))

// -----------------------------------------------------------------------
// main enums

#define GLOB 0x01

/* TODO: make sure there are no collisions */
enum
{
    /* keywords */
    Kstart=11,  Kfor, Kin, Kwhile, Kif, Kelse, 
    Kswitch, Kcase, Kfunc, Ktwiddle, 
    Kbang, Ksubsh, Kend,

    /* tokens */
    Aword='w', Aredir='r', Adup='d', Asimple='s', 
    Aargs='A', Awords='W', Abrace='b', Aparen='p', Asub='S',
    Apcmd='c', Apipefd='-', Aandand='%', Aoror='@', Acount='#', 

    Atick='`', Apipe = '|',  Adol='$', Aquote='"', Aand='&', 
    Alparen = '(', Arparen = ')', Albrace='{', Arbrace='}',
    Asemi=';', Acarot='^', Aeq='=',
};

enum
{
    Rappend = 1,
    Rwrite  = 2,
    Rread   = 3,
    Rhere   = 4,
    Rdupfd  = 5,
    Rclose  = 6,
    Rrdwr   = 7,
};

// -----------------------------------------------------------------------
// main types

typedef union  Code    Code;
typedef struct Word    Word;
typedef struct List    List;
typedef struct Var     Var;
typedef struct Tree    Tree;
typedef struct Builtin Builtin;
typedef struct Thread  Thread;
typedef struct Io      Io;

union Code 
{
    int    i;
    char  *s;
    void (*f)(void);
};

struct Word
{
    char *word;
    Word *link;
};

struct List
{
    Word *words;
    List *link;
};

struct Var
{
    string name;
    Word   *val;
    Code   *func;
    Var    *link;
};

struct Tree
{
    ushort type;
    uchar  quoted : 1;
    union {
        char  *str;
        struct {
            ushort type;
            int fd[2];
        } redir;
    };

    Tree *child[3], *link;
};

struct Builtin
{
    char  *cmd;
    void (*func)(void);
};

struct Thread
{
    Code   *code, *ip;
    List   *stack;
    Var    *local;
    struct {
        uchar eof : 1;
        int   line;
        char *name;
        Io   *io;
    } cmd;

    int     pid;
    Tree   *nodes;
    Thread *link; /* continuation */
};

struct Io
{
    int   fd;
    uchar *b, *e, buf[];
};

// -----------------------------------------------------------------------
// global interpreter variables

extern Thread *shell;
extern int     ntrap;
extern int     status;

extern Io     *errio;

extern Builtin builtins[];
extern Var    *globals[1021]; /* for now must be prime */

// -----------------------------------------------------------------------
// interpreter functions (defined in exec.c)

/* 
 * notation: 
 * (var1, var2, ...) : items from stack
 * [var1, var2, ...] : items from code stream
 *      -> moves value
 */
extern void Xmark(void);      /* Xmark: delimit stack with new list */
extern void Xword(void);      /* Xword[val] -> (val) */
extern void Xassign(void);    /* Xassign(name, val): assign name to val */
extern void Xdol(void);       /* Xdol(name): get variable value */
extern void Xsimple(void);    /* Xsimple(args): run command */
extern void Xexit(void);      /* Xexit: exit with status */
extern void Xerror(char *s);  /* Xerror: report an error */
extern void Xparse(void);

// -----------------------------------------------------------------------
// shell functions

/* 
 * util.c 
 */
void *emalloc(uintptr size);
void *erealloc(void *ptr, uintptr size);
void  efree(void *);
void  panic(char *msg, int n);

/* 
 * io.c 
 */
Io   *openfd(int fd);
Io   *openstr(void);
Io   *opencore(int len, char *s);
void  rewindio(Io *f);
void  closeio(Io *f);
void  flush(Io **fp);

/* reads */
int   rchr(Io *f);

/* writes */
int   pchr(Io *f, int c);
void  pquo(Io *f, char *s);
void  pwrd(Io *f, char *s);
void  pptr(Io *f, void *v);
void  pstr(Io *f, char *s);
void  pdec(Io *f, int n);
void  poct(Io *f, uint n);
void  pval(Io *f, Word *a);
void  pcmd(Io *f, Tree *t);
void  pfmt(Io *f, char *fmt, ...);
void  vpfmt(Io *f, char *fmt, va_list args); 

/* 
 * word.c 
 */
void pushlist(void);
void freelist(Word *w);
void poplist(void);

int   count(Word *w);
Word *newword(char *w, Word *link);
void  pushword(char *w);

/* 
 * tree.c 
 */

Tree *newtree(void);
void  freetree(Tree *t);
Tree *tree3(int type, Tree *c0, Tree *c1, Tree *c2);
Tree *tree2(int type, Tree *c0, Tree *c1);
Tree *tree1(int type, Tree *c0);

Tree *hang1(Tree *p, Tree *c0);
Tree *hang2(Tree *p, Tree *c0, Tree *c1);
Tree *hang3(Tree *p, Tree *c0, Tree *c1, Tree *c2);
Tree *epihang(Tree *c, Tree *epi);
Tree *simplehang(Tree *t);
Tree *wordnode(char *w);

/*
 * var.c
 */

Var  *newvar(char *name, Var *link);
Var  *gvlookup(char *name);
Var  *vlookup(char *name);

int  kwlookup(char *name);
void initkw(void);

/* 
 * lex.c 
 */

void skipnl(void);
int  wordchr(char c);

void rcerror(char *msg, ...);
int  lex(Tree **node);

/*
 * parse.c
 */

int parse(void);

/*
 * main.c
 */

void dotrap(void);