-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathread-command.c
1017 lines (944 loc) · 24.2 KB
/
read-command.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
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
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// UCLA CS 111 Lab 1 command reading
#include "command.h"
#include "command-internals.h"
#include <error.h>
#include <stddef.h>
#include <ctype.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#define false 0
#define true 1
typedef short bool;
/* FIXME: You may need to add #include directives, macro definitions,
static function definitions, etc. */
/* FIXME: Define the type 'struct command_stream' here. This should
complete the incomplete type declaration in command.h. */
int cur_shellnum = 1;
void copy (char* src, char* dest, int size)
{
int i;
for (i = 0; i < size; i++)
dest[i] = src[i];
}
char* concat(char* str1, char* str2)
{
if (!str1)
return str2;
if (!str2)
return str1;
int l1 = strlen(str1), l2 = strlen(str2), i;
char* out = malloc(l1+l2);
for (i = 0 ;i < l1; ++i)
out[i] = str1[i];
for ( ;i < l1+l2; ++i)
out[i] = str2[i-l1];
return out;
}
char* spaceclean(char* str)
{
char* out = str;
while (out && *out == ' ') out++;
if (!out)
return 0;
char* end = out + strlen(out) - 1;
while (end && end > out && *end == ' ') end--;
*(end+1) = 0;
return out;
}
typedef struct command_stream
{
struct command_stream* next;
struct command_stream* prev;
command* root;
} command_stream;
typedef enum
{
WORD_TOKEN, //ls foo
SEMICOLON_TOKEN, // ;
PIPE_TOKEN, // |
AND_TOKEN, // &&
OR_TOKEN, // ||
LEFT_PAREN_TOKEN, // (
RIGHT_PAREN_TOKEN, // )
GREATER_TOKEN, // >
LESS_TOKEN, // <
NEWLINE_TOKEN, // \n
NULL_TOKEN, //
} tokentype;
int precedence(tokentype t)
{
switch(t)
{
case SEMICOLON_TOKEN:
return -6;
case PIPE_TOKEN:
return -3;
case AND_TOKEN:
return -4;
case OR_TOKEN:
return -4;
case GREATER_TOKEN:
return -2;
case LESS_TOKEN:
return -2;
default:
return -99999;
}
}
tokentype tokentypeOf(char c)
{
switch(c)
{
case '(':
return LEFT_PAREN_TOKEN;break;
case ')':
return RIGHT_PAREN_TOKEN;break;
case '<':
return LESS_TOKEN;break;
case '>':
return GREATER_TOKEN;break;
case ';':
return SEMICOLON_TOKEN;break;
case '\n':
return NEWLINE_TOKEN;break;
default:
return NULL_TOKEN;
}
}
typedef struct token
{
tokentype t;
char** words;
struct token* next;
struct token* prev;
int sub;
} token;
int find_sp(char* string, int size)
{
int i;
for (i = 0; i < size; i++)
if (string[i] == ' ')
return i;
return -1;
}
int count_sp(char* string, int size)
{
int i, count=0;
for (i = 0; i < size; i++)
if (string[i] == ' ')
count++;
return count? count : 1;
}
void addToken(token** start, token** end, token* temptoken)
{
token* newtoken = malloc(sizeof(token));
newtoken->t = temptoken->t;
newtoken->words = temptoken->words;
newtoken->sub = temptoken->sub;
if (*start == NULL && *end == NULL) //this is 1st element
{
*end = *start = newtoken;
(*end)->prev = (*end)->next = NULL;
}
else if ((*end)->next) //end is not really the end
{
newtoken->prev = *end;
newtoken->next = (*end)->next;
(*end)->next = newtoken;
newtoken->next->prev = newtoken;
}
else
{
newtoken->next = NULL;
newtoken->prev = *end;
(*end)->next = newtoken;
*end = newtoken;
}
}
token* popToken(token** start, token** end)
{
if (!(*end)) return 0;
token* output = *end;
if (*start == *end)
*start = *end = NULL;
else
{
*end = (*end)->prev;
(*end)->next = NULL;
}
return output;
}
void addToken_make(token*** start, token*** end, tokentype t, char* tempchars, int cwp, bool paren) //copy memory, paren is no longer used
{
//first transfer tokchars to a 2-d chararray only if it's relevant
int i;
bool x = paren;
char** list;
int TOT_SIZE, TOT_LINES;
char* t_tokchars = malloc(cwp+1);
for (i=0; i < cwp; i++)
t_tokchars[i] = tempchars[i];
t_tokchars[i] = ' ';
char* tokchars = malloc(cwp+1);
int j=0;
i= 0;
while (i < cwp+1)
{
if (t_tokchars[i] == ' ')
{
tokchars[j++] = ' ';
while (i < cwp+1 && t_tokchars[i] == ' ')
i++;
}
else
tokchars[j++] = t_tokchars[i++];
}
int clistpos;
if (cwp > 0)
{
TOT_SIZE = cwp+1;
TOT_LINES = count_sp(tokchars, TOT_SIZE);
list = (char**) malloc((TOT_LINES+1)*sizeof(char*));
int space = find_sp(tokchars, TOT_SIZE);
clistpos = 0;
int bytespos = 0;
while (space != -1 && bytespos != TOT_SIZE)
{
list[clistpos] = (char*) malloc(space+1);
for (i=0; i <= space; i++)
list[clistpos][i] = tokchars[bytespos+i];
bytespos += space+1;
space = find_sp(tokchars+bytespos, TOT_SIZE-bytespos);
clistpos++;
}
for (i = 0; i < clistpos; i++)
{
char* str = spaceclean(list[i]);
if (!str || *str == 0)
{
list[i] = 0;
break;
}
else
list[i] = str;
}
}
token* newtoken = malloc(sizeof(token));
newtoken->words = list;
newtoken->t = t;
if (t == LEFT_PAREN_TOKEN)
newtoken->sub = ++cur_shellnum;
else if (t == RIGHT_PAREN_TOKEN)
newtoken->sub = cur_shellnum--;
else
newtoken->sub = cur_shellnum;
addToken(*start, *end, newtoken);
}
token* infixToPostfix(token* start)
{
token* final_start = NULL;
token* final_end = NULL;
token* stack_start = NULL;
token* stack_end = NULL;
for (; start != NULL; start = start->next)
if (start->t == WORD_TOKEN) //if operand, then add to final
addToken(&final_start, &final_end, start);
else if (start->t == LEFT_PAREN_TOKEN) //if left paren, then add it to stack
addToken(&stack_start, &stack_end, start);
else if (start->t == RIGHT_PAREN_TOKEN) //if right paren, pop stack until matching left paren
{
while (stack_end && stack_end->t != LEFT_PAREN_TOKEN)
{
token* output = popToken(&stack_start, &stack_end);
addToken(&final_start, &final_end, output);
}
popToken(&stack_start, &stack_end);
}
else if (start->t == SEMICOLON_TOKEN && start->sub == 1)
{
while (stack_end && stack_end->t != LEFT_PAREN_TOKEN)
addToken(&final_start, &final_end, popToken(&stack_start, &stack_end));
token* semitoken = malloc(sizeof(token));
semitoken->t = SEMICOLON_TOKEN;
semitoken->sub = 1;
addToken(&final_start, &final_end, semitoken);
}
else if (stack_end == NULL || precedence(start->t) > precedence(stack_end->t)) //if more important operator than top, then add to stack
addToken(&stack_start, &stack_end, start);
else if (precedence(start->t) < precedence(stack_end->t)) //if less important operator than top, then keep transferring until I'm more important and then add me to stack
{
token* output;
while (stack_end != NULL)
{
if (precedence(start->t) > precedence(stack_end->t))
break;
addToken(&final_start, &final_end, popToken(&stack_start, &stack_end));
}
addToken(&stack_start, &stack_end, start);
}
else if (precedence(start->t) ==precedence(stack_end->t)) //if same, then pop stack and push me to stack
{
addToken(&final_start, &final_end, popToken(&stack_start, &stack_end));
addToken(&stack_start, &stack_end, start);
}
while (stack_end)
addToken(&final_start, &final_end, popToken(&stack_start, &stack_end));
return final_start;
}
enum command_type tokenToCommandType(tokentype t)
{
switch (t)
{
case PIPE_TOKEN:
return PIPE_COMMAND;
case AND_TOKEN:
return AND_COMMAND;
case OR_TOKEN:
return OR_COMMAND;
case SEMICOLON_TOKEN:
return SEQUENCE_COMMAND;
default:
return SIMPLE_COMMAND;
}
}
void addCommand(command** start, command** end, command* newcommand)
{
if (*start == NULL && *end == NULL) //this is 1st element
{
*end = *start = newcommand;
(*end)->prev = (*end)->next = NULL;
}
else
{
newcommand->next = NULL;
newcommand->prev = *end;
(*end)->next = newcommand;
*end = newcommand;
}
}
command* popCommand(command** start, command** end)
{
if (!(*end)) return 0;
command* output = *end;
if (*start == *end)
*start = *end = NULL;
else
{
*end = (*end)->prev;
(*end)->next = NULL;
}
return output;
}
void addStream(command_stream** start, command_stream** end, command_stream* newcommand)
{
if (!(*start) && !(*end)) //this is 1st element
{
*end = *start = newcommand;
(*end)->prev = (*end)->next = NULL;
}
else
{
newcommand->next = NULL;
newcommand->prev = *end;
(*end)->next = newcommand;
*end = newcommand;
}
}
command_stream* evaluatePostfix(token* finalTokenStream)
{
/*
notes on structures and types declared above
typedef enum
{
WORD_TOKEN, //ls foo
SEMICOLON_TOKEN, // ;
PIPE_TOKEN, // |
AND_TOKEN, // &&
OR_TOKEN, // ||
LEFT_PAREN_TOKEN, // ( //not relevant at this point
RIGHT_PAREN_TOKEN, // )//not relevant at this point
GREATER_TOKEN, // >
LESS_TOKEN, // <
NEWLINE_TOKEN, // \n //not relevant at this point
NULL_TOKEN, // //not relevant at this point
} tokentype;
enum command_type
{
AND_COMMAND, // A && B
SEQUENCE_COMMAND, // A ; B
OR_COMMAND, // A || B
PIPE_COMMAND, // A | B
SIMPLE_COMMAND, // a simple command
SUBSHELL_COMMAND, // ( A )
};
typedef struct command_stream
{
struct command_stream* next;
struct command_stream* prev;
struct command* root;
} command_stream;
struct command
{
command* prev;
command* next; //for linked list before tree conversion
enum command_type type;
// Exit status, or -1 if not known (e.g., because it has not exited yet).
int status;
// I/O redirections, or 0 if none.
char *input;
char *output;
union
{
// for AND_COMMAND, SEQUENCE_COMMAND, OR_COMMAND, PIPE_COMMAND:
struct command *command[2];
// for SIMPLE_COMMAND:
char **word;
// for SUBSHELL_COMMAND:
struct command *subshell_command;
} u;
};
*/
command* cmd_start = NULL;
command* cmd_end = NULL;
command_stream* str_start = NULL;
command_stream* str_end = NULL;
command_stream* firststream = malloc(sizeof(command_stream));
addStream(&str_start, &str_end, firststream);
token* curr;
for (curr = finalTokenStream; curr != NULL; curr = curr->next)
if (curr->t == WORD_TOKEN) //add simple command to command stack
{
command* newcommand = malloc(sizeof(command));
newcommand->type = SIMPLE_COMMAND;
newcommand->status = -1;
newcommand->input = newcommand->output = 0;
newcommand->u.word = curr->words;
if (curr->prev && curr->prev->sub > 1 && curr->sub == 1) //if we just ended a subshell, then make what was there a subshell command on itself
{
command* prevcmd = malloc(sizeof(command));
prevcmd->type = SUBSHELL_COMMAND;
prevcmd->input = prevcmd->output = 0;
prevcmd->status = -1;
prevcmd->u.subshell_command = popCommand(&cmd_start, &cmd_end);
addCommand(&cmd_start, &cmd_end, prevcmd);
}
addCommand(&cmd_start, &cmd_end, newcommand);
}
else if (curr->t == SEMICOLON_TOKEN && curr->sub == 1) //stop working on the current stream and instead make a new stream if no subshell, otherwise make a sequence command
{
if (curr->prev && curr->prev->sub > 1) //if we just ended a subshell, then make cmd_start a subshell command on itself
{
command* newcommand = malloc(sizeof(command));
newcommand->type = SUBSHELL_COMMAND;
newcommand->input = newcommand->output = 0;
newcommand->status = -1;
newcommand->u.subshell_command = cmd_start;
cmd_start = newcommand;
}
str_end->root=cmd_start;
cmd_start = cmd_end = NULL;
command_stream* newstream = malloc(sizeof(command_stream));
addStream(&str_start, &str_end, newstream);
}
else if (curr->t == LESS_TOKEN || curr->t == GREATER_TOKEN) //change the input based on what command you are redirecting
{
//remove two last commands and make them a subtree with correct depth level and add subtree back to command list
command* dest = popCommand(&cmd_start, &cmd_end); //where to redirect
command* operation = popCommand(&cmd_start, &cmd_end); //what to do, assume always simple command
command* newcommand = malloc(sizeof(command));
newcommand->type = SIMPLE_COMMAND;
newcommand->status = -1;
if (curr->t == LESS_TOKEN)
{
newcommand->input = concat(dest->u.word[0], dest->u.word[1]);
newcommand->output= operation->output;
}
else
{
newcommand->output= concat(dest->u.word[0], dest->u.word[1]);
newcommand->input = operation->input;
}
newcommand->u.word = operation->u.word;
addCommand(&cmd_start, &cmd_end, newcommand);
}
else //an operation that needs to be made into a tree
{
command* operand2 = popCommand(&cmd_start, &cmd_end), *operand1 = popCommand(&cmd_start, &cmd_end);
if ((curr->prev && curr->prev->sub > 1 && curr->sub == 1) || operand2->type == SEQUENCE_COMMAND) //if we just ended a subshell, then make operand2 a subshell command on itself
{
command* newcommand = malloc(sizeof(command));
newcommand->type = SUBSHELL_COMMAND;
newcommand->input = newcommand->output = 0;
newcommand->status = -1;
newcommand->u.subshell_command = operand2;
operand2 = newcommand;
}
if (operand1->type == SEQUENCE_COMMAND) //convert operand1 into subshell if it was a sequence, as sequences are only possible in subshells
{
command* newcommand = malloc(sizeof(command));
newcommand->type = SUBSHELL_COMMAND;
newcommand->input = newcommand->output = 0;
newcommand->status = -1;
newcommand->u.subshell_command = operand1;
operand1 = newcommand;
}
command* newcommand = malloc(sizeof(command));
newcommand->type = tokenToCommandType(curr->t);
newcommand->status = -1;
newcommand->input = newcommand->output = 0;
newcommand->u.command[0] = operand1;
newcommand->u.command[1] = operand2;
addCommand(&cmd_start, &cmd_end, newcommand);
}
str_end->root=cmd_start;
return str_start;
}
/*bool parenmatch(char* buf)
{
int i, n = strlen(buf), left=0, right=0;
bool cmnt = false;
for (i = 0; i < n; i++)
if (cmnt && buf[i] == '\n')
cmnt = false;
else if (cmnt)
continue;
else if (buf[i] == '#')
cmnt = true;
else if (buf[i] == '(')
left++;
else if (buf[i] == ')')
right++;
return (left==right);
}*/
/*bool redirOrderValid(char* buf)
{
buf = spaceclean(buf);
int i, n=strlen(buf);
bool cmnt = false;
for (i = 0; i < n; i++)
if (cmnt && buf[i] == '\n')
cmnt = false;
else if (cmnt)
continue;
else if (buf[i] == '#')
cmnt = true;
else if (buf[i] == '<' || buf[i] == '>')
if (i == 0 || i == n-1)
return false;
return true;
}*/
/*bool goodNewline(char* buf)
{
buf = spaceclean(buf);
int i, n=strlen(buf), k;
bool cmnt = false;
for (i = 0; i < n; i++)
if (cmnt && buf[i] == '\n')
cmnt = false;
else if (cmnt)
continue;
else if (buf[i] == '#')
cmnt = true;
else if (buf[i] == '\n')
for (k = i; k < n; k++)
if (buf[k] == ' ' || buf[k] == '\t')
continue;
else if (buf[k] == ';')
return false;
else
break;
return true;
}*/
/*bool goodSemicolon(char* buf)
{
int i, n=strlen(buf);
bool cmnt = false;
for (i = 0; i < n; i++)
if (cmnt && buf[i] == '\n')
cmnt = false;
else if (cmnt)
continue;
else if (buf[i] == '#')
cmnt = true;
else if (buf[i] == ';' && (i == 0 || buf[i-1]==';'))
return false;
return true;
}*/
/*bool goodAndor(char* buf)
{
buf = spaceclean(buf);
int i, n=strlen(buf);
bool cmnt = false;
for (i = 0; i < n; i++)
if (cmnt && buf[i] == '\n')
cmnt = false;
else if (cmnt)
continue;
else if (buf[i] == '#')
cmnt = true;
else if (buf[i] == '&')
{
if (i == 0 || i+1 >= n || buf[i+1] != '&' || i+2 >= n || buf[i+2] == '&')
return false;
i++;
}
else if (buf[i] == '|')
{
if (i == 0 || i+1 >= n || buf[i-1] == '\n' || i+2 >= n || buf[i+2] == '|')
return false;
}
return true;
}*/
bool basicSyntax(char* buf)
{
buf = spaceclean(buf);
int i, n = strlen(buf), left=0, right=0, k;
bool cmnt = false;
for (i = 0; i < n; i++)
if (cmnt && buf[i] == '\n')
cmnt = false;
else if (cmnt)
continue;
else if (buf[i] == '#')
cmnt = true;
else if (buf[i] == '(')
left++;
else if (buf[i] == ')')
right++;
else if (buf[i] == '<' || buf[i] == '>')
{
if (i == 0 || i == n-1)
return false;
}
else if (buf[i] == '\n')
for (k = i; k < n; k++)
if (buf[k] == ' ' || buf[k] == '\t')
continue;
else if (buf[k] == ';')
return false;
else
break;
else if (buf[i] == ';' && (i == 0 || buf[i-1]==';'))
return false;
else if (buf[i] == '&')
{
if (i == 0 || i+1 >= n || buf[i+1] != '&' || i+2 >= n || buf[i+2] == '&')
return false;
i++;
}
else if (buf[i] == '|')
{
if (i == 0 || i+1 >= n || buf[i-1] == '\n' || (buf[i+1] == '|' && (i+2 >= n || buf[i+2] == '|')))
return false;
}
return (left==right);
}
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. */
// error (1, 0, "command reading not yet implemented");
// return 0;
//get all input chars to buf
char* buf = malloc(65536);
int size = 65536;
int end_buf = 0;
int i;
int val;
for (; (val = get_next_byte(get_next_byte_argument)) != EOF; )
{
char value = (char) val;
if (end_buf >= size)
{
char* t_buf = malloc(size *= 2);
copy(buf, t_buf, size);
free(buf);
buf = t_buf;
}
buf[end_buf++] = value;
}
buf[end_buf++] = ' ';
//handle errors
if (!basicSyntax(buf))
error(1, 0, "basic syntax error!");
//tokenize buffer
tokentype t = NULL_TOKEN;
char* tokchars = malloc(128);
int cwp = 0; //current word pointer
bool commenting; //true or false: are we commenting?
token* start, *end; //represents linked list of tokens
token** andstart = &start;
token** andend = &end;
start = end = NULL;
for (i = 0; i < end_buf;)
{
char c = buf[i];
if (c == '#')
{
if (t != NULL_TOKEN)
addToken_make(&andstart, &andend, t, tokchars, cwp, 0);
t = NULL_TOKEN;
free(tokchars);
tokchars = malloc(128);
cwp = 0;
commenting = 1;
i++;
}
else if (c == '\n' && commenting)
{
commenting = 0;
i++;
}
else if (commenting)
{
i++;
continue;
}
else if (c == '\r')
continue;
else if (c == '&')
{
if (t != NULL_TOKEN)
addToken_make(&andstart, &andend, t, tokchars, cwp, 0);
t = NULL_TOKEN;
token* andtoken = malloc(sizeof(token));
andtoken->t = AND_TOKEN;
andtoken->sub = cur_shellnum;
addToken(&start, &end, andtoken);
t = NULL_TOKEN;
i += 2;
free(tokchars);
tokchars = malloc(128);
cwp = 0;
}
else if (c == '|')
{
if (t != NULL_TOKEN)
addToken_make(&andstart, &andend, t, tokchars, cwp, 0);
t = NULL_TOKEN;
if (buf[i+1] == '|')
{
token* ortoken = malloc(sizeof(token));
ortoken->t = OR_TOKEN;
ortoken->sub = cur_shellnum;
addToken(&start, &end, ortoken);
t = NULL_TOKEN;
i += 2;
}
else
{
token* pipetoken = malloc(sizeof(token));
pipetoken->t = PIPE_TOKEN;
pipetoken->sub = cur_shellnum;
addToken(&start, &end, pipetoken);
t = NULL_TOKEN;
i += 1;
}
free(tokchars);
tokchars = malloc(128);
cwp = 0;
}
else if (c == '(' || c == ')' || c == '<' || c == '>' || c == ';' || c == '\n')
{
if (t != NULL_TOKEN)
addToken_make(&andstart, &andend, t, tokchars, cwp, 0);
t = NULL_TOKEN;
addToken_make(&andstart, &andend, tokentypeOf(c), 0, 0, (c == '(' || c == ')'));
free(tokchars);
tokchars = malloc(128);
cwp = 0;
i++;
}
else if (c == ' ' || c == '\t' || !c)
{
if (t == WORD_TOKEN)
tokchars[cwp++] = c;
else
{
free(tokchars);
tokchars = malloc(128);
cwp = 0;
t = NULL_TOKEN;
}
i++;
}
else if (c == '!' || c == '%' || c == '+' || c == ',' || c == '-' || c == '.' || c == '/' || c == ':' || c == '@' || c == '^' || c == '_' || ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z') || ('0' <= c && c <= '9'))
{
t = WORD_TOKEN;
tokchars[cwp++] = c;
i++;
}
else
{
int xx = 0;
error (1, 0, "unrecognized character");
}
}
if (t != NULL_TOKEN)
addToken_make(&andstart, &andend, t, tokchars, cwp, 0);
//clean token stream first
token* clean;
token** andclean = &clean;
for (clean = start; clean; clean = clean->next) //start by removing adjacent newlines and checking syntax of semicolon, pipe, and, and or
if (clean->t == NEWLINE_TOKEN)
while (clean->next && clean->next->t == NEWLINE_TOKEN)
{
clean->next = clean->next->next;
if (clean->next) clean->next->prev = clean;
}
else if (clean->t == SEMICOLON_TOKEN || clean->t == PIPE_TOKEN || clean->t == AND_TOKEN || clean->t == OR_TOKEN)
{
if (!clean->prev || clean->prev->t == NEWLINE_TOKEN)
error (1, 0, "illegal semicolon or operator syntax");
}
for (clean = start; clean; clean = clean->next) //then check operator syntax once more
if (clean->t == AND_TOKEN || clean->t == OR_TOKEN || clean->t == PIPE_TOKEN)
{
if (!clean->next || clean->next->t == SEMICOLON_TOKEN || !clean->prev || clean->prev->t == SEMICOLON_TOKEN)
error (1, 0, "illegal operator syntax");
if (clean->next->t == NEWLINE_TOKEN)
{
token* temp = clean->next;
for (; temp && temp->t == NEWLINE_TOKEN; temp = temp->next);
if (!temp || temp->t == SEMICOLON_TOKEN)
error (1, 0, "illegal operator syntax");
}
}
else if (clean->t == LESS_TOKEN || clean->t == GREATER_TOKEN)
if (!clean->prev || !clean->next || clean->prev->t != WORD_TOKEN || clean->next->t != WORD_TOKEN)
error(1, 0, "bad redirection!");
for (clean = start; clean; clean = clean->next) //then get rid of all newlines
{
if (clean->t == NEWLINE_TOKEN)
{
if ((clean->prev != NULL && clean->prev->t != WORD_TOKEN && clean->prev->t != RIGHT_PAREN_TOKEN) || (clean->next != NULL && clean->next->t != WORD_TOKEN && clean->next->t != LEFT_PAREN_TOKEN)) //function as whitespace
{
token* p = clean->prev;
token* n = clean->next;
p->next = n;
if (n) n->prev = p;
continue;
}
else //function as semicolon
clean->t = SEMICOLON_TOKEN;
}
if (clean->t == SEMICOLON_TOKEN)
while (clean->next && clean->next->t == SEMICOLON_TOKEN)
{
clean->next = clean->next->next;
if (clean->next) clean->next->prev = clean;
}
}
if (start && start->t == SEMICOLON_TOKEN)
{
start = start->next;
start->prev = 0;
}
//change token stream from infix to postfix
token* finalTokenStream = infixToPostfix(start);
command_stream* strm = evaluatePostfix(finalTokenStream);
return strm;
}
command_stream_t* streamArrayFromList(command_stream* strm, int* lengthOut)
{
command_stream* cs;
int i;
*lengthOut = 0;
for (cs = strm; (cs = cs->next);)
(*lengthOut)++;
command_stream** array = malloc((*lengthOut) * sizeof(command_stream*));
for (i = 0, cs = strm; i < *lengthOut && cs; i++, cs=cs->next)
array[i] = cs;
return array;
}
char** getInputs(command_t cmd)
{
int i = 0, j = 0;
char** inp = calloc(64, sizeof(char*));
char** left;
char** right;
switch (cmd->type)
{
case AND_COMMAND:
case SEQUENCE_COMMAND:
case OR_COMMAND:
case PIPE_COMMAND:
left = getInputs(cmd->u.command[0]);
right= getInputs(cmd->u.command[1]);
for (; left[i]; i++)
inp[i] = left[i];
for (; right[j]; j++)
inp[i+j] = right[j];
break;
case SIMPLE_COMMAND:
case SUBSHELL_COMMAND:
for (i = 1; cmd->u.word[i]; i++) //i to navigate thru all args
if (cmd->u.word[i][0] != '-')
inp[j++] = cmd->u.word[i]; //j to set actual inputs
if (cmd->input)
inp[j] = cmd->input;
break;
}
return inp;
}
char** getInputs_pub(command_stream_t strm)
{
return getInputs(strm->root);
}
char** getOutputs(command_t cmd)
{
int i = 0, j = 0;
char** out = calloc(64, sizeof(char*));
char** left;
char** right;
switch (cmd->type)
{
case AND_COMMAND:
case SEQUENCE_COMMAND:
case OR_COMMAND:
case PIPE_COMMAND:
left = getOutputs(cmd->u.command[0]);
right= getOutputs(cmd->u.command[1]);
for (; left[i]; i++)
out[i] = left[i];
for (; right[j]; j++)
out[i+j] = right[j];
break;
case SIMPLE_COMMAND:
case SUBSHELL_COMMAND:
if (cmd->output)
out[0] = cmd->output;
}
return out;
}
char** getOutputs_pub(command_stream_t strm)
{
return getOutputs(strm->root);
}
command_t getCmd(command_stream_t strm)
{
return strm->root;
}
bool done = false;
command_t
read_command_stream (command_stream_t s) //this does not correct the previous, only next