-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.c
189 lines (158 loc) · 4.84 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
/* mush
* A minimally useful shell. Can run all commands
* available to it in its given path.
* The shell also supports pipelines, and features
* both an interactive and a batch processing mode.
*
* This shell is limited to a maximum pipeline size
* of 10, a maximum amount of arguments to a given
* function of 10, and a maximum line length of 512.
* These values can be adjusted in the misc header.
*
* Mush also has support for redirection of the
* input and output of a file, using ">" and "<"
* like one would in BASH.
*
* The shell also handles an interrupt signal.
* If the signal is generated during the running of a
* child process, it will terminate the child but
* continue running the shell. However, if no children
* are being run and the shell receives an interrupt
* signal, it will close the shell.
*
* Written by Michael Georgariou for
* CPE 357 with Professor Nico at
* Cal Poly SLO. */
#include "misc.h"
int helper(char input[], int length) {
char* lines[PIPE_COMMANDS];
struct command pipeline[PIPE_COMMANDS];
int j = 0;
char *start, *end;
/* set our start pointer to the beginning of the input */
start = input;
while (1) {
/* breaks once we hit the end of the input */
/* populates lines to be parsed later */
/* set the end pointer to the first occurance of a pipe */
end = strchr(start, '|');
/* if there are no more pipes */
if (end == NULL) {
/* set the end pointer to the end of our input */
end = &input[length];
/* store the line in our list of lines */
lines[j] = (char *) calloc(end - start + 1, sizeof(char));
strncpy(lines[j++], start, end - start);
/* and we are done */
break;
}
else {
/* if the pipe is not surrounded by whitespace, exit */
if (!isspace(*(end-1)) || !isspace(*(end+1))) {
fprintf(stderr, "invalid null command.\n");
return -1;
}
/* store the line in our list of lines */
lines[j] = (char *) calloc(end - start + 1, sizeof(char));
strncpy(lines[j++], start, end - start);
}
/* get ready to find next pipe */
start = end + 1;
}
if (j > PIPE_COMMANDS) {
fprintf(stderr, "Pipeline too deep.\n");
return(-1);
}
/* once we've gotten here, lines is populated and ready to be parsed */
if (-1 == parselines(lines, j, pipeline)) {
return -1;
}
if (-1 == run_commands(j, pipeline)) {
signal(SIGINT, SIG_DFL);
return -1;
}
/* guarantees that signals are being handled again,
* in case an edge case occurs where the signal is
* still being blocked */
signal(SIGINT, SIG_DFL);
return 0;
}
int clearbuffer(char input[]) {
int i;
for (i = 0; i < LINE_LENGTH; i++) {
input[i] = '\0';
}
return 0;
}
int main(int argc, char* argv[]) {
int c = 0, length;
char input[LINE_LENGTH + 1];
FILE* infile;
/* parse input file, if given */
if (argc != 1) {
/* only expect two arguments, max */
if (argc > 2) {
fprintf(stderr, "%s: too many arguments.\n", argv[0]);
return -1;
}
if (NULL == (infile = fopen(argv[1], "r"))) {
perror(argv[1]);
return -1;
}
}
/* if there was no input, set stdin as our input */
else {
infile = stdin;
}
/* loop in here until the exit command is typed */
while (1) {
length = 0;
/* if we were are the end of the file: */
if (c == EOF) {
/* and stdin is our input, reset. */
if (infile == stdin) {
printf("\n");
}
/* and something else is our input, stop doing stuff */
else {
break;
}
}
/* print the prompt (but only if using stdin) */
if (infile == stdin) {
printf("\033[0;32m");
printf("8-P ");
printf("\033[0m");
fflush(stdout);
}
/* grab the input from stdin */
while (((c = fgetc(infile)) != '\n') && (c != EOF)) {
if (length == LINE_LENGTH) {
fprintf(stderr, "command too long.\n");
length++;
continue;
}
else if (length > LINE_LENGTH) {
continue;
}
else {
input[length++] = c;
}
}
/* null terminate (so we can treat it like a string */
input[length] = '\0';
/* if the user inputted nothing, go away */
if (length == 0) {
continue;
}
/* handle the input. see above function */
if (length <= LINE_LENGTH)
helper(input, length);
/* clear the buffer for the next command */
clearbuffer(input);
}
if (infile != stdin) {
fclose(infile);
}
return 0;
}