-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsimple.c
446 lines (431 loc) · 11.3 KB
/
simple.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
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
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
#include <sys/types.h>
#include <signal.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <sys/wait.h>
#define NO_PIPE 0
#define PIPE_IN -1
#define PIPE_OUT 1
#define BUFFSIZE 512
#define DEBUG 0
char **envp;
int exit_num;
char ** split_spaces_cmd(const char * name,const char * params){
int i = 1;
char delims[] = " ";
char * next = NULL;
char * working = strdup(params);
char **params_array = (char**)malloc(sizeof(char**)*(strlen(params) + 2));
params_array[0] = strdup(name);
next = strtok(working,delims);
if(next == NULL){
params_array[i] = strdup(params);
i++;
}
else {
while(next != NULL){
params_array[i] = strdup(next);
i++;
next = strtok(NULL,delims);
}
}
params_array[i] = (char *)0;
return params_array;
}
char * sub_q(char * input){
char * buffer = strdup(input);
char * buffer2;
char code[4];
sprintf(code,"%d",exit_num);
for(int i = 0; buffer[i] != '\0';i++){
if(buffer[i] == '$' && buffer[i+1] == '?'){
buffer2 = (char *)malloc(strlen(input) + 2);
strncpy(buffer2,input,i);
strncpy(&buffer2[i],code,3);
strcat(buffer2,&input[i+2]);
i += strlen(code) - 2;
buffer = strdup(buffer2);
}
}
return buffer;
}
typedef struct StackNode
{
char* item;
struct StackNode * next;
} StackNode;
StackNode * stack_push(StackNode * head, const char * noot){
StackNode * new_node = (StackNode *)malloc(sizeof(*new_node));
if(new_node == NULL){return NULL;}
new_node->item = strdup(noot);
new_node->next = head;
return new_node;
}
char * stack_pop(StackNode * head){
char * item = head->item;
head = head->next;
return item;
}
void print_stack(StackNode * node){
if(node != NULL){
printf("%s",node->item);
if(node->next != NULL){
printf(" ");
print_stack(node->next);
}
}
}
void print_stack_lines(StackNode * node){
if(node->next != NULL){
print_stack_lines(node->next);
printf("%s\n",node->item);
}
else if(node == NULL){}
else {
printf("%s\n",node->item);
}
}
StackNode * root;
StackNode * history;
// Command functions
int cd(const char * dir){
if(dir == NULL){return 0;}
else{
return chdir(dir);
}
}
int pwd(){
char * pwd;
if((pwd = getcwd(NULL,0)) == NULL){return 1;}
else {
printf("%s\n",pwd);
}
free(pwd);
return 0;
}
int echo(const char * string){
if(string == NULL){return 0;}
if((strlen(string) >= 2) && (string[0] == '-' && string[1] == 'e')){
printf("%s", &string[2]);
}
else{
printf("%s\n",string);
}
return 0;
}
int pushd(const char * dir){
int ret = 0;
char * pwd;
if((pwd = getcwd(NULL,0)) == NULL){return 1;}
if(dir == NULL){
if(root == NULL){return 1;}
else{
char * top = stack_pop(root);
root = stack_push(root,pwd);
printf("%s ", top);
print_stack(root);
printf("\n");
return cd(top);
}
}
else {
ret = cd(dir);
char * new_dir;
if((new_dir = getcwd(NULL,0)) == NULL){return 1;}
root = stack_push(root,pwd);
printf("%s ", new_dir);
print_stack(root);
printf("\n");
return ret;
}
}
int popd(){
print_stack(root);
printf("\n");
char * new_dir = stack_pop(root);
root = root->next;
return cd(new_dir);
}
void exitc(const char * code){
if(code == NULL){
exit(0);
}
else {
int ret = atoi(code);
if(0 <= ret && ret <= 255){
exit(ret);
}
else {
exit(1);
}
}
}
int env(){
char **iter = envp;
for(;iter != NULL;iter++){
printf("%s\n", *iter);
}
return 1;
}
//big ass switchy thingy
int find_and_exec(const char * command_name, const char * parameters){
int ret;
if(command_name == NULL){
return 0;
}
else if(strcmp(command_name,"pwd") == 0){
return pwd();
}
else if(strcmp(command_name,"cd") == 0){
return cd(parameters);
}
else if(strcmp(command_name,"echo") == 0){
return echo(parameters);
}
else if(strcmp(command_name,"pushd") == 0){
return pushd(parameters);
}
else if(strcmp(command_name,"popd") == 0){
return popd();
}
else if(strcmp(command_name,"exit") == 0){
exitc(parameters);
}
else if(strcmp(command_name,"set") == 0){
printf("Did you mean export? If you are looking for windows try your walls\n");
return 0;
}
else if(strcmp(command_name,"history") == 0){
print_stack_lines(history);
return 0;
}
else if(strcmp(command_name,"unsetenv") == 0){
return unsetenv(parameters);
}
else {
int pid = fork();
if(pid == -1){return 1;}
else if(pid == 0){
if(parameters == NULL) {
execlp(command_name,command_name,NULL);
}
else {
execvp(command_name,split_spaces_cmd(command_name,parameters));
}
}
else {
wait(&ret);
return ret;
}
}
return 0;
}
int parse_exec(const char* input){
if(input[0] == '\0'){return 1;}
int ret = 0;
int head = 0;
int pipev = NO_PIPE;
int pipeline = 0;
char * parsable = (char *)strdup(input);
if (parsable == NULL){
return 1;
}
char * command = NULL;
char * params = NULL;
char * file = NULL;
for(;parsable[head] == ' ' || parsable[head] == '\t';head++){}
int tail = head;
for(;parsable[head] != '\0';head++){
if((parsable[head] == ' ' || parsable[head] == '\t') && command == NULL && pipev == NO_PIPE){//first word terminated by space character
command = (char *)malloc(sizeof(char) *(head - tail + 1));
if (command == NULL){return 1;}
if (strncpy(command,parsable + tail,head-tail) == NULL){return 1;}
command[head-tail] = '\0';
for(;parsable[head] == ' ' || parsable[head] == '\t';head++){}//discard any extra spaces after the first one
tail = head;
head--;//the head is at the next character but is going to get incremented so we must decrement first
}
else if((parsable[head] == '<' || parsable[head] == '>') && pipev == NO_PIPE){
if (parsable[head] == '<'){// this has a bug that I don't need to fix because the spec changed
pipev = PIPE_IN;
}
else {
pipev = PIPE_OUT;
}
if (tail != head){
if(command == NULL){//the first word is terminated with a pipe rather than a space
command = (char *)malloc(head - tail + 1);
if (strncpy(command,parsable + tail,head-tail) == NULL){return 1;}
command[head-tail] = '\0';
tail = head + 1;//We set the tail ahead of the head so that we don't include the current character
}
else {
int back = head;
for(;parsable[back] == ' ' || parsable[back] == '\t' || parsable[back] == '>'|| parsable[back] == '<';back--){}
back++;
params = (char *)malloc(back - tail + 1);
if (strncpy(params,parsable + tail,back-tail) == NULL){return 1;}
params[back-tail] = '\0';
tail = head + 1;
}
}
}
else if(parsable[head] == '|'){
if (pipev != NO_PIPE){//redirect was found first
for(;parsable[tail] == ' ' || parsable[tail] == '\t';tail++);//no reason to have spaces at head of the redirect file
if (head != tail){//redirect was not the previous character
file = (char *)malloc(head - tail + 1);
if (strncpy(file,parsable + tail,head-tail) == NULL){return 1;}
file[head-tail] = '\0';
}
}
else if(command == NULL){
if(tail != head){//the first word is terminated with a pipe rather than a space
command = (char *)malloc(head - tail + 1);
if (strncpy(command,parsable + tail,head-tail) == NULL){return 1;}
command[head-tail] = '\0';
}
//this "branch" denotes that the pipe is the first character
}
else {// input with params and no redirect or a few other things
int back = head;
for(;parsable[back] == ' ' || parsable[back] == '\t' || parsable[back] == '|';back--){}
back++;
if(tail < back){
params = (char *)malloc(back - tail + 1);
if (strncpy(params,parsable + tail,back-tail) == NULL){return 1;}
params[back-tail] = '\0';
}
}
pipeline = 1;
head++;
tail = head;//Now both head and tail should be set ahead of the pipe
break;
}
}
if(tail != head){
if(parsable[tail] == '<' || parsable[tail] == '>' || parsable[tail] == '|'){tail++;}
for(;tail < strlen(parsable) && (parsable[tail] == ' ' || parsable[tail] == '\t');tail++);
}
if(tail != head && pipeline == 0){
if(pipev == PIPE_IN || pipev == PIPE_OUT){
file = (char *)malloc(head - tail + 1);
if (strncpy(file,parsable + tail,head-tail) == NULL){return 1;}
file[head-tail] = '\0';
tail = head;
}
else if(command == NULL){
command = (char *)malloc(head - tail + 1);
if (strncpy(command,parsable + tail,head-tail) == NULL){return 1;}
command[head-tail] = '\0';
tail = head;
}
else {
int num = head-tail;
params = (char *)malloc(sizeof(char)*(head - tail + 1));
if (strncpy(params,parsable + tail,num) == NULL){return 1;}
params[head-tail] = '\0';
tail = head;
}
}
if(DEBUG == 1){
if(command != NULL){
printf("Command is: %s\n", command);
}
if(params != NULL){
printf("Params are: %s\n", params);
}
if(file != NULL){
printf("File is: %s\n", file);
}
printf("pipev: %d\n",pipev);
printf("pipeline: %d\n",pipeline);
}
int oldredir;
int pipefile;
if(pipev == PIPE_IN){
oldredir = dup(STDIN_FILENO);
if(file != NULL){
pipefile = open(file,O_RDONLY);//add file check
dup2(pipefile,STDIN_FILENO);
close(pipefile);
}
}
else if(pipev == PIPE_OUT){
oldredir = dup(STDOUT_FILENO);
fflush(stdout);
if(file != NULL){
pipefile = open(file,O_WRONLY|O_CREAT,0777);
dup2(pipefile,STDOUT_FILENO);
close(pipefile);
}
else {
dup2(STDIN_FILENO,STDOUT_FILENO);
}
}
if(pipeline == 1){
int oldredir2;
int tubing[2];
pid_t pid;
if (pipe(tubing) < 0) {return 1;}
if ((pid = fork()) < 0) {return 1;}
else if (pid == 0){
close(tubing[1]);
oldredir2 = dup(STDIN_FILENO);
dup2(tubing[0], STDIN_FILENO);
ret = parse_exec(&parsable[head]);
close(tubing[0]);
dup2(oldredir2, STDIN_FILENO);
exit(ret);
}
else {
fflush(stdout);
close(tubing[0]);
oldredir2 = dup(STDOUT_FILENO);
dup2(tubing[1], STDOUT_FILENO);
find_and_exec(command,params);
fflush(stdout);
close(tubing[1]);
dup2(oldredir2, STDOUT_FILENO);
wait(&ret);
}
}
else{
ret = find_and_exec(command,params);
}
if(pipev == PIPE_IN){
dup2(oldredir,STDIN_FILENO);
}
else if(pipev == PIPE_OUT){
fflush(stdout);
dup2(oldredir,STDOUT_FILENO);
}
return ret;
}
int main(int argc, char *argv[], char *envp[]){
root = NULL;
history = NULL;
exit_num = 0;
size_t buff = BUFFSIZE;
int bytes_read;
char * cmd_line;
cmd_line = (char*)malloc(buff + 1);
for(;;){
#ifdef PROMPT
printf("==>");
#endif
bytes_read = getline(&cmd_line,&buff,stdin);//yeah prompt but stupid trailing newline
if (bytes_read == -1){return 1;}
else if(cmd_line[strlen(cmd_line) -1] == '\n'){
cmd_line[strlen(cmd_line) -1] = '\0';
}
history = stack_push(history,cmd_line);
cmd_line = sub_q(cmd_line);
exit_num = parse_exec(cmd_line);
}
printf("goodbye\n");
return 1;
}