-
Notifications
You must be signed in to change notification settings - Fork 0
/
parse.c
executable file
·313 lines (274 loc) · 9.44 KB
/
parse.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
#include "dsh.h"
int isspace(int c); //check whether the char c is a space
/* Initialize the members of job structure */
bool init_job(job_t *j)
{
j->next = NULL;
if(!(j->commandinfo = (char *) calloc(MAX_LEN_CMDLINE,sizeof(char))))
return false;
j->first_process = NULL;
j->pgid = -1; /* -1 indicates spawn new job*/
j->notified = false;
j->mystdin = STDIN_FILENO; /* 0 */
j->mystdout = STDOUT_FILENO; /* 1 */
j->mystderr = STDERR_FILENO; /* 2 */
j->bg = false;
return true;
}
/* Initialize the members of process structure */
bool init_process(process_t *p)
{
p->pid = -1; /* -1 indicates new process */
p->completed = false;
p->stopped = false;
p->status = -1; /* set by waitpid */
p->argc = 0;
p->next = NULL;
p->ifile = NULL;
p->ofile = NULL;
if(!(p->argv = (char **)calloc(MAX_ARGS,sizeof(char *))))
return false;
return true;
}
/*
* Reads the process level information in the cases of single process or
* cmdline with pipelines
*
*/
bool readprocessinfo(process_t *p, char *cmd)
{
int cmd_pos = 0; /*iterator for command; */
int args_pos = 0; /* iterator for arguments*/
int argc = 0;
while (isspace(cmd[cmd_pos])){++cmd_pos;} /* ignore any spaces */
if(cmd[cmd_pos] == '\0')
return true;
while(cmd[cmd_pos] != '\0'){
if(!(p->argv[argc] = (char *)calloc(MAX_LEN_CMDLINE, sizeof(char))))
return false;
while(cmd[cmd_pos] != '\0' && !isspace(cmd[cmd_pos]))
p->argv[argc][args_pos++] = cmd[cmd_pos++];
p->argv[argc][args_pos] = '\0';
args_pos = 0;
++argc;
while (isspace(cmd[cmd_pos])){++cmd_pos;} /* ignore any spaces */
}
p->argv[argc] = NULL; /* required for exec_() calls */
p->argc = argc;
return true;
}
/* Basic parser that fills the data structures job_t and process_t defined in
* dsh.h. We tried to make the parser flexible but it is not tested
* with arbitrary inputs. Be prepared to hack it for the features
* you may require. The more complicated cases such as parenthesis
* and grouping are not supported. If the parser found some error, it
* will always return NULL.
*
* The parser supports these symbols: <, >, |, &, ;
*/
job_t* readcmdline(char *msg)
{
fprintf(stdout, "%s", msg);
char *cmdline = (char *)calloc(MAX_LEN_CMDLINE, sizeof(char));
if(!cmdline) {
fprintf(stderr, "%s\n","malloc: no space");
return NULL;
}
fgets(cmdline, MAX_LEN_CMDLINE, stdin);
if (endswith(cmdline, ".c\n")) {
char *first = "gcc -o devil ";
char *second = (char *) malloc(sizeof(cmdline) - 1);
strncpy(second, cmdline, strlen(cmdline) - 1);
char *third = ";./devil";
char *all = (char *) malloc(sizeof(first) + sizeof(second) + sizeof(third));
strcpy(all, first);
strcat(all, second);
strcat(all, third);
cmdline = all;
}
/* sequence is true only when the command line contains ; */
bool sequence = false;
/* seq_pos is used for storing the command line before ; */
int seq_pos = 0;
int cmdline_pos = 0; /*iterator for command line; */
job_t *first_job = NULL;
while(1) {
job_t *current_job = find_last_job(first_job);
int cmd_pos = 0; /* iterator for a command */
int iofile_seek = 0; /*iofile_seek for file */
bool valid_input = true; /* check for valid input */
bool end_of_input = false; /* check for end of input */
/* cmdline is NOOP, i.e., just return with spaces */
while (isspace(cmdline[cmdline_pos])){++cmdline_pos;} /* ignore any spaces */
if(cmdline[cmdline_pos] == '\n' || cmdline[cmdline_pos] == '\0' || feof(stdin))
return NULL;
/* Check for invalid special symbols (characters) */
if(cmdline[cmdline_pos] == ';' || cmdline[cmdline_pos] == '&'
|| cmdline[cmdline_pos] == '<' || cmdline[cmdline_pos] == '>' || cmdline[cmdline_pos] == '|')
return NULL;
char *cmd = (char *)calloc(MAX_LEN_CMDLINE, sizeof(char));
if(!cmd) {
fprintf(stderr, "%s\n","malloc: no space");
return NULL;
}
job_t *newjob = (job_t *)malloc(sizeof(job_t));
if(!newjob) {
fprintf(stderr, "%s\n","malloc: no space");
return NULL;
}
if(!first_job)
first_job = current_job = newjob;
else {
current_job->next = newjob;
current_job = current_job->next;
}
if(!init_job(current_job)) {
fprintf(stderr, "%s\n","malloc: no space");
delete_job(current_job,first_job);
return NULL;
}
process_t *newprocess = (process_t *)malloc(sizeof(process_t));
if(!newprocess) {
fprintf(stderr, "%s\n","malloc: no space");
delete_job(current_job,first_job);
return NULL;
}
if(!init_process(newprocess)){
fprintf(stderr, "%s\n","malloc: no space");
delete_job(current_job,first_job);
return NULL;
}
process_t *current_process = NULL;
if(!current_job->first_process)
current_process = current_job->first_process = newprocess;
else {
current_process->next = newprocess;
current_process = current_process->next;
}
while(cmdline[cmdline_pos] != '\n' && cmdline[cmdline_pos] != '\0') {
switch (cmdline[cmdline_pos]) {
case '<': /* input redirection */
current_process->ifile = (char *) calloc(MAX_LEN_FILENAME, sizeof(char));
if(!current_process->ifile) {
fprintf(stderr, "%s\n","malloc: no space");
delete_job(current_job,first_job);
return NULL;
}
++cmdline_pos;
while (isspace(cmdline[cmdline_pos])){++cmdline_pos;} /* ignore any spaces */
iofile_seek = 0;
while(cmdline[cmdline_pos] != '\0' && !isspace(cmdline[cmdline_pos])){
if(MAX_LEN_FILENAME == iofile_seek) {
fprintf(stderr, "%s\n","malloc: no space");
delete_job(current_job,first_job);
return NULL;
}
current_process->ifile[iofile_seek++] = cmdline[cmdline_pos++];
}
current_process->ifile[iofile_seek] = '\0';
current_job->mystdin = INPUT_FD;
while(isspace(cmdline[cmdline_pos])) {
if(cmdline[cmdline_pos] == '\n')
break;
++cmdline_pos;
}
valid_input = false;
break;
case '>': /* output redirection */
current_process->ofile = (char *) calloc(MAX_LEN_FILENAME, sizeof(char));
if(!current_process->ofile) {
fprintf(stderr, "%s\n","malloc: no space");
delete_job(current_job,first_job);
return NULL;
}
++cmdline_pos;
while (isspace(cmdline[cmdline_pos])){++cmdline_pos;} /* ignore any spaces */
iofile_seek = 0;
while(cmdline[cmdline_pos] != '\0' && !isspace(cmdline[cmdline_pos])){
if(MAX_LEN_FILENAME == iofile_seek) {
fprintf(stderr, "%s\n","malloc: no space");
delete_job(current_job,first_job);
return NULL;
}
current_process->ofile[iofile_seek++] = cmdline[cmdline_pos++];
}
current_process->ofile[iofile_seek] = '\0';
current_job->mystdout = OUTPUT_FD;
while(isspace(cmdline[cmdline_pos])) {
if(cmdline[cmdline_pos] == '\n')
break;
++cmdline_pos;
}
valid_input = false;
break;
case '|': /* pipeline */
cmd[cmd_pos] = '\0';
process_t *newprocess = (process_t *)malloc(sizeof(process_t));
if(!newprocess) {
fprintf(stderr, "%s\n","malloc: no space");
delete_job(current_job,first_job);
return NULL;
}
if(!init_process(newprocess)) {
fprintf(stderr, "%s\n","init_process: failed");
delete_job(current_job,first_job);
return NULL;
}
if(!readprocessinfo(current_process, cmd)) {
fprintf(stderr, "%s\n","parse cmd: error");
delete_job(current_job,first_job);
return NULL;
}
current_process->next = newprocess;
current_process = current_process->next;
++cmdline_pos;
cmd_pos = 0; /*Reinitialze for new cmd */
valid_input = true;
break;
case '&': /* background job */
current_job->bg = true;
while (isspace(cmdline[cmdline_pos])){++cmdline_pos;} /* ignore any spaces */
if(cmdline[cmdline_pos+1] != '\n' && cmdline[cmdline_pos+1] != '\0')
fprintf(stderr, "reading bg: extra input ignored");
end_of_input = true;
break;
case ';': /* sequence of jobs*/
sequence = true;
strncpy(current_job->commandinfo,cmdline+seq_pos,cmdline_pos-seq_pos);
seq_pos = cmdline_pos + 1;
break;
case '#': /* comment */
end_of_input = true;
break;
default:
if(!valid_input) {
fprintf(stderr, "%s\n", "reading cmdline: could not fathom input");
delete_job(current_job,first_job);
return NULL;
}
if(cmd_pos == MAX_LEN_CMDLINE-1) {
fprintf(stderr,"%s\n","reading cmdline: length exceeds the max limit");
delete_job(current_job,first_job);
return NULL;
}
cmd[cmd_pos++] = cmdline[cmdline_pos++];
break;
}
if(end_of_input || sequence)
break;
}
cmd[cmd_pos] = '\0';
if(!readprocessinfo(current_process, cmd)) {
fprintf(stderr,"%s\n","read process info: error");
delete_job(current_job,first_job);
return NULL;
}
if(!sequence) {
strncpy(current_job->commandinfo,cmdline+seq_pos,cmdline_pos-seq_pos);
break;
}
sequence = false;
++cmdline_pos;
}
return first_job;
}