-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread-command.c
More file actions
executable file
·848 lines (757 loc) · 25.9 KB
/
read-command.c
File metadata and controls
executable file
·848 lines (757 loc) · 25.9 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
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
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
// David and Russell
// UCLA CS 111 Lab 1 command reading
#include "command.h"
#include "command-internals.h"
#include <error.h>
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <stdbool.h>
#include <string.h>
#define initialSize 5
#define IO_LENGTH 20
/* FIXME: You may need to add #include directives, macro definitions,
static function definitions, etc. */
typedef struct symbol *symbol_t;
typedef struct symbol_header *symbol_header_t;
typedef enum {
COMMAND_SYMBOL, // Regular commands
OR_SYMBOL, // ||
AND_SYMBOL, // &&
PIPE_SYMBOL, // |
LBRACKET_SYMBOL, // (
RBRACKET_SYMBOL, // )
SEQUENCE_SYMBOL, // ;
NEWCOMMAND_SYMBOL, // \n
INPUT_SYMBOL, // <
OUTPUT_SYMBOL // >
} symbol_type;
struct symbol {
symbol_type type;
char *simple_command;
struct symbol *next;
};
typedef struct {
void *data; //data pointer
int dataSize; //the size of the particular data (e.g. sizeof(int))
int length; //number of elements in the container
int allocLength; //the length of memory reserved for this container
} stack;
//Command Node
struct command_node {
command_t root;
command_node_t next;
};
struct command_stream{
command_node_t head;
command_node_t tail;
};
symbol_t newSymbol() {
struct symbol *new = (struct symbol*)malloc(sizeof(struct symbol));
new->simple_command = NULL;
new->next = NULL;
return new;
}
int precedence(symbol_type op){
if(op == SEQUENCE_SYMBOL){
return 1;
}
else if(op == OR_SYMBOL || op == AND_SYMBOL){
return 2;
}
else if(op == PIPE_SYMBOL){
return 3;
}
else{
printf("Error! %d\n", op);
return -1;
}
}
//command_node initalize
void node_init(command_node_t n, command_t command){
n->root=command;
n->next=NULL;
}
//get tail of list
command_node_t getTail(command_node_t head){
command_node_t tail=head;
while(tail->next != NULL) tail=tail->next;
return tail;
}
//add to list
void append(command_node_t tail, command_node_t newNode){
tail->next=newNode;
newNode->next=NULL;
tail=tail->next;
}
void newStack(stack *st, int dataSize){
if (dataSize <= 0) {
error(4, 0, "Bad data size");
}
st->dataSize = dataSize;
st->length = 0;
st->allocLength = initialSize;
st->data = malloc(initialSize * dataSize);
if (st->data == NULL) {
error(5, 0, "st->data = NULL");
}
}
//free stack
void destroyStack(stack *st){
free(st->data);
}
//isempty
bool StackisEmpty(const stack *st){
if(st->length == 0) return true;
return false;
}
//push
void pushStack(stack *st, const void* add){
void *destination;
if(st->length == st->allocLength){
st->allocLength *= 2;
st->data = realloc(st->data, st->allocLength * st->dataSize);
if (st->data == NULL) {
error(5, 0, "st->data = NULL");
}
}
destination = (char *)st->data + st->length * st->dataSize;
memcpy(destination,add,st->dataSize);
st->length++;
}
//pop
void popStack(stack *st, void* top){
const void *src;
if (StackisEmpty(st)) {
error(6, 0, "Empty stack");
}
st->length--;
src = (const char *) st->data + st->length * st->dataSize;
memcpy(top, src, st->dataSize);
}
//view top element (doesnt affect stack)
void topStack(stack *st, void* top){
const void *src;
if (StackisEmpty(st)) {
error(6, 0, "Empty stack");
}
src = (const char *) st->data + (st->length - 1) * st->dataSize;
memcpy(top, src, st->dataSize);
}
/* FIXME: Define the type 'struct command_stream' here. This should
complete the incomplete type declaration in command.h. */
void commandStreamInit(command_stream_t stream){
stream->head = NULL;
stream->tail = NULL;
}
// 0 is false, 1 is true.
int isOperator(symbol_type t) {
if (t == SEQUENCE_SYMBOL || t == AND_SYMBOL || t == OR_SYMBOL || t == PIPE_SYMBOL) {
return 1;
} else {
return 0;
}
}
// goes through an idetnified simple command and checks for input and output redirection and puts it inside the command
int countwrds(char * arr){
char * temp = arr;
int count = 0;
int iwrd = 0;
do switch(*temp){
case '\0':
case ' ':
if(iwrd){
iwrd = 0;
count++;
}
break;
default: iwrd = 1;
} while(*temp++);
return count;
}
void parseSimpCommand(char * parserOutput, char **input, char **output, char ***word){
int pos = 0;
int hasInput = -1; //represents the position of '<'
int inputCount = 0;
int hasOutput = -1; //represents the position of '>'
int outputCount = 0;
while(parserOutput[pos] != '\0'){ //TODO: remember to end strings with '\0'
if(parserOutput[pos] == '<'){
hasInput = pos;
inputCount++;
}
if(parserOutput[pos] == '>'){
hasOutput = pos;
outputCount++;
}
pos = pos + 1;
}
if(inputCount > 1 || outputCount > 1) error(1, 0, "Too many I/O symbols");
if(hasOutput == -1 && hasInput == -1){ //CASE 1: NO I/O REDIRECTION
*input = NULL;
*output = NULL;
int rSize = countwrds(parserOutput) + 1; //calc num of words in the string
*word =(char **) malloc(rSize * sizeof(char *)); //allocate 2nd dim
(*word)[rSize-1] = NULL; //set the NULL at the end of the dim
int i;
char * token;
const char s[2] = " ";
token = strtok(parserOutput,s);
for(i = 0; i < rSize - 1 ; i++){
(*word)[i] = malloc(sizeof(parserOutput));
strcpy((*word)[i], token);
token = strtok(NULL, s);
}
return;
}
if(hasOutput != -1 && hasInput == -1){//CASE 2: Output but no Input
char result_word[pos];
char output_word[pos];
int i;
if(outputCount == 1 && countwrds(parserOutput) == 1) error(1, 0, "pls2");
for(i = 0; i < hasOutput; i++){
result_word[i] = parserOutput[i]; //get result word
}
if(isspace(result_word[hasOutput-1])){ //elim whitespace before > if there is one
result_word[hasOutput - 1] = '\0';
}
else{
result_word[hasOutput] = '\0';
}
if(isspace(parserOutput[hasOutput + 1])){
i = hasOutput + 2; //elim whitespace after > if there is any
}
else{
i = hasOutput + 1;
}
int offset = i;
for(i = offset; i < pos; i++){
output_word[i - offset] = parserOutput[i]; //get output
}
output_word[pos - offset] = '\0';
int rSize = countwrds(result_word) + 1; //calc num of words in the string
*word =(char **) malloc(rSize * sizeof(char *));
(*word)[rSize-1] = NULL;
int j;
char * token;
const char s[2] = " ";
token = strtok(result_word,s);
for(j = 0; j < rSize - 1 ; j++){
(*word)[j] = malloc(sizeof(result_word));
strcpy((*word)[j], token);
token = strtok(NULL, s);
}
*input = NULL;
*output = (char* )malloc(sizeof(output_word));
char *outTok;
outTok=strtok(output_word,s);
strcpy(*output, outTok);
return;
}
if(hasOutput == -1 && hasInput != -1){//CASE 3: Input but no Output
char result_word[pos];
char input_word[pos];
int i;
for(i = 0; i<hasInput; i++){
result_word[i] = parserOutput[i]; //get result word
}
if(isspace(result_word[hasInput-1])){ //elim whitespace before < if there is one
result_word[hasInput - 1] = '\0';
}
else{
result_word[hasInput] = '\0';
}
if(isspace(parserOutput[hasInput + 1])){
i = hasInput + 2; //elim whitespace after < if there is any
}
else{
i = hasInput + 1;
}
int offset = i;
for(i = offset; i < pos; i++){
input_word[i - offset] = parserOutput[i]; //get output
}
input_word[pos - offset] = '\0';
if(countwrds(result_word) == 0) error(1, 0, "No words used");
int rSize = countwrds(result_word) + 1; //calc num of words in the string
*word =(char **) malloc(rSize * sizeof(char *));
(*word)[rSize-1] = NULL;
int j;
char * token;
const char s[2] = " ";
token = strtok(result_word,s);
for(j = 0; j < rSize - 1 ; j++){
(*word)[j] = malloc(sizeof(result_word));
strcpy((*word)[j], token);
token = strtok(NULL, s);
}
*input = (char* )malloc(sizeof(input_word));
char *inTok;
inTok=strtok(input_word,s);
if(inputCount == 1 && countwrds(inTok) < 1) error(1, 0, "pls1");
*output = 0;
strcpy(*input,inTok);
return;
}
if(hasInput != -1 && hasOutput != -1){//CASE 4: has both I/O
//In test cases, Input always comes before Output, so I'm going to assume that's
//the correct syntax.
char result_word[pos];
char input_word[pos];
char output_word[pos];
int i;
for(i = 0; i < hasInput; i++){
result_word[i] = parserOutput[i]; //get result file word
}
if(isspace(result_word[hasInput-1])){ //elim whitespace before < if there is one
result_word[hasInput - 1] = '\0';
}
else{
result_word[hasInput] = '\0';
}
if(isspace(parserOutput[hasInput + 1])){
i = hasInput + 2; //elim whitespace after < if there is any
}
else{
i = hasInput + 1;
}
int offset = i;
for(i = offset; i < hasOutput; i++){
input_word[i - offset] = parserOutput[i]; //get output
}
if(isspace(result_word[hasOutput-1])){ //elim whitespace before > if there is one
input_word[hasOutput - offset -1] = '\0';
}
else{
input_word[hasOutput - offset] = '\0';
}
if(isspace(parserOutput[hasOutput + 1])){
i = hasOutput + 2; //elim whitespace after > if there is any
}
else{
i = hasOutput + 1;
}
offset = i;
for(i = offset; i < pos; i++){
output_word[i - offset] = parserOutput[i]; //get output
}
output_word[pos - offset] = '\0';
int rSize = countwrds(result_word) + 1; //calc num of words in the string
*word =(char **) malloc(rSize * sizeof(char *));
(*word)[rSize-1] = NULL;
int j;
char * token;
const char s[2] = " ";
token = strtok(result_word,s);
for(j = 0; j < rSize - 1 ; j++){
(*word)[j] = malloc(sizeof(result_word));
strcpy((*word)[j], token);
token = strtok(NULL, s);
}
*input = (char* )malloc(sizeof(input_word));
*output = (char* )malloc(sizeof(output_word));
char *outTok;
outTok=strtok(output_word,s);
char *inTok;
inTok=strtok(input_word,s);
if(hasOutput < hasInput) error(1, 0, "This Shouldn't Work");
strcpy(*input,inTok);
strcpy(*output, outTok);
return;
}
printf("No events triggered! Error!\n");
}
//breaks up the simple command from the parser and constructs command
void constructSimpleCommand(command_t com, char * parserOutput){
com->type = SIMPLE_COMMAND;
//printf("%d\n", SIMPLE_COMMAND);
com->status = -1; //TODO: EDIT THIS IN LAB 1B
parseSimpCommand(parserOutput, &(com->input), &(com->output), &(com->u.word));/*TODO: Need to check this*/
}
//combines two commands into result
void combine_commands(command_t right,command_t left ,command_t result, symbol_type op){
if(op == AND_SYMBOL) result->type = AND_COMMAND;
else if(op ==SEQUENCE_SYMBOL) result->type = SEQUENCE_COMMAND;
else if(op == OR_SYMBOL) result->type = OR_COMMAND;
else result->type = PIPE_COMMAND;
result->status = -1; //TODO: for part 1B ( dont worry for 1A)
result->input = NULL;
result->output = NULL;
result->u.command[0] = right;
result->u.command[1] = left;
}
//combine_helper: see d) (1-3) in the psuedocode below in make_command_stream
void combine_helper(stack *opStack, stack *cmdStack, symbol_type *tempOp){
command_t r = (command_t) malloc(sizeof(struct command));
command_t l = (command_t) malloc(sizeof(struct command));;
command_t result = (command_t) malloc(sizeof(struct command));;
popStack(opStack, tempOp);
popStack(cmdStack, l);
popStack(cmdStack, r);
combine_commands(r,l, result, *tempOp);
pushStack(cmdStack, result);
if (!StackisEmpty(opStack)) {
topStack(opStack, tempOp);
}
}
//creates a subshell
void createSubshell(command_t topCommand, command_t subshellCommand){
subshellCommand->type = SUBSHELL_COMMAND;
subshellCommand->status = -1; //TODO: change this in 1B.
subshellCommand->input = NULL;
subshellCommand->output = NULL;
subshellCommand->u.subshell_command = topCommand;
}
//Remember to fix & and *s
void createSymbol(symbol_t *sym, symbol_type type) {
(*sym)->type = type;
symbol_t tempSymbol = newSymbol();
(*sym)->next = tempSymbol;
(*sym) = tempSymbol;
(*sym)->next = NULL;
}
//Helper for turning simple commands into tokens
//Pointer bug, fix later.
void createSimpCommand(symbol_t *sym, int *len, int *maxLen, char **data, int *empty){
if (*len == *maxLen) {
*maxLen += 1;
*data = (char*)realloc(*data, (*maxLen)*sizeof(char));
}
(*data)[*len] = '\0';
(*sym)->simple_command = *data;
createSymbol(sym, COMMAND_SYMBOL);
*len = 0;
*maxLen = initialSize;
*data = (char*)malloc(initialSize * sizeof(char));
*empty = 0;
}
//Helper for parsing the IO given the new updated symbols
char * parseIO(char * io){
char * token = strtok(io," ");
return token;
}
command_stream_t
make_command_stream (int (*get_next_byte) (void *),
void *get_next_byte_argument)
{
/* FIXME: Replace this with your implementation. You may need to
add auxiliary functions and otherwise modify the source code.
You can also use external functions defined in the GNU C Library. */
char currentChar = get_next_byte(get_next_byte_argument);
int commandLength = 0;
int allocLength = initialSize;
char *simpleCommand = (char*)malloc(initialSize * sizeof(char));
int empty = 0; // Tracking whether the last simple command was empty.
// 0 = empty, 1 = command, 2 = subshell
int skip = 0; // Skips the character read at the end of each
// iteration of while: ugly workaround to
// distinguishing | and ||
symbol_t currentSymbol = newSymbol();
symbol_t headSymbol = currentSymbol;
symbol_t tempSymbol;
while (currentChar != EOF) { // Parsing
switch (currentChar) {
case ';': // Assert that there is a non-null
// command prior, or the operation is
// invalid.
if (empty == 0) {
error(2, 0, "Too many operators: ;");
} else if (empty == 1) {
createSimpCommand(¤tSymbol, &commandLength, &allocLength, &simpleCommand, &empty);
} else {
empty = 0;
}
//currentSymbol->type = SEQUENCE_SYMBOL;
createSymbol(¤tSymbol, SEQUENCE_SYMBOL);
break;
case '|':
if (empty == 0) {
error(2, 0, "Too many operators: |");
} else if (empty == 1) {
createSimpCommand(¤tSymbol, &commandLength, &allocLength, &simpleCommand, &empty);
} else {
empty = 0;
} // Check the next character. If it's also a pipe, we
// have an or operator. If it's anything else, it's
// just a pipe.
currentChar = get_next_byte(get_next_byte_argument);
if (currentChar == '|') {
createSymbol(¤tSymbol, OR_SYMBOL);
} else {
createSymbol(¤tSymbol, PIPE_SYMBOL);
skip = 1;
}
break;
case '&':
if (empty == 0) {
error(2, 0, "Too many operators: &");
} else if (empty == 1) {
createSimpCommand(¤tSymbol, &commandLength, &allocLength, &simpleCommand, &empty);
} else {
empty = 0;
}
if (!(get_next_byte(get_next_byte_argument) == '&')) {
error(3, 0, "Too few & symbols");
}
// If the & character isn't followed by another one,
// the operator is invalid.
createSymbol(¤tSymbol, AND_SYMBOL);
break;
case '(':
if (empty == 1) { // There does not necessarily need to
// be a simple command before a '('
createSimpCommand(¤tSymbol, &commandLength, &allocLength, &simpleCommand, &empty);
}
createSymbol(¤tSymbol, LBRACKET_SYMBOL);
break;
case ')':
if (empty == 0) {
error(2, 0, "Too many operators: )");
} else if (empty == 1) {
createSimpCommand(¤tSymbol, &commandLength, &allocLength, &simpleCommand, &empty);
} else {
empty = 0;
}
createSymbol(¤tSymbol, RBRACKET_SYMBOL);
empty = 2;
break;
case '#': // Advance to end of line. No break becase # is
// an effective newline
while (currentChar != '\n') {
currentChar = get_next_byte(get_next_byte_argument);
}
case '\n':
if (empty > 0) {
if (empty == 1) {
createSimpCommand(¤tSymbol, &commandLength, &allocLength, &simpleCommand, &empty);
} else {
empty = 0;
}
// Check the next byte. if it's also a newline, we have a new command.
currentChar = get_next_byte(get_next_byte_argument);
if (currentChar == '\n' || currentChar == '#') {
createSymbol(¤tSymbol, NEWCOMMAND_SYMBOL);
while (currentChar == '\n') {
currentChar = get_next_byte(get_next_byte_argument);
if (currentChar =='#') {
while (currentChar != '\n') {
currentChar = get_next_byte(get_next_byte_argument);
}
}
}
} else if (currentChar == EOF) {
break;
} else { //otherwise, we just have a sequence command.
createSymbol(¤tSymbol, SEQUENCE_SYMBOL);
}
skip = 1;
}
break;
case '`':
error(1, 0, "got a `");
break;
case '<':
if (empty == 0) {
error(2, 0, "Too many operators: <");
} else if (empty == 1) {
createSimpCommand(¤tSymbol, &commandLength, &allocLength, &simpleCommand, &empty);
} else {
empty = 0;
} // Assert that there is a non-null
// command prior or subshell, or the operation is
// invalid.
createSymbol(¤tSymbol, INPUT_SYMBOL);
break;
case '>':
if (empty == 0) {
error(2, 0, "Too many operators: >");
} else if (empty == 1) {
createSimpCommand(¤tSymbol, &commandLength, &allocLength, &simpleCommand, &empty);
} else {
empty = 0;
} // Assert that there is a non-null
// command prior or subshell, or the operation is
// invalid.
createSymbol(¤tSymbol, OUTPUT_SYMBOL);
break;
default: // Making the dangerous assumption that all other
// characters are safe
if (empty == 0 && currentChar != ' ') {
empty = 1;
}
if (commandLength == allocLength) {
allocLength *= 2;
simpleCommand = realloc(simpleCommand, allocLength *
sizeof(char));
}
simpleCommand[commandLength++] = currentChar;
break;
}
if (skip == 0) {
currentChar = get_next_byte(get_next_byte_argument);
} else {
skip = 0;
}
}
//error (1, 0, "command reading not yet implemented");
//return 0;
//TODO: initialize a command_stream linked list
//initialize a command_stream linked list
command_stream_t stream = (command_stream_t)malloc(sizeof(struct command_stream));
commandStreamInit(stream);
/*TODO: Parser from left to right. Create command_node every time
There's a new command
*/
// The output from this function I will denote as parserOutput for now
// Formerly parserOutput, now we use currentSymbol to denote the
// current output node.
currentSymbol = headSymbol;
//INIT STACKS
stack operatorStack;
stack commandStack;
command_node_t currCommandNode = (command_node_t) malloc(sizeof(struct command_node));// init first command node
command_t currCommand = (command_t)malloc(sizeof(struct command));
stream->head = currCommandNode;
stream->tail = currCommandNode; //attach to the stream
newStack(&commandStack,sizeof(struct command));
newStack(&operatorStack,sizeof(symbol_type)); //enums symbols are ints
while(!(currentSymbol->type == COMMAND_SYMBOL && currentSymbol->simple_command == NULL) /*currentSymbol != NULL*/) {
if(currentSymbol->type == NEWCOMMAND_SYMBOL){
//create a new command node, hook the last one to the tail of the stream
if(!StackisEmpty(&operatorStack)){//g)When all words are gone, pop each operator and
//combine them with 2 commands similar to d)
symbol_type * tempOp = (symbol_type *)malloc(sizeof(symbol_type));
topStack(&operatorStack, tempOp);
while(!StackisEmpty(&operatorStack)){
combine_helper(&operatorStack,&commandStack,tempOp);
}
}
popStack(&commandStack, currCommand); // get the command off the stack
//if(currCommand->type ==SUBSHELL_COMMAND) printf("Here's a subshell!\n");
currCommandNode->root = currCommand; //attach the root to the commandNode
stream->tail->next = currCommandNode; //attach to tail
stream->tail = currCommandNode; //update tail
currCommandNode->next = NULL; //update tail's next
//create a new nodes for use;
currCommandNode = (command_node_t) malloc(sizeof(struct command_node));
currCommand = (command_t)malloc(sizeof(struct command));
//clear the stacks
destroyStack(&operatorStack);
destroyStack(&commandStack);
newStack(&commandStack,sizeof(struct command));
newStack(&operatorStack,sizeof(symbol_type)); //enums symbols are ints
} else if(currentSymbol->type == COMMAND_SYMBOL){
//check if this symbol is a newline and the next symbol is a newline
//a)If a simple command, push to a command stack
struct command simpCommand;
constructSimpleCommand(&simpCommand, currentSymbol->simple_command);
//printf("%d\n",simpCommand.type);
pushStack(&commandStack, &simpCommand);
} else if(currentSymbol->type == LBRACKET_SYMBOL){
//b)If it is a "(", push it onto an operator-stack
pushStack(&operatorStack, &(currentSymbol->type));
} else if(isOperator(currentSymbol->type) && StackisEmpty(&operatorStack)){
//c)If it is an Operator and operator stack is empty
// 1)push the operator onto the operator stack
pushStack(&operatorStack, &(currentSymbol->type));
} else if(isOperator(currentSymbol->type) && !StackisEmpty(&operatorStack)){
/* d)If it is an operator and the operator stack is NOT empty
1)Pop all operators with greator or equal precedence off the operator stack
For each popped off Operator, Pop 2 commands off command stack
combine them into a new command
2)Stop when you reach an operator with lower precedence or a "("
3)Push the operator onto the stack
*/
symbol_type * tempOp = (symbol_type *)malloc(sizeof(symbol_type));
topStack(&operatorStack, tempOp);
while(!StackisEmpty(&operatorStack) && *tempOp != LBRACKET_SYMBOL && precedence(*tempOp)>=precedence(currentSymbol->type)){
combine_helper(&operatorStack, &commandStack, tempOp);
}
pushStack(&operatorStack, &(currentSymbol->type));
} else if(currentSymbol->type == RBRACKET_SYMBOL){
// e)If encounter ")", pop operators off the stack
//(for each operator, pop two commands, combine, push back on command stack)
//until you find a matching "(". then create a subshell command by popping
//out 1 command from command stack.
symbol_type * tempOp = (symbol_type *)malloc(sizeof(symbol_type));
topStack(&operatorStack,tempOp);
while(!StackisEmpty(&operatorStack) && *tempOp != LBRACKET_SYMBOL){
combine_helper(&operatorStack,&commandStack,tempOp);
}
//command_t subshellCommand = (command_t) malloc(sizeof(struct command));
command_t topCommand = (command_t) malloc(sizeof(struct command));
command_t subshellCommand = (command_t) malloc(sizeof(struct command));
//struct command subshellCommand;
popStack(&commandStack, topCommand);
popStack(&operatorStack, tempOp);
createSubshell(topCommand, subshellCommand);
//printf("%i\n",subshellCommand.type);
pushStack(&commandStack, subshellCommand);
//command_t temp = (command_t) malloc(sizeof(struct command));
//topStack(&commandStack, temp);
//printf("%i\n",temp->type);
}
else if(currentSymbol->type == INPUT_SYMBOL){
//assign next symbol as the input of the top of the command stack
symbol_t tempSymbol = currentSymbol->next; //get the next symbol
if(tempSymbol == NULL)error(1,0, "Bad Input Error");
if(tempSymbol->simple_command == NULL)error(1,0, "Bad Input Error");
command_t topCommand = (command_t)malloc(sizeof(struct command));
popStack(&commandStack,topCommand);
topCommand->input = (char *)malloc(sizeof(char) * IO_LENGTH);
char * in = parseIO(tempSymbol->simple_command);
strcpy(topCommand->input,in);
pushStack(&commandStack,topCommand);
currentSymbol = currentSymbol->next;
}
else if(currentSymbol->type == OUTPUT_SYMBOL){
// assign the next symbol as the output of the top of the command stack
symbol_t tempSymbol = currentSymbol->next; //get the next symbol
if(tempSymbol == NULL)error(1,0, "Bad Output Error");
if(tempSymbol->simple_command == NULL)error(1,0, "Bad Output Error");
command_t topCommand = (command_t)malloc(sizeof(struct command));
popStack(&commandStack,topCommand);
topCommand->output = (char *) malloc(sizeof(char) * IO_LENGTH);
char * out = parseIO(tempSymbol->simple_command);
strcpy(topCommand->output, out);
pushStack(&commandStack,topCommand);
currentSymbol = currentSymbol->next;
}
//f) Advance to next word (simple command, and, or) go to a)
currentSymbol=currentSymbol->next;
}
//g)When all words are gone, pop each operator and
//combine them with 2 commands similar to d)
if (!StackisEmpty(&operatorStack)) {
symbol_type * tempOp = (symbol_type *)malloc(sizeof(symbol_type));
topStack(&operatorStack, tempOp);
while(!StackisEmpty(&operatorStack)){
combine_helper(&operatorStack,&commandStack,tempOp);
}
}
if(!StackisEmpty(&commandStack)) {
//rootNode is now ready to add to the tree.
command_t rootNode = (command_t)malloc(sizeof(struct command));;
popStack(&commandStack, rootNode);
//TODO: add the rootNode to a command_Node
// printf("%i\n",rootNode->type);
currCommandNode->root = rootNode;
//TODO: add the command_Node to the linked list
stream->tail->next = currCommandNode; //ATTACHES THE LAST COMMAND
currCommandNode->next = NULL; //MARKS THE END
stream->tail=currCommandNode;
}
stream->tail->next = NULL;
//return Command_stream linked list
return stream;
}
command_t
read_command_stream (command_stream_t s)
{
if(s->head != NULL){
command_node_t current = s->head; //get a pointer to a commandNode
s->head = current -> next; //update the stream's head
return current -> root; //return the command on the node
}
return NULL;
}