-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhush.c
2071 lines (1744 loc) · 47.9 KB
/
hush.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
/* vi: set sw=8 ts=8: */
/*
* hush.c -- a prototype Bourne shell grammar parser
* Intended to follow the original Thompson and Ritchie
* "small and simple is beautiful" philosophy, which
* incidentally is a good match to today's BusyBox.
*
* Copyright (C) 2000,2001 Larry Doolittle <[email protected]>
*
* Credits:
* The parser routines proper are all original material, first
* written Dec 2000 and Jan 2001 by Larry Doolittle.
* The execution engine, the builtins, and much of the underlying
* support has been adapted from busybox-0.49pre's lash,
* which is Copyright (C) 2000 by Lineo, Inc., and
* written by Erik Andersen <[email protected]>, <[email protected]>.
* That, in turn, is based in part on ladsh.c, by Michael K. Johnson and
* Erik W. Troan, which they placed in the public domain. I don't know
* how much of the Johnson/Troan code has survived the repeated rewrites.
* Other credits:
* simple_itoa() was lifted from boa-0.93.15
* b_addchr() derived from similar w_addchar function in glibc-2.2
* setup_redirect(), redirect_opt_num(), and big chunks of main()
* and many builtins derived from contributions by Erik Andersen
* miscellaneous bugfixes from Matt Kraai
*/
/** @page shell_notes Simple Shell Environment: Hush
*
* @par Notes from the source:
*
* Intended to follow the original Thompson and Ritchie "small and simple
* is beautiful" philosophy, which incidentally is a good match to
* today's BusyBox.
*
* There are two big (and related) architecture differences between
* this parser and the lash parser. One is that this version is
* actually designed from the ground up to understand nearly all
* of the Bourne grammar. The second, consequential change is that
* the parser and input reader have been turned inside out. Now,
* the parser is in control, and asks for input as needed. The old
* way had the input reader in control, and it asked for parsing to
* take place as needed. The new way makes it much easier to properly
* handle the recursion implicit in the various substitutions, especially
* across continuation lines.
*
* Bash grammar not implemented: (how many of these were in original sh?)
* - $@ (those sure look like weird quoting rules)
* - $_
* - ! negation operator for pipes
* - &> and >& redirection of stdout+stderr
* - Brace Expansion
* - Tilde Expansion
* - fancy forms of Parameter Expansion
* - aliases
* - Arithmetic Expansion
* - <(list) and >(list) Process Substitution
* - reserved words: case, esac, select, function
* - Here Documents ( << word )
* - Functions
*
* Major bugs:
* - job handling woefully incomplete and buggy
* - reserved word execution woefully incomplete and buggy
*
* to-do:
* - port selected bugfixes from post-0.49 busybox lash - done?
* - finish implementing reserved words: for, while, until, do, done
* - change { and } from special chars to reserved words
* - builtins: break, continue, eval, return, set, trap, ulimit
* - test magic exec
* - handle children going into background
* - clean up recognition of null pipes
* - check setting of global_argc and global_argv
* - control-C handling, probably with longjmp
* - follow IFS rules more precisely, including update semantics
* - figure out what to do with backslash-newline
* - explain why we use signal instead of sigaction
* - propagate syntax errors, die on resource errors?
* - continuation lines, both explicit and implicit - done?
* - memory leak finding and plugging - done?
* - more testing, especially quoting rules and redirection
* - document how quoting rules not precisely followed for variable assignments
* - maybe change map[] to use 2-bit entries
* - (eventually) remove all the printf's
*
* Things that do _not_ work in this environment:
*
* - redirecting (stdout to a file for example)
* - recursion
*
* Enable the "Hush parser" in "General Settings", "Select your shell" to
* get the new console feeling.
*
*/
/*
* 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 <malloc.h> /* malloc, free, realloc*/
#include <xfuncs.h>
#include <linux/ctype.h> /* isalpha, isdigit */
#include <common.h> /* readline */
#include <environment.h>
#include <command.h> /* find_cmd */
#include <driver.h>
#include <errno.h>
#include <fs.h>
#include <libbb.h>
#include <password.h>
#include <glob.h>
#include <getopt.h>
#include <libfile.h>
#include <libbb.h>
#include <magicvar.h>
#include <linux/list.h>
#include <binfmt.h>
#include <init.h>
#include <shell.h>
/*cmd_boot.c*/
extern int do_bootd(int flag, int argc, char *argv[]); /* do_bootd */
#define SPECIAL_VAR_SYMBOL 03
#define EXIT_SUCCESS 0
#define EOF -1
#define xstrdup strdup
#define error_msg printf
typedef enum {
PIPE_SEQ = 1,
PIPE_AND = 2,
PIPE_OR = 3,
PIPE_BG = 4,
} pipe_style;
/* might eventually control execution */
typedef enum {
RES_NONE = 0,
RES_IF = 1,
RES_THEN = 2,
RES_ELIF = 3,
RES_ELSE = 4,
RES_FI = 5,
RES_FOR = 6,
RES_WHILE = 7,
RES_UNTIL = 8,
RES_DO = 9,
RES_DONE = 10,
RES_XXXX = 11,
RES_IN = 12,
RES_SNTX = 13
} reserved_style;
#define FLAG_END (1<<RES_NONE)
#define FLAG_IF (1<<RES_IF)
#define FLAG_THEN (1<<RES_THEN)
#define FLAG_ELIF (1<<RES_ELIF)
#define FLAG_ELSE (1<<RES_ELSE)
#define FLAG_FI (1<<RES_FI)
#define FLAG_FOR (1<<RES_FOR)
#define FLAG_WHILE (1<<RES_WHILE)
#define FLAG_UNTIL (1<<RES_UNTIL)
#define FLAG_DO (1<<RES_DO)
#define FLAG_DONE (1<<RES_DONE)
#define FLAG_IN (1<<RES_IN)
#define FLAG_START (1<<RES_XXXX)
#define FLAG_EXIT_FROM_LOOP 1
#define FLAG_PARSE_SEMICOLON (1 << 1) /* symbol ';' is special for parser */
#define FLAG_REPARSING (1 << 2) /* >=2nd pass */
struct option {
struct list_head list;
char opt;
char *optarg;
};
/* This holds pointers to the various results of parsing */
struct p_context {
struct child_prog *child;
struct pipe *list_head;
struct pipe *pipe;
reserved_style w;
int old_flag; /* for figuring out valid reserved words */
struct p_context *stack;
int type; /* define type of parser : ";$" common or special symbol */
/* How about quoting status? */
char **global_argv;
unsigned int global_argc;
int options_parsed;
struct list_head options;
};
struct child_prog {
char **argv; /* program name and arguments */
int argc; /* number of program arguments */
struct pipe *group; /* if non-NULL, first in group or subshell */
int sp; /* number of SPECIAL_VAR_SYMBOL */
int type;
glob_t glob_result; /* result of parameter globbing */
};
struct pipe {
int num_progs; /* total number of programs in job */
struct child_prog *progs; /* array of commands in pipe */
struct pipe *next; /* to track background commands */
pipe_style followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */
reserved_style r_mode; /* supports if, for, while, until */
};
static char console_buffer[CONFIG_CBSIZE]; /* console I/O buffer */
/* globals, connect us to the outside world
* the first three support $?, $#, and $1 */
static unsigned int last_return_code;
int shell_get_last_return_code(void)
{
return last_return_code;
}
/* "globals" within this file */
static uchar *ifs;
static char map[256];
#define B_CHUNK (100)
#define B_NOSPAC 1
typedef struct {
char *data;
int length;
int maxlen;
int quote;
int nonnull;
} o_string;
#define NULL_O_STRING {NULL,0,0,0,0}
/* used for initialization:
o_string foo = NULL_O_STRING; */
/* I can almost use ordinary FILE *. Is open_memstream() universally
* available? Where is it documented? */
struct in_str {
const char *p;
int interrupt;
int promptmode;
int (*get) (struct in_str *);
int (*peek) (struct in_str *);
};
#define b_getch(input) ((input)->get(input))
#define b_peek(input) ((input)->peek(input))
#define final_printf debug
static void syntax(void)
{
printf("syntax error\n");
}
static void syntaxf(const char *fmt, ...)
{
va_list args;
printf("syntax error: ");
va_start(args, fmt);
vprintf(fmt, args);
va_end(args);
}
static void syntax_unexpected_token(const char *token)
{
syntaxf("unexpected token `%s'\n", token);
}
/* o_string manipulation: */
static int b_check_space(o_string *o, int len);
static int b_addchr(o_string *o, int ch);
static void b_reset(o_string *o);
/* in_str manipulations: */
static int static_get(struct in_str *i);
static int static_peek(struct in_str *i);
static int file_get(struct in_str *i);
static int file_peek(struct in_str *i);
static void setup_file_in_str(struct in_str *i);
static void setup_string_in_str(struct in_str *i, const char *s);
/* "run" the final data structures: */
static int free_pipe_list(struct pipe *head, int indent);
static int free_pipe(struct pipe *pi, int indent);
/* really run the final data structures: */
static int run_list_real(struct p_context *ctx, struct pipe *pi);
static int run_pipe_real(struct p_context *ctx, struct pipe *pi);
/* extended glob support: */
/* variable assignment: */
static int is_assignment(const char *s);
/* data structure manipulation: */
static void initialize_context(struct p_context *ctx);
static void release_context(struct p_context *ctx);
static int done_word(o_string *dest, struct p_context *ctx);
static int done_command(struct p_context *ctx);
static int done_pipe(struct p_context *ctx, pipe_style type);
/* primary string parsing: */
static const char *lookup_param(char *src);
static char *make_string(char **inp);
static int handle_dollar(o_string *dest, struct p_context *ctx, struct in_str *input);
static int parse_stream(o_string *dest, struct p_context *ctx, struct in_str *input0, int end_trigger);
/* setup: */
static int parse_stream_outer(struct p_context *ctx, struct in_str *inp, int flag);
static int parse_string_outer(struct p_context *ctx, const char *s, int flag);
/* local variable support */
static char **make_list_in(char **inp, char *name);
static char *insert_var_value(char *inp);
static int set_local_var(const char *s, int flg_export);
static int execute_script(const char *path, int argc, char *argv[]);
static int source_script(const char *path, int argc, char *argv[]);
static int b_check_space(o_string *o, int len)
{
/* It would be easy to drop a more restrictive policy
* in here, such as setting a maximum string length */
if (o->length + len > o->maxlen) {
char *old_data = o->data;
/* assert (data == NULL || o->maxlen != 0); */
o->maxlen += max(2*len, B_CHUNK);
o->data = realloc(o->data, 1 + o->maxlen);
if (o->data == NULL) {
free(old_data);
}
}
return o->data == NULL;
}
static int b_addchr(o_string *o, int ch)
{
debug("%s: %c %d %p\n", __func__, ch, o->length, o);
if (b_check_space(o, 1))
return B_NOSPAC;
o->data[o->length] = ch;
o->length++;
o->data[o->length] = '\0';
return 0;
}
static int b_addstr(o_string *o, const char *str)
{
int ret;
while (*str) {
ret = b_addchr(o, *str++);
if (ret)
return ret;
}
return 0;
}
static void b_reset(o_string *o)
{
o->length = 0;
o->nonnull = 0;
if (o->data != NULL)
*o->data = '\0';
}
static void b_free(o_string *o)
{
b_reset(o);
free(o->data);
o->data = NULL;
o->maxlen = 0;
}
static int b_adduint(o_string *o, unsigned int i)
{
int r;
char *p = simple_itoa(i);
/* no escape checking necessary */
do {
r = b_addchr(o, *p++);
} while (r == 0 && *p);
return r;
}
static int static_get(struct in_str *i)
{
int ch = *i->p++;
if (ch == '\0')
return EOF;
return ch;
}
static int static_peek(struct in_str *i)
{
return *i->p;
}
static char *getprompt(void)
{
static char prompt[PATH_MAX + 32];
#ifdef CONFIG_HUSH_FANCY_PROMPT
const char *ps1 = getenv("PS1");
if (ps1)
process_escape_sequence(ps1, prompt, PATH_MAX + 32);
else
#endif
sprintf(prompt, "%s%s ", CONFIG_PROMPT, getcwd());
return prompt;
}
static void get_user_input(struct in_str *i)
{
int n;
static char the_command[CONFIG_CBSIZE];
char *prompt;
if (i->promptmode == 1)
prompt = getprompt();
else
prompt = CONFIG_PROMPT_HUSH_PS2;
n = readline(prompt, console_buffer, CONFIG_CBSIZE);
if (n == -1 ) {
i->interrupt = 1;
n = 0;
}
console_buffer[n] = '\n';
console_buffer[n + 1]= '\0';
if (i->promptmode == 1) {
strcpy(the_command,console_buffer);
i->p = the_command;
return;
}
if (console_buffer[0] != '\n') {
if (strlen(the_command) + strlen(console_buffer)
< CONFIG_CBSIZE) {
n = strlen(the_command);
the_command[n - 1] = ' ';
strcpy(&the_command[n], console_buffer);
} else {
the_command[0] = '\n';
the_command[1] = '\0';
}
}
if (i->interrupt) {
the_command[0] = '\n';
the_command[1] = '\0';
}
i->p = console_buffer;
}
/* This is the magic location that prints prompts
* and gets data back from the user */
static int file_get(struct in_str *i)
{
int ch;
ch = 0;
/* If there is data waiting, eat it up */
if (i->p && *i->p)
return *i->p++;
/* need to double check i->file because we might be doing something
* more complicated by now, like sourcing or substituting. */
while (!i->p || strlen(i->p) == 0 )
get_user_input(i);
i->promptmode = 2;
if (i->p && *i->p)
ch = *i->p++;
debug("%s: got a %d\n", __func__, ch);
return ch;
}
/* All the callers guarantee this routine will never be
* used right after a newline, so prompting is not needed.
*/
static int file_peek(struct in_str *i)
{
return *i->p;
}
static void setup_file_in_str(struct in_str *i)
{
i->peek = file_peek;
i->get = file_get;
i->interrupt = 0;
i->promptmode = 1;
i->p = NULL;
}
static void setup_string_in_str(struct in_str *i, const char *s)
{
i->peek = static_peek;
i->get = static_get;
i->interrupt = 0;
i->promptmode = 1;
i->p = s;
}
#ifdef CONFIG_CMD_GETOPT
static int builtin_getopt(struct p_context *ctx, struct child_prog *child,
int argc, char *argv[])
{
char *optstring, *var;
int opt, ret = 0;
char opta[2];
struct option *o;
struct getopt_context gc;
if (argc != 3)
return -2 - 1;
optstring = argv[1];
var = argv[2];
getopt_context_store(&gc);
if (!ctx->options_parsed) {
while ((opt = getopt(ctx->global_argc, ctx->global_argv, optstring)) > 0) {
o = xzalloc(sizeof(*o));
o->opt = opt;
o->optarg = xstrdup(optarg);
list_add_tail(&o->list, &ctx->options);
}
ctx->global_argv += optind - 1;
ctx->global_argc -= optind - 1;
}
ctx->options_parsed = 1;
if (list_empty(&ctx->options)) {
ret = -1;
goto out;
}
o = list_first_entry(&ctx->options, struct option, list);
opta[0] = o->opt;
opta[1] = 0;
setenv(var, opta);
setenv("OPTARG", o->optarg);
free(o->optarg);
list_del(&o->list);
free(o);
out:
getopt_context_restore(&gc);
return ret;
}
BAREBOX_MAGICVAR(OPTARG, "optarg for hush builtin getopt");
#else
static int builtin_getopt(struct p_context *ctx, struct child_prog *child,
int argc, char *argv[])
{
return -1;
}
#endif
static int builtin_exit(struct p_context *ctx, struct child_prog *child,
int argc, char *argv[])
{
int r;
r = last_return_code;
if (argc > 1)
r = simple_strtoul(argv[1], NULL, 0);
return -r - 2;
}
static void remove_quotes_in_str(char *src)
{
char *trg = src;
while (*src) {
if (*src == '\'') {
src++;
while (*src != '\'')
*trg++ = *src++;
src++;
continue;
}
/* drop quotes */
if (*src == '"') {
src++;
continue;
}
/* replace \" with " */
if (*src == '\\' && *(src + 1) == '"') {
*trg++ = '"';
src += 2;
continue;
}
/* replace \' with ' */
if (*src == '\\' && *(src + 1) == '\'') {
*trg++ = '\'';
src += 2;
continue;
}
/* replace \\ with \ */
if (*src == '\\' && *(src + 1) == '\\') {
*trg++ = '\\';
src += 2;
continue;
}
*trg++ = *src++;
}
*trg = 0;
}
static void remove_quotes(int argc, char *argv[])
{
int i;
for (i = 0; i < argc; i++)
remove_quotes_in_str(argv[i]);
}
static int fake_glob(const char *src, int flags,
int (*errfunc) (const char *epath, int eerrno),
glob_t *pglob)
{
int pathc;
if (!(flags & GLOB_APPEND)) {
globfree(pglob);
pglob->gl_pathv = NULL;
pglob->gl_pathc = 0;
pglob->gl_offs = 0;
}
pathc = ++pglob->gl_pathc;
pglob->gl_pathv = xrealloc(pglob->gl_pathv, (pathc + 1) * sizeof(*pglob->gl_pathv));
pglob->gl_pathv[pathc - 1] = xstrdup(src);
pglob->gl_pathv[pathc] = NULL;
return 0;
}
#ifdef CONFIG_GLOB
static int do_glob(const char *src, int flags,
int (*errfunc) (const char *epath, int eerrno),
glob_t *pglob)
{
return glob(src, flags, errfunc, pglob);
}
#else
static int do_glob(const char *src, int flags,
int (*errfunc) (const char *epath, int eerrno),
glob_t *pglob)
{
return fake_glob(src, flags, errfunc, pglob);
}
#endif
static void do_glob_in_argv(glob_t *globbuf, int argc, char **argv)
{
int i;
int flags;
globbuf->gl_offs = 0;
flags = GLOB_DOOFFS | GLOB_NOCHECK;
for (i = 0; i < argc; i++) {
int ret;
ret = do_glob(argv[i], flags, NULL, globbuf);
flags |= GLOB_APPEND;
}
}
/* run_pipe_real() starts all the jobs, but doesn't wait for anything
* to finish. See checkjobs().
*
* return code is normally -1, when the caller has to wait for children
* to finish to determine the exit status of the pipe. If the pipe
* is a simple builtin command, however, the action is done by the
* time run_pipe_real returns, and the exit code is provided as the
* return value.
*
* The input of the pipe is always stdin, the output is always
* stdout. The outpipe[] mechanism in BusyBox-0.48 lash is bogus,
* because it tries to avoid running the command substitution in
* subshell, when that is in fact necessary. The subshell process
* now has its stdout directed to the input of the appropriate pipe,
* so this routine is noticeably simpler.
*/
static int run_pipe_real(struct p_context *ctx, struct pipe *pi)
{
int i;
int nextin;
struct child_prog *child;
char *p;
glob_t globbuf = {};
int ret;
int rcode;
# if __GNUC__
/* Avoid longjmp clobbering */
(void) &i;
(void) &nextin;
(void) &child;
# endif
nextin = 0;
/*
* We do not support pipes in barebox, so pi->num_progs can't
* be bigger than 1. pi->num_progs == 0 is already catched in
* the caller, so everything else than 1 is a bug.
*/
BUG_ON(pi->num_progs != 1);
child = &pi->progs[0];
if (child->group) {
debug("non-subshell grouping\n");
rcode = run_list_real(ctx, child->group);
return rcode;
}
if (!pi->progs[0].argv)
return -1;
for (i = 0; is_assignment(child->argv[i]); i++)
{ /* nothing */ }
if (i != 0 && child->argv[i] == NULL) {
/* assignments, but no command: set the local environment */
for (i = 0; child->argv[i] != NULL; i++) {
/* Ok, this case is tricky. We have to decide if this is a
* local variable, or an already exported variable. If it is
* already exported, we have to export the new value. If it is
* not exported, we need only set this as a local variable.
* This junk is all to decide whether or not to export this
* variable. */
int export_me = 0;
char *name, *value;
name = xstrdup(child->argv[i]);
debug("Local environment set: %s\n", name);
value = strchr(name, '=');
if (value)
*value = 0;
free(name);
p = insert_var_value(child->argv[i]);
rcode = set_local_var(p, export_me);
if (rcode)
return 1;
if (p != child->argv[i])
free(p);
}
return EXIT_SUCCESS; /* don't worry about errors in set_local_var() yet */
}
for (i = 0; is_assignment(child->argv[i]); i++) {
p = insert_var_value(child->argv[i]);
rcode = set_local_var(p, 0);
if (rcode)
return 1;
if (p != child->argv[i]) {
child->sp--;
free(p);
}
}
if (child->sp) {
char * str = NULL;
struct p_context ctx1;
initialize_context(&ctx1);
str = make_string((child->argv + i));
rcode = parse_string_outer(&ctx1, str, FLAG_EXIT_FROM_LOOP | FLAG_REPARSING);
release_context(&ctx1);
free(str);
return rcode;
}
do_glob_in_argv(&globbuf, child->argc - i, &child->argv[i]);
remove_quotes(globbuf.gl_pathc, globbuf.gl_pathv);
if (!strcmp(globbuf.gl_pathv[0], "getopt") &&
IS_ENABLED(CONFIG_CMD_GETOPT)) {
ret = builtin_getopt(ctx, child, globbuf.gl_pathc, globbuf.gl_pathv);
} else if (!strcmp(globbuf.gl_pathv[0], "exit")) {
ret = builtin_exit(ctx, child, globbuf.gl_pathc, globbuf.gl_pathv);
} else {
ret = execute_binfmt(globbuf.gl_pathc, globbuf.gl_pathv);
if (ret < 0) {
printf("%s: %s\n", globbuf.gl_pathv[0], strerror(-ret));
ret = 127;
}
}
globfree(&globbuf);
return ret;
}
static int run_list_real(struct p_context *ctx, struct pipe *pi)
{
char *save_name = NULL;
char **list = NULL;
char **save_list = NULL;
struct pipe *rpipe;
int flag_rep = 0;
int rcode=0, flag_skip=1;
int flag_restore = 0;
int if_code=0, next_if_code=0; /* need double-buffer to handle elif */
reserved_style rmode, skip_more_in_this_rmode = RES_XXXX;
/* check syntax for "for" */
for (rpipe = pi; rpipe; rpipe = rpipe->next) {
if ((rpipe->r_mode == RES_IN ||
rpipe->r_mode == RES_FOR) &&
(rpipe->next == NULL)) {
syntax();
return 1;
}
if ((rpipe->r_mode == RES_IN &&
(rpipe->next->r_mode == RES_IN &&
rpipe->next->progs->argv != NULL))||
(rpipe->r_mode == RES_FOR &&
rpipe->next->r_mode != RES_IN)) {
syntax();
return 1;
}
}
for (; pi; pi = (flag_restore != 0) ? rpipe : pi->next) {
if (pi->r_mode == RES_WHILE || pi->r_mode == RES_UNTIL ||
pi->r_mode == RES_FOR) {
/* check Ctrl-C */
if (ctrlc())
return 1;
flag_restore = 0;
if (!rpipe) {
flag_rep = 0;
rpipe = pi;
}
}
rmode = pi->r_mode;
debug("rmode=%d if_code=%d next_if_code=%d skip_more=%d\n",
rmode, if_code, next_if_code, skip_more_in_this_rmode);
if (rmode == skip_more_in_this_rmode && flag_skip) {
if (pi->followup == PIPE_SEQ)
flag_skip=0;
continue;
}
flag_skip = 1;
skip_more_in_this_rmode = RES_XXXX;
if (rmode == RES_THEN || rmode == RES_ELSE)
if_code = next_if_code;
if (rmode == RES_THEN && if_code)
continue;
if (rmode == RES_ELSE && !if_code)
continue;
if (rmode == RES_ELIF && !if_code)
break;
if (rmode == RES_FOR && pi->num_progs) {
if (!list) {
/* if no variable values after "in" we skip "for" */
if (!pi->next->progs->argv)
continue;
/* create list of variable values */
list = make_list_in(pi->next->progs->argv,
pi->progs->argv[0]);
save_list = list;
save_name = pi->progs->argv[0];
pi->progs->argv[0] = NULL;
flag_rep = 1;
}
if (!(*list)) {
free(pi->progs->argv[0]);
free(save_list);
list = NULL;
flag_rep = 0;
pi->progs->argv[0] = save_name;
continue;
} else {
/* insert new value from list for variable */
if (pi->progs->argv[0])
free(pi->progs->argv[0]);
pi->progs->argv[0] = *list++;
}
}
if (rmode == RES_IN)
continue;
if (rmode == RES_DO) {
if (!flag_rep)
continue;
}
if (rmode == RES_DONE) {
if (flag_rep) {
flag_restore = 1;
} else {
rpipe = NULL;
}
}
if (pi->num_progs == 0)
continue;
rcode = run_pipe_real(ctx, pi);
debug("run_pipe_real returned %d\n",rcode);
if (rcode < -1) {
last_return_code = -rcode - 2;
return rcode; /* exit */
}
last_return_code = rcode;
if (rmode == RES_IF || rmode == RES_ELIF )
next_if_code = rcode; /* can be overwritten a number of times */
if (rmode == RES_WHILE)
flag_rep = !last_return_code;
if (rmode == RES_UNTIL)
flag_rep = last_return_code;
if ((rcode == EXIT_SUCCESS && pi->followup == PIPE_OR) ||
(rcode != EXIT_SUCCESS && pi->followup == PIPE_AND) )
skip_more_in_this_rmode = rmode;
}
return rcode;
}
/* broken, of course, but OK for testing */
static __maybe_unused char *indenter(int i)
{
static char blanks[] = " ";
return &blanks[sizeof(blanks) - i - 1];
}
/* return code is the exit status of the pipe */
static int free_pipe(struct pipe *pi, int indent)
{
char **p;
struct child_prog *child;
int a, i, ret_code = 0;