-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpipe.c
919 lines (795 loc) · 19.8 KB
/
pipe.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
#include <errno.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>
#include <glob.h>
#include "mark.h"
#include "alias.h"
#include "util.h"
#include "jobs.h"
/*
* parser specific macros
*/
#define IS_ESCAPE(c) ((c) == '\\')
#define IS_CHILD_DELIM(c) ((c) == ' ')
#define IS_BG_DELIM(c) ((c) == '&')
#define IS_PIPE_DELIM(c) ((c) == '|')
#define IS_PARENT_DELIM(c) ((c)=='|' || (c)==';' || (c)=='&')
#define IS_GRAND_DELIM(c) ((c)=='\n')
#define IS_REDIR_DELIM(c) ((c) == '>')
#define IS_REDIRTOFD_DELIM(inp) (*(inp)=='&' && *((inp)-1)=='>')
#define IS_REDIRAPPEND_DELIM(inp) (*(inp)=='>' && *((inp)-1)=='>')
#define IS_QUOTE(c) ((c)=='\'' || (c)=='\"')
#define IS_SUBSHELL_OPEN(c) ((c) == '(')
#define IS_SUBSHELL_CLOSE(c) ((c) == ')')
#define TERM_DELIM_INIT {'|', ';', '&', '\n', '\0'}
#define SS_DELIM_INIT {'(', ')'}
#define CHILD_DELIM ' '
#define SUBSHELL_OPEN '('
#define SUBSHELL_CLOSE ')'
#define ESCAPE '\\'
#define GRAND_DELIM '\n'
#define PIPE '|'
/*
* flags for parse_cmd
*/
#define PARSE_DONTFILLCMD 1 /* dont fill cmd array */
#define PARSE_WHOLELINE 2 /* parse till GRAND_DELIM only */
#define PARSE_DONTPRINT 4 /* parser wont print to stdout */
/*
* parse_cmd return flags
*/
#define PARSERET_SUBSHELL 1
#define PARSERET_UNCLOSED 2
#define PARSERET_SYNERR 4
/*
* preproc_cmdline flags
*/
#define PREPRO_CREND 1 /* append '\n' at end */
#define PREPRO_NULLBEG 2 /* add '\0' before start */
/* fork_and_exec flags */
#define FE_REDIR 1 /* redirection */
#define FE_PP 2 /* post_process cmdline */
#define FE_UBLK 4 /* signal unblock */
#define FE_SETPG 8 /* set process group */
#define FE_SETTERM 16 /* set terminal for process */
#define FE_ALL (FE_REDIR|FE_PP|FE_UBLK|FE_SETPG|FE_SETTERM)
/*
* this flags as of now is only set to signal if the shell
* is a subshell or not ,global to jobs.c for safer term
* control
* */
int subshell_flag;
/* head of the singly linked list of alias blacklist to avoid nasty
* recurisive alias cases, like alias "ls=ls;ls" */
static alias *al_blist;
/* this marks the location at cmdline till alias blacklisting is done */
static char *al_deadend;
/* nah no ps3 or ps4 in my shell */
static char ps1[PRMT_MAX] = "ASH>";
static char ps2[PRMT_MAX] = " >";
/* globbing global member, freed in builtins */
static glob_t glob_res;
/*
* searches from str len bytes for the first occurence
* of any one char in array c of size n. The char is treated
* as a special delim, so the ones in quotes and escaped ones
* are ignored. returns the location if found, else NULL
*/
static char *strdelimvec(char *str, int len, int c[], int n)
{
int i;
char *end = str + len;
for(; str && str<=end; str++) {
if (IS_QUOTE(*str) && !IS_ESCAPE(*(str-1))) {
if (!(str = astrchr(str+1, *str, 1)))
break;
}
else {
for (i=0; i<n; i++) {
if (*str == c[i] && !IS_ESCAPE(*(str-1)))
return str;
}
}
}
return NULL;
}
/* 1 if subshell closed else zero */
static int is_ss_closed(char **str)
{
int ss_pair = 1;
int ss_delim[] = SS_DELIM_INIT;
for ((*str)++; (*str = strdelimvec(*str, strlen(*str),
ss_delim,sizeof(ss_delim) / sizeof(int)));) {
if (**str == SUBSHELL_OPEN)
++ss_pair;
else if (ss_pair == 1) {
ss_pair--;
break;
}
else
ss_pair--;
(*str)++;
}
return ss_pair==0 ? 1 : 0;
}
/* reinitializes the blacklisting helpers fresh aliasizing */
static inline void al_reinit_blist()
{
al_deadend = NULL;
if (al_blist)
al_lin_free(&al_blist);
}
/* the start of iterative alias expansion fixed at start_arg location.
* inp and delim are from parse_cmd since this is closely tied with it */
static int aliasize(char *start_arg, char *delim, char **inp)
{
int len;
alias *al_cur;
char al_arg[ARG_MAX] = {0};
if (start_arg >= al_deadend) {
/* no blacklisting since we crossed the deadend,
* so freeing the list and al_deadend reinitialized */
al_reinit_blist();
}
if ((al_cur = is_alias(start_arg )) &&
(al_lin_src(al_blist, start_arg) == NULL)) {
/* this is not a blacklisted one, hence alias it */
/* but from now till deadend it is */
al_lin_ins(&al_blist, start_arg);
len = strlen(start_arg);
astrcpy(al_arg, start_arg, len, 1);
**inp = *delim; /* so that repl_str works till actual
delim of cmdline */
repl_str(al_arg, al_cur->trans, start_arg);
if (al_deadend == NULL)
al_deadend = start_arg + strlen(al_cur->trans);
else
al_deadend = al_deadend + ALIAS_DIFF(al_cur);
}
else {
goto dont_aliasize;
}
return 0;
dont_aliasize :
return -1;
}
/*
* the god-forsaken parser!!!
* inp references the main input string, cmd is the array to be filled
* for execve from index start_ind. pflags are parser flags, (see macros
* above), and rflags are written as per the outcome of the parsing.
* Quoted args have leading quote in the cmd argument as an FYI for
* postproc_cmdline. If subshell, then cmd[0] will point to the whole
* subshell line, from '(' to ')', as an FYI for fe_or_ss.
* Aliasize is also called by this. If everything goes A-ok, then
* it returns the delim else -1. Check rflags and return value
* all the time.
*/
static char parse_cmd(char **inp, char **cmd, int start_ind,
int pflags, int *rflags)
{
int i = start_ind;
char delim;
char *start_arg;
if (!inp || !(*inp) || !(**inp)) {
printf("Godnamit\n");
goto hell;
}
while (**inp == ' ')
(*inp)++;
*rflags = 0;
for (start_arg=*inp; *inp; (*inp)++) {
if (IS_QUOTE(**inp)) {
delim = **inp;
/* keeping the leading quote in cmd[i] for post_proc as
* a signal to ignore special characters */
if (!(pflags & PARSE_DONTFILLCMD))
cmd[i++] = start_arg;
else
i++;
if ((*inp = astrchr((*inp)+1, delim, 1)))
**inp = '\0';
else
goto unclosed;
start_arg = *inp + 1;
}
else if (IS_ESCAPE(**inp)) {
if (!IS_GRAND_DELIM(*(*inp+1)))
*inp += 1;
else
goto unclosed;
}
else if (IS_SUBSHELL_OPEN(**inp)) {
char *ss_close;
int term_delim[] = TERM_DELIM_INIT;
if (i || *(*inp-1)) {
/* die if this not a start of new command */
if (i) {
goto hell;
}
/* die if prev_char is an escaped */
else if (*(*inp-2) && IS_ESCAPE(*(*inp-2)))
goto hell;
/* die if prev_char is not proper delim */
else if (!(IS_PARENT_DELIM(*(*inp-1))
|| IS_CHILD_DELIM(*(*inp-1)) ))
goto hell;
}
ss_close = *inp;
if (is_ss_closed(&ss_close)) {
*(ss_close++) = ' ';
if (!(ss_close = strdelimvec(ss_close, strlen(ss_close),
term_delim,
sizeof(term_delim) / sizeof(int))))
goto unclosed;
(*rflags) |= PARSERET_SUBSHELL;
delim = *ss_close;
*ss_close = '\n'; /* to make subshell happy */
/* signals that this is a subshell by the leading '('
* from now on , i MUST BE ZERO!!! */
if (!(pflags & PARSE_DONTFILLCMD))
cmd[i++] = *inp;
else
i++;
*inp = ss_close + 1;
goto done;
}
else
goto unclosed;
}
else if (IS_CHILD_DELIM(**inp) || IS_PARENT_DELIM(**inp) || IS_GRAND_DELIM(**inp)) {
if (IS_REDIRTOFD_DELIM(*inp))
continue;
delim = **inp;
**inp = '\0'; /* now cmd[i] can point till here */
/*
* start_arg can be NULL, for eg if multiple spaces are dealt
* with in which the above line nullify start_arg itself which
* is an 'ok' way to skip the bloody spaces
* */
if (*start_arg) {
/* alias only the cmd, ie cmd[0], not its args */
if (!i && is_alias(start_arg)) {
if (aliasize(start_arg, &delim, inp) == 0) {
*inp = start_arg - 1;
continue;
}
}
if (!(pflags & PARSE_DONTFILLCMD))
cmd[i++] = start_arg;
else
i++;
}
if (IS_PARENT_DELIM(delim) || IS_GRAND_DELIM(delim)) {
if (!(pflags&PARSE_WHOLELINE) || IS_GRAND_DELIM(delim)) {
(*inp)++;
goto done;
}
}
start_arg = *inp + 1;
}
}
done :
if (!(pflags & PARSE_DONTFILLCMD))
cmd[i] = NULL; /* to make argvp happy */
#ifdef DEBUG_ON
if (!(pflags & PARSE_DONTFILLCMD) &&
!(pflags & PARSE_DONTPRINT)) {
printf("%d "TERMSTR_GREEN("parse_cmd : "), getpid());
for (int j=0; cmd[j]; j++) {
prints(cmd[j]);
printf("|");
}
printf("%s with delim %d\n", "NULL", delim);
}
#endif
return delim; /* for verifying if this to be piped or bg'd etc */
unclosed :
(*rflags) |= PARSERET_UNCLOSED;
if (!(pflags & PARSE_DONTPRINT))
fprintf(stderr, "SHELL : cmdline unclosed\n");
return -1;
hell:
(*rflags) |= PARSERET_SYNERR;
if (!(pflags & PARSE_DONTPRINT))
fprintf(stderr, "SHELL : syntax error\n");
return -1;
}
/* unqoutes quoted arg, removes backslash of escapes, globs wildcards */
static void postproc_cmdline(char **cmd)
{
int i;
char *tok, **after_glob;
for (; NOTNULL(cmd); cmd=cmd+1) {
if (IS_QUOTE(**cmd)) {
*cmd += 1;
}
else {
glob(*cmd, GLOB_TILDE | GLOB_BRACE, NULL, &glob_res);
if (glob_res.gl_pathc > 0) {
/* we got matching files/dir with wildcard */
for (i=1, after_glob = cmd+glob_res.gl_pathc;
NOTNULL(cmd+i); i++, after_glob += 1) {
/* args after glob exp are copied to end to make
* room for glob expansion in cmd[][] */
*after_glob = *(cmd + i);
}
*after_glob = NULL; /* the way execvp likes it */
/* glob api maintains the results in the heap segment
* so making cmd point to it is not the worst idea i guess */
for (i=0; i<glob_res.gl_pathc; i++, cmd++)
*cmd = glob_res.gl_pathv[i];
}
else {
/* removing \ */
for (tok=*cmd; (tok=strchr(tok, ESCAPE)); tok++)
repl_str("\\","", tok);
}
}
}
}
/* @ startup of shell */
static void shell_init()
{
setbuf(stdout, NULL);
if (dup2(1, TERMFD) < 0)
ERR_EXIT("dup2");
if (fcntl(TERMFD, F_SETFD, FD_CLOEXEC) < 0)
ERR_EXIT("fcntl");
signal_init();
setpgid(0, 0);
tcsetpgrp(TERMFD, getpid());
}
/* @ death of shell */
static void shell_cleanup()
{
al_free();
mark_free();
job_free();
clean_up("f", TERMFD);
}
/* redir_fd is redirected to redir_dst for append or not as per
* ap_flag */
static void redir_me(int redir_fd, char *redir_dst, int ap_flag)
{
int dst_fd;
if (!redir_dst) {
ERRMSG("Error redirecting\n");
return;
}
if (!ap_flag) {/* don't append the output */
if ((dst_fd = creat(redir_dst, 0644)) < 0) {
ERR("creat");
return;
}
}
else {/* append the output */
if ((dst_fd = open(redir_dst, O_RDWR | O_APPEND)) < 0) {
ERR("open");
return;
}
}
/* now exec can be done */
if (dup2(dst_fd, redir_fd) < 0) {
ERR_EXIT("dup2");
}
/* the temporary dst_fd can sometimes be the redir_fd, think about
* 3>, dst_fd most cases will be 3, in that case dont't close it! */
if (dst_fd != redir_fd)
clean_up("f", dst_fd);
}
/* sets up the fds for the current cmd to be exec'd */
static void do_redir(char **cmd)
{
int redir_fd, redir_tofd, ap_flag, null_flag = 1;
char *redir_tok, *redir_dst;
while (NOTNULL(cmd)) {
redir_tok = int_till_txt(*cmd, &redir_fd);
if (redir_fd >= 1 && IS_REDIR_DELIM(*(redir_tok++))) {
if (IS_REDIRTOFD_DELIM(redir_tok)) {
int_till_txt(redir_tok+1, &redir_tofd);
if (!redir_tofd || dup2(redir_tofd, redir_fd) < 0)
ERRMSG("Error redirecting\n");
}
/* redir_dst can be filename or can be
* NULL(which will fail at redir_me) */
else {
redir_dst = *(cmd+1);
ap_flag = IS_REDIRAPPEND_DELIM(redir_tok)
? 1 : 0;
/* append flag and dst set */
redir_me(redir_fd, redir_dst, ap_flag);
}
if (null_flag) {
*cmd = NULL;
null_flag = 0;
}
}
cmd = cmd+1; /* next arg */
}
}
/* what cd tuns */
static void do_cd(char **cmd)
{
if (!cmd[1]) {
chdir(getenv("HOME"));
return;
}
postproc_cmdline(cmd);
if (chdir(cmd[1]) < 0)
ERR("chdir");
globfree(&glob_res);
}
/* does shell builtins, hopefully */
static int builtin(char **cmd)
{
if (cmd[0]) {
switch (*cmd[0]) {
case 'a' :
if (!strcmp(cmd[0], "alias")) {
postproc_cmdline(cmd);
alias_me(cmd);
globfree(&glob_res);
return 1;
}
break;
case 'b' :
if (!strcmp(cmd[0], "bg")) {
do_bgfg(cmd, BG);
return 1;
}
break;
case 'c' :
if (!strcmp(cmd[0], "cd")) {
do_cd(cmd);
return 1;
}
break;
case 'g' :
if (!strcmp(cmd[0], "gt")) {
goto_mark(cmd);
return 1;
}
break;
case 'm' :
if (!strcmp(cmd[0], "mk")) {
mark_me(cmd);
return 1;
}
break;
case 'e' :
if (!strcmp(cmd[0], "exit")) {
shell_cleanup();
fprintf(stdout, "Exiting\n");
exit(EXIT_SUCCESS);
}
case 'f' :
if (!strcmp(cmd[0], "fg")) {
do_bgfg(cmd, FG);
return 1;
}
break;
case 'j' :
if (!strcmp(cmd[0], "jobs")) {
printjobs();
return 1;
}
break;
case 'u' :
if (!strcmp(cmd[0], "unalias")) {
postproc_cmdline(cmd);
unalias_me(cmd);
globfree(&glob_res);
return 1;
}
if (!strcmp(cmd[0], "unmk")) {
unmark_me(cmd);
return 1;
}
break;
case 'v' :
if (!strcmp(cmd[0], "var")) {
return 1;
}
break;
}
}
return 0;
}
/* the function name explains for itself
* facilitates pgrp, tc, signal unblock */
static pid_t fork_and_exec(char **cmd, int flags,
pid_t pgid, sigset_t msk)
{
pid_t pid;
if (subshell_flag) {
/* if subshell then every forks belong to the same pg
* sharing the same terminal */
flags &= ~(FE_SETTERM|FE_SETPG);
}
pid = fork();
switch (pid) {
case -1 :
ERR_EXIT("fork");
break;
case 0 :
SIG_TGL(SIGTTOU, SIG_DFL);
SIG_TGL(SIGTTIN, SIG_DFL);
pid = getpid();
if (flags & FE_SETTERM) {
if (!pgid) {
while (tcgetpgrp(TERMFD) != pid)
; /* tcpgrp will be pid itseld then */
}
else {
while (tcgetpgrp(TERMFD) != pgid)
; /* tcpgrp as per arg in function */
}
}
if (flags & FE_REDIR)
do_redir(cmd);
if (flags & FE_PP)
postproc_cmdline(cmd);
if (flags & FE_UBLK)
MASK_ALLSIG(SIG_UNBLOCK, msk);
if (flags & FE_SETPG) {
if (setpgid(0, pgid) < 0)
perror("setpgid");
}
#ifdef DEBUG_ON
printf("%d "TERMSTR_RED("fork_and_exec : "), getpid());
for (int j=0; cmd[j]; j++)
printf("%s|", cmd[j]);
printf("NULL\n");
#endif
if (cmd[0]) {
if (execvp(cmd[0], cmd) < 0)
ERR_EXIT("execvp");
}
else{
_exit(EXIT_SUCCESS);
}
break;
default :
/*
* force setpgid here, sometimes the parent, ie the shell,
* goes in a hurry and the child won't have a chance to setpgid
* and that would create havoc especially in pipelines
* */
if (flags & FE_SETPG) {
if (setpgid(pid, pgid ?
pgid : pid) < 0)
perror("setpgid");
}
/* so that child don't spin too much with tcsetpgrp */
if (flags & FE_SETTERM) {
tcsetpgrp(TERMFD, pgid ? pgid : pid);
}
break;
}
return pid;
}
static void eval(char *);
/* subshell is just a simple fork and a call to eval,
* the forked shell will have the global subshell_flag set to one */
static pid_t run_subshell(char *ss_inp, int flags, pid_t pgid)
{
pid_t pid;
pid = fork();
switch (pid) {
case -1 :
ERR_EXIT("fork");
break;
case 0 :
subshell_flag = 1;
pid = getpid();
SIG_TGL(SIGTSTP, SIG_DFL);
SIG_TGL(SIGINT, SIG_DFL);
if (flags & FE_SETTERM) {
if (!pgid) {
while (tcgetpgrp(TERMFD) != pid)
;
}
else {
while (tcgetpgrp(TERMFD) != pgid)
;
}
}
if (flags & FE_SETPG) {
if (setpgid(0, pgid) < 0)
perror("setpgid");
}
eval(ss_inp);
exit(EXIT_SUCCESS);
default :
if (flags & FE_SETPG) {
if (setpgid(pid, pgid ?
pgid : pid) < 0)
perror("setpgid");
}
if (flags & FE_SETTERM) {
tcsetpgrp(TERMFD, pgid ? pgid : pid);
}
break;
}
return pid;
}
/* fork_and_exec or run_subshell as per the 1st char of cmd[0] */
static pid_t fe_or_ss(char **cmd,
int flags, pid_t pgid, sigset_t msk)
{
if (!NOTNULL(cmd))
return -1;
if (*cmd[0] == SUBSHELL_OPEN) {
return run_subshell(cmd[0] + 1, flags, pgid);
}
else {
return fork_and_exec(cmd, flags, pgid, msk);
}
}
/* regular fork and exec routine with a shell twist */
static void exec_me(char **cmd, int state)
{
pid_t pid;
jobs **job;
sigset_t msk;
if (!cmd || !(*cmd) || !(**cmd))
return;
if (builtin(cmd))
return;
MASK_SIG(SIG_BLOCK, SIGCHLD, msk);
pid = fe_or_ss(cmd,
(state == FG) ? FE_ALL : (FE_ALL^FE_SETTERM), 0, msk);
job = addjob(pid, state, cmd);
if(state == FG) {
wait_fg(job);
}
else {
shell_printf(SHELLPRINT_CHKSS | SHELLPRINT_CHKTERM,
"[%d] %d\t%s\n",(*job)->jid, (*job)->pid[0], (*job)->cmdline);
}
MASK_SIG(SIG_UNBLOCK, SIGCHLD, msk);
}
/* piper */
static void pipe_me(char **cmd, char **inp, char *delim)
{
int pipe_fd[2], i, parse_rflags = 0;
char pipe_cmds[ARG_MAX];
pid_t pgid = 0;
sigset_t msk;
jobs **job;
if (!cmd || !(*cmd) || !(**cmd)) {
return;
}
MASK_SIG(SIG_BLOCK, SIGCHLD, msk);
for (i=0; IS_PIPE_DELIM(*delim);) { /* there is more to pipe */
if (pipe(pipe_fd) < 0)
ERR("pipe");
if (dup2(pipe_fd[1], STDOUT_FILENO) < 0)
ERR_EXIT("dup2");
clean_up("f", pipe_fd[1]);
if (!pgid) {
pgid = fe_or_ss(cmd, FE_ALL, 0, msk);
job = addjob(pgid, FG, cmd); /* so (*job)->pid[0] = pgid */
}
else {
(*job)->pid[++i] = fe_or_ss(cmd, FE_ALL^FE_SETTERM, pgid, msk);
strcat((*job)->cmdline, " | ");
stringify(pipe_cmds, cmd);
strcat((*job)->cmdline, pipe_cmds);
}
/* sets up for 'read from pipe' for next child in loop */
if (dup2(pipe_fd[0], STDIN_FILENO) < 0)
ERR_EXIT("dup2");
clean_up("f", pipe_fd[0]);
*delim = parse_cmd(inp, cmd, 0, 0, &parse_rflags);
}
/* restores the orig stdout for the final exec */
if (dup2(TERMFD, STDOUT_FILENO) < 0) {
ERR_EXIT("dup2");
}
(*job)->pid[++i] = fe_or_ss(cmd, FE_ALL^FE_SETTERM, pgid, msk);
strcat((*job)->cmdline, " | ");
stringify(pipe_cmds, cmd);
strcat((*job)->cmdline, pipe_cmds);
(*job)->pid[++i] = 0;
if (dup2(TERMFD, STDIN_FILENO) < 0) { /* restores the orig stdin*/
ERR_EXIT("dup2");
}
wait_fg(job);
MASK_SIG(SIG_UNBLOCK, SIGCHLD, msk);
}
/* where it all starts */
static void eval(char *inp)
{
char *cmd[ARG_MAX] = {0}; /* main cmd array for exec */
char delim; /* delim between cmds */
int parse_rflags = 0; /* maybe useful in the future */
#ifdef DEBUG_ON
printf("%d "TERMSTR_YELLOW("eval : "), getpid());
prints(inp);
printf("\n");
#endif
do {
parse_rflags = 0;
if ((delim = parse_cmd(&inp, cmd, 0, 0, &parse_rflags)) == PIPE) {
pipe_me(cmd, &inp, &delim);
}
else if (delim == -1) {
break;
}
else {
exec_me(cmd,IS_BG_DELIM(delim) ? BG : FG);
}
} while (!IS_GRAND_DELIM(delim) && !IS_GRAND_DELIM(*inp));
/* main cmdline end in '\n' , i guess.. */
al_reinit_blist();
}
/* for checking if PS2 is required, returns true on unclosed quotes,
* subshells, escaped \n */
static int is_closed(char *str)
{
char cmd[LINE_MAX + 1] = {0};
char *cmdline = cmd + 1;
int parse_rflags = 0;
astrcpy(cmdline, str, strlen(str), 1);
al_reinit_blist();
if (parse_cmd(&cmdline, NULL, 0, PARSE_DONTFILLCMD|PARSE_WHOLELINE
|PARSE_DONTPRINT,&parse_rflags) == -1) {
if (parse_rflags & PARSERET_UNCLOSED)
return 0;
}
return 1;
}
/* eats trailing spaces and calls tilde expansion */
static void preproc_cmdline(char *inp, int flags)
{
char *i;
if (flags & PREPRO_CREND) {
for (i=inp + (strlen(inp)-1); *i==' '; i--)
;
if (*i)
*(i+1) = GRAND_DELIM;
else
*i = GRAND_DELIM;
}
if (flags & PREPRO_NULLBEG) {
*(inp - 1) = 0;
}
}
static void prompt()
{
char cmdline[LINE_MAX + 1] = {0}; /* main command line */
char *inp; /* points to where input is done on main command line */
inp = cmdline + 1; /* ok, I am scared about parsing */
while (1) {
if (isatty(STDIN_FILENO))
printf("%s", ps1);
fgets(inp, LINE_MAX, stdin);
while (!is_closed(cmdline + 1)) {
if (!isatty(STDIN_FILENO))
break;
if ((inp = strchr(inp, GRAND_DELIM)) && IS_ESCAPE(*(inp-1)))
inp--;
printf("%s", ps2);
fgets(inp, LINE_MAX, stdin);
}
inp = cmdline + 1;
preproc_cmdline(inp, PREPRO_CREND | PREPRO_NULLBEG);
eval(inp);
memset(cmdline, 0, LINE_MAX);
if (!isatty(STDIN_FILENO))
break;
}
}
int main()
{
shell_init();
prompt();
shell_cleanup();
exit(EXIT_FAILURE);
}