-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
87 lines (69 loc) · 2.26 KB
/
Copy pathshell.c
File metadata and controls
87 lines (69 loc) · 2.26 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "interpreter.h"
#include "shellmemory.h"
#include "shell.h"
#define FRAME_STORE_SIZE framesize
#define VARIABLE_STORE_SIZE varmemsize
int MAX_USER_INPUT = 1000;
int parseInput(char ui[]);
int main(int argc, char *argv[]) {
system("rm -rf ./backingStore"); //Clear backingStore if already exists
system("mkdir ./backingStore"); //Create backingStore
printf("%s\n", "Shell v2.0");
printf("Frame Store Size = %d; Variable Store Size = %d\n", FRAME_STORE_SIZE, VARIABLE_STORE_SIZE);
char prompt = '$'; // Shell prompt
char userInput[MAX_USER_INPUT]; // user's input stored here
int errorCode = 0; // zero means no error, default
//init user input
for (int i = 0; i < MAX_USER_INPUT; i++)
userInput[i] = '\0';
//init shell memory
memInit();
while(1) {
if (isatty(fileno(stdin))) printf("%c ",prompt);
char *str = fgets(userInput, MAX_USER_INPUT-1, stdin);
if (feof(stdin)){
freopen("/dev/tty", "r", stdin);
}
if(strlen(userInput) > 0) {
errorCode = parseInput(userInput);
if (errorCode == -1) exit(99); // ignore all other errors
memset(userInput, 0, sizeof(userInput));
}
}
return 0;
}
int parseInput(char *ui) {
char tmp[200];
char *words[100];
memset(words, 0, sizeof(words));
int a = 0;
int b;
int w = 0; // wordID
int errorCode;
for(a = 0; ui[a]==' ' && a < 1000; a++); // skip white spaces
while (a < 1000 && a < strlen(ui) && ui[a] != '\n' && ui[a] != '\0') {
while (ui[a]==' ') a++;
if (ui[a] == '\0') break;
for(b = 0; ui[a]!=';' && ui[a]!='\0' && ui[a]!='\n' && ui[a]!=' ' && a < 1000; a++, b++) tmp[b] = ui[a];
tmp[b] = '\0';
if(strlen(tmp) == 0) continue;
words[w] = strdup(tmp);
if (ui[a]==';') {
w++;
errorCode = interpreter(words, w);
if(errorCode == -1) return errorCode;
a++;
w = 0;
for(; ui[a]==' ' && a < 1000; a++); // skip white spaces
continue;
}
w++;
a++;
}
errorCode = interpreter(words, w);
return errorCode;
}