-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbashMain.c
293 lines (239 loc) · 7.71 KB
/
bashMain.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
// mainBash.c Stan Eisenstat (11/11/17)
//
// Prompts for commands, expands environment variables, parses them into
// command structures, and then executes the commands as per specification.
//
// Bash version based on expression tree
// Dumps token list or CMD tree if DUMP_LIST or DUMP_TREE is set.
#include "process.h"
int main()
{
int nCmd = 1; // Command number
char *line = NULL; // Space for line read
token *list; // Linked list of tokens
CMD *cmd; // Parsed command
setenv ("?", "0", 1); // Initial status
setvbuf (stdin, NULL, _IONBF, 1); // Disable buffering of stdin
size_t nLine = 0; // #chars allocated
for ( ; ; ) {
printf ("(%d)$ ", nCmd); // Prompt for command
fflush (stdout);
if (getline (&line,&nLine, stdin) <= 0) // Read line
break; // Break on end of file
list = tokenize (line); // Lex line into tokens
if (list == NULL)
continue;
else if (getenv ("DUMP_LIST")) // Dump token list only if
dumpList (list); // environment variable set
cmd = parse (list); // Parsed command
freeList (list); // Free token list
if (cmd == NULL)
continue;
else if (getenv ("DUMP_TREE")) { // Dump command tree if
dumpTree (cmd, 0); // environment variable set
printf ("\n");
fflush (stdout);
}
process (cmd); // Execute command
if (getenv ("DUMP_TREE_AGAIN")) { // Dump command tree again if
dumpTree (cmd, 0); // environment variable set
printf ("\n");
fflush (stdout);
}
freeCMD (cmd); // Free CMD tree
nCmd++; // Adjust prompt
}
free (line);
return EXIT_SUCCESS;
}
// Print list of tokens LIST
void dumpList (struct token *list)
{
struct token *p;
for (p = list; p != NULL; p = p->next) // Walk down linked list
printf ("%s:%d ", p->text, p->type); // printing token and type
putchar ('\n'); // Terminate line
}
// Free list of tokens LIST
void freeList (token *list)
{
token *p, *pnext;
for (p = list; p; p = pnext) {
pnext = p->next; p->next = NULL; // Zap p->next and p->text
free(p->text); p->text = NULL; // to stop accidental reuse
free(p);
}
}
// Allocate, initialize, and return a pointer to an empty command structure
CMD *mallocCMD (void)
{
CMD *new = malloc(sizeof(*new));
new->type = NONE;
new->argc = 0;
new->argv = malloc (sizeof(char *));
new->argv[0] = NULL;
new->nLocal = 0;
new->locVar = NULL;
new->locVal = NULL;
new->fromType = NONE;
new->fromFile = NULL;
new->toType = NONE;
new->toFile = NULL;
new->errType = NONE;
new->errFile = NULL;
new->left = NULL;
new->right = NULL;
return new;
}
// Free tree of commands rooted at *C
void freeCMD (CMD *c)
{
if (!c)
return;
for (int i = 0; i < c->nLocal; i++) {
free (c->locVar[i]);
free (c->locVal[i]);
}
free (c->locVar);
free (c->locVal);
for (char **p = c->argv; *p; p++)
free (*p);
free (c->argv);
free (c->fromFile);
free (c->toFile);
free (c->errFile);
freeCMD (c->left);
freeCMD (c->right);
free (c);
}
///////////////////////////////////////////////////////////////////////////////
// Dump CMD structure in tree format
// Print arguments in command data structure rooted at *C
void dumpArgs (CMD *c)
{
if (c->argc < 0)
fprintf (stdout, " ARGC < 0");
else if (c->argv == NULL)
fprintf (stdout, " ARGV = NULL");
else if (c->argv[c->argc] != NULL)
fprintf (stdout, " ARGV[ARGC] != NULL");
else {
//// fprintf (stdout, ", argc = %d", c->argc);
for (char **q = c->argv; *q; q++)
fprintf (stdout, ", argv[%ld] = %s", q-(c->argv), *q);
}
}
// Print input/output redirections and local variables in command data
// structure rooted at *C
void dumpRedirect (CMD *c)
{
if (c->fromType == NONE && c->fromFile == NULL)
;
else if (c->fromType == RED_IN && c->fromFile != NULL)
fprintf (stdout, " <%s", c->fromFile);
else if (c->fromType == RED_IN_HERE && c->fromFile != NULL)
fprintf (stdout, " <<HERE");
else
fprintf (stdout, " ILLEGAL INPUT REDIRECTION");
if (c->toType == NONE && c->toFile == NULL)
;
else if (c->toType == RED_OUT && c->toFile != NULL)
fprintf (stdout, " >%s", c->toFile);
else if (c->toType == RED_OUT_APP && c->toFile != NULL)
fprintf (stdout, " >>%s", c->toFile);
else if (c->toType == RED_OUT_ERR && c->toFile != NULL)
fprintf (stdout, " &>%s", c->toFile);
else
fprintf (stdout, " ILLEGAL OUTPUT REDIRECTION");
if (c->errType == NONE && c->errFile == NULL)
;
else if (c->errType == RED_ERR && c->errFile != NULL)
fprintf (stdout, " 2>%s", c->errFile);
else if (c->errType == RED_ERR_APP && c->errFile != NULL)
fprintf (stdout, " 2>>%s", c->errFile);
else if (c->errType == RED_OUT_ERR && c->errFile == NULL)
fprintf (stdout, " &>%s", c->toFile);
else
fprintf (stdout, " ILLEGAL ERROR REDIRECTION");
if (c->nLocal < 0) {
fprintf (stdout, " INVALID NLOCAL");
} else if (c->nLocal == 0) {
;
} else if (c->locVar == NULL || c->locVal == NULL) {
fprintf (stdout, " INVALID LOCVAL or LOCVAR");
} else {
fprintf (stdout, "\n LOCAL: ");
for (int i = 0; i < c->nLocal; i++)
if (strchr (c->locVal[i], '='))
fprintf (stdout, "%s = %s, ", c->locVar[i], c->locVal[i]);
else
fprintf (stdout, "%s=%s, ", c->locVar[i], c->locVal[i]);
}
if (c->fromType == RED_IN_HERE) {
if (c->fromFile == NULL) {
fprintf (stdout, " INVALID FROMFILE FOR RED_IN_HERE");
} else {
fprintf (stdout, "\n HERE: ");
for (char *s = c->fromFile; *s; s++) {
if (*s != '\n')
fputc (*s, stdout);
else if (s[1])
fprintf (stdout, "\n HERE: ");
else
fprintf (stdout, "<newline>");
}
}
}
}
// Print in in-order command data structure rooted at *C at depth LEVEL
void dumpTree (CMD *c, int level)
{
if (!c)
return;
dumpTree (c->left, level+1);
////fprintf (stdout, "CMD (Level = %d): ", level);
fprintf (stdout, "CMD (Depth = %d): ", level);
if (c->type == SIMPLE) {
if (c->left != NULL)
fprintf (stdout, " SIMPLE HAS LEFT CHILD");
else if (c->right != NULL)
fprintf (stdout, " SIMPLE HAS RIGHT CHILD");
else {
fprintf (stdout, "SIMPLE");
dumpArgs (c);
dumpRedirect (c);
}
} else if (c->argc > 0) {
fprintf (stdout, " NON-SIMPLE HAS ARGUMENTS");
} else if (c->type == SUBCMD) {
if (c->right != NULL)
fprintf (stdout, " SUBCMD HAS RIGHT CHILD");
else {
fprintf (stdout, "SUBCMD");
dumpRedirect (c);
}
} else if (c->fromType != NONE
|| c->fromFile != NULL
|| c->toType != NONE
|| c->toFile != NULL
|| c->errType != NONE
|| c->errFile != NULL) {
fprintf (stdout, " NON-SIMPLE, NON-SUBCMD HAS I/O REDIRECTION");
} else if (c->nLocal > 0) {
fprintf (stdout, " NON-SIMPLE, NON-SUBCMD HAS LOCAL VARIABLES");
} else if (c->type == PIPE) {
fprintf (stdout, "PIPE");
} else if (c->type == SEP_AND) {
fprintf (stdout, "SEP_AND");
} else if (c->type == SEP_OR) {
fprintf (stdout, "SEP_OR");
} else if (c->type == SEP_END) {
fprintf (stdout, "SEP_END");
} else if (c->type == SEP_BG) {
fprintf (stdout, "SEP_BG");
} else {
fprintf (stdout, "NODE HAS INVALID TYPE");
}
fprintf (stdout, "\n");
dumpTree (c->right, level+1);
}