-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinterpreter.c
More file actions
169 lines (132 loc) · 3.33 KB
/
interpreter.c
File metadata and controls
169 lines (132 loc) · 3.33 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
/*
============================================================================
Name : interpreter.c
Author :
Version :
Copyright : Your copyright notice
Description : Hello World in C, Ansi-style
============================================================================
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "shellmemory.h"
#include "shell.h"
#include "kernel.h"
#include "ram.h"
#include "memorymanager.h"
// quit command
int quit(void) {
printf("Bye!\n");
exit(0);
}
// help command
int help(void) {
printf("quit Description:Exits\n ");
printf("help Description: Displays all the commands\n");
printf("set VAR STRING Description: assign value to shell memory\n");
printf("print VAR d Description: displays the string assigns to VAR\n");
printf("run SCRIPT.TXT Description: Executes the file SCRIPT.TXT\n");
printf("exec p1 p2 p3 Description: Executes concurrent programs\n");
return 0;
}
// set variables to value command
int set(char *words[]) {
// call add function in shellmemory
int err = add(words[1], words[2]);
return err;
}
// print value command
int print(char *words[]) {
// call printList function in shellmemory
int err = printList(words[1]);
return err;
}
// run script command
int run(char *words[]) {
int err = 0;
char line[1000];
FILE *p;
const char delim[10] = { 10, ' ', 13, 0 };
char * token;
// read file.txt
p = fopen(words[1], "r");
// if the file doesnt exist
if (p == NULL) {
printf("Script not found\n");
return 0;
}
// get the first line
fgets(line, 999, p);
// iterate through the lines till the end
while (!(feof(p))) {
// quit the script when quit line was seen
token = strtok(line, delim);
// when end of file is not quit
if (token == NULL) {
return 0;
}
if ((strcmp(token, "quit")) == 0) {
printf("end of script \n");
return 0;
}
err = parse(line);
if (err != 0) {
fclose(p);
return err;
}
fgets(line, 999, p);
}
// close the file
fclose(p);
return err;
}
int exec(char * words[], int argc) {
int err = 0;
// for each program
for (int i = 1; i < argc; i++) {
char string[50] = "BackingStore/";
char *fileName = strdup(words[i]);
//check if input is valid
FILE *p = fopen(words[i], "r");
if (p == NULL) {
printf("ERROR. %s not found.\n", words[i]);
clearEverything();
return err;
} else { // file opened
strcat(string, fileName);
err = launcher(p, string);
if (err != 0) {
return err;
}
}
}
scheduler();
return err;
}
// interpret the commands
int interpreter(char *words[], int argc) {
int errCode = 0;
if ((strcmp(words[0], "quit")) == 0) {
quit();
} else if ((strcmp(words[0], "help")) == 0) {
errCode = help();
} else if ((strcmp(words[0], "set")) == 0) {
errCode = set(words);
} else if ((strcmp(words[0], "print")) == 0) {
errCode = print(words);
} else if ((strcmp(words[0], "run")) == 0) {
errCode = run(words);
} else if ((strcmp(words[0], "exec")) == 0) {
if (argc <= 1) { // only exec as argument
printf("insufficient arguments");
errCode = 0;
} else {
errCode = exec(words, argc);
}
}
else {
errCode = 2;
}
return errCode;
}