-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
513 lines (491 loc) · 13.3 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
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
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*
* cpl: A shell pipeline cracker
*
* Author: Dr. Phillip Nico
* Department of Computer Science
* California Polytechnic State University
* One Grand Avenue.
* San Luis Obispo, CA 93407 USA
*
* Email: pnico@csc.calpoly.edu
*
* Revision History:
* $Log: main.c,v $
* Revision 1.4 2003-04-15 18:11:34-07 pnico
* Checkpointing before distribution
*
* Revision 1.3 2003-04-12 10:37:22-07 pnico
* added config.h to list of inclusions
*
* Revision 1.2 2003-04-11 08:38:55-07 pnico
* Ready for first release of mice
*
* Revision 1.1 2003-04-10 19:36:24-07 pnico
* Initial revision
*
*
*/
#include "config.h"
#include <sys/types.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <ctype.h>
#include <signal.h>
#include <termios.h>
#include "pipeline.h"
#include "longstr.h"
#include "parser.h"
#include "vssh.h"
//static void printusage(char *name);
static void prompt(char *prompt);
int main(int argc, char *argv[]){
char *line;
pipeline pl;
int run;
//flags
int redo = 0;
int skip = 0;
int isResume = 0;
char *promptstr;
char* buffer;
numExits = 0;
jobIndex = 0;
histIndex = 0;
back = 0;
script = 0;
FILE* in;
/* check for the right number of arguments */
//if ( argc > 2 ) {
// printusage(argv[0]);
// exit(-1);
//}
if (argc >= 2)
{
DIR* dirp;
struct dirent *dp;
int isFound = 0;
if ((dirp = opendir(".")) == NULL)
{
perror("couldn't open path");
return 0;
}
//put files into array and sort their names
while ((dp = readdir(dirp)) != NULL)
{
if (strcmp(dp->d_name, argv[1]) == 0)
{
isFound = 1;
}
}
if (isFound == 0)
{
printf("%s: Command not found\n", argv[1]);
}
else
{
in = fopen(argv[1], "r");
buffer = (char*)malloc(sizeof(in));
script = 1;
}
}
//struct termios saved_term_mode;
struct sigaction action;
action.sa_handler = killHandle; /* set tick to be the handler function */
sigemptyset(&action.sa_mask); /* clear out masked functions */
action.sa_flags = 0; /* no special handling */
struct sigaction action2;
action2.sa_handler = susHandle; /* set tick to be the handler function */
sigemptyset(&action2.sa_mask); /* clear out masked functions */
action2.sa_flags = 0; /* no special handling */
/*
* Use the sigaction function to associate the signal action with SIGALRM.
*/
if (sigaction(SIGINT, &action, NULL) < 0 ) {
fprintf(stderr, "SIGINT\n");
exit(-1);
}
if(sigaction(SIGTSTP, &action2, NULL) < 0) {
fprintf(stderr, "SIGTSTP\n");
exit(-1);
}
//saved_term_mode = set_raw_term_mode();
initHistory();
initJobs();
//strcpy(jobs[1], "hey");
/* set prompt */
promptstr = PROMPT;
run = TRUE;
if (script == 0)
prompt(promptstr);
while ( run )
{
/// if reexecute was not called
if (redo == 0)
{
if (script == 0)
{
if ( NULL == (line = readLongString(stdin)) )
{
if ( feof(stdin) )
run = FALSE;
}
}
else
{
fgets(line, sizeof(in), in);
if (line[0] == '%')
{
line++;
isResume = 1;
}
}
}
else
{
//printf("histInd: %d\n", histIndex);
///if reexecute was called,
///find the correct place in the history
///list to extract the reexecuted command
int temp = histIndex;
temp--;
int ind = 0;
while (ind != reNumber)
{
temp--;
ind++;
}
line = history[temp];
printf("%s\n", line);
}
if (line == NULL)
{
if (feof(stdin))
run = FALSE;
}
else
{
if (line[0] == '%')
{
line++;
isResume = 1;
}
/* We got a line, send it off to the pipeline cracker and
* launch it
*/
//if (redo == 0)
//{
pl = crack_pipeline(line);
//}
redo = 0;
/*
* Show that it worked. This is where you're going to do
* something radically different: rather than printing the
* pipeline, you're going to execute it.
*/
if ( pl != NULL && strlen(pl->cline) > 0)
{
/*
if (pl->cline[0] == '%')
{
int i;
isResume = 1;
pl->stage[0].argv[0][0] = ' ';
for (i = 0; i < strlen(pl->cline)-1; i++)
{
pl->stage[0].argv[0][i] = pl->stage[0].argv[0][i+1];
}
pl->stage[0].argv[0][strlen(pl->cline)-1] = '\0';
}
*/
//print_pipeline(stdout,pl); /* print it. */
addHistory(pl->cline);
///add to the history what was typed into the command line
if (strcmp(pl->stage[0].argv[0], "history") == 0)
{
///print the history if possible
if (pl->stage[0].argc > 1)
{
printf("Junk after built-in command\n");
skip = 1;
}
numExits = 0;
if (skip == 0)
printHistory();
skip = 0;
}
else if (strcmp(pl->stage[0].argv[0], "jobs") == 0)
{
///print the suspended jobs if possible
if (pl->stage[0].argc > 1)
{
printf("Junk after built-in command\n");
skip = 1;
}
numExits = 0;
if (skip == 0)
printJobs();
skip = 0;
}
else if (strcmp(pl->stage[0].argv[0], "exit") == 0)
{
///exit vssh if possible
if (pl->stage[0].argc > 1)
{
fprintf(stderr, "Junk after built-in command\n");
skip = 1;
}
numExits++;
if (skip == 0)
exitCmd();
skip = 0;
}
else if (strcmp(pl->stage[0].argv[0], "bg") == 0)
{
///background the most recently suspended job if possible
if (pl->stage[0].argc > 1)
{
fprintf(stderr, "Junk after built-in command\n");
skip = 1;
}
numExits = 0;
if (skip == 0)
bg(jobs[jobIndex-1]);
skip = 0;
}
else if (strcmp(pl->stage[0].argv[0], "fg") == 0)
{
///foreground the most recently backgrounded job if possible
if (pl->stage[0].argc > 1)
{
fprintf(stderr, "Junk after built-in command\n");
skip = 1;
}
numExits = 0;
if (skip == 0)
fg(jobs[jobIndex-1]);
skip = 0;
}
else if (strcmp(pl->stage[0].argv[0], "cd") == 0)
{
///change the directory of vssh if possible
if (pl->stage[0].argc > 2)
{
fprintf(stderr, "Junk after built-in command\n");
skip = 1;
}
numExits = 0;
if (skip == 0)
cd(pl->stage[0].argv[1]);
skip = 0;
}
else if (isResume == 1)
{
///foreground the specified job if possible
if (pl->stage[0].argc > 1)
{
fprintf(stderr, "Junk after built-in command\n");
skip = 1;
}
numExits = 0;
if (skip == 0)
{
//pl->stage[0].argv[0]++;
int i;
int killFlag = 0;
for (i = 0; i < strlen(pl->stage[0].argv[0]); i++)
{
if (!isdigit(pl->stage[0].argv[0][i]))
{
///if the supplied job number is not a number
///then print error
killFlag = 1;
fprintf(stderr, "%s: No such job.\n", pl->stage[0].argv[0]);
}
}
if (killFlag == 0)
{
if (atoi(pl->stage[0].argv[0]) <= 0 || atoi(pl->stage[0].argv[0]) > jobIndex)
///if the supplied job number is not a valid job index
///then print error
fprintf(stderr, "%s: No such job.\n", pl->stage[0].argv[0]);
else
resume(jobs[atoi(pl->stage[0].argv[0])-1]->id, atoi(pl->stage[0].argv[0])-1);
}
}
skip = 0;
}
else if (strcmp(pl->stage[0].argv[0], "!") == 0)
{
///reexecute the specified history command if possible
if (pl->stage[0].argc > 2 || strlen(pl->stage[0].argv[1]) > 3)
{
fprintf(stderr, "Junk after built-in command\n");
skip = 1;
}
///if the number supplied is not a valid history index
///then print error
if (pl->stage[0].argv[1][0] == '0' && skip == 0)
{
fprintf(stderr, "%s: Event not found\n", pl->stage[0].argv[1]);
skip = 1;
}
if (strlen(history[atoi(pl->stage[0].argv[1])]) <= 0 && skip == 0)
{
fprintf(stderr, "%s: Event not found\n", pl->stage[0].argv[1]);
skip = 1;
}
int i;
for (i = 0; i < strlen(pl->stage[0].argv[1]); i++)
{
if (!isdigit(pl->stage[0].argv[1][i]) && skip == 0)
{
///if the number supplied is not a number
///then print error
fprintf(stderr, "%s: Event not found\n", pl->stage[0].argv[1]);
skip = 1;
}
}
numExits = 0;
if (skip == 0)
{
redo = 1;
reNumber = atoi(pl->stage[0].argv[1]);
//printf("renum: %d\n", reNumber);
//pl = crack_pipeline(history[atoi(pl->stage[0].argv[1])]);
//printf("%s\n", pl->cline);
//lineno++;
//continue;
}
skip = 0;
}
else if (strcmp(pl->stage[0].argv[0], "kill") == 0)
{
///kill the specified job if possible
if (pl->stage[0].argc > 2)
{
fprintf(stderr, "Junk after built-in command\n");
skip = 1;
}
numExits = 0;
if (skip == 0)
{
int i;
int killFlag = 0;
for (i = 0; i < strlen(pl->stage[0].argv[1]); i++)
{
///if the supplied job number is not a number
///then print error
if (!isdigit(pl->stage[0].argv[1][i]))
{
killFlag = 1;
fprintf(stderr, "%s: No such job.\n", pl->stage[0].argv[1]);
}
}
if (killFlag == 0)
{
if (atoi(pl->stage[0].argv[1]) <= 0 || atoi(pl->stage[0].argv[1]) > jobIndex)
///if the supplied job number is not a valid job index
///then print error
fprintf(stderr, "%s: No such job.\n", pl->stage[0].argv[1]);
else
killProcess(atoi(pl->stage[0].argv[1])-1, jobs[atoi(pl->stage[0].argv[1])-1]->id);
}
}
skip = 0;
}
else
{
///pipe/redirect as necessary
numExits = 0;
//int i;
//for (i = 0; i < pl->length; i++)
//{
//else
//{
//handle backgrounding "&"
//find the &
int j;
int andFound = 0;
int pipeFound = 0;
for (j = 1; j < strlen(pl->cline); j++)
{
if (pl->cline[j] == '|')
{
//fprintf(stderr, "Pipelines cannot be backgrounded\n");
pipeFound = 1;
}
else if (pl->cline[j] == '&')
{
//error if & is not the last arg, exec nothing
if (pipeFound == 1)
{
fprintf(stderr, "Pipelines cannot be backgrounded\n");
}
else if (j != strlen(pl->cline) - 1)
{
andFound = 1;
fprintf(stderr, "Junk after '&'.\n");
}
else if (pl->cline[j-1] == ' ')
{
andFound = 1;
pl->cline[j] = '\0';
//backExec(pl->stage[0].argv[0], pl->stage[0].argv, pl->cline);
back = 1;
}
}
}
if (pl->length >= 2)
{
///pipe if the pipline has 2 or more stages
pipeCmd(pl);
}
else if (pl->stage[0].outname != NULL && pl->stage[0].inname != NULL)
{
redirectBoth(pl->stage[0].argv, pl->stage[0].inname, pl->stage[0].outname);
}
else if (pl->stage[0].outname != NULL)
{
///otherwise redirect output if there is an outfile
redirectOutput(pl->stage[0].argv, pl->stage[0].outname, pl->cline);
}
else if (pl->stage[0].inname != NULL)
{
///otherwise redirect input if there is an infile
redirectInput(pl->stage[0].argv, pl->stage[0].inname, pl->cline);
}
else if (andFound == 0)
{
//execute if not a built-in command
execute(pl->stage[0].argv[0], pl->stage[0].argv, pl->cline);
}
back = 0;
//}
//}
}
}
free_pipeline(pl);
fflush(stdout); /* also frees line */
lineno++; /* readLongString trims newlines, so increment it manually */
}
if (run )
{
if (script == 0) /* assuming we still want to run */
prompt(promptstr);
}
}
return 0;
}
static void prompt(char *pr) {
/* If this is an interactive shell, flush the output streams and
* print a prompt
*/
if ( isatty(STDIN_FILENO) && isatty(STDOUT_FILENO) ) {
printf("%s", pr);
fflush(stdout);
}
}
//static void printusage(char *name){
// fprintf(stderr,"usage: %s\n",name);
//}