-
Notifications
You must be signed in to change notification settings - Fork 273
/
cmdline.c
2651 lines (2226 loc) · 61.8 KB
/
cmdline.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
/* cmdline.c - core analysis suite
*
* Copyright (C) 1999, 2000, 2001, 2002 Mission Critical Linux, Inc.
* Copyright (C) 2002-2015,2019 David Anderson
* Copyright (C) 2002-2015,2019 Red Hat, Inc. All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#include "defs.h"
static void restore_sanity(void);
static void restore_ifile_sanity(void);
static int pseudo_command(char *);
static void check_special_handling(char *);
static int is_executable_in_PATH(char *);
static int is_shell_script(char *);
static void list_aliases(char *);
static int allocate_alias(int);
static int alias_exists(char *);
static void resolve_aliases(void);
static int setup_redirect(int);
int multiple_pipes(char **);
static int output_command_to_pids(void);
static void set_my_tty(void);
static char *signame(int);
static int setup_stdpipe(void);
static void wait_for_children(ulong);
#define ZOMBIES_ONLY (1)
#define ALL_CHILDREN (2)
int shell_command(char *);
static void modify_orig_line(char *, struct args_input_file *);
static void modify_expression_arg(char *, char **, struct args_input_file *);
static int verify_args_input_file(char *);
static char *crash_readline_completion_generator(const char *, int);
static char **crash_readline_completer(const char *, int, int);
#define READLINE_LIBRARY
#include <readline.h>
#include <rldefs.h>
#include <history.h>
static void readline_init(void);
static struct alias_data alias_head = { 0 };
void
process_command_line(void)
{
/*
* Restore normal environment, clearing out any excess baggage
* piled up by the previous command.
*/
restore_sanity();
fp = stdout;
BZERO(pc->command_line, BUFSIZE);
if (!pc->ifile_in_progress && !(pc->flags &
(TTY|SILENT|CMDLINE_IFILE|RCHOME_IFILE|RCLOCAL_IFILE)))
fprintf(fp, "%s", pc->prompt);
fflush(fp);
/*
* Input can come from five possible sources:
*
* 1. an .rc file located in the user's HOME directory.
* 2. an .rc file located in the current directory.
* 3. an input file that was designated by the -i flag at
* program invocation.
* 4. from a terminal.
* 5. from a pipe, if stdin is a pipe rather than a terminal.
*
* But first, handle the interruption of an input file caused
* by a FATAL error in one of its commands.
*
*/
if (pc->ifile_in_progress) {
switch (pc->ifile_in_progress)
{
case RCHOME_IFILE:
pc->flags |= INIT_IFILE|RCHOME_IFILE;
sprintf(pc->command_line, "< %s/.%src",
pc->home, pc->program_name);
break;
case RCLOCAL_IFILE:
sprintf(pc->command_line, "< .%src", pc->program_name);
pc->flags |= INIT_IFILE|RCLOCAL_IFILE;
break;
case CMDLINE_IFILE:
sprintf(pc->command_line, "< %s", pc->input_file);
pc->flags |= INIT_IFILE|CMDLINE_IFILE;
break;
case RUNTIME_IFILE:
sprintf(pc->command_line, "%s", pc->runtime_ifile_cmd);
pc->flags |= IFILE_ERROR;
break;
default:
error(FATAL, "invalid input file\n");
}
} else if (pc->flags & RCHOME_IFILE) {
sprintf(pc->command_line, "< %s/.%src",
pc->home, pc->program_name);
pc->flags |= INIT_IFILE;
} else if (pc->flags & RCLOCAL_IFILE) {
sprintf(pc->command_line, "< .%src", pc->program_name);
pc->flags |= INIT_IFILE;
} else if (pc->flags & CMDLINE_IFILE) {
sprintf(pc->command_line, "< %s", pc->input_file);
pc->flags |= INIT_IFILE;
} else if (pc->flags & TTY) {
if (!(pc->readline = readline(pc->prompt))) {
args[0] = NULL;
fprintf(fp, "\n");
return;
}
if (strlen(pc->readline) >= BUFSIZE)
error(FATAL, "input line exceeds maximum of 1500 bytes\n");
else
strcpy(pc->command_line, pc->readline);
free(pc->readline);
clean_line(pc->command_line);
pseudo_command(pc->command_line);
strcpy(pc->orig_line, pc->command_line);
if (strlen(pc->command_line) && !iscntrl(pc->command_line[0]))
add_history(pc->command_line);
check_special_handling(pc->command_line);
} else {
if (fgets(pc->command_line, BUFSIZE-1, stdin) == NULL)
clean_exit(1);
if (!(pc->flags & SILENT)) {
fprintf(fp, "%s", pc->command_line);
fflush(fp);
}
clean_line(pc->command_line);
strcpy(pc->orig_line, pc->command_line);
}
/*
* First clean out all linefeeds and leading/trailing spaces.
* Then substitute aliases for the real thing they represent.
*/
clean_line(pc->command_line);
resolve_aliases();
/*
* Setup output redirection based upon the command line itself or
* based upon the default scrolling behavior, if any.
*/
switch (setup_redirect(FROM_COMMAND_LINE))
{
case REDIRECT_NOT_DONE:
case REDIRECT_TO_STDPIPE:
case REDIRECT_TO_PIPE:
case REDIRECT_TO_FILE:
break;
case REDIRECT_SHELL_ESCAPE:
case REDIRECT_SHELL_COMMAND:
case REDIRECT_FAILURE:
RESTART();
break;
}
/*
* Setup the global argcnt and args[] array for use by everybody
* during the life of this command.
*/
argcnt = parse_line(pc->command_line, args);
}
/*
* Allow input file redirection without having to put a space between
* the < and the filename. Allow the "pointer-to" asterisk to "touch"
* the structure/union name.
*/
static void
check_special_handling(char *s)
{
char local[BUFSIZE];
strcpy(local, s);
if ((local[0] == '*') && (!whitespace(local[1]))) {
sprintf(s, "* %s", &local[1]);
return;
}
if ((local[0] == '<') && (!whitespace(local[1]))) {
sprintf(s, "< %s", &local[1]);
return;
}
}
static int
is_executable_in_PATH(char *filename)
{
char *buf1, *buf2;
char *tok, *path;
int retval;
if ((path = getenv("PATH"))) {
buf1 = GETBUF(strlen(path)+1);
buf2 = GETBUF(strlen(path)+1);
strcpy(buf2, path);
} else
return FALSE;
retval = FALSE;
tok = strtok(buf2, ":");
while (tok) {
sprintf(buf1, "%s/%s", tok, filename);
if (file_exists(buf1, NULL) &&
(access(buf1, X_OK) == 0)) {
retval = TRUE;
break;
}
tok = strtok(NULL, ":");
}
FREEBUF(buf1);
FREEBUF(buf2);
return retval;
}
/*
* At this point the only pseudo commands are the "r" (repeat) and
* the "h" (history) command:
*
* 1. an "r" alone, or "!!" along, just means repeat the last command.
* 2. an "r" followed by a number, means repeat that command from the
* history table.
* 3. an "!" followed by a number that is not the name of a command
* in the user's PATH, means repeat that command from the history table.
* 4. an "r" followed by one or more non-decimal characters means to
* seek back until a line-beginning match is found.
* 5. an "h" alone, or a string beginning with "hi", means history.
*/
static int
pseudo_command(char *input)
{
int i;
HIST_ENTRY *entry;
int idx, found;
char *p;
clean_line(input);
/*
* Just dump all commands that have been entered to date.
*/
if (STREQ(input, "h") || STRNEQ(input, "hi")) {
dump_history();
pc->command_line[0] = NULLCHAR;
return TRUE;
}
if (STREQ(input, "r") || STREQ(input, "!!")) {
if (!history_offset)
error(FATAL, "no commands entered!\n");
entry = history_get(history_offset);
strcpy(input, entry->line);
fprintf(fp, "%s%s\n", pc->prompt, input);
return TRUE;
}
if ((input[0] == 'r') && decimal(&input[1], 0)) {
if (!history_offset)
error(FATAL, "no commands entered!\n");
p = &input[1];
goto rerun;
}
if ((input[0] == '!') && decimal(&input[1], 0) &&
!is_executable_in_PATH(first_nonspace(&input[1]))) {
p = first_nonspace(&input[1]);
goto rerun;
}
if (STRNEQ(input, "r ")) {
if (!history_offset)
error(FATAL, "no commands entered!\n");
p = first_nonspace(&input[1]);
rerun:
if (decimal(p, 0)) {
idx = atoi(p);
if (idx == 0)
goto invalid_repeat_request;
if (idx > history_offset)
error(FATAL, "command %d not entered yet!\n",
idx);
entry = history_get(idx);
strcpy(input, entry->line);
fprintf(fp, "%s%s\n", pc->prompt, input);
return TRUE;
}
idx = -1;
found = FALSE;
for (i = history_offset; i > 0; i--) {
entry = history_get(i);
if (STRNEQ(entry->line, p)) {
found = TRUE;
break;
}
}
if (found) {
strcpy(input, entry->line);
fprintf(fp, "%s%s\n", pc->prompt, input);
return TRUE;
}
invalid_repeat_request:
fprintf(fp, "invalid repeat request: %s\n", input);
strcpy(input, "");
return TRUE;
}
return FALSE;
}
/*
* Dump the history table in first-to-last chronological order.
*/
void
dump_history(void)
{
int i;
HIST_ENTRY **the_history;
HIST_ENTRY *entry;
if (!history_offset)
error(FATAL, "no commands entered!\n");
the_history = history_list();
for (i = 0; i < history_offset; i++) {
entry = the_history[i];
fprintf(fp, "[%d] %s\n", i+1, entry->line);
}
}
/*
* Pager arguments.
*/
static char *less_argv[5] = {
"/usr/bin/less",
"-E",
"-X",
"-Ps -- MORE -- forward\\: <SPACE>, <ENTER> or j backward\\: b or k quit\\: q",
NULL
};
static char *more_argv[2] = {
"/bin/more",
NULL
};
static char **CRASHPAGER_argv = NULL;
int
CRASHPAGER_valid(void)
{
int i, c;
char *env, *CRASHPAGER_buf;
char *arglist[MAXARGS];
if (CRASHPAGER_argv)
return TRUE;
if (!(env = getenv("CRASHPAGER")))
return FALSE;
if (strstr(env, "|") || strstr(env, "<") || strstr(env, ">")) {
error(INFO,
"CRASHPAGER ignored: contains invalid character: \"%s\"\n",
env);
return FALSE;
}
if ((CRASHPAGER_buf = (char *)malloc(strlen(env)+1)) == NULL)
return FALSE;
strcpy(CRASHPAGER_buf, env);
if (!(c = parse_line(CRASHPAGER_buf, arglist)) ||
!file_exists(arglist[0], NULL) || access(arglist[0], X_OK) ||
!(CRASHPAGER_argv = (char **)malloc(sizeof(char *) * (c+1)))) {
free(CRASHPAGER_buf);
if (strlen(env))
error(INFO,
"CRASHPAGER ignored: \"%s\"\n", env);
return FALSE;
}
for (i = 0; i < c; i++)
CRASHPAGER_argv[i] = arglist[i];
CRASHPAGER_argv[i] = NULL;
return TRUE;
}
/*
* Set up a command string buffer for error/help output.
*/
char *
setup_scroll_command(void)
{
char *buf;
long i, len;
if (!(pc->flags & SCROLL))
return NULL;
switch (pc->scroll_command)
{
case SCROLL_LESS:
buf = GETBUF(strlen(less_argv[0])+1);
strcpy(buf, less_argv[0]);
break;
case SCROLL_MORE:
buf = GETBUF(strlen(more_argv[0])+1);
strcpy(buf, more_argv[0]);
break;
case SCROLL_CRASHPAGER:
for (i = len = 0; CRASHPAGER_argv[i]; i++)
len += strlen(CRASHPAGER_argv[i])+1;
buf = GETBUF(len);
for (i = 0; CRASHPAGER_argv[i]; i++) {
sprintf(&buf[strlen(buf)], "%s%s",
i ? " " : "",
CRASHPAGER_argv[i]);
}
break;
default:
return NULL;
}
return buf;
}
/*
* Parse the command line for pipe or redirect characters:
*
* 1. if a "|" character is found, popen() what comes after it, and
* modify the contents of the global "fp" FILE pointer.
* 2. if one or two ">" characters are found, fopen() the filename that
* follows, and modify the contents of the global "fp" FILE pointer.
*
* Care is taken to segregate:
*
* 1. expressions encompassed by parentheses, or
* 2. strings encompassed by single or double quotation marks
*
* When either of the above are in affect, no redirection is done.
*
* Lastly, if no redirection is requested by the user on the command line,
* output is passed to the default scrolling command, which is popen()'d
* and again, the contents of the global "fp" FILE pointer is modified.
* This default behavior is not performed if the command is coming from
* an input file, nor if scrolling has been turned off.
*/
static int
setup_redirect(int origin)
{
char *p, which;
int append;
int expression;
int string;
int ret ATTRIBUTE_UNUSED;
FILE *pipe;
FILE *ofile;
pc->redirect = origin;
pc->eoc_index = 0;
p = pc->command_line;
if (STREQ(p, "|") || STREQ(p, "!")) {
ret = system("/bin/sh");
pc->redirect |= REDIRECT_SHELL_ESCAPE;
return REDIRECT_SHELL_ESCAPE;
}
if (FIRSTCHAR(p) == '|' || FIRSTCHAR(p) == '!')
pc->redirect |= REDIRECT_SHELL_COMMAND;
expression = 0;
string = FALSE;
while (*p) {
if (*p == '(')
expression++;
if (*p == ')')
expression--;
if ((*p == '"') || (*p == '\''))
string = !string;
if (!(expression || string) &&
((*p == '|') || (*p == '!'))) {
which = *p;
*p = NULLCHAR;
pc->eoc_index = p - pc->command_line;
p++;
p = strip_beginning_whitespace(p);
if (!strlen(p)) {
error(INFO, "no shell command after '%c'\n",
which);
pc->redirect |= REDIRECT_FAILURE;
return REDIRECT_FAILURE;
}
if (LASTCHAR(p) == '|')
error(FATAL_RESTART, "pipe to nowhere?\n");
if (pc->redirect & REDIRECT_SHELL_COMMAND)
return shell_command(p);
if ((pipe = popen(p, "w")) == NULL) {
error(INFO, "cannot open pipe\n");
pc->redirect |= REDIRECT_FAILURE;
return REDIRECT_FAILURE;
}
setbuf(pipe, NULL);
switch (origin)
{
case FROM_COMMAND_LINE:
fp = pc->pipe = pipe;
break;
case FROM_INPUT_FILE:
fp = pc->ifile_pipe = pipe;
break;
}
if (multiple_pipes(&p))
pc->redirect |= REDIRECT_MULTI_PIPE;
strcpy(pc->pipe_command, p);
null_first_space(pc->pipe_command);
pc->redirect |= REDIRECT_TO_PIPE;
if (!(pc->redirect & REDIRECT_SHELL_COMMAND)) {
if ((pc->pipe_pid = output_command_to_pids()))
pc->redirect |= REDIRECT_PID_KNOWN;
else
error(FATAL_RESTART,
"pipe operation failed\n");
}
return REDIRECT_TO_PIPE;
}
if (!(expression || string) && (*p == '>') &&
!((p > pc->command_line) && (*(p-1) == '-'))) {
append = FALSE;
*p = NULLCHAR;
pc->eoc_index = p - pc->command_line;
if (*(p+1) == '>') {
append = TRUE;
*p = NULLCHAR;
p++;
}
p++;
p = strip_beginning_whitespace(p);
if (!strlen(p)) {
error(INFO, "no file name after %s\n",
append ? ">>" : ">");
pc->redirect |= REDIRECT_FAILURE;
return REDIRECT_FAILURE;
}
if (pc->flags & IFILE_ERROR)
append = TRUE;
if ((ofile =
fopen(p, append ? "a+" : "w+")) == NULL) {
error(INFO, "unable to open %s\n", p);
pc->redirect = REDIRECT_FAILURE;
return REDIRECT_FAILURE;
}
setbuf(ofile, NULL);
switch (origin)
{
case FROM_COMMAND_LINE:
fp = pc->ofile = ofile;
break;
case FROM_INPUT_FILE:
fp = pc->ifile_ofile = ofile;
break;
}
pc->redirect |= REDIRECT_TO_FILE;
return REDIRECT_TO_FILE;
}
p++;
}
if ((origin == FROM_COMMAND_LINE) && (pc->flags & TTY) &&
(pc->flags & SCROLL) && pc->scroll_command) {
if (!strlen(pc->command_line) ||
STREQ(pc->command_line, "q") ||
STREQ(pc->command_line, "Q") ||
STREQ(pc->command_line, "exit") ||
STRNEQ(pc->command_line, "<")) {
pc->redirect |= REDIRECT_NOT_DONE;
return REDIRECT_NOT_DONE;
}
if (!setup_stdpipe()) {
error(INFO, "cannot open pipe\n");
pc->redirect |= REDIRECT_FAILURE;
return REDIRECT_FAILURE;
}
fp = pc->stdpipe;
pc->redirect |= REDIRECT_TO_STDPIPE;
switch (pc->scroll_command)
{
case SCROLL_LESS:
strcpy(pc->pipe_command, less_argv[0]);
break;
case SCROLL_MORE:
strcpy(pc->pipe_command, more_argv[0]);
break;
case SCROLL_CRASHPAGER:
strcpy(pc->pipe_command, CRASHPAGER_argv[0]);
break;
}
return REDIRECT_TO_STDPIPE;
}
pc->redirect |= REDIRECT_NOT_DONE;
return REDIRECT_NOT_DONE;
}
/*
* Find the last command in an input line that possibly contains
* multiple pipes.
*/
int
multiple_pipes(char **input)
{
char *p, *found;
int quote;
found = NULL;
quote = FALSE;
for (p = *input; *p; p++) {
if ((*p == '\'') || (*p == '"')) {
quote = !quote;
continue;
} else if (quote)
continue;
if (*p == '|') {
if (STRNEQ(p, "||"))
break;
found = first_nonspace(p+1);
}
}
if (found) {
*input = found;
return TRUE;
} else
return FALSE;
}
void
debug_redirect(char *s)
{
int others;
int alive;
others = 0;
console("%s: (", s);
if (pc->redirect & FROM_COMMAND_LINE)
console("%sFROM_COMMAND_LINE", others++ ? "|" : "");
if (pc->redirect & FROM_INPUT_FILE)
console("%sFROM_INPUT_FILE", others++ ? "|" : "");
if (pc->redirect & REDIRECT_NOT_DONE)
console("%sREDIRECT_NOT_DONE", others++ ? "|" : "");
if (pc->redirect & REDIRECT_TO_PIPE)
console("%sREDIRECT_TO_PIPE", others++ ? "|" : "");
if (pc->redirect & REDIRECT_TO_STDPIPE)
console("%sREDIRECT_TO_STDPIPE", others++ ? "|" : "");
if (pc->redirect & REDIRECT_TO_FILE)
console("%sREDIRECT_TO_FILE", others++ ? "|" : "");
if (pc->redirect & REDIRECT_FAILURE)
console("%sREDIRECT_FAILURE", others++ ? "|" : "");
if (pc->redirect & REDIRECT_SHELL_ESCAPE)
console("%sREDIRECT_SHELL_ESCAPE", others++ ? "|" : "");
if (pc->redirect & REDIRECT_SHELL_COMMAND)
console("%sREDIRECT_SHELL_COMMAND", others++ ? "|" : "");
if (pc->redirect & REDIRECT_PID_KNOWN)
console("%sREDIRECT_PID_KNOWN", others++ ? "|" : "");
if (pc->redirect & REDIRECT_MULTI_PIPE)
console("%sREDIRECT_MULTI_PIPE", others++ ? "|" : "");
console(")\n");
if (pc->pipe_pid || strlen(pc->pipe_command)) {
if (pc->pipe_pid && PID_ALIVE(pc->pipe_pid))
alive = TRUE;
else
alive = FALSE;
console("pipe_pid: %d (%s) pipe_command: %s\n",
pc->pipe_pid,
alive ? "alive" : "dead",
pc->pipe_command);
}
}
/*
* Determine whether the pid receiving the current piped output is still
* alive.
*
* NOTE: This routine returns TRUE by default, and only returns FALSE if
* the pipe_pid exists *and* it's known to have died. Therefore the
* caller must be cognizant of pc->pipe_pid or pc->stdpipe_pid.
*/
int
output_open(void)
{
int waitstatus, waitret;
if (!(pc->flags & TTY))
return TRUE;
switch (pc->redirect & PIPE_OPTIONS)
{
case (REDIRECT_TO_STDPIPE|FROM_COMMAND_LINE):
waitret = waitpid(pc->stdpipe_pid, &waitstatus, WNOHANG);
if ((waitret == pc->stdpipe_pid) || (waitret == -1))
return FALSE;
break;
case (REDIRECT_TO_PIPE|FROM_INPUT_FILE):
if (pc->curcmd_flags & REPEAT)
break;
/* FALLTHROUGH */
case (REDIRECT_TO_PIPE|FROM_COMMAND_LINE):
switch (pc->redirect & (REDIRECT_MULTI_PIPE))
{
case REDIRECT_MULTI_PIPE:
if (!PID_ALIVE(pc->pipe_pid))
return FALSE;
break;
default:
waitret = waitpid(pc->pipe_pid, &waitstatus, WNOHANG);
if (waitret == pc->pipe_pid)
return FALSE;
if (waitret == -1) { /* intervening sh */
if (!PID_ALIVE(pc->pipe_pid))
return FALSE;
}
break;
}
break;
default:
break;
}
return TRUE;
}
/*
* Determine the pids of the current popen'd shell and output command.
* This is all done using /proc; the ps kludge at the bottom of this
* routine is legacy, and should only get executed if /proc doesn't exist.
*/
static int
output_command_to_pids(void)
{
DIR *dirp;
struct dirent *dp;
FILE *stp;
char buf1[BUFSIZE];
char buf2[BUFSIZE];
char lookfor[BUFSIZE+2];
char *pid, *name, *status, *p_pid, *pgrp, *comm;
char *arglist[MAXARGS];
int argc;
FILE *pipe;
int retries, shell_has_exited;
retries = 0;
shell_has_exited = FALSE;
pc->pipe_pid = pc->pipe_shell_pid = 0;
comm = strrchr(pc->pipe_command, '/');
sprintf(lookfor, "(%s)", comm ? ++comm : pc->pipe_command);
stall(1000);
retry:
if (is_directory("/proc") && (dirp = opendir("/proc"))) {
for (dp = readdir(dirp); dp && !pc->pipe_pid;
dp = readdir(dirp)) {
if (!decimal(dp->d_name, 0))
continue;
sprintf(buf1, "/proc/%s/stat", dp->d_name);
if (file_exists(buf1, NULL) &&
(stp = fopen(buf1, "r"))) {
if (fgets(buf2, BUFSIZE, stp)) {
pid = strtok(buf2, " ");
name = strtok(NULL, " ");
status = strtok(NULL, " ");
p_pid = strtok(NULL, " ");
pgrp = strtok(NULL, " ");
if (STREQ(name, "(sh)") &&
(atoi(p_pid) == getpid())) {
pc->pipe_shell_pid = atoi(pid);
if (STREQ(status, "Z"))
shell_has_exited = TRUE;
}
if (STREQ(name, lookfor) &&
((atoi(p_pid) == getpid()) ||
(atoi(p_pid) == pc->pipe_shell_pid)
|| (atoi(pgrp) == getpid()))) {
pc->pipe_pid = atoi(pid);
console(
"FOUND[%d] (%d->%d->%d) %s %s p_pid: %s pgrp: %s\n",
retries, getpid(),
pc->pipe_shell_pid,
pc->pipe_pid,
name, status, p_pid, pgrp);
}
}
fclose(stp);
}
}
closedir(dirp);
}
if (!pc->pipe_pid && !shell_has_exited &&
((retries++ < 10) || pc->pipe_shell_pid)) {
stall(1000);
goto retry;
}
console("getpid: %d pipe_shell_pid: %d pipe_pid: %d\n",
getpid(), pc->pipe_shell_pid, pc->pipe_pid);
if (pc->pipe_pid)
return pc->pipe_pid;
sprintf(buf1, "ps -ft %s", pc->my_tty);
console("%s: ", buf1);
if ((pipe = popen(buf1, "r")) == NULL) {
error(INFO, "cannot determine output pid\n");
return 0;
}
while (fgets(buf1, BUFSIZE, pipe)) {
argc = parse_line(buf1, arglist);
if ((argc >= 8) &&
STREQ(arglist[7], pc->pipe_command) &&
STRNEQ(pc->my_tty, arglist[5])) {
pc->pipe_pid = atoi(arglist[1]);
break;
}
}
pclose(pipe);
console("%d\n", pc->pipe_pid);
return pc->pipe_pid;
}
/*
* Close straggling, piped-to, output commands.
*/
void
close_output(void)
{
if ((pc->flags & TTY) &&
(pc->pipe_pid || strlen(pc->pipe_command)) &&
output_open())
kill(pc->pipe_pid, 9);
}
/*
* Initialize what's needed for the command line:
*
* 1. termios structures for raw and cooked terminal mode.
* 2. set up SIGINT and SIGPIPE handlers for aborted commands.
* 3. set up the command history table.
* 4. create the prompt string.
*/
void
cmdline_init(void)
{
int fd = 0;
/*
* Stash a copy of the original termios setup.
* Build a raw version for quick use for each command entry.
*/
if (isatty(fileno(stdin)) && ((fd = open("/dev/tty", O_RDONLY)) >= 0)) {
if (tcgetattr(fd, &pc->termios_orig) == -1)
error(FATAL, "tcgetattr /dev/tty: %s\n",
strerror(errno));
if (tcgetattr(fd, &pc->termios_raw) == -1)
error(FATAL, "tcgetattr /dev/tty: %s\n",
strerror(errno));
close(fd);
pc->termios_raw.c_lflag &= ~ECHO & ~ICANON;
pc->termios_raw.c_cc[VMIN] = (char)1;
pc->termios_raw.c_cc[VTIME] = (char)0;
restore_sanity();
pc->flags |= TTY;
set_my_tty();
SIGACTION(SIGINT, restart, &pc->sigaction, NULL);
readline_init();
}
else {
if (fd < 0)
error(INFO, "/dev/tty: %s\n", strerror(errno));
if (!(pc->flags & SILENT))
fprintf(fp, "NOTE: stdin: not a tty\n\n");
fflush(fp);
pc->flags &= ~TTY;
}
SIGACTION(SIGPIPE, SIG_IGN, &pc->sigaction, NULL);
set_command_prompt(NULL);
}
/*
* Create and stash the original prompt, but allow changes during runtime.
*/
void
set_command_prompt(char *new_prompt)
{
static char *orig_prompt = NULL;
if (!orig_prompt) {
if (!(orig_prompt = (char *)malloc(strlen(pc->program_name)+3)))
error(FATAL, "cannot malloc prompt string\n");
sprintf(orig_prompt, "%s> ", pc->program_name);
}
if (new_prompt)
pc->prompt = new_prompt;
else
pc->prompt = orig_prompt;
}
/*
* SIGINT, SIGPIPE, and SIGSEGV handler.
* Signal number 0 is sent for a generic restart.
*/
#define MAX_RECURSIVE_SIGNALS (10)
#define MAX_SIGINTS_ACCEPTED (1)
void
restart(int sig)
{