-
Notifications
You must be signed in to change notification settings - Fork 0
/
commander.cpp
359 lines (282 loc) · 6.8 KB
/
commander.cpp
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
// Adrian Barberis
// Commander Process
//Last Edit: Fri MAR 3, 2017
/*----------------------------- PREPROCESSORS -----------------------------*/
#include <sys/types.h>
#include <string.h>
#include <unistd.h>
#include <sys/wait.h>
#include <stdio.h>
#include <iostream>
#define READ_END 0
#define WRITE_END 1
/* ---------------------------- BEGIN MAIN -------------------------------- */
// A return value of 0 == All is Well
// A return value of 1 == something whent wrong
int main(int argc, char** argv)
{
if(argc > 1 || argc < 1)
{
fprintf(stderr, "Error: Invalid argument count!\n");
return 1;
}
//Comander Prcess PIpe
int cmdp[2];
//Store Address of Commander Pipe
char mc0[10], mc1[10];
//Important variables for getline
char* line = NULL;
ssize_t leng = 0;
size_t cap = 0;
//Token storage for strtok() and sscanf()
char* token = 0;
//Keep track of "Initial Line Read" for strtok()
int init = 0;
if(pipe(cmdp))
{
perror("CMNDR: Pipe Creation Failed!");
return 1;
}
//create a string value, to pass the child process, that is the address of pipe
sprintf(mc0, "%d", cmdp[0]);
sprintf(mc1, "%d", cmdp[1]);
pid_t pid = fork();
if(pid < 0)
{
perror("CMNDR: Fork Failed!");
return 1;
}
else if(pid > 0)
{
/* IS PARENT */
//close pipe read end
close(cmdp[READ_END]);
while((leng = getline(&line, &cap, stdin)) > 0)
{
//get intial strtok read
if(init == 0)
{
token = strtok(line, " ");
}
//set init to 1 in order to NOT RE-READ "line" into strtok
init = 1;
switch(*token)
{
case 'S':
{
sleep(2);
//write function identifier to pipe
write(cmdp[WRITE_END], token, strlen(token));
//storage for sscanf
int num = 0;
//argument count check
int argcount = 0;
//Read from stdin and parse
while(token != NULL)
{
//If S has more than 3 arg quit
if(argcount > 3)
{
printf("ERROR: Too many(> 3) Arguments for S()!\nExiting...\n");
return 1;
}
//Tokenize the current line
token = strtok(NULL," ");
if(token != NULL)
{
//convert char read from strtok() into integer
sscanf(token, "%d", &num);
write(cmdp[WRITE_END], &num, sizeof(int));
//Increment argcount for check
argcount++;
}
}
//Reset the inti value in order to read next line
init = 0;
break;
}
case 'B':
{
sleep(2);
//Write function identifier to pipe
write(cmdp[WRITE_END], token, strlen(token));
//storage for sscanf()
int num = 0;
//argument count check
int argcount = 0;
//read in from stdin and parse
while(token != NULL)
{
//If more than 1 argument; quit
if(argcount > 1)
{
printf("ERROR: Too many(> 1) Arguments for B()!\nExiting...\n");
return 1;
}
//Tokenize current line
token = strtok(NULL," ");
if(token != NULL)
{
//convert token read to int
sscanf(token, "%d", &num);
write(cmdp[WRITE_END], &num, sizeof(int));
//increment argcount for check
argcount++;
}
}
//reset init to read next line
init = 0;
break;
}
case 'U':
{
sleep(2);
//write function identifier to pipe
write(cmdp[WRITE_END], token, strlen(token));
//storage for sscanf()
int num = 0;
//argument count check
int argcount = 0;
//read from stdin and parse
while(token != NULL)
{
//if more than 1 arg; quit
if(argcount > 1)
{
printf("ERROR: Too many(> 1) Arguments for U() !\nExiting...\n");
return 1;
}
//tokenize line
token = strtok(NULL," ");
if(token != NULL)
{
//convert current token into integer
sscanf(token, "%d", &num);
write(cmdp[WRITE_END], &num, sizeof(int));
//increment argcount for check
argcount++;
}
}
//reset init to read next line
init = 0;
break;
}
case 'C':
{
sleep(2);
//write function identifier to pipe
write(cmdp[WRITE_END], token, sizeof(char));
//storage for sscanf()
int num = 0;
//read and parse ONLY 2 args
for(int i=0; i<2; i++)
{
//First arg should be a letter i.e. char
if(i == 0 && token != NULL)
{
//tokenize line
token = strtok(NULL," ");
write(cmdp[WRITE_END], token, sizeof(char));
}
else if(i == 1 && token != NULL) //second arg should be number
{
//tokenize line
token = strtok(NULL, " ");
sscanf(token, "%d", &num);
write(cmdp[WRITE_END], &num, sizeof(int));
}
}
//check for argcount > 2
token = strtok(NULL, " ");
if(token != NULL)
{
fprintf(stderr, "ERROR: Too many(> 2) Arguments for C()!\nExiting...\n");
return 1;
}
//reset init for next line
init = 0;
break;
}
case 'P':
{
sleep(2);
//write function identifier to pipe
write(cmdp[WRITE_END], token, sizeof(char));
//if next read token is not NULL then too many args; quit
token = strtok(NULL," ");
if(token != NULL)
{
printf("ERROR: Too many(> 0) Arguments for P()\nExiting...\n");
return 1;
}
//reset init for next line
init = 0;
break;
}
case 'T':
{
sleep(2);
//write function identifier to pipe
write(cmdp[WRITE_END], token, sizeof(char));
//If a token beyond the Identifier is read; too many args, quit
token = strtok(NULL," ");
if(token != NULL)
{
printf("ERROR: Too many(> 0) Arguments for T()\nExiting...\n");
return 1;
}
//reset init for next line
init = 0;
// close the pipe and wait for child
close(cmdp[WRITE_END]);
wait(NULL);
return 0;
}
case 'Q':
{
sleep(2);
//Write function identifier to pipe
write(cmdp[WRITE_END], token, sizeof(char));
//If a token beyond the Identifier is read; too many args, quit
token = strtok(NULL," ");
if(token != NULL)
{
printf("ERROR: Too many(> 0) Arguments for Q()\nExiting...\n");
return 1;
}
//reset init for next line
init = 0;
break;
}
default:
{
printf("CMNDR: Unidentified Command!\nExiting...\n");
close(cmdp[WRITE_END]);
return 1;
}
}
//Reset for getline()
line = NULL;
cap = 0;
}
//close pipe and wait for child
close(cmdp[WRITE_END]);
wait(NULL);
return 0;
}
else
{
/* IS CHILD */
//Execute processManager
size_t stat = execl("procMan", "procMan", mc0, mc1, NULL);
//if execution of processManager fails; quit
if(stat == -1)
{
printf("CMNDR: Error Excuting Process Manager!\nExiting...\n");
return 1;
}
return 0;
}
return 0;
}
/* END MAIN */