-
Notifications
You must be signed in to change notification settings - Fork 0
/
sish.c
459 lines (393 loc) · 12.4 KB
/
sish.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
447
448
449
450
451
452
453
454
455
456
457
458
459
#include <sys/wait.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <stdint.h>
#include <assert.h>
#include <ctype.h>
#define BUFFERSIZE 4096
#define MAX_NUM_TOKENS 4096
#define MAX_NUM_CMDS 4096
#define MAX_NUM_ARGS 4096
#define PROMPT "sish> "
#define true 1
#define false 0
#define READ 0
#define WRITE 1
typedef int8_t bool;
struct HistoryListNode {
char line[BUFFERSIZE];
struct HistoryListNode *next;
};
typedef struct HistoryListNode HistoryListNode;
struct HistoryLinkedList{
HistoryListNode *head;
HistoryListNode *tail;
int capacity;
int size;
};
typedef struct HistoryLinkedList HistoryLinkedList;
struct CMD {
char** args;
bool isPipe;
};
typedef struct CMD CMD;
char *getInput();
char **getTokens(char *input);
void printTokens(char **tokens);
CMD **getCmds(char **tokens);
void freeCmds(CMD **cmds);
void printCmds(CMD **cmds);
void execCmd(CMD *cmd);
void execCmds(CMD **cmds);
void history_add(HistoryLinkedList *history, char *input);
void history_clear(HistoryLinkedList *history);
char *history_get(HistoryLinkedList *history, int offset);
void history_init(HistoryLinkedList *history);
void history_print(HistoryLinkedList *history);
void freeHistory(HistoryLinkedList *history);
// utility functions
void exit_err(char *msg);
void make_fork(pid_t *cpid, char *msg);
void make_pipe(int **pipe_fd, char *msg);
void make_dup2(int fd1, int fd2, char *msg);
bool isInteger(char* number);
bool didEarlyExit = false;
HistoryLinkedList history = {0}; // this is bad practice...
int
main(int argc, char* argv[]) {
history_init(&history);
while(true) {
const char* prompt = PROMPT;
printf("%s", prompt);
char* input = getInput();
//printf("input: %s\n", input);
history_add(&history, input);
char** tokens = getTokens(input);
// printTokens(tokens);
CMD** cmds = getCmds(tokens);
// printCmds(cmds);
// I guess I can do error validation inside of execCmds
execCmds(cmds);
freeCmds(cmds);
free(tokens);
free(input);
if(didEarlyExit){
break;
}
}
freeHistory(&history);
printf("Thank you for using sish!\n");
return EXIT_SUCCESS;
}
char *
getInput() {
size_t bufferSize = BUFFERSIZE;
char *line = (char*)malloc(sizeof(char) * bufferSize);
ssize_t bytesRead;
if ((bytesRead = getline(&line, &bufferSize, stdin)) == -1) {
printf("This is a non fatal error!\n");
}
line[bytesRead - 1] = '\0';
return line;
}
char **
getTokens(char* input) {
int tokenIndex = 0;
char** tokens = (char**)malloc(sizeof(char*) * MAX_NUM_TOKENS);
char *token, *position;
char *delimiters = " ";
token = strtok_r(input, delimiters, &position);
tokens[tokenIndex] = token;
tokenIndex++;
while((token = strtok_r(NULL, delimiters, &position)) != NULL) {
tokens[tokenIndex] = token;
tokenIndex++;
}
tokens[tokenIndex] = NULL;
return tokens;
}
void
printTokens(char **tokens) {
for (int tokenIndex = 0; tokens[tokenIndex] != NULL; tokenIndex++) {
printf("token: %s\n", tokens[tokenIndex]);
}
}
CMD **
getCmds(char **tokens) {
CMD** cmds = (CMD**)malloc(sizeof(CMD*) * MAX_NUM_CMDS);
int tokenIndex = 0;
int cmdIndex = 0;
while (tokens[tokenIndex] != NULL) {
CMD* cmd = (CMD*)malloc(sizeof(CMD));
cmd->args = (char**)malloc(sizeof(char*) * MAX_NUM_ARGS);
int argIndex = 0;
if (strcmp(tokens[tokenIndex], "|") == 0) {
cmd->args[argIndex] = tokens[tokenIndex];
cmd->isPipe = true;
argIndex++;
tokenIndex++;
} else {
cmd->isPipe = false;
while (tokens[tokenIndex] != NULL && strcmp(tokens[tokenIndex], "|") != 0) {
cmd->args[argIndex] = tokens[tokenIndex];
argIndex++;
tokenIndex++;
}
}
cmd->args[argIndex] = NULL;
cmds[cmdIndex] = cmd;
cmdIndex++;
}
cmds[cmdIndex] = NULL;
return cmds;
}
void
freeCmds(CMD** cmds) {
for (int cmdIndex = 0; cmds[cmdIndex] != NULL; cmdIndex++) {
free(cmds[cmdIndex]->args);
}
free(cmds);
}
void
printCmds(CMD** cmds) {
for (int cmdIndex = 0; cmds[cmdIndex] != NULL; cmdIndex++) {
printf("cmd: ");
for (int argIndex = 0; cmds[cmdIndex]->args[argIndex] != NULL; argIndex++) {
printf("%s ", cmds[cmdIndex]->args[argIndex]);
}
printf("\n");
}
}
void
execCmd(CMD* cmd) {
execvp(cmd->args[0], cmd->args);
char errMsg[BUFFERSIZE];
sprintf(errMsg, "failed to execvp on \"%s\"", cmd->args[0]);
exit_err(errMsg);
return;
}
void
execCmds(CMD **cmds) {
// I guess I can validate the commands in here???????
CMD* childrenCmds[BUFFERSIZE];
int numChildren = 0;
int numPipes = 0;
for (int cmdIndex = 0; cmds[cmdIndex] != NULL; cmdIndex++) {
if (cmds[cmdIndex]->isPipe == true) {
numPipes++;
} else {
childrenCmds[numChildren] = cmds[cmdIndex];
numChildren++;
}
}
int **pipes = (int**)malloc(sizeof(int*) * numPipes);
pid_t *cpids = (pid_t*)malloc(sizeof(pid_t) * numChildren);
for (int pipeIndex = 0; pipeIndex < numPipes; pipeIndex++) {
make_pipe(&pipes[pipeIndex], "failed to make pipe");
}
for (int childIndex = 0; childIndex < numChildren; childIndex++) {
CMD* childCmd = childrenCmds[childIndex];
if (strcmp(childCmd->args[0], "exit") == 0) {
didEarlyExit = true;
return;
} else if (strcmp(childCmd->args[0], "cd") == 0) {
int dir = chdir(childCmd->args[1]);
if(dir != 0){
printf("Failed to change directory to %s\n", childCmd -> args[1]);
}
} else if (strcmp(childCmd->args[0], "history") == 0) {
// check for clear and check for offset
bool shouldClear = false;
int offset = -1;
for (int argIndex = 1; childCmd->args[argIndex] != NULL; argIndex++) {
if (strcmp(childCmd->args[argIndex], "-c") == 0) {
shouldClear = true;
} else if (isInteger(childCmd->args[argIndex])) {
offset = atoi(childCmd->args[argIndex]);
} else {
printf("bad arguments supplied to history cmd\n");
//exit(0);
}
}
if (shouldClear) {
history_clear(&history);
} else if (offset != -1) {
char* line = history_get(&history, offset);
history_add(&history, line);
char **tokens = getTokens(line);
CMD **cmds = getCmds(tokens);
execCmds(cmds);
} else{
history_print(&history);
}
} else {
make_fork(&cpids[childIndex], "failed to make fork");
if (cpids[childIndex] == 0) {
// close all pipes that it is not using
// cpid 0 | reads from stdin, writes to fd[0]
// cpid 1 | reads from fd[0], writes to fd[1]
// ...
// cpid n | reads from fd[n-1], writes to stdout
for (int pipeIndex = 0; pipeIndex < numPipes; pipeIndex++) {
if (pipeIndex != childIndex-1 && pipeIndex != childIndex) {
for (int pipeEnd = 0; pipeEnd < 2; pipeEnd++) {
close(pipes[pipeIndex][pipeEnd]);
}
}
}
if (childIndex == 0 && childIndex == numChildren - 1) {
// one child case
execCmd(childCmd);
} else if (childIndex == 0) {
close(pipes[0][READ]); // not reading from pipe to next process
make_dup2(pipes[0][WRITE], STDOUT_FILENO, "failed to make dup2");
close(pipes[0][WRITE]); // close write end bc dup2
execCmd(childCmd);
} else if (childIndex == numChildren - 1) {
close(pipes[childIndex-1][WRITE]); // not writing to pipe from prev process
make_dup2(pipes[childIndex-1][READ], STDIN_FILENO, "failed to make dup2");
close(pipes[childIndex-1][READ]); // close read end bc dup2
execCmd(childCmd);
} else {
close(pipes[childIndex-1][WRITE]); // not writing to pipe from prev process
close(pipes[childIndex][READ]); // not reading from pipe to next process
make_dup2(pipes[childIndex-1][READ], STDIN_FILENO, "failed to make dup2");
make_dup2(pipes[childIndex][WRITE], STDOUT_FILENO, "failed to make dup2");
close(pipes[childIndex-1][READ]); // close read end bc dup2
close(pipes[childIndex][WRITE]); // close write end bc dup2
execCmd(childCmd);
}
printf("This part only happens if they use exit, cd, or history\n");
return;
}
}
}
// close all of the pipes in main process because it doesn't use them
for (int i = 0; i < numPipes; i++) {
for (int pipeEnd = 0; pipeEnd < 2; pipeEnd++) {
close(pipes[i][pipeEnd]);
}
}
// wait for all of the children processes to finish
for (int i = 0; i < numChildren; i++) {
waitpid(cpids[i], NULL, 0);
}
free(cpids);
for (int i = 0; i < numPipes; i++) {
free(pipes[i]);
}
free(pipes);
}
void
history_add(HistoryLinkedList *history, char *input) {
// edge case for empty history
// history of size 1
// history of
// if the size == capacity
// move the head to the next node, delete the old head,
// else if the size is 0 make the head and the tail a new node
//
// else just make a new node attach it to the current tail
// to do the last part just
// make a new tail
// attach the tail to the current tail
HistoryListNode* newTail = (HistoryListNode*)malloc(sizeof(HistoryListNode));
strcpy(newTail->line, input);
newTail->next = NULL;
if (history->size == history->capacity) {
HistoryListNode* oldHead = history->head;
history->head = history->head->next;
free(oldHead);
history->tail->next = newTail;
history->tail = newTail;
} else if (history->size == 0) {
history->head = newTail;
history->tail = newTail;
history->size++;
} else {
history->tail->next = newTail;
history->tail = newTail;
history->size++;
}
}
void
history_clear(HistoryLinkedList *history) {
freeHistory(history);
history_init(history);
}
char *
history_get(HistoryLinkedList *history, int offset) {
if (offset < 0 || offset >= history->size) {
return "";
}
int index = 0;
HistoryListNode *current = history->head;
while (index < offset) {
current = current->next;
index++;
}
return current->line;
}
void
history_print(HistoryLinkedList *history) {
HistoryListNode *current = history->head;
int offset = 0;
while (current != NULL) {
HistoryListNode *next = current->next;
printf("%d:\t %s\n", offset, current->line);
current = next;
offset++;
}
}
void
history_init(HistoryLinkedList *history) {
history->head = NULL;
history->tail = NULL;
history->size = 0;
history->capacity = 100;
assert(history->capacity > 0);
}
void
freeHistory(HistoryLinkedList *history) {
HistoryListNode *current = history->head;
while (current != NULL) {
HistoryListNode *next = current->next;
free(current);
current = next;
}
}
void
exit_err(char* msg) {
perror(msg);
exit(EXIT_FAILURE);
}
void
make_fork(pid_t* cpid, char* msg) {
if ((*cpid = fork()) < 0) {
exit_err(msg);
}
}
void
make_pipe(int** pipe_fd, char* msg) {
*pipe_fd = (int*)malloc(2 * sizeof(int));
if (pipe(*pipe_fd) == - 1) {
exit_err(msg);
}
}
void
make_dup2(int fd1, int fd2, char* msg) {
if ((dup2(fd1, fd2)) == -1) {
exit_err(msg);
}
}
bool
isInteger(char* number) {
for (int i = 0; number[i] != '\0'; i++) {
if (!isdigit(number[i])) {
return false;
}
}
return true;
}