-
Notifications
You must be signed in to change notification settings - Fork 0
/
dsh-tmp.c
790 lines (674 loc) · 20.8 KB
/
dsh-tmp.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
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
#include <sys/types.h>
#include <termios.h>
#include <unistd.h> /* getpid()*/
#include <signal.h> /* signal name macros, and sig handlers*/
#include <stdlib.h> /* for exit() */
#include <errno.h> /* for errno */
#include <sys/wait.h> /* for WAIT_ANY */
#include <string.h>
#include <fcntl.h>
#include "dsh.h"
int isspace(int c);
/* Keep track of attributes of the shell. */
pid_t shell_pgid;
struct termios shell_tmodes;
int shell_terminal;
int shell_is_interactive;
void init_shell();
void spawn_job(job_t *j, bool fg);
job_t * find_job(pid_t pgid);
int job_is_stopped(job_t *j);
int job_is_completed(job_t *j);
bool free_job(job_t *j);
void eval(job_t *j);
void restore_control(job_t *j);
void wait_for_job(job_t *j);
void put_job_in_foreground (job_t *j, int cont);
void put_job_in_background (job_t *j, int cont);
int find_lowest_index();
job_t *find_prev_job(job_t *j);
/* Initializing the header for the job list. The active jobs are linked into a list. */
job_t *first_job = NULL;
pid_t * job_array;
/*Finds open spot in job_array
returns -1 when array is full
*/
int find_lowest_index(){
int i;
for(i=0; i<20; i++){
if(!job_array[i])
return i;
}
return -1;
}
void remove_and_free(job_t *j){
job_t * prev = find_prev_job(j);
if(!prev){ //must be first job
if(first_job != j)
perror("wrong pgid");
job_t * tmp = first_job;
if (first_job->next) first_job = first_job->next;
free_job(tmp);
return;
}
if (j->next) {
job_t * tmp;
tmp = j->next;
j->next = tmp->next;
free_job(tmp);
}
}
/* Find the prevzjob with the indicated pgid. */
job_t *find_prev_job(job_t *j) {
job_t * tmp = first_job;
while(tmp->next){
if(tmp->next == j){
return tmp;
}
tmp = tmp->next;
}
return NULL;
}
/* Find the job with the indicated pgid. */
job_t *find_job(pid_t pgid) {
job_t *j;
for(j = first_job; j; j = j->next)
if(j->pgid == pgid)
return j;
return NULL;
}
int mark_process_status (pid_t pid, int status) {
job_t *j;
process_t *p;
if (pid > 0) {
/* Update the record for the process. */
for (j = first_job; j; j = j->next)
for (p = j->first_process; p; p = p->next)
if (p->pid == pid) {
p->status = status;
if (WIFSTOPPED(status)) p->stopped = 1;
else {
p->completed = 1;
if (WIFSIGNALED(status))
fprintf (stderr, "%d: Terminated by signal %d.\n", (int) pid, WTERMSIG(p->status));
}
return 0;
}
fprintf (stderr, "No child process %d.\n", pid);
return -1;
}
else if (pid == 0 || errno == ECHILD) return -1;
else {
/* Other weird errors. */
perror ("waitpid");
return -1;
}
}
/* Return true if all processes in the job have stopped or completed. */
int job_is_stopped(job_t *j) {
process_t *p;
for(p = j->first_process; p; p = p->next)
if(!p->completed && !p->stopped)
return 0;
return 1;
}
/* Return true if all processes in the job have completed. */
int job_is_completed(job_t *j) {
process_t *p;
for(p = j->first_process; p; p = p->next)
if(!p->completed)
return 0;
return 1;
}
/* Find the last job. */
job_t *find_last_job() {
job_t *j = first_job;
if(!j) return NULL;
while(j->next != NULL)
j = j->next;
return j;
}
void wait_for_job(job_t *j) {
int status;
pid_t pid;
do
pid = waitpid(WAIT_ANY, &status, WUNTRACED);
while (!mark_process_status(pid, status)&& !job_is_stopped(j)
&& !job_is_completed(j));
}
/* Find the last process in the pipeline (job). */
process_t *find_last_process(job_t *j) {
process_t *p = j->first_process;
if(!p) return NULL;
while(p->next != NULL)
p = p->next;
return p;
}
bool free_job(job_t *j) {
if(!j)
return true;
free(j->commandinfo);
free(j->ifile);
free(j->ofile);
process_t *p;
for(p = j->first_process; p; p = p->next) {
int i;
for(i = 0; i < p->argc; i++)
free(p->argv[i]);
}
free(j);
return true;
}
/* Make sure the shell is running interactively as the foreground job
* before proceeding.
* */
void init_shell() {
/* See if we are running interactively. */
shell_terminal = STDIN_FILENO;
/* isatty test whether a file descriptor referes to a terminal */
shell_is_interactive = isatty(shell_terminal);
if(shell_is_interactive) {
/* Loop until we are in the foreground. */
while(tcgetpgrp(shell_terminal) != (shell_pgid = getpgrp()))
kill(- shell_pgid, SIGTTIN);
/* Ignore interactive and job-control signals. */
/* If tcsetpgrp() is called by a member of a background process
* group in its session, and the calling process is not blocking or
* ignoring SIGTTOU, a SIGTTOU signal is sent to all members of
* this background process group.
*/
signal(SIGTTOU, SIG_IGN);
/* Put ourselves in our own process group. */
shell_pgid = getpid();
if(setpgid(shell_pgid, shell_pgid) < 0) {
perror("Couldn't put the shell in its own process group");
exit(1);
}
/* Grab control of the terminal. */
tcsetpgrp(shell_terminal, shell_pgid);
/* Save default terminal attributes for shell. */
tcgetattr(shell_terminal, &shell_tmodes);
}
}
/* Sends SIGCONT signal to wake up the blocked job */
void continue_job(job_t *j) {
if(kill(-j->pgid, SIGCONT) < 0)
perror("kill(SIGCONT)");
}
/* Spawning a process with job control. fg is true if the
* newly-created process is to be placed in the foreground.
* (This implicitly puts the calling process in the background,
* so watch out for tty I/O after doing this.) pgid is -1 to
* create a new job, in which case the returned pid is also the
* pgid of the new job. Else pgid specifies an existing job's
* pgid: this feature is used to start the second or
* subsequent processes in a pipeline.
* */
void spawn_job(job_t *j, bool fg) {
pid_t pid;
process_t *p;
int mypipe[2], infile, outfile;
int original_input = dup(0);
int original_output = dup(1);
infile = j->mystdin;
outfile = j->mystdout;
if(infile!= STDIN_FILENO) infile = open(j->ifile, O_RDONLY);
if(outfile!= STDOUT_FILENO) outfile =open(j->ofile, O_TRUNC | O_CREAT | O_WRONLY, 0666);
dup2 (infile, 0);
dup2 (outfile, 1);
for(p = j->first_process; p; p = p->next) {
if(p->completed)
continue;
if (p->next) {
if (pipe (mypipe) < 0) {
perror("pipe");
exit (1);
}
outfile = mypipe[1]; //mypide[1] is for writing, [0] for reading
}
else outfile = j->mystdout;
switch (pid = fork()) {
case -1: /* fork failure */
perror("fork");
exit(EXIT_FAILURE);
case 0: /* child */
//printf("Here is the j->pgid %d\n", j->pgid);
if ((int) j->pgid < 0){
// printf("Updating the job_array!\n");
j->pgid = getpid();
int low = find_lowest_index();
job_array[low] = j->pgid;
}
p->pid = 0;
if (!setpgid(0,j->pgid)) if(fg) tcsetpgrp(shell_terminal, j->pgid); // assign the terminal
/* Set the handling for job control signals back to the default. */
signal(SIGTTOU, SIG_DFL);
if(infile != STDIN_FILENO) {
dup2(infile, STDIN_FILENO);
close(infile);
}
if (outfile != STDOUT_FILENO) {
dup2 (outfile, STDOUT_FILENO);
close (outfile);
}
if (j->mystderr != STDERR_FILENO) {
dup2 (j->mystderr, STDERR_FILENO);
close (j->mystderr);
}
execvp (p->argv[0], p->argv);
perror ("execvp");
exit (1);
/* execute the command through exec_ call */
default: /* parent */
/* establish child process group here to avoid race
* conditions. */
p->pid = pid;
if (j->pgid < 0) {
j->pgid = pid;
int low = find_lowest_index();
job_array[low] = j->pgid;
}
setpgid(pid, j->pgid);
}
/* Reset file IOs if necessary */
if (infile != j->mystdin) close (infile);
if (outfile != j->mystdout) close (outfile);
infile = mypipe[0];
}
if(fg) {
wait_for_job (j);
/* Wait for the job to complete */
put_job_in_foreground (j, 0);
}
else {
//wait_for_job (j);
put_job_in_background (j, 0);
}
dup2(original_input, 0);
dup2(original_output, 1);
restore_control(j);
}
void restore_control(job_t *j) {
tcsetpgrp(shell_terminal, shell_pgid);
tcgetattr (shell_terminal, &j->tmodes);
tcsetattr (shell_terminal, TCSADRAIN, &shell_tmodes);
}
bool init_job(job_t *j) {
j->next = NULL;
if(!(j->commandinfo = (char *)malloc(sizeof(char)*MAX_LEN_CMDLINE)))
return false;
j->first_process = NULL;
j->pgid = -1; /* -1 indicates new spawn new job*/
j->notified = false;
j->mystdin = STDIN_FILENO; /* 0 */
j->mystdout = STDOUT_FILENO; /* 1 */
j->mystderr = STDERR_FILENO; /* 2 */
j->bg = false;
j->ifile = NULL;
j->ofile = NULL;
return true;
}
bool init_process(process_t *p) {
p->pid = -1; /* -1 indicates new process */
p->completed = false;
p->stopped = false;
p->status = -1; /* set by waitpid */
p->argc = 0;
p->next = NULL;
if(!(p->argv = (char **)calloc(MAX_ARGS,sizeof(char *)))) return false;
return true;
}
bool readprocessinfo(process_t *p, char *cmd) {
int cmd_pos = 0; /*iterator for command; */
int args_pos = 0; /* iterator for arguments*/
int argc = 0;
while (isspace(cmd[cmd_pos])){++cmd_pos;} /* ignore any spaces */
if(cmd[cmd_pos] == '\0') return true;
while(cmd[cmd_pos] != '\0'){
if(!(p->argv[argc] = (char *)calloc(MAX_LEN_CMDLINE, sizeof(char)))) return false;
while(cmd[cmd_pos] != '\0' && !isspace(cmd[cmd_pos])) p->argv[argc][args_pos++] = cmd[cmd_pos++];
p->argv[argc][args_pos] = '\0';
args_pos = 0;
++argc;
while (isspace(cmd[cmd_pos])) ++cmd_pos; /* ignore any spaces */
}
p->argv[argc] = NULL; /* required for exec_() calls */
p->argc = argc;
return true;
}
bool invokefree(job_t *j, char *msg){
fprintf(stderr, "%s\n",msg);
return free_job(j);
}
/* Prints the active jobs in the list. */
void print_job() {
job_t *j;
process_t *p;
for(j = first_job; j; j = j->next) {
fprintf(stdout, "job: %s\n", j->commandinfo);
for(p = j->first_process; p; p = p->next) {
fprintf(stdout,"cmd: %s\t", p->argv[0]);
int i;
for(i = 1; i < p->argc; i++)
fprintf(stdout, "%s ", p->argv[i]);
fprintf(stdout, "\n");
}
if(j->bg) fprintf(stdout, "Background job\n");
else fprintf(stdout, "Foreground job\n");
if(j->mystdin == INPUT_FD)
fprintf(stdout, "Input file name: %s\n", j->ifile);
if(j->mystdout == OUTPUT_FD)
fprintf(stdout, "Outpt file name: %s\n", j->ofile);
}
}
/* Basic parser that fills the data structures job_t and process_t defined in
* dsh.h. We tried to make the parser flexible but it is not tested
* with arbitrary inputs. Be prepared to hack it for the features
* you may require. The more complicated cases such as parenthesis
* and grouping are not supported. If the parser found some error, it
* will always return NULL.
*
* The parser supports these symbols: <, >, |, &, ;
*/
bool readcmdline(char *msg) {
fprintf(stdout, "%s", msg);
char *cmdline = (char *)calloc(MAX_LEN_CMDLINE, sizeof(char));
if(!cmdline)
return invokefree(NULL, "malloc: no space");
fgets(cmdline, MAX_LEN_CMDLINE, stdin);
/* sequence is true only when the command line contains ; */
bool sequence = false;
/* seq_pos is used for storing the command line before ; */
int seq_pos = 0;
int cmdline_pos = 0; /*iterator for command line; */
while(1) {
job_t *current_job = find_last_job();
int cmd_pos = 0; /* iterator for a command */
int iofile_seek = 0; /*iofile_seek for file */
bool valid_input = true; /* check for valid input */
bool end_of_input = false; /* check for end of input */
/* cmdline is NOOP, i.e., just return with spaces */
while (isspace(cmdline[cmdline_pos])){++cmdline_pos;} /* ignore any spaces */
if(cmdline[cmdline_pos] == '\n' || cmdline[cmdline_pos] == '\0' || feof(stdin))
return false;
char *cmd = (char *)calloc(MAX_LEN_CMDLINE, sizeof(char));
if(!cmd)
return invokefree(NULL,"malloc: no space");
job_t *newjob = (job_t *)malloc(sizeof(job_t));
if(!newjob)
return invokefree(NULL,"malloc: no space");
if(!first_job)
first_job = current_job = newjob;
else {
current_job->next = newjob;
current_job = current_job->next;
}
if(!init_job(current_job))
return invokefree(current_job,"init_job: malloc failed");
process_t *current_process = find_last_process(current_job);
while(cmdline[cmdline_pos] != '\n' && cmdline[cmdline_pos] != '\0') {
switch (cmdline[cmdline_pos]) {
case '<': /* input redirection */
current_job->ifile = (char *) calloc(MAX_LEN_FILENAME, sizeof(char));
if(!current_job->ifile)
return invokefree(current_job,"malloc: no space");
++cmdline_pos;
while (isspace(cmdline[cmdline_pos])){++cmdline_pos;} /* ignore any spaces */
iofile_seek = 0;
while(cmdline[cmdline_pos] != '\0' && !isspace(cmdline[cmdline_pos])){
if(MAX_LEN_FILENAME == iofile_seek)
return invokefree(current_job,"input redirection: file length exceeded");
current_job->ifile[iofile_seek++] = cmdline[cmdline_pos++];
}
current_job->ifile[iofile_seek] = '\0';
current_job->mystdin = INPUT_FD;
while(isspace(cmdline[cmdline_pos])) {
if(cmdline[cmdline_pos] == '\n')
break;
++cmdline_pos;
}
valid_input = false;
break;
case '>': /* output redirection */
current_job->ofile = (char *) calloc(MAX_LEN_FILENAME, sizeof(char));
if(!current_job->ofile)
return invokefree(current_job,"malloc: no space");
++cmdline_pos;
while (isspace(cmdline[cmdline_pos])){++cmdline_pos;} /* ignore any spaces */
iofile_seek = 0;
while(cmdline[cmdline_pos] != '\0' && !isspace(cmdline[cmdline_pos])){
if(MAX_LEN_FILENAME == iofile_seek)
return invokefree(current_job,"input redirection: file length exceeded");
current_job->ofile[iofile_seek++] = cmdline[cmdline_pos++];
}
current_job->ofile[iofile_seek] = '\0';
current_job->mystdout = OUTPUT_FD;
while(isspace(cmdline[cmdline_pos])) {
if(cmdline[cmdline_pos] == '\n')
break;
++cmdline_pos;
}
valid_input = false;
break;
case '|': /* pipeline */
cmd[cmd_pos] = '\0';
process_t *newprocess = (process_t *)malloc(sizeof(process_t));
if(!newprocess)
return invokefree(current_job,"malloc: no space");
if(!init_process(newprocess))
return invokefree(current_job,"init_process: failed");
if(!current_job->first_process)
current_process = current_job->first_process = newprocess;
else {
current_process->next = newprocess;
current_process = current_process->next;
}
if(!readprocessinfo(current_process, cmd))
return invokefree(current_job,"parse cmd: error");
++cmdline_pos;
cmd_pos = 0; /*Reinitialze for new cmd */
valid_input = true;
break;
case '&': /* background job */
current_job->bg = true;
while (isspace(cmdline[cmdline_pos])){++cmdline_pos;} /* ignore any spaces */
if(cmdline[cmdline_pos+1] != '\n' && cmdline[cmdline_pos+1] != '\0')
fprintf(stderr, "reading bg: extra input ignored");
end_of_input = true;
break;
case ';': /* sequence of jobs*/
sequence = true;
strncpy(current_job->commandinfo,cmdline+seq_pos,cmdline_pos-seq_pos);
seq_pos = cmdline_pos + 1;
break;
case '#': /* comment */
end_of_input = true;
break;
default:
if(!valid_input)
return invokefree(current_job,"reading cmdline: could not fathom input");
if(cmd_pos == MAX_LEN_CMDLINE-1)
return invokefree(current_job,"reading cmdline: length exceeds the max limit");
cmd[cmd_pos++] = cmdline[cmdline_pos++];
break;
}
if(end_of_input || sequence)
break;
}
cmd[cmd_pos] = '\0';
process_t *newprocess = (process_t *)malloc(sizeof(process_t));
if(!newprocess)
return invokefree(current_job,"malloc: no space");
if(!init_process(newprocess))
return invokefree(current_job,"init_process: failed");
if(!current_job->first_process)
current_process = current_job->first_process = newprocess;
else {
current_process->next = newprocess;
current_process = current_process->next;
}
if(!readprocessinfo(current_process, cmd))
return invokefree(current_job,"read process info: error");
if(!sequence) {
strncpy(current_job->commandinfo,cmdline+seq_pos,cmdline_pos-seq_pos);
break;
}
sequence = false;
++cmdline_pos;
}
return true;
}
/* Build prompt messaage; Change this to include process ID (pid)*/
char* promptmsg() {
return "dsh$ ";
}
void eval(job_t *j){
pid_t pid;
if(!j) return;
signal (SIGINT, SIG_DFL);
signal (SIGQUIT, SIG_DFL);
signal (SIGTSTP, SIG_DFL);
signal (SIGTTIN, SIG_DFL);
signal (SIGTTOU, SIG_DFL);
signal (SIGCHLD, SIG_DFL);
pid = fork();
if(pid==0){
process_t * process = j->first_process;
int i;
for(i=0; i<process->argc;i++) printf("%s\n", process->argv[i]);
if((execve(process->argv[0], process->argv, NULL)) <0) perror("execv failed");
exit(1);
}
else if(pid>0){
int status;
if( waitpid(pid, &status, 0) <0){
perror("waitpid()");
exit(EXIT_FAILURE);
}
} else{
/* The fork failed. */
perror ("fork");
exit (1);
}
return;
}
void put_job_in_foreground (job_t *j, int cont) {
/* Put the job into the foreground. */
tcsetpgrp (shell_terminal, j->pgid);
/* Send the job a
continue signal, if necessary. */
if (cont)
{
tcsetattr (shell_terminal, TCSADRAIN, &j->tmodes);
if (kill (- j->pgid, SIGCONT) < 0)
perror ("kill (SIGCONT)");
}
wait_for_job (j);
/* Put the shell back in the foreground. */
tcsetpgrp (shell_terminal, shell_pgid);
/* Restore the shell's terminal modes. */
tcgetattr (shell_terminal, &j->tmodes);
tcsetattr (shell_terminal, TCSADRAIN, &shell_tmodes);
}
/* Put a job in the background. If the cont argument is true, send
the process group a SIGCONT signal to wake it up. */
void put_job_in_background (job_t *j, int cont) {
/* Send the job a continue signal, if necessary. */
if (cont)
if (kill (-j->pgid, SIGCONT) < 0)
perror ("kill (SIGCONT)");
}
void change_directory (job_t *j, int cont) {
//change directory
//I know he talked about this one being easy - can't remember what he said to do for it
}
void list_jobs (job_t *j, int cont) {
//print the jobs and their status
int i;
for (i = 0; i < 20; i++) {
if (job_array[i] != 0) {
//printf("Getting in for iter: %d\n", i);
job_t * temp = find_job(job_array[i]);
char* status;
if (temp->first_process->stopped) {
status = "Stopped";
}
else if (temp->first_process->completed) {
status = "Completed";
}
else status = "Running";
//+ is most recently invoked bg job
//- is second most recently invoke bg job
// is for anything else
char* position;
position = " ";
// if (i == 19 && temp->bg) position = "+";
// else {
// if (find_job(job_array[i+2])->bg) {
// }
// }
printf("[%d]%s %s %s\n", i, position, status, temp->commandinfo);
if(temp->first_process->completed){
//printf("Seg faul here");
remove_and_free(temp);
job_array[i]=0;
}
//printf("Got to the end of jobs helper!\n");
}
}
}
int main() {
init_shell();
job_array = (pid_t *) malloc(20*sizeof(pid_t));
while(1) {
if(!readcmdline(promptmsg())) {
if (feof(stdin)) { /* End of file (ctrl-d) */
fflush(stdout);
printf("\n");
exit(EXIT_SUCCESS);
}
continue; /* NOOP; user entered return or spaces with return */
}
/* Only for debugging purposes and to show parser output */
//print_job();
job_t * next_job = first_job;
while(next_job){
if(next_job->pgid ==-1){
int lowest = find_lowest_index();
//printf("%s %d\n","Lowest index: ", lowest);
if(lowest != -1){
bool bg = next_job->bg;
process_t * p = next_job->first_process;
char* cmd = p->argv[0];
if(strcmp (cmd,"cd") == 0){
//printf("%s\n", "found cd");
}
else if (strcmp (cmd, "jobs") == 0) {
list_jobs(next_job, 0);
job_t *tmp = next_job;
if (next_job->next) next_job=next_job->next;
remove_and_free(tmp); //why are we remove and freeing a built in command
}
else if (strcmp(cmd, "fg") == 0) {
}
else if (strcmp(cmd, "bg") == 0) {
}
else { /*If not built-in*/
spawn_job(next_job, !bg);
next_job = next_job->next;
}
break;
}
}
else
next_job = next_job->next;
}
/* You need to loop through jobs list since a command line can contain ;*/
/* Check for built-in commands */
/* If not built-in */
/* If job j runs in foreground */
/* spawn_job(j,true) */
/* else */
/* spawn_job(j,false) */
}
}