forked from martinliu0/cosc301_proj02
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
284 lines (252 loc) · 7 KB
/
main.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <fcntl.h>
#include <string.h>
#include <ctype.h>
#include <sys/resource.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <poll.h>
#include <signal.h>
#include "list.h"
// Stuff
/*
Basic Algorithm
0) Start Prompt
1) Tokenify the Input Lines
a) Ignore comments (#). Whitespace doesn't matter
b) Semi-colon is the delimeter.
c) Be able to handle any options on commands (-l, -s, etc).
2) Fork on new commands
3)
4) Print to command line:
a) Linked List
b) CPU Resource Statistics (formatted)
i) amount of time program spent executing in user space
ii) amount of time spent executing in kernel space.
*/
char** tokenify(const char *s) {
// input a constant string --> return an array of token strings (that were separated by whitespace)
int count = 0;
char *copy = strdup(s);
char *token;
char **words=malloc(sizeof(char*)*(strlen(s)));
token = strtok(copy ,";");
while (token!=NULL){
//Error case is null...so no redundancy needed
if (strstr(token, "#")==NULL){
//if the line contains a #, ignore the rest of the tokens.
words[count]=strdup(token);
token = strtok(NULL, ";");
count++;
}
else{
if (token[0]=='#'){
break;
}
//slice up to #
char *token_hash=strtok(token, "#");
words[count]=strdup(token_hash);
count++;
break;
}
}
words[count]=NULL;
free(copy);
for (int i=0; i<count; i++){
//printf("Token %i: %s\n", i, words[i]);
//fflush(stdout);
free(words[count]);
}
return words;
}
// parses a single command, returns a pointer to each element
// first element will be the file path
// all following elements will be flags
char** parse_command(const char *s) {
char *str = strdup(s);
int index = 0;
int numWhiteSpaces = 0;
for (int i = 0; i < strlen(s); i++){
if (isspace(s[i])){
numWhiteSpaces++;
}
}
// allocate array of pointers
char **returnArray = (char**) malloc(sizeof(char*) * numWhiteSpaces+5);
// "Go red sox!"
char *token = malloc(sizeof(char));
//char *
token = strtok(str, " \n\t"); // pointer to first word...
while (token != NULL){
token = strdup(token);
returnArray[index] = token;
index++;
//free(token);
token = strtok(NULL, " \n\t");
}
returnArray[index] = NULL;
free(str);
free(token);
return returnArray; // pointer to rv
}
void print_tokens(char *tokens[]) {
int i = 0;
while (tokens[i] != NULL) {
printf("Token %d: %s\n", i+1, tokens[i]);
i++;
}
}
int overhead(char **line, int sequential){
int count =0;
for (int i = 0; line[i]!=NULL; i++){
//count number of terms in a command.
count++;
}
if (count==0){
//if there are no terms, do nothing.
return 0;
}
else{
if (count==1){
//if 1 term, there is a limited number of options.
if (strcmp(line[0], "exit")==0){
return 1;
}
if (strcmp(line[0], "mode")==0){
if (sequential==1){
printf("Mode is Sequential\n");
return 4;
}
else {
printf("Mode is Parallel\n");
return 4;
}
}
if (strcmp(line[0],"sequential")==0){
return 2;
}
if (strcmp(line[0],"parallel")==0){
return 3;
}
}
if (count==2){
//likewise if there is only 2 terms then there is a limited number of options.
if ( strcmp(line[0],"mode")==0 && strcmp(line[1], "s")==0){
return 2;
}
if (strcmp(line[0], "mode")==0 && strcmp(line[1], "p")==0){
return 3;;
}
}
//if (count>2) then this is not an overhead command.
}
return 0;
}
int main(int argc, char **argv) {
//get Input
int sequential = 1;
int count = 0;
printf("Prompt: ");
fflush(stdout);
char line[1024];
for(;fgets(line, 1024, stdin) != NULL; printf("Prompt: ")) {
for (int i =0; i<1024; i++){
if (line[i]=='\n'){
line[i]='\0';
}
}
//tokenify the line. Returns separate commands.
char ** tokens = tokenify(line);
int exit_terminal = 0;
int mode_switch = 0;
pid_t pid;
for (int i=0; tokens[i]!=NULL; i++){
char ** results = parse_command(tokens[i]);
int overhead_command = overhead(results, sequential);
//if (overhead_command==0) do nothing.
if (overhead_command > 0) {
// free
}
if (overhead_command==1){
//exit command issued
exit_terminal = 1;
free(results);
continue;
}
if (overhead_command==2){
//switch to sequential mode
mode_switch = 1;
continue;
}
if (overhead_command==3){
//switch to parallel
mode_switch = 2;
continue;
}
if (overhead_command==4){
//already printed what mode we are in
continue;
}
pid = fork();
if (pid==0){
FILE *fp;
fp = fopen("shell-config", "r");
char holder[20];
// initialize linked list to store paths
struct node *head = NULL;
while (fscanf(fp, "%s/n", holder) != EOF) {
list_insert(holder, &head);
}
fclose(fp);
struct node *iterator = head;
struct stat statresult;
int rv = stat(results[0], &statresult);
//printf("%i\n", rv);
// this loop checks to see if there are any path folders that may contain the command
if (rv < 0){
//printf("RV not negative\n");
//printf("%s\n", iterator -> name);
while (iterator != NULL){
char* name = strdup(iterator -> name);
rv = stat(iterator->name, &statresult);
if (rv < 0){
// not found
continue;
} else {
results[0] = iterator -> name;
}
rv = stat(iterator->name, &statresult);
iterator = iterator -> next;
}
}
list_delete(head);
//printf("results[0]: %s\n", results[0]);
if (execv(results[0],results)<0){
printf("Process entered is wrong.\n");
exit(0);
}
}
if (sequential) {
wait(&pid);
}
}
if (sequential == 0){
wait(&pid);
}
if (exit_terminal==1){
free(tokens);
return 0;
}
if (mode_switch==1){
sequential = 1; //switch to sequential
}
if (mode_switch==2){
sequential = 0; //switch to parallel
}
count ++;
}
return 0;
}