-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcdshell.c
More file actions
322 lines (268 loc) · 6.89 KB
/
Copy pathcdshell.c
File metadata and controls
322 lines (268 loc) · 6.89 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
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
#include <stdio.h>
#include <unistd.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>
#include <readline/readline.h>
#include <readline/history.h>
//Chase Deets
//chd5hq@virginia.edu
//CS 4414-001 - Project 1: Implementing a Shell in C
//currently compiled with: gcc -lreadline -std=c11 cdshell.c -o cdshell
//returns an int after populating an array of token groups
//input: line from commandline, array of strings for arguments
int parseTok(char* cmdline, char** args)
{
int numArgs = 0;
char s[64];
strcpy(s, cmdline);
char *tok;
if(!tok){
printf("ERROR: failed to tokenize input, exiting...\n");
return -1;
}
tok = strtok(s, "|");
while(tok != NULL) {
printf("tok: %s\n", tok);
args[numArgs] = malloc(strlen(tok)+1);
strcpy(args[numArgs], tok);
printf("arg: %s - ", args[numArgs]);
//args[numArgs] = strdup(tok);
//args[numArgs] = strcpy(args[numArgs], tok);
//printf("added arg: %s\n", args[numArgs]);
numArgs++;
printf("%d\n", numArgs);
tok = strtok(NULL, "|"); //pass null to continue strtok on tok
}
//args[numArgs] = NULL;
printf("done, returning args..");
return numArgs;
}
int validateCommand(char *argument, char **parameters, int *numIns, int *numOuts, int *paramCt){
char temp[64];
char *par;
int p;
strcpy(temp, argument);
printf("validating: %s\n", temp);
par = strtok(temp, " ");
p = 0;
//numIns = 0;
//numOuts = 0;
int ins = 0;
int outs = 0;
while(par != 0){
parameters[p] = malloc(strlen(par)+1);
strcpy(parameters[p], par);
printf("parameter[%d]:%s\n", p, parameters[p]);
par = strtok(NULL, " ");
p++;
}
parameters[p] = '\0';
*paramCt = p;
for(int i = 0; i < p; i++){
if(strcmp(parameters[i], "<") == 0){
ins++;
printf("%d input redirects found!\n", ins);
}
if(strcmp(parameters[i], ">") == 0){
outs++;
printf("%d output redirects found!\n", outs);
}
}
*numIns = ins;
*numOuts = outs;
if(ins >= 2)
return -1;
else if(outs >= 2)
return -2;
else
return 0;
}
int executeCommand(char **cmdArgs){
printf("executing %s\n", cmdArgs[0]);
//char *testArgs[5] = {"ls", "-l", NULL};
pid_t pid;
//fork process
pid = fork();
if(pid < 0){
fprintf(stderr, "ERROR: fork failed\n");
return 1;
}
else if(pid == 0) {
execvp(cmdArgs[0], cmdArgs);
perror("ERROR");
return 1;
}
else{
wait(NULL);
printf("Child has executed process!\n");
return 0;
}
return 0;
}
int executeRedirect(char **cmdArgs, int numOfCmds, char **reParams){
int inFile;
int outFile;
int inRedInd;
int outRedInd;
int numIns;
int numOuts;
int numPrms;
char tempArg[64];
pid_t pid;
printf("executing redirect\n");
printf("numOfCmds: %d\n", numOfCmds);
numIns = 0;
numOuts = 0;
numPrms = 0;
for(int k = 0; k < numOfCmds; k++){
if(strcmp(cmdArgs[k], "<") == 0){
inRedInd = k;
numIns++;
printf("inRedInd: %d\n", inRedInd);
k++;
}else if (strcmp(cmdArgs[k], ">") == 0){
outRedInd = k;
numOuts++;
printf("outRedInd: %d\n", outRedInd);
k++;
}else{
printf("add param %s to reparams...\n", cmdArgs[k]);
reParams[k] = malloc(strlen(cmdArgs[k]+1));
strcpy(reParams[k], cmdArgs[k]);
printf("readied param: %s\n", reParams[k]);
numPrms++;
}
reParams[numPrms] = '\0'; //null terminate for later execution ease.
}
printf("numIns: %d\n", numIns);
printf("numOuts: %d\n", numOuts);
if(numOuts == 1){
//setup output redirect and outfile name
outFile = open(cmdArgs[outRedInd + 1], O_WRONLY | O_TRUNC | O_CREAT, S_IRUSR | S_IRGRP | S_IWGRP | S_IWUSR);
printf("outfile: %d - %s\n", outFile, cmdArgs[outRedInd + 1]);
if(outFile == -1){
printf("ERROR: could not access output file. You have been terminated.\n");
exit(1);
}
}
if(numIns == 1){
//setup input redirect and infile name
inFile = open(cmdArgs[inRedInd + 1], O_RDONLY);
printf("infile: %d - %s\n", inFile, cmdArgs[inRedInd + 1]);
if(inFile == -1){
printf("ERROR: could not access input file. You have been terminated.\n");
exit(1);
}
}
for(int j = 0; j < numPrms; j++)
printf("readiedParam[%d]: %s ", j, reParams[j]);
printf("\n");
//fork process
pid = fork();
//printf("pid: %d", pid);
if(pid < 0){
fprintf(stderr, "ERROR: fork failed\n");
exit(1);
}
else if(pid == 0) {
//printf("hello from child!");
if(numIns > 0){
dup2(inFile, 0);
close(inFile);
}
if(numOuts > 0){
dup2(outFile, 1);
close(outFile);
}
//printf("executing readied parameters!\n");
execvp(reParams[0], reParams);
perror("ERROR: failure to execute command\n");
return 0;
}
else{
if(numOuts > 0){
close(outFile);
}
if(numIns > 0){
close(inFile);
}
wait(NULL);
printf("Child has executed process!\n");
return 0;
}
return 0;
}
int main(int argc, char** argv)
{
printf("- Starting Shell...\n");
printf("-- CDShell V0.1 --\n");
int numTokenGroups;
int numPipes;
int numInputRedirects;
int inputInd;
int numOutRedirects;
int numParams;
int outputInd;
int execStatus;
int validCmd;
int validToks;
char *inpt; //input from user
char command[64];
//char **tokenGroup = malloc(256 * sizeof(char*));
char **tokenGroup; //collection of tokens
char *params[64]; //token --> Params to be executed
char *redirArgs[64];
while(1)
{
inpt = readline(">>> ");
add_history(inpt); //allows for repeating previous cmds
if((strcmp(inpt, "exit") == 0) || (strcmp(inpt, "Exit") == 0)){
printf("Exiting shell... Ciao!\n");
exit(0);
}
//populate token group with input line
numTokenGroups = parseTok(inpt, tokenGroup);
//token group with tokens
numPipes = numTokenGroups - 1;
numInputRedirects = 0;
numOutRedirects = 0;
printf("successful parse! numCmds: %d\n", numTokenGroups);
printf("number of pipes: %d\n", numPipes);
if(numTokenGroups == 0)
printf("ERROR: No command entered\n");
if(numTokenGroups == 1){ //no pipes
strcpy(command, tokenGroup[0]);
printf("command found: %s\n", command);
//params[0] = malloc(strlen(command)+1);
//strcpy(params[0], command);
//printf("parameter[%d]:%s\n", 0, params[0]);
validCmd = validateCommand(command, params, &numInputRedirects, &numOutRedirects, &numParams);
printf("validCmd: %d\n", validCmd);
printf("inredirs: %d -- outredirs: %d\n", numInputRedirects, numOutRedirects);
printf("num of Parameters: %d\n", numParams);
if(validCmd != 0){
printf("ERROR: Invalid command entered. Check syntax.\n");
}
else
printf("ready to execute command - %s\n", params[0]);
if((numInputRedirects == 0) && (numOutRedirects == 0))
execStatus = executeCommand(params);
else
execStatus = executeRedirect(params, numParams, redirArgs);
if(execStatus == 0)
printf("Successful Command Execution\n");
else
printf("ERROR: Could not execute Command\n");
}
else{ //pipes!
printf("Number of pipes to handle: %d\n", numPipes);
}
free(inpt);
//free(temp);
//free(tokenGroup);
}
return 0;
}