-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathatwShell.c
More file actions
268 lines (229 loc) · 6.65 KB
/
Copy pathatwShell.c
File metadata and controls
268 lines (229 loc) · 6.65 KB
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
/*
atwShell.c
Andrew T. Woosnam
Sept 23, 2017
Thanks to help from
Sherri Goings & Jeff Ondich
TO DO:
* pipe with redirects doesn't fully work
*/
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <fcntl.h>
#include <ctype.h>
#define clear() printf("\033[H\033[J")
char** readLineOfWords();
int isValid(char c);
// A line may be at most 100 characters long, which means longest word is 100 chars,
// and max possible tokens is 51 as must be space between each
size_t MAX_WORD_LENGTH = 100;
size_t MAX_NUM_WORDS = 51;
int main() {
int wait = 1; // default = wait for child processes to finish
char* outfile = NULL;
char* infile = NULL;
int numPipes = 0;
char* leftOfPipe[MAX_NUM_WORDS];
char* rightOfPipe[MAX_NUM_WORDS];
int pipeArr[2]; // this pipe array may or may not be used
int i; int j;
clear();
printf("Enter a shell command: ");
fflush(stdout);
while (1) {
wait = 1;
// clean up before next command
outfile = NULL;
infile = NULL;
for (i = 0; i < MAX_NUM_WORDS; i++) {
leftOfPipe[i] = NULL;
rightOfPipe[i] = NULL;
}
numPipes = 0;
i = 0; j = 0;
char** words = readLineOfWords();
if (words[i] == NULL) {
printf("Enter a shell command: "); fflush(stdout);
continue;
} else if (strcmp(words[0], "-1") == 0) {
// value of -1 is signal that readLineOfWords() read > 100 characters
printf("ERROR: Commands can only contain a maximum of 100 characters.\n");
printf("\nEnter a shell command: "); fflush(stdout);
continue;
} else {
int invalidChar = 0;
while (words[i] != NULL) {
// check for invalid characters
while (words[i][j] != '\0') {
if (!(isValid(words[i][j]))) {
printf("ERROR: invalid character: %c\n", words[i][j]);
invalidChar = 1;
break;
}
j++; // next char
}
// scan for special operators, record infile/outfile appropriately
char token = (* words[i]);
j = 0;
if (token == '|') {
// signal that we have a piped command
numPipes++;
}
else if (token == '>') {
// stop execution before it tries to execute anything after '>'
words[i] = NULL;
outfile = words[i + 1];
}
else if (token == '<') {
// stop execution before it tries to execute anything after '>'
words[i] = NULL;
infile = words[i + 1];
}
else if (token == '&') {
words[i] = NULL;
wait = 0; // don't wait for child to finish
}
i++; // next word in command string
}
if (invalidChar == 1) {
invalidChar = 0;
printf("\nEnter a shell command: "); fflush(stdout);
continue; // new command prompt
}
printf("\n");
// special pre-fork setup in case of piping
if (numPipes != 0) {
pipe(pipeArr);
i = 0;
// store "pre-pipe" command
while ((* words[i]) != '|') {
leftOfPipe[i] = words[i];
i++;
}
leftOfPipe[i] = NULL;
i++;
j = 0;
// store "post-pipe" command
while (words[i] != NULL) {
rightOfPipe[j] = words[i];
i++; j++;
}
rightOfPipe[i] = NULL;
}
int pid = fork(); // every command should fork at least 1 child
if ( pid == 0 ) { // child process
if (outfile != NULL) {
int newfd = open(outfile, O_CREAT | O_WRONLY, 0644);
dup2(newfd, 1);
}
if (infile != NULL) {
int newfd = open(infile, O_CREAT | O_RDONLY, 0644);
dup2(newfd, 0);
}
// piping case
if (numPipes != 0) {
// execute pre-pipe command
close(pipeArr[0]);
dup2(pipeArr[1], 1);
int err = execvp(leftOfPipe[0], leftOfPipe);
printf("EXECUTION ERROR: %d\n", err);
break;
} else { // not piping
int err = execvp(words[0], words);
printf( "EXECUTION ERROR: %d\n", err );
break;
}
}
else { // parent
if (wait == 1) {
waitpid(pid, NULL, 0);
}
if (numPipes != 0) {
// if we're piping, we'll need another child
int pidLast = fork();
if (pidLast == 0) {
// execute post-pipe command
close(pipeArr[1]);
dup2(pipeArr[0], 0);
int err = execvp(rightOfPipe[0], rightOfPipe);
printf( "EXECUTION ERROR: %d\n", err );
break;
}
else { // parent
close(pipeArr[0]);
close(pipeArr[1]);
if (wait == 1) {
waitpid(pidLast, NULL, 0);
}
}
}
}
printf("\nEnter a shell command: "); fflush(stdout);
}
}
return 0;
}
/*
* Function to check whether or not c is an acceptable character
* to appear in an input command to the shell.
* Returns 1 if c is valid, returns 0 if c is invalid.
*/
int isValid(char c) {
int result = 0;
char otherValids[] = {'-', '.', '/', '_', '<', '>', '&', '|'};
if (isalpha(c) || isdigit(c))
result = 1;
else {
for (int i = 0; i < 8; i++) {
if (c == otherValids[i]) {
result = 1;
break;
}
}
}
return result;
}
/*
* reads a single line from terminal and parses it into an array of
* tokens/words by splitting the line on spaces. Adds NULL as final token
*/
char** readLineOfWords() {
// allocate memory for array of array of characters (list of words)
char** words = (char**) malloc( MAX_NUM_WORDS * sizeof(char*) );
int i;
for (i = 0; i < MAX_NUM_WORDS; i++) {
words[i] = (char*) malloc( MAX_WORD_LENGTH );
}
// read actual line of input from terminal
int bytes_read;
char *buf;
buf = (char*) malloc( MAX_WORD_LENGTH + 1 );
bytes_read = getline(&buf, &MAX_WORD_LENGTH, stdin);
if (bytes_read > 100) {
words[0] = "-1";
return words;
} else if (bytes_read == -1) {
// this is EOF (ctrl-D)
printf("logout\n[Process completed]\n"); fflush(stdout);
exit(0);
}
// take each word from line and add it to next spot in list of words
i = 0;
char* word = (char*) malloc( MAX_WORD_LENGTH );
word = strtok(buf, " \n");
while (word != NULL && i < MAX_NUM_WORDS) {
strcpy(words[i++], word);
word = strtok(NULL, " \n");
}
if (i >= MAX_NUM_WORDS) {
printf( "WARNING: line contains more than %d words!\n", (int)MAX_NUM_WORDS );
}
else
words[i] = NULL;
// return the list of words
return words;
}