-
Notifications
You must be signed in to change notification settings - Fork 0
/
command_stream.c
446 lines (379 loc) · 9.22 KB
/
command_stream.c
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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <error.h>
#include "token_stream.h"
#include "command_stream.h"
#define malloc_command ((command_t) malloc (sizeof (struct command)))
#define malloc_command_node ((command_node_t) malloc (sizeof (struct command_node)))
#define malloc_command_stream ((command_stream_t) malloc (sizeof (struct command_stream)))
#define istkn(tkn, tp) ((tkn) != NULL && (tkn)->type == tp)
#define isiotkn(t) (istkn (t, TKN_INPUT) || istkn (t, TKN_DUPIN) || istkn (t, TKN_IODUAL) || istkn (t, TKN_OUTPUT) || istkn (t, TKN_DUPOUT) || istkn (t, TKN_APPEND) || istkn (t, TKN_CLOBBER))
command_t
parse_command_sequence (token_stream_t strm);
command_t
create_command (enum command_type t)
{
command_t cmd = malloc_command;
cmd->type = t;
cmd->status = -1;
cmd->io_head = NULL;
cmd->io_tail = NULL;
cmd->io_count = 0;
if (t == CMD_SIMPLE)
cmd->u.word = NULL;
else if (t == CMD_SUBSHELL)
cmd->u.subshell_command = NULL;
else // the remaining command types
{
cmd->u.command[0] = NULL;
cmd->u.command[1] = NULL;
}
return cmd;
}
void
assert_token (token_stream_t strm, enum token_type t)
{
if (!istkn (current_token (strm), t))
{
char *tw;
switch (t)
{
case TKN_WORD: tw = "word"; break;
case TKN_IONUMBER: tw = "io number"; break;
case TKN_INPUT: tw = "'<'"; break;
case TKN_DUPIN: tw = "'<&'"; break;
case TKN_IODUAL: tw = "'<>'"; break;
case TKN_OUTPUT: tw = "'>'"; break;
case TKN_DUPOUT: tw = "'>&'"; break;
case TKN_APPEND: tw = "'>>'"; break;
case TKN_CLOBBER: tw = "'>|'"; break;
case TKN_PIPE: tw = "'|'"; break;
case TKN_OPENPAREN: tw = "'('"; break;
case TKN_CLOSEPAREN: tw = "')'"; break;
case TKN_AND: tw = "'&&'"; break;
case TKN_OR: tw = "'||'"; break;
case TKN_SEMICOLON: tw = "';'"; break;
case TKN_DBLSEMICLN: tw = "';;'"; break;
case TKN_EOF: tw = "eof"; break;
}
error (1, 0, "%d: expecting %s token\n", current_token (strm)->line, tw);
}
}
command_t
parse_simple_command (token_stream_t strm)
{
int c = 0;
int i = 0;
while (istkn (current_token (strm), TKN_WORD))
{
c++;
next_token (strm);
}
command_t cmd = create_command (CMD_SIMPLE);
cmd->u.word = (char**) malloc ((c + 1) * sizeof (char *));
backward_token_stream (strm, c);
while (istkn (current_token (strm), TKN_WORD))
{
cmd->u.word[i] = strdup (current_token (strm)->value);
i++;
next_token (strm);
}
cmd->u.word[i] = NULL;
return cmd;
}
command_t
parse_io_redirection (token_stream_t strm, command_t cmd)
{
io_node_t io;
io = (io_node_t) malloc (sizeof (struct io_node));
io->io_num = NULL;
io->word = NULL;
io->next = NULL;
token_t c = current_token (strm);
token_t n = next_token (strm);
if (istkn (c, TKN_IONUMBER))
{
io->io_num = strdup (c->value);
c = current_token (strm);
n = next_token (strm);
}
if (istkn (c, TKN_INPUT))
{
assert_token (strm, TKN_WORD);
io->op = IO_INPUT;
}
else if (istkn (c, TKN_DUPIN))
{
assert_token (strm, TKN_IONUMBER);
io->op = IO_DUPIN;
}
else if (istkn (c, TKN_IODUAL))
{
assert_token (strm, TKN_WORD);
io->op = IO_IODUAL;
}
else if (istkn (c, TKN_OUTPUT))
{
assert_token (strm, TKN_WORD);
io->op = IO_OUTPUT;
}
else if (istkn (c, TKN_DUPOUT))
{
assert_token (strm, TKN_IONUMBER);
io->op = IO_DUPOUT;
}
else if (istkn (c, TKN_APPEND))
{
assert_token (strm, TKN_WORD);
io->op = IO_APPEND;
}
else if (istkn (c, TKN_CLOBBER))
{
assert_token (strm, TKN_WORD);
io->op = IO_CLOBBER;
}
else if (io->io_num != NULL)
error (1, 0, "%d: expecting io token\n", c->line);
io->word = strdup (n->value);
if (cmd->io_head == NULL || cmd->io_tail == NULL)
{
cmd->io_head = io;
cmd->io_tail = io;
cmd->io_count++;
}
else
{
cmd->io_tail->next = io;
cmd->io_tail = io;
cmd->io_count++;
}
next_token (strm);
return cmd;
}
command_t
parse_subshell_command (token_stream_t strm)
{
assert_token (strm, TKN_OPENPAREN);
next_token (strm);
command_t cmd = create_command (CMD_SUBSHELL);
cmd->u.subshell_command = parse_command_sequence (strm);
assert_token (strm, TKN_CLOSEPAREN);
next_token (strm);
return cmd;
}
command_t
parse_command (token_stream_t strm)
{
command_t cmd;
token_t t = current_token (strm);
if (istkn (t, TKN_WORD))
cmd = parse_simple_command (strm);
else if (istkn (t, TKN_OPENPAREN))
cmd = parse_subshell_command (strm);
else
error (1, 0, "%d: expecting word or '(' token\n", t->line);
t = current_token (strm);
while (istkn (t, TKN_IONUMBER) || isiotkn (t))
{
cmd = parse_io_redirection (strm, cmd);
t = current_token (strm);
}
return cmd;
}
command_t
parse_pipelines (token_stream_t strm)
{
token_t t;
command_t lft;
command_t rgt;
command_t pipe;
lft = parse_command (strm);
t = current_token (strm);
while (istkn (t, TKN_PIPE))
{
next_token (strm);
pipe = create_command (CMD_PIPE);
rgt = parse_command (strm);
pipe->u.command[0] = lft;
pipe->u.command[1] = rgt;
lft = pipe;
t = current_token (strm);
}
return lft;
}
command_t
parse_logicals (token_stream_t strm)
{
token_t t;
command_t lft;
command_t rgt;
command_t lgcl;
lft = parse_pipelines (strm);
t = current_token (strm);
while (istkn (t, TKN_AND) || istkn (t, TKN_OR))
{
next_token (strm);
if (istkn (t, TKN_AND))
lgcl = create_command (CMD_AND);
else if (istkn (t, TKN_OR))
lgcl = create_command (CMD_OR);
rgt = parse_pipelines (strm);
lgcl->u.command[0] = lft;
lgcl->u.command[1] = rgt;
lft = lgcl;
t = current_token (strm);
}
return lft;
}
command_t
parse_command_sequence (token_stream_t strm)
{
command_t lft;
command_t rgt;
command_t seq;
lft = parse_logicals (strm);
while (istkn (current_token (strm), TKN_SEMICOLON))
{
next_token (strm);
rgt = parse_logicals (strm);
seq = create_command (CMD_SEQUENCE);
seq->u.command[0] = lft;
seq->u.command[1] = rgt;
lft = seq;
}
return lft;
}
command_node_t
add_command (command_stream_t strm, command_t cmd)
{
command_node_t node = malloc_command_node;
node->value = cmd;
node->prev = NULL;
node->next = NULL;
if (strm->head == NULL || strm->tail == NULL)
{
strm->head = node;
strm->tail = node;
strm->curr = node;
}
else
{
strm->tail->next = node;
node->prev = strm->tail;
strm->tail = node;
}
return node;
}
command_stream_t
parse (token_stream_t strm)
{
command_stream_t cmd_strm = malloc_command_stream;
cmd_strm->head = NULL;
cmd_strm->tail = NULL;
cmd_strm->curr = NULL;
while (!istkn (current_token (strm), TKN_EOF))
{
add_command (cmd_strm, parse_command_sequence (strm));
while (istkn (current_token (strm), TKN_DBLSEMICLN))
{
next_token (strm);
add_command (cmd_strm, parse_command_sequence (strm));
}
}
return cmd_strm;
}
command_stream_t
create_command_stream (int (*next_char) (void *), void *file)
{
token_stream_t tkn_strm = create_token_stream (next_char, file);
command_stream_t cmd_strm = parse (tkn_strm);
destroy_token_stream (tkn_strm);
return cmd_strm;
}
void
destroy_command (command_t cmd)
{
switch (cmd->type)
{
case CMD_SEQUENCE:
case CMD_AND:
case CMD_OR:
case CMD_PIPE:
{
destroy_command (cmd->u.command[0]);
destroy_command (cmd->u.command[1]);
break;
}
case CMD_SIMPLE:
{
char **w = cmd->u.word;
while (*w != NULL)
{
free (*w);
w++;
}
free (cmd->u.word);
break;
}
case CMD_SUBSHELL:
{
destroy_command (cmd->u.subshell_command);
break;
}
}
io_node_t c;
io_node_t n;
c = cmd->io_head;
while (c != NULL)
{
if (c->io_num)
free (c->io_num);
free (c->word);
n = c->next;
free (c);
c = n;
}
free (cmd);
}
void
destroy_command_stream (command_stream_t strm)
{
if (strm != NULL)
{
if (strm->head != NULL && strm->tail != NULL)
{
while (strm->tail != strm->head)
{
destroy_command (strm->tail->value);
strm->tail = strm->tail->prev;
free (strm->tail->next);
}
destroy_command (strm->tail->value);
free (strm->tail);
}
free (strm);
}
}
command_t
next_command (command_stream_t strm)
{
if (strm->curr != NULL)
{
strm->curr = strm->curr->next;
}
return current_command (strm);
}
command_t
current_command (command_stream_t strm)
{
return strm->curr != NULL ? strm->curr->value : NULL;
}
command_t
reset_command_stream (command_stream_t strm)
{
strm->curr = strm->head;
return current_command (strm);
}