-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparser_tester.c
81 lines (71 loc) · 2.12 KB
/
parser_tester.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
/* COMP 530: Tar Heel SHell
*
* This file is a test harness for the lab 1
* parser. It takes in commands from standard in,
* passes them to the parser, and outputs the parsed
* version in a standardized format for unit testing.
*
* Do not change this file.
*/
#include "thsh.h"
int main(int argc, char **argv, char **envp) {
// flag that the program should end
bool finished = 0;
// buffer to hold current command
int ret = 0;
do {
int length;
char cmd[MAX_INPUT];
// Get a pointer to cmd that type-checks with char *
char *buf = &cmd[0];
char *parsed_commands[MAX_PIPELINE][MAX_ARGS];
char *infile = NULL;
char *outfile = NULL;
// Reset memory from the last iteration
for(int i = 0; i < MAX_PIPELINE; i++) {
for(int j = 0; j < MAX_ARGS; j++) {
parsed_commands[i][j] = NULL;
}
}
// Read a line of input
length = read_one_line(0, buf, MAX_INPUT);
if (length <= 0) {
ret = length;
break;
}
// Pass it to the parser
ret = parse_line(buf, length, parsed_commands, &infile, &outfile);
if (ret == -ENOSYS) {
printf("parse_line (probably) not implemented. Giving up.\n");
break;
} else if (ret < 0) {
printf("parse_line failed (%d), exiting\n", ret);
break;
}
// Pretty print everything
for (int i = 0; parsed_commands[i][0]; i++) {
printf("Pipeline Stage %d: ", i);
for (int j = 0; parsed_commands[i][j]; j++) {
printf("[%s] ", parsed_commands[i][j]);
}
printf("\n");
}
if (infile) {
printf("Input redirection to file [%s]\n", infile);
}
if (outfile) {
printf("Output redirection to file [%s]\n", outfile);
}
// If any commands are built-in commands, execute them.
// Otherwise, we will handle this in lab 2
for (int i = 0; parsed_commands[i][0]; i++) {
int rv;
if (handle_builtin(parsed_commands[i], 0, 1, &rv)) {
printf("Command [%s] is a built-in, returned %d.\n", parsed_commands[i][0], rv);
} else {
printf("Command [%s] is not a built-in.\n", parsed_commands[i][0]);
}
}
} while (!finished);
return ret;
}