-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread-command.c
More file actions
840 lines (709 loc) · 24.1 KB
/
read-command.c
File metadata and controls
840 lines (709 loc) · 24.1 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
// UCLA CS 111 Lab 1 command reading
#include "command.h"
#include "command-internals.h"
#include "string.h"
#include "alloc.h"
#include "stack.h"
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
//
/* FIXME: You may need to add #include directives, macro definitions,
static function definitions, etc. */
enum operator_type {
OR,
AND,
PIPE,
SEQUENCE,
INDIRECT,
OUTDIRECT,
CLOSE_PAREN,
NONE,
};
// **************** TODO CHANGE checked_malloc TO CHECKED_MALLOC ***************
const char* COMPLETE_CMD_DELIM_STR = "~";
const command_t OPEN_PAREN_COMMAND = NULL;
/**
* print error message and exit
*/
void
error_and_quit(char* message, int lineNum) {
fprintf(stderr, "%d: %s\n", lineNum, message);
exit(1);
}
/**
* replace all whitespace before ')'
*/
void
replace_whitespace_before_close_paren(char* str) {
if (str) {
int length = strlen(str) - 1;
int i;
for (i = length; i > 0; i--) {
// two consecutive newlines
if (str[i] == ')') {
int j;
// replace all whitespace characters preceeding a close paren with spaces
for (j = i-1; str[j] == '\n' || str[j] == ' ' || str[j] == '\t'; j--) {
str[j] = ' ';
}
i = j + 1;
}
}
}
}
/**
* replace 2 or more newlines with COMPLETE_CMD_DELIM
*/
void
replace_multiple_newlines(char* str) {
if (str) {
int length = strlen(str);
int i;
for (i = 0; i < length; i++) {
// two consecutive newlines
if ((str[i] == '\n') && (i != length-1) && ((str[i+1] == '\n') || str[i+1] == '#')) {
str[i] = COMPLETE_CMD_DELIM_STR[0];
int j = i+1;
if (str[i+1] == '#') {
for (j = i+1; str[j] && str[j] != '\n'; j++)
str[j] = COMPLETE_CMD_DELIM_STR[0];
}
// replace 2nd newline, and trailing whitespace, with delim
for (; str[j] == '\n' || str[j] == ' ' || str[j] == '\t' || str[j] == '#'; j++) {
if (str[j] == '#') {
do {
str[j] = COMPLETE_CMD_DELIM_STR[0];
j++;
} while (str[j] && str[j] != '\n');
j--;
}
else
str[j] = COMPLETE_CMD_DELIM_STR[0];
}
i = j-1;
}
}
}
}
/**
* replace any whitespace after an operator with spaces
*/
void
replace_whitespace_after_op(char* str) {
if (str) {
int length = strlen(str);
int i;
for (i = 0; i < length; i++) {
// ignore comments
if (str[i] == '#') {
do {
i++;
} while (str[i] && str[i] != '\n');
i--;
}
// two consecutive newlines
if (str[i] == '&' || str[i] == '|' || str[i] == '(' || str[i] == ';') {
int j;
for (j = i+1; str[j] == '\n' || str[j] == ' ' || str[j] == '\t'; j++) {
str[j] = ' ';
}
i = j-1;
}
}
}
}
/**
* split up str by COMPLETE_CMD_DELIM and return an array of c-strings
*/
char**
tokenize_complete_cmds(char* str) {
if (str) {
int cmdCount = 1;
int length = strlen(str);
// determine size of char** array
int i = 0;
for (i = 0; i < length; ++i) {
if (str[i] == COMPLETE_CMD_DELIM_STR[0]) {
cmdCount++;
while (str[i] == COMPLETE_CMD_DELIM_STR[0]) {
i++;
}
continue;
}
}
char** cmdArray = checked_malloc((cmdCount + 1) * sizeof(char*));
// to find length later
cmdArray[cmdCount] = NULL;
int cmdIndex;
char* complete_cmd = strtok (str, COMPLETE_CMD_DELIM_STR);
// while there are tokens left, put in cmdArray
for (cmdIndex = 0; complete_cmd != NULL && cmdIndex < cmdCount; cmdIndex++) {
cmdArray[cmdIndex] = complete_cmd;
//printf("%s\n", complete_cmd);
complete_cmd = strtok (NULL, COMPLETE_CMD_DELIM_STR);
}
return cmdArray;
}
else
return NULL;
}
/**
* reads the content of get_next_byte_argument into a c-string
*/
char*
file_to_str(int (*get_next_byte) (void *),
void *get_next_byte_argument) {
fseek(get_next_byte_argument, 0L, SEEK_END);
size_t size = ftell(get_next_byte_argument);
fseek(get_next_byte_argument, 0L, SEEK_SET);
char* toReturn = checked_malloc(size+1);
int c = get_next_byte(get_next_byte_argument);
int i = 0;
while (c != EOF) {
toReturn[i++] = c;
c = get_next_byte(get_next_byte_argument);
}
toReturn[i] = '\0';
i--;
while (toReturn[i] == ' ' || toReturn[i] == '\t' || toReturn[i] == '\n') {
toReturn[i] = '\0';
i--;
}
i = 0;
while (toReturn[i] && (toReturn[i] == '\n' || toReturn[i] == '\t' || toReturn[i] == ' '))
toReturn[i++] = ' ';
return toReturn;
}
/**
* returns true if c is alphanumeric or any of the following: ! % + , - . / : @ ^ _
*/
bool
is_valid_word_char(int c) {
if (isalnum(c) || c == '!' || c == '%' || c == '+' || c == ',' || c == '-'
|| c == '.' || c == '/' || c == ':' || c == '@' || c == '^' || c == '_') {
return true;
}
return false;
}
/**
* validate the input for valid POSIX shell syntax. fail if not
*/
// TODO: what if str is empty (or just whitespace) ?
void
validate(const char* str) {
if (str) {
int index = 0;
int lineNum = 1;
int unmatchedParenCount = 0;
while (str[index] == ' ' || str[index] == '\t' || str[index] == '\n')
index++;
if (str[index] != '(' && !is_valid_word_char(str[index])) {
error_and_quit("Script starts with invalid char", lineNum);
return;
}
bool inWordNow = false;
enum operator_type lastSeenOp = NONE;
while (str[index]) {
if (is_valid_word_char(str[index])) {
if (lastSeenOp == INDIRECT || lastSeenOp == OUTDIRECT) {
error_and_quit("Too many arguments to I/O redirect", lineNum);
return;
} else if (lastSeenOp == CLOSE_PAREN) {
error_and_quit("Too few operators", lineNum);
return;
}
inWordNow = true;
}
else if (str[index] == '&') {
if (!inWordNow) {
error_and_quit("Too few operands", lineNum);
return;
}
if (str[index+1] != '&') {
error_and_quit("Invalid operator found", lineNum);
return;
}
lastSeenOp = AND;
index++;
inWordNow = false;
}
else if (str[index] == '|') {
if (!inWordNow) {
error_and_quit("Too few operands", lineNum);
return;
}
// found OR
if (str[index+1] == '|') {
lastSeenOp = OR;
index++;
}
// found PIPE
else {
lastSeenOp = PIPE;
}
inWordNow = false;
}
else if (str[index] == '<') {
if (!inWordNow) {
error_and_quit("Too few operands", lineNum);
return;
}
if (lastSeenOp == INDIRECT || lastSeenOp == OUTDIRECT) {
error_and_quit("Invalid use of input redirect", lineNum);
return;
}
do {
index++;
} while (str[index] == ' ' || str[index] == '\t');
if (!is_valid_word_char(str[index])) {
error_and_quit("No file specified", lineNum);
return;
}
do {
index++;
} while (is_valid_word_char(str[index]));
/* inWordNow is left as true */
lastSeenOp = INDIRECT;
continue;
}
else if (str[index] == '>') {
if (!inWordNow) {
error_and_quit("Too few operands", lineNum);
return;
}
if (lastSeenOp == OUTDIRECT) {
error_and_quit("Output redirect cannot follow output redirect", lineNum);
return;
}
do {
index++;
} while (str[index] == ' ' || str[index] == '\t');
if (!is_valid_word_char(str[index])) {
error_and_quit("No file specified", lineNum);
return;
}
do {
index++;
} while (is_valid_word_char(str[index]));
lastSeenOp = OUTDIRECT;
continue;
/* inWordNow is left as true */
}
else if (str[index] == ';') {
if (!inWordNow) {
error_and_quit("Too few operands", lineNum);
return;
}
lastSeenOp = SEQUENCE;
inWordNow = false;
}
else if (str[index] == '(') {
if (inWordNow) {
error_and_quit("Too few operators", lineNum);
return;
}
unmatchedParenCount++;
inWordNow = false;
}
else if (str[index] == ')') {
if (!inWordNow) {
error_and_quit("Too few operands", lineNum);
return;
}
unmatchedParenCount--;
if (unmatchedParenCount < 0) {
error_and_quit("Unmatched parentheses", lineNum);
return;
}
lastSeenOp = CLOSE_PAREN;
/* inWordNow is left as true */
}
else if (str[index] == '\t' || str[index] == ' ') {
/* do nothing */
}
else if (str[index] == '#') {
do {
index++;
} while (str[index] && str[index] != '\n');
continue;
}
else if (str[index] == '\\') {
if (str[index+1] != '\n') {
error_and_quit("", lineNum);
}
}
else if (str[index] == '\n') {
// delimeter for complete command
if (inWordNow && str[index+1] == '\n') {
lineNum++;
do {
index++;
if (str[index] == '\n')
lineNum++;
} while (str[index] == '\n' || str[index] == ' ' || str[index] == '\t');
if (str[index] != '(' && !is_valid_word_char(str[index]) && str[index] != '#') {
error_and_quit("Script starts with invalid char", lineNum);
}
inWordNow = false;
lastSeenOp = NONE;
continue;
}
if (inWordNow)
lastSeenOp = SEQUENCE;
inWordNow = false;
lineNum++;
}
// invalid character
else {
error_and_quit("Invalid character found ", lineNum);
return;
}
index++;
}
if (!inWordNow) {
error_and_quit("Too few operands", lineNum);
return;
}
if (unmatchedParenCount > 0) {
error_and_quit("Unmatched parentheses", lineNum);
return;
}
}
//printf("ABSOLUTELY VALID\n");
}
/**
* starting from *index, return next non-whitespace character
*/
int
get_next_nonwhitespace_char(const char* str, int* index) {
int c;
do {
(*index)++;
c = str[(*index)];
} while (c == ' ' || c == '\t');
return c;
}
/**
* return the next word starting from index
*/
char*
get_next_word(const char* str, int* index) {
int wordEndIndex = *index;
while (is_valid_word_char(str[wordEndIndex])) {
wordEndIndex++;
}
//wordEndIndex now just past the word
char* word = checked_malloc((wordEndIndex - (*index)) + 1);
int i;
for (i = *index; i < wordEndIndex; i++)
word[i-(*index)] = str[i];
word[i-(*index)] = '\0';
// skip over whitespace to start of next token
while (str[wordEndIndex] == ' ' || str[wordEndIndex] == '\t') {
wordEndIndex++;
}
*index = wordEndIndex;
return word;
}
/**
* combines first and second into a single command (op)
*/
command_t
combine_command(command_t first, command_t second, command_t op) {
op->u.command[0] = first;
op->u.command[1] = second;
return op;
}
/**
* returns an int corresponding to the precedence of operator
*/
int
precedence(command_t operator) {
if (operator) {
switch (operator->type) {
case SEQUENCE_COMMAND:
return 1;
case AND_COMMAND:
case OR_COMMAND:
return 2;
case PIPE_COMMAND:
return 3;
default:
return 0;
}
}
// add an error() here. "defensive programming"
return -1;
}
/**
* called when an operator is found when building a command tree
*/
void
push_new_operator(command_t op, cmd_stk_t cmdStack, cmd_stk_t opStack) {
if (empty(opStack) || (precedence(op) > precedence(peek(opStack)))) {
push(opStack, op);
} else {
while (peek(opStack) != OPEN_PAREN_COMMAND && precedence(op) <= precedence(peek(opStack))) {
command_t topOperator = pop(opStack);
command_t secondCmd = pop(cmdStack);
command_t firstCmd = pop(cmdStack);
command_t combinedCmd = combine_command(firstCmd, secondCmd, topOperator);
push(cmdStack, combinedCmd);
if (empty(opStack))
break;
}
push(opStack, op);
}
}
/**
* called when a close paren is found when building a command tree
*/
void
handle_close_paren(cmd_stk_t cmdStack, cmd_stk_t opStack) {
command_t topOperator = pop(opStack);
while (topOperator != OPEN_PAREN_COMMAND) {
command_t secondCmd = pop(cmdStack);
command_t firstCmd = pop(cmdStack);
command_t combinedCmd = combine_command(firstCmd, secondCmd, topOperator);
push(cmdStack, combinedCmd);
topOperator = pop(opStack);
}
command_t subshellCmd = checked_malloc(sizeof(struct command));
subshellCmd->type = SUBSHELL_COMMAND;
subshellCmd->input = subshellCmd->output = NULL;
subshellCmd->u.subshell_command = pop(cmdStack);
push(cmdStack, subshellCmd);
}
/**
* given a string corresponding to a valid complete command, build a command tree
*/
command_t
parse_complete_command(const char* str) {
int index = -1;
cmd_stk_t cmdStack = create_stack();
cmd_stk_t opStack = create_stack();
get_next_nonwhitespace_char(str, &index);
while(str[index]) {
//printf("%c\t%d\n", str[index], index);
if (str[index] == '(') {
push(opStack, OPEN_PAREN_COMMAND);
get_next_nonwhitespace_char(str, &index);
}
else if (str[index] == '<') {
get_next_nonwhitespace_char(str, &index);
char *inputFileName = get_next_word(str, &index);
command_t cmd = pop(cmdStack);
cmd->input = inputFileName;
push(cmdStack, cmd);
}
else if (str[index] == '>') {
get_next_nonwhitespace_char(str, &index);
char *outputFileName = get_next_word(str, &index);
command_t cmd = pop(cmdStack);
cmd->output = outputFileName;
push(cmdStack, cmd);
}
// simple command
else if (is_valid_word_char(str[index])) {
char** words = NULL;
int wordCount = 0;
while (is_valid_word_char(str[index])) {
wordCount++;
// +1 to leave space for '\0' at end
words = checked_realloc(words, (wordCount+1)* sizeof(char*));
char* word = get_next_word(str, &index);
words[wordCount-1] = word;
}
words[wordCount] = NULL;
command_t cmd = checked_malloc(sizeof(struct command));
cmd->type = SIMPLE_COMMAND;
cmd->u.word = words;
cmd->status = -1;
cmd->input = NULL;
cmd->output = NULL;
push(cmdStack, cmd);
continue;
}
else if (str[index] == ')') {
handle_close_paren(cmdStack, opStack);
get_next_nonwhitespace_char(str, &index);
continue;
}
else if (str[index] == '&') {
command_t op = checked_malloc(sizeof(struct command));
op->type = AND_COMMAND;
op->status = -1;
op->input = NULL;
op->output = NULL;
push_new_operator(op, cmdStack, opStack);
index++;
get_next_nonwhitespace_char(str, &index);
continue;
}
else if (str[index] == '|') {
command_t op = checked_malloc(sizeof(struct command));
op->type = PIPE_COMMAND;
if (str[index+1] == '|') {
op->type = OR_COMMAND;
index++;
}
op->status = -1;
op->input = NULL;
op->output = NULL;
push_new_operator(op, cmdStack, opStack);
get_next_nonwhitespace_char(str, &index);
continue;
}
else if (str[index] == ';' || str[index] == '\n') {
command_t op = checked_malloc(sizeof(struct command));
op->type = SEQUENCE_COMMAND;
op->status = -1;
op->input = NULL;
op->output = NULL;
push_new_operator(op, cmdStack, opStack);
get_next_nonwhitespace_char(str, &index);
continue;
}
else if (str[index] == '#') {
do {
index++;
} while (str[index] && str[index] != '\n');
//get_next_nonwhitespace_char(str, &index);
continue;
}
}
//combine all operators sitting on opStack
while (!empty(opStack)) {
command_t topOperator = pop(opStack);
command_t secondCmd = pop(cmdStack);
command_t firstCmd = pop(cmdStack);
command_t combinedCmd = combine_command(firstCmd, secondCmd, topOperator);
push(cmdStack, combinedCmd);
}
return pop(cmdStack);
}
/**********************/
/*** Tests ***/
/**********************/
void
test_replace_backslash_newline() {
char testArray[] = "ec\\\n\\\nho test";
printf("TEST REPLACE BACKSLASH NEWLINE\n--------------------------------\n\n");
printf("ORIGINAL COMMAND STRING\n----------------\n");
printf("%s\n", testArray);
//replace_backslash_newline(testArray);
printf("NEW COMMAND STRING\n----------------\n");
printf("%s\n", testArray);
}
void
test_replace_whitespace_after_op() {
//char testArray[] = "a | \n \n \t \n \n b \n\n e || f | \n g";
char testArray[] = "a || b \n\n c && \n d || e | f \n\n\n echo test \n\n";
printf("TEST REPLACE WHITESPACE AFTER OP\n--------------------------------\n\n");
printf("ORIGINAL COMMAND STRING\n----------------\n");
printf("%s\n", testArray);
replace_whitespace_after_op(testArray);
printf("NEW COMMAND STRING\n----------------\n");
printf("%s\n", testArray);
}
void
test_replace_multiple_newlines() {
//char testArray[] = "a |\nb \n\n \n b && c || e | f \n\n\n\n";
char testArray[] = "a || b \n\n c && \n d || e | f \n\n\n echo test \n\n";
printf("TEST REPLACE MULTIPLE NEWLINES\n--------------------------------\n\n");
printf("ORIGINAL COMMAND STRING\n----------------\n");
printf("%s\n", testArray);
replace_whitespace_after_op(testArray);
replace_multiple_newlines(testArray);
printf("NEW COMMAND STRING\n----------------\n");
printf("%s\n", testArray);
}
void
test_tokenize_complete_cmds() {
//char testArray[] = "a || b \n\n c && \n d || \n e | f \n\n\n echo test \n\n";
char testArray[] = "echo test\necho test";
//"true\n\ng++ -c foo.c\n\n: : :\n\n\n\n\ncat < /etc/passwd | tr a-z A-Z | sort -u || echo sort failed!\n\na b<c > d\n\ncat < /etc/passwd | tr a-z A-Z | sort -u > out || echo sort failed!\n\na&&b||\nc &&\n d | e && f|\n\ng<h\n\n# This is a weird example: nobody would ever want to run this.\na<b>c|d<e>f|g<h>i";
printf("TEST TOKENIZE COMPLETE COMMANDS\n--------------------------------\n\n");
printf("ORIGINAL COMMAND STRING\n----------------\n");
printf("%s\n", testArray);
replace_whitespace_after_op(testArray);
replace_multiple_newlines(testArray);
char** newArray = tokenize_complete_cmds(testArray);
printf("COMMAND ARRAY\n----------------\n");
int i;
for (i = 0; newArray[i] != NULL; ++i) {
printf("%s\t\t%d\n", newArray[i], i);
}
}
void
test_validate_fail() {
//THESE SHOULD FAIL - from test-p-bad.sh
char** bad = (char *[]){
"a\t>\t>\t>b",
"a>b<c",
"(\t&&)",
"(\n\t\n)"
};
int i;
for (i = 0; i < 4; ++i) {
printf("--------\n%d\n", i);
validate(bad[i]);
printf("%s\n--------\n\n\n", bad[i]);
}
// THESE SHOULD PASS - from test-p-ok.sh
//char* test = "true\n\ng++ -c foo.c\n\n: : :\n\ncat < /etc/passwd | tr a-z A-Z | sort -u || echo sort failed!\n\na b<c > d\n\ncat < /etc/passwd | tr a-z A-Z | sort -u > out || echo sort failed!\n\na&&b||\n c &&\n d | e && f|\n\ng<h\n\n# This is a weird example: nobody would ever want to run this.\na<b>c|d<e>f|g<h>i";
//char* test = "echo b && #lol\nb";
//char* test3 = "echo b && \n\n\n #g;e \necho test";
//char* test = "a || \n\n b";
// TODO-TUAN
//char* test2 = "a \n \n b";
//validate(test);
}
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. */
//printf("at the beginning\n\n");
// convert file to c-string
char* scriptStr = file_to_str(get_next_byte, get_next_byte_argument);
replace_whitespace_before_close_paren(scriptStr);
/* validation */
validate(scriptStr);
//printf("after validation\n\n");
/* cleanup a little bit before splitting by \n\n */
replace_whitespace_after_op(scriptStr);
replace_multiple_newlines(scriptStr);
// printf("after cleanup\n\n");
// printf("%s\n", scriptStr);
/* split by \n\n and put into array */
char** completeCmds = tokenize_complete_cmds(scriptStr);
// printf("after tokenizing\n\n");
// printf("COMMAND ARRAY\n----------------\n");
int j;
for (j = 0; completeCmds[j] != NULL; j++) {
// printf("%s\t\t%d\n", completeCmds[j], j);
}
command_stream_t commandStream = create_stack();
//printf("created command stream\n\n");
/* loop through the array, making each a command tree */
int i;
for (i = 0; i < j; ++i) {
// printf("building command %d\n\n", i);
push_back(commandStream, parse_complete_command(completeCmds[i]));
}
return commandStream;
////error (1, 0, "command reading not yet implemented");
}
command_t
read_command_stream (command_stream_t s)
{
if (!empty(s)) {
return pop(s);
}
//error (1, 0, "command reading not yet implemented");
return NULL;
}