-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhau1551.tar
343 lines (300 loc) · 20 KB
/
hau1551.tar
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
main.cpp 0000664 0001750 0001750 00000000405 12226066020 011443 0 ustar david david /*
* Author: David Chau
* File: main.cpp
* Description: Driver main file.
* This program is a shell program made for unix/linux systems
*/
#include "shell.h"
int main(int argc, char *argv[])
{
shell myShell;
myShell.runShell();
return 0;
} shell.h 0000664 0001750 0001750 00000001634 12226065575 011316 0 ustar david david /*
* Author: David Chau
* File: shell.h
* Description: Header file to support implementation
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include "enums.h"
class shell
{
public:
//Default constructor
shell();
//Runs the shell program
void runShell();
protected:
//Parses input into the first argument and additional arguments
//Sees if there are any pipes, indirection, or redirection
//Returns number of arguments if needed
int parse(char *inputString, char *cmdArgv[], char **supplementPtr);
//Goes to the end of the word and delimits it with a null character
void nullDelimateArgs(char *srcPtr);
//Function which executes OS calls
void execute(char **cmdArgv, char **supplementPtr);
private:
modes mode;
char CWD[MAX_PATH];
char *userInput;
size_t len;
}; shell.cpp 0000664 0001750 0001750 00000012011 12226066243 011631 0 ustar david david /*
* Author: David Chau
* File: shell.cpp
* Description: Shell implementation file for unix/linux OS's
*/
#include "shell.h"
shell::shell()
{
mode = NORMAL;
userInput = (char*)malloc(sizeof(char)*MAX_INPUT);
len = MAX_INPUT;
}
void shell::runShell()
{
char *supplement;
char *cmdArgv[MAX_INPUT];
while(true)
{
mode = NORMAL;
getcwd(CWD, MAX_PATH);
std::cout<< CWD << ">$ ";
getline(&userInput, &len, stdin);
//Seeing if the user entered exit. If they did, then exit normally
if(strcmp(userInput, "exit\n") == 0)
exit(1);
parse(userInput, cmdArgv, &supplement);
if(strcmp(*cmdArgv, "cd") == 0)
{
if(cmdArgv[1] == NULL)
chdir(getenv("HOME"));
else
chdir(cmdArgv[1]);
}
else
execute(cmdArgv, &supplement);
}
}
int shell::parse(char *inputString, char *cmdArgv[], char **supplementPtr)
{
int cmdArgc = 0;
bool stopParsing = false;
char *inputCharPtr = inputString;
//Going through the entire input line
while(*inputCharPtr != '\0' && stopParsing == false)
{
*cmdArgv = inputCharPtr;
cmdArgc++;
//Iterating though all alpha-numeric characters
while((isspace(*inputCharPtr) == 0) && *inputCharPtr != '\0'
&& stopParsing == false)
{
switch(*inputCharPtr)
{
case '&':
mode = BACKGROUND;
break;
case '>':
case '<':
if(*inputCharPtr == '>')
mode = OUTPUT_REDIRECTION;
else
mode = INPUT_REDIRECTION;
*cmdArgv = '\0';
inputCharPtr++;
//Iterating the input until the next argument
while(*inputCharPtr == ' ' || *inputCharPtr == '\t')
inputCharPtr++;
*supplementPtr = inputCharPtr;
nullDelimateArgs(*supplementPtr);
stopParsing = true;
break;
case '|':
mode = PIPELINE;
*cmdArgv = '\0';
inputCharPtr++;
//Iterating the input until the next argument
while(*inputCharPtr == ' ' || *inputCharPtr == '\t')
inputCharPtr++;
*supplementPtr = inputCharPtr;
stopParsing = true;
break;
default:
//This should have nothing in it because we don't need
//to do anything while scanning the input.
break;
}
inputCharPtr++;
}
//Going to the next space and replacing all non-alpha characters
//with a null character
while((isspace(*inputCharPtr) != 0) && stopParsing == false)
{
*inputCharPtr = '\0';
inputCharPtr++;
}
cmdArgv++;
}
*cmdArgv = '\0';
return cmdArgc;
}
void shell::nullDelimateArgs(char *inputCharPtr)
{
//Going to the end of the word
while(*inputCharPtr != ' ' && *inputCharPtr != '\t' && *inputCharPtr != '\n')
inputCharPtr++;
//Making the last argument be a null character
*inputCharPtr = '\0';
}
void shell::execute(char **cmdArgv, char **addArgsPtr)
{
//Making two process ids
pid_t pid, pid2;
FILE *fp;
int status;
//Creating pipes so that threads can share info through IPC's
int fd[2];
char *cmdArgv2[MAX_INPUT];
char *addArgs2 = NULL;
if(mode == PIPELINE)
{
if(pipe(fd)) //create pipe
{
std::cerr<< "An error occured making a Pipe failed!";
exit(-1);
}
parse(*addArgsPtr, cmdArgv2, &addArgs2);
}
pid = fork();
if( pid < 0)
{
std::cerr<< "An error occured creating a child process.\n";
exit(-1);
}
//Child process
else if(pid == 0)
{
switch(mode)
{
case OUTPUT_REDIRECTION:
//Opens the file and gives write/update privileges
fp = fopen(*addArgsPtr, "w+");
dup2(fileno(fp), 1);
break;
case INPUT_REDIRECTION:
//Opens the file and gives read access
fp = fopen(*addArgsPtr, "r");
dup2(fileno(fp), 0);
break;
case PIPELINE:
//close input of pipe
close(fd[0]);
//Sending data to another process
dup2(fd[1], fileno(stdout));
close(fd[1]);
break;
}
if(execvp(*cmdArgv, cmdArgv) == -1)
std::cout<<"The command is not supported\n";
}
//Parent process
else
{
if(mode == BACKGROUND)
;
else if(mode == PIPELINE)
{
//wait for child process 1 to finish
waitpid(pid, &status, 0);
pid2 = fork();
if(pid2 < 0)
{
std::cerr<< "There was an error forking the second process.\n";
exit(-1);
}
else if(pid2 == 0)
{
//close output to pipe
close(fd[1]);
//Reading input from other process
dup2(fd[0], fileno(stdin));
close(fd[0]);
if(execvp(*cmdArgv2, cmdArgv2) == -1)
std::cout<<"The command is not supported\n";
}
else
{
close(fd[0]);
close(fd[1]);
}
}
else
waitpid(pid, &status, 0);
}
} enums.h 0000664 0001750 0001750 00000000640 12226065406 011323 0 ustar david david /*
* Author: David Chau
* File: enums.h
* Description: File that contains structures and definitions
*/
//Max history path of user's location
#define MAX_PATH 100
//Max number of characters that can be entered by the user
#define MAX_INPUT 80
//Different Modes which can occur
//Dependent on user input command
enum modes
{
NORMAL, OUTPUT_REDIRECTION, INPUT_REDIRECTION,
PIPELINE, BACKGROUND
}; makefile 0000664 0001750 0001750 00000000261 12226026723 011521 0 ustar david david myShell: main.o shell.o
g++ main.o shell.o -o myShell
main.o: main.cpp shell.h
g++ -c main.cpp
shell.o: shell.cpp shell.h enums.h
g++ -c shell.cpp
clean:
rm -rf *o shellLab