-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpreter.c
More file actions
261 lines (207 loc) · 5.16 KB
/
Copy pathinterpreter.c
File metadata and controls
261 lines (207 loc) · 5.16 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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h> // for my_cd
#include <sys/stat.h> // for my_mkdir
#include "shellmemory.h"
#include "shell.h"
int MAX_ARGS_SIZE = 7; //Change to 7 for enchanced set
int badcommand(){
printf("%s\n", "Unknown Command");
return 1;
}
int badcommandSet(){
printf("%s\n", "Bad command: set");
return 1;
}
int badcommandCd(){
printf("%s\n", "Bad command: my_cd");
return 1;
}
// For run command only
int badcommandFileDoesNotExist(){
printf("%s\n", "Bad command: File not found");
return 3;
}
int help();
int quit();
int set(char* var, char* value);
int print(char* var);
int run(char* script);
int touch(char* filename);
int cat(char* filename);
int badcommandFileDoesNotExist();
int echo(char* argument);
int my_mkdir(char* directory);
int my_cd(char* directory);
// Interpret commands and their arguments
int interpreter(char* command_args[], int args_size){
int i;
if ( args_size < 1 ){
return badcommand();
}
for ( i=0; i<args_size; i++){ //strip spaces new line etc
command_args[i][strcspn(command_args[i], "\r\n")] = 0;
}
if (strcmp(command_args[0], "help")==0){
//help
if (args_size != 1) return badcommand();
return help();
} else if (strcmp(command_args[0], "quit")==0) {
//quit
if (args_size != 1) return badcommand();
return quit();
} else if (strcmp(command_args[0], "set")==0) {
//set
if ((args_size < 3) || (args_size > 7)) return badcommandSet(); //change to 1 <= x <= 5
int totalLength = 0;
for(int i = 2; i < args_size; i++){
totalLength += strlen(command_args[i]) + 1;
}
char* allocatedString = malloc(totalLength * sizeof(char)); //allocate the string formed from tokens
allocatedString[0] = '\0';
for(int i = 2; i < args_size; i ++){
strcat(allocatedString, command_args[i]);
if (i < args_size - 1) {
strcat(allocatedString, " ");
}
}
return set(command_args[1], allocatedString);
}
else if (strcmp(command_args[0], "echo")==0) {
// echo
if (args_size != 2) return badcommand();
return echo(command_args[1]);
}
else if (strcmp(command_args[0], "my_mkdir")==0) {
// my_mkdir
if (args_size != 2) return badcommand();
return my_mkdir(command_args[1]);
}
else if (strcmp(command_args[0], "my_cd")==0) {
// my_cd
if (args_size != 2) return badcommand();
return my_cd(command_args[1]);
}
else if (strcmp(command_args[0], "print")==0) {
if (args_size != 2) return badcommand();
return print(command_args[1]);
} else if (strcmp(command_args[0], "run")==0) {
if (args_size != 2) return badcommand();
return run(command_args[1]);
} else if (strcmp(command_args[0], "my_ls")==0) {
if (args_size != 1) return badcommand();
return system("ls");
} else if (strcmp(command_args[0], "my_touch")==0) {
if (args_size != 2) return badcommand();
return touch(command_args[1]);
} else if (strcmp(command_args[0], "my_cat")==0) {
if (args_size != 2) return badcommand();
return cat(command_args[1]);
} else {
return badcommand();
}
}
int help(){
char help_string[] = "COMMAND DESCRIPTION\n \
help Displays all the commands\n \
quit Exits / terminates the shell with “Bye!”\n \
set VAR STRING Assigns a value to shell memory\n \
print VAR Displays the STRING assigned to VAR\n \
run SCRIPT.TXT Executes the file SCRIPT.TXT\n ";
printf("%s\n", help_string);
return 0;
}
int quit(){
printf("%s\n", "Bye!");
exit(0);
}
int set(char* var, char* value){
char *link = "=";
char buffer[1000];
strcpy(buffer, var);
strcat(buffer, link);
strcat(buffer, value);
mem_set_value(var, value);
return 0;
}
int echo(char* argument) {
if (argument[0] == '$') {
char* result = mem_get_value(argument + 1);
if (strcmp(result, "Variable does not exist") == 0) {
printf("%s\n", "Variable does not exist");
//return -1; Depending on whats expected
}
else {
printf("%s\n", result);
}
free(result);
}
else {
printf("%s\n", argument);
}
return 0;
}
int my_mkdir(char* directory) {
// 0777 = 111 111 111 for permissions
if(mkdir(directory, 0777) == -1) {
// maybe delegate to error function
printf("%s\n", "Error ");
return 1;
}
return 0;
}
int my_cd(char* directory) {
if (chdir(directory) == -1) {
return badcommandCd();
}
return 0;
}
int print(char* var){
printf("%s\n", mem_get_value(var));
return 0;
}
int run(char* script){
int errCode = 0;
char line[1000];
FILE *p = fopen(script,"rt"); // the program is in a file
if(p == NULL){
return badcommandFileDoesNotExist();
}
fgets(line,999,p);
while(1){
errCode = parseInput(line); // which calls interpreter()
memset(line, 0, sizeof(line));
if(feof(p)){
break;
}
fgets(line,999,p);
}
fclose(p);
return errCode;
}
int touch(char* filename){
FILE *newFile = fopen(filename, "w");
if (newFile == NULL){
printf("%s\n", "Error creating file");
return 1;
}
fclose(newFile);
return 0;
}
int cat(char* filename){
FILE *toRead = fopen(filename, "r");
if (toRead == NULL){
printf("%s\n", "File does not exist");
return 1;
}
char* line = NULL;
size_t len = 0;
ssize_t read;
while((read = getline(&line, &len, toRead)) != -1){
printf("%s", line);
}
free(line);
fclose(toRead);
return 0;
}