-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.c
383 lines (358 loc) · 11.8 KB
/
parser.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
#include "parser.h"
#include "headers.h"
#include "util.h"
#include "ls.h"
#include "pinfo.h"
#include "zombie_killer.h"
#include "process_maker.h"
#include <signal.h>
#include "history_handler.h"
#include "nightswatch.h"
#include "env_var.h"
// runs the commands / built ins
void run_command(char **tokens, int num_tokens, int bg, int *pipe, int prev_open, char * oldcommand) {
char *first = tokens[0];
if (strcmp(first, "cd") == 0) {
if (num_tokens == 1) {
tokens[1] = malloc(4);
strcpy(tokens[1], "~");
}
cd_handler(tokens);
} else if (strcmp(first, "pwd") == 0) {
pwd_handler();
} else if (strcmp(first, "ls") == 0) {
ls_handler(tokens, num_tokens, currDir, homeDir);
} else if (strcmp(first, "echo") == 0) {
echo_handler(tokens, num_tokens);
} else if (strcmp(first, "quit") == 0) {
killbg();
printf("cya\n");
_exit(0);
} else if (strcmp(first, "clear") == 0) {
clearScreen();
} else if (strcmp(first, "pinfo") == 0) {
if (num_tokens == 1) {
tokens[1] = malloc(10);
sprintf(tokens[1], "%d", getpid());
}
pinfo_handler(tokens);
} else if (strcmp(first, "history") == 0) {
if (num_tokens == 1) {
show_history(10);
} else {
/*if (tokens[1][0] != '-') {
printf("Second arg must be a flag\n");
return;
}
tokens[1]++;*/
if (strtol(tokens[1], NULL, 10) <= 0 || strtol(tokens[1], NULL, 10) > 20) {
fprintf(stderr, "history <int n> \n n > 0 && n <= 20\n");
exit_code = 1;
return;
}
show_history(atoi(tokens[1]));
}
} else if (strcmp(first, "nightswatch") == 0) {
nightswatch_handler(tokens, num_tokens);
} else if (strcmp(first, "getenv") == 0) {
getenv_handler(tokens, num_tokens);
} else if (strcmp(first, "setenv") == 0) {
setenv_handler(tokens, num_tokens);
} else if (strcmp(first, "unsetenv") == 0) {
unsetenv_handler(tokens, num_tokens);
} else if (strcmp(first, "jobs") == 0) {
job_printer();
} else if (strcmp(first, "kjob") == 0) {
kjob_handler(tokens, num_tokens);
}else if (strcmp(first, "overkill") == 0) {
overkill_handler(tokens, num_tokens);
}else if (strcmp(first, "fg") == 0) {
fg_handler(tokens, num_tokens);
}else if (strcmp(first, "bg") == 0) {
bg_handler(tokens, num_tokens);
} else
make_process(tokens, num_tokens, bg, pipe, prev_open, oldcommand);
}
// note
// redirection are parsed left to right
// the positon of them doesnt matter
// <fileA means stdin of this command is from fileA
// >fileB means stdout of this command is to fileB
// the file is the "word" after the symbol
// none of these are arguments
// cat file means the C code for cat opens the file
// cat < file means that file is now the stdin and cat reads from its stdin
// >fileA >fileB will first open fileA as stdout (open as overwriting), then as it parses ">fileB" it will close fileA (thus overwriting it as empty) and then link fileB to the stdout
// > opens in overwrite mode and >> opens in append mode
// pipe uses pipe syscall to get the fds and sets stdin/stdout based on that
// parses > < >>
// what it does is take a token and separate all instaces
// of tokens from the words
// eg ls>a.txt => ls > a.txt with token >
int tokenize(const char *token, char *string, char *tokens[100]) {
int len_tok = strlen(token);
int len_str = strlen(string);
int count = 0;
int start = 0;
for (int i = 0; i < len_str - len_tok + 1;) {
bool found = true;
for (int j = 0; j < len_tok; j++) {
if (token[j] != string[i + j]) {
found = false;
break;
}
}
if (found) {
// last char has just been matched
for (int k = i; k < i + len_tok; k++)
string[k] = '\0';
tokens[count] = malloc(size_buff);
strcpy(tokens[count], string + start);
count++;
tokens[count] = malloc(size_buff);
strcpy(tokens[count], token);
count++;
start = i + len_tok;
i += len_tok;
} else {
i += 1;
}
}
tokens[count] = malloc(size_buff);
strcpy(tokens[count], string + start);
count++;
// for (int i = 0; i < count; i++) {
// printf("%d--%s---\n", i, tokens[i]);
// }
return count;
}
// redirection changes
void changeInput(char *token, char *file) {
if (strcmp(token, ">") == 0) {
// change stdout
int new_fd;
if ((new_fd = open(file, O_WRONLY | O_CREAT | O_TRUNC, 0644)) == -1) {
exit_code = 1;
perror("cannot redirect output");
} else {
close(STDOUT_FILENO);
dup(new_fd);
close(new_fd);
//printf("redirecting %d to another file", t);
}
} else if (strcmp(token, ">>") == 0) {
// change stdout
int new_fd;
if ((new_fd = open(file, O_WRONLY | O_CREAT | O_APPEND, 0644)) == -1) {
exit_code = 1;
perror("cannot redirect output");
} else {
close(STDOUT_FILENO);
dup(new_fd);
close(new_fd);
//printf("redirecting %d to another file", t);
}
} else if (strcmp(token, "<") == 0) {
// change in
int new_fd;
//printf("redirecting stdin to another file");
if ((new_fd = open(file, O_RDONLY)) == -1) {
perror("cannot redirect input");
exit_code = 1;
} else {
close(STDIN_FILENO);
dup(new_fd);
close(new_fd);
//printf("redirecting %d to another file", t);
}
}
}
void fixInput(int in, int out) {
dup2(in, 0);
close(in);
dup2(out, 1);
close(out);
}
// takes care of redirection and spaces
void redirectionHandler(char *input, int bg, int *pipe, int prev_open) {
char *tokens[1000];
int num_tokens = 0;
char input2[size_buff];
strcpy(input2, input);
tokens[0] = strtok(input, " \t\n");
while (tokens[num_tokens] != NULL) {
tokens[++num_tokens] = strtok(NULL, " \t");
}
if (num_tokens == 0) {
return;
}
char tokens_append[100][1000];
int n = 0;
for (int i = 0; i < num_tokens; i++) {
char *new_tokens[100];
int c = tokenize(">>", tokens[i], new_tokens);
for (int j = 0; j < c; j++) {
if (strcmp(new_tokens[j], "") != 0)
strcpy(tokens_append[n++], new_tokens[j]);
free(new_tokens[j]);
}
}
// >
char tokens_append_out[100][1000];
num_tokens = n;
n = 0;
for (int i = 0; i < num_tokens; i++) {
//printf("-token : %s-\n", tokens_append[i]);
char *new_tokens[100];
if (strcmp(tokens_append[i], ">>") == 0) {
strcpy(tokens_append_out[n++], tokens_append[i]);
continue;
}
int c = tokenize(">", tokens_append[i], new_tokens);
for (int j = 0; j < c; j++) {
if (strcmp(new_tokens[j], "") != 0) {
strcpy(tokens_append_out[n++], new_tokens[j]);
}
free(new_tokens[j]);
}
}
// <
char tokens_final[100][1000];
num_tokens = n;
n = 0;
for (int i = 0; i < num_tokens; i++) {
//printf("-token : %s-\n", tokens_append_out[i]);
char *new_tokens[100];
int c = tokenize("<", tokens_append_out[i], new_tokens);
for (int j = 0; j < c; j++) {
if (strcmp(new_tokens[j], "") != 0)
strcpy(tokens_final[n++], new_tokens[j]);
free(new_tokens[j]);
}
}
//now we need to do the redirection
//int backup_stdout = dup(STDOUT_FILENO);
//int backup_stdin = dup(STDIN_FILENO);
char *command_tokens[1000];
int num_word_command = 0;
for (int i = 0; i < n; i++) {
char *word = tokens_final[i];
//fprintf(stderr, "--%s--\n", word);
if (strcmp(word, ">") == 0 || strcmp(word, ">>") == 0 || strcmp(word, "<") == 0) {
if (i + 1 == n || tokens_final[i + 1] == NULL) {
fprintf(stderr, "unexpected token after %s \n", word);
exit_code = 1;
return;
}
changeInput(word, tokens_final[i + 1]);
i++;
} else {
command_tokens[num_word_command] = malloc(size_buff);
strcpy(command_tokens[num_word_command], tokens_final[i]);
num_word_command++;
}
}
run_command(command_tokens, num_word_command, bg, pipe, prev_open, input2);
for (int i = 0; i < num_word_command; i++)
free(command_tokens[i]);
}
// checks for pipes
void pipeChecker(char *cmd, int bg) {
int pipee = 0;
for (int i = 0; i < strlen(cmd); i++)
if (cmd[i] == '|')
pipee++;
if (pipee == 0) {
exit_code = 0;
redirectionHandler(cmd, bg, NULL, -1);
return;
} else if (cmd[0] == '|' || cmd[strlen(cmd) - 1] == '|') {
fprintf(stderr, "Pipe does not have both ends \n");
exit_code = 1;
return;
}
char *commands[1000];
int n = 0;
char *t = strtok(cmd, "|");
while (t != NULL) {
commands[n] = malloc(size_buff);
strcpy(commands[n], t);
t = strtok(NULL, "|");
//printf("-%s-\n", commands[n]);
n++;
}
int out = dup(1);
int in = dup(0);
int prev_open = -1;
for (int i = 0; i < n - 1; i++) {
int pipes[2];
if (pipe(pipes) == -1) {
//perror("cannot open pipe");
return;
}
if (prev_open != -1) {
dup2(prev_open, 0);
//perror("connected prev input pipe");
close(prev_open);
//perror("closed prev input pipe");
}
dup2(pipes[1], 1);
//perror("connected new output pipe");
close(pipes[1]);
//perror("connected output pipe old fd");
redirectionHandler(commands[i], bg, pipes, prev_open);
prev_open = pipes[0];
free(commands[i]);
}
dup2(out, 1);
//perror("connected output to stdout");
close(out);
if (prev_open != -1) {
dup2(prev_open, 0);
//perror("connected prev input pipe");
close(prev_open);
}
exit_code = 0;
redirectionHandler(commands[n - 1], bg, NULL, prev_open);
dup2(in, 0);
close(in);
}
// separates commands by ; &
void getIndividualCommands(char *line) {
char *command;
char line2[size_buff], line3[size_buff];
strcpy(line2, line);
strcpy(line3, line);
command = strtok(line, ";&");
int c = 0;
while (command != NULL) {
c++;
command = strtok(NULL, ";&");
}
char *commands[c + 1];
int i = 0;
if (c <= 0) return;
char *beg = line2;
commands[0] = strtok(line2, ";&");
while (commands[i] != NULL && strcmp(commands[i], "") != 0) {
//printf("%c\n", line3[strlen(commands[i]) + line2 - beg]);
//printf("%c\n", line3[strlen(commands[i]) + (commands[i] - beg)]);
i++;
commands[i] = strtok(NULL, ";&");
}
for (int j = 0; j < c; j++) {
bool bg = false;
if (line3[strlen(commands[j]) + (commands[j] - beg)] == '&') {
bg = true;
}
commands[j] = trim_whitespace(commands[j]);
//redirectionHandler(commands[j], bg);
int backup_stdout = dup(STDOUT_FILENO);
int backup_stdin = dup(STDIN_FILENO);
pipeChecker(commands[j], bg);
fixInput(backup_stdin, backup_stdout);
}
// everything gets automatically deallocated as strtok is in place
}
// input -> get commands (splits by & and ;) -> pipeChecker (handles piping) -> redirectionHandler (handles redirection)
// -> (uses tokenizer) -> run command