blob: 748ef60ac90b8af8052631c7ad3a6aed2629140b [file] [log] [blame]
Francis Laniel110b7692023-12-22 22:02:27 +01001/* vi: set sw=4 ts=4: */
2/*
3 * A prototype Bourne shell grammar parser.
4 * Intended to follow the original Thompson and Ritchie
5 * "small and simple is beautiful" philosophy, which
6 * incidentally is a good match to today's BusyBox.
7 *
8 * Copyright (C) 2000,2001 Larry Doolittle <larry@doolittle.boa.org>
9 * Copyright (C) 2008,2009 Denys Vlasenko <vda.linux@googlemail.com>
10 *
11 * Licensed under GPLv2 or later, see file LICENSE in this source tree.
12 *
13 * Credits:
14 * The parser routines proper are all original material, first
15 * written Dec 2000 and Jan 2001 by Larry Doolittle. The
16 * execution engine, the builtins, and much of the underlying
17 * support has been adapted from busybox-0.49pre's lash, which is
18 * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
19 * written by Erik Andersen <andersen@codepoet.org>. That, in turn,
20 * is based in part on ladsh.c, by Michael K. Johnson and Erik W.
21 * Troan, which they placed in the public domain. I don't know
22 * how much of the Johnson/Troan code has survived the repeated
23 * rewrites.
24 *
25 * Other credits:
26 * o_addchr derived from similar w_addchar function in glibc-2.2.
27 * parse_redirect, redirect_opt_num, and big chunks of main
28 * and many builtins derived from contributions by Erik Andersen.
29 * Miscellaneous bugfixes from Matt Kraai.
30 *
31 * There are two big (and related) architecture differences between
32 * this parser and the lash parser. One is that this version is
33 * actually designed from the ground up to understand nearly all
34 * of the Bourne grammar. The second, consequential change is that
35 * the parser and input reader have been turned inside out. Now,
36 * the parser is in control, and asks for input as needed. The old
37 * way had the input reader in control, and it asked for parsing to
38 * take place as needed. The new way makes it much easier to properly
39 * handle the recursion implicit in the various substitutions, especially
40 * across continuation lines.
41 *
42 * TODOs:
43 * grep for "TODO" and fix (some of them are easy)
44 * make complex ${var%...} constructs support optional
45 * make here documents optional
46 * special variables (done: PWD, PPID, RANDOM)
47 * follow IFS rules more precisely, including update semantics
48 * tilde expansion
49 * aliases
50 * "command" missing features:
51 * command -p CMD: run CMD using default $PATH
52 * (can use this to override standalone shell as well?)
53 * command BLTIN: disables special-ness (e.g. errors do not abort)
54 * command -V CMD1 CMD2 CMD3 (multiple args) (not in standard)
55 * builtins mandated by standards we don't support:
56 * [un]alias, fc:
57 * fc -l[nr] [BEG] [END]: list range of commands in history
58 * fc [-e EDITOR] [BEG] [END]: edit/rerun range of commands
59 * fc -s [PAT=REP] [CMD]: rerun CMD, replacing PAT with REP
60 *
61 * Bash compat TODO:
62 * redirection of stdout+stderr: &> and >&
63 * reserved words: function select
64 * advanced test: [[ ]]
65 * process substitution: <(list) and >(list)
66 * let EXPR [EXPR...]
67 * Each EXPR is an arithmetic expression (ARITHMETIC EVALUATION)
68 * If the last arg evaluates to 0, let returns 1; 0 otherwise.
69 * NB: let `echo 'a=a + 1'` - error (IOW: multi-word expansion is used)
70 * ((EXPR))
71 * The EXPR is evaluated according to ARITHMETIC EVALUATION.
72 * This is exactly equivalent to let "EXPR".
73 * $[EXPR]: synonym for $((EXPR))
74 * indirect expansion: ${!VAR}
75 * substring op on @: ${@:n:m}
76 *
77 * Won't do:
78 * Some builtins mandated by standards:
79 * newgrp [GRP]: not a builtin in bash but a suid binary
80 * which spawns a new shell with new group ID
81 *
82 * Status of [[ support:
83 * [[ args ]] are CMD_SINGLEWORD_NOGLOB:
84 * v='a b'; [[ $v = 'a b' ]]; echo 0:$?
85 * [[ /bin/n* ]]; echo 0:$?
86 * = is glob match operator, not equality operator: STR = GLOB
87 * == same as =
88 * =~ is regex match operator: STR =~ REGEX
89 * TODO:
90 * quoting needs to be considered (-f is an operator, "-f" and ""-f are not; etc)
91 * in word = GLOB, quoting should be significant on char-by-char basis: a*cd"*"
92 */
93//config:config HUSH
Francis Laniele7ca3a32023-12-22 22:02:42 +010094//config: bool "hush (70 kb)"
Francis Laniel110b7692023-12-22 22:02:27 +010095//config: default y
96//config: select SHELL_HUSH
97//config: help
98//config: hush is a small shell. It handles the normal flow control
99//config: constructs such as if/then/elif/else/fi, for/in/do/done, while loops,
100//config: case/esac. Redirections, here documents, $((arithmetic))
101//config: and functions are supported.
102//config:
103//config: It will compile and work on no-mmu systems.
104//config:
105//config: It does not handle select, aliases, tilde expansion,
106//config: &>file and >&file redirection of stdout+stderr.
107//config:
108// This option is visible (has a description) to make it possible to select
109// a "scripted" applet (such as NOLOGIN) but avoid selecting any shells:
110//config:config SHELL_HUSH
111//config: bool "Internal shell for embedded script support"
112//config: default n
113//config:
114//config:# hush options
115//config:# It's only needed to get "nice" menuconfig indenting.
116//config:if SHELL_HUSH || HUSH || SH_IS_HUSH || BASH_IS_HUSH
117//config:
118//config:config HUSH_BASH_COMPAT
119//config: bool "bash-compatible extensions"
120//config: default y
121//config: depends on SHELL_HUSH
122//config:
123//config:config HUSH_BRACE_EXPANSION
124//config: bool "Brace expansion"
125//config: default y
126//config: depends on HUSH_BASH_COMPAT
127//config: help
128//config: Enable {abc,def} extension.
129//config:
130//config:config HUSH_BASH_SOURCE_CURDIR
131//config: bool "'source' and '.' builtins search current directory after $PATH"
132//config: default n # do not encourage non-standard behavior
133//config: depends on HUSH_BASH_COMPAT
134//config: help
135//config: This is not compliant with standards. Avoid if possible.
136//config:
137//config:config HUSH_LINENO_VAR
138//config: bool "$LINENO variable (bashism)"
139//config: default y
140//config: depends on SHELL_HUSH
141//config:
142//config:config HUSH_INTERACTIVE
143//config: bool "Interactive mode"
144//config: default y
145//config: depends on SHELL_HUSH
146//config: help
147//config: Enable interactive mode (prompt and command editing).
148//config: Without this, hush simply reads and executes commands
149//config: from stdin just like a shell script from a file.
150//config: No prompt, no PS1/PS2 magic shell variables.
151//config:
152//config:config HUSH_SAVEHISTORY
153//config: bool "Save command history to .hush_history"
154//config: default y
155//config: depends on HUSH_INTERACTIVE && FEATURE_EDITING_SAVEHISTORY
156//config:
157//config:config HUSH_JOB
158//config: bool "Job control"
159//config: default y
160//config: depends on HUSH_INTERACTIVE
161//config: help
162//config: Enable job control: Ctrl-Z backgrounds, Ctrl-C interrupts current
163//config: command (not entire shell), fg/bg builtins work. Without this option,
164//config: "cmd &" still works by simply spawning a process and immediately
165//config: prompting for next command (or executing next command in a script),
166//config: but no separate process group is formed.
167//config:
168//config:config HUSH_TICK
169//config: bool "Support command substitution"
170//config: default y
171//config: depends on SHELL_HUSH
172//config: help
173//config: Enable `command` and $(command).
174//config:
175//config:config HUSH_IF
176//config: bool "Support if/then/elif/else/fi"
177//config: default y
178//config: depends on SHELL_HUSH
179//config:
180//config:config HUSH_LOOPS
181//config: bool "Support for, while and until loops"
182//config: default y
183//config: depends on SHELL_HUSH
184//config:
185//config:config HUSH_CASE
186//config: bool "Support case ... esac statement"
187//config: default y
188//config: depends on SHELL_HUSH
189//config: help
190//config: Enable case ... esac statement. +400 bytes.
191//config:
192//config:config HUSH_FUNCTIONS
193//config: bool "Support funcname() { commands; } syntax"
194//config: default y
195//config: depends on SHELL_HUSH
196//config: help
197//config: Enable support for shell functions. +800 bytes.
198//config:
199//config:config HUSH_LOCAL
200//config: bool "local builtin"
201//config: default y
202//config: depends on HUSH_FUNCTIONS
203//config: help
204//config: Enable support for local variables in functions.
205//config:
206//config:config HUSH_RANDOM_SUPPORT
207//config: bool "Pseudorandom generator and $RANDOM variable"
208//config: default y
209//config: depends on SHELL_HUSH
210//config: help
211//config: Enable pseudorandom generator and dynamic variable "$RANDOM".
212//config: Each read of "$RANDOM" will generate a new pseudorandom value.
213//config:
214//config:config HUSH_MODE_X
215//config: bool "Support 'hush -x' option and 'set -x' command"
216//config: default y
217//config: depends on SHELL_HUSH
218//config: help
219//config: This instructs hush to print commands before execution.
220//config: Adds ~300 bytes.
221//config:
222//config:config HUSH_ECHO
223//config: bool "echo builtin"
224//config: default y
225//config: depends on SHELL_HUSH
226//config:
227//config:config HUSH_PRINTF
228//config: bool "printf builtin"
229//config: default y
230//config: depends on SHELL_HUSH
231//config:
232//config:config HUSH_TEST
233//config: bool "test builtin"
234//config: default y
235//config: depends on SHELL_HUSH
236//config:
237//config:config HUSH_HELP
238//config: bool "help builtin"
239//config: default y
240//config: depends on SHELL_HUSH
241//config:
242//config:config HUSH_EXPORT
243//config: bool "export builtin"
244//config: default y
245//config: depends on SHELL_HUSH
246//config:
247//config:config HUSH_EXPORT_N
248//config: bool "Support 'export -n' option"
249//config: default y
250//config: depends on HUSH_EXPORT
251//config: help
252//config: export -n unexports variables. It is a bash extension.
253//config:
254//config:config HUSH_READONLY
255//config: bool "readonly builtin"
256//config: default y
257//config: depends on SHELL_HUSH
258//config: help
259//config: Enable support for read-only variables.
260//config:
261//config:config HUSH_KILL
262//config: bool "kill builtin (supports kill %jobspec)"
263//config: default y
264//config: depends on SHELL_HUSH
265//config:
266//config:config HUSH_WAIT
267//config: bool "wait builtin"
268//config: default y
269//config: depends on SHELL_HUSH
270//config:
271//config:config HUSH_COMMAND
272//config: bool "command builtin"
273//config: default y
274//config: depends on SHELL_HUSH
275//config:
276//config:config HUSH_TRAP
277//config: bool "trap builtin"
278//config: default y
279//config: depends on SHELL_HUSH
280//config:
281//config:config HUSH_TYPE
282//config: bool "type builtin"
283//config: default y
284//config: depends on SHELL_HUSH
285//config:
286//config:config HUSH_TIMES
287//config: bool "times builtin"
288//config: default y
289//config: depends on SHELL_HUSH
290//config:
291//config:config HUSH_READ
292//config: bool "read builtin"
293//config: default y
294//config: depends on SHELL_HUSH
295//config:
296//config:config HUSH_SET
297//config: bool "set builtin"
298//config: default y
299//config: depends on SHELL_HUSH
300//config:
301//config:config HUSH_UNSET
302//config: bool "unset builtin"
303//config: default y
304//config: depends on SHELL_HUSH
305//config:
306//config:config HUSH_ULIMIT
307//config: bool "ulimit builtin"
308//config: default y
309//config: depends on SHELL_HUSH
310//config:
311//config:config HUSH_UMASK
312//config: bool "umask builtin"
313//config: default y
314//config: depends on SHELL_HUSH
315//config:
316//config:config HUSH_GETOPTS
317//config: bool "getopts builtin"
318//config: default y
319//config: depends on SHELL_HUSH
320//config:
321//config:config HUSH_MEMLEAK
322//config: bool "memleak builtin (debugging)"
323//config: default n
324//config: depends on SHELL_HUSH
325//config:
326//config:endif # hush options
327
328//applet:IF_HUSH(APPLET(hush, BB_DIR_BIN, BB_SUID_DROP))
329// APPLET_ODDNAME:name main location suid_type help
330//applet:IF_SH_IS_HUSH( APPLET_ODDNAME(sh, hush, BB_DIR_BIN, BB_SUID_DROP, hush))
331//applet:IF_BASH_IS_HUSH(APPLET_ODDNAME(bash, hush, BB_DIR_BIN, BB_SUID_DROP, hush))
332
333//kbuild:lib-$(CONFIG_SHELL_HUSH) += hush.o match.o shell_common.o
334//kbuild:lib-$(CONFIG_HUSH_RANDOM_SUPPORT) += random.o
335
336/* -i (interactive) is also accepted,
337 * but does nothing, therefore not shown in help.
338 * NOMMU-specific options are not meant to be used by users,
339 * therefore we don't show them either.
340 */
341//usage:#define hush_trivial_usage
Francis Laniele7ca3a32023-12-22 22:02:42 +0100342//usage: "[-enxl] [-c 'SCRIPT' [ARG0 ARGS] | FILE ARGS | -s ARGS]"
Francis Laniel110b7692023-12-22 22:02:27 +0100343//usage:#define hush_full_usage "\n\n"
344//usage: "Unix shell interpreter"
345
Francis Laniel36836fc2023-12-22 22:02:28 +0100346#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +0100347#if !(defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \
348 || defined(__APPLE__) \
349 )
350# include <malloc.h> /* for malloc_trim */
351#endif
352#include <glob.h>
353/* #include <dmalloc.h> */
354#if ENABLE_HUSH_CASE
355# include <fnmatch.h>
356#endif
357#include <sys/times.h>
358#include <sys/utsname.h> /* for setting $HOSTNAME */
359
360#include "busybox.h" /* for APPLET_IS_NOFORK/NOEXEC */
361#include "unicode.h"
362#include "shell_common.h"
363#include "math.h"
364#include "match.h"
365#if ENABLE_HUSH_RANDOM_SUPPORT
366# include "random.h"
367#else
368# define CLEAR_RANDOM_T(rnd) ((void)0)
369#endif
370#ifndef O_CLOEXEC
371# define O_CLOEXEC 0
372#endif
373#ifndef F_DUPFD_CLOEXEC
374# define F_DUPFD_CLOEXEC F_DUPFD
375#endif
376
Francis Laniele7ca3a32023-12-22 22:02:42 +0100377#if ENABLE_FEATURE_SH_EMBEDDED_SCRIPTS && !ENABLE_SHELL_ASH
Francis Laniel110b7692023-12-22 22:02:27 +0100378# include "embedded_scripts.h"
379#else
380# define NUM_SCRIPTS 0
381#endif
Francis Laniel36836fc2023-12-22 22:02:28 +0100382#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +0100383
384/* So far, all bash compat is controlled by one config option */
385/* Separate defines document which part of code implements what */
386#define BASH_PATTERN_SUBST ENABLE_HUSH_BASH_COMPAT
387#define BASH_SUBSTR ENABLE_HUSH_BASH_COMPAT
388#define BASH_SOURCE ENABLE_HUSH_BASH_COMPAT
389#define BASH_DOLLAR_SQUOTE ENABLE_HUSH_BASH_COMPAT
390#define BASH_HOSTNAME_VAR ENABLE_HUSH_BASH_COMPAT
391#define BASH_EPOCH_VARS ENABLE_HUSH_BASH_COMPAT
392#define BASH_TEST2 (ENABLE_HUSH_BASH_COMPAT && ENABLE_HUSH_TEST)
393#define BASH_READ_D ENABLE_HUSH_BASH_COMPAT
394
Francis Laniel110b7692023-12-22 22:02:27 +0100395/* Build knobs */
396#define LEAK_HUNTING 0
397#define BUILD_AS_NOMMU 0
398/* Enable/disable sanity checks. Ok to enable in production,
399 * only adds a bit of bloat. Set to >1 to get non-production level verbosity.
400 * Keeping 1 for now even in released versions.
401 */
402#define HUSH_DEBUG 1
403/* Slightly bigger (+200 bytes), but faster hush.
404 * So far it only enables a trick with counting SIGCHLDs and forks,
405 * which allows us to do fewer waitpid's.
406 * (we can detect a case where neither forks were done nor SIGCHLDs happened
407 * and therefore waitpid will return the same result as last time)
408 */
409#define ENABLE_HUSH_FAST 0
410/* TODO: implement simplified code for users which do not need ${var%...} ops
411 * So far ${var%...} ops are always enabled:
412 */
413#define ENABLE_HUSH_DOLLAR_OPS 1
414
Francis Laniel110b7692023-12-22 22:02:27 +0100415#if BUILD_AS_NOMMU
416# undef BB_MMU
417# undef USE_FOR_NOMMU
418# undef USE_FOR_MMU
419# define BB_MMU 0
420# define USE_FOR_NOMMU(...) __VA_ARGS__
421# define USE_FOR_MMU(...)
422#endif
423
Francis Laniel36836fc2023-12-22 22:02:28 +0100424#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +0100425#include "NUM_APPLETS.h"
426#if NUM_APPLETS == 1
427/* STANDALONE does not make sense, and won't compile */
Francis Laniel110b7692023-12-22 22:02:27 +0100428# undef ENABLE_FEATURE_SH_STANDALONE
429# undef IF_FEATURE_SH_STANDALONE
430# undef IF_NOT_FEATURE_SH_STANDALONE
431# define ENABLE_FEATURE_SH_STANDALONE 0
432# define IF_FEATURE_SH_STANDALONE(...)
433# define IF_NOT_FEATURE_SH_STANDALONE(...) __VA_ARGS__
434#endif
Francis Laniel36836fc2023-12-22 22:02:28 +0100435#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +0100436
437#if !ENABLE_HUSH_INTERACTIVE
438# undef ENABLE_FEATURE_EDITING
439# define ENABLE_FEATURE_EDITING 0
440# undef ENABLE_FEATURE_EDITING_FANCY_PROMPT
441# define ENABLE_FEATURE_EDITING_FANCY_PROMPT 0
442# undef ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
443# define ENABLE_FEATURE_EDITING_SAVE_ON_EXIT 0
444#endif
445
446/* Do we support ANY keywords? */
447#if ENABLE_HUSH_IF || ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE
448# define HAS_KEYWORDS 1
449# define IF_HAS_KEYWORDS(...) __VA_ARGS__
450# define IF_HAS_NO_KEYWORDS(...)
451#else
452# define HAS_KEYWORDS 0
453# define IF_HAS_KEYWORDS(...)
454# define IF_HAS_NO_KEYWORDS(...) __VA_ARGS__
455#endif
456
457/* If you comment out one of these below, it will be #defined later
458 * to perform debug printfs to stderr: */
459#define debug_printf(...) do {} while (0)
460/* Finer-grained debug switches */
461#define debug_printf_parse(...) do {} while (0)
462#define debug_printf_heredoc(...) do {} while (0)
463#define debug_print_tree(a, b) do {} while (0)
464#define debug_printf_exec(...) do {} while (0)
465#define debug_printf_env(...) do {} while (0)
466#define debug_printf_jobs(...) do {} while (0)
467#define debug_printf_expand(...) do {} while (0)
468#define debug_printf_varexp(...) do {} while (0)
469#define debug_printf_glob(...) do {} while (0)
470#define debug_printf_redir(...) do {} while (0)
471#define debug_printf_list(...) do {} while (0)
472#define debug_printf_subst(...) do {} while (0)
473#define debug_printf_prompt(...) do {} while (0)
474#define debug_printf_clean(...) do {} while (0)
475
476#define ERR_PTR ((void*)(long)1)
477
478#define JOB_STATUS_FORMAT "[%u] %-22s %.40s\n"
479
480#define _SPECIAL_VARS_STR "_*@$!?#-"
Francis Laniel36836fc2023-12-22 22:02:28 +0100481#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +0100482#define SPECIAL_VARS_STR ("_*@$!?#-" + 1)
483#define NUMERIC_SPECVARS_STR ("_*@$!?#-" + 3)
Francis Laniel36836fc2023-12-22 22:02:28 +0100484#else /* __U_BOOT__ */
485#define SPECIAL_VARS_STR "*@$!?#-"
486#define NUMERIC_SPECVARS_STR "$!?#-"
487#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +0100488#if BASH_PATTERN_SUBST
489/* Support / and // replace ops */
490/* Note that // is stored as \ in "encoded" string representation */
491# define VAR_ENCODED_SUBST_OPS "\\/%#:-=+?"
492# define VAR_SUBST_OPS ("\\/%#:-=+?" + 1)
493# define MINUS_PLUS_EQUAL_QUESTION ("\\/%#:-=+?" + 5)
494#else
495# define VAR_ENCODED_SUBST_OPS "%#:-=+?"
496# define VAR_SUBST_OPS "%#:-=+?"
497# define MINUS_PLUS_EQUAL_QUESTION ("%#:-=+?" + 3)
498#endif
499
500#define SPECIAL_VAR_SYMBOL_STR "\3"
501#define SPECIAL_VAR_SYMBOL 3
502/* The "variable" with name "\1" emits string "\3". Testcase: "echo ^C" */
503#define SPECIAL_VAR_QUOTED_SVS 1
504
505struct variable;
506
507static const char hush_version_str[] ALIGN1 = "HUSH_VERSION="BB_VER;
508
509/* This supports saving pointers malloced in vfork child,
510 * to be freed in the parent.
511 */
512#if !BB_MMU
513typedef struct nommu_save_t {
514 struct variable *old_vars;
515 char **argv;
516 char **argv_from_re_execing;
517} nommu_save_t;
518#endif
519
520enum {
521 RES_NONE = 0,
522#if ENABLE_HUSH_IF
523 RES_IF ,
524 RES_THEN ,
525 RES_ELIF ,
526 RES_ELSE ,
527 RES_FI ,
528#endif
529#if ENABLE_HUSH_LOOPS
530 RES_FOR ,
531 RES_WHILE ,
532 RES_UNTIL ,
533 RES_DO ,
534 RES_DONE ,
535#endif
536#if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE
537 RES_IN ,
538#endif
539#if ENABLE_HUSH_CASE
540 RES_CASE ,
541 /* three pseudo-keywords support contrived "case" syntax: */
542 RES_CASE_IN, /* "case ... IN", turns into RES_MATCH when IN is observed */
543 RES_MATCH , /* "word)" */
544 RES_CASE_BODY, /* "this command is inside CASE" */
545 RES_ESAC ,
546#endif
547 RES_XXXX ,
548 RES_SNTX
549};
550
551typedef struct o_string {
552 char *data;
553 int length; /* position where data is appended */
554 int maxlen;
555 int o_expflags;
556 /* At least some part of the string was inside '' or "",
557 * possibly empty one: word"", wo''rd etc. */
558 smallint has_quoted_part;
559 smallint has_empty_slot;
560 smallint ended_in_ifs;
561} o_string;
562enum {
563 EXP_FLAG_SINGLEWORD = 0x80, /* must be 0x80 */
564 EXP_FLAG_GLOB = 0x2,
565 /* Protect newly added chars against globbing
566 * by prepending \ to *, ?, [, \ */
567 EXP_FLAG_ESC_GLOB_CHARS = 0x1,
568};
569/* Used for initialization: o_string foo = NULL_O_STRING; */
570#define NULL_O_STRING { NULL }
571
572#ifndef debug_printf_parse
Francis Laniele7ca3a32023-12-22 22:02:42 +0100573static const char *const assignment_flag[] ALIGN_PTR = {
Francis Laniel110b7692023-12-22 22:02:27 +0100574 "MAYBE_ASSIGNMENT",
575 "DEFINITELY_ASSIGNMENT",
576 "NOT_ASSIGNMENT",
577 "WORD_IS_KEYWORD",
578};
579#endif
580
581/* We almost can use standard FILE api, but we need an ability to move
582 * its fd when redirects coincide with it. No api exists for that
583 * (RFE for it at https://sourceware.org/bugzilla/show_bug.cgi?id=21902).
584 * HFILE is our internal alternative. Only supports reading.
585 * Since we now can, we incorporate linked list of all opened HFILEs
586 * into the struct (used to be a separate mini-list).
587 */
588typedef struct HFILE {
589 char *cur;
590 char *end;
591 struct HFILE *next_hfile;
592 int fd;
593 char buf[1024];
594} HFILE;
595
596typedef struct in_str {
597 const char *p;
598 int peek_buf[2];
599 int last_char;
600 HFILE *file;
601} in_str;
602
Francis Laniel36836fc2023-12-22 22:02:28 +0100603#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +0100604/* The descrip member of this structure is only used to make
605 * debugging output pretty */
606static const struct {
607 int32_t mode;
608 signed char default_fd;
609 char descrip[3];
610} redir_table[] ALIGN4 = {
611 { O_RDONLY, 0, "<" },
612 { O_CREAT|O_TRUNC|O_WRONLY, 1, ">" },
613 { O_CREAT|O_APPEND|O_WRONLY, 1, ">>" },
614 { O_CREAT|O_RDWR, 1, "<>" },
615 { O_RDONLY, 0, "<<" },
616/* Should not be needed. Bogus default_fd helps in debugging */
617/* { O_RDONLY, 77, "<<" }, */
618};
619
620struct redir_struct {
621 struct redir_struct *next;
622 char *rd_filename; /* filename */
623 int rd_fd; /* fd to redirect */
624 /* fd to redirect to, or -3 if rd_fd is to be closed (n>&-) */
625 int rd_dup;
626 smallint rd_type; /* (enum redir_type) */
627 /* note: for heredocs, rd_filename contains heredoc delimiter,
628 * and subsequently heredoc itself; and rd_dup is a bitmask:
629 * bit 0: do we need to trim leading tabs?
630 * bit 1: is heredoc quoted (<<'delim' syntax) ?
631 */
632};
633typedef enum redir_type {
634 REDIRECT_INPUT = 0,
635 REDIRECT_OVERWRITE = 1,
636 REDIRECT_APPEND = 2,
637 REDIRECT_IO = 3,
638 REDIRECT_HEREDOC = 4,
639 REDIRECT_HEREDOC2 = 5, /* REDIRECT_HEREDOC after heredoc is loaded */
640
641 REDIRFD_CLOSE = -3,
642 REDIRFD_SYNTAX_ERR = -2,
643 REDIRFD_TO_FILE = -1,
644 /* otherwise, rd_fd is redirected to rd_dup */
645
646 HEREDOC_SKIPTABS = 1,
647 HEREDOC_QUOTED = 2,
648} redir_type;
649
Francis Laniel36836fc2023-12-22 22:02:28 +0100650#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +0100651
652struct command {
Francis Laniel36836fc2023-12-22 22:02:28 +0100653#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +0100654 pid_t pid; /* 0 if exited */
Francis Laniel36836fc2023-12-22 22:02:28 +0100655#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +0100656 unsigned assignment_cnt; /* how many argv[i] are assignments? */
657#if ENABLE_HUSH_LINENO_VAR
658 unsigned lineno;
659#endif
660 smallint cmd_type; /* CMD_xxx */
661#define CMD_NORMAL 0
662#define CMD_SUBSHELL 1
663#if BASH_TEST2
664/* used for "[[ EXPR ]]" */
665# define CMD_TEST2_SINGLEWORD_NOGLOB 2
666#endif
667#if BASH_TEST2 || ENABLE_HUSH_LOCAL || ENABLE_HUSH_EXPORT || ENABLE_HUSH_READONLY
668/* used to prevent word splitting and globbing in "export v=t*" */
669# define CMD_SINGLEWORD_NOGLOB 3
670#endif
671#if ENABLE_HUSH_FUNCTIONS
672# define CMD_FUNCDEF 4
673#endif
674
675 smalluint cmd_exitcode;
676 /* if non-NULL, this "command" is { list }, ( list ), or a compound statement */
677 struct pipe *group;
678#if !BB_MMU
679 char *group_as_string;
680#endif
681#if ENABLE_HUSH_FUNCTIONS
682 struct function *child_func;
683/* This field is used to prevent a bug here:
684 * while...do f1() {a;}; f1; f1() {b;}; f1; done
685 * When we execute "f1() {a;}" cmd, we create new function and clear
686 * cmd->group, cmd->group_as_string, cmd->argv[0].
687 * When we execute "f1() {b;}", we notice that f1 exists,
688 * and that its "parent cmd" struct is still "alive",
689 * we put those fields back into cmd->xxx
690 * (struct function has ->parent_cmd ptr to facilitate that).
691 * When we loop back, we can execute "f1() {a;}" again and set f1 correctly.
692 * Without this trick, loop would execute a;b;b;b;...
693 * instead of correct sequence a;b;a;b;...
694 * When command is freed, it severs the link
695 * (sets ->child_func->parent_cmd to NULL).
696 */
697#endif
Francis Laniel36836fc2023-12-22 22:02:28 +0100698#ifdef __U_BOOT__
699 int argc; /* number of program arguments */
700#endif
Francis Laniel110b7692023-12-22 22:02:27 +0100701 char **argv; /* command name and arguments */
702/* argv vector may contain variable references (^Cvar^C, ^C0^C etc)
703 * and on execution these are substituted with their values.
704 * Substitution can make _several_ words out of one argv[n]!
705 * Example: argv[0]=='.^C*^C.' here: echo .$*.
706 * References of the form ^C`cmd arg^C are `cmd arg` substitutions.
707 */
Francis Laniel36836fc2023-12-22 22:02:28 +0100708#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +0100709 struct redir_struct *redirects; /* I/O redirections */
Francis Laniel36836fc2023-12-22 22:02:28 +0100710#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +0100711};
712/* Is there anything in this command at all? */
Francis Laniel36836fc2023-12-22 22:02:28 +0100713#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +0100714#define IS_NULL_CMD(cmd) \
715 (!(cmd)->group && !(cmd)->argv && !(cmd)->redirects)
716
Francis Laniel36836fc2023-12-22 22:02:28 +0100717#else /* __U_BOOT__ */
718#define IS_NULL_CMD(cmd) \
719 (!(cmd)->group && !(cmd)->argv)
720#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +0100721struct pipe {
722 struct pipe *next;
723 int num_cmds; /* total number of commands in pipe */
Francis Laniel36836fc2023-12-22 22:02:28 +0100724#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +0100725 int alive_cmds; /* number of commands running (not exited) */
726 int stopped_cmds; /* number of commands alive, but stopped */
727#if ENABLE_HUSH_JOB
728 unsigned jobid; /* job number */
729 pid_t pgrp; /* process group ID for the job */
730 char *cmdtext; /* name of job */
731#endif
Francis Laniel36836fc2023-12-22 22:02:28 +0100732#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +0100733 struct command *cmds; /* array of commands in pipe */
734 smallint followup; /* PIPE_BG, PIPE_SEQ, PIPE_OR, PIPE_AND */
735 IF_HAS_KEYWORDS(smallint pi_inverted;) /* "! cmd | cmd" */
736 IF_HAS_KEYWORDS(smallint res_word;) /* needed for if, for, while, until... */
737};
738typedef enum pipe_style {
739 PIPE_SEQ = 0,
740 PIPE_AND = 1,
741 PIPE_OR = 2,
742 PIPE_BG = 3,
743} pipe_style;
744/* Is there anything in this pipe at all? */
745#define IS_NULL_PIPE(pi) \
746 ((pi)->num_cmds == 0 IF_HAS_KEYWORDS( && (pi)->res_word == RES_NONE))
747
748/* This holds pointers to the various results of parsing */
749struct parse_context {
750 /* linked list of pipes */
751 struct pipe *list_head;
752 /* last pipe (being constructed right now) */
753 struct pipe *pipe;
754 /* last command in pipe (being constructed right now) */
755 struct command *command;
Francis Laniel36836fc2023-12-22 22:02:28 +0100756#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +0100757 /* last redirect in command->redirects list */
758 struct redir_struct *pending_redirect;
Francis Laniel36836fc2023-12-22 22:02:28 +0100759#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +0100760 o_string word;
761#if !BB_MMU
762 o_string as_string;
763#endif
764 smallint is_assignment; /* 0:maybe, 1:yes, 2:no, 3:keyword */
765#if HAS_KEYWORDS
766 smallint ctx_res_w;
767 smallint ctx_inverted; /* "! cmd | cmd" */
768#if ENABLE_HUSH_CASE
769 smallint ctx_dsemicolon; /* ";;" seen */
770#endif
771 /* bitmask of FLAG_xxx, for figuring out valid reserved words */
772 int old_flag;
773 /* group we are enclosed in:
774 * example: "if pipe1; pipe2; then pipe3; fi"
775 * when we see "if" or "then", we malloc and copy current context,
776 * and make ->stack point to it. then we parse pipeN.
777 * when closing "then" / fi" / whatever is found,
778 * we move list_head into ->stack->command->group,
779 * copy ->stack into current context, and delete ->stack.
780 * (parsing of { list } and ( list ) doesn't use this method)
781 */
782 struct parse_context *stack;
783#endif
784};
785enum {
786 MAYBE_ASSIGNMENT = 0,
787 DEFINITELY_ASSIGNMENT = 1,
788 NOT_ASSIGNMENT = 2,
789 /* Not an assignment, but next word may be: "if v=xyz cmd;" */
790 WORD_IS_KEYWORD = 3,
791};
792
Francis Laniel36836fc2023-12-22 22:02:28 +0100793#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +0100794/* On program start, environ points to initial environment.
795 * putenv adds new pointers into it, unsetenv removes them.
796 * Neither of these (de)allocates the strings.
797 * setenv allocates new strings in malloc space and does putenv,
798 * and thus setenv is unusable (leaky) for shell's purposes */
799#define setenv(...) setenv_is_leaky_dont_use()
Francis Laniel36836fc2023-12-22 22:02:28 +0100800#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +0100801struct variable {
802 struct variable *next;
803 char *varstr; /* points to "name=" portion */
804 int max_len; /* if > 0, name is part of initial env; else name is malloced */
Francis Laniel36836fc2023-12-22 22:02:28 +0100805#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +0100806 uint16_t var_nest_level;
807 smallint flg_export; /* putenv should be done on this var */
808 smallint flg_read_only;
Francis Laniel36836fc2023-12-22 22:02:28 +0100809#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +0100810};
811
812enum {
813 BC_BREAK = 1,
814 BC_CONTINUE = 2,
815};
816
817#if ENABLE_HUSH_FUNCTIONS
818struct function {
819 struct function *next;
820 char *name;
821 struct command *parent_cmd;
822 struct pipe *body;
823# if !BB_MMU
824 char *body_as_string;
825# endif
826};
827#endif
828
Francis Laniel110b7692023-12-22 22:02:27 +0100829/* set -/+o OPT support. (TODO: make it optional)
830 * bash supports the following opts:
831 * allexport off
832 * braceexpand on
833 * emacs on
834 * errexit off
835 * errtrace off
836 * functrace off
837 * hashall on
838 * histexpand off
839 * history on
840 * ignoreeof off
841 * interactive-comments on
842 * keyword off
843 * monitor on
844 * noclobber off
845 * noexec off
846 * noglob off
847 * nolog off
848 * notify off
849 * nounset off
850 * onecmd off
851 * physical off
852 * pipefail off
853 * posix off
854 * privileged off
855 * verbose off
856 * vi off
857 * xtrace off
858 */
859static const char o_opt_strings[] ALIGN1 =
860 "pipefail\0"
861 "noexec\0"
862 "errexit\0"
863#if ENABLE_HUSH_MODE_X
864 "xtrace\0"
865#endif
866 ;
867enum {
868 OPT_O_PIPEFAIL,
869 OPT_O_NOEXEC,
870 OPT_O_ERREXIT,
871#if ENABLE_HUSH_MODE_X
872 OPT_O_XTRACE,
873#endif
874 NUM_OPT_O
875};
876
877/* "Globals" within this file */
878/* Sorted roughly by size (smaller offsets == smaller code) */
879struct globals {
Francis Laniel36836fc2023-12-22 22:02:28 +0100880#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +0100881 /* interactive_fd != 0 means we are an interactive shell.
882 * If we are, then saved_tty_pgrp can also be != 0, meaning
883 * that controlling tty is available. With saved_tty_pgrp == 0,
884 * job control still works, but terminal signals
885 * (^C, ^Z, ^Y, ^\) won't work at all, and background
886 * process groups can only be created with "cmd &".
887 * With saved_tty_pgrp != 0, hush will use tcsetpgrp()
888 * to give tty to the foreground process group,
889 * and will take it back when the group is stopped (^Z)
890 * or killed (^C).
891 */
892#if ENABLE_HUSH_INTERACTIVE
893 /* 'interactive_fd' is a fd# open to ctty, if we have one
894 * _AND_ if we decided to act interactively */
895 int interactive_fd;
896 IF_NOT_FEATURE_EDITING_FANCY_PROMPT(char *PS1;)
897# define G_interactive_fd (G.interactive_fd)
898#else
899# define G_interactive_fd 0
900#endif
Francis Laniel36836fc2023-12-22 22:02:28 +0100901#else /* __U_BOOT__ */
902# define G_interactive_fd 0
903#endif /* __U_BOOT__ */
904#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +0100905#if ENABLE_FEATURE_EDITING
906 line_input_t *line_input_state;
907#endif
908 pid_t root_pid;
909 pid_t root_ppid;
910 pid_t last_bg_pid;
911#if ENABLE_HUSH_RANDOM_SUPPORT
912 random_t random_gen;
913#endif
914#if ENABLE_HUSH_JOB
915 int run_list_level;
916 unsigned last_jobid;
917 pid_t saved_tty_pgrp;
918 struct pipe *job_list;
919# define G_saved_tty_pgrp (G.saved_tty_pgrp)
920#else
921# define G_saved_tty_pgrp 0
922#endif
Francis Laniel36836fc2023-12-22 22:02:28 +0100923#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +0100924 /* How deeply are we in context where "set -e" is ignored */
925 int errexit_depth;
Francis Laniel36836fc2023-12-22 22:02:28 +0100926#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +0100927 /* "set -e" rules (do we follow them correctly?):
928 * Exit if pipe, list, or compound command exits with a non-zero status.
929 * Shell does not exit if failed command is part of condition in
930 * if/while, part of && or || list except the last command, any command
931 * in a pipe but the last, or if the command's return value is being
932 * inverted with !. If a compound command other than a subshell returns a
933 * non-zero status because a command failed while -e was being ignored, the
934 * shell does not exit. A trap on ERR, if set, is executed before the shell
935 * exits [ERR is a bashism].
936 *
937 * If a compound command or function executes in a context where -e is
938 * ignored, none of the commands executed within are affected by the -e
939 * setting. If a compound command or function sets -e while executing in a
940 * context where -e is ignored, that setting does not have any effect until
941 * the compound command or the command containing the function call completes.
942 */
943
944 char o_opt[NUM_OPT_O];
945#if ENABLE_HUSH_MODE_X
946# define G_x_mode (G.o_opt[OPT_O_XTRACE])
947#else
948# define G_x_mode 0
949#endif
950 char opt_s;
951 char opt_c;
Francis Laniel36836fc2023-12-22 22:02:28 +0100952#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +0100953#if ENABLE_HUSH_INTERACTIVE
954 smallint promptmode; /* 0: PS1, 1: PS2 */
955#endif
Francis Laniele7ca3a32023-12-22 22:02:42 +0100956 /* set by signal handler if SIGINT is received _and_ its trap is not set */
Francis Laniel110b7692023-12-22 22:02:27 +0100957 smallint flag_SIGINT;
Francis Laniel36836fc2023-12-22 22:02:28 +0100958#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +0100959#if ENABLE_HUSH_LOOPS
960 smallint flag_break_continue;
961#endif
Francis Laniel36836fc2023-12-22 22:02:28 +0100962#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +0100963#if ENABLE_HUSH_FUNCTIONS
964 /* 0: outside of a function (or sourced file)
965 * -1: inside of a function, ok to use return builtin
966 * 1: return is invoked, skip all till end of func
967 */
968 smallint flag_return_in_progress;
969# define G_flag_return_in_progress (G.flag_return_in_progress)
970#else
971# define G_flag_return_in_progress 0
972#endif
973 smallint exiting; /* used to prevent EXIT trap recursion */
974 /* These support $? */
975 smalluint last_exitcode;
976 smalluint expand_exitcode;
977 smalluint last_bg_pid_exitcode;
Francis Laniel36836fc2023-12-22 22:02:28 +0100978#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +0100979#if ENABLE_HUSH_SET
980 /* are global_argv and global_argv[1..n] malloced? (note: not [0]) */
981 smalluint global_args_malloced;
982# define G_global_args_malloced (G.global_args_malloced)
983#else
984# define G_global_args_malloced 0
985#endif
986#if ENABLE_HUSH_BASH_COMPAT
987 int dead_job_exitcode; /* for "wait -n" */
988#endif
Francis Laniel36836fc2023-12-22 22:02:28 +0100989#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +0100990 /* how many non-NULL argv's we have. NB: $# + 1 */
991 int global_argc;
992 char **global_argv;
993#if !BB_MMU
994 char *argv0_for_re_execing;
995#endif
996#if ENABLE_HUSH_LOOPS
Francis Laniel36836fc2023-12-22 22:02:28 +0100997#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +0100998 unsigned depth_break_continue;
Francis Laniel36836fc2023-12-22 22:02:28 +0100999#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01001000 unsigned depth_of_loop;
1001#endif
Francis Laniel36836fc2023-12-22 22:02:28 +01001002#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01001003#if ENABLE_HUSH_GETOPTS
1004 unsigned getopt_count;
1005#endif
Francis Laniel36836fc2023-12-22 22:02:28 +01001006#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01001007 const char *ifs;
Francis Laniel36836fc2023-12-22 22:02:28 +01001008#ifdef __U_BOOT__
1009 int flag_repeat;
1010 int do_repeat;
Francis Laniel26cafe12023-12-22 22:02:34 +01001011 int run_command_flags;
Francis Laniel36836fc2023-12-22 22:02:28 +01001012#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01001013 char *ifs_whitespace; /* = G.ifs or malloced */
Francis Laniel36836fc2023-12-22 22:02:28 +01001014#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01001015 const char *cwd;
Francis Laniel36836fc2023-12-22 22:02:28 +01001016#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01001017 struct variable *top_var;
1018 char **expanded_assignments;
1019 struct variable **shadowed_vars_pp;
1020 unsigned var_nest_level;
Francis Laniel36836fc2023-12-22 22:02:28 +01001021#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01001022#if ENABLE_HUSH_FUNCTIONS
1023# if ENABLE_HUSH_LOCAL
1024 unsigned func_nest_level; /* solely to prevent "local v" in non-functions */
1025# endif
1026 struct function *top_func;
1027#endif
1028 /* Signal and trap handling */
1029#if ENABLE_HUSH_FAST
1030 unsigned count_SIGCHLD;
1031 unsigned handled_SIGCHLD;
1032 smallint we_have_children;
1033#endif
1034#if ENABLE_HUSH_LINENO_VAR
1035 unsigned parse_lineno;
1036 unsigned execute_lineno;
1037#endif
1038 HFILE *HFILE_list;
1039 HFILE *HFILE_stdin;
1040 /* Which signals have non-DFL handler (even with no traps set)?
1041 * Set at the start to:
1042 * (SIGQUIT + maybe SPECIAL_INTERACTIVE_SIGS + maybe SPECIAL_JOBSTOP_SIGS)
1043 * SPECIAL_INTERACTIVE_SIGS are cleared after fork.
1044 * The rest is cleared right before execv syscalls.
1045 * Other than these two times, never modified.
1046 */
1047 unsigned special_sig_mask;
1048#if ENABLE_HUSH_JOB
1049 unsigned fatal_sig_mask;
1050# define G_fatal_sig_mask (G.fatal_sig_mask)
1051#else
1052# define G_fatal_sig_mask 0
1053#endif
1054#if ENABLE_HUSH_TRAP
1055 int pre_trap_exitcode;
1056# if ENABLE_HUSH_FUNCTIONS
1057 int return_exitcode;
1058# endif
1059 char **traps; /* char *traps[NSIG] */
1060# define G_traps G.traps
1061#else
1062# define G_traps ((char**)NULL)
1063#endif
1064 sigset_t pending_set;
1065#if ENABLE_HUSH_MEMLEAK
1066 unsigned long memleak_value;
1067#endif
1068#if ENABLE_HUSH_MODE_X
1069 unsigned x_mode_depth;
1070 /* "set -x" output should not be redirectable with subsequent 2>FILE.
1071 * We dup fd#2 to x_mode_fd when "set -x" is executed, and use it
1072 * for all subsequent output.
1073 */
1074 int x_mode_fd;
1075 o_string x_mode_buf;
1076#endif
Francis Laniel36836fc2023-12-22 22:02:28 +01001077#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01001078#if HUSH_DEBUG >= 2
1079 int debug_indent;
1080#endif
Francis Laniel36836fc2023-12-22 22:02:28 +01001081#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01001082 struct sigaction sa;
1083 char optstring_buf[sizeof("eixcs")];
1084#if BASH_EPOCH_VARS
1085 char epoch_buf[sizeof("%llu.nnnnnn") + sizeof(long long)*3];
1086#endif
1087#if ENABLE_FEATURE_EDITING
1088 char user_input_buf[CONFIG_FEATURE_EDITING_MAX_LEN];
1089#endif
Francis Laniel36836fc2023-12-22 22:02:28 +01001090#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01001091};
Francis Laniel36836fc2023-12-22 22:02:28 +01001092#ifdef __U_BOOT__
1093struct globals *ptr_to_globals;
1094#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01001095#define G (*ptr_to_globals)
1096/* Not #defining name to G.name - this quickly gets unwieldy
1097 * (too many defines). Also, I actually prefer to see when a variable
1098 * is global, thus "G." prefix is a useful hint */
Francis Laniel36836fc2023-12-22 22:02:28 +01001099#ifdef __U_BOOT__
1100#define SET_PTR_TO_GLOBALS(x) do { \
1101 (*(struct globals**)&ptr_to_globals) = (void*)(x); \
1102 barrier(); \
1103} while (0)
1104#define INIT_G() do { \
1105 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
1106 G.promptmode = 1; \
1107} while (0)
1108#else /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01001109#define INIT_G() do { \
1110 SET_PTR_TO_GLOBALS(xzalloc(sizeof(G))); \
1111 /* memset(&G.sa, 0, sizeof(G.sa)); */ \
1112 sigfillset(&G.sa.sa_mask); \
1113 G.sa.sa_flags = SA_RESTART; \
1114} while (0)
Francis Laniel36836fc2023-12-22 22:02:28 +01001115#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01001116
Francis Laniel36836fc2023-12-22 22:02:28 +01001117#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01001118/* Function prototypes for builtins */
1119static int builtin_cd(char **argv) FAST_FUNC;
1120#if ENABLE_HUSH_ECHO
1121static int builtin_echo(char **argv) FAST_FUNC;
1122#endif
1123static int builtin_eval(char **argv) FAST_FUNC;
1124static int builtin_exec(char **argv) FAST_FUNC;
1125static int builtin_exit(char **argv) FAST_FUNC;
1126#if ENABLE_HUSH_EXPORT
1127static int builtin_export(char **argv) FAST_FUNC;
1128#endif
1129#if ENABLE_HUSH_READONLY
1130static int builtin_readonly(char **argv) FAST_FUNC;
1131#endif
Francis Laniele7ca3a32023-12-22 22:02:42 +01001132static int builtin_false(char **argv) FAST_FUNC;
Francis Laniel110b7692023-12-22 22:02:27 +01001133#if ENABLE_HUSH_JOB
1134static int builtin_fg_bg(char **argv) FAST_FUNC;
1135static int builtin_jobs(char **argv) FAST_FUNC;
1136#endif
1137#if ENABLE_HUSH_GETOPTS
1138static int builtin_getopts(char **argv) FAST_FUNC;
1139#endif
1140#if ENABLE_HUSH_HELP
1141static int builtin_help(char **argv) FAST_FUNC;
1142#endif
1143#if MAX_HISTORY && ENABLE_FEATURE_EDITING
1144static int builtin_history(char **argv) FAST_FUNC;
1145#endif
1146#if ENABLE_HUSH_LOCAL
1147static int builtin_local(char **argv) FAST_FUNC;
1148#endif
1149#if ENABLE_HUSH_MEMLEAK
1150static int builtin_memleak(char **argv) FAST_FUNC;
1151#endif
1152#if ENABLE_HUSH_PRINTF
1153static int builtin_printf(char **argv) FAST_FUNC;
1154#endif
1155static int builtin_pwd(char **argv) FAST_FUNC;
1156#if ENABLE_HUSH_READ
1157static int builtin_read(char **argv) FAST_FUNC;
1158#endif
1159#if ENABLE_HUSH_SET
1160static int builtin_set(char **argv) FAST_FUNC;
1161#endif
1162static int builtin_shift(char **argv) FAST_FUNC;
1163static int builtin_source(char **argv) FAST_FUNC;
1164#if ENABLE_HUSH_TEST || BASH_TEST2
1165static int builtin_test(char **argv) FAST_FUNC;
1166#endif
1167#if ENABLE_HUSH_TRAP
1168static int builtin_trap(char **argv) FAST_FUNC;
1169#endif
1170#if ENABLE_HUSH_TYPE
1171static int builtin_type(char **argv) FAST_FUNC;
1172#endif
1173#if ENABLE_HUSH_TIMES
1174static int builtin_times(char **argv) FAST_FUNC;
1175#endif
1176static int builtin_true(char **argv) FAST_FUNC;
1177#if ENABLE_HUSH_UMASK
1178static int builtin_umask(char **argv) FAST_FUNC;
1179#endif
1180#if ENABLE_HUSH_UNSET
1181static int builtin_unset(char **argv) FAST_FUNC;
1182#endif
1183#if ENABLE_HUSH_KILL
1184static int builtin_kill(char **argv) FAST_FUNC;
1185#endif
1186#if ENABLE_HUSH_WAIT
1187static int builtin_wait(char **argv) FAST_FUNC;
1188#endif
1189#if ENABLE_HUSH_LOOPS
1190static int builtin_break(char **argv) FAST_FUNC;
1191static int builtin_continue(char **argv) FAST_FUNC;
1192#endif
1193#if ENABLE_HUSH_FUNCTIONS
1194static int builtin_return(char **argv) FAST_FUNC;
1195#endif
1196
1197/* Table of built-in functions. They can be forked or not, depending on
1198 * context: within pipes, they fork. As simple commands, they do not.
1199 * When used in non-forking context, they can change global variables
1200 * in the parent shell process. If forked, of course they cannot.
1201 * For example, 'unset foo | whatever' will parse and run, but foo will
1202 * still be set at the end. */
1203struct built_in_command {
1204 const char *b_cmd;
1205 int (*b_function)(char **argv) FAST_FUNC;
1206#if ENABLE_HUSH_HELP
1207 const char *b_descr;
1208# define BLTIN(cmd, func, help) { cmd, func, help }
1209#else
1210# define BLTIN(cmd, func, help) { cmd, func }
1211#endif
1212};
1213
1214static const struct built_in_command bltins1[] ALIGN_PTR = {
1215 BLTIN("." , builtin_source , "Run commands in file"),
1216 BLTIN(":" , builtin_true , NULL),
1217#if ENABLE_HUSH_JOB
1218 BLTIN("bg" , builtin_fg_bg , "Resume job in background"),
1219#endif
1220#if ENABLE_HUSH_LOOPS
1221 BLTIN("break" , builtin_break , "Exit loop"),
1222#endif
1223 BLTIN("cd" , builtin_cd , "Change directory"),
1224#if ENABLE_HUSH_LOOPS
1225 BLTIN("continue" , builtin_continue, "Start new loop iteration"),
1226#endif
1227 BLTIN("eval" , builtin_eval , "Construct and run shell command"),
1228 BLTIN("exec" , builtin_exec , "Execute command, don't return to shell"),
1229 BLTIN("exit" , builtin_exit , NULL),
1230#if ENABLE_HUSH_EXPORT
1231 BLTIN("export" , builtin_export , "Set environment variables"),
1232#endif
Francis Laniele7ca3a32023-12-22 22:02:42 +01001233 BLTIN("false" , builtin_false , NULL),
Francis Laniel110b7692023-12-22 22:02:27 +01001234#if ENABLE_HUSH_JOB
1235 BLTIN("fg" , builtin_fg_bg , "Bring job to foreground"),
1236#endif
1237#if ENABLE_HUSH_GETOPTS
1238 BLTIN("getopts" , builtin_getopts , NULL),
1239#endif
1240#if ENABLE_HUSH_HELP
1241 BLTIN("help" , builtin_help , NULL),
1242#endif
1243#if MAX_HISTORY && ENABLE_FEATURE_EDITING
1244 BLTIN("history" , builtin_history , "Show history"),
1245#endif
1246#if ENABLE_HUSH_JOB
1247 BLTIN("jobs" , builtin_jobs , "List jobs"),
1248#endif
1249#if ENABLE_HUSH_KILL
1250 BLTIN("kill" , builtin_kill , "Send signals to processes"),
1251#endif
1252#if ENABLE_HUSH_LOCAL
1253 BLTIN("local" , builtin_local , "Set local variables"),
1254#endif
1255#if ENABLE_HUSH_MEMLEAK
1256 BLTIN("memleak" , builtin_memleak , NULL),
1257#endif
1258#if ENABLE_HUSH_READ
1259 BLTIN("read" , builtin_read , "Input into variable"),
1260#endif
1261#if ENABLE_HUSH_READONLY
1262 BLTIN("readonly" , builtin_readonly, "Make variables read-only"),
1263#endif
1264#if ENABLE_HUSH_FUNCTIONS
1265 BLTIN("return" , builtin_return , "Return from function"),
1266#endif
1267#if ENABLE_HUSH_SET
1268 BLTIN("set" , builtin_set , "Set positional parameters"),
1269#endif
1270 BLTIN("shift" , builtin_shift , "Shift positional parameters"),
1271#if BASH_SOURCE
1272 BLTIN("source" , builtin_source , NULL),
1273#endif
1274#if ENABLE_HUSH_TIMES
1275 BLTIN("times" , builtin_times , NULL),
1276#endif
1277#if ENABLE_HUSH_TRAP
1278 BLTIN("trap" , builtin_trap , "Trap signals"),
1279#endif
1280 BLTIN("true" , builtin_true , NULL),
1281#if ENABLE_HUSH_TYPE
1282 BLTIN("type" , builtin_type , "Show command type"),
1283#endif
1284#if ENABLE_HUSH_ULIMIT
1285 BLTIN("ulimit" , shell_builtin_ulimit, "Control resource limits"),
1286#endif
1287#if ENABLE_HUSH_UMASK
1288 BLTIN("umask" , builtin_umask , "Set file creation mask"),
1289#endif
1290#if ENABLE_HUSH_UNSET
1291 BLTIN("unset" , builtin_unset , "Unset variables"),
1292#endif
1293#if ENABLE_HUSH_WAIT
1294 BLTIN("wait" , builtin_wait , "Wait for process to finish"),
1295#endif
1296};
1297/* These builtins won't be used if we are on NOMMU and need to re-exec
1298 * (it's cheaper to run an external program in this case):
1299 */
1300static const struct built_in_command bltins2[] ALIGN_PTR = {
1301#if ENABLE_HUSH_TEST
1302 BLTIN("[" , builtin_test , NULL),
1303#endif
1304#if BASH_TEST2
1305 BLTIN("[[" , builtin_test , NULL),
1306#endif
1307#if ENABLE_HUSH_ECHO
1308 BLTIN("echo" , builtin_echo , NULL),
1309#endif
1310#if ENABLE_HUSH_PRINTF
1311 BLTIN("printf" , builtin_printf , NULL),
1312#endif
1313 BLTIN("pwd" , builtin_pwd , NULL),
1314#if ENABLE_HUSH_TEST
1315 BLTIN("test" , builtin_test , NULL),
1316#endif
1317};
1318
Francis Laniel36836fc2023-12-22 22:02:28 +01001319#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01001320
1321/* Debug printouts.
1322 */
1323#if HUSH_DEBUG >= 2
1324/* prevent disasters with G.debug_indent < 0 */
1325# define indent() fdprintf(2, "%*s", (G.debug_indent * 2) & 0xff, "")
1326# define debug_enter() (G.debug_indent++)
1327# define debug_leave() (G.debug_indent--)
1328#else
1329# define indent() ((void)0)
1330# define debug_enter() ((void)0)
1331# define debug_leave() ((void)0)
1332#endif
1333
1334#ifndef debug_printf
1335# define debug_printf(...) (indent(), fdprintf(2, __VA_ARGS__))
1336#endif
1337
1338#ifndef debug_printf_parse
1339# define debug_printf_parse(...) (indent(), fdprintf(2, __VA_ARGS__))
1340#endif
1341
1342#ifndef debug_printf_heredoc
1343# define debug_printf_heredoc(...) (indent(), fdprintf(2, __VA_ARGS__))
1344#endif
1345
1346#ifndef debug_printf_exec
1347#define debug_printf_exec(...) (indent(), fdprintf(2, __VA_ARGS__))
1348#endif
1349
1350#ifndef debug_printf_env
1351# define debug_printf_env(...) (indent(), fdprintf(2, __VA_ARGS__))
1352#endif
1353
1354#ifndef debug_printf_jobs
1355# define debug_printf_jobs(...) (indent(), fdprintf(2, __VA_ARGS__))
1356# define DEBUG_JOBS 1
1357#else
1358# define DEBUG_JOBS 0
1359#endif
1360
1361#ifndef debug_printf_expand
1362# define debug_printf_expand(...) (indent(), fdprintf(2, __VA_ARGS__))
1363# define DEBUG_EXPAND 1
1364#else
1365# define DEBUG_EXPAND 0
1366#endif
1367
1368#ifndef debug_printf_varexp
1369# define debug_printf_varexp(...) (indent(), fdprintf(2, __VA_ARGS__))
1370#endif
1371
1372#ifndef debug_printf_glob
1373# define debug_printf_glob(...) (indent(), fdprintf(2, __VA_ARGS__))
1374# define DEBUG_GLOB 1
1375#else
1376# define DEBUG_GLOB 0
1377#endif
1378
1379#ifndef debug_printf_redir
1380# define debug_printf_redir(...) (indent(), fdprintf(2, __VA_ARGS__))
1381#endif
1382
1383#ifndef debug_printf_list
1384# define debug_printf_list(...) (indent(), fdprintf(2, __VA_ARGS__))
1385#endif
1386
1387#ifndef debug_printf_subst
1388# define debug_printf_subst(...) (indent(), fdprintf(2, __VA_ARGS__))
1389#endif
1390
1391#ifndef debug_printf_prompt
1392# define debug_printf_prompt(...) (indent(), fdprintf(2, __VA_ARGS__))
1393#endif
1394
1395#ifndef debug_printf_clean
1396# define debug_printf_clean(...) (indent(), fdprintf(2, __VA_ARGS__))
1397# define DEBUG_CLEAN 1
1398#else
1399# define DEBUG_CLEAN 0
1400#endif
1401
1402#if DEBUG_EXPAND
1403static void debug_print_strings(const char *prefix, char **vv)
1404{
1405 indent();
1406 fdprintf(2, "%s:\n", prefix);
1407 while (*vv)
1408 fdprintf(2, " '%s'\n", *vv++);
1409}
1410#else
1411# define debug_print_strings(prefix, vv) ((void)0)
1412#endif
1413
Francis Laniel110b7692023-12-22 22:02:27 +01001414/* Leak hunting. Use hush_leaktool.sh for post-processing.
1415 */
1416#if LEAK_HUNTING
1417static void *xxmalloc(int lineno, size_t size)
1418{
1419 void *ptr = xmalloc((size + 0xff) & ~0xff);
1420 fdprintf(2, "line %d: malloc %p\n", lineno, ptr);
1421 return ptr;
1422}
1423static void *xxrealloc(int lineno, void *ptr, size_t size)
1424{
1425 ptr = xrealloc(ptr, (size + 0xff) & ~0xff);
1426 fdprintf(2, "line %d: realloc %p\n", lineno, ptr);
1427 return ptr;
1428}
1429static char *xxstrdup(int lineno, const char *str)
1430{
1431 char *ptr = xstrdup(str);
1432 fdprintf(2, "line %d: strdup %p\n", lineno, ptr);
1433 return ptr;
1434}
1435static void xxfree(void *ptr)
1436{
1437 fdprintf(2, "free %p\n", ptr);
1438 free(ptr);
1439}
1440# define xmalloc(s) xxmalloc(__LINE__, s)
1441# define xrealloc(p, s) xxrealloc(__LINE__, p, s)
1442# define xstrdup(s) xxstrdup(__LINE__, s)
1443# define free(p) xxfree(p)
1444#endif
1445
Francis Laniel110b7692023-12-22 22:02:27 +01001446/* Syntax and runtime errors. They always abort scripts.
1447 * In interactive use they usually discard unparsed and/or unexecuted commands
1448 * and return to the prompt.
1449 * HUSH_DEBUG >= 2 prints line number in this file where it was detected.
1450 */
1451#if HUSH_DEBUG < 2
1452# define msg_and_die_if_script(lineno, ...) msg_and_die_if_script(__VA_ARGS__)
1453# define syntax_error(lineno, msg) syntax_error(msg)
1454# define syntax_error_at(lineno, msg) syntax_error_at(msg)
1455# define syntax_error_unterm_ch(lineno, ch) syntax_error_unterm_ch(ch)
1456# define syntax_error_unterm_str(lineno, s) syntax_error_unterm_str(s)
1457# define syntax_error_unexpected_ch(lineno, ch) syntax_error_unexpected_ch(ch)
1458#endif
1459
1460static void die_if_script(void)
1461{
1462 if (!G_interactive_fd) {
1463 if (G.last_exitcode) /* sometines it's 2, not 1 (bash compat) */
1464 xfunc_error_retval = G.last_exitcode;
1465 xfunc_die();
1466 }
1467}
1468
Francis Laniel36836fc2023-12-22 22:02:28 +01001469#ifdef __U_BOOT__
1470static void __maybe_unused msg_and_die_if_script(unsigned lineno, const char *fmt, ...)
1471#else /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01001472static void msg_and_die_if_script(unsigned lineno, const char *fmt, ...)
Francis Laniel36836fc2023-12-22 22:02:28 +01001473#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01001474{
1475 va_list p;
1476
1477#if HUSH_DEBUG >= 2
1478 bb_error_msg("hush.c:%u", lineno);
1479#endif
1480 va_start(p, fmt);
1481 bb_verror_msg(fmt, p, NULL);
1482 va_end(p);
1483 die_if_script();
1484}
1485
1486static void syntax_error(unsigned lineno UNUSED_PARAM, const char *msg)
1487{
1488 if (msg)
1489 bb_error_msg("syntax error: %s", msg);
1490 else
1491 bb_simple_error_msg("syntax error");
1492 die_if_script();
1493}
1494
1495static void syntax_error_at(unsigned lineno UNUSED_PARAM, const char *msg)
1496{
1497 bb_error_msg("syntax error at '%s'", msg);
1498 die_if_script();
1499}
1500
1501static void syntax_error_unterm_str(unsigned lineno UNUSED_PARAM, const char *s)
1502{
1503 bb_error_msg("syntax error: unterminated %s", s);
1504//? source4.tests fails: in bash, echo ${^} in script does not terminate the script
Francis Laniele7ca3a32023-12-22 22:02:42 +01001505// (but bash --posix, or if bash is run as "sh", does terminate in script, so maybe uncomment this?)
Francis Laniel110b7692023-12-22 22:02:27 +01001506// die_if_script();
1507}
1508
1509static void syntax_error_unterm_ch(unsigned lineno, char ch)
1510{
1511 char msg[2] = { ch, '\0' };
1512 syntax_error_unterm_str(lineno, msg);
1513}
1514
1515static void syntax_error_unexpected_ch(unsigned lineno UNUSED_PARAM, int ch)
1516{
1517 char msg[2];
1518 msg[0] = ch;
1519 msg[1] = '\0';
1520#if HUSH_DEBUG >= 2
1521 bb_error_msg("hush.c:%u", lineno);
1522#endif
1523 bb_error_msg("syntax error: unexpected %s", ch == EOF ? "EOF" : msg);
1524 die_if_script();
1525}
1526
1527#if HUSH_DEBUG < 2
1528# undef msg_and_die_if_script
1529# undef syntax_error
1530# undef syntax_error_at
1531# undef syntax_error_unterm_ch
1532# undef syntax_error_unterm_str
1533# undef syntax_error_unexpected_ch
1534#else
1535# define msg_and_die_if_script(...) msg_and_die_if_script(__LINE__, __VA_ARGS__)
1536# define syntax_error(msg) syntax_error(__LINE__, msg)
1537# define syntax_error_at(msg) syntax_error_at(__LINE__, msg)
1538# define syntax_error_unterm_ch(ch) syntax_error_unterm_ch(__LINE__, ch)
1539# define syntax_error_unterm_str(s) syntax_error_unterm_str(__LINE__, s)
1540# define syntax_error_unexpected_ch(ch) syntax_error_unexpected_ch(__LINE__, ch)
1541#endif
1542
Francis Laniel110b7692023-12-22 22:02:27 +01001543/* Utility functions
1544 */
1545/* Replace each \x with x in place, return ptr past NUL. */
1546static char *unbackslash(char *src)
1547{
Francis Laniel36836fc2023-12-22 22:02:28 +01001548#ifdef __U_BOOT__
1549 char *dst = src = (char *)strchrnul(src, '\\');
1550#else /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01001551 char *dst = src = strchrnul(src, '\\');
Francis Laniel36836fc2023-12-22 22:02:28 +01001552#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01001553 while (1) {
1554 if (*src == '\\') {
1555 src++;
1556 if (*src != '\0') {
1557 /* \x -> x */
1558 *dst++ = *src++;
1559 continue;
1560 }
1561 /* else: "\<nul>". Do not delete this backslash.
1562 * Testcase: eval 'echo ok\'
1563 */
1564 *dst++ = '\\';
1565 /* fallthrough */
1566 }
1567 if ((*dst++ = *src++) == '\0')
1568 break;
1569 }
1570 return dst;
1571}
1572
1573static char **add_strings_to_strings(char **strings, char **add, int need_to_dup)
1574{
1575 int i;
1576 unsigned count1;
1577 unsigned count2;
1578 char **v;
1579
1580 v = strings;
1581 count1 = 0;
1582 if (v) {
1583 while (*v) {
1584 count1++;
1585 v++;
1586 }
1587 }
1588 count2 = 0;
1589 v = add;
1590 while (*v) {
1591 count2++;
1592 v++;
1593 }
1594 v = xrealloc(strings, (count1 + count2 + 1) * sizeof(char*));
1595 v[count1 + count2] = NULL;
1596 i = count2;
1597 while (--i >= 0)
1598 v[count1 + i] = (need_to_dup ? xstrdup(add[i]) : add[i]);
1599 return v;
1600}
1601#if LEAK_HUNTING
1602static char **xx_add_strings_to_strings(int lineno, char **strings, char **add, int need_to_dup)
1603{
1604 char **ptr = add_strings_to_strings(strings, add, need_to_dup);
1605 fdprintf(2, "line %d: add_strings_to_strings %p\n", lineno, ptr);
1606 return ptr;
1607}
1608#define add_strings_to_strings(strings, add, need_to_dup) \
1609 xx_add_strings_to_strings(__LINE__, strings, add, need_to_dup)
1610#endif
1611
1612/* Note: takes ownership of "add" ptr (it is not strdup'ed) */
1613static char **add_string_to_strings(char **strings, char *add)
1614{
1615 char *v[2];
1616 v[0] = add;
1617 v[1] = NULL;
1618 return add_strings_to_strings(strings, v, /*dup:*/ 0);
1619}
Francis Laniel36836fc2023-12-22 22:02:28 +01001620
Francis Laniel110b7692023-12-22 22:02:27 +01001621#if LEAK_HUNTING
1622static char **xx_add_string_to_strings(int lineno, char **strings, char *add)
1623{
1624 char **ptr = add_string_to_strings(strings, add);
1625 fdprintf(2, "line %d: add_string_to_strings %p\n", lineno, ptr);
1626 return ptr;
1627}
1628#define add_string_to_strings(strings, add) \
1629 xx_add_string_to_strings(__LINE__, strings, add)
1630#endif
1631
1632static void free_strings(char **strings)
1633{
1634 char **v;
1635
1636 if (!strings)
1637 return;
1638 v = strings;
1639 while (*v) {
1640 free(*v);
1641 v++;
1642 }
1643 free(strings);
1644}
1645
Francis Laniel36836fc2023-12-22 22:02:28 +01001646#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01001647static int dup_CLOEXEC(int fd, int avoid_fd)
1648{
1649 int newfd;
1650 repeat:
1651 newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1);
1652 if (newfd >= 0) {
1653 if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */
Francis Laniel03061a82024-09-03 19:09:43 +02001654 close_on_exec_on(newfd);
Francis Laniel110b7692023-12-22 22:02:27 +01001655 } else { /* newfd < 0 */
1656 if (errno == EBUSY)
1657 goto repeat;
1658 if (errno == EINTR)
1659 goto repeat;
Francis Laniel03061a82024-09-03 19:09:43 +02001660 if (errno != EBADF) {
1661 /* "echo >&9999" gets EINVAL trying to save fd 1 to above 9999.
1662 * We could try saving it _below_ 9999 instead (how?), but
1663 * this probably means that dup2(9999,1) to effectuate >&9999
1664 * would also not work: fd 9999 can't exist.
1665 * (This differs from "echo >&99" where saving works, but
1666 * subsequent dup2(99,1) fails if fd 99 is not open).
1667 */
1668 bb_perror_msg("fcntl(%d,F_DUPFD,%d)", fd, avoid_fd + 1);
1669 }
Francis Laniel110b7692023-12-22 22:02:27 +01001670 }
1671 return newfd;
1672}
1673
1674static int xdup_CLOEXEC_and_close(int fd, int avoid_fd)
1675{
1676 int newfd;
1677 repeat:
1678 newfd = fcntl(fd, F_DUPFD_CLOEXEC, avoid_fd + 1);
1679 if (newfd < 0) {
1680 if (errno == EBUSY)
1681 goto repeat;
1682 if (errno == EINTR)
1683 goto repeat;
1684 /* fd was not open? */
1685 if (errno == EBADF)
1686 return fd;
1687 xfunc_die();
1688 }
1689 if (F_DUPFD_CLOEXEC == F_DUPFD) /* if old libc (w/o F_DUPFD_CLOEXEC) */
Francis Laniel03061a82024-09-03 19:09:43 +02001690 close_on_exec_on(newfd);
Francis Laniel110b7692023-12-22 22:02:27 +01001691 close(fd);
1692 return newfd;
1693}
1694
Francis Laniel110b7692023-12-22 22:02:27 +01001695/* Manipulating HFILEs */
1696static HFILE *hfopen(const char *name)
1697{
1698 HFILE *fp;
1699 int fd;
1700
1701 fd = STDIN_FILENO;
1702 if (name) {
1703 fd = open(name, O_RDONLY | O_CLOEXEC);
1704 if (fd < 0)
1705 return NULL;
1706 if (O_CLOEXEC == 0) /* ancient libc */
1707 close_on_exec_on(fd);
1708 }
1709
1710 fp = xmalloc(sizeof(*fp));
1711 if (name == NULL)
1712 G.HFILE_stdin = fp;
1713 fp->fd = fd;
1714 fp->cur = fp->end = fp->buf;
1715 fp->next_hfile = G.HFILE_list;
1716 G.HFILE_list = fp;
1717 return fp;
1718}
1719static void hfclose(HFILE *fp)
1720{
1721 HFILE **pp = &G.HFILE_list;
1722 while (*pp) {
1723 HFILE *cur = *pp;
1724 if (cur == fp) {
1725 *pp = cur->next_hfile;
1726 break;
1727 }
1728 pp = &cur->next_hfile;
1729 }
1730 if (fp->fd >= 0)
1731 close(fp->fd);
1732 free(fp);
1733}
1734static int refill_HFILE_and_getc(HFILE *fp)
1735{
1736 int n;
1737
1738 if (fp->fd < 0) {
1739 /* Already saw EOF */
1740 return EOF;
1741 }
1742#if ENABLE_HUSH_INTERACTIVE && !ENABLE_FEATURE_EDITING
1743 /* If user presses ^C, read() restarts after SIGINT (we use SA_RESTART).
1744 * IOW: ^C will not immediately stop line input.
1745 * But poll() is different: it does NOT restart after signals.
1746 */
1747 if (fp == G.HFILE_stdin) {
1748 struct pollfd pfd[1];
1749 pfd[0].fd = fp->fd;
1750 pfd[0].events = POLLIN;
1751 n = poll(pfd, 1, -1);
1752 if (n < 0
1753 /*&& errno == EINTR - assumed true */
1754 && sigismember(&G.pending_set, SIGINT)
1755 ) {
1756 return '\0';
1757 }
1758 }
1759#else
1760/* if FEATURE_EDITING=y, we do not use this routine for interactive input */
1761#endif
1762 /* Try to buffer more input */
1763 n = safe_read(fp->fd, fp->buf, sizeof(fp->buf));
1764 if (n < 0) {
1765 bb_simple_perror_msg("read error");
1766 n = 0;
1767 }
1768 fp->cur = fp->buf;
1769 fp->end = fp->buf + n;
1770 if (n == 0) {
1771 /* EOF/error */
1772 close(fp->fd);
1773 fp->fd = -1;
1774 return EOF;
1775 }
1776 return (unsigned char)(*fp->cur++);
1777}
1778/* Inlined for common case of non-empty buffer.
1779 */
1780static ALWAYS_INLINE int hfgetc(HFILE *fp)
1781{
1782 if (fp->cur < fp->end)
1783 return (unsigned char)(*fp->cur++);
1784 /* Buffer empty */
1785 return refill_HFILE_and_getc(fp);
1786}
1787static int move_HFILEs_on_redirect(int fd, int avoid_fd)
1788{
1789 HFILE *fl = G.HFILE_list;
1790 while (fl) {
1791 if (fd == fl->fd) {
1792 /* We use it only on script files, they are all CLOEXEC */
1793 fl->fd = xdup_CLOEXEC_and_close(fd, avoid_fd);
1794 debug_printf_redir("redirect_fd %d: matches a script fd, moving it to %d\n", fd, fl->fd);
1795 return 1; /* "found and moved" */
1796 }
1797 fl = fl->next_hfile;
1798 }
1799#if ENABLE_HUSH_MODE_X
1800 if (G.x_mode_fd > 0 && fd == G.x_mode_fd) {
1801 G.x_mode_fd = xdup_CLOEXEC_and_close(fd, avoid_fd);
1802 return 1; /* "found and moved" */
1803 }
1804#endif
1805 return 0; /* "not in the list" */
1806}
1807#if ENABLE_FEATURE_SH_STANDALONE && BB_MMU
1808static void close_all_HFILE_list(void)
1809{
1810 HFILE *fl = G.HFILE_list;
1811 while (fl) {
1812 /* hfclose would also free HFILE object.
1813 * It is disastrous if we share memory with a vforked parent.
1814 * I'm not sure we never come here after vfork.
1815 * Therefore just close fd, nothing more.
1816 *
1817 * ">" instead of ">=": we don't close fd#0,
1818 * interactive shell uses hfopen(NULL) as stdin input
1819 * which has fl->fd == 0, but fd#0 gets redirected in pipes.
1820 * If we'd close it here, then e.g. interactive "set | sort"
1821 * with NOFORKed sort, would have sort's input fd closed.
1822 */
1823 if (fl->fd > 0)
1824 /*hfclose(fl); - unsafe */
1825 close(fl->fd);
1826 fl = fl->next_hfile;
1827 }
1828}
1829#endif
1830static int fd_in_HFILEs(int fd)
1831{
1832 HFILE *fl = G.HFILE_list;
1833 while (fl) {
1834 if (fl->fd == fd)
1835 return 1;
1836 fl = fl->next_hfile;
1837 }
1838 return 0;
1839}
1840
Francis Laniel36836fc2023-12-22 22:02:28 +01001841#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01001842
1843/* Helpers for setting new $n and restoring them back
1844 */
1845typedef struct save_arg_t {
1846 char *sv_argv0;
1847 char **sv_g_argv;
1848 int sv_g_argc;
Francis Laniel36836fc2023-12-22 22:02:28 +01001849#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01001850 IF_HUSH_SET(smallint sv_g_malloced;)
Francis Laniel36836fc2023-12-22 22:02:28 +01001851#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01001852} save_arg_t;
1853
Francis Laniel36836fc2023-12-22 22:02:28 +01001854#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01001855static void save_and_replace_G_args(save_arg_t *sv, char **argv)
1856{
1857 sv->sv_argv0 = argv[0];
1858 sv->sv_g_argv = G.global_argv;
1859 sv->sv_g_argc = G.global_argc;
1860 IF_HUSH_SET(sv->sv_g_malloced = G.global_args_malloced;)
1861
1862 argv[0] = G.global_argv[0]; /* retain $0 */
1863 G.global_argv = argv;
1864 IF_HUSH_SET(G.global_args_malloced = 0;)
1865
1866 G.global_argc = 1 + string_array_len(argv + 1);
1867}
1868
1869static void restore_G_args(save_arg_t *sv, char **argv)
1870{
1871#if ENABLE_HUSH_SET
1872 if (G.global_args_malloced) {
1873 /* someone ran "set -- arg1 arg2 ...", undo */
1874 char **pp = G.global_argv;
1875 while (*++pp) /* note: does not free $0 */
1876 free(*pp);
1877 free(G.global_argv);
1878 }
1879#endif
1880 argv[0] = sv->sv_argv0;
1881 G.global_argv = sv->sv_g_argv;
1882 G.global_argc = sv->sv_g_argc;
1883 IF_HUSH_SET(G.global_args_malloced = sv->sv_g_malloced;)
1884}
Francis Laniel36836fc2023-12-22 22:02:28 +01001885#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01001886
Francis Laniel36836fc2023-12-22 22:02:28 +01001887#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01001888/* Basic theory of signal handling in shell
1889 * ========================================
1890 * This does not describe what hush does, rather, it is current understanding
1891 * what it _should_ do. If it doesn't, it's a bug.
1892 * http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#trap
1893 *
1894 * Signals are handled only after each pipe ("cmd | cmd | cmd" thing)
1895 * is finished or backgrounded. It is the same in interactive and
1896 * non-interactive shells, and is the same regardless of whether
1897 * a user trap handler is installed or a shell special one is in effect.
1898 * ^C or ^Z from keyboard seems to execute "at once" because it usually
1899 * backgrounds (i.e. stops) or kills all members of currently running
1900 * pipe.
1901 *
1902 * Wait builtin is interruptible by signals for which user trap is set
1903 * or by SIGINT in interactive shell.
1904 *
1905 * Trap handlers will execute even within trap handlers. (right?)
1906 *
1907 * User trap handlers are forgotten when subshell ("(cmd)") is entered,
1908 * except for handlers set to '' (empty string).
1909 *
1910 * If job control is off, backgrounded commands ("cmd &")
1911 * have SIGINT, SIGQUIT set to SIG_IGN.
1912 *
1913 * Commands which are run in command substitution ("`cmd`")
1914 * have SIGTTIN, SIGTTOU, SIGTSTP set to SIG_IGN.
1915 *
1916 * Ordinary commands have signals set to SIG_IGN/DFL as inherited
1917 * by the shell from its parent.
1918 *
1919 * Signals which differ from SIG_DFL action
1920 * (note: child (i.e., [v]forked) shell is not an interactive shell):
1921 *
1922 * SIGQUIT: ignore
1923 * SIGTERM (interactive): ignore
1924 * SIGHUP (interactive):
Francis Laniele7ca3a32023-12-22 22:02:42 +01001925 * Send SIGCONT to stopped jobs, send SIGHUP to all jobs and exit.
1926 * Kernel would do this for us ("orphaned process group" handling
1927 * according to POSIX) if we are a session leader and thus our death
1928 * frees the controlling tty, but to be bash-compatible, we also do it
1929 * for every interactive shell's death by SIGHUP.
1930 * (Also, we need to restore tty pgrp, otherwise e.g. Midnight Commander
1931 * backgrounds when hush started from it gets killed by SIGHUP).
Francis Laniel110b7692023-12-22 22:02:27 +01001932 * SIGTTIN, SIGTTOU, SIGTSTP (if job control is on): ignore
1933 * Note that ^Z is handled not by trapping SIGTSTP, but by seeing
1934 * that all pipe members are stopped. Try this in bash:
1935 * while :; do :; done - ^Z does not background it
1936 * (while :; do :; done) - ^Z backgrounds it
1937 * SIGINT (interactive): wait for last pipe, ignore the rest
1938 * of the command line, show prompt. NB: ^C does not send SIGINT
1939 * to interactive shell while shell is waiting for a pipe,
1940 * since shell is bg'ed (is not in foreground process group).
1941 * Example 1: this waits 5 sec, but does not execute ls:
1942 * "echo $$; sleep 5; ls -l" + "kill -INT <pid>"
1943 * Example 2: this does not wait and does not execute ls:
1944 * "echo $$; sleep 5 & wait; ls -l" + "kill -INT <pid>"
1945 * Example 3: this does not wait 5 sec, but executes ls:
1946 * "sleep 5; ls -l" + press ^C
1947 * Example 4: this does not wait and does not execute ls:
1948 * "sleep 5 & wait; ls -l" + press ^C
1949 *
1950 * (What happens to signals which are IGN on shell start?)
1951 * (What happens with signal mask on shell start?)
1952 *
1953 * Old implementation
1954 * ==================
1955 * We use in-kernel pending signal mask to determine which signals were sent.
1956 * We block all signals which we don't want to take action immediately,
1957 * i.e. we block all signals which need to have special handling as described
1958 * above, and all signals which have traps set.
1959 * After each pipe execution, we extract any pending signals via sigtimedwait()
1960 * and act on them.
1961 *
1962 * unsigned special_sig_mask: a mask of such "special" signals
1963 * sigset_t blocked_set: current blocked signal set
1964 *
1965 * "trap - SIGxxx":
1966 * clear bit in blocked_set unless it is also in special_sig_mask
1967 * "trap 'cmd' SIGxxx":
1968 * set bit in blocked_set (even if 'cmd' is '')
1969 * after [v]fork, if we plan to be a shell:
1970 * unblock signals with special interactive handling
1971 * (child shell is not interactive),
1972 * unset all traps except '' (note: regardless of child shell's type - {}, (), etc)
1973 * after [v]fork, if we plan to exec:
1974 * POSIX says fork clears pending signal mask in child - no need to clear it.
1975 * Restore blocked signal set to one inherited by shell just prior to exec.
1976 *
1977 * Note: as a result, we do not use signal handlers much. The only uses
1978 * are to count SIGCHLDs
1979 * and to restore tty pgrp on signal-induced exit.
1980 *
1981 * Note 2 (compat):
1982 * Standard says "When a subshell is entered, traps that are not being ignored
1983 * are set to the default actions". bash interprets it so that traps which
1984 * are set to '' (ignore) are NOT reset to defaults. We do the same.
1985 *
1986 * Problem: the above approach makes it unwieldy to catch signals while
1987 * we are in read builtin, or while we read commands from stdin:
1988 * masked signals are not visible!
1989 *
1990 * New implementation
1991 * ==================
1992 * We record each signal we are interested in by installing signal handler
1993 * for them - a bit like emulating kernel pending signal mask in userspace.
1994 * We are interested in: signals which need to have special handling
1995 * as described above, and all signals which have traps set.
1996 * Signals are recorded in pending_set.
1997 * After each pipe execution, we extract any pending signals
1998 * and act on them.
1999 *
2000 * unsigned special_sig_mask: a mask of shell-special signals.
2001 * unsigned fatal_sig_mask: a mask of signals on which we restore tty pgrp.
2002 * char *traps[sig] if trap for sig is set (even if it's '').
2003 * sigset_t pending_set: set of sigs we received.
2004 *
2005 * "trap - SIGxxx":
2006 * if sig is in special_sig_mask, set handler back to:
2007 * record_pending_signo, or to IGN if it's a tty stop signal
2008 * if sig is in fatal_sig_mask, set handler back to sigexit.
2009 * else: set handler back to SIG_DFL
2010 * "trap 'cmd' SIGxxx":
2011 * set handler to record_pending_signo.
2012 * "trap '' SIGxxx":
2013 * set handler to SIG_IGN.
2014 * after [v]fork, if we plan to be a shell:
2015 * set signals with special interactive handling to SIG_DFL
2016 * (because child shell is not interactive),
2017 * unset all traps except '' (note: regardless of child shell's type - {}, (), etc)
2018 * after [v]fork, if we plan to exec:
2019 * POSIX says fork clears pending signal mask in child - no need to clear it.
2020 *
2021 * To make wait builtin interruptible, we handle SIGCHLD as special signal,
2022 * otherwise (if we leave it SIG_DFL) sigsuspend in wait builtin will not wake up on it.
2023 *
2024 * Note (compat):
2025 * Standard says "When a subshell is entered, traps that are not being ignored
2026 * are set to the default actions". bash interprets it so that traps which
2027 * are set to '' (ignore) are NOT reset to defaults. We do the same.
2028 */
2029enum {
2030 SPECIAL_INTERACTIVE_SIGS = 0
2031 | (1 << SIGTERM)
2032 | (1 << SIGINT)
2033 | (1 << SIGHUP)
2034 ,
2035 SPECIAL_JOBSTOP_SIGS = 0
2036#if ENABLE_HUSH_JOB
2037 | (1 << SIGTTIN)
2038 | (1 << SIGTTOU)
2039 | (1 << SIGTSTP)
2040#endif
2041 ,
2042};
2043
2044static void record_pending_signo(int sig)
2045{
2046 sigaddset(&G.pending_set, sig);
Francis Laniele7ca3a32023-12-22 22:02:42 +01002047#if ENABLE_FEATURE_EDITING
2048 if (sig != SIGCHLD
2049 || (G_traps && G_traps[SIGCHLD] && G_traps[SIGCHLD][0])
2050 /* ^^^ if SIGCHLD, interrupt line reading only if it has a trap */
2051 ) {
2052 bb_got_signal = sig; /* for read_line_input: "we got a signal" */
2053 }
2054#endif
Francis Laniel110b7692023-12-22 22:02:27 +01002055#if ENABLE_HUSH_FAST
2056 if (sig == SIGCHLD) {
2057 G.count_SIGCHLD++;
2058//bb_error_msg("[%d] SIGCHLD_handler: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD);
2059 }
2060#endif
2061}
2062
2063static sighandler_t install_sighandler(int sig, sighandler_t handler)
2064{
2065 struct sigaction old_sa;
2066
2067 /* We could use signal() to install handlers... almost:
2068 * except that we need to mask ALL signals while handlers run.
2069 * I saw signal nesting in strace, race window isn't small.
2070 * SA_RESTART is also needed, but in Linux, signal()
2071 * sets SA_RESTART too.
2072 */
2073 /* memset(&G.sa, 0, sizeof(G.sa)); - already done */
2074 /* sigfillset(&G.sa.sa_mask); - already done */
2075 /* G.sa.sa_flags = SA_RESTART; - already done */
2076 G.sa.sa_handler = handler;
2077 sigaction(sig, &G.sa, &old_sa);
2078 return old_sa.sa_handler;
2079}
Francis Laniel36836fc2023-12-22 22:02:28 +01002080#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01002081
Francis Laniel36836fc2023-12-22 22:02:28 +01002082#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01002083static void hush_exit(int exitcode) NORETURN;
2084
2085static void restore_ttypgrp_and__exit(void) NORETURN;
2086static void restore_ttypgrp_and__exit(void)
2087{
2088 /* xfunc has failed! die die die */
2089 /* no EXIT traps, this is an escape hatch! */
2090 G.exiting = 1;
2091 hush_exit(xfunc_error_retval);
2092}
2093
2094#if ENABLE_HUSH_JOB
2095
2096/* Needed only on some libc:
2097 * It was observed that on exit(), fgetc'ed buffered data
2098 * gets "unwound" via lseek(fd, -NUM, SEEK_CUR).
2099 * With the net effect that even after fork(), not vfork(),
2100 * exit() in NOEXECed applet in "sh SCRIPT":
2101 * noexec_applet_here
2102 * echo END_OF_SCRIPT
2103 * lseeks fd in input FILE object from EOF to "e" in "echo END_OF_SCRIPT".
2104 * This makes "echo END_OF_SCRIPT" executed twice.
2105 * Similar problems can be seen with msg_and_die_if_script() -> xfunc_die()
2106 * and in `cmd` handling.
2107 * If set as die_func(), this makes xfunc_die() exit via _exit(), not exit():
2108 */
2109static void fflush_and__exit(void) NORETURN;
2110static void fflush_and__exit(void)
2111{
2112 fflush_all();
2113 _exit(xfunc_error_retval);
2114}
2115
2116/* After [v]fork, in child: do not restore tty pgrp on xfunc death */
2117# define disable_restore_tty_pgrp_on_exit() (die_func = fflush_and__exit)
2118/* After [v]fork, in parent: restore tty pgrp on xfunc death */
2119# define enable_restore_tty_pgrp_on_exit() (die_func = restore_ttypgrp_and__exit)
2120
2121/* Restores tty foreground process group, and exits.
2122 * May be called as signal handler for fatal signal
2123 * (will resend signal to itself, producing correct exit state)
2124 * or called directly with -EXITCODE.
2125 * We also call it if xfunc is exiting.
2126 */
2127static void sigexit(int sig) NORETURN;
2128static void sigexit(int sig)
2129{
2130 /* Careful: we can end up here after [v]fork. Do not restore
2131 * tty pgrp then, only top-level shell process does that */
2132 if (G_saved_tty_pgrp && getpid() == G.root_pid) {
2133 /* Disable all signals: job control, SIGPIPE, etc.
2134 * Mostly paranoid measure, to prevent infinite SIGTTOU.
2135 */
2136 sigprocmask_allsigs(SIG_BLOCK);
2137 tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp);
2138 }
2139
2140 /* Not a signal, just exit */
2141 if (sig <= 0)
2142 _exit(- sig);
2143
2144 kill_myself_with_sig(sig); /* does not return */
2145}
2146#else
2147
2148# define disable_restore_tty_pgrp_on_exit() ((void)0)
2149# define enable_restore_tty_pgrp_on_exit() ((void)0)
2150
2151#endif
Francis Laniel36836fc2023-12-22 22:02:28 +01002152#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01002153
Francis Laniel36836fc2023-12-22 22:02:28 +01002154#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01002155static sighandler_t pick_sighandler(unsigned sig)
2156{
2157 sighandler_t handler = SIG_DFL;
2158 if (sig < sizeof(unsigned)*8) {
2159 unsigned sigmask = (1 << sig);
2160
2161#if ENABLE_HUSH_JOB
2162 /* is sig fatal? */
2163 if (G_fatal_sig_mask & sigmask)
2164 handler = sigexit;
2165 else
2166#endif
2167 /* sig has special handling? */
2168 if (G.special_sig_mask & sigmask) {
2169 handler = record_pending_signo;
2170 /* TTIN/TTOU/TSTP can't be set to record_pending_signo
2171 * in order to ignore them: they will be raised
2172 * in an endless loop when we try to do some
2173 * terminal ioctls! We do have to _ignore_ these.
2174 */
2175 if (SPECIAL_JOBSTOP_SIGS & sigmask)
2176 handler = SIG_IGN;
2177 }
2178 }
2179 return handler;
2180}
Francis Laniel36836fc2023-12-22 22:02:28 +01002181#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01002182
Francis Laniel36836fc2023-12-22 22:02:28 +01002183#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01002184/* Restores tty foreground process group, and exits. */
2185static void hush_exit(int exitcode)
2186{
2187#if ENABLE_FEATURE_EDITING_SAVE_ON_EXIT
2188 save_history(G.line_input_state); /* may be NULL */
2189#endif
2190
2191 fflush_all();
2192 if (G.exiting <= 0 && G_traps && G_traps[0] && G_traps[0][0]) {
2193 char *argv[3];
2194 /* argv[0] is unused */
2195 argv[1] = xstrdup(G_traps[0]); /* copy, since EXIT trap handler may modify G_traps[0] */
2196 argv[2] = NULL;
2197 G.exiting = 1; /* prevent EXIT trap recursion */
2198 /* Note: G_traps[0] is not cleared!
2199 * "trap" will still show it, if executed
2200 * in the handler */
2201 builtin_eval(argv);
2202 }
2203
2204#if ENABLE_FEATURE_CLEAN_UP
2205 {
2206 struct variable *cur_var;
2207 if (G.cwd != bb_msg_unknown)
2208 free((char*)G.cwd);
2209 cur_var = G.top_var;
2210 while (cur_var) {
2211 struct variable *tmp = cur_var;
2212 if (!cur_var->max_len)
2213 free(cur_var->varstr);
2214 cur_var = cur_var->next;
2215 free(tmp);
2216 }
2217 }
2218#endif
2219
2220 fflush_all();
2221#if ENABLE_HUSH_JOB
2222 sigexit(- (exitcode & 0xff));
2223#else
2224 _exit(exitcode);
2225#endif
2226}
2227
2228//TODO: return a mask of ALL handled sigs?
2229static int check_and_run_traps(void)
2230{
2231 int last_sig = 0;
2232
2233 while (1) {
2234 int sig;
2235
2236 if (sigisemptyset(&G.pending_set))
2237 break;
2238 sig = 0;
2239 do {
2240 sig++;
2241 if (sigismember(&G.pending_set, sig)) {
2242 sigdelset(&G.pending_set, sig);
2243 goto got_sig;
2244 }
2245 } while (sig < NSIG);
2246 break;
2247 got_sig:
2248#if ENABLE_HUSH_TRAP
2249 if (G_traps && G_traps[sig]) {
2250 debug_printf_exec("%s: sig:%d handler:'%s'\n", __func__, sig, G.traps[sig]);
2251 if (G_traps[sig][0]) {
2252 /* We have user-defined handler */
2253 smalluint save_rcode;
2254 int save_pre;
2255 char *argv[3];
2256 /* argv[0] is unused */
2257 argv[1] = xstrdup(G_traps[sig]);
2258 /* why strdup? trap can modify itself: trap 'trap "echo oops" INT' INT */
2259 argv[2] = NULL;
2260 save_pre = G.pre_trap_exitcode;
2261 G.pre_trap_exitcode = save_rcode = G.last_exitcode;
2262 builtin_eval(argv);
2263 free(argv[1]);
2264 G.pre_trap_exitcode = save_pre;
2265 G.last_exitcode = save_rcode;
2266# if ENABLE_HUSH_FUNCTIONS
2267 if (G.return_exitcode >= 0) {
2268 debug_printf_exec("trap exitcode:%d\n", G.return_exitcode);
2269 G.last_exitcode = G.return_exitcode;
2270 }
2271# endif
2272 last_sig = sig;
2273 } /* else: "" trap, ignoring signal */
2274 continue;
2275 }
2276#endif
2277 /* not a trap: special action */
2278 switch (sig) {
2279 case SIGINT:
2280 debug_printf_exec("%s: sig:%d default SIGINT handler\n", __func__, sig);
2281 G.flag_SIGINT = 1;
2282 last_sig = sig;
2283 break;
2284#if ENABLE_HUSH_JOB
2285 case SIGHUP: {
Francis Laniele7ca3a32023-12-22 22:02:42 +01002286 /* if (G_interactive_fd) - no need to check, the handler
2287 * is only installed if we *are* interactive */
2288 {
2289 /* bash compat: "Before exiting, an interactive
2290 * shell resends the SIGHUP to all jobs, running
2291 * or stopped. Stopped jobs are sent SIGCONT
2292 * to ensure that they receive the SIGHUP."
2293 */
2294 struct pipe *job;
2295 debug_printf_exec("%s: sig:%d default SIGHUP handler\n", __func__, sig);
2296 /* bash is observed to signal whole process groups,
2297 * not individual processes */
2298 for (job = G.job_list; job; job = job->next) {
2299 if (job->pgrp <= 0)
2300 continue;
2301 debug_printf_exec("HUPing pgrp %d\n", job->pgrp);
2302 if (kill(- job->pgrp, SIGHUP) == 0)
2303 kill(- job->pgrp, SIGCONT);
2304 }
Francis Laniel110b7692023-12-22 22:02:27 +01002305 }
Francis Laniele7ca3a32023-12-22 22:02:42 +01002306 /* this restores tty pgrp, then kills us with SIGHUP */
Francis Laniel110b7692023-12-22 22:02:27 +01002307 sigexit(SIGHUP);
2308 }
2309#endif
2310#if ENABLE_HUSH_FAST
2311 case SIGCHLD:
2312 debug_printf_exec("%s: sig:%d default SIGCHLD handler\n", __func__, sig);
2313 G.count_SIGCHLD++;
2314//bb_error_msg("[%d] check_and_run_traps: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD);
2315 /* Note:
2316 * We don't do 'last_sig = sig' here -> NOT returning this sig.
2317 * This simplifies wait builtin a bit.
2318 */
2319 break;
2320#endif
2321 default: /* ignored: */
2322 debug_printf_exec("%s: sig:%d default handling is to ignore\n", __func__, sig);
2323 /* SIGTERM, SIGQUIT, SIGTTIN, SIGTTOU, SIGTSTP */
2324 /* Note:
2325 * We don't do 'last_sig = sig' here -> NOT returning this sig.
2326 * Example: wait is not interrupted by TERM
2327 * in interactive shell, because TERM is ignored.
2328 */
2329 break;
2330 }
2331 }
2332 return last_sig;
2333}
2334
Francis Laniel110b7692023-12-22 22:02:27 +01002335static const char *get_cwd(int force)
2336{
2337 if (force || G.cwd == NULL) {
2338 /* xrealloc_getcwd_or_warn(arg) calls free(arg),
2339 * we must not try to free(bb_msg_unknown) */
2340 if (G.cwd == bb_msg_unknown)
2341 G.cwd = NULL;
2342 G.cwd = xrealloc_getcwd_or_warn((char *)G.cwd);
2343 if (!G.cwd)
2344 G.cwd = bb_msg_unknown;
2345 }
2346 return G.cwd;
2347}
2348
Francis Laniel36836fc2023-12-22 22:02:28 +01002349#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01002350
2351/*
2352 * Shell and environment variable support
2353 */
Francis Laniele7ca3a32023-12-22 22:02:42 +01002354static struct variable **get_ptr_to_local_var(const char *name)
Francis Laniel110b7692023-12-22 22:02:27 +01002355{
2356 struct variable **pp;
2357 struct variable *cur;
2358
2359 pp = &G.top_var;
2360 while ((cur = *pp) != NULL) {
Francis Laniele7ca3a32023-12-22 22:02:42 +01002361 if (varcmp(cur->varstr, name) == 0)
Francis Laniel110b7692023-12-22 22:02:27 +01002362 return pp;
2363 pp = &cur->next;
2364 }
2365 return NULL;
2366}
2367
2368static const char* FAST_FUNC get_local_var_value(const char *name)
2369{
2370 struct variable **vpp;
Francis Laniel110b7692023-12-22 22:02:27 +01002371
2372 if (G.expanded_assignments) {
2373 char **cpp = G.expanded_assignments;
2374 while (*cpp) {
2375 char *cp = *cpp;
Francis Laniele7ca3a32023-12-22 22:02:42 +01002376 if (varcmp(cp, name) == 0)
2377 return strchr(cp, '=') + 1;
Francis Laniel110b7692023-12-22 22:02:27 +01002378 cpp++;
2379 }
2380 }
2381
Francis Laniele7ca3a32023-12-22 22:02:42 +01002382 vpp = get_ptr_to_local_var(name);
Francis Laniel110b7692023-12-22 22:02:27 +01002383 if (vpp)
Francis Laniele7ca3a32023-12-22 22:02:42 +01002384 return strchr((*vpp)->varstr, '=') + 1;
Francis Laniel110b7692023-12-22 22:02:27 +01002385
Francis Laniel36836fc2023-12-22 22:02:28 +01002386#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01002387 if (strcmp(name, "PPID") == 0)
2388 return utoa(G.root_ppid);
Francis Laniel36836fc2023-12-22 22:02:28 +01002389#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01002390 // bash compat: UID? EUID?
2391#if ENABLE_HUSH_RANDOM_SUPPORT
2392 if (strcmp(name, "RANDOM") == 0)
2393 return utoa(next_random(&G.random_gen));
2394#endif
2395#if ENABLE_HUSH_LINENO_VAR
2396 if (strcmp(name, "LINENO") == 0)
2397 return utoa(G.execute_lineno);
2398#endif
2399#if BASH_EPOCH_VARS
2400 {
2401 const char *fmt = NULL;
2402 if (strcmp(name, "EPOCHSECONDS") == 0)
2403 fmt = "%llu";
2404 else if (strcmp(name, "EPOCHREALTIME") == 0)
2405 fmt = "%llu.%06u";
2406 if (fmt) {
2407 struct timeval tv;
2408 xgettimeofday(&tv);
2409 sprintf(G.epoch_buf, fmt, (unsigned long long)tv.tv_sec,
2410 (unsigned)tv.tv_usec);
2411 return G.epoch_buf;
2412 }
2413 }
2414#endif
2415 return NULL;
2416}
2417
Francis Laniel36836fc2023-12-22 22:02:28 +01002418#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01002419#if ENABLE_HUSH_GETOPTS
Francis Laniele7ca3a32023-12-22 22:02:42 +01002420static void handle_changed_special_names(const char *name)
Francis Laniel110b7692023-12-22 22:02:27 +01002421{
Francis Laniele7ca3a32023-12-22 22:02:42 +01002422 if (varcmp(name, "OPTIND") == 0) {
2423 G.getopt_count = 0;
2424 return;
Francis Laniel110b7692023-12-22 22:02:27 +01002425 }
2426}
2427#else
2428/* Do not even bother evaluating arguments */
2429# define handle_changed_special_names(...) ((void)0)
2430#endif
Francis Laniel36836fc2023-12-22 22:02:28 +01002431#else /* __U_BOOT__ */
2432/* Do not even bother evaluating arguments */
2433# define handle_changed_special_names(...) ((void)0)
2434#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01002435
2436/* str holds "NAME=VAL" and is expected to be malloced.
2437 * We take ownership of it.
2438 */
Francis Laniel36836fc2023-12-22 22:02:28 +01002439#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01002440#define SETFLAG_EXPORT (1 << 0)
2441#define SETFLAG_UNEXPORT (1 << 1)
2442#define SETFLAG_MAKE_RO (1 << 2)
Francis Laniel36836fc2023-12-22 22:02:28 +01002443#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01002444#define SETFLAG_VARLVL_SHIFT 3
Francis Laniel36836fc2023-12-22 22:02:28 +01002445#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01002446static int set_local_var(char *str, unsigned flags)
Francis Laniel36836fc2023-12-22 22:02:28 +01002447#else /* __U_BOOT__ */
2448int set_local_var_modern(char *str, int flags)
2449#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01002450{
2451 struct variable **cur_pp;
2452 struct variable *cur;
2453 char *free_me = NULL;
2454 char *eq_sign;
2455 int name_len;
2456 int retval;
Francis Laniel36836fc2023-12-22 22:02:28 +01002457#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01002458 unsigned local_lvl = (flags >> SETFLAG_VARLVL_SHIFT);
Francis Laniel36836fc2023-12-22 22:02:28 +01002459#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01002460
2461 eq_sign = strchr(str, '=');
2462 if (HUSH_DEBUG && !eq_sign)
2463 bb_simple_error_msg_and_die("BUG in setvar");
2464
2465 name_len = eq_sign - str + 1; /* including '=' */
2466 cur_pp = &G.top_var;
2467 while ((cur = *cur_pp) != NULL) {
2468 if (strncmp(cur->varstr, str, name_len) != 0) {
2469 cur_pp = &cur->next;
2470 continue;
2471 }
2472
Francis Laniel36836fc2023-12-22 22:02:28 +01002473#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01002474 /* We found an existing var with this name */
2475 if (cur->flg_read_only) {
2476 bb_error_msg("%s: readonly variable", str);
2477 free(str);
2478//NOTE: in bash, assignment in "export READONLY_VAR=Z" fails, and sets $?=1,
2479//but export per se succeeds (does put the var in env). We don't mimic that.
2480 return -1;
2481 }
2482 if (flags & SETFLAG_UNEXPORT) { // && cur->flg_export ?
2483 debug_printf_env("%s: unsetenv '%s'\n", __func__, str);
2484 *eq_sign = '\0';
2485 unsetenv(str);
2486 *eq_sign = '=';
2487 }
2488 if (cur->var_nest_level < local_lvl) {
2489 /* bash 3.2.33(1) and exported vars:
2490 * # export z=z
2491 * # f() { local z=a; env | grep ^z; }
2492 * # f
2493 * z=a
2494 * # env | grep ^z
2495 * z=z
2496 */
2497 if (cur->flg_export)
2498 flags |= SETFLAG_EXPORT;
2499 /* New variable is local ("local VAR=VAL" or
2500 * "VAR=VAL cmd")
2501 * and existing one is global, or local
2502 * on a lower level that new one.
2503 * Remove it from global variable list:
2504 */
2505 *cur_pp = cur->next;
2506 if (G.shadowed_vars_pp) {
2507 /* Save in "shadowed" list */
2508 debug_printf_env("shadowing %s'%s'/%u by '%s'/%u\n",
2509 cur->flg_export ? "exported " : "",
2510 cur->varstr, cur->var_nest_level, str, local_lvl
2511 );
2512 cur->next = *G.shadowed_vars_pp;
2513 *G.shadowed_vars_pp = cur;
2514 } else {
2515 /* Came from pseudo_exec_argv(), no need to save: delete it */
2516 debug_printf_env("shadow-deleting %s'%s'/%u by '%s'/%u\n",
2517 cur->flg_export ? "exported " : "",
2518 cur->varstr, cur->var_nest_level, str, local_lvl
2519 );
2520 if (cur->max_len == 0) /* allocated "VAR=VAL"? */
2521 free_me = cur->varstr; /* then free it later */
2522 free(cur);
2523 }
2524 break;
2525 }
Francis Laniel36836fc2023-12-22 22:02:28 +01002526#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01002527
2528 if (strcmp(cur->varstr + name_len, eq_sign + 1) == 0) {
2529 debug_printf_env("assignement '%s' does not change anything\n", str);
2530 free_and_exp:
2531 free(str);
2532 goto exp;
2533 }
2534
2535 /* Replace the value in the found "struct variable" */
2536 if (cur->max_len != 0) {
2537 if (cur->max_len >= strnlen(str, cur->max_len + 1)) {
2538 /* This one is from startup env, reuse space */
2539 debug_printf_env("reusing startup env for '%s'\n", str);
2540 strcpy(cur->varstr, str);
2541 goto free_and_exp;
2542 }
2543 /* Can't reuse */
2544 cur->max_len = 0;
2545 goto set_str_and_exp;
2546 }
2547 /* max_len == 0 signifies "malloced" var, which we can
2548 * (and have to) free. But we can't free(cur->varstr) here:
2549 * if cur->flg_export is 1, it is in the environment.
2550 * We should either unsetenv+free, or wait until putenv,
2551 * then putenv(new)+free(old).
2552 */
2553 free_me = cur->varstr;
2554 goto set_str_and_exp;
2555 }
2556
2557 /* Not found or shadowed - create new variable struct */
Francis Laniel36836fc2023-12-22 22:02:28 +01002558#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01002559 debug_printf_env("%s: alloc new var '%s'/%u\n", __func__, str, local_lvl);
Francis Laniel36836fc2023-12-22 22:02:28 +01002560#else /* __U_BOOT__ */
2561 debug_printf_env("%s: alloc new var '%s'\n", __func__, str);
2562#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01002563 cur = xzalloc(sizeof(*cur));
Francis Laniel36836fc2023-12-22 22:02:28 +01002564#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01002565 cur->var_nest_level = local_lvl;
Francis Laniel36836fc2023-12-22 22:02:28 +01002566#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01002567 cur->next = *cur_pp;
2568 *cur_pp = cur;
2569
2570 set_str_and_exp:
2571 cur->varstr = str;
2572 exp:
Francis Laniel36836fc2023-12-22 22:02:28 +01002573#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01002574#if !BB_MMU || ENABLE_HUSH_READONLY
2575 if (flags & SETFLAG_MAKE_RO) {
2576 cur->flg_read_only = 1;
2577 }
2578#endif
2579 if (flags & SETFLAG_EXPORT)
2580 cur->flg_export = 1;
Francis Laniel36836fc2023-12-22 22:02:28 +01002581#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01002582 retval = 0;
Francis Laniel36836fc2023-12-22 22:02:28 +01002583#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01002584 if (cur->flg_export) {
2585 if (flags & SETFLAG_UNEXPORT) {
2586 cur->flg_export = 0;
2587 /* unsetenv was already done */
2588 } else {
2589 debug_printf_env("%s: putenv '%s'/%u\n", __func__, cur->varstr, cur->var_nest_level);
2590 retval = putenv(cur->varstr);
2591 /* fall through to "free(free_me)" -
2592 * only now we can free old exported malloced string
2593 */
2594 }
2595 }
Francis Laniel36836fc2023-12-22 22:02:28 +01002596#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01002597 free(free_me);
2598
Francis Laniele7ca3a32023-12-22 22:02:42 +01002599 handle_changed_special_names(cur->varstr);
Francis Laniel110b7692023-12-22 22:02:27 +01002600
2601 return retval;
2602}
2603
Francis Laniel36836fc2023-12-22 22:02:28 +01002604#ifndef __U_BOOT__
Francis Laniele7ca3a32023-12-22 22:02:42 +01002605static int set_local_var0(char *str)
2606{
2607 return set_local_var(str, 0);
2608}
2609
Francis Laniel110b7692023-12-22 22:02:27 +01002610static void FAST_FUNC set_local_var_from_halves(const char *name, const char *val)
2611{
2612 char *var = xasprintf("%s=%s", name, val);
Francis Laniele7ca3a32023-12-22 22:02:42 +01002613 set_local_var0(var);
Francis Laniel110b7692023-12-22 22:02:27 +01002614}
2615
2616/* Used at startup and after each cd */
2617static void set_pwd_var(unsigned flag)
2618{
2619 set_local_var(xasprintf("PWD=%s", get_cwd(/*force:*/ 1)), flag);
2620}
Francis Laniel36836fc2023-12-22 22:02:28 +01002621#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01002622
2623#if ENABLE_HUSH_UNSET || ENABLE_HUSH_GETOPTS
Francis Laniele7ca3a32023-12-22 22:02:42 +01002624static int unset_local_var(const char *name)
Francis Laniel110b7692023-12-22 22:02:27 +01002625{
2626 struct variable *cur;
2627 struct variable **cur_pp;
2628
2629 cur_pp = &G.top_var;
2630 while ((cur = *cur_pp) != NULL) {
Francis Laniele7ca3a32023-12-22 22:02:42 +01002631 if (varcmp(cur->varstr, name) == 0) {
Francis Laniel110b7692023-12-22 22:02:27 +01002632 if (cur->flg_read_only) {
2633 bb_error_msg("%s: readonly variable", name);
2634 return EXIT_FAILURE;
2635 }
2636
2637 *cur_pp = cur->next;
2638 debug_printf_env("%s: unsetenv '%s'\n", __func__, cur->varstr);
2639 bb_unsetenv(cur->varstr);
2640 if (!cur->max_len)
2641 free(cur->varstr);
2642 free(cur);
2643
2644 break;
2645 }
2646 cur_pp = &cur->next;
2647 }
2648
2649 /* Handle "unset LINENO" et al even if did not find the variable to unset */
Francis Laniele7ca3a32023-12-22 22:02:42 +01002650 handle_changed_special_names(name);
Francis Laniel110b7692023-12-22 22:02:27 +01002651
2652 return EXIT_SUCCESS;
2653}
Francis Laniel110b7692023-12-22 22:02:27 +01002654#endif
2655
Francis Laniel36836fc2023-12-22 22:02:28 +01002656#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01002657/*
2658 * Helpers for "var1=val1 var2=val2 cmd" feature
2659 */
2660static void add_vars(struct variable *var)
2661{
2662 struct variable *next;
2663
2664 while (var) {
2665 next = var->next;
2666 var->next = G.top_var;
2667 G.top_var = var;
2668 if (var->flg_export) {
2669 debug_printf_env("%s: restoring exported '%s'/%u\n", __func__, var->varstr, var->var_nest_level);
2670 putenv(var->varstr);
2671 } else {
2672 debug_printf_env("%s: restoring variable '%s'/%u\n", __func__, var->varstr, var->var_nest_level);
2673 }
2674 var = next;
2675 }
2676}
2677
2678/* We put strings[i] into variable table and possibly putenv them.
2679 * If variable is read only, we can free the strings[i]
2680 * which attempts to overwrite it.
2681 * The strings[] vector itself is freed.
2682 */
2683static void set_vars_and_save_old(char **strings)
2684{
2685 char **s;
2686
2687 if (!strings)
2688 return;
2689
2690 s = strings;
2691 while (*s) {
2692 struct variable *var_p;
2693 struct variable **var_pp;
2694 char *eq;
2695
2696 eq = strchr(*s, '=');
2697 if (HUSH_DEBUG && !eq)
2698 bb_simple_error_msg_and_die("BUG in varexp4");
Francis Laniele7ca3a32023-12-22 22:02:42 +01002699 var_pp = get_ptr_to_local_var(*s);
Francis Laniel110b7692023-12-22 22:02:27 +01002700 if (var_pp) {
2701 var_p = *var_pp;
2702 if (var_p->flg_read_only) {
2703 char **p;
2704 bb_error_msg("%s: readonly variable", *s);
2705 /*
2706 * "VAR=V BLTIN" unsets VARs after BLTIN completes.
2707 * If VAR is readonly, leaving it in the list
2708 * after asssignment error (msg above)
2709 * causes doubled error message later, on unset.
2710 */
2711 debug_printf_env("removing/freeing '%s' element\n", *s);
2712 free(*s);
2713 p = s;
2714 do { *p = p[1]; p++; } while (*p);
2715 goto next;
2716 }
2717 /* below, set_local_var() with nest level will
2718 * "shadow" (remove) this variable from
2719 * global linked list.
2720 */
2721 }
2722 debug_printf_env("%s: env override '%s'/%u\n", __func__, *s, G.var_nest_level);
2723 set_local_var(*s, (G.var_nest_level << SETFLAG_VARLVL_SHIFT) | SETFLAG_EXPORT);
2724 s++;
2725 next: ;
2726 }
2727 free(strings);
2728}
2729
Francis Laniel110b7692023-12-22 22:02:27 +01002730/*
2731 * Unicode helper
2732 */
2733static void reinit_unicode_for_hush(void)
2734{
2735 /* Unicode support should be activated even if LANG is set
2736 * _during_ shell execution, not only if it was set when
2737 * shell was started. Therefore, re-check LANG every time:
2738 */
2739 if (ENABLE_FEATURE_CHECK_UNICODE_IN_ENV
2740 || ENABLE_UNICODE_USING_LOCALE
2741 ) {
2742 const char *s = get_local_var_value("LC_ALL");
2743 if (!s) s = get_local_var_value("LC_CTYPE");
2744 if (!s) s = get_local_var_value("LANG");
2745 reinit_unicode(s);
2746 }
2747}
2748
Francis Laniel36836fc2023-12-22 22:02:28 +01002749#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01002750/*
2751 * in_str support (strings, and "strings" read from files).
2752 */
2753
2754#if ENABLE_HUSH_INTERACTIVE
Francis Laniel36836fc2023-12-22 22:02:28 +01002755#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01002756/* To test correct lineedit/interactive behavior, type from command line:
2757 * echo $P\
2758 * \
2759 * AT\
2760 * H\
2761 * \
2762 * It exercises a lot of corner cases.
2763 */
2764static const char *setup_prompt_string(void)
2765{
2766 const char *prompt_str;
2767
2768 debug_printf_prompt("%s promptmode:%d\n", __func__, G.promptmode);
2769
2770# if ENABLE_FEATURE_EDITING_FANCY_PROMPT
2771 prompt_str = get_local_var_value(G.promptmode == 0 ? "PS1" : "PS2");
2772 if (!prompt_str)
2773 prompt_str = "";
2774# else
2775 prompt_str = "> "; /* if PS2, else... */
2776 if (G.promptmode == 0) { /* PS1 */
2777 /* No fancy prompts supported, (re)generate "CURDIR $ " by hand */
2778 free(G.PS1);
2779 /* bash uses $PWD value, even if it is set by user.
2780 * It uses current dir only if PWD is unset.
2781 * We always use current dir. */
2782 prompt_str = G.PS1 = xasprintf("%s %c ", get_cwd(0), (geteuid() != 0) ? '$' : '#');
2783 }
2784# endif
2785 debug_printf("prompt_str '%s'\n", prompt_str);
2786 return prompt_str;
2787}
Francis Laniel36836fc2023-12-22 22:02:28 +01002788#endif /* !__U_BOOT__ */
2789
2790#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01002791static int get_user_input(struct in_str *i)
Francis Laniel36836fc2023-12-22 22:02:28 +01002792#else /* __U_BOOT__ */
2793static void get_user_input(struct in_str *i)
2794#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01002795{
Francis Laniel36836fc2023-12-22 22:02:28 +01002796#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01002797# if ENABLE_FEATURE_EDITING
2798 /* In EDITING case, this function reads next input line,
2799 * saves it in i->p, then returns 1st char of it.
2800 */
2801 int r;
2802 const char *prompt_str;
2803
2804 prompt_str = setup_prompt_string();
2805 for (;;) {
2806 reinit_unicode_for_hush();
2807 G.flag_SIGINT = 0;
Francis Laniele7ca3a32023-12-22 22:02:42 +01002808
2809 bb_got_signal = 0;
2810 if (!sigisemptyset(&G.pending_set)) {
2811 /* Whoops, already got a signal, do not call read_line_input */
2812 bb_got_signal = r = -1;
2813 } else {
2814 /* For shell, LI_INTERRUPTIBLE is set:
2815 * read_line_input will abort on either
2816 * getting EINTR in poll() and bb_got_signal became != 0,
2817 * or if it sees bb_got_signal != 0
2818 * (IOW: if signal arrives before poll() is reached).
2819 * Interactive testcases:
2820 * (while kill -INT $$; do sleep 1; done) &
2821 * #^^^ prints ^C, prints prompt, repeats
2822 * trap 'echo I' int; (while kill -INT $$; do sleep 1; done) &
2823 * #^^^ prints ^C, prints "I", prints prompt, repeats
2824 * trap 'echo T' term; (while kill $$; do sleep 1; done) &
2825 * #^^^ prints "T", prints prompt, repeats
2826 * #(bash 5.0.17 exits after first "T", looks like a bug)
2827 */
2828 r = read_line_input(G.line_input_state, prompt_str,
Francis Laniel110b7692023-12-22 22:02:27 +01002829 G.user_input_buf, CONFIG_FEATURE_EDITING_MAX_LEN-1
Francis Laniele7ca3a32023-12-22 22:02:42 +01002830 );
2831 /* read_line_input intercepts ^C, "convert" it to SIGINT */
2832 if (r == 0)
2833 raise(SIGINT);
2834 }
2835 /* bash prints ^C (before running a trap, if any)
2836 * both on keyboard ^C and on real SIGINT (non-kbd generated).
2837 */
2838 if (sigismember(&G.pending_set, SIGINT)) {
2839 write(STDOUT_FILENO, "^C\n", 3);
2840 G.last_exitcode = 128 | SIGINT;
Francis Laniel110b7692023-12-22 22:02:27 +01002841 }
2842 check_and_run_traps();
Francis Laniele7ca3a32023-12-22 22:02:42 +01002843 if (r == 0) /* keyboard ^C? */
2844 continue; /* go back, read another input line */
2845 if (r > 0) /* normal input? (no ^C, no ^D, no signals) */
Francis Laniel110b7692023-12-22 22:02:27 +01002846 break;
Francis Laniele7ca3a32023-12-22 22:02:42 +01002847 if (!bb_got_signal) {
2848 /* r < 0: ^D/EOF/error detected (but not signal) */
2849 /* ^D on interactive input goes to next line before exiting: */
2850 write(STDOUT_FILENO, "\n", 1);
2851 i->p = NULL;
2852 i->peek_buf[0] = r = EOF;
2853 return r;
2854 }
2855 /* it was a signal: go back, read another input line */
Francis Laniel110b7692023-12-22 22:02:27 +01002856 }
2857 i->p = G.user_input_buf;
2858 return (unsigned char)*i->p++;
2859# else
2860 /* In !EDITING case, this function gets called for every char.
2861 * Buffering happens deeper in the call chain, in hfgetc(i->file).
2862 */
2863 int r;
2864
2865 for (;;) {
2866 G.flag_SIGINT = 0;
2867 if (i->last_char == '\0' || i->last_char == '\n') {
2868 const char *prompt_str = setup_prompt_string();
2869 /* Why check_and_run_traps here? Try this interactively:
2870 * $ trap 'echo INT' INT; (sleep 2; kill -INT $$) &
2871 * $ <[enter], repeatedly...>
2872 * Without check_and_run_traps, handler never runs.
2873 */
2874 check_and_run_traps();
2875 fputs_stdout(prompt_str);
2876 fflush_all();
2877 }
2878 r = hfgetc(i->file);
2879 /* In !ENABLE_FEATURE_EDITING we don't use read_line_input,
2880 * no ^C masking happens during fgetc, no special code for ^C:
2881 * it generates SIGINT as usual.
2882 */
2883 check_and_run_traps();
2884 if (r != '\0' && !G.flag_SIGINT)
2885 break;
2886 if (G.flag_SIGINT) {
2887 /* ^C or SIGINT: repeat */
2888 /* bash prints ^C even on real SIGINT (non-kbd generated) */
2889 /* kernel prints "^C" itself, just print newline: */
2890 write(STDOUT_FILENO, "\n", 1);
2891 G.last_exitcode = 128 | SIGINT;
2892 }
2893 }
2894 return r;
2895# endif
Francis Laniel36836fc2023-12-22 22:02:28 +01002896#else /* __U_BOOT__ */
2897 int n;
2898 int promptme;
2899 static char the_command[CONFIG_SYS_CBSIZE + 1];
2900
2901 bootretry_reset_cmd_timeout();
2902 promptme = 1;
2903 n = u_boot_cli_readline(i);
2904
2905# ifdef CONFIG_BOOT_RETRY_TIME
2906 if (n == -2) {
2907 puts("\nTimeout waiting for command\n");
2908# ifdef CONFIG_RESET_TO_RETRY
2909 do_reset(NULL, 0, 0, NULL);
Caleb Connolly9c09c092025-03-31 14:23:19 +02002910# elif IS_ENABLED(CONFIG_RETRY_BOOTCMD)
2911 strcpy(console_buffer, "run bootcmd\n");
2912# else
2913# error "This only works with CONFIG_RESET_TO_RETRY or CONFIG_BOOT_RETRY_COMMAND enabled"
Francis Laniel36836fc2023-12-22 22:02:28 +01002914# endif
2915 }
2916# endif
2917 if (n == -1 ) {
2918 G.flag_repeat = 0;
2919 promptme = 0;
2920 }
2921 n = strlen(console_buffer);
2922 console_buffer[n] = '\n';
2923 console_buffer[n+1]= '\0';
2924 if (had_ctrlc())
2925 G.flag_repeat = 0;
2926 clear_ctrlc();
2927 G.do_repeat = 0;
2928#ifndef __U_BOOT__
2929 if (G.promptmode == 1) {
2930#else /* __U_BOOT__ */
2931 if (!G.promptmode) {
2932#endif /* __U_BOOT__ */
2933 if (console_buffer[0] == '\n'&& G.flag_repeat == 0) {
2934 strcpy(the_command, console_buffer);
2935 }
2936 else {
2937 if (console_buffer[0] != '\n') {
2938 strcpy(the_command, console_buffer);
2939 G.flag_repeat = 1;
2940 }
2941 else {
2942 G.do_repeat = 1;
2943 }
2944 }
2945 i->p = the_command;
2946 }
2947 else {
2948 if (console_buffer[0] != '\n') {
2949 if (strlen(the_command) + strlen(console_buffer)
2950 < CONFIG_SYS_CBSIZE) {
2951 n = strlen(the_command);
2952#ifdef __U_BOOT__
2953 /*
2954 * To avoid writing to bad places, we check if
2955 * n is greater than 0.
2956 * This bug was found by Harald Seiler.
2957 */
2958 if (n > 0)
2959 the_command[n-1] = ' ';
2960 strcpy(&the_command[n], console_buffer);
2961#else /* !__U_BOOT__ */
2962 the_command[n-1] = ' ';
2963 strcpy(&the_command[n], console_buffer);
2964#endif /* !__U_BOOT__ */
2965 }
2966 else {
2967 the_command[0] = '\n';
2968 the_command[1] = '\0';
2969 G.flag_repeat = 0;
2970 }
2971 }
2972 if (promptme == 0) {
2973 the_command[0] = '\n';
2974 the_command[1] = '\0';
2975 }
2976 i->p = console_buffer;
2977 }
2978#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01002979}
2980/* This is the magic location that prints prompts
2981 * and gets data back from the user */
2982static int fgetc_interactive(struct in_str *i)
2983{
2984 int ch;
Francis Laniel36836fc2023-12-22 22:02:28 +01002985#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01002986 /* If it's interactive stdin, get new line. */
2987 if (G_interactive_fd && i->file == G.HFILE_stdin) {
Francis Laniel36836fc2023-12-22 22:02:28 +01002988#endif /* !__U_BOOT__ */
2989#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01002990 /* Returns first char (or EOF), the rest is in i->p[] */
2991 ch = get_user_input(i);
Francis Laniel36836fc2023-12-22 22:02:28 +01002992#else /* __U_BOOT__ */
2993 /* Avoid garbage value and make clang happy. */
2994 ch = 0;
2995 /*
2996 * get_user_input() does not return anything when used in
2997 * U-Boot.
2998 * So, we need to take the read character from i->p[].
2999 */
3000 get_user_input(i);
3001 if (i->p && *i->p) {
3002 ch = *i->p++;
3003 }
3004#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01003005 G.promptmode = 1; /* PS2 */
3006 debug_printf_prompt("%s promptmode=%d\n", __func__, G.promptmode);
Francis Laniel36836fc2023-12-22 22:02:28 +01003007#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01003008 } else {
3009 /* Not stdin: script file, sourced file, etc */
3010 do ch = hfgetc(i->file); while (ch == '\0');
3011 }
Francis Laniel36836fc2023-12-22 22:02:28 +01003012#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01003013 return ch;
3014}
3015#else /* !INTERACTIVE */
Francis Laniel36836fc2023-12-22 22:02:28 +01003016#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01003017static ALWAYS_INLINE int fgetc_interactive(struct in_str *i)
3018{
3019 int ch;
3020 do ch = hfgetc(i->file); while (ch == '\0');
3021 return ch;
3022}
Francis Laniel36836fc2023-12-22 22:02:28 +01003023#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01003024#endif /* !INTERACTIVE */
3025
3026static int i_getch(struct in_str *i)
3027{
3028 int ch;
3029
Francis Laniel36836fc2023-12-22 22:02:28 +01003030#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01003031 if (!i->file) {
3032 /* string-based in_str */
3033 ch = (unsigned char)*i->p;
3034 if (ch != '\0') {
3035 i->p++;
3036 i->last_char = ch;
Francis Laniele7ca3a32023-12-22 22:02:42 +01003037#if ENABLE_HUSH_LINENO_VAR
3038 if (ch == '\n') {
3039 G.parse_lineno++;
3040 debug_printf_parse("G.parse_lineno++ = %u\n", G.parse_lineno);
3041 }
3042#endif
Francis Laniel110b7692023-12-22 22:02:27 +01003043 return ch;
3044 }
3045 return EOF;
3046 }
3047
Francis Laniel36836fc2023-12-22 22:02:28 +01003048#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01003049 /* FILE-based in_str */
3050
3051#if ENABLE_FEATURE_EDITING
3052 /* This can be stdin, check line editing char[] buffer */
3053 if (i->p && *i->p != '\0') {
3054 ch = (unsigned char)*i->p++;
3055 goto out;
Francis Laniel26cafe12023-12-22 22:02:34 +01003056#ifndef __U_BOOT__
3057 }
3058#else /* __U_BOOT__ */
3059 /*
3060 * There are two ways for command to be called:
3061 * 1. The first one is when they are typed by the user.
3062 * 2. The second one is through run_command() (NOTE command run
3063 * internally calls run_command()).
3064 *
3065 * In the second case, we do not get input from the user, so once we
3066 * get a '\0', it means we need to stop.
3067 * NOTE G.run_command_flags is only set on run_command call stack, so
3068 * we use this to know if we come from user input or run_command().
3069 */
3070 } else if (i->p && *i->p == '\0' && G.run_command_flags){
3071 return EOF;
Francis Laniel110b7692023-12-22 22:02:27 +01003072 }
Francis Laniel26cafe12023-12-22 22:02:34 +01003073#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01003074#endif
Francis Laniel36836fc2023-12-22 22:02:28 +01003075#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01003076 /* peek_buf[] is an int array, not char. Can contain EOF. */
3077 ch = i->peek_buf[0];
3078 if (ch != 0) {
3079 int ch2 = i->peek_buf[1];
3080 i->peek_buf[0] = ch2;
3081 if (ch2 == 0) /* very likely, avoid redundant write */
3082 goto out;
3083 i->peek_buf[1] = 0;
3084 goto out;
3085 }
3086
Francis Laniel36836fc2023-12-22 22:02:28 +01003087#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01003088 ch = fgetc_interactive(i);
3089 out:
3090 debug_printf("file_get: got '%c' %d\n", ch, ch);
3091 i->last_char = ch;
3092#if ENABLE_HUSH_LINENO_VAR
3093 if (ch == '\n') {
3094 G.parse_lineno++;
3095 debug_printf_parse("G.parse_lineno++ = %u\n", G.parse_lineno);
3096 }
3097#endif
3098 return ch;
3099}
3100
3101static int i_peek(struct in_str *i)
3102{
Francis Laniel36836fc2023-12-22 22:02:28 +01003103#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01003104 int ch;
3105
3106 if (!i->file) {
3107 /* string-based in_str */
3108 /* Doesn't report EOF on NUL. None of the callers care. */
3109 return (unsigned char)*i->p;
3110 }
3111
3112 /* FILE-based in_str */
3113
3114#if ENABLE_FEATURE_EDITING && ENABLE_HUSH_INTERACTIVE
3115 /* This can be stdin, check line editing char[] buffer */
3116 if (i->p && *i->p != '\0')
3117 return (unsigned char)*i->p;
3118#endif
3119 /* peek_buf[] is an int array, not char. Can contain EOF. */
3120 ch = i->peek_buf[0];
3121 if (ch != 0)
3122 return ch;
3123
3124 /* Need to get a new char */
3125 ch = fgetc_interactive(i);
3126 debug_printf("file_peek: got '%c' %d\n", ch, ch);
3127
3128 /* Save it by either rolling back line editing buffer, or in i->peek_buf[0] */
3129#if ENABLE_FEATURE_EDITING && ENABLE_HUSH_INTERACTIVE
3130 if (i->p) {
3131 i->p -= 1;
3132 return ch;
3133 }
3134#endif
3135 i->peek_buf[0] = ch;
3136 /*i->peek_buf[1] = 0; - already is */
3137 return ch;
Francis Laniel36836fc2023-12-22 22:02:28 +01003138#else /* __U_BOOT__ */
3139 /* string-based in_str */
3140 /* Doesn't report EOF on NUL. None of the callers care. */
3141 return (unsigned char)*i->p;
3142#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01003143}
3144
3145/* Only ever called if i_peek() was called, and did not return EOF.
3146 * IOW: we know the previous peek saw an ordinary char, not EOF, not NUL,
3147 * not end-of-line. Therefore we never need to read a new editing line here.
3148 */
3149static int i_peek2(struct in_str *i)
3150{
Francis Laniel36836fc2023-12-22 22:02:28 +01003151#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01003152 int ch;
Francis Laniel36836fc2023-12-22 22:02:28 +01003153#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01003154
3155 /* There are two cases when i->p[] buffer exists.
3156 * (1) it's a string in_str.
3157 * (2) It's a file, and we have a saved line editing buffer.
3158 * In both cases, we know that i->p[0] exists and not NUL, and
3159 * the peek2 result is in i->p[1].
3160 */
3161 if (i->p)
3162 return (unsigned char)i->p[1];
3163
Francis Laniel36836fc2023-12-22 22:02:28 +01003164#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01003165 /* Now we know it is a file-based in_str. */
3166
3167 /* peek_buf[] is an int array, not char. Can contain EOF. */
3168 /* Is there 2nd char? */
3169 ch = i->peek_buf[1];
3170 if (ch == 0) {
3171 /* We did not read it yet, get it now */
3172 do ch = hfgetc(i->file); while (ch == '\0');
3173 i->peek_buf[1] = ch;
3174 }
3175
3176 debug_printf("file_peek2: got '%c' %d\n", ch, ch);
3177 return ch;
Francis Laniel36836fc2023-12-22 22:02:28 +01003178#else
3179 return 0;
3180#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01003181}
3182
3183static int i_getch_and_eat_bkslash_nl(struct in_str *input)
3184{
3185 for (;;) {
3186 int ch, ch2;
3187
3188 ch = i_getch(input);
3189 if (ch != '\\')
3190 return ch;
3191 ch2 = i_peek(input);
3192 if (ch2 != '\n')
3193 return ch;
3194 /* backslash+newline, skip it */
3195 i_getch(input);
3196 }
3197}
3198
3199/* Note: this function _eats_ \<newline> pairs, safe to use plain
3200 * i_getch() after it instead of i_getch_and_eat_bkslash_nl().
3201 */
3202static int i_peek_and_eat_bkslash_nl(struct in_str *input)
3203{
3204 for (;;) {
3205 int ch, ch2;
3206
3207 ch = i_peek(input);
3208 if (ch != '\\')
3209 return ch;
3210 ch2 = i_peek2(input);
3211 if (ch2 != '\n')
3212 return ch;
3213 /* backslash+newline, skip it */
3214 i_getch(input);
3215 i_getch(input);
3216 }
3217}
3218
Francis Laniel36836fc2023-12-22 22:02:28 +01003219#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01003220static void setup_file_in_str(struct in_str *i, HFILE *fp)
Francis Laniel36836fc2023-12-22 22:02:28 +01003221#else /* __U_BOOT__ */
3222static void setup_file_in_str(struct in_str *i)
3223#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01003224{
3225 memset(i, 0, sizeof(*i));
Francis Laniel36836fc2023-12-22 22:02:28 +01003226#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01003227 i->file = fp;
3228 /* i->p = NULL; */
Francis Laniel36836fc2023-12-22 22:02:28 +01003229#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01003230}
3231
3232static void setup_string_in_str(struct in_str *i, const char *s)
3233{
3234 memset(i, 0, sizeof(*i));
3235 /*i->file = NULL */;
3236 i->p = s;
3237}
3238
Francis Laniel110b7692023-12-22 22:02:27 +01003239/*
3240 * o_string support
3241 */
3242#define B_CHUNK (32 * sizeof(char*))
3243
3244static void o_reset_to_empty_unquoted(o_string *o)
3245{
3246 o->length = 0;
3247 o->has_quoted_part = 0;
3248 if (o->data)
3249 o->data[0] = '\0';
3250}
3251
3252static void o_free_and_set_NULL(o_string *o)
3253{
3254 free(o->data);
3255 memset(o, 0, sizeof(*o));
3256}
3257
3258static ALWAYS_INLINE void o_free(o_string *o)
3259{
3260 free(o->data);
3261}
3262
3263static void o_grow_by(o_string *o, int len)
3264{
3265 if (o->length + len > o->maxlen) {
3266 o->maxlen += (2 * len) | (B_CHUNK-1);
3267 o->data = xrealloc(o->data, 1 + o->maxlen);
3268 }
3269}
3270
3271static void o_addchr(o_string *o, int ch)
3272{
3273 debug_printf("o_addchr: '%c' o->length=%d o=%p\n", ch, o->length, o);
3274 if (o->length < o->maxlen) {
3275 /* likely. avoid o_grow_by() call */
3276 add:
3277 o->data[o->length] = ch;
3278 o->length++;
3279 o->data[o->length] = '\0';
3280 return;
3281 }
3282 o_grow_by(o, 1);
3283 goto add;
3284}
3285
3286#if 0
3287/* Valid only if we know o_string is not empty */
3288static void o_delchr(o_string *o)
3289{
3290 o->length--;
3291 o->data[o->length] = '\0';
3292}
3293#endif
3294
3295static void o_addblock(o_string *o, const char *str, int len)
3296{
3297 o_grow_by(o, len);
3298 ((char*)mempcpy(&o->data[o->length], str, len))[0] = '\0';
3299 o->length += len;
3300}
3301
3302static void o_addstr(o_string *o, const char *str)
3303{
3304 o_addblock(o, str, strlen(str));
3305}
3306
Francis Laniel36836fc2023-12-22 22:02:28 +01003307#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01003308static void o_addstr_with_NUL(o_string *o, const char *str)
3309{
3310 o_addblock(o, str, strlen(str) + 1);
3311}
Francis Laniel36836fc2023-12-22 22:02:28 +01003312#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01003313
3314#if !BB_MMU
3315static void nommu_addchr(o_string *o, int ch)
3316{
3317 if (o)
3318 o_addchr(o, ch);
3319}
3320#else
3321# define nommu_addchr(o, str) ((void)0)
3322#endif
3323
Francis Laniel36836fc2023-12-22 22:02:28 +01003324#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01003325#if ENABLE_HUSH_MODE_X
3326static void x_mode_addchr(int ch)
3327{
3328 o_addchr(&G.x_mode_buf, ch);
3329}
3330static void x_mode_addstr(const char *str)
3331{
3332 o_addstr(&G.x_mode_buf, str);
3333}
3334static void x_mode_addblock(const char *str, int len)
3335{
3336 o_addblock(&G.x_mode_buf, str, len);
3337}
3338static void x_mode_prefix(void)
3339{
3340 int n = G.x_mode_depth;
3341 do x_mode_addchr('+'); while (--n >= 0);
3342}
3343static void x_mode_flush(void)
3344{
3345 int len = G.x_mode_buf.length;
3346 if (len <= 0)
3347 return;
3348 if (G.x_mode_fd > 0) {
3349 G.x_mode_buf.data[len] = '\n';
3350 full_write(G.x_mode_fd, G.x_mode_buf.data, len + 1);
3351 }
3352 G.x_mode_buf.length = 0;
3353}
3354#endif
Francis Laniel36836fc2023-12-22 22:02:28 +01003355#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01003356
3357/*
3358 * HUSH_BRACE_EXPANSION code needs corresponding quoting on variable expansion side.
3359 * Currently, "v='{q,w}'; echo $v" erroneously expands braces in $v.
3360 * Apparently, on unquoted $v bash still does globbing
3361 * ("v='*.txt'; echo $v" prints all .txt files),
3362 * but NOT brace expansion! Thus, there should be TWO independent
3363 * quoting mechanisms on $v expansion side: one protects
3364 * $v from brace expansion, and other additionally protects "$v" against globbing.
3365 * We have only second one.
3366 */
3367
3368#if ENABLE_HUSH_BRACE_EXPANSION
3369# define MAYBE_BRACES "{}"
3370#else
3371# define MAYBE_BRACES ""
3372#endif
3373
3374/* My analysis of quoting semantics tells me that state information
3375 * is associated with a destination, not a source.
3376 */
3377static void o_addqchr(o_string *o, int ch)
3378{
3379 int sz = 1;
3380 /* '-' is included because of this case:
3381 * >filename0 >filename1 >filename9; v='-'; echo filename[0"$v"9]
3382 */
3383 char *found = strchr("*?[-\\" MAYBE_BRACES, ch);
3384 if (found)
3385 sz++;
3386 o_grow_by(o, sz);
3387 if (found) {
3388 o->data[o->length] = '\\';
3389 o->length++;
3390 }
3391 o->data[o->length] = ch;
3392 o->length++;
3393 o->data[o->length] = '\0';
3394}
3395
3396static void o_addQchr(o_string *o, int ch)
3397{
3398 int sz = 1;
3399 if ((o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)
3400 && strchr("*?[-\\" MAYBE_BRACES, ch)
3401 ) {
3402 sz++;
3403 o->data[o->length] = '\\';
3404 o->length++;
3405 }
3406 o_grow_by(o, sz);
3407 o->data[o->length] = ch;
3408 o->length++;
3409 o->data[o->length] = '\0';
3410}
3411
3412static void o_addqblock(o_string *o, const char *str, int len)
3413{
3414 while (len) {
3415 char ch;
3416 int sz;
3417 int ordinary_cnt = strcspn(str, "*?[-\\" MAYBE_BRACES);
3418 if (ordinary_cnt > len) /* paranoia */
3419 ordinary_cnt = len;
3420 o_addblock(o, str, ordinary_cnt);
3421 if (ordinary_cnt == len)
3422 return; /* NUL is already added by o_addblock */
3423 str += ordinary_cnt;
3424 len -= ordinary_cnt + 1; /* we are processing + 1 char below */
3425
3426 ch = *str++;
3427 sz = 1;
3428 if (ch) { /* it is necessarily one of "*?[-\\" MAYBE_BRACES */
3429 sz++;
3430 o->data[o->length] = '\\';
3431 o->length++;
3432 }
3433 o_grow_by(o, sz);
3434 o->data[o->length] = ch;
3435 o->length++;
3436 }
3437 o->data[o->length] = '\0';
3438}
3439
3440static void o_addQblock(o_string *o, const char *str, int len)
3441{
3442 if (!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS)) {
3443 o_addblock(o, str, len);
3444 return;
3445 }
3446 o_addqblock(o, str, len);
3447}
3448
3449static void o_addQstr(o_string *o, const char *str)
3450{
3451 o_addQblock(o, str, strlen(str));
3452}
3453
3454/* A special kind of o_string for $VAR and `cmd` expansion.
3455 * It contains char* list[] at the beginning, which is grown in 16 element
3456 * increments. Actual string data starts at the next multiple of 16 * (char*).
3457 * list[i] contains an INDEX (int!) into this string data.
3458 * It means that if list[] needs to grow, data needs to be moved higher up
3459 * but list[i]'s need not be modified.
3460 * NB: remembering how many list[i]'s you have there is crucial.
3461 * o_finalize_list() operation post-processes this structure - calculates
3462 * and stores actual char* ptrs in list[]. Oh, it NULL terminates it as well.
3463 */
3464#if DEBUG_EXPAND || DEBUG_GLOB
3465static void debug_print_list(const char *prefix, o_string *o, int n)
3466{
3467 char **list = (char**)o->data;
3468 int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]);
3469 int i = 0;
3470
3471 indent();
3472 fdprintf(2, "%s: list:%p n:%d string_start:%d length:%d maxlen:%d glob:%d quoted:%d escape:%d\n",
3473 prefix, list, n, string_start, o->length, o->maxlen,
3474 !!(o->o_expflags & EXP_FLAG_GLOB),
3475 o->has_quoted_part,
3476 !!(o->o_expflags & EXP_FLAG_ESC_GLOB_CHARS));
3477 while (i < n) {
3478 indent();
3479 fdprintf(2, " list[%d]=%d '%s' %p\n", i, (int)(uintptr_t)list[i],
3480 o->data + (int)(uintptr_t)list[i] + string_start,
3481 o->data + (int)(uintptr_t)list[i] + string_start);
3482 i++;
3483 }
3484 if (n) {
3485 const char *p = o->data + (int)(uintptr_t)list[n - 1] + string_start;
3486 indent();
Francis Laniel36836fc2023-12-22 22:02:28 +01003487#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01003488 fdprintf(2, " total_sz:%ld\n", (long)((p + strlen(p) + 1) - o->data));
Francis Laniel36836fc2023-12-22 22:02:28 +01003489#else /* __U_BOOT__ */
3490 printf(" total_sz:%ld\n", (long)((p + strlen(p) + 1) - o->data));
3491#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01003492 }
3493}
3494#else
3495# define debug_print_list(prefix, o, n) ((void)0)
3496#endif
3497
3498/* n = o_save_ptr_helper(str, n) "starts new string" by storing an index value
3499 * in list[n] so that it points past last stored byte so far.
3500 * It returns n+1. */
3501static int o_save_ptr_helper(o_string *o, int n)
3502{
3503 char **list = (char**)o->data;
3504 int string_start;
3505 int string_len;
3506
3507 if (!o->has_empty_slot) {
3508 string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]);
3509 string_len = o->length - string_start;
3510 if (!(n & 0xf)) { /* 0, 0x10, 0x20...? */
3511 debug_printf_list("list[%d]=%d string_start=%d (growing)\n", n, string_len, string_start);
3512 /* list[n] points to string_start, make space for 16 more pointers */
3513 o->maxlen += 0x10 * sizeof(list[0]);
3514 o->data = xrealloc(o->data, o->maxlen + 1);
3515 list = (char**)o->data;
3516 memmove(list + n + 0x10, list + n, string_len);
3517 /*
3518 * expand_on_ifs() has a "previous argv[] ends in IFS?"
3519 * check. (grep for -prev-ifs-check-).
3520 * Ensure that argv[-1][last] is not garbage
3521 * but zero bytes, to save index check there.
3522 */
3523 list[n + 0x10 - 1] = 0;
3524 o->length += 0x10 * sizeof(list[0]);
3525 } else {
3526 debug_printf_list("list[%d]=%d string_start=%d\n",
3527 n, string_len, string_start);
3528 }
3529 } else {
3530 /* We have empty slot at list[n], reuse without growth */
3531 string_start = ((n+1 + 0xf) & ~0xf) * sizeof(list[0]); /* NB: n+1! */
3532 string_len = o->length - string_start;
3533 debug_printf_list("list[%d]=%d string_start=%d (empty slot)\n",
3534 n, string_len, string_start);
3535 o->has_empty_slot = 0;
3536 }
3537 o->has_quoted_part = 0;
3538 list[n] = (char*)(uintptr_t)string_len;
3539 return n + 1;
3540}
3541
3542/* "What was our last o_save_ptr'ed position (byte offset relative o->data)?" */
3543static int o_get_last_ptr(o_string *o, int n)
3544{
3545 char **list = (char**)o->data;
3546 int string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]);
3547
3548 return ((int)(uintptr_t)list[n-1]) + string_start;
3549}
3550
3551/*
3552 * Globbing routines.
3553 *
3554 * Most words in commands need to be globbed, even ones which are
3555 * (single or double) quoted. This stems from the possiblity of
3556 * constructs like "abc"* and 'abc'* - these should be globbed.
3557 * Having a different code path for fully-quoted strings ("abc",
3558 * 'abc') would only help performance-wise, but we still need
3559 * code for partially-quoted strings.
3560 *
3561 * Unfortunately, if we want to match bash and ash behavior in all cases,
3562 * the logic can't be "shell-syntax argument is first transformed
3563 * to a string, then globbed, and if globbing does not match anything,
3564 * it is used verbatim". Here are two examples where it fails:
3565 *
3566 * echo 'b\*'?
3567 *
3568 * The globbing can't be avoided (because of '?' at the end).
3569 * The glob pattern is: b\\\*? - IOW, both \ and * are literals
3570 * and are glob-escaped. If this does not match, bash/ash print b\*?
3571 * - IOW: they "unbackslash" the glob pattern.
3572 * Now, look at this:
3573 *
3574 * v='\\\*'; echo b$v?
3575 *
3576 * The glob pattern is the same here: b\\\*? - the unquoted $v expansion
3577 * should be used as glob pattern with no changes. However, if glob
3578 * does not match, bash/ash print b\\\*? - NOT THE SAME as first example!
3579 *
3580 * ash implements this by having an encoded representation of the word
3581 * to glob, which IS NOT THE SAME as the glob pattern - it has more data.
3582 * Glob pattern is derived from it. If glob fails, the decision what result
3583 * should be is made using that encoded representation. Not glob pattern.
3584 */
3585
3586#if ENABLE_HUSH_BRACE_EXPANSION
3587/* There in a GNU extension, GLOB_BRACE, but it is not usable:
3588 * first, it processes even {a} (no commas), second,
3589 * I didn't manage to make it return strings when they don't match
3590 * existing files. Need to re-implement it.
3591 */
3592
3593/* Helper */
3594static int glob_needed(const char *s)
3595{
3596 while (*s) {
3597 if (*s == '\\') {
3598 if (!s[1])
3599 return 0;
3600 s += 2;
3601 continue;
3602 }
3603 if (*s == '*' || *s == '[' || *s == '?' || *s == '{')
3604 return 1;
3605 s++;
3606 }
3607 return 0;
3608}
3609/* Return pointer to next closing brace or to comma */
3610static const char *next_brace_sub(const char *cp)
3611{
3612 unsigned depth = 0;
3613 cp++;
3614 while (*cp != '\0') {
3615 if (*cp == '\\') {
3616 if (*++cp == '\0')
3617 break;
3618 cp++;
3619 continue;
3620 }
3621 if ((*cp == '}' && depth-- == 0) || (*cp == ',' && depth == 0))
3622 break;
3623 if (*cp++ == '{')
3624 depth++;
3625 }
3626
3627 return *cp != '\0' ? cp : NULL;
3628}
3629/* Recursive brace globber. Note: may garble pattern[]. */
3630static int glob_brace(char *pattern, o_string *o, int n)
3631{
3632 char *new_pattern_buf;
3633 const char *begin;
3634 const char *next;
3635 const char *rest;
3636 const char *p;
3637 size_t rest_len;
3638
3639 debug_printf_glob("glob_brace('%s')\n", pattern);
3640
3641 begin = pattern;
3642 while (1) {
3643 if (*begin == '\0')
3644 goto simple_glob;
3645 if (*begin == '{') {
3646 /* Find the first sub-pattern and at the same time
3647 * find the rest after the closing brace */
3648 next = next_brace_sub(begin);
3649 if (next == NULL) {
3650 /* An illegal expression */
3651 goto simple_glob;
3652 }
3653 if (*next == '}') {
3654 /* "{abc}" with no commas - illegal
3655 * brace expr, disregard and skip it */
3656 begin = next + 1;
3657 continue;
3658 }
3659 break;
3660 }
3661 if (*begin == '\\' && begin[1] != '\0')
3662 begin++;
3663 begin++;
3664 }
3665 debug_printf_glob("begin:%s\n", begin);
3666 debug_printf_glob("next:%s\n", next);
3667
3668 /* Now find the end of the whole brace expression */
3669 rest = next;
3670 while (*rest != '}') {
3671 rest = next_brace_sub(rest);
3672 if (rest == NULL) {
3673 /* An illegal expression */
3674 goto simple_glob;
3675 }
3676 debug_printf_glob("rest:%s\n", rest);
3677 }
3678 rest_len = strlen(++rest) + 1;
3679
3680 /* We are sure the brace expression is well-formed */
3681
3682 /* Allocate working buffer large enough for our work */
3683 new_pattern_buf = xmalloc(strlen(pattern));
3684
3685 /* We have a brace expression. BEGIN points to the opening {,
3686 * NEXT points past the terminator of the first element, and REST
3687 * points past the final }. We will accumulate result names from
3688 * recursive runs for each brace alternative in the buffer using
Francis Laniele7ca3a32023-12-22 22:02:42 +01003689 * GLOB_APPEND. */
Francis Laniel110b7692023-12-22 22:02:27 +01003690
3691 p = begin + 1;
3692 while (1) {
3693 /* Construct the new glob expression */
3694 memcpy(
3695 mempcpy(
3696 mempcpy(new_pattern_buf,
3697 /* We know the prefix for all sub-patterns */
3698 pattern, begin - pattern),
3699 p, next - p),
3700 rest, rest_len);
3701
3702 /* Note: glob_brace() may garble new_pattern_buf[].
3703 * That's why we re-copy prefix every time (1st memcpy above).
3704 */
3705 n = glob_brace(new_pattern_buf, o, n);
3706 if (*next == '}') {
3707 /* We saw the last entry */
3708 break;
3709 }
3710 p = next + 1;
3711 next = next_brace_sub(next);
3712 }
3713 free(new_pattern_buf);
3714 return n;
3715
3716 simple_glob:
3717 {
3718 int gr;
3719 glob_t globdata;
3720
3721 memset(&globdata, 0, sizeof(globdata));
3722 gr = glob(pattern, 0, NULL, &globdata);
3723 debug_printf_glob("glob('%s'):%d\n", pattern, gr);
3724 if (gr != 0) {
3725 if (gr == GLOB_NOMATCH) {
3726 globfree(&globdata);
3727 /* NB: garbles parameter */
3728 unbackslash(pattern);
3729 o_addstr_with_NUL(o, pattern);
3730 debug_printf_glob("glob pattern '%s' is literal\n", pattern);
3731 return o_save_ptr_helper(o, n);
3732 }
3733 if (gr == GLOB_NOSPACE)
3734 bb_die_memory_exhausted();
3735 /* GLOB_ABORTED? Only happens with GLOB_ERR flag,
3736 * but we didn't specify it. Paranoia again. */
3737 bb_error_msg_and_die("glob error %d on '%s'", gr, pattern);
3738 }
3739 if (globdata.gl_pathv && globdata.gl_pathv[0]) {
3740 char **argv = globdata.gl_pathv;
3741 while (1) {
3742 o_addstr_with_NUL(o, *argv);
3743 n = o_save_ptr_helper(o, n);
3744 argv++;
3745 if (!*argv)
3746 break;
3747 }
3748 }
3749 globfree(&globdata);
3750 }
3751 return n;
3752}
3753/* Performs globbing on last list[],
3754 * saving each result as a new list[].
3755 */
3756static int perform_glob(o_string *o, int n)
3757{
3758 char *pattern, *copy;
3759
3760 debug_printf_glob("start perform_glob: n:%d o->data:%p\n", n, o->data);
3761 if (!o->data)
3762 return o_save_ptr_helper(o, n);
3763 pattern = o->data + o_get_last_ptr(o, n);
3764 debug_printf_glob("glob pattern '%s'\n", pattern);
3765 if (!glob_needed(pattern)) {
3766 /* unbackslash last string in o in place, fix length */
3767 o->length = unbackslash(pattern) - o->data;
3768 debug_printf_glob("glob pattern '%s' is literal\n", pattern);
3769 return o_save_ptr_helper(o, n);
3770 }
3771
3772 copy = xstrdup(pattern);
3773 /* "forget" pattern in o */
3774 o->length = pattern - o->data;
3775 n = glob_brace(copy, o, n);
3776 free(copy);
3777 if (DEBUG_GLOB)
3778 debug_print_list("perform_glob returning", o, n);
3779 return n;
3780}
3781
3782#else /* !HUSH_BRACE_EXPANSION */
3783
3784/* Helper */
3785static int glob_needed(const char *s)
3786{
3787 while (*s) {
3788 if (*s == '\\') {
3789 if (!s[1])
3790 return 0;
3791 s += 2;
3792 continue;
3793 }
3794 if (*s == '*' || *s == '[' || *s == '?')
3795 return 1;
3796 s++;
3797 }
3798 return 0;
3799}
3800/* Performs globbing on last list[],
3801 * saving each result as a new list[].
3802 */
3803static int perform_glob(o_string *o, int n)
3804{
Francis Lanielbfc406a2023-12-22 22:02:33 +01003805#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01003806 glob_t globdata;
3807 int gr;
Francis Lanielbfc406a2023-12-22 22:02:33 +01003808#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01003809 char *pattern;
3810
3811 debug_printf_glob("start perform_glob: n:%d o->data:%p\n", n, o->data);
3812 if (!o->data)
3813 return o_save_ptr_helper(o, n);
3814 pattern = o->data + o_get_last_ptr(o, n);
3815 debug_printf_glob("glob pattern '%s'\n", pattern);
3816 if (!glob_needed(pattern)) {
Francis Lanielbfc406a2023-12-22 22:02:33 +01003817#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01003818 literal:
Francis Lanielbfc406a2023-12-22 22:02:33 +01003819#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01003820 /* unbackslash last string in o in place, fix length */
3821 o->length = unbackslash(pattern) - o->data;
3822 debug_printf_glob("glob pattern '%s' is literal\n", pattern);
3823 return o_save_ptr_helper(o, n);
3824 }
3825
Francis Lanielbfc406a2023-12-22 22:02:33 +01003826#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01003827 memset(&globdata, 0, sizeof(globdata));
3828 /* Can't use GLOB_NOCHECK: it does not unescape the string.
3829 * If we glob "*.\*" and don't find anything, we need
3830 * to fall back to using literal "*.*", but GLOB_NOCHECK
3831 * will return "*.\*"!
3832 */
3833 gr = glob(pattern, 0, NULL, &globdata);
3834 debug_printf_glob("glob('%s'):%d\n", pattern, gr);
3835 if (gr != 0) {
3836 if (gr == GLOB_NOMATCH) {
3837 globfree(&globdata);
3838 goto literal;
3839 }
3840 if (gr == GLOB_NOSPACE)
3841 bb_die_memory_exhausted();
3842 /* GLOB_ABORTED? Only happens with GLOB_ERR flag,
3843 * but we didn't specify it. Paranoia again. */
3844 bb_error_msg_and_die("glob error %d on '%s'", gr, pattern);
3845 }
3846 if (globdata.gl_pathv && globdata.gl_pathv[0]) {
3847 char **argv = globdata.gl_pathv;
3848 /* "forget" pattern in o */
3849 o->length = pattern - o->data;
3850 while (1) {
3851 o_addstr_with_NUL(o, *argv);
3852 n = o_save_ptr_helper(o, n);
3853 argv++;
3854 if (!*argv)
3855 break;
3856 }
3857 }
3858 globfree(&globdata);
3859 if (DEBUG_GLOB)
3860 debug_print_list("perform_glob returning", o, n);
3861 return n;
Francis Lanielbfc406a2023-12-22 22:02:33 +01003862#else /* __U_BOOT__ */
3863 /*
3864 * NOTE We only use perform glob to call unbackslash to remove backslash
3865 * from string once expanded.
3866 * So, it seems OK to return this if no previous return was done.
3867 */
3868 return o_save_ptr_helper(o, n);
3869#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01003870}
3871
3872#endif /* !HUSH_BRACE_EXPANSION */
3873
3874/* If o->o_expflags & EXP_FLAG_GLOB, glob the string so far remembered.
3875 * Otherwise, just finish current list[] and start new */
3876static int o_save_ptr(o_string *o, int n)
3877{
3878 if (o->o_expflags & EXP_FLAG_GLOB) {
3879 /* If o->has_empty_slot, list[n] was already globbed
3880 * (if it was requested back then when it was filled)
3881 * so don't do that again! */
3882 if (!o->has_empty_slot)
3883 return perform_glob(o, n); /* o_save_ptr_helper is inside */
3884 }
3885 return o_save_ptr_helper(o, n);
3886}
3887
3888/* "Please convert list[n] to real char* ptrs, and NULL terminate it." */
3889static char **o_finalize_list(o_string *o, int n)
3890{
3891 char **list;
3892 int string_start;
3893
3894 if (DEBUG_EXPAND)
3895 debug_print_list("finalized", o, n);
3896 debug_printf_expand("finalized n:%d\n", n);
3897 list = (char**)o->data;
3898 string_start = ((n + 0xf) & ~0xf) * sizeof(list[0]);
3899 list[--n] = NULL;
3900 while (n) {
3901 n--;
3902 list[n] = o->data + (int)(uintptr_t)list[n] + string_start;
3903 }
3904 return list;
3905}
3906
3907static void free_pipe_list(struct pipe *pi);
3908
3909/* Returns pi->next - next pipe in the list */
3910static struct pipe *free_pipe(struct pipe *pi)
3911{
3912 struct pipe *next;
3913 int i;
3914
Francis Laniel36836fc2023-12-22 22:02:28 +01003915#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01003916 debug_printf_clean("free_pipe (pid %d)\n", getpid());
Francis Laniel36836fc2023-12-22 22:02:28 +01003917#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01003918 for (i = 0; i < pi->num_cmds; i++) {
3919 struct command *command;
Francis Laniel36836fc2023-12-22 22:02:28 +01003920#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01003921 struct redir_struct *r, *rnext;
Francis Laniel36836fc2023-12-22 22:02:28 +01003922#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01003923
3924 command = &pi->cmds[i];
3925 debug_printf_clean(" command %d:\n", i);
3926 if (command->argv) {
3927 if (DEBUG_CLEAN) {
3928 int a;
3929 char **p;
3930 for (a = 0, p = command->argv; *p; a++, p++) {
3931 debug_printf_clean(" argv[%d] = %s\n", a, *p);
3932 }
3933 }
3934 free_strings(command->argv);
3935 //command->argv = NULL;
3936 }
3937 /* not "else if": on syntax error, we may have both! */
3938 if (command->group) {
3939 debug_printf_clean(" begin group (cmd_type:%d)\n",
3940 command->cmd_type);
3941 free_pipe_list(command->group);
3942 debug_printf_clean(" end group\n");
3943 //command->group = NULL;
3944 }
3945 /* else is crucial here.
3946 * If group != NULL, child_func is meaningless */
3947#if ENABLE_HUSH_FUNCTIONS
3948 else if (command->child_func) {
3949 debug_printf_exec("cmd %p releases child func at %p\n", command, command->child_func);
3950 command->child_func->parent_cmd = NULL;
3951 }
3952#endif
3953#if !BB_MMU
3954 free(command->group_as_string);
3955 //command->group_as_string = NULL;
3956#endif
Francis Laniel36836fc2023-12-22 22:02:28 +01003957#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01003958 for (r = command->redirects; r; r = rnext) {
3959 debug_printf_clean(" redirect %d%s",
3960 r->rd_fd, redir_table[r->rd_type].descrip);
3961 /* guard against the case >$FOO, where foo is unset or blank */
3962 if (r->rd_filename) {
3963 debug_printf_clean(" fname:'%s'\n", r->rd_filename);
3964 free(r->rd_filename);
3965 //r->rd_filename = NULL;
3966 }
3967 debug_printf_clean(" rd_dup:%d\n", r->rd_dup);
3968 rnext = r->next;
3969 free(r);
3970 }
3971 //command->redirects = NULL;
Francis Laniel36836fc2023-12-22 22:02:28 +01003972#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01003973 }
3974 free(pi->cmds); /* children are an array, they get freed all at once */
3975 //pi->cmds = NULL;
Francis Laniel36836fc2023-12-22 22:02:28 +01003976#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01003977#if ENABLE_HUSH_JOB
3978 free(pi->cmdtext);
3979 //pi->cmdtext = NULL;
3980#endif
Francis Laniel36836fc2023-12-22 22:02:28 +01003981#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01003982
3983 next = pi->next;
3984 free(pi);
3985 return next;
3986}
3987
3988static void free_pipe_list(struct pipe *pi)
3989{
3990 while (pi) {
3991#if HAS_KEYWORDS
3992 debug_printf_clean("pipe reserved word %d\n", pi->res_word);
3993#endif
3994 debug_printf_clean("pipe followup code %d\n", pi->followup);
3995 pi = free_pipe(pi);
3996 }
3997}
3998
Francis Laniel110b7692023-12-22 22:02:27 +01003999/*** Parsing routines ***/
4000
4001#ifndef debug_print_tree
4002static void debug_print_tree(struct pipe *pi, int lvl)
4003{
Francis Laniele7ca3a32023-12-22 22:02:42 +01004004 static const char *const PIPE[] ALIGN_PTR = {
Francis Laniel110b7692023-12-22 22:02:27 +01004005 [PIPE_SEQ] = "SEQ",
4006 [PIPE_AND] = "AND",
4007 [PIPE_OR ] = "OR" ,
4008 [PIPE_BG ] = "BG" ,
4009 };
4010 static const char *RES[] = {
4011 [RES_NONE ] = "NONE" ,
4012# if ENABLE_HUSH_IF
4013 [RES_IF ] = "IF" ,
4014 [RES_THEN ] = "THEN" ,
4015 [RES_ELIF ] = "ELIF" ,
4016 [RES_ELSE ] = "ELSE" ,
4017 [RES_FI ] = "FI" ,
4018# endif
4019# if ENABLE_HUSH_LOOPS
4020 [RES_FOR ] = "FOR" ,
4021 [RES_WHILE] = "WHILE",
4022 [RES_UNTIL] = "UNTIL",
4023 [RES_DO ] = "DO" ,
4024 [RES_DONE ] = "DONE" ,
4025# endif
4026# if ENABLE_HUSH_LOOPS || ENABLE_HUSH_CASE
4027 [RES_IN ] = "IN" ,
4028# endif
4029# if ENABLE_HUSH_CASE
4030 [RES_CASE ] = "CASE" ,
4031 [RES_CASE_IN ] = "CASE_IN" ,
4032 [RES_MATCH] = "MATCH",
4033 [RES_CASE_BODY] = "CASE_BODY",
4034 [RES_ESAC ] = "ESAC" ,
4035# endif
4036 [RES_XXXX ] = "XXXX" ,
4037 [RES_SNTX ] = "SNTX" ,
4038 };
Francis Laniele7ca3a32023-12-22 22:02:42 +01004039 static const char *const CMDTYPE[] ALIGN_PTR = {
Francis Laniel110b7692023-12-22 22:02:27 +01004040 "{}",
4041 "()",
4042 "[noglob]",
4043# if ENABLE_HUSH_FUNCTIONS
4044 "func()",
4045# endif
4046 };
4047
4048 int pin, prn;
4049
4050 pin = 0;
4051 while (pi) {
4052 fdprintf(2, "%*spipe %d #cmds:%d %sres_word=%s followup=%d %s\n",
4053 lvl*2, "",
4054 pin,
4055 pi->num_cmds,
4056 (IF_HAS_KEYWORDS(pi->pi_inverted ? "! " :) ""),
4057 RES[pi->res_word],
4058 pi->followup, PIPE[pi->followup]
4059 );
4060 prn = 0;
4061 while (prn < pi->num_cmds) {
4062 struct command *command = &pi->cmds[prn];
4063 char **argv = command->argv;
4064
4065 fdprintf(2, "%*s cmd %d assignment_cnt:%d",
4066 lvl*2, "", prn,
4067 command->assignment_cnt);
4068# if ENABLE_HUSH_LINENO_VAR
4069 fdprintf(2, " LINENO:%u", command->lineno);
4070# endif
4071 if (command->group) {
4072 fdprintf(2, " group %s: (argv=%p)%s%s\n",
4073 CMDTYPE[command->cmd_type],
4074 argv
4075# if !BB_MMU
4076 , " group_as_string:", command->group_as_string
4077# else
4078 , "", ""
4079# endif
4080 );
4081 debug_print_tree(command->group, lvl+1);
4082 prn++;
4083 continue;
4084 }
4085 if (argv) while (*argv) {
4086 fdprintf(2, " '%s'", *argv);
4087 argv++;
4088 }
Francis Laniel36836fc2023-12-22 22:02:28 +01004089#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01004090 if (command->redirects)
4091 fdprintf(2, " {redir}");
Francis Laniel36836fc2023-12-22 22:02:28 +01004092#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01004093 fdprintf(2, "\n");
4094 prn++;
4095 }
4096 pi = pi->next;
4097 pin++;
4098 }
4099}
4100#endif /* debug_print_tree */
4101
4102static struct pipe *new_pipe(void)
4103{
4104 struct pipe *pi;
4105 pi = xzalloc(sizeof(struct pipe));
4106 /*pi->res_word = RES_NONE; - RES_NONE is 0 anyway */
4107 return pi;
4108}
4109
4110/* Command (member of a pipe) is complete, or we start a new pipe
4111 * if ctx->command is NULL.
4112 * No errors possible here.
4113 */
4114static int done_command(struct parse_context *ctx)
4115{
4116 /* The command is really already in the pipe structure, so
4117 * advance the pipe counter and make a new, null command. */
4118 struct pipe *pi = ctx->pipe;
4119 struct command *command = ctx->command;
4120
4121#if 0 /* Instead we emit error message at run time */
4122 if (ctx->pending_redirect) {
4123 /* For example, "cmd >" (no filename to redirect to) */
4124 syntax_error("invalid redirect");
4125 ctx->pending_redirect = NULL;
4126 }
4127#endif
4128
4129 if (command) {
4130 if (IS_NULL_CMD(command)) {
4131 debug_printf_parse("done_command: skipping null cmd, num_cmds=%d\n", pi->num_cmds);
4132 goto clear_and_ret;
4133 }
4134 pi->num_cmds++;
4135 debug_printf_parse("done_command: ++num_cmds=%d\n", pi->num_cmds);
4136 //debug_print_tree(ctx->list_head, 20);
4137 } else {
4138 debug_printf_parse("done_command: initializing, num_cmds=%d\n", pi->num_cmds);
4139 }
4140
4141 /* Only real trickiness here is that the uncommitted
4142 * command structure is not counted in pi->num_cmds. */
4143 pi->cmds = xrealloc(pi->cmds, sizeof(*pi->cmds) * (pi->num_cmds+1));
4144 ctx->command = command = &pi->cmds[pi->num_cmds];
4145 clear_and_ret:
4146 memset(command, 0, sizeof(*command));
4147#if ENABLE_HUSH_LINENO_VAR
4148 command->lineno = G.parse_lineno;
4149 debug_printf_parse("command->lineno = G.parse_lineno (%u)\n", G.parse_lineno);
4150#endif
4151 return pi->num_cmds; /* used only for 0/nonzero check */
4152}
4153
4154static void done_pipe(struct parse_context *ctx, pipe_style type)
4155{
4156 int not_null;
4157
4158 debug_printf_parse("done_pipe entered, followup %d\n", type);
4159 /* Close previous command */
4160 not_null = done_command(ctx);
4161#if HAS_KEYWORDS
4162 ctx->pipe->pi_inverted = ctx->ctx_inverted;
4163 ctx->ctx_inverted = 0;
4164 ctx->pipe->res_word = ctx->ctx_res_w;
4165#endif
4166 if (type == PIPE_BG && ctx->list_head != ctx->pipe) {
4167 /* Necessary since && and || have precedence over &:
4168 * "cmd1 && cmd2 &" must spawn both cmds, not only cmd2,
4169 * in a backgrounded subshell.
4170 */
4171 struct pipe *pi;
4172 struct command *command;
4173
4174 /* Is this actually this construct, all pipes end with && or ||? */
4175 pi = ctx->list_head;
4176 while (pi != ctx->pipe) {
4177 if (pi->followup != PIPE_AND && pi->followup != PIPE_OR)
4178 goto no_conv;
4179 pi = pi->next;
4180 }
4181
4182 debug_printf_parse("BG with more than one pipe, converting to { p1 &&...pN; } &\n");
4183 pi->followup = PIPE_SEQ; /* close pN _not_ with "&"! */
4184 pi = xzalloc(sizeof(*pi));
4185 pi->followup = PIPE_BG;
4186 pi->num_cmds = 1;
4187 pi->cmds = xzalloc(sizeof(pi->cmds[0]));
4188 command = &pi->cmds[0];
4189 if (CMD_NORMAL != 0) /* "if xzalloc didn't do that already" */
4190 command->cmd_type = CMD_NORMAL;
4191 command->group = ctx->list_head;
4192#if !BB_MMU
4193 command->group_as_string = xstrndup(
4194 ctx->as_string.data,
4195 ctx->as_string.length - 1 /* do not copy last char, "&" */
4196 );
4197#endif
4198 /* Replace all pipes in ctx with one newly created */
4199 ctx->list_head = ctx->pipe = pi;
4200 /* for cases like "cmd && &", do not be tricked by last command
4201 * being null - the entire {...} & is NOT null! */
4202 not_null = 1;
4203 } else {
4204 no_conv:
4205 ctx->pipe->followup = type;
4206 }
4207
4208 /* Without this check, even just <enter> on command line generates
4209 * tree of three NOPs (!). Which is harmless but annoying.
4210 * IOW: it is safe to do it unconditionally. */
4211 if (not_null
4212#if ENABLE_HUSH_IF
4213 || ctx->ctx_res_w == RES_FI
4214#endif
4215#if ENABLE_HUSH_LOOPS
4216 || ctx->ctx_res_w == RES_DONE
4217 || ctx->ctx_res_w == RES_FOR
4218 || ctx->ctx_res_w == RES_IN
4219#endif
4220#if ENABLE_HUSH_CASE
4221 || ctx->ctx_res_w == RES_ESAC
4222#endif
4223 ) {
4224 struct pipe *new_p;
4225 debug_printf_parse("done_pipe: adding new pipe: "
Francis Laniel36836fc2023-12-22 22:02:28 +01004226#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01004227 "not_null:%d ctx->ctx_res_w:%d\n",
4228 not_null, ctx->ctx_res_w);
Francis Laniel36836fc2023-12-22 22:02:28 +01004229#else /* __U_BOOT__ */
4230 "not_null:%d\n",
4231 not_null);
4232#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01004233 new_p = new_pipe();
4234 ctx->pipe->next = new_p;
4235 ctx->pipe = new_p;
4236 /* RES_THEN, RES_DO etc are "sticky" -
4237 * they remain set for pipes inside if/while.
4238 * This is used to control execution.
4239 * RES_FOR and RES_IN are NOT sticky (needed to support
4240 * cases where variable or value happens to match a keyword):
4241 */
4242#if ENABLE_HUSH_LOOPS
4243 if (ctx->ctx_res_w == RES_FOR
4244 || ctx->ctx_res_w == RES_IN)
4245 ctx->ctx_res_w = RES_NONE;
4246#endif
4247#if ENABLE_HUSH_CASE
4248 if (ctx->ctx_res_w == RES_MATCH)
4249 ctx->ctx_res_w = RES_CASE_BODY;
4250 if (ctx->ctx_res_w == RES_CASE)
4251 ctx->ctx_res_w = RES_CASE_IN;
4252#endif
4253 ctx->command = NULL; /* trick done_command below */
4254 /* Create the memory for command, roughly:
4255 * ctx->pipe->cmds = new struct command;
4256 * ctx->command = &ctx->pipe->cmds[0];
4257 */
4258 done_command(ctx);
4259 //debug_print_tree(ctx->list_head, 10);
4260 }
4261 debug_printf_parse("done_pipe return\n");
4262}
4263
4264static void initialize_context(struct parse_context *ctx)
4265{
4266 memset(ctx, 0, sizeof(*ctx));
4267 if (MAYBE_ASSIGNMENT != 0)
4268 ctx->is_assignment = MAYBE_ASSIGNMENT;
4269 ctx->pipe = ctx->list_head = new_pipe();
4270 /* Create the memory for command, roughly:
4271 * ctx->pipe->cmds = new struct command;
4272 * ctx->command = &ctx->pipe->cmds[0];
4273 */
4274 done_command(ctx);
4275}
4276
4277/* If a reserved word is found and processed, parse context is modified
4278 * and 1 is returned.
4279 */
4280#if HAS_KEYWORDS
4281struct reserved_combo {
4282 char literal[6];
4283 unsigned char res;
4284 unsigned char assignment_flag;
4285 uint32_t flag;
4286};
4287enum {
4288 FLAG_END = (1 << RES_NONE ),
4289# if ENABLE_HUSH_IF
4290 FLAG_IF = (1 << RES_IF ),
4291 FLAG_THEN = (1 << RES_THEN ),
4292 FLAG_ELIF = (1 << RES_ELIF ),
4293 FLAG_ELSE = (1 << RES_ELSE ),
4294 FLAG_FI = (1 << RES_FI ),
4295# endif
4296# if ENABLE_HUSH_LOOPS
4297 FLAG_FOR = (1 << RES_FOR ),
4298 FLAG_WHILE = (1 << RES_WHILE),
4299 FLAG_UNTIL = (1 << RES_UNTIL),
4300 FLAG_DO = (1 << RES_DO ),
4301 FLAG_DONE = (1 << RES_DONE ),
4302 FLAG_IN = (1 << RES_IN ),
4303# endif
4304# if ENABLE_HUSH_CASE
4305 FLAG_MATCH = (1 << RES_MATCH),
4306 FLAG_ESAC = (1 << RES_ESAC ),
4307# endif
4308 FLAG_START = (1 << RES_XXXX ),
4309};
4310
4311static const struct reserved_combo* match_reserved_word(o_string *word)
4312{
4313 /* Mostly a list of accepted follow-up reserved words.
4314 * FLAG_END means we are done with the sequence, and are ready
4315 * to turn the compound list into a command.
4316 * FLAG_START means the word must start a new compound list.
4317 */
4318 static const struct reserved_combo reserved_list[] ALIGN4 = {
4319# if ENABLE_HUSH_IF
4320 { "!", RES_NONE, NOT_ASSIGNMENT , 0 },
4321 { "if", RES_IF, MAYBE_ASSIGNMENT, FLAG_THEN | FLAG_START },
4322 { "then", RES_THEN, MAYBE_ASSIGNMENT, FLAG_ELIF | FLAG_ELSE | FLAG_FI },
4323 { "elif", RES_ELIF, MAYBE_ASSIGNMENT, FLAG_THEN },
4324 { "else", RES_ELSE, MAYBE_ASSIGNMENT, FLAG_FI },
4325 { "fi", RES_FI, NOT_ASSIGNMENT , FLAG_END },
4326# endif
4327# if ENABLE_HUSH_LOOPS
4328 { "for", RES_FOR, NOT_ASSIGNMENT , FLAG_IN | FLAG_DO | FLAG_START },
4329 { "while", RES_WHILE, MAYBE_ASSIGNMENT, FLAG_DO | FLAG_START },
4330 { "until", RES_UNTIL, MAYBE_ASSIGNMENT, FLAG_DO | FLAG_START },
4331 { "in", RES_IN, NOT_ASSIGNMENT , FLAG_DO },
4332 { "do", RES_DO, MAYBE_ASSIGNMENT, FLAG_DONE },
4333 { "done", RES_DONE, NOT_ASSIGNMENT , FLAG_END },
4334# endif
4335# if ENABLE_HUSH_CASE
4336 { "case", RES_CASE, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_START },
4337 { "esac", RES_ESAC, NOT_ASSIGNMENT , FLAG_END },
4338# endif
4339 };
4340 const struct reserved_combo *r;
4341
4342 for (r = reserved_list; r < reserved_list + ARRAY_SIZE(reserved_list); r++) {
4343 if (strcmp(word->data, r->literal) == 0)
4344 return r;
4345 }
4346 return NULL;
4347}
4348/* Return NULL: not a keyword, else: keyword
4349 */
4350static const struct reserved_combo* reserved_word(struct parse_context *ctx)
4351{
4352# if ENABLE_HUSH_CASE
4353 static const struct reserved_combo reserved_match = {
4354 "", RES_MATCH, NOT_ASSIGNMENT , FLAG_MATCH | FLAG_ESAC
4355 };
4356# endif
4357 const struct reserved_combo *r;
4358
4359 if (ctx->word.has_quoted_part)
4360 return 0;
4361 r = match_reserved_word(&ctx->word);
4362 if (!r)
4363 return r; /* NULL */
4364
4365 debug_printf("found reserved word %s, res %d\n", r->literal, r->res);
4366# if ENABLE_HUSH_CASE
4367 if (r->res == RES_IN && ctx->ctx_res_w == RES_CASE_IN) {
4368 /* "case word IN ..." - IN part starts first MATCH part */
4369 r = &reserved_match;
4370 } else
4371# endif
4372 if (r->flag == 0) { /* '!' */
4373 if (ctx->ctx_inverted) { /* bash doesn't accept '! ! true' */
4374 syntax_error("! ! command");
4375 ctx->ctx_res_w = RES_SNTX;
4376 }
4377 ctx->ctx_inverted = 1;
4378 return r;
4379 }
4380 if (r->flag & FLAG_START) {
4381 struct parse_context *old;
4382
4383 old = xmemdup(ctx, sizeof(*ctx));
4384 debug_printf_parse("push stack %p\n", old);
4385 initialize_context(ctx);
4386 ctx->stack = old;
4387 } else if (/*ctx->ctx_res_w == RES_NONE ||*/ !(ctx->old_flag & (1 << r->res))) {
4388 syntax_error_at(ctx->word.data);
4389 ctx->ctx_res_w = RES_SNTX;
4390 return r;
4391 } else {
4392 /* "{...} fi" is ok. "{...} if" is not
4393 * Example:
4394 * if { echo foo; } then { echo bar; } fi */
4395 if (ctx->command->group)
4396 done_pipe(ctx, PIPE_SEQ);
4397 }
4398
4399 ctx->ctx_res_w = r->res;
4400 ctx->old_flag = r->flag;
4401 ctx->is_assignment = r->assignment_flag;
4402 debug_printf_parse("ctx->is_assignment='%s'\n", assignment_flag[ctx->is_assignment]);
4403
4404 if (ctx->old_flag & FLAG_END) {
4405 struct parse_context *old;
4406
4407 done_pipe(ctx, PIPE_SEQ);
4408 debug_printf_parse("pop stack %p\n", ctx->stack);
4409 old = ctx->stack;
4410 old->command->group = ctx->list_head;
4411 old->command->cmd_type = CMD_NORMAL;
4412# if !BB_MMU
4413 /* At this point, the compound command's string is in
4414 * ctx->as_string... except for the leading keyword!
4415 * Consider this example: "echo a | if true; then echo a; fi"
4416 * ctx->as_string will contain "true; then echo a; fi",
4417 * with "if " remaining in old->as_string!
4418 */
4419 {
4420 char *str;
4421 int len = old->as_string.length;
4422 /* Concatenate halves */
4423 o_addstr(&old->as_string, ctx->as_string.data);
4424 o_free(&ctx->as_string);
4425 /* Find where leading keyword starts in first half */
4426 str = old->as_string.data + len;
4427 if (str > old->as_string.data)
4428 str--; /* skip whitespace after keyword */
4429 while (str > old->as_string.data && isalpha(str[-1]))
4430 str--;
4431 /* Ugh, we're done with this horrid hack */
4432 old->command->group_as_string = xstrdup(str);
4433 debug_printf_parse("pop, remembering as:'%s'\n",
4434 old->command->group_as_string);
4435 }
4436# endif
4437 *ctx = *old; /* physical copy */
4438 free(old);
4439 }
4440 return r;
4441}
4442#endif /* HAS_KEYWORDS */
4443
4444/* Word is complete, look at it and update parsing context.
4445 * Normal return is 0. Syntax errors return 1.
4446 * Note: on return, word is reset, but not o_free'd!
4447 */
4448static int done_word(struct parse_context *ctx)
4449{
4450 struct command *command = ctx->command;
4451
4452 debug_printf_parse("done_word entered: '%s' %p\n", ctx->word.data, command);
4453 if (ctx->word.length == 0 && !ctx->word.has_quoted_part) {
4454 debug_printf_parse("done_word return 0: true null, ignored\n");
4455 return 0;
4456 }
4457
Francis Laniel36836fc2023-12-22 22:02:28 +01004458#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01004459 if (ctx->pending_redirect) {
4460 /* We do not glob in e.g. >*.tmp case. bash seems to glob here
4461 * only if run as "bash", not "sh" */
4462 /* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
4463 * "2.7 Redirection
4464 * If the redirection operator is "<<" or "<<-", the word
4465 * that follows the redirection operator shall be
4466 * subjected to quote removal; it is unspecified whether
4467 * any of the other expansions occur. For the other
4468 * redirection operators, the word that follows the
4469 * redirection operator shall be subjected to tilde
4470 * expansion, parameter expansion, command substitution,
4471 * arithmetic expansion, and quote removal.
4472 * Pathname expansion shall not be performed
4473 * on the word by a non-interactive shell; an interactive
4474 * shell may perform it, but shall do so only when
4475 * the expansion would result in one word."
4476 */
4477//bash does not do parameter/command substitution or arithmetic expansion
4478//for _heredoc_ redirection word: these constructs look for exact eof marker
4479// as written:
4480// <<EOF$t
4481// <<EOF$((1))
4482// <<EOF`true` [this case also makes heredoc "quoted", a-la <<"EOF". Probably bash-4.3.43 bug]
4483
4484 ctx->pending_redirect->rd_filename = xstrdup(ctx->word.data);
4485 /* Cater for >\file case:
4486 * >\a creates file a; >\\a, >"\a", >"\\a" create file \a
4487 * Same with heredocs:
4488 * for <<\H delim is H; <<\\H, <<"\H", <<"\\H" - \H
4489 */
4490 if (ctx->pending_redirect->rd_type == REDIRECT_HEREDOC) {
4491 unbackslash(ctx->pending_redirect->rd_filename);
4492 /* Is it <<"HEREDOC"? */
4493 if (ctx->word.has_quoted_part) {
4494 ctx->pending_redirect->rd_dup |= HEREDOC_QUOTED;
4495 }
4496 }
4497 debug_printf_parse("word stored in rd_filename: '%s'\n", ctx->word.data);
4498 ctx->pending_redirect = NULL;
4499 } else {
Francis Laniel36836fc2023-12-22 22:02:28 +01004500#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01004501#if HAS_KEYWORDS
4502# if ENABLE_HUSH_CASE
4503 if (ctx->ctx_dsemicolon
4504 && strcmp(ctx->word.data, "esac") != 0 /* not "... pattern) cmd;; esac" */
4505 ) {
4506 /* already done when ctx_dsemicolon was set to 1: */
4507 /* ctx->ctx_res_w = RES_MATCH; */
4508 ctx->ctx_dsemicolon = 0;
4509 } else
4510# endif
4511# if defined(CMD_TEST2_SINGLEWORD_NOGLOB)
4512 if (command->cmd_type == CMD_TEST2_SINGLEWORD_NOGLOB
4513 && strcmp(ctx->word.data, "]]") == 0
4514 ) {
4515 /* allow "[[ ]] >file" etc */
4516 command->cmd_type = CMD_SINGLEWORD_NOGLOB;
4517 } else
4518# endif
4519 if (!command->argv /* if it's the first word... */
4520# if ENABLE_HUSH_LOOPS
4521 && ctx->ctx_res_w != RES_FOR /* ...not after FOR or IN */
4522 && ctx->ctx_res_w != RES_IN
4523# endif
4524# if ENABLE_HUSH_CASE
4525 && ctx->ctx_res_w != RES_CASE
4526# endif
4527 ) {
4528 const struct reserved_combo *reserved;
4529 reserved = reserved_word(ctx);
4530 debug_printf_parse("checking for reserved-ness: %d\n", !!reserved);
4531 if (reserved) {
4532# if ENABLE_HUSH_LINENO_VAR
4533/* Case:
4534 * "while ...; do
4535 * cmd ..."
4536 * If we don't close the pipe _now_, immediately after "do", lineno logic
4537 * sees "cmd" as starting at "do" - i.e., at the previous line.
4538 */
4539 if (0
4540 IF_HUSH_IF(|| reserved->res == RES_THEN)
4541 IF_HUSH_IF(|| reserved->res == RES_ELIF)
4542 IF_HUSH_IF(|| reserved->res == RES_ELSE)
4543 IF_HUSH_LOOPS(|| reserved->res == RES_DO)
4544 ) {
4545 done_pipe(ctx, PIPE_SEQ);
4546 }
4547# endif
4548 o_reset_to_empty_unquoted(&ctx->word);
4549 debug_printf_parse("done_word return %d\n",
4550 (ctx->ctx_res_w == RES_SNTX));
4551 return (ctx->ctx_res_w == RES_SNTX);
4552 }
4553# if defined(CMD_TEST2_SINGLEWORD_NOGLOB)
4554 if (strcmp(ctx->word.data, "[[") == 0) {
4555 command->cmd_type = CMD_TEST2_SINGLEWORD_NOGLOB;
4556 } else
4557# endif
4558# if defined(CMD_SINGLEWORD_NOGLOB)
4559 if (0
4560 /* In bash, local/export/readonly are special, args
4561 * are assignments and therefore expansion of them
4562 * should be "one-word" expansion:
4563 * $ export i=`echo 'a b'` # one arg: "i=a b"
4564 * compare with:
4565 * $ ls i=`echo 'a b'` # two args: "i=a" and "b"
4566 * ls: cannot access i=a: No such file or directory
4567 * ls: cannot access b: No such file or directory
4568 * Note: bash 3.2.33(1) does this only if export word
4569 * itself is not quoted:
4570 * $ export i=`echo 'aaa bbb'`; echo "$i"
4571 * aaa bbb
4572 * $ "export" i=`echo 'aaa bbb'`; echo "$i"
4573 * aaa
4574 */
4575 IF_HUSH_LOCAL( || strcmp(ctx->word.data, "local") == 0)
4576 IF_HUSH_EXPORT( || strcmp(ctx->word.data, "export") == 0)
4577 IF_HUSH_READONLY(|| strcmp(ctx->word.data, "readonly") == 0)
4578 ) {
4579 command->cmd_type = CMD_SINGLEWORD_NOGLOB;
4580 }
4581# else
4582 { /* empty block to pair "if ... else" */ }
4583# endif
4584 }
4585#endif /* HAS_KEYWORDS */
4586
4587 if (command->group) {
4588 /* "{ echo foo; } echo bar" - bad */
4589 syntax_error_at(ctx->word.data);
4590 debug_printf_parse("done_word return 1: syntax error, "
4591 "groups and arglists don't mix\n");
4592 return 1;
4593 }
4594
4595 /* If this word wasn't an assignment, next ones definitely
4596 * can't be assignments. Even if they look like ones. */
4597 if (ctx->is_assignment != DEFINITELY_ASSIGNMENT
4598 && ctx->is_assignment != WORD_IS_KEYWORD
4599 ) {
4600 ctx->is_assignment = NOT_ASSIGNMENT;
4601 } else {
4602 if (ctx->is_assignment == DEFINITELY_ASSIGNMENT) {
4603 command->assignment_cnt++;
4604 debug_printf_parse("++assignment_cnt=%d\n", command->assignment_cnt);
4605 }
4606 debug_printf_parse("ctx->is_assignment was:'%s'\n", assignment_flag[ctx->is_assignment]);
4607 ctx->is_assignment = MAYBE_ASSIGNMENT;
4608 }
4609 debug_printf_parse("ctx->is_assignment='%s'\n", assignment_flag[ctx->is_assignment]);
4610 command->argv = add_string_to_strings(command->argv, xstrdup(ctx->word.data));
Francis Laniel36836fc2023-12-22 22:02:28 +01004611#ifdef __U_BOOT__
4612 command->argc++;
4613#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01004614 debug_print_strings("word appended to argv", command->argv);
Francis Laniel36836fc2023-12-22 22:02:28 +01004615
4616#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01004617 }
Francis Laniel36836fc2023-12-22 22:02:28 +01004618#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01004619
4620#if ENABLE_HUSH_LOOPS
4621 if (ctx->ctx_res_w == RES_FOR) {
4622 if (ctx->word.has_quoted_part
4623 || endofname(command->argv[0])[0] != '\0'
4624 ) {
4625 /* bash says just "not a valid identifier" */
Francis Laniele7ca3a32023-12-22 22:02:42 +01004626 syntax_error("bad for loop variable");
Francis Laniel110b7692023-12-22 22:02:27 +01004627 return 1;
4628 }
4629 /* Force FOR to have just one word (variable name) */
4630 /* NB: basically, this makes hush see "for v in ..."
4631 * syntax as if it is "for v; in ...". FOR and IN become
4632 * two pipe structs in parse tree. */
4633 done_pipe(ctx, PIPE_SEQ);
4634 }
4635#endif
4636#if ENABLE_HUSH_CASE
4637 /* Force CASE to have just one word */
4638 if (ctx->ctx_res_w == RES_CASE) {
4639 done_pipe(ctx, PIPE_SEQ);
4640 }
4641#endif
4642
4643 o_reset_to_empty_unquoted(&ctx->word);
4644
4645 debug_printf_parse("done_word return 0\n");
4646 return 0;
4647}
4648
Francis Laniel36836fc2023-12-22 22:02:28 +01004649#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01004650/* Peek ahead in the input to find out if we have a "&n" construct,
4651 * as in "2>&1", that represents duplicating a file descriptor.
4652 * Return:
4653 * REDIRFD_CLOSE if >&- "close fd" construct is seen,
4654 * REDIRFD_SYNTAX_ERR if syntax error,
4655 * REDIRFD_TO_FILE if no & was seen,
4656 * or the number found.
4657 */
4658#if BB_MMU
4659#define parse_redir_right_fd(as_string, input) \
4660 parse_redir_right_fd(input)
4661#endif
4662static int parse_redir_right_fd(o_string *as_string, struct in_str *input)
4663{
4664 int ch, d, ok;
4665
4666 ch = i_peek(input);
4667 if (ch != '&')
4668 return REDIRFD_TO_FILE;
4669
4670 ch = i_getch(input); /* get the & */
4671 nommu_addchr(as_string, ch);
4672 ch = i_peek(input);
4673 if (ch == '-') {
4674 ch = i_getch(input);
4675 nommu_addchr(as_string, ch);
4676 return REDIRFD_CLOSE;
4677 }
4678 d = 0;
4679 ok = 0;
4680 while (ch != EOF && isdigit(ch)) {
4681 d = d*10 + (ch-'0');
4682 ok = 1;
4683 ch = i_getch(input);
4684 nommu_addchr(as_string, ch);
4685 ch = i_peek(input);
4686 }
4687 if (ok) return d;
4688
4689//TODO: this is the place to catch ">&file" bashism (redirect both fd 1 and 2)
4690
4691 bb_simple_error_msg("ambiguous redirect");
4692 return REDIRFD_SYNTAX_ERR;
4693}
4694
4695/* Return code is 0 normal, 1 if a syntax error is detected
4696 */
4697static int parse_redirect(struct parse_context *ctx,
4698 int fd,
4699 redir_type style,
4700 struct in_str *input)
4701{
4702 struct command *command = ctx->command;
4703 struct redir_struct *redir;
4704 struct redir_struct **redirp;
4705 int dup_num;
4706
4707 dup_num = REDIRFD_TO_FILE;
4708 if (style != REDIRECT_HEREDOC) {
4709 /* Check for a '>&1' type redirect */
4710 dup_num = parse_redir_right_fd(&ctx->as_string, input);
4711 if (dup_num == REDIRFD_SYNTAX_ERR)
4712 return 1;
4713 } else {
4714 int ch = i_peek_and_eat_bkslash_nl(input);
4715 dup_num = (ch == '-'); /* HEREDOC_SKIPTABS bit is 1 */
4716 if (dup_num) { /* <<-... */
4717 ch = i_getch(input);
4718 nommu_addchr(&ctx->as_string, ch);
4719 ch = i_peek(input);
4720 }
4721 }
4722
4723 if (style == REDIRECT_OVERWRITE && dup_num == REDIRFD_TO_FILE) {
4724 int ch = i_peek_and_eat_bkslash_nl(input);
4725 if (ch == '|') {
4726 /* >|FILE redirect ("clobbering" >).
4727 * Since we do not support "set -o noclobber" yet,
4728 * >| and > are the same for now. Just eat |.
4729 */
4730 ch = i_getch(input);
4731 nommu_addchr(&ctx->as_string, ch);
4732 }
4733 }
4734
4735 /* Create a new redir_struct and append it to the linked list */
4736 redirp = &command->redirects;
4737 while ((redir = *redirp) != NULL) {
4738 redirp = &(redir->next);
4739 }
4740 *redirp = redir = xzalloc(sizeof(*redir));
4741 /* redir->next = NULL; */
4742 /* redir->rd_filename = NULL; */
4743 redir->rd_type = style;
4744 redir->rd_fd = (fd == -1) ? redir_table[style].default_fd : fd;
4745
4746 debug_printf_parse("redirect type %d %s\n", redir->rd_fd,
4747 redir_table[style].descrip);
4748
4749 redir->rd_dup = dup_num;
4750 if (style != REDIRECT_HEREDOC && dup_num != REDIRFD_TO_FILE) {
4751 /* Erik had a check here that the file descriptor in question
4752 * is legit; I postpone that to "run time"
4753 * A "-" representation of "close me" shows up as a -3 here */
4754 debug_printf_parse("duplicating redirect '%d>&%d'\n",
4755 redir->rd_fd, redir->rd_dup);
4756 } else {
4757#if 0 /* Instead we emit error message at run time */
4758 if (ctx->pending_redirect) {
4759 /* For example, "cmd > <file" */
4760 syntax_error("invalid redirect");
4761 }
4762#endif
4763 /* Set ctx->pending_redirect, so we know what to do at the
4764 * end of the next parsed word. */
4765 ctx->pending_redirect = redir;
4766 }
4767 return 0;
4768}
4769
4770/* If a redirect is immediately preceded by a number, that number is
4771 * supposed to tell which file descriptor to redirect. This routine
4772 * looks for such preceding numbers. In an ideal world this routine
4773 * needs to handle all the following classes of redirects...
4774 * echo 2>foo # redirects fd 2 to file "foo", nothing passed to echo
4775 * echo 49>foo # redirects fd 49 to file "foo", nothing passed to echo
4776 * echo -2>foo # redirects fd 1 to file "foo", "-2" passed to echo
4777 * echo 49x>foo # redirects fd 1 to file "foo", "49x" passed to echo
4778 *
4779 * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html
4780 * "2.7 Redirection
4781 * ... If n is quoted, the number shall not be recognized as part of
4782 * the redirection expression. For example:
4783 * echo \2>a
4784 * writes the character 2 into file a"
4785 * We are getting it right by setting ->has_quoted_part on any \<char>
4786 *
4787 * A -1 return means no valid number was found,
4788 * the caller should use the appropriate default for this redirection.
4789 */
4790static int redirect_opt_num(o_string *o)
4791{
4792 int num;
4793
4794 if (o->data == NULL)
4795 return -1;
4796 num = bb_strtou(o->data, NULL, 10);
4797 if (errno || num < 0)
4798 return -1;
4799 o_reset_to_empty_unquoted(o);
4800 return num;
4801}
4802
4803#if BB_MMU
4804#define fetch_till_str(as_string, input, word, skip_tabs) \
4805 fetch_till_str(input, word, skip_tabs)
4806#endif
4807static char *fetch_till_str(o_string *as_string,
4808 struct in_str *input,
4809 const char *word,
4810 int heredoc_flags)
4811{
4812 o_string heredoc = NULL_O_STRING;
4813 unsigned past_EOL;
4814 int prev = 0; /* not \ */
4815 int ch;
4816
4817 /* Starting with "" is necessary for this case:
4818 * cat <<EOF
4819 *
4820 * xxx
4821 * EOF
4822 */
4823 heredoc.data = xzalloc(1); /* start as "", not as NULL */
4824
4825 goto jump_in;
4826
4827 while (1) {
4828 ch = i_getch(input);
4829 if (ch != EOF)
4830 nommu_addchr(as_string, ch);
4831 if (ch == '\n' || ch == EOF) {
4832 check_heredoc_end:
Francis Laniel36836fc2023-12-22 22:02:28 +01004833#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01004834 if ((heredoc_flags & HEREDOC_QUOTED) || prev != '\\') {
Francis Laniel36836fc2023-12-22 22:02:28 +01004835#else /* __U_BOOT__ */
4836 if (prev != '\\') {
4837#endif
Francis Laniel110b7692023-12-22 22:02:27 +01004838 /* End-of-line, and not a line continuation */
4839 if (strcmp(heredoc.data + past_EOL, word) == 0) {
4840 heredoc.data[past_EOL] = '\0';
4841 debug_printf_heredoc("parsed '%s' heredoc '%s'\n", word, heredoc.data);
4842 return heredoc.data;
4843 }
4844 if (ch == '\n') {
4845 /* This is a new line.
4846 * Remember position and backslash-escaping status.
4847 */
4848 o_addchr(&heredoc, ch);
4849 prev = ch;
4850 jump_in:
4851 past_EOL = heredoc.length;
4852 /* Get 1st char of next line, possibly skipping leading tabs */
4853 do {
4854 ch = i_getch(input);
4855 if (ch != EOF)
4856 nommu_addchr(as_string, ch);
Francis Laniel36836fc2023-12-22 22:02:28 +01004857#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01004858 } while ((heredoc_flags & HEREDOC_SKIPTABS) && ch == '\t');
Francis Laniel36836fc2023-12-22 22:02:28 +01004859#else /* __U_BOOT__ */
4860 } while (ch == '\t');
4861#endif
Francis Laniel110b7692023-12-22 22:02:27 +01004862 /* If this immediately ended the line,
4863 * go back to end-of-line checks.
4864 */
4865 if (ch == '\n')
4866 goto check_heredoc_end;
4867 }
4868 } else {
4869 /* Backslash-line continuation in an unquoted
4870 * heredoc. This does not need special handling
4871 * for heredoc body (unquoted heredocs are
4872 * expanded on "execution" and that would take
4873 * care of this case too), but not the case
4874 * of line continuation *in terminator*:
4875 * cat <<EOF
4876 * Ok1
4877 * EO\
4878 * F
4879 */
4880 heredoc.data[--heredoc.length] = '\0';
4881 prev = 0; /* not '\' */
4882 continue;
4883 }
4884 }
4885 if (ch == EOF) {
4886 o_free(&heredoc);
4887 return NULL; /* error */
4888 }
4889 o_addchr(&heredoc, ch);
4890 nommu_addchr(as_string, ch);
4891 if (prev == '\\' && ch == '\\')
4892 /* Correctly handle foo\\<eol> (not a line cont.) */
4893 prev = 0; /* not '\' */
4894 else
4895 prev = ch;
4896 }
4897}
Francis Laniel36836fc2023-12-22 22:02:28 +01004898#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01004899
4900/* Look at entire parse tree for not-yet-loaded REDIRECT_HEREDOCs
4901 * and load them all. There should be exactly heredoc_cnt of them.
4902 */
4903#if BB_MMU
4904#define fetch_heredocs(as_string, pi, heredoc_cnt, input) \
4905 fetch_heredocs(pi, heredoc_cnt, input)
4906#endif
4907static int fetch_heredocs(o_string *as_string, struct pipe *pi, int heredoc_cnt, struct in_str *input)
4908{
4909 while (pi && heredoc_cnt) {
4910 int i;
4911 struct command *cmd = pi->cmds;
4912
4913 debug_printf_heredoc("fetch_heredocs: num_cmds:%d cmd argv0:'%s'\n",
4914 pi->num_cmds,
4915 cmd->argv ? cmd->argv[0] : "NONE"
4916 );
4917 for (i = 0; i < pi->num_cmds; i++) {
Francis Laniel36836fc2023-12-22 22:02:28 +01004918#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01004919 struct redir_struct *redir = cmd->redirects;
4920
Francis Laniel36836fc2023-12-22 22:02:28 +01004921#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01004922 debug_printf_heredoc("fetch_heredocs: %d cmd argv0:'%s'\n",
4923 i, cmd->argv ? cmd->argv[0] : "NONE");
Francis Laniel36836fc2023-12-22 22:02:28 +01004924#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01004925 while (redir) {
4926 if (redir->rd_type == REDIRECT_HEREDOC) {
4927 char *p;
4928
4929 redir->rd_type = REDIRECT_HEREDOC2;
4930 /* redir->rd_dup is (ab)used to indicate <<- */
4931 p = fetch_till_str(as_string, input,
4932 redir->rd_filename, redir->rd_dup);
4933 if (!p) {
4934 syntax_error("unexpected EOF in here document");
4935 return -1;
4936 }
4937 free(redir->rd_filename);
4938 redir->rd_filename = p;
4939 heredoc_cnt--;
4940 }
4941 redir = redir->next;
4942 }
Francis Laniel36836fc2023-12-22 22:02:28 +01004943#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01004944 if (cmd->group) {
4945 //bb_error_msg("%s:%u heredoc_cnt:%d", __func__, __LINE__, heredoc_cnt);
4946 heredoc_cnt = fetch_heredocs(as_string, cmd->group, heredoc_cnt, input);
4947 //bb_error_msg("%s:%u heredoc_cnt:%d", __func__, __LINE__, heredoc_cnt);
4948 if (heredoc_cnt < 0)
4949 return heredoc_cnt; /* error */
4950 }
4951 cmd++;
4952 }
4953 pi = pi->next;
4954 }
4955 return heredoc_cnt;
4956}
4957
Francis Laniel110b7692023-12-22 22:02:27 +01004958static int run_list(struct pipe *pi);
4959#if BB_MMU
4960#define parse_stream(pstring, heredoc_cnt_ptr, input, end_trigger) \
4961 parse_stream(heredoc_cnt_ptr, input, end_trigger)
4962#endif
4963static struct pipe *parse_stream(char **pstring,
4964 int *heredoc_cnt_ptr,
4965 struct in_str *input,
4966 int end_trigger);
4967
4968/* Returns number of heredocs not yet consumed,
4969 * or -1 on error.
4970 */
4971static int parse_group(struct parse_context *ctx,
4972 struct in_str *input, int ch)
4973{
4974 /* ctx->word contains characters seen prior to ( or {.
4975 * Typically it's empty, but for function defs,
4976 * it contains function name (without '()'). */
4977#if BB_MMU
4978# define as_string NULL
4979#else
4980 char *as_string = NULL;
4981#endif
4982 struct pipe *pipe_list;
4983 int heredoc_cnt = 0;
4984 int endch;
4985 struct command *command = ctx->command;
4986
4987 debug_printf_parse("parse_group entered\n");
4988#if ENABLE_HUSH_FUNCTIONS
4989 if (ch == '(' && !ctx->word.has_quoted_part) {
4990 if (ctx->word.length)
4991 if (done_word(ctx))
4992 return -1;
4993 if (!command->argv)
4994 goto skip; /* (... */
4995 if (command->argv[1]) { /* word word ... (... */
4996 syntax_error_unexpected_ch('(');
4997 return -1;
4998 }
4999 /* it is "word(..." or "word (..." */
5000 do
5001 ch = i_getch(input);
5002 while (ch == ' ' || ch == '\t');
5003 if (ch != ')') {
5004 syntax_error_unexpected_ch(ch);
5005 return -1;
5006 }
5007 nommu_addchr(&ctx->as_string, ch);
5008 do
5009 ch = i_getch(input);
5010 while (ch == ' ' || ch == '\t' || ch == '\n');
5011 if (ch != '{' && ch != '(') {
5012 syntax_error_unexpected_ch(ch);
5013 return -1;
5014 }
Francis Laniele7ca3a32023-12-22 22:02:42 +01005015//bash allows functions named "123", "..", "return"!
5016// if (endofname(command->argv[0])[0] != '\0') {
5017// syntax_error("bad function name");
5018// return -1;
5019// }
Francis Laniel110b7692023-12-22 22:02:27 +01005020 nommu_addchr(&ctx->as_string, ch);
5021 command->cmd_type = CMD_FUNCDEF;
5022 goto skip;
5023 }
5024#endif
5025
5026#if 0 /* Prevented by caller */
5027 if (command->argv /* word [word]{... */
5028 || ctx->word.length /* word{... */
5029 || ctx->word.has_quoted_part /* ""{... */
5030 ) {
5031 syntax_error(NULL);
5032 debug_printf_parse("parse_group return -1: "
5033 "syntax error, groups and arglists don't mix\n");
5034 return -1;
5035 }
5036#endif
5037
Francis Laniel36836fc2023-12-22 22:02:28 +01005038#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01005039 IF_HUSH_FUNCTIONS(skip:)
Francis Laniel36836fc2023-12-22 22:02:28 +01005040#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01005041
5042 endch = '}';
5043 if (ch == '(') {
5044 endch = ')';
Francis Laniel36836fc2023-12-22 22:02:28 +01005045#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01005046 IF_HUSH_FUNCTIONS(if (command->cmd_type != CMD_FUNCDEF))
5047 command->cmd_type = CMD_SUBSHELL;
Francis Laniel36836fc2023-12-22 22:02:28 +01005048#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01005049 } else {
5050 /* bash does not allow "{echo...", requires whitespace */
5051 ch = i_peek(input);
5052 if (ch != ' ' && ch != '\t' && ch != '\n'
5053 && ch != '(' /* but "{(..." is allowed (without whitespace) */
5054 ) {
5055 syntax_error_unexpected_ch(ch);
5056 return -1;
5057 }
5058 if (ch != '(') {
5059 ch = i_getch(input);
5060 nommu_addchr(&ctx->as_string, ch);
5061 }
5062 }
5063
5064 debug_printf_heredoc("calling parse_stream, heredoc_cnt:%d\n", heredoc_cnt);
5065 pipe_list = parse_stream(&as_string, &heredoc_cnt, input, endch);
5066 debug_printf_heredoc("parse_stream returned: heredoc_cnt:%d\n", heredoc_cnt);
5067#if !BB_MMU
5068 if (as_string)
5069 o_addstr(&ctx->as_string, as_string);
5070#endif
5071
5072 /* empty ()/{} or parse error? */
5073 if (!pipe_list || pipe_list == ERR_PTR) {
5074 /* parse_stream already emitted error msg */
5075 if (!BB_MMU)
5076 free(as_string);
5077 debug_printf_parse("parse_group return -1: "
5078 "parse_stream returned %p\n", pipe_list);
5079 return -1;
5080 }
5081#if !BB_MMU
5082 as_string[strlen(as_string) - 1] = '\0'; /* plink ')' or '}' */
5083 command->group_as_string = as_string;
5084 debug_printf_parse("end of group, remembering as:'%s'\n",
5085 command->group_as_string);
5086#endif
5087
5088#if ENABLE_HUSH_FUNCTIONS
5089 /* Convert "f() (cmds)" to "f() {(cmds)}" */
5090 if (command->cmd_type == CMD_FUNCDEF && endch == ')') {
5091 struct command *cmd2;
5092
5093 cmd2 = xzalloc(sizeof(*cmd2));
5094 cmd2->cmd_type = CMD_SUBSHELL;
5095 cmd2->group = pipe_list;
5096# if !BB_MMU
5097//UNTESTED!
5098 cmd2->group_as_string = command->group_as_string;
5099 command->group_as_string = xasprintf("(%s)", command->group_as_string);
5100# endif
5101
5102 pipe_list = new_pipe();
5103 pipe_list->cmds = cmd2;
5104 pipe_list->num_cmds = 1;
5105 }
5106#endif
5107
5108 command->group = pipe_list;
5109
5110 debug_printf_parse("parse_group return %d\n", heredoc_cnt);
5111 return heredoc_cnt;
5112 /* command remains "open", available for possible redirects */
5113#undef as_string
5114}
5115
Francis Laniel36836fc2023-12-22 22:02:28 +01005116#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01005117#if ENABLE_HUSH_TICK || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_DOLLAR_OPS
5118/* Subroutines for copying $(...) and `...` things */
5119/* '...' */
5120static int add_till_single_quote(o_string *dest, struct in_str *input)
5121{
5122 while (1) {
5123 int ch = i_getch(input);
5124 if (ch == EOF) {
5125 syntax_error_unterm_ch('\'');
5126 return 0;
5127 }
5128 if (ch == '\'')
5129 return 1;
5130 o_addchr(dest, ch);
5131 }
5132}
5133static int add_till_single_quote_dquoted(o_string *dest, struct in_str *input)
5134{
5135 while (1) {
5136 int ch = i_getch(input);
5137 if (ch == EOF) {
5138 syntax_error_unterm_ch('\'');
5139 return 0;
5140 }
5141 if (ch == '\'')
5142 return 1;
5143 o_addqchr(dest, ch);
5144 }
5145}
Francis Laniel36836fc2023-12-22 22:02:28 +01005146
Francis Laniel110b7692023-12-22 22:02:27 +01005147/* "...\"...`..`...." - do we need to handle "...$(..)..." too? */
5148static int add_till_backquote(o_string *dest, struct in_str *input, int in_dquote);
5149static int add_till_double_quote(o_string *dest, struct in_str *input)
5150{
5151 while (1) {
5152 int ch = i_getch(input);
5153 if (ch == EOF) {
5154 syntax_error_unterm_ch('"');
5155 return 0;
5156 }
5157 if (ch == '"')
5158 return 1;
5159 if (ch == '\\') { /* \x. Copy both chars. */
5160 o_addchr(dest, ch);
5161 ch = i_getch(input);
5162 }
5163 o_addchr(dest, ch);
5164 if (ch == '`') {
5165 if (!add_till_backquote(dest, input, /*in_dquote:*/ 1))
5166 return 0;
5167 o_addchr(dest, ch);
5168 continue;
5169 }
5170 //if (ch == '$') ...
5171 }
5172}
Francis Laniel36836fc2023-12-22 22:02:28 +01005173
Francis Laniel110b7692023-12-22 22:02:27 +01005174/* Process `cmd` - copy contents until "`" is seen. Complicated by
5175 * \` quoting.
5176 * "Within the backquoted style of command substitution, backslash
5177 * shall retain its literal meaning, except when followed by: '$', '`', or '\'.
5178 * The search for the matching backquote shall be satisfied by the first
5179 * backquote found without a preceding backslash; during this search,
5180 * if a non-escaped backquote is encountered within a shell comment,
5181 * a here-document, an embedded command substitution of the $(command)
5182 * form, or a quoted string, undefined results occur. A single-quoted
5183 * or double-quoted string that begins, but does not end, within the
5184 * "`...`" sequence produces undefined results."
5185 * Example Output
5186 * echo `echo '\'TEST\`echo ZZ\`BEST` \TESTZZBEST
5187 */
5188static int add_till_backquote(o_string *dest, struct in_str *input, int in_dquote)
5189{
5190 while (1) {
5191 int ch = i_getch(input);
5192 if (ch == '`')
5193 return 1;
5194 if (ch == '\\') {
5195 /* \x. Copy both unless it is \`, \$, \\ and maybe \" */
5196 ch = i_getch(input);
5197 if (ch != '`'
5198 && ch != '$'
5199 && ch != '\\'
5200 && (!in_dquote || ch != '"')
5201 ) {
5202 o_addchr(dest, '\\');
5203 }
5204 }
5205 if (ch == EOF) {
5206 syntax_error_unterm_ch('`');
5207 return 0;
5208 }
5209 o_addchr(dest, ch);
5210 }
5211}
5212/* Process $(cmd) - copy contents until ")" is seen. Complicated by
5213 * quoting and nested ()s.
5214 * "With the $(command) style of command substitution, all characters
5215 * following the open parenthesis to the matching closing parenthesis
5216 * constitute the command. Any valid shell script can be used for command,
5217 * except a script consisting solely of redirections which produces
5218 * unspecified results."
5219 * Example Output
5220 * echo $(echo '(TEST)' BEST) (TEST) BEST
5221 * echo $(echo 'TEST)' BEST) TEST) BEST
5222 * echo $(echo \(\(TEST\) BEST) ((TEST) BEST
5223 *
5224 * Also adapted to eat ${var%...} and $((...)) constructs, since ... part
5225 * can contain arbitrary constructs, just like $(cmd).
5226 * In bash compat mode, it needs to also be able to stop on ':' or '/'
5227 * for ${var:N[:M]} and ${var/P[/R]} parsing.
5228 */
5229#define DOUBLE_CLOSE_CHAR_FLAG 0x80
5230static int add_till_closing_bracket(o_string *dest, struct in_str *input, unsigned end_ch)
5231{
5232 int ch;
5233 char dbl = end_ch & DOUBLE_CLOSE_CHAR_FLAG;
5234# if BASH_SUBSTR || BASH_PATTERN_SUBST
5235 char end_char2 = end_ch >> 8;
5236# endif
5237 end_ch &= (DOUBLE_CLOSE_CHAR_FLAG - 1);
5238
5239# if ENABLE_HUSH_INTERACTIVE
5240 G.promptmode = 1; /* PS2 */
5241# endif
5242 debug_printf_prompt("%s promptmode=%d\n", __func__, G.promptmode);
5243
5244 while (1) {
5245 ch = i_getch(input);
5246 if (ch == EOF) {
5247 syntax_error_unterm_ch(end_ch);
5248 return 0;
5249 }
5250 if (ch == end_ch
5251# if BASH_SUBSTR || BASH_PATTERN_SUBST
5252 || ch == end_char2
5253# endif
5254 ) {
5255 if (!dbl)
5256 break;
5257 /* we look for closing )) of $((EXPR)) */
5258 if (i_peek_and_eat_bkslash_nl(input) == end_ch) {
5259 i_getch(input); /* eat second ')' */
5260 break;
5261 }
5262 }
5263 o_addchr(dest, ch);
5264 //bb_error_msg("%s:o_addchr('%c')", __func__, ch);
5265 if (ch == '(' || ch == '{') {
5266 ch = (ch == '(' ? ')' : '}');
5267 if (!add_till_closing_bracket(dest, input, ch))
5268 return 0;
5269 o_addchr(dest, ch);
5270 continue;
5271 }
5272 if (ch == '\'') {
5273 if (!add_till_single_quote(dest, input))
5274 return 0;
5275 o_addchr(dest, ch);
5276 continue;
5277 }
5278 if (ch == '"') {
5279 if (!add_till_double_quote(dest, input))
5280 return 0;
5281 o_addchr(dest, ch);
5282 continue;
5283 }
5284 if (ch == '`') {
5285 if (!add_till_backquote(dest, input, /*in_dquote:*/ 0))
5286 return 0;
5287 o_addchr(dest, ch);
5288 continue;
5289 }
5290 if (ch == '\\') {
5291 /* \x. Copy verbatim. Important for \(, \) */
5292 ch = i_getch(input);
5293 if (ch == EOF) {
5294 syntax_error_unterm_ch(end_ch);
5295 return 0;
5296 }
5297# if 0
5298 if (ch == '\n') {
5299 /* "backslash+newline", ignore both */
5300 o_delchr(dest); /* undo insertion of '\' */
5301 continue;
5302 }
5303# endif
5304 o_addchr(dest, ch);
5305 //bb_error_msg("%s:o_addchr('%c') after '\\'", __func__, ch);
5306 continue;
5307 }
5308 }
5309 debug_printf_parse("%s return '%s' ch:'%c'\n", __func__, dest->data, ch);
5310 return ch;
5311}
5312#endif /* ENABLE_HUSH_TICK || ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_DOLLAR_OPS */
5313
5314#if BASH_DOLLAR_SQUOTE
5315/* Return code: 1 for "found and parsed", 0 for "seen something else" */
5316# if BB_MMU
5317#define parse_dollar_squote(as_string, dest, input) \
5318 parse_dollar_squote(dest, input)
5319#define as_string NULL
5320# endif
5321static int parse_dollar_squote(o_string *as_string, o_string *dest, struct in_str *input)
5322{
5323 int start;
5324 int ch = i_peek_and_eat_bkslash_nl(input); /* first character after the $ */
5325 debug_printf_parse("parse_dollar_squote entered: ch='%c'\n", ch);
5326 if (ch != '\'')
5327 return 0;
5328
5329 dest->has_quoted_part = 1;
5330 start = dest->length;
5331
5332 ch = i_getch(input); /* eat ' */
5333 nommu_addchr(as_string, ch);
5334 while (1) {
5335 ch = i_getch(input);
5336 nommu_addchr(as_string, ch);
5337 if (ch == EOF) {
5338 syntax_error_unterm_ch('\'');
5339 return 0;
5340 }
5341 if (ch == '\'')
5342 break;
5343 if (ch == SPECIAL_VAR_SYMBOL) {
5344 /* Convert raw ^C to corresponding special variable reference */
5345 o_addchr(dest, SPECIAL_VAR_SYMBOL);
5346 o_addchr(dest, SPECIAL_VAR_QUOTED_SVS);
5347 /* will addchr() another SPECIAL_VAR_SYMBOL (see after the if() block) */
5348 } else if (ch == '\\') {
5349 static const char C_escapes[] ALIGN1 = "nrbtfav""x\\01234567";
5350
5351 ch = i_getch(input);
5352 nommu_addchr(as_string, ch);
5353 if (strchr(C_escapes, ch)) {
5354 char buf[4];
5355 char *p = buf;
5356 int cnt = 2;
5357
5358 buf[0] = ch;
5359 if ((unsigned char)(ch - '0') <= 7) { /* \ooo */
5360 do {
5361 ch = i_peek(input);
5362 if ((unsigned char)(ch - '0') > 7)
5363 break;
5364 *++p = ch = i_getch(input);
5365 nommu_addchr(as_string, ch);
5366 } while (--cnt != 0);
5367 } else if (ch == 'x') { /* \xHH */
5368 do {
5369 ch = i_peek(input);
5370 if (!isxdigit(ch))
5371 break;
5372 *++p = ch = i_getch(input);
5373 nommu_addchr(as_string, ch);
5374 } while (--cnt != 0);
5375 if (cnt == 2) { /* \x but next char is "bad" */
5376 ch = 'x';
5377 goto unrecognized;
5378 }
5379 } /* else simple seq like \\ or \t */
5380 *++p = '\0';
5381 p = buf;
5382 ch = bb_process_escape_sequence((void*)&p);
5383 //bb_error_msg("buf:'%s' ch:%x", buf, ch);
5384 if (ch == '\0')
5385 continue; /* bash compat: $'...\0...' emits nothing */
5386 } else { /* unrecognized "\z": encode both chars unless ' or " */
5387 if (ch != '\'' && ch != '"') {
5388 unrecognized:
5389 o_addqchr(dest, '\\');
5390 }
5391 }
5392 } /* if (\...) */
5393 o_addqchr(dest, ch);
5394 }
5395
5396 if (dest->length == start) {
5397 /* $'', $'\0', $'\000\x00' and the like */
5398 o_addchr(dest, SPECIAL_VAR_SYMBOL);
5399 o_addchr(dest, SPECIAL_VAR_SYMBOL);
5400 }
5401
5402 return 1;
5403# undef as_string
5404}
5405#else
Francis Laniele7ca3a32023-12-22 22:02:42 +01005406# define parse_dollar_squote(as_string, dest, input) 0
Francis Laniel110b7692023-12-22 22:02:27 +01005407#endif /* BASH_DOLLAR_SQUOTE */
Francis Laniel36836fc2023-12-22 22:02:28 +01005408#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01005409
5410/* Return code: 0 for OK, 1 for syntax error */
5411#if BB_MMU
5412#define parse_dollar(as_string, dest, input, quote_mask) \
5413 parse_dollar(dest, input, quote_mask)
5414#define as_string NULL
5415#endif
5416static int parse_dollar(o_string *as_string,
5417 o_string *dest,
5418 struct in_str *input, unsigned char quote_mask)
5419{
5420 int ch = i_peek_and_eat_bkslash_nl(input); /* first character after the $ */
5421
5422 debug_printf_parse("parse_dollar entered: ch='%c' quote_mask:0x%x\n", ch, quote_mask);
5423 if (isalpha(ch)) {
5424 make_var:
5425 ch = i_getch(input);
5426 nommu_addchr(as_string, ch);
5427 /*make_var1:*/
5428 o_addchr(dest, SPECIAL_VAR_SYMBOL);
5429 while (1) {
5430 debug_printf_parse(": '%c'\n", ch);
5431 o_addchr(dest, ch | quote_mask);
5432 quote_mask = 0;
5433 ch = i_peek_and_eat_bkslash_nl(input);
5434 if (!isalnum(ch) && ch != '_') {
5435 /* End of variable name reached */
5436 break;
5437 }
5438 ch = i_getch(input);
5439 nommu_addchr(as_string, ch);
5440 }
5441 o_addchr(dest, SPECIAL_VAR_SYMBOL);
5442 } else if (isdigit(ch)) {
5443 make_one_char_var:
5444 ch = i_getch(input);
5445 nommu_addchr(as_string, ch);
5446 o_addchr(dest, SPECIAL_VAR_SYMBOL);
5447 debug_printf_parse(": '%c'\n", ch);
5448 o_addchr(dest, ch | quote_mask);
5449 o_addchr(dest, SPECIAL_VAR_SYMBOL);
5450 } else switch (ch) {
Francis Laniel36836fc2023-12-22 22:02:28 +01005451#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01005452 case '$': /* pid */
5453 case '!': /* last bg pid */
Francis Laniel36836fc2023-12-22 22:02:28 +01005454#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01005455 case '?': /* last exit code */
5456 case '#': /* number of args */
5457 case '*': /* args */
5458 case '@': /* args */
5459 case '-': /* $- option flags set by set builtin or shell options (-i etc) */
5460 goto make_one_char_var;
5461 case '{': {
5462 char len_single_ch;
5463
5464 o_addchr(dest, SPECIAL_VAR_SYMBOL);
5465
5466 ch = i_getch(input); /* eat '{' */
5467 nommu_addchr(as_string, ch);
5468
5469 ch = i_getch_and_eat_bkslash_nl(input); /* first char after '{' */
5470 /* It should be ${?}, or ${#var},
5471 * or even ${?+subst} - operator acting on a special variable,
5472 * or the beginning of variable name.
5473 */
5474 if (ch == EOF
5475 || (!strchr(_SPECIAL_VARS_STR, ch) && !isalnum(ch)) /* not one of those */
5476 ) {
5477 bad_dollar_syntax:
5478 syntax_error_unterm_str("${name}");
5479 debug_printf_parse("parse_dollar return 0: unterminated ${name}\n");
5480 return 0;
5481 }
5482 nommu_addchr(as_string, ch);
5483 len_single_ch = ch;
5484 ch |= quote_mask;
5485
5486 /* It's possible to just call add_till_closing_bracket() at this point.
5487 * However, this regresses some of our testsuite cases
5488 * which check invalid constructs like ${%}.
5489 * Oh well... let's check that the var name part is fine... */
5490
5491 if (isdigit(len_single_ch)
5492 || (len_single_ch == '#' && isdigit(i_peek_and_eat_bkslash_nl(input)))
5493 ) {
5494 /* Execution engine uses plain xatoi_positive()
5495 * to interpret ${NNN} and {#NNN},
5496 * check syntax here in the parser.
5497 * (bash does not support expressions in ${#NN},
5498 * e.g. ${#$var} and {#1:+WORD} are not supported).
5499 */
5500 unsigned cnt = 9; /* max 9 digits for ${NN} and 8 for {#NN} */
5501 while (1) {
5502 o_addchr(dest, ch);
5503 debug_printf_parse(": '%c'\n", ch);
5504 ch = i_getch_and_eat_bkslash_nl(input);
5505 nommu_addchr(as_string, ch);
5506 if (ch == '}')
5507 break;
5508 if (--cnt == 0)
5509 goto bad_dollar_syntax;
5510 if (len_single_ch != '#' && strchr(VAR_SUBST_OPS, ch))
5511 /* ${NN<op>...} is valid */
5512 goto eat_until_closing;
5513 if (!isdigit(ch))
5514 goto bad_dollar_syntax;
5515 }
5516 } else
5517 while (1) {
5518 unsigned pos;
5519
5520 o_addchr(dest, ch);
5521 debug_printf_parse(": '%c'\n", ch);
5522
5523 ch = i_getch(input);
5524 nommu_addchr(as_string, ch);
5525 if (ch == '}')
5526 break;
Francis Lanielbfc406a2023-12-22 22:02:33 +01005527#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01005528 if (!isalnum(ch) && ch != '_') {
Francis Lanielbfc406a2023-12-22 22:02:33 +01005529#else /* __U_BOOT__ */
5530 /*
5531 * In several places in U-Boot, we use variable like
5532 * foo# (e.g. serial#), particularly in env.
5533 * So, we need to authorize # to appear inside
5534 * variable name and then expand this variable.
5535 * NOTE Having # in variable name is not permitted in
5536 * upstream hush but expansion will be done (even though
5537 * the result will be empty).
5538 */
5539 if (!isalnum(ch) && ch != '_' && ch != '#') {
5540#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01005541 unsigned end_ch;
Francis Laniel36836fc2023-12-22 22:02:28 +01005542#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01005543 unsigned char last_ch;
Francis Laniel36836fc2023-12-22 22:02:28 +01005544#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01005545 /* handle parameter expansions
5546 * http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html#tag_02_06_02
5547 */
5548 if (!strchr(VAR_SUBST_OPS, ch)) { /* ${var<bad_char>... */
5549 if (len_single_ch != '#'
5550 /*|| !strchr(SPECIAL_VARS_STR, ch) - disallow errors like ${#+} ? */
5551 || i_peek(input) != '}'
5552 ) {
5553 goto bad_dollar_syntax;
5554 }
5555 /* else: it's "length of C" ${#C} op,
5556 * where C is a single char
5557 * special var name, e.g. ${#!}.
5558 */
5559 }
5560 eat_until_closing:
5561 /* Eat everything until closing '}' (or ':') */
5562 end_ch = '}';
Francis Laniel36836fc2023-12-22 22:02:28 +01005563#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01005564 if (BASH_SUBSTR
5565 && ch == ':'
5566 && !strchr(MINUS_PLUS_EQUAL_QUESTION, i_peek(input))
5567 ) {
5568 /* It's ${var:N[:M]} thing */
5569 end_ch = '}' * 0x100 + ':';
5570 }
5571 if (BASH_PATTERN_SUBST
5572 && ch == '/'
5573 ) {
5574 /* It's ${var/[/]pattern[/repl]} thing */
5575 if (i_peek(input) == '/') { /* ${var//pattern[/repl]}? */
5576 i_getch(input);
5577 nommu_addchr(as_string, '/');
5578 ch = '\\';
5579 }
5580 end_ch = '}' * 0x100 + '/';
5581 }
Francis Laniel36836fc2023-12-22 22:02:28 +01005582#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01005583 o_addchr(dest, ch);
5584 /* The pattern can't be empty.
5585 * IOW: if the first char after "${v//" is a slash,
5586 * it does not terminate the pattern - it's the first char of the pattern:
5587 * v=/dev/ram; echo ${v////-} prints -dev-ram (pattern is "/")
5588 * v=/dev/ram; echo ${v///r/-} prints /dev-am (pattern is "/r")
5589 */
5590 if (i_peek(input) == '/') {
5591 o_addchr(dest, i_getch(input));
5592 }
Francis Laniel36836fc2023-12-22 22:02:28 +01005593#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01005594 again:
Francis Laniel36836fc2023-12-22 22:02:28 +01005595#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01005596 if (!BB_MMU)
5597 pos = dest->length;
5598#if ENABLE_HUSH_DOLLAR_OPS
Francis Laniel36836fc2023-12-22 22:02:28 +01005599#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01005600 last_ch = add_till_closing_bracket(dest, input, end_ch);
5601 if (last_ch == 0) /* error? */
5602 return 0;
Francis Laniel36836fc2023-12-22 22:02:28 +01005603#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01005604#else
5605# error Simple code to only allow ${var} is not implemented
5606#endif
5607 if (as_string) {
5608 o_addstr(as_string, dest->data + pos);
Francis Laniel36836fc2023-12-22 22:02:28 +01005609#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01005610 o_addchr(as_string, last_ch);
Francis Laniel36836fc2023-12-22 22:02:28 +01005611#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01005612 }
5613
Francis Laniel36836fc2023-12-22 22:02:28 +01005614#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01005615 if ((BASH_SUBSTR || BASH_PATTERN_SUBST)
5616 && (end_ch & 0xff00)
5617 ) {
5618 /* close the first block: */
5619 o_addchr(dest, SPECIAL_VAR_SYMBOL);
5620 /* while parsing N from ${var:N[:M]}
5621 * or pattern from ${var/[/]pattern[/repl]} */
5622 if ((end_ch & 0xff) == last_ch) {
5623 /* got ':' or '/'- parse the rest */
5624 end_ch = '}';
5625 goto again;
5626 }
5627 /* got '}' */
5628 if (BASH_SUBSTR && end_ch == '}' * 0x100 + ':') {
5629 /* it's ${var:N} - emulate :999999999 */
5630 o_addstr(dest, "999999999");
5631 } /* else: it's ${var/[/]pattern} */
5632 }
Francis Laniel36836fc2023-12-22 22:02:28 +01005633#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01005634 break;
5635 }
5636 len_single_ch = 0; /* it can't be ${#C} op */
5637 }
5638 o_addchr(dest, SPECIAL_VAR_SYMBOL);
5639 break;
5640 }
5641#if ENABLE_FEATURE_SH_MATH || ENABLE_HUSH_TICK
5642 case '(': {
5643 unsigned pos;
5644
5645 ch = i_getch(input);
5646 nommu_addchr(as_string, ch);
5647# if ENABLE_FEATURE_SH_MATH
5648 if (i_peek_and_eat_bkslash_nl(input) == '(') {
5649 ch = i_getch(input);
5650 nommu_addchr(as_string, ch);
5651 o_addchr(dest, SPECIAL_VAR_SYMBOL);
5652 o_addchr(dest, quote_mask | '+');
5653 if (!BB_MMU)
5654 pos = dest->length;
5655 if (!add_till_closing_bracket(dest, input, ')' | DOUBLE_CLOSE_CHAR_FLAG))
5656 return 0; /* error */
5657 if (as_string) {
5658 o_addstr(as_string, dest->data + pos);
5659 o_addchr(as_string, ')');
5660 o_addchr(as_string, ')');
5661 }
5662 o_addchr(dest, SPECIAL_VAR_SYMBOL);
5663 break;
5664 }
5665# endif
5666# if ENABLE_HUSH_TICK
5667 o_addchr(dest, SPECIAL_VAR_SYMBOL);
5668 o_addchr(dest, quote_mask | '`');
5669 if (!BB_MMU)
5670 pos = dest->length;
5671 if (!add_till_closing_bracket(dest, input, ')'))
5672 return 0; /* error */
5673 if (as_string) {
5674 o_addstr(as_string, dest->data + pos);
5675 o_addchr(as_string, ')');
5676 }
5677 o_addchr(dest, SPECIAL_VAR_SYMBOL);
5678# endif
5679 break;
5680 }
5681#endif
5682 case '_':
5683 goto make_var;
5684#if 0
5685 /* TODO: $_: */
5686 /* $_ Shell or shell script name; or last argument of last command
5687 * (if last command wasn't a pipe; if it was, bash sets $_ to "");
5688 * but in command's env, set to full pathname used to invoke it */
5689 ch = i_getch(input);
5690 nommu_addchr(as_string, ch);
5691 ch = i_peek_and_eat_bkslash_nl(input);
5692 if (isalnum(ch)) { /* it's $_name or $_123 */
5693 ch = '_';
5694 goto make_var1;
5695 }
5696 /* else: it's $_ */
5697#endif
5698 default:
5699 o_addQchr(dest, '$');
5700 }
5701 debug_printf_parse("parse_dollar return 1 (ok)\n");
5702 return 1;
5703#undef as_string
5704}
5705
5706#if BB_MMU
5707#define encode_string(as_string, dest, input, dquote_end) \
5708 encode_string(dest, input, dquote_end)
5709#define as_string NULL
5710#endif
5711static int encode_string(o_string *as_string,
5712 o_string *dest,
5713 struct in_str *input,
5714 int dquote_end)
5715{
5716 int ch;
5717 int next;
5718
5719 again:
5720 ch = i_getch(input);
5721 if (ch != EOF)
5722 nommu_addchr(as_string, ch);
5723 if (ch == dquote_end) { /* may be only '"' or EOF */
5724 debug_printf_parse("encode_string return 1 (ok)\n");
5725 return 1;
5726 }
5727 /* note: can't move it above ch == dquote_end check! */
5728 if (ch == EOF) {
5729 syntax_error_unterm_ch('"');
5730 return 0; /* error */
5731 }
5732 next = '\0';
5733 if (ch != '\n') {
5734 next = i_peek(input);
5735 }
5736 debug_printf_parse("\" ch=%c (%d) escape=%d\n",
5737 ch, ch, !!(dest->o_expflags & EXP_FLAG_ESC_GLOB_CHARS));
5738 if (ch == '\\') {
5739 if (next == EOF) {
5740 /* Testcase: in interactive shell a file with
5741 * echo "unterminated string\<eof>
5742 * is sourced.
5743 */
5744 syntax_error_unterm_ch('"');
5745 return 0; /* error */
5746 }
5747 /* bash:
5748 * "The backslash retains its special meaning [in "..."]
5749 * only when followed by one of the following characters:
5750 * $, `, ", \, or <newline>. A double quote may be quoted
5751 * within double quotes by preceding it with a backslash."
5752 * NB: in (unquoted) heredoc, above does not apply to ",
5753 * therefore we check for it by "next == dquote_end" cond.
5754 */
5755 if (next == dquote_end || strchr("$`\\\n", next)) {
5756 ch = i_getch(input); /* eat next */
5757 if (ch == '\n')
5758 goto again; /* skip \<newline> */
5759 } /* else: ch remains == '\\', and we double it below: */
5760 o_addqchr(dest, ch); /* \c if c is a glob char, else just c */
5761 nommu_addchr(as_string, ch);
5762 goto again;
5763 }
5764 if (ch == '$') {
5765 //if (parse_dollar_squote(as_string, dest, input))
5766 // goto again;
5767 if (!parse_dollar(as_string, dest, input, /*quote_mask:*/ 0x80)) {
5768 debug_printf_parse("encode_string return 0: "
5769 "parse_dollar returned 0 (error)\n");
5770 return 0;
5771 }
5772 goto again;
5773 }
5774#if ENABLE_HUSH_TICK
5775 if (ch == '`') {
5776 //unsigned pos = dest->length;
5777 o_addchr(dest, SPECIAL_VAR_SYMBOL);
5778 o_addchr(dest, 0x80 | '`');
5779 if (!add_till_backquote(dest, input, /*in_dquote:*/ dquote_end == '"'))
5780 return 0; /* error */
5781 o_addchr(dest, SPECIAL_VAR_SYMBOL);
5782 //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos);
5783 goto again;
5784 }
5785#endif
5786 o_addQchr(dest, ch);
5787 if (ch == SPECIAL_VAR_SYMBOL) {
5788 /* Convert "^C" to corresponding special variable reference */
5789 o_addchr(dest, SPECIAL_VAR_QUOTED_SVS);
5790 o_addchr(dest, SPECIAL_VAR_SYMBOL);
5791 }
5792 goto again;
5793#undef as_string
5794}
5795
5796/*
5797 * Scan input until EOF or end_trigger char.
5798 * Return a list of pipes to execute, or NULL on EOF
5799 * or if end_trigger character is met.
5800 * On syntax error, exit if shell is not interactive,
5801 * reset parsing machinery and start parsing anew,
5802 * or return ERR_PTR.
5803 */
5804static struct pipe *parse_stream(char **pstring,
5805 int *heredoc_cnt_ptr,
5806 struct in_str *input,
5807 int end_trigger)
5808{
5809 struct parse_context ctx;
5810 int heredoc_cnt;
5811
5812 /* Single-quote triggers a bypass of the main loop until its mate is
5813 * found. When recursing, quote state is passed in via ctx.word.o_expflags.
5814 */
5815 debug_printf_parse("parse_stream entered, end_trigger='%c'\n",
5816 end_trigger ? end_trigger : 'X');
5817 debug_enter();
5818
5819 initialize_context(&ctx);
5820
5821 /* If very first arg is "" or '', ctx.word.data may end up NULL.
5822 * Preventing this:
5823 */
5824 ctx.word.data = xzalloc(1); /* start as "", not as NULL */
5825
5826 /* We used to separate words on $IFS here. This was wrong.
5827 * $IFS is used only for word splitting when $var is expanded,
5828 * here we should use blank chars as separators, not $IFS
5829 */
5830
5831 heredoc_cnt = 0;
5832 while (1) {
5833 const char *is_blank;
5834 const char *is_special;
5835 int ch;
5836 int next;
Francis Laniel36836fc2023-12-22 22:02:28 +01005837#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01005838 int redir_fd;
5839 redir_type redir_style;
Francis Laniel36836fc2023-12-22 22:02:28 +01005840#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01005841
5842 ch = i_getch(input);
5843 debug_printf_parse(": ch=%c (%d) escape=%d\n",
5844 ch, ch, !!(ctx.word.o_expflags & EXP_FLAG_ESC_GLOB_CHARS));
5845 if (ch == EOF) {
5846 struct pipe *pi;
5847
5848 if (heredoc_cnt) {
5849 syntax_error_unterm_str("here document");
5850 goto parse_error_exitcode1;
5851 }
5852 if (end_trigger == ')') {
5853 syntax_error_unterm_ch('(');
5854 goto parse_error_exitcode1;
5855 }
5856 if (end_trigger == '}') {
5857 syntax_error_unterm_ch('{');
5858 goto parse_error_exitcode1;
5859 }
5860
5861 if (done_word(&ctx)) {
5862 goto parse_error_exitcode1;
5863 }
5864 o_free_and_set_NULL(&ctx.word);
5865 done_pipe(&ctx, PIPE_SEQ);
Francis Laniel03061a82024-09-03 19:09:43 +02005866
5867 /* Do we sit inside of any if's, loops or case's? */
5868 if (HAS_KEYWORDS
5869 IF_HAS_KEYWORDS(&& (ctx.ctx_res_w != RES_NONE || ctx.old_flag != 0))
5870 ) {
5871 syntax_error_unterm_str("compound statement");
5872 goto parse_error_exitcode1;
5873 }
5874
Francis Laniel110b7692023-12-22 22:02:27 +01005875 pi = ctx.list_head;
5876 /* If we got nothing... */
5877 /* (this makes bare "&" cmd a no-op.
5878 * bash says: "syntax error near unexpected token '&'") */
5879 if (pi->num_cmds == 0
5880 IF_HAS_KEYWORDS(&& pi->res_word == RES_NONE)
5881 ) {
5882 free_pipe_list(pi);
5883 pi = NULL;
5884 }
5885#if !BB_MMU
5886 debug_printf_parse("as_string1 '%s'\n", ctx.as_string.data);
5887 if (pstring)
5888 *pstring = ctx.as_string.data;
5889 else
5890 o_free(&ctx.as_string);
5891#endif
5892 // heredoc_cnt must be 0 here anyway
5893 //if (heredoc_cnt_ptr)
5894 // *heredoc_cnt_ptr = heredoc_cnt;
5895 debug_leave();
5896 debug_printf_heredoc("parse_stream return heredoc_cnt:%d\n", heredoc_cnt);
Francis Laniel03061a82024-09-03 19:09:43 +02005897 debug_printf_parse("parse_stream return %p: EOF\n", pi);
Francis Laniel110b7692023-12-22 22:02:27 +01005898 return pi;
5899 }
5900
5901 /* Handle "'" and "\" first, as they won't play nice with
5902 * i_peek_and_eat_bkslash_nl() anyway:
5903 * echo z\\
5904 * and
5905 * echo '\
5906 * '
5907 * would break.
5908 */
5909 if (ch == '\\') {
5910 ch = i_getch(input);
5911 if (ch == '\n')
5912 continue; /* drop \<newline>, get next char */
5913 nommu_addchr(&ctx.as_string, '\\');
5914 if (ch == SPECIAL_VAR_SYMBOL) {
5915 nommu_addchr(&ctx.as_string, ch);
5916 /* Convert \^C to corresponding special variable reference */
5917 goto case_SPECIAL_VAR_SYMBOL;
5918 }
5919 o_addchr(&ctx.word, '\\');
5920 if (ch == EOF) {
5921 /* Testcase: eval 'echo Ok\' */
5922 /* bash-4.3.43 was removing backslash,
5923 * but 4.4.19 retains it, most other shells too
5924 */
5925 continue; /* get next char */
5926 }
5927 /* Example: echo Hello \2>file
5928 * we need to know that word 2 is quoted
5929 */
5930 ctx.word.has_quoted_part = 1;
5931 nommu_addchr(&ctx.as_string, ch);
5932 o_addchr(&ctx.word, ch);
5933 continue; /* get next char */
5934 }
5935 nommu_addchr(&ctx.as_string, ch);
5936 if (ch == '\'') {
5937 ctx.word.has_quoted_part = 1;
5938 next = i_getch(input);
Francis Laniel36836fc2023-12-22 22:02:28 +01005939#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01005940 if (next == '\'' && !ctx.pending_redirect)
5941 goto insert_empty_quoted_str_marker;
Francis Laniel36836fc2023-12-22 22:02:28 +01005942#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01005943
5944 ch = next;
5945 while (1) {
5946 if (ch == EOF) {
5947 syntax_error_unterm_ch('\'');
5948 goto parse_error_exitcode1;
5949 }
5950 nommu_addchr(&ctx.as_string, ch);
5951 if (ch == '\'')
5952 break;
5953 if (ch == SPECIAL_VAR_SYMBOL) {
5954 /* Convert raw ^C to corresponding special variable reference */
5955 o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL);
5956 o_addchr(&ctx.word, SPECIAL_VAR_QUOTED_SVS);
5957 }
5958 o_addqchr(&ctx.word, ch);
5959 ch = i_getch(input);
5960 }
5961 continue; /* get next char */
5962 }
5963
5964 next = '\0';
5965 if (ch != '\n')
5966 next = i_peek_and_eat_bkslash_nl(input);
5967
5968 is_special = "{}<>&|();#" /* special outside of "str" */
Francis Laniel36836fc2023-12-22 22:02:28 +01005969#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01005970 "$\"" IF_HUSH_TICK("`") /* always special */
Francis Laniel36836fc2023-12-22 22:02:28 +01005971#else /* __U_BOOT__ */
5972 "$\""
5973#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01005974 SPECIAL_VAR_SYMBOL_STR;
5975#if defined(CMD_TEST2_SINGLEWORD_NOGLOB)
5976 if (ctx.command->cmd_type == CMD_TEST2_SINGLEWORD_NOGLOB) {
5977 /* In [[ ]], {}<>&|() are not special */
5978 is_special += 8;
5979 } else
5980#endif
5981 /* Are { and } special here? */
5982 if (ctx.command->argv /* word [word]{... - non-special */
5983 || ctx.word.length /* word{... - non-special */
5984 || ctx.word.has_quoted_part /* ""{... - non-special */
5985 || (next != ';' /* }; - special */
5986 && next != ')' /* }) - special */
5987 && next != '(' /* {( - special */
5988 && next != '&' /* }& and }&& ... - special */
5989 && next != '|' /* }|| ... - special */
5990 && !strchr(defifs, next) /* {word - non-special */
5991 )
5992 ) {
5993 /* They are not special, skip "{}" */
5994 is_special += 2;
5995 }
5996 is_special = strchr(is_special, ch);
5997 is_blank = strchr(defifs, ch);
5998
5999 if (!is_special && !is_blank) { /* ordinary char */
6000 ordinary_char:
6001 o_addQchr(&ctx.word, ch);
6002 if ((ctx.is_assignment == MAYBE_ASSIGNMENT
6003 || ctx.is_assignment == WORD_IS_KEYWORD)
6004 && ch == '='
6005 && endofname(ctx.word.data)[0] == '='
6006 ) {
6007 ctx.is_assignment = DEFINITELY_ASSIGNMENT;
6008 debug_printf_parse("ctx.is_assignment='%s'\n", assignment_flag[ctx.is_assignment]);
6009 }
6010 continue;
6011 }
6012
6013 if (is_blank) {
6014#if ENABLE_HUSH_LINENO_VAR
6015/* Case:
6016 * "while ...; do<whitespace><newline>
6017 * cmd ..."
6018 * would think that "cmd" starts in <whitespace> -
6019 * i.e., at the previous line.
6020 * We need to skip all whitespace before newlines.
6021 */
6022 while (ch != '\n') {
6023 next = i_peek(input);
6024 if (next != ' ' && next != '\t' && next != '\n')
6025 break; /* next char is not ws */
6026 ch = i_getch(input);
6027 }
6028 /* ch == last eaten whitespace char */
6029#endif
6030 if (done_word(&ctx)) {
6031 goto parse_error_exitcode1;
6032 }
6033 if (ch == '\n') {
6034 /* Is this a case when newline is simply ignored?
6035 * Some examples:
6036 * "cmd | <newline> cmd ..."
6037 * "case ... in <newline> word) ..."
6038 */
6039 if (IS_NULL_CMD(ctx.command)
6040 && ctx.word.length == 0
6041 && !ctx.word.has_quoted_part
6042 && heredoc_cnt == 0
6043 ) {
6044 /* This newline can be ignored. But...
6045 * Without check #1, interactive shell
6046 * ignores even bare <newline>,
6047 * and shows the continuation prompt:
6048 * ps1_prompt$ <enter>
6049 * ps2> _ <=== wrong, should be ps1
6050 * Without check #2, "cmd & <newline>"
6051 * is similarly mistreated.
6052 * (BTW, this makes "cmd & cmd"
6053 * and "cmd && cmd" non-orthogonal.
6054 * Really, ask yourself, why
6055 * "cmd && <newline>" doesn't start
6056 * cmd but waits for more input?
6057 * The only reason is that it might be
6058 * a "cmd1 && <nl> cmd2 &" construct,
6059 * cmd1 may need to run in BG).
6060 */
6061 struct pipe *pi = ctx.list_head;
6062 if (pi->num_cmds != 0 /* check #1 */
6063 && pi->followup != PIPE_BG /* check #2 */
6064 ) {
6065 continue;
6066 }
6067 }
6068 /* Treat newline as a command separator. */
6069 done_pipe(&ctx, PIPE_SEQ);
6070 debug_printf_heredoc("heredoc_cnt:%d\n", heredoc_cnt);
6071 if (heredoc_cnt) {
6072 heredoc_cnt = fetch_heredocs(&ctx.as_string, ctx.list_head, heredoc_cnt, input);
6073 if (heredoc_cnt != 0)
6074 goto parse_error_exitcode1;
6075 }
6076 ctx.is_assignment = MAYBE_ASSIGNMENT;
6077 debug_printf_parse("ctx.is_assignment='%s'\n", assignment_flag[ctx.is_assignment]);
6078 ch = ';';
6079 /* note: if (is_blank) continue;
6080 * will still trigger for us */
6081 }
6082 }
6083
6084 /* "cmd}" or "cmd }..." without semicolon or &:
6085 * } is an ordinary char in this case, even inside { cmd; }
6086 * Pathological example: { ""}; } should exec "}" cmd
6087 */
Francis Laniel36836fc2023-12-22 22:02:28 +01006088#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01006089 if (ch == '}') {
Francis Laniel36836fc2023-12-22 22:02:28 +01006090#else /* __U_BOOT__ */
6091 if (ch == '}' || ch == ')') {
6092#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01006093 if (ctx.word.length != 0 /* word} */
6094 || ctx.word.has_quoted_part /* ""} */
6095 ) {
6096 goto ordinary_char;
6097 }
6098 if (!IS_NULL_CMD(ctx.command)) { /* cmd } */
6099 /* Generally, there should be semicolon: "cmd; }"
6100 * However, bash allows to omit it if "cmd" is
6101 * a group. Examples:
6102 * { { echo 1; } }
6103 * {(echo 1)}
6104 * { echo 0 >&2 | { echo 1; } }
6105 * { while false; do :; done }
6106 * { case a in b) ;; esac }
6107 */
6108 if (ctx.command->group)
6109 goto term_group;
6110 goto ordinary_char;
6111 }
6112 if (!IS_NULL_PIPE(ctx.pipe)) /* cmd | } */
6113 /* Can't be an end of {cmd}, skip the check */
6114 goto skip_end_trigger;
6115 /* else: } does terminate a group */
6116 }
6117 term_group:
6118 if (end_trigger && end_trigger == ch
6119 && (ch != ';' || heredoc_cnt == 0)
6120#if ENABLE_HUSH_CASE
6121 && (ch != ')'
6122 || ctx.ctx_res_w != RES_MATCH
6123 || (!ctx.word.has_quoted_part && strcmp(ctx.word.data, "esac") == 0)
6124 )
6125#endif
6126 ) {
6127 if (done_word(&ctx)) {
6128 goto parse_error_exitcode1;
6129 }
6130 done_pipe(&ctx, PIPE_SEQ);
6131 ctx.is_assignment = MAYBE_ASSIGNMENT;
6132 debug_printf_parse("ctx.is_assignment='%s'\n", assignment_flag[ctx.is_assignment]);
6133 /* Do we sit outside of any if's, loops or case's? */
6134 if (!HAS_KEYWORDS
6135 IF_HAS_KEYWORDS(|| (ctx.ctx_res_w == RES_NONE && ctx.old_flag == 0))
6136 ) {
6137 o_free_and_set_NULL(&ctx.word);
6138#if !BB_MMU
6139 debug_printf_parse("as_string2 '%s'\n", ctx.as_string.data);
6140 if (pstring)
6141 *pstring = ctx.as_string.data;
6142 else
6143 o_free(&ctx.as_string);
6144#endif
6145 if (ch != ';' && IS_NULL_PIPE(ctx.list_head)) {
6146 /* Example: bare "{ }", "()" */
6147 G.last_exitcode = 2; /* bash compat */
6148 syntax_error_unexpected_ch(ch);
6149 goto parse_error;
6150 }
6151 if (heredoc_cnt_ptr)
6152 *heredoc_cnt_ptr = heredoc_cnt;
6153 debug_printf_heredoc("parse_stream return heredoc_cnt:%d\n", heredoc_cnt);
6154 debug_printf_parse("parse_stream return %p: "
6155 "end_trigger char found\n",
6156 ctx.list_head);
6157 debug_leave();
6158 return ctx.list_head;
6159 }
6160 }
6161
6162 if (is_blank)
6163 continue;
6164
6165 /* Catch <, > before deciding whether this word is
6166 * an assignment. a=1 2>z b=2: b=2 is still assignment */
6167 switch (ch) {
Francis Laniel36836fc2023-12-22 22:02:28 +01006168#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01006169 case '>':
6170 redir_fd = redirect_opt_num(&ctx.word);
6171 if (done_word(&ctx)) {
6172 goto parse_error_exitcode1;
6173 }
6174 redir_style = REDIRECT_OVERWRITE;
6175 if (next == '>') {
6176 redir_style = REDIRECT_APPEND;
6177 ch = i_getch(input);
6178 nommu_addchr(&ctx.as_string, ch);
6179 }
6180#if 0
6181 else if (next == '(') {
6182 syntax_error(">(process) not supported");
6183 goto parse_error_exitcode1;
6184 }
6185#endif
6186 if (parse_redirect(&ctx, redir_fd, redir_style, input))
6187 goto parse_error_exitcode1;
6188 continue; /* get next char */
6189 case '<':
6190 redir_fd = redirect_opt_num(&ctx.word);
6191 if (done_word(&ctx)) {
6192 goto parse_error_exitcode1;
6193 }
6194 redir_style = REDIRECT_INPUT;
6195 if (next == '<') {
6196 redir_style = REDIRECT_HEREDOC;
6197 heredoc_cnt++;
6198 debug_printf_heredoc("++heredoc_cnt=%d\n", heredoc_cnt);
6199 ch = i_getch(input);
6200 nommu_addchr(&ctx.as_string, ch);
6201 } else if (next == '>') {
6202 redir_style = REDIRECT_IO;
6203 ch = i_getch(input);
6204 nommu_addchr(&ctx.as_string, ch);
6205 }
6206#if 0
6207 else if (next == '(') {
6208 syntax_error("<(process) not supported");
6209 goto parse_error_exitcode1;
6210 }
6211#endif
6212 if (parse_redirect(&ctx, redir_fd, redir_style, input))
6213 goto parse_error_exitcode1;
6214 continue; /* get next char */
Francis Lanielaa44c262023-12-22 22:02:38 +01006215#else /* __U_BOOT__ */
6216 /*
6217 * In U-Boot, '<' and '>' can be used in test command to test if
6218 * a string is, alphabetically, before or after another.
6219 * In 2021 Busybox hush, we will keep the same behavior and so not treat
6220 * them as redirection operator.
6221 *
6222 * Indeed, in U-Boot, tests are handled by the test command and not by the
6223 * shell code.
6224 * So, better to give this character as input to test command.
6225 *
6226 * NOTE In my opinion, when you use '<' or '>' I am almost sure
6227 * you wanted to use "-gt" or "-lt" in place, so thinking to
6228 * escape these will make you should check your code (sh syntax
6229 * at this level is, for me, error prone).
6230 */
6231 case '>':
6232 fallthrough;
6233 case '<':
6234 o_addQchr(&ctx.word, ch);
6235 continue;
6236#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01006237 case '#':
6238 if (ctx.word.length == 0 && !ctx.word.has_quoted_part) {
6239 /* skip "#comment" */
6240 /* note: we do not add it to &ctx.as_string */
6241/* TODO: in bash:
6242 * comment inside $() goes to the next \n, even inside quoted string (!):
6243 * cmd "$(cmd2 #comment)" - syntax error
6244 * cmd "`cmd2 #comment`" - ok
6245 * We accept both (comment ends where command subst ends, in both cases).
6246 */
6247 while (1) {
6248 ch = i_peek(input);
6249 if (ch == '\n') {
6250 nommu_addchr(&ctx.as_string, '\n');
6251 break;
6252 }
6253 ch = i_getch(input);
6254 if (ch == EOF)
6255 break;
6256 }
6257 continue; /* get next char */
6258 }
6259 break;
6260 }
6261 skip_end_trigger:
6262
6263 if (ctx.is_assignment == MAYBE_ASSIGNMENT
Francis Laniel36836fc2023-12-22 22:02:28 +01006264#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01006265 /* check that we are not in word in "a=1 2>word b=1": */
6266 && !ctx.pending_redirect
Francis Laniel36836fc2023-12-22 22:02:28 +01006267#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01006268 ) {
6269 /* ch is a special char and thus this word
6270 * cannot be an assignment */
6271 ctx.is_assignment = NOT_ASSIGNMENT;
6272 debug_printf_parse("ctx.is_assignment='%s'\n", assignment_flag[ctx.is_assignment]);
6273 }
6274
6275 /* Note: nommu_addchr(&ctx.as_string, ch) is already done */
6276
6277 switch (ch) {
6278 case_SPECIAL_VAR_SYMBOL:
6279 case SPECIAL_VAR_SYMBOL:
6280 /* Convert raw ^C to corresponding special variable reference */
6281 o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL);
6282 o_addchr(&ctx.word, SPECIAL_VAR_QUOTED_SVS);
6283 /* fall through */
6284 case '#':
6285 /* non-comment #: "echo a#b" etc */
6286 o_addchr(&ctx.word, ch);
6287 continue; /* get next char */
6288 case '$':
Francis Laniel36836fc2023-12-22 22:02:28 +01006289#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01006290 if (parse_dollar_squote(&ctx.as_string, &ctx.word, input))
6291 continue; /* get next char */
Francis Laniel36836fc2023-12-22 22:02:28 +01006292#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01006293 if (!parse_dollar(&ctx.as_string, &ctx.word, input, /*quote_mask:*/ 0)) {
6294 debug_printf_parse("parse_stream parse error: "
6295 "parse_dollar returned 0 (error)\n");
6296 goto parse_error_exitcode1;
6297 }
6298 continue; /* get next char */
6299 case '"':
6300 ctx.word.has_quoted_part = 1;
Francis Laniel36836fc2023-12-22 22:02:28 +01006301#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01006302 if (next == '"' && !ctx.pending_redirect) {
Francis Laniel36836fc2023-12-22 22:02:28 +01006303#else /* __U_BOOT__ */
6304 if (next == '"') {
6305#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01006306 i_getch(input); /* eat second " */
Francis Laniel36836fc2023-12-22 22:02:28 +01006307#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01006308 insert_empty_quoted_str_marker:
Francis Laniel36836fc2023-12-22 22:02:28 +01006309#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01006310 nommu_addchr(&ctx.as_string, next);
6311 o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL);
6312 o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL);
6313 continue; /* get next char */
6314 }
6315 if (ctx.is_assignment == NOT_ASSIGNMENT)
6316 ctx.word.o_expflags |= EXP_FLAG_ESC_GLOB_CHARS;
6317 if (!encode_string(&ctx.as_string, &ctx.word, input, '"'))
6318 goto parse_error_exitcode1;
6319 ctx.word.o_expflags &= ~EXP_FLAG_ESC_GLOB_CHARS;
6320 continue; /* get next char */
6321#if ENABLE_HUSH_TICK
6322 case '`': {
6323 USE_FOR_NOMMU(unsigned pos;)
6324
6325 o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL);
6326 o_addchr(&ctx.word, '`');
6327 USE_FOR_NOMMU(pos = ctx.word.length;)
6328 if (!add_till_backquote(&ctx.word, input, /*in_dquote:*/ 0))
6329 goto parse_error_exitcode1;
6330# if !BB_MMU
6331 o_addstr(&ctx.as_string, ctx.word.data + pos);
6332 o_addchr(&ctx.as_string, '`');
6333# endif
6334 o_addchr(&ctx.word, SPECIAL_VAR_SYMBOL);
6335 //debug_printf_subst("SUBST RES3 '%s'\n", ctx.word.data + pos);
6336 continue; /* get next char */
6337 }
6338#endif
6339 case ';':
6340#if ENABLE_HUSH_CASE
6341 case_semi:
6342#endif
6343 if (done_word(&ctx)) {
6344 goto parse_error_exitcode1;
6345 }
6346 done_pipe(&ctx, PIPE_SEQ);
6347#if ENABLE_HUSH_CASE
6348 /* Eat multiple semicolons, detect
6349 * whether it means something special */
6350 while (1) {
6351 ch = i_peek_and_eat_bkslash_nl(input);
6352 if (ch != ';')
6353 break;
6354 ch = i_getch(input);
6355 nommu_addchr(&ctx.as_string, ch);
6356 if (ctx.ctx_res_w == RES_CASE_BODY) {
6357 ctx.ctx_dsemicolon = 1;
6358 ctx.ctx_res_w = RES_MATCH;
6359 break;
6360 }
6361 }
6362#endif
6363 new_cmd:
6364 /* We just finished a cmd. New one may start
6365 * with an assignment */
6366 ctx.is_assignment = MAYBE_ASSIGNMENT;
6367 debug_printf_parse("ctx.is_assignment='%s'\n", assignment_flag[ctx.is_assignment]);
6368 continue; /* get next char */
6369 case '&':
6370 if (done_word(&ctx)) {
6371 goto parse_error_exitcode1;
6372 }
6373 if (next == '&') {
6374 ch = i_getch(input);
6375 nommu_addchr(&ctx.as_string, ch);
6376 done_pipe(&ctx, PIPE_AND);
6377 } else {
6378 done_pipe(&ctx, PIPE_BG);
6379 }
6380 goto new_cmd;
6381 case '|':
6382 if (done_word(&ctx)) {
6383 goto parse_error_exitcode1;
6384 }
6385#if ENABLE_HUSH_CASE
6386 if (ctx.ctx_res_w == RES_MATCH)
6387 break; /* we are in case's "word | word)" */
6388#endif
6389 if (next == '|') { /* || */
6390 ch = i_getch(input);
6391 nommu_addchr(&ctx.as_string, ch);
6392 done_pipe(&ctx, PIPE_OR);
6393 } else {
6394 /* we could pick up a file descriptor choice here
6395 * with redirect_opt_num(), but bash doesn't do it.
6396 * "echo foo 2| cat" yields "foo 2". */
6397 done_command(&ctx);
6398 }
6399 goto new_cmd;
6400 case '(':
6401#if ENABLE_HUSH_CASE
6402 /* "case... in [(]word)..." - skip '(' */
6403 if (ctx.ctx_res_w == RES_MATCH
6404 && ctx.command->argv == NULL /* not (word|(... */
6405 && ctx.word.length == 0 /* not word(... */
6406 && ctx.word.has_quoted_part == 0 /* not ""(... */
6407 ) {
6408 continue; /* get next char */
6409 }
6410#endif
6411 /* fall through */
6412 case '{': {
6413 int n = parse_group(&ctx, input, ch);
6414 if (n < 0) {
6415 goto parse_error_exitcode1;
6416 }
6417 debug_printf_heredoc("parse_group done, needs heredocs:%d\n", n);
6418 heredoc_cnt += n;
6419 goto new_cmd;
6420 }
6421 case ')':
6422#if ENABLE_HUSH_CASE
6423 if (ctx.ctx_res_w == RES_MATCH)
6424 goto case_semi;
6425#endif
6426 case '}':
6427 /* proper use of this character is caught by end_trigger:
6428 * if we see {, we call parse_group(..., end_trigger='}')
6429 * and it will match } earlier (not here). */
6430 G.last_exitcode = 2;
6431 syntax_error_unexpected_ch(ch);
6432 goto parse_error;
6433 default:
6434 if (HUSH_DEBUG)
6435 bb_error_msg_and_die("BUG: unexpected %c", ch);
6436 }
6437 } /* while (1) */
6438
6439 parse_error_exitcode1:
6440 G.last_exitcode = 1;
6441 parse_error:
6442 {
6443 struct parse_context *pctx;
6444 IF_HAS_KEYWORDS(struct parse_context *p2;)
6445
6446 /* Clean up allocated tree.
6447 * Sample for finding leaks on syntax error recovery path.
6448 * Run it from interactive shell, watch pmap `pidof hush`.
6449 * while if false; then false; fi; do break; fi
6450 * Samples to catch leaks at execution:
6451 * while if (true | { true;}); then echo ok; fi; do break; done
6452 * while if (true | { true;}); then echo ok; fi; do (if echo ok; break; then :; fi) | cat; break; done
6453 */
6454 pctx = &ctx;
6455 do {
6456 /* Update pipe/command counts,
6457 * otherwise freeing may miss some */
6458 done_pipe(pctx, PIPE_SEQ);
6459 debug_printf_clean("freeing list %p from ctx %p\n",
6460 pctx->list_head, pctx);
6461 debug_print_tree(pctx->list_head, 0);
6462 free_pipe_list(pctx->list_head);
6463 debug_printf_clean("freed list %p\n", pctx->list_head);
6464#if !BB_MMU
6465 o_free(&pctx->as_string);
6466#endif
6467 IF_HAS_KEYWORDS(p2 = pctx->stack;)
6468 if (pctx != &ctx) {
6469 free(pctx);
6470 }
6471 IF_HAS_KEYWORDS(pctx = p2;)
6472 } while (HAS_KEYWORDS && pctx);
6473
6474 o_free(&ctx.word);
6475#if !BB_MMU
6476 if (pstring)
6477 *pstring = NULL;
6478#endif
6479 debug_leave();
6480 return ERR_PTR;
6481 }
6482}
6483
Francis Laniel110b7692023-12-22 22:02:27 +01006484/*** Execution routines ***/
6485
6486/* Expansion can recurse, need forward decls: */
6487#if !BASH_PATTERN_SUBST && !ENABLE_HUSH_CASE
6488#define expand_string_to_string(str, EXP_flags, do_unbackslash) \
6489 expand_string_to_string(str)
6490#endif
6491static char *expand_string_to_string(const char *str, int EXP_flags, int do_unbackslash);
6492#if ENABLE_HUSH_TICK
6493static int process_command_subs(o_string *dest, const char *s);
6494#endif
6495static int expand_vars_to_list(o_string *output, int n, char *arg);
6496
6497/* expand_strvec_to_strvec() takes a list of strings, expands
6498 * all variable references within and returns a pointer to
6499 * a list of expanded strings, possibly with larger number
6500 * of strings. (Think VAR="a b"; echo $VAR).
6501 * This new list is allocated as a single malloc block.
6502 * NULL-terminated list of char* pointers is at the beginning of it,
6503 * followed by strings themselves.
6504 * Caller can deallocate entire list by single free(list). */
6505
6506/* A horde of its helpers come first: */
6507
6508static void o_addblock_duplicate_backslash(o_string *o, const char *str, int len)
6509{
6510 while (--len >= 0) {
6511 char c = *str++;
6512
6513#if ENABLE_HUSH_BRACE_EXPANSION
6514 if (c == '{' || c == '}') {
6515 /* { -> \{, } -> \} */
6516 o_addchr(o, '\\');
6517 /* And now we want to add { or } and continue:
6518 * o_addchr(o, c);
6519 * continue;
6520 * luckily, just falling through achieves this.
6521 */
6522 }
6523#endif
6524 o_addchr(o, c);
6525 if (c == '\\') {
6526 /* \z -> \\\z; \<eol> -> \\<eol> */
6527 o_addchr(o, '\\');
6528 if (len) {
6529 len--;
6530 o_addchr(o, '\\');
6531 o_addchr(o, *str++);
6532 }
6533 }
6534 }
6535}
6536
6537/* Store given string, finalizing the word and starting new one whenever
6538 * we encounter IFS char(s). This is used for expanding variable values.
6539 * End-of-string does NOT finalize word: think about 'echo -$VAR-'.
6540 * Return in output->ended_in_ifs:
6541 * 1 - ended with IFS char, else 0 (this includes case of empty str).
6542 */
6543static int expand_on_ifs(o_string *output, int n, const char *str)
6544{
6545 int last_is_ifs = 0;
6546
6547 while (1) {
6548 int word_len;
6549
6550 if (!*str) /* EOL - do not finalize word */
6551 break;
6552 word_len = strcspn(str, G.ifs);
6553 if (word_len) {
6554 /* We have WORD_LEN leading non-IFS chars */
6555 if (!(output->o_expflags & EXP_FLAG_GLOB)) {
6556 o_addblock(output, str, word_len);
6557 } else {
6558 /* Protect backslashes against globbing up :)
6559 * Example: "v='\*'; echo b$v" prints "b\*"
6560 * (and does not try to glob on "*")
6561 */
6562 o_addblock_duplicate_backslash(output, str, word_len);
6563 /*/ Why can't we do it easier? */
6564 /*o_addblock(output, str, word_len); - WRONG: "v='\*'; echo Z$v" prints "Z*" instead of "Z\*" */
6565 /*o_addqblock(output, str, word_len); - WRONG: "v='*'; echo Z$v" prints "Z*" instead of Z* files */
6566 }
6567 last_is_ifs = 0;
6568 str += word_len;
6569 if (!*str) /* EOL - do not finalize word */
6570 break;
6571 }
6572
6573 /* We know str here points to at least one IFS char */
6574 last_is_ifs = 1;
6575 str += strspn(str, G.ifs_whitespace); /* skip IFS whitespace chars */
6576 if (!*str) /* EOL - do not finalize word */
6577 break;
6578
6579 if (G.ifs_whitespace != G.ifs /* usually false ($IFS is usually all whitespace), */
6580 && strchr(G.ifs, *str) /* the second check would fail */
6581 ) {
6582 /* This is a non-whitespace $IFS char */
6583 /* Skip it and IFS whitespace chars, start new word */
6584 str++;
6585 str += strspn(str, G.ifs_whitespace);
6586 goto new_word;
6587 }
6588
6589 /* Start new word... but not always! */
6590 /* Case "v=' a'; echo ''$v": we do need to finalize empty word: */
6591 if (output->has_quoted_part
6592 /*
6593 * Case "v=' a'; echo $v":
6594 * here nothing precedes the space in $v expansion,
6595 * therefore we should not finish the word
6596 * (IOW: if there *is* word to finalize, only then do it):
6597 * It's okay if this accesses the byte before first argv[]:
6598 * past call to o_save_ptr() cleared it to zero byte
6599 * (grep for -prev-ifs-check-).
6600 */
6601 || output->data[output->length - 1]
6602 ) {
6603 new_word:
6604 o_addchr(output, '\0');
6605 debug_print_list("expand_on_ifs", output, n);
6606 n = o_save_ptr(output, n);
6607 }
6608 }
6609
6610 output->ended_in_ifs = last_is_ifs;
6611 debug_print_list("expand_on_ifs[1]", output, n);
6612 return n;
6613}
6614
Francis Laniel36836fc2023-12-22 22:02:28 +01006615#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01006616/* Helper to expand $((...)) and heredoc body. These act as if
6617 * they are in double quotes, with the exception that they are not :).
6618 * Just the rules are similar: "expand only $var and `cmd`"
6619 *
6620 * Returns malloced string.
6621 * As an optimization, we return NULL if expansion is not needed.
6622 */
6623static char *encode_then_expand_string(const char *str)
6624{
6625 char *exp_str;
6626 struct in_str input;
6627 o_string dest = NULL_O_STRING;
6628 const char *cp;
6629
6630 cp = str;
6631 for (;;) {
6632 if (!*cp) return NULL; /* string has no special chars */
6633 if (*cp == '$') break;
6634 if (*cp == '\\') break;
6635#if ENABLE_HUSH_TICK
6636 if (*cp == '`') break;
6637#endif
6638 cp++;
6639 }
6640
6641 /* We need to expand. Example:
6642 * echo $(($a + `echo 1`)) $((1 + $((2)) ))
6643 */
6644 setup_string_in_str(&input, str);
6645 encode_string(NULL, &dest, &input, EOF);
6646//TODO: error check (encode_string returns 0 on error)?
6647 //bb_error_msg("'%s' -> '%s'", str, dest.data);
6648 exp_str = expand_string_to_string(dest.data,
6649 EXP_FLAG_ESC_GLOB_CHARS,
6650 /*unbackslash:*/ 1
6651 );
6652 //bb_error_msg("'%s' -> '%s'", dest.data, exp_str);
6653 o_free(&dest);
6654 return exp_str;
6655}
6656
6657static const char *first_special_char_in_vararg(const char *cp)
6658{
6659 for (;;) {
6660 if (!*cp) return NULL; /* string has no special chars */
6661 if (*cp == '$') return cp;
6662 if (*cp == '\\') return cp;
6663 if (*cp == '\'') return cp;
6664 if (*cp == '"') return cp;
6665#if ENABLE_HUSH_TICK
6666 if (*cp == '`') return cp;
6667#endif
6668 /* dquoted "${x:+ARG}" should not glob, therefore
6669 * '*' et al require some non-literal processing: */
6670 if (*cp == '*') return cp;
6671 if (*cp == '?') return cp;
6672 if (*cp == '[') return cp;
6673 cp++;
6674 }
6675}
6676
6677/* Expanding ARG in ${var#ARG}, ${var%ARG}, or ${var/ARG/ARG}.
6678 * These can contain single- and double-quoted strings,
6679 * and treated as if the ARG string is initially unquoted. IOW:
6680 * ${var#ARG} and "${var#ARG}" treat ARG the same (ARG can even be
6681 * a dquoted string: "${var#"zz"}"), the difference only comes later
6682 * (word splitting and globbing of the ${var...} result).
6683 */
6684#if !BASH_PATTERN_SUBST
6685#define encode_then_expand_vararg(str, handle_squotes, do_unbackslash) \
6686 encode_then_expand_vararg(str, handle_squotes)
6687#endif
6688static char *encode_then_expand_vararg(const char *str, int handle_squotes, int do_unbackslash)
6689{
6690#if !BASH_PATTERN_SUBST && ENABLE_HUSH_CASE
6691 const int do_unbackslash = 0;
6692#endif
6693 char *exp_str;
6694 struct in_str input;
6695 o_string dest = NULL_O_STRING;
6696
6697 if (!first_special_char_in_vararg(str)) {
6698 /* string has no special chars */
6699 return NULL;
6700 }
6701
6702 setup_string_in_str(&input, str);
6703 dest.data = xzalloc(1); /* start as "", not as NULL */
6704 exp_str = NULL;
6705
6706 for (;;) {
6707 int ch;
6708
6709 ch = i_getch(&input);
6710 debug_printf_parse("%s: ch=%c (%d) escape=%d\n",
6711 __func__, ch, ch, !!dest.o_expflags);
6712
6713 if (!dest.o_expflags) {
6714 if (ch == EOF)
6715 break;
6716 if (handle_squotes && ch == '\'') {
6717 if (!add_till_single_quote_dquoted(&dest, &input))
6718 goto ret; /* error */
6719 continue;
6720 }
6721 }
6722 if (ch == EOF) {
6723 syntax_error_unterm_ch('"');
6724 goto ret; /* error */
6725 }
6726 if (ch == '"') {
6727 dest.o_expflags ^= EXP_FLAG_ESC_GLOB_CHARS;
6728 continue;
6729 }
6730 if (ch == '\\') {
6731 ch = i_getch(&input);
6732 if (ch == EOF) {
6733//example? error message? syntax_error_unterm_ch('"');
6734 debug_printf_parse("%s: error: \\<eof>\n", __func__);
6735 goto ret;
6736 }
6737 o_addqchr(&dest, ch);
6738 continue;
6739 }
6740 if (ch == '$') {
6741 if (parse_dollar_squote(NULL, &dest, &input))
6742 continue;
6743 if (!parse_dollar(NULL, &dest, &input, /*quote_mask:*/ 0x80)) {
6744 debug_printf_parse("%s: error: parse_dollar returned 0 (error)\n", __func__);
6745 goto ret;
6746 }
6747 continue;
6748 }
6749#if ENABLE_HUSH_TICK
6750 if (ch == '`') {
6751 //unsigned pos = dest->length;
6752 o_addchr(&dest, SPECIAL_VAR_SYMBOL);
6753 o_addchr(&dest, 0x80 | '`');
6754 if (!add_till_backquote(&dest, &input,
6755 /*in_dquote:*/ dest.o_expflags /* nonzero if EXP_FLAG_ESC_GLOB_CHARS set */
6756 )
6757 ) {
6758 goto ret; /* error */
6759 }
6760 o_addchr(&dest, SPECIAL_VAR_SYMBOL);
6761 //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos);
6762 continue;
6763 }
6764#endif
6765 o_addQchr(&dest, ch);
6766 } /* for (;;) */
6767
6768 debug_printf_parse("encode: '%s' -> '%s'\n", str, dest.data);
6769 exp_str = expand_string_to_string(dest.data,
6770 do_unbackslash ? EXP_FLAG_ESC_GLOB_CHARS : 0,
6771 do_unbackslash
6772 );
6773 ret:
6774 debug_printf_parse("expand: '%s' -> '%s'\n", dest.data, exp_str);
6775 o_free(&dest);
6776 return exp_str;
6777}
6778
6779/* Expanding ARG in ${var+ARG}, ${var-ARG}
6780 */
Francis Laniele7ca3a32023-12-22 22:02:42 +01006781static NOINLINE int encode_then_append_var_plusminus(o_string *output, int n,
Francis Laniel110b7692023-12-22 22:02:27 +01006782 char *str, int dquoted)
6783{
6784 struct in_str input;
6785 o_string dest = NULL_O_STRING;
6786
6787 if (!first_special_char_in_vararg(str)
6788 && '\0' == str[strcspn(str, G.ifs)]
6789 ) {
6790 /* string has no special chars
6791 * && string has no $IFS chars
6792 */
6793 if (dquoted) {
6794 /* Prints 1 (quoted expansion is a "" word, not nothing):
6795 * set -- "${notexist-}"; echo $#
6796 */
6797 output->has_quoted_part = 1;
6798 }
6799 return expand_vars_to_list(output, n, str);
6800 }
6801
6802 setup_string_in_str(&input, str);
6803
6804 for (;;) {
6805 int ch;
6806
6807 ch = i_getch(&input);
6808 debug_printf_parse("%s: ch=%c (%d) escape=%x\n",
6809 __func__, ch, ch, dest.o_expflags);
6810
6811 if (!dest.o_expflags) {
6812 if (ch == EOF)
6813 break;
Francis Laniele7ca3a32023-12-22 22:02:42 +01006814 if (!dquoted && !(output->o_expflags & EXP_FLAG_SINGLEWORD) && strchr(G.ifs, ch)) {
Francis Laniel110b7692023-12-22 22:02:27 +01006815 /* PREFIX${x:d${e}f ...} and we met space: expand "d${e}f" and start new word.
6816 * do not assume we are at the start of the word (PREFIX above).
6817 */
6818 if (dest.data) {
6819 n = expand_vars_to_list(output, n, dest.data);
6820 o_free_and_set_NULL(&dest);
6821 o_addchr(output, '\0');
6822 n = o_save_ptr(output, n); /* create next word */
6823 } else
6824 if (output->length != o_get_last_ptr(output, n)
6825 || output->has_quoted_part
6826 ) {
6827 /* For these cases:
6828 * f() { for i; do echo "|$i|"; done; }; x=x
6829 * f a${x:+ }b # 1st condition
6830 * |a|
6831 * |b|
6832 * f ""${x:+ }b # 2nd condition
6833 * ||
6834 * |b|
6835 */
6836 o_addchr(output, '\0');
6837 n = o_save_ptr(output, n); /* create next word */
6838 }
6839 continue;
6840 }
6841 if (!dquoted && ch == '\'') {
6842 if (!add_till_single_quote_dquoted(&dest, &input))
6843 goto ret; /* error */
6844 o_addchr(&dest, SPECIAL_VAR_SYMBOL);
6845 o_addchr(&dest, SPECIAL_VAR_SYMBOL);
6846 continue;
6847 }
6848 }
6849 if (ch == EOF) {
6850 syntax_error_unterm_ch('"');
6851 goto ret; /* error */
6852 }
6853 if (ch == '"') {
6854 dest.o_expflags ^= EXP_FLAG_ESC_GLOB_CHARS;
6855 if (dest.o_expflags) {
6856 o_addchr(&dest, SPECIAL_VAR_SYMBOL);
6857 o_addchr(&dest, SPECIAL_VAR_SYMBOL);
6858 }
6859 continue;
6860 }
6861 if (ch == '\\') {
6862 ch = i_getch(&input);
6863 if (ch == EOF) {
6864//example? error message? syntax_error_unterm_ch('"');
6865 debug_printf_parse("%s: error: \\<eof>\n", __func__);
6866 goto ret;
6867 }
6868 o_addqchr(&dest, ch);
6869 continue;
6870 }
6871 if (ch == '$') {
6872 if (!parse_dollar(NULL, &dest, &input, /*quote_mask:*/ (dest.o_expflags || dquoted) ? 0x80 : 0)) {
6873 debug_printf_parse("%s: error: parse_dollar returned 0 (error)\n", __func__);
6874 goto ret;
6875 }
6876 continue;
6877 }
6878#if ENABLE_HUSH_TICK
6879 if (ch == '`') {
6880 //unsigned pos = dest->length;
6881 o_addchr(&dest, SPECIAL_VAR_SYMBOL);
6882 o_addchr(&dest, (dest.o_expflags || dquoted) ? 0x80 | '`' : '`');
6883 if (!add_till_backquote(&dest, &input,
6884 /*in_dquote:*/ dest.o_expflags /* nonzero if EXP_FLAG_ESC_GLOB_CHARS set */
6885 )
6886 ) {
6887 goto ret; /* error */
6888 }
6889 o_addchr(&dest, SPECIAL_VAR_SYMBOL);
6890 //debug_printf_subst("SUBST RES3 '%s'\n", dest->data + pos);
6891 continue;
6892 }
6893#endif
6894 if (dquoted) {
6895 /* Always glob-protect if in dquotes:
6896 * x=x; echo "${x:+/bin/c*}" - prints: /bin/c*
6897 * x=x; echo "${x:+"/bin/c*"}" - prints: /bin/c*
6898 */
6899 o_addqchr(&dest, ch);
6900 } else {
6901 /* Glob-protect only if char is quoted:
6902 * x=x; echo ${x:+/bin/c*} - prints many filenames
6903 * x=x; echo ${x:+"/bin/c*"} - prints: /bin/c*
6904 */
6905 o_addQchr(&dest, ch);
6906 }
6907 } /* for (;;) */
6908
6909 if (dest.data) {
6910 n = expand_vars_to_list(output, n, dest.data);
6911 }
6912 ret:
6913 o_free(&dest);
6914 return n;
6915}
Francis Laniel36836fc2023-12-22 22:02:28 +01006916#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01006917
Francis Laniel36836fc2023-12-22 22:02:28 +01006918#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01006919#if ENABLE_FEATURE_SH_MATH
6920static arith_t expand_and_evaluate_arith(const char *arg, const char **errmsg_p)
6921{
6922 arith_state_t math_state;
6923 arith_t res;
6924 char *exp_str;
6925
6926 math_state.lookupvar = get_local_var_value;
6927 math_state.setvar = set_local_var_from_halves;
6928 //math_state.endofname = endofname;
6929 exp_str = encode_then_expand_string(arg);
6930 res = arith(&math_state, exp_str ? exp_str : arg);
6931 free(exp_str);
6932 if (errmsg_p)
6933 *errmsg_p = math_state.errmsg;
6934 if (math_state.errmsg)
6935 msg_and_die_if_script(math_state.errmsg);
6936 return res;
6937}
6938#endif
Francis Laniel36836fc2023-12-22 22:02:28 +01006939#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01006940
Francis Laniel36836fc2023-12-22 22:02:28 +01006941#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01006942#if BASH_PATTERN_SUBST
6943/* ${var/[/]pattern[/repl]} helpers */
6944static char *strstr_pattern(char *val, const char *pattern, int *size)
6945{
Francis Laniele7ca3a32023-12-22 22:02:42 +01006946 int first_escaped = (pattern[0] == '\\' && pattern[1]);
6947 /* "first_escaped" trick allows to treat e.g. "\*no_glob_chars"
6948 * as literal too (as it is semi-common, and easy to accomodate
6949 * by just using str + 1).
6950 */
6951 int sz = strcspn(pattern + first_escaped * 2, "*?[\\");
6952 if ((pattern + first_escaped * 2)[sz] == '\0') {
Francis Laniel110b7692023-12-22 22:02:27 +01006953 /* Optimization for trivial patterns.
6954 * Testcase for very slow replace (performs about 22k replaces):
6955 * x=::::::::::::::::::::::
6956 * x=$x$x;x=$x$x;x=$x$x;x=$x$x;x=$x$x;x=$x$x;x=$x$x;x=$x$x;x=$x$x;x=$x$x;echo ${#x}
6957 * echo "${x//:/|}"
6958 */
Francis Laniele7ca3a32023-12-22 22:02:42 +01006959 *size = sz + first_escaped;
6960 return strstr(val, pattern + first_escaped);
Francis Laniel110b7692023-12-22 22:02:27 +01006961 }
6962
6963 while (1) {
6964 char *end = scan_and_match(val, pattern, SCAN_MOVE_FROM_RIGHT + SCAN_MATCH_LEFT_HALF);
6965 debug_printf_varexp("val:'%s' pattern:'%s' end:'%s'\n", val, pattern, end);
6966 if (end) {
6967 *size = end - val;
6968 return val;
6969 }
6970 if (*val == '\0')
6971 return NULL;
6972 /* Optimization: if "*pat" did not match the start of "string",
6973 * we know that "tring", "ring" etc will not match too:
6974 */
6975 if (pattern[0] == '*')
6976 return NULL;
6977 val++;
6978 }
6979}
6980static char *replace_pattern(char *val, const char *pattern, const char *repl, char exp_op)
6981{
6982 char *result = NULL;
6983 unsigned res_len = 0;
6984 unsigned repl_len = strlen(repl);
6985
6986 /* Null pattern never matches, including if "var" is empty */
6987 if (!pattern[0])
6988 return result; /* NULL, no replaces happened */
6989
6990 while (1) {
6991 int size;
6992 char *s = strstr_pattern(val, pattern, &size);
6993 if (!s)
6994 break;
6995
6996 result = xrealloc(result, res_len + (s - val) + repl_len + 1);
6997 strcpy(mempcpy(result + res_len, val, s - val), repl);
6998 res_len += (s - val) + repl_len;
6999 debug_printf_varexp("val:'%s' s:'%s' result:'%s'\n", val, s, result);
7000
7001 val = s + size;
7002 if (exp_op == '/')
7003 break;
7004 }
7005 if (*val && result) {
7006 result = xrealloc(result, res_len + strlen(val) + 1);
7007 strcpy(result + res_len, val);
7008 debug_printf_varexp("val:'%s' result:'%s'\n", val, result);
7009 }
7010 debug_printf_varexp("result:'%s'\n", result);
7011 return result;
7012}
7013#endif /* BASH_PATTERN_SUBST */
Francis Laniel36836fc2023-12-22 22:02:28 +01007014#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01007015
7016static int append_str_maybe_ifs_split(o_string *output, int n,
7017 int first_ch, const char *val)
7018{
7019 if (!(first_ch & 0x80)) { /* unquoted $VAR */
7020 debug_printf_expand("unquoted '%s', output->o_escape:%d\n", val,
7021 !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS));
7022 if (val && val[0])
7023 n = expand_on_ifs(output, n, val);
7024 } else { /* quoted "$VAR" */
7025 output->has_quoted_part = 1;
7026 debug_printf_expand("quoted '%s', output->o_escape:%d\n", val,
7027 !!(output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS));
7028 if (val && val[0])
7029 o_addQstr(output, val);
7030 }
7031 return n;
7032}
7033
7034/* Handle <SPECIAL_VAR_SYMBOL>varname...<SPECIAL_VAR_SYMBOL> construct.
7035 */
7036static NOINLINE int expand_one_var(o_string *output, int n,
7037 int first_ch, char *arg, char **pp)
7038{
7039 const char *val;
7040 char *to_be_freed;
7041 char *p;
7042 char *var;
7043 char exp_op;
7044 char exp_save = exp_save; /* for compiler */
7045 char *exp_saveptr; /* points to expansion operator */
7046 char *exp_word = exp_word; /* for compiler */
7047 char arg0;
7048
7049 val = NULL;
7050 to_be_freed = NULL;
7051 p = *pp;
7052 *p = '\0'; /* replace trailing SPECIAL_VAR_SYMBOL */
7053 var = arg;
7054 exp_saveptr = arg[1] ? strchr(VAR_ENCODED_SUBST_OPS, arg[1]) : NULL;
7055 arg0 = arg[0];
7056 arg[0] = (arg0 & 0x7f);
7057 exp_op = 0;
7058
7059 if (arg[0] == '#' && arg[1] /* ${#...} but not ${#} */
7060 && (!exp_saveptr /* and ( not(${#<op_char>...}) */
7061 || (arg[2] == '\0' && strchr(SPECIAL_VARS_STR, arg[1])) /* or ${#C} "len of $C" ) */
7062 ) /* NB: skipping ^^^specvar check mishandles ${#::2} */
7063 ) {
7064 /* It must be length operator: ${#var} */
7065 var++;
7066 exp_op = 'L';
7067 } else {
7068 /* Maybe handle parameter expansion */
7069 if (exp_saveptr /* if 2nd char is one of expansion operators */
7070 && strchr(NUMERIC_SPECVARS_STR, arg[0]) /* 1st char is special variable */
7071 ) {
7072 /* ${?:0}, ${#[:]%0} etc */
7073 exp_saveptr = var + 1;
7074 } else {
7075 /* ${?}, ${var}, ${var:0}, ${var[:]%0} etc */
7076 exp_saveptr = var+1 + strcspn(var+1, VAR_ENCODED_SUBST_OPS);
7077 }
7078 exp_op = exp_save = *exp_saveptr;
Francis Laniel36836fc2023-12-22 22:02:28 +01007079#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01007080 if (exp_op) {
7081 exp_word = exp_saveptr + 1;
7082 if (exp_op == ':') {
7083 exp_op = *exp_word++;
7084//TODO: try ${var:} and ${var:bogus} in non-bash config
7085 if (BASH_SUBSTR
7086 && (!exp_op || !strchr(MINUS_PLUS_EQUAL_QUESTION, exp_op))
7087 ) {
7088 /* oops... it's ${var:N[:M]}, not ${var:?xxx} or some such */
7089 exp_op = ':';
7090 exp_word--;
7091 }
7092 }
7093 *exp_saveptr = '\0';
7094 } /* else: it's not an expansion op, but bare ${var} */
Francis Laniel36836fc2023-12-22 22:02:28 +01007095#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01007096 }
7097
7098 /* Look up the variable in question */
7099 if (isdigit(var[0])) {
7100 /* parse_dollar should have vetted var for us */
Francis Laniel36836fc2023-12-22 22:02:28 +01007101#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01007102 int nn = xatoi_positive(var);
Francis Laniel36836fc2023-12-22 22:02:28 +01007103#else /* __U_BOOT__ */
7104 int nn = simple_strtoul(var, NULL, 10);
7105#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01007106 if (nn < G.global_argc)
7107 val = G.global_argv[nn];
7108 /* else val remains NULL: $N with too big N */
7109 } else {
7110 switch (var[0]) {
Francis Laniel36836fc2023-12-22 22:02:28 +01007111#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01007112 case '$': /* pid */
7113 val = utoa(G.root_pid);
7114 break;
7115 case '!': /* bg pid */
7116 val = G.last_bg_pid ? utoa(G.last_bg_pid) : "";
7117 break;
Francis Laniel36836fc2023-12-22 22:02:28 +01007118#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01007119 case '?': /* exitcode */
7120 val = utoa(G.last_exitcode);
7121 break;
7122 case '#': /* argc */
7123 val = utoa(G.global_argc ? G.global_argc-1 : 0);
7124 break;
Francis Laniel36836fc2023-12-22 22:02:28 +01007125#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01007126 case '-': { /* active options */
7127 /* Check set_mode() to see what option chars we support */
7128 char *cp;
7129 val = cp = G.optstring_buf;
7130 if (G.o_opt[OPT_O_ERREXIT])
7131 *cp++ = 'e';
7132 if (G_interactive_fd)
7133 *cp++ = 'i';
7134 if (G_x_mode)
7135 *cp++ = 'x';
7136 /* If G.o_opt[OPT_O_NOEXEC] is true,
7137 * commands read but are not executed,
7138 * so $- can not execute too, 'n' is never seen in $-.
7139 */
7140 if (G.opt_c)
7141 *cp++ = 'c';
7142 if (G.opt_s)
7143 *cp++ = 's';
7144 *cp = '\0';
7145 break;
7146 }
Francis Laniel36836fc2023-12-22 22:02:28 +01007147#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01007148 default:
Francis Lanielbfc406a2023-12-22 22:02:33 +01007149#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01007150 val = get_local_var_value(var);
Francis Lanielbfc406a2023-12-22 22:02:33 +01007151#else /* __U_BOOT__ */
7152 /*
7153 * Environment variable set with setenv* have to be
7154 * expanded.
7155 * So, we first search if the variable exists in
7156 * environment, if this is not the case, we default to
7157 * local value.
7158 */
7159 val = env_get(var);
7160 if (!val)
7161 val = get_local_var_value(var);
7162#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01007163 }
7164 }
7165
Francis Laniel36836fc2023-12-22 22:02:28 +01007166#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01007167 /* Handle any expansions */
7168 if (exp_op == 'L') {
7169 reinit_unicode_for_hush();
7170 debug_printf_expand("expand: length(%s)=", val);
7171 val = utoa(val ? unicode_strlen(val) : 0);
7172 debug_printf_expand("%s\n", val);
7173 } else if (exp_op) {
7174 if (exp_op == '%' || exp_op == '#') {
7175 /* Standard-mandated substring removal ops:
7176 * ${parameter%word} - remove smallest suffix pattern
7177 * ${parameter%%word} - remove largest suffix pattern
7178 * ${parameter#word} - remove smallest prefix pattern
7179 * ${parameter##word} - remove largest prefix pattern
7180 *
7181 * Word is expanded to produce a glob pattern.
7182 * Then var's value is matched to it and matching part removed.
7183 */
7184 /* bash compat: if x is "" and no shrinking of it is possible,
7185 * inner ${...} is not evaluated. Example:
7186 * unset b; : ${a%${b=B}}; echo $b
7187 * assignment b=B only happens if $a is not "".
7188 */
7189 if (val && val[0]) {
7190 char *t;
7191 char *exp_exp_word;
7192 char *loc;
7193 unsigned scan_flags = pick_scan(exp_op, *exp_word);
7194 if (exp_op == *exp_word) /* ## or %% */
7195 exp_word++;
7196 debug_printf_expand("expand: exp_word:'%s'\n", exp_word);
7197 exp_exp_word = encode_then_expand_vararg(exp_word, /*handle_squotes:*/ 1, /*unbackslash:*/ 0);
7198 if (exp_exp_word)
7199 exp_word = exp_exp_word;
7200 debug_printf_expand("expand: exp_word:'%s'\n", exp_word);
7201 /*
7202 * HACK ALERT. We depend here on the fact that
7203 * G.global_argv and results of utoa and get_local_var_value
7204 * are actually in writable memory:
7205 * scan_and_match momentarily stores NULs there.
7206 */
7207 t = (char*)val;
7208 loc = scan_and_match(t, exp_word, scan_flags);
7209 debug_printf_expand("op:%c str:'%s' pat:'%s' res:'%s'\n", exp_op, t, exp_word, loc);
7210 free(exp_exp_word);
7211 if (loc) { /* match was found */
7212 if (scan_flags & SCAN_MATCH_LEFT_HALF) /* #[#] */
7213 val = loc; /* take right part */
7214 else /* %[%] */
7215 val = to_be_freed = xstrndup(val, loc - val); /* left */
7216 }
7217 }
7218 }
7219#if BASH_PATTERN_SUBST
7220 else if (exp_op == '/' || exp_op == '\\') {
7221 /* It's ${var/[/]pattern[/repl]} thing.
7222 * Note that in encoded form it has TWO parts:
7223 * var/pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL>
7224 * and if // is used, it is encoded as \:
7225 * var\pattern<SPECIAL_VAR_SYMBOL>repl<SPECIAL_VAR_SYMBOL>
7226 */
7227 /* bash compat: if var is "", both pattern and repl
7228 * are still evaluated, if it is unset, then not:
7229 * unset b; a=; : ${a/z/${b=3}}; echo $b # b=3
7230 * unset b; unset a; : ${a/z/${b=3}}; echo $b # b not set
7231 */
7232 if (val /*&& val[0]*/) {
7233 /* pattern uses non-standard expansion.
7234 * repl should be unbackslashed and globbed
7235 * by the usual expansion rules:
7236 * >az >bz
7237 * v='a bz'; echo "${v/a*z/a*z}" #prints "a*z"
7238 * v='a bz'; echo "${v/a*z/\z}" #prints "z"
7239 * v='a bz'; echo ${v/a*z/a*z} #prints "az"
7240 * v='a bz'; echo ${v/a*z/\z} #prints "z"
7241 * (note that a*z _pattern_ is never globbed!)
7242 */
7243 char *pattern, *repl, *t;
7244 pattern = encode_then_expand_vararg(exp_word, /*handle_squotes:*/ 1, /*unbackslash:*/ 0);
7245 if (!pattern)
7246 pattern = xstrdup(exp_word);
7247 debug_printf_varexp("pattern:'%s'->'%s'\n", exp_word, pattern);
7248 *p++ = SPECIAL_VAR_SYMBOL;
7249 exp_word = p;
7250 p = strchr(p, SPECIAL_VAR_SYMBOL);
7251 *p = '\0';
7252 repl = encode_then_expand_vararg(exp_word, /*handle_squotes:*/ 1, /*unbackslash:*/ 1);
7253 debug_printf_varexp("repl:'%s'->'%s'\n", exp_word, repl);
7254 /* HACK ALERT. We depend here on the fact that
7255 * G.global_argv and results of utoa and get_local_var_value
7256 * are actually in writable memory:
7257 * replace_pattern momentarily stores NULs there. */
7258 t = (char*)val;
7259 to_be_freed = replace_pattern(t,
7260 pattern,
7261 (repl ? repl : exp_word),
7262 exp_op);
7263 if (to_be_freed) /* at least one replace happened */
7264 val = to_be_freed;
7265 free(pattern);
7266 free(repl);
7267 } else {
7268 /* Unset variable always gives nothing */
7269 // a=; echo ${a/*/w} # "w"
7270 // unset a; echo ${a/*/w} # ""
7271 /* Just skip "replace" part */
7272 *p++ = SPECIAL_VAR_SYMBOL;
7273 p = strchr(p, SPECIAL_VAR_SYMBOL);
7274 *p = '\0';
7275 }
7276 }
7277#endif /* BASH_PATTERN_SUBST */
7278 else if (exp_op == ':') {
7279#if BASH_SUBSTR && ENABLE_FEATURE_SH_MATH
7280 /* It's ${var:N[:M]} bashism.
7281 * Note that in encoded form it has TWO parts:
7282 * var:N<SPECIAL_VAR_SYMBOL>M<SPECIAL_VAR_SYMBOL>
7283 */
7284 arith_t beg, len;
7285 unsigned vallen;
7286 const char *errmsg;
7287
7288 beg = expand_and_evaluate_arith(exp_word, &errmsg);
7289 if (errmsg)
7290 goto empty_result;
7291 debug_printf_varexp("beg:'%s'=%lld\n", exp_word, (long long)beg);
7292 *p++ = SPECIAL_VAR_SYMBOL;
7293 exp_word = p;
7294 p = strchr(p, SPECIAL_VAR_SYMBOL);
7295 *p = '\0';
7296 vallen = val ? strlen(val) : 0;
7297 if (beg < 0) {
7298 /* negative beg counts from the end */
7299 beg = (arith_t)vallen + beg;
7300 }
7301 /* If expansion will be empty, do not even evaluate len */
7302 if (!val || beg < 0 || beg > vallen) {
7303 /* Why > vallen, not >=? bash:
7304 * unset b; a=ab; : ${a:2:${b=3}}; echo $b # "", b=3 (!!!)
7305 * unset b; a=a; : ${a:2:${b=3}}; echo $b # "", b not set
7306 */
7307 goto empty_result;
7308 }
7309 len = expand_and_evaluate_arith(exp_word, &errmsg);
7310 if (errmsg)
7311 goto empty_result;
7312 debug_printf_varexp("len:'%s'=%lld\n", exp_word, (long long)len);
7313 debug_printf_varexp("from val:'%s'\n", val);
7314 if (len < 0) {
7315 /* in bash, len=-n means strlen()-n */
7316 len = (arith_t)vallen - beg + len;
7317 if (len < 0) /* bash compat */
7318 msg_and_die_if_script("%s: substring expression < 0", var);
7319 }
7320 if (len <= 0 || !val /*|| beg >= vallen*/) {
7321 empty_result:
7322 val = NULL;
7323 } else {
7324 /* Paranoia. What if user entered 9999999999999
7325 * which fits in arith_t but not int? */
7326 if (len > INT_MAX)
7327 len = INT_MAX;
7328 val = to_be_freed = xstrndup(val + beg, len);
7329 }
7330 debug_printf_varexp("val:'%s'\n", val);
7331#else /* not (HUSH_SUBSTR_EXPANSION && FEATURE_SH_MATH) */
7332 msg_and_die_if_script("malformed ${%s:...}", var);
7333 val = NULL;
7334#endif
7335 } else { /* one of "-=+?" */
7336 /* Standard-mandated substitution ops:
7337 * ${var?word} - indicate error if unset
7338 * If var is unset, word (or a message indicating it is unset
7339 * if word is null) is written to standard error
7340 * and the shell exits with a non-zero exit status.
7341 * Otherwise, the value of var is substituted.
7342 * ${var-word} - use default value
7343 * If var is unset, word is substituted.
7344 * ${var=word} - assign and use default value
7345 * If var is unset, word is assigned to var.
7346 * In all cases, final value of var is substituted.
7347 * ${var+word} - use alternative value
7348 * If var is unset, null is substituted.
7349 * Otherwise, word is substituted.
7350 *
7351 * Word is subjected to tilde expansion, parameter expansion,
7352 * command substitution, and arithmetic expansion.
7353 * If word is not needed, it is not expanded.
7354 *
7355 * Colon forms (${var:-word}, ${var:=word} etc) do the same,
7356 * but also treat null var as if it is unset.
7357 *
7358 * Word-splitting and single quote behavior:
7359 *
7360 * $ f() { for i; do echo "|$i|"; done; }
7361 *
7362 * $ x=; f ${x:?'x y' z}; echo $?
7363 * bash: x: x y z # neither f nor "echo $?" executes
7364 * (if interactive, bash does not exit, but merely aborts to prompt. $? is set to 1)
7365 * $ x=; f "${x:?'x y' z}"
7366 * bash: x: x y z # dash prints: dash: x: 'x y' z
7367 *
7368 * $ x=; f ${x:='x y' z}
7369 * |x|
7370 * |y|
7371 * |z|
7372 * $ x=; f "${x:='x y' z}"
7373 * |'x y' z|
7374 *
7375 * $ x=x; f ${x:+'x y' z}
7376 * |x y|
7377 * |z|
7378 * $ x=x; f "${x:+'x y' z}"
7379 * |'x y' z|
7380 *
7381 * $ x=; f ${x:-'x y' z}
7382 * |x y|
7383 * |z|
7384 * $ x=; f "${x:-'x y' z}"
7385 * |'x y' z|
7386 */
7387 int use_word = (!val || ((exp_save == ':') && !val[0]));
7388 if (exp_op == '+')
7389 use_word = !use_word;
7390 debug_printf_expand("expand: op:%c (null:%s) test:%i\n", exp_op,
7391 (exp_save == ':') ? "true" : "false", use_word);
7392 if (use_word) {
7393 if (exp_op == '+' || exp_op == '-') {
7394 /* ${var+word} - use alternative value */
7395 /* ${var-word} - use default value */
7396 n = encode_then_append_var_plusminus(output, n, exp_word,
7397 /*dquoted:*/ (arg0 & 0x80)
7398 );
7399 val = NULL;
7400 } else {
7401 /* ${var?word} - indicate error if unset */
7402 /* ${var=word} - assign and use default value */
7403 to_be_freed = encode_then_expand_vararg(exp_word,
7404 /*handle_squotes:*/ !(arg0 & 0x80),
7405 /*unbackslash:*/ 0
7406 );
7407 if (to_be_freed)
7408 exp_word = to_be_freed;
7409 if (exp_op == '?') {
7410 /* mimic bash message */
7411 msg_and_die_if_script("%s: %s",
7412 var,
7413 exp_word[0]
7414 ? exp_word
7415 : "parameter null or not set"
7416 /* ash has more specific messages, a-la: */
7417 /*: (exp_save == ':' ? "parameter null or not set" : "parameter not set")*/
7418 );
7419//TODO: how interactive bash aborts expansion mid-command?
7420//It aborts the entire line, returns to prompt:
7421// $ f() { for i; do echo "|$i|"; done; }; x=; f "${x:?'x y' z}"; echo YO
7422// bash: x: x y z
7423// $
7424// ("echo YO" is not executed, neither the f function call)
7425 } else {
7426 val = exp_word;
7427 }
7428 if (exp_op == '=') {
7429 /* ${var=[word]} or ${var:=[word]} */
7430 if (isdigit(var[0]) || var[0] == '#') {
7431 /* mimic bash message */
7432 msg_and_die_if_script("$%s: cannot assign in this way", var);
7433 val = NULL;
7434 } else {
7435 char *new_var = xasprintf("%s=%s", var, val);
Francis Laniele7ca3a32023-12-22 22:02:42 +01007436 set_local_var0(new_var);
Francis Laniel110b7692023-12-22 22:02:27 +01007437 }
7438 }
7439 }
7440 }
7441 } /* one of "-=+?" */
7442
7443 *exp_saveptr = exp_save;
7444 } /* if (exp_op) */
7445
Francis Laniel36836fc2023-12-22 22:02:28 +01007446#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01007447 arg[0] = arg0;
7448 *pp = p;
7449
7450 n = append_str_maybe_ifs_split(output, n, first_ch, val);
7451
7452 free(to_be_freed);
7453 return n;
7454}
7455
7456/* Expand all variable references in given string, adding words to list[]
7457 * at n, n+1,... positions. Return updated n (so that list[n] is next one
7458 * to be filled). This routine is extremely tricky: has to deal with
7459 * variables/parameters with whitespace, $* and $@, and constructs like
7460 * 'echo -$*-'. If you play here, you must run testsuite afterwards! */
7461static NOINLINE int expand_vars_to_list(o_string *output, int n, char *arg)
7462{
7463 /* output->o_expflags & EXP_FLAG_SINGLEWORD (0x80) if we are in
7464 * expansion of right-hand side of assignment == 1-element expand.
7465 */
7466 char cant_be_null = 0; /* only bit 0x80 matters */
7467 char *p;
7468
7469 debug_printf_expand("expand_vars_to_list: arg:'%s' singleword:%x\n", arg,
7470 !!(output->o_expflags & EXP_FLAG_SINGLEWORD));
7471 debug_print_list("expand_vars_to_list[0]", output, n);
7472
7473 while ((p = strchr(arg, SPECIAL_VAR_SYMBOL)) != NULL) {
7474 char first_ch;
7475#if ENABLE_FEATURE_SH_MATH
7476 char arith_buf[sizeof(arith_t)*3 + 2];
7477#endif
7478
7479 if (output->ended_in_ifs) {
7480 o_addchr(output, '\0');
7481 n = o_save_ptr(output, n);
7482 output->ended_in_ifs = 0;
7483 }
7484
7485 o_addblock(output, arg, p - arg);
7486 debug_print_list("expand_vars_to_list[1]", output, n);
7487 arg = ++p;
7488 p = strchr(p, SPECIAL_VAR_SYMBOL);
7489
7490 /* Fetch special var name (if it is indeed one of them)
7491 * and quote bit, force the bit on if singleword expansion -
7492 * important for not getting v=$@ expand to many words. */
7493 first_ch = arg[0] | (output->o_expflags & EXP_FLAG_SINGLEWORD);
7494
7495 /* Is this variable quoted and thus expansion can't be null?
7496 * "$@" is special. Even if quoted, it can still
7497 * expand to nothing (not even an empty string),
7498 * thus it is excluded. */
7499 if ((first_ch & 0x7f) != '@')
7500 cant_be_null |= first_ch;
7501
7502 switch (first_ch & 0x7f) {
7503 /* Highest bit in first_ch indicates that var is double-quoted */
7504 case '*':
7505 case '@': {
7506 int i;
Francis Lanielbfc406a2023-12-22 22:02:33 +01007507#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01007508 if (!G.global_argv[1])
Francis Lanielbfc406a2023-12-22 22:02:33 +01007509#else /* __U_BOOT__ */
7510 if (!G.global_argv || !G.global_argv[1])
7511#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01007512 break;
7513 i = 1;
7514 cant_be_null |= first_ch; /* do it for "$@" _now_, when we know it's not empty */
7515 if (!(first_ch & 0x80)) { /* unquoted $* or $@ */
7516 while (G.global_argv[i]) {
7517 n = expand_on_ifs(output, n, G.global_argv[i]);
7518 debug_printf_expand("expand_vars_to_list: argv %d (last %d)\n", i, G.global_argc - 1);
7519 if (G.global_argv[i++][0] && G.global_argv[i]) {
7520 /* this argv[] is not empty and not last:
7521 * put terminating NUL, start new word */
7522 o_addchr(output, '\0');
7523 debug_print_list("expand_vars_to_list[2]", output, n);
7524 n = o_save_ptr(output, n);
7525 debug_print_list("expand_vars_to_list[3]", output, n);
7526 }
7527 }
7528 } else
7529 /* If EXP_FLAG_SINGLEWORD, we handle assignment 'a=....$@.....'
7530 * and in this case should treat it like '$*' - see 'else...' below */
7531 if (first_ch == (char)('@'|0x80) /* quoted $@ */
7532 && !(output->o_expflags & EXP_FLAG_SINGLEWORD) /* not v="$@" case */
7533 ) {
7534 while (1) {
7535 o_addQstr(output, G.global_argv[i]);
7536 if (++i >= G.global_argc)
7537 break;
7538 o_addchr(output, '\0');
7539 debug_print_list("expand_vars_to_list[4]", output, n);
7540 n = o_save_ptr(output, n);
7541 }
7542 } else { /* quoted $* (or v="$@" case): add as one word */
7543 while (1) {
7544 o_addQstr(output, G.global_argv[i]);
7545 if (!G.global_argv[++i])
7546 break;
7547 if (G.ifs[0])
7548 o_addchr(output, G.ifs[0]);
7549 }
7550 output->has_quoted_part = 1;
7551 }
7552 break;
7553 }
7554 case SPECIAL_VAR_SYMBOL: {
7555 /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_SYMBOL> */
7556 /* "Empty variable", used to make "" etc to not disappear */
7557 output->has_quoted_part = 1;
7558 cant_be_null = 0x80;
7559 arg++;
7560 break;
7561 }
7562 case SPECIAL_VAR_QUOTED_SVS:
7563 /* <SPECIAL_VAR_SYMBOL><SPECIAL_VAR_QUOTED_SVS><SPECIAL_VAR_SYMBOL> */
7564 /* "^C variable", represents literal ^C char (possible in scripts) */
7565 o_addchr(output, SPECIAL_VAR_SYMBOL);
7566 arg++;
7567 break;
7568#if ENABLE_HUSH_TICK
7569 case '`': {
7570 /* <SPECIAL_VAR_SYMBOL>`cmd<SPECIAL_VAR_SYMBOL> */
7571 o_string subst_result = NULL_O_STRING;
7572
7573 *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */
7574 arg++;
7575 /* Can't just stuff it into output o_string,
7576 * expanded result may need to be globbed
7577 * and $IFS-split */
7578 debug_printf_subst("SUBST '%s' first_ch %x\n", arg, first_ch);
7579 G.last_exitcode = process_command_subs(&subst_result, arg);
7580 G.expand_exitcode = G.last_exitcode;
7581 debug_printf_subst("SUBST RES:%d '%s'\n", G.last_exitcode, subst_result.data);
7582 n = append_str_maybe_ifs_split(output, n, first_ch, subst_result.data);
7583 o_free(&subst_result);
7584 break;
7585 }
7586#endif
7587#if ENABLE_FEATURE_SH_MATH
7588 case '+': {
7589 /* <SPECIAL_VAR_SYMBOL>+arith<SPECIAL_VAR_SYMBOL> */
7590 arith_t res;
7591
7592 arg++; /* skip '+' */
7593 *p = '\0'; /* replace trailing <SPECIAL_VAR_SYMBOL> */
7594 debug_printf_subst("ARITH '%s' first_ch %x\n", arg, first_ch);
7595 res = expand_and_evaluate_arith(arg, NULL);
7596 debug_printf_subst("ARITH RES '"ARITH_FMT"'\n", res);
7597 sprintf(arith_buf, ARITH_FMT, res);
7598 if (res < 0
7599 && first_ch == (char)('+'|0x80)
7600 /* && (output->o_expflags & EXP_FLAG_ESC_GLOB_CHARS) */
7601 ) {
7602 /* Quoted negative ariths, like filename[0"$((-9))"],
7603 * should not be interpreted as glob ranges.
7604 * Convert leading '-' to '\-':
7605 */
7606 o_grow_by(output, 1);
7607 output->data[output->length++] = '\\';
7608 }
7609 o_addstr(output, arith_buf);
7610 break;
7611 }
7612#endif
7613 default:
7614 /* <SPECIAL_VAR_SYMBOL>varname[ops]<SPECIAL_VAR_SYMBOL> */
7615 n = expand_one_var(output, n, first_ch, arg, &p);
7616 break;
7617 } /* switch (char after <SPECIAL_VAR_SYMBOL>) */
7618
7619 /* Restore NULL'ed SPECIAL_VAR_SYMBOL.
7620 * Do the check to avoid writing to a const string. */
7621 if (*p != SPECIAL_VAR_SYMBOL)
7622 *p = SPECIAL_VAR_SYMBOL;
7623 arg = ++p;
7624 } /* end of "while (SPECIAL_VAR_SYMBOL is found) ..." */
7625
7626 if (*arg) {
7627 /* handle trailing string */
7628 if (output->ended_in_ifs) {
7629 o_addchr(output, '\0');
7630 n = o_save_ptr(output, n);
7631 }
7632 debug_print_list("expand_vars_to_list[a]", output, n);
7633 /* this part is literal, and it was already pre-quoted
7634 * if needed (much earlier), do not use o_addQstr here!
7635 */
7636 o_addstr(output, arg);
7637 debug_print_list("expand_vars_to_list[b]", output, n);
7638 } else
7639 if (output->length == o_get_last_ptr(output, n) /* expansion is empty */
7640 && !(cant_be_null & 0x80) /* and all vars were not quoted */
7641 && !output->has_quoted_part
7642 ) {
7643 n--;
7644 /* allow to reuse list[n] later without re-growth */
7645 output->has_empty_slot = 1;
7646 }
7647
7648 return n;
7649}
7650
7651static char **expand_variables(char **argv, unsigned expflags)
7652{
7653 int n;
7654 char **list;
7655 o_string output = NULL_O_STRING;
7656
7657 output.o_expflags = expflags;
7658
7659 n = 0;
7660 for (;;) {
7661 /* go to next list[n] */
7662 output.ended_in_ifs = 0;
7663 n = o_save_ptr(&output, n);
7664
7665 if (!*argv)
7666 break;
7667
7668 /* expand argv[i] */
7669 n = expand_vars_to_list(&output, n, *argv++);
7670 /* if (!output->has_empty_slot) -- need this?? */
7671 o_addchr(&output, '\0');
7672 }
7673 debug_print_list("expand_variables", &output, n);
7674
7675 /* output.data (malloced in one block) gets returned in "list" */
7676 list = o_finalize_list(&output, n);
7677 debug_print_strings("expand_variables[1]", list);
7678 return list;
7679}
7680
7681static char **expand_strvec_to_strvec(char **argv)
7682{
7683 return expand_variables(argv, EXP_FLAG_GLOB | EXP_FLAG_ESC_GLOB_CHARS);
7684}
7685
7686#if defined(CMD_SINGLEWORD_NOGLOB) || defined(CMD_TEST2_SINGLEWORD_NOGLOB)
7687static char **expand_strvec_to_strvec_singleword_noglob(char **argv)
7688{
7689 return expand_variables(argv, EXP_FLAG_SINGLEWORD);
7690}
7691#endif
7692
7693/* Used for expansion of right hand of assignments,
7694 * $((...)), heredocs, variable expansion parts.
7695 *
7696 * NB: should NOT do globbing!
7697 * "export v=/bin/c*; env | grep ^v=" outputs "v=/bin/c*"
7698 */
7699static char *expand_string_to_string(const char *str, int EXP_flags, int do_unbackslash)
7700{
7701#if !BASH_PATTERN_SUBST && !ENABLE_HUSH_CASE
7702 const int do_unbackslash = 1;
7703 const int EXP_flags = EXP_FLAG_ESC_GLOB_CHARS;
7704#endif
7705 char *argv[2], **list;
7706
7707 debug_printf_expand("string_to_string<='%s'\n", str);
7708 /* This is generally an optimization, but it also
7709 * handles "", which otherwise trips over !list[0] check below.
7710 * (is this ever happens that we actually get str="" here?)
7711 */
7712 if (!strchr(str, SPECIAL_VAR_SYMBOL) && !strchr(str, '\\')) {
7713 //TODO: Can use on strings with \ too, just unbackslash() them?
7714 debug_printf_expand("string_to_string(fast)=>'%s'\n", str);
7715 return xstrdup(str);
7716 }
7717
7718 argv[0] = (char*)str;
7719 argv[1] = NULL;
7720 list = expand_variables(argv, EXP_flags | EXP_FLAG_SINGLEWORD);
7721 if (!list[0]) {
7722 /* Example where it happens:
7723 * x=; echo ${x:-"$@"}
7724 */
7725 ((char*)list)[0] = '\0';
7726 } else {
7727 if (HUSH_DEBUG)
7728 if (list[1])
7729 bb_simple_error_msg_and_die("BUG in varexp2");
7730 /* actually, just move string 2*sizeof(char*) bytes back */
7731 overlapping_strcpy((char*)list, list[0]);
7732 if (do_unbackslash)
7733 unbackslash((char*)list);
7734 }
7735 debug_printf_expand("string_to_string=>'%s'\n", (char*)list);
7736 return (char*)list;
7737}
7738
7739#if 0
7740static char* expand_strvec_to_string(char **argv)
7741{
7742 char **list;
7743
7744 list = expand_variables(argv, EXP_FLAG_SINGLEWORD);
7745 /* Convert all NULs to spaces */
7746 if (list[0]) {
7747 int n = 1;
7748 while (list[n]) {
7749 if (HUSH_DEBUG)
7750 if (list[n-1] + strlen(list[n-1]) + 1 != list[n])
7751 bb_error_msg_and_die("BUG in varexp3");
7752 /* bash uses ' ' regardless of $IFS contents */
7753 list[n][-1] = ' ';
7754 n++;
7755 }
7756 }
7757 overlapping_strcpy((char*)list, list[0] ? list[0] : "");
7758 debug_printf_expand("strvec_to_string='%s'\n", (char*)list);
7759 return (char*)list;
7760}
7761#endif
7762
Francis Laniel36836fc2023-12-22 22:02:28 +01007763#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01007764static char **expand_assignments(char **argv, int count)
7765{
7766 int i;
7767 char **p;
7768
7769 G.expanded_assignments = p = NULL;
7770 /* Expand assignments into one string each */
7771 for (i = 0; i < count; i++) {
7772 p = add_string_to_strings(p,
7773 expand_string_to_string(argv[i],
7774 EXP_FLAG_ESC_GLOB_CHARS,
7775 /*unbackslash:*/ 1
7776 )
7777 );
7778 G.expanded_assignments = p;
7779 }
7780 G.expanded_assignments = NULL;
7781 return p;
7782}
7783
Francis Laniel110b7692023-12-22 22:02:27 +01007784static void switch_off_special_sigs(unsigned mask)
7785{
7786 unsigned sig = 0;
7787 while ((mask >>= 1) != 0) {
7788 sig++;
7789 if (!(mask & 1))
7790 continue;
7791#if ENABLE_HUSH_TRAP
7792 if (G_traps) {
7793 if (G_traps[sig] && !G_traps[sig][0])
7794 /* trap is '', has to remain SIG_IGN */
7795 continue;
7796 free(G_traps[sig]);
7797 G_traps[sig] = NULL;
7798 }
7799#endif
7800 /* We are here only if no trap or trap was not '' */
7801 install_sighandler(sig, SIG_DFL);
7802 }
7803}
Francis Laniel36836fc2023-12-22 22:02:28 +01007804#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01007805
Francis Laniel36836fc2023-12-22 22:02:28 +01007806#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01007807#if BB_MMU
7808/* never called */
7809void re_execute_shell(char ***to_free, const char *s,
7810 char *g_argv0, char **g_argv,
7811 char **builtin_argv) NORETURN;
7812
7813static void reset_traps_to_defaults(void)
7814{
7815 /* This function is always called in a child shell
7816 * after fork (not vfork, NOMMU doesn't use this function).
7817 */
7818 IF_HUSH_TRAP(unsigned sig;)
7819 unsigned mask;
7820
7821 /* Child shells are not interactive.
7822 * SIGTTIN/SIGTTOU/SIGTSTP should not have special handling.
7823 * Testcase: (while :; do :; done) + ^Z should background.
7824 * Same goes for SIGTERM, SIGHUP, SIGINT.
7825 */
7826 mask = (G.special_sig_mask & SPECIAL_INTERACTIVE_SIGS) | G_fatal_sig_mask;
7827 if (!G_traps && !mask)
7828 return; /* already no traps and no special sigs */
7829
7830 /* Switch off special sigs */
7831 switch_off_special_sigs(mask);
7832# if ENABLE_HUSH_JOB
7833 G_fatal_sig_mask = 0;
7834# endif
7835 G.special_sig_mask &= ~SPECIAL_INTERACTIVE_SIGS;
7836 /* SIGQUIT,SIGCHLD and maybe SPECIAL_JOBSTOP_SIGS
7837 * remain set in G.special_sig_mask */
7838
7839# if ENABLE_HUSH_TRAP
7840 if (!G_traps)
7841 return;
7842
7843 /* Reset all sigs to default except ones with empty traps */
7844 for (sig = 0; sig < NSIG; sig++) {
7845 if (!G_traps[sig])
7846 continue; /* no trap: nothing to do */
7847 if (!G_traps[sig][0])
7848 continue; /* empty trap: has to remain SIG_IGN */
7849 /* sig has non-empty trap, reset it: */
7850 free(G_traps[sig]);
7851 G_traps[sig] = NULL;
7852 /* There is no signal for trap 0 (EXIT) */
7853 if (sig == 0)
7854 continue;
7855 install_sighandler(sig, pick_sighandler(sig));
7856 }
7857# endif
7858}
7859
7860#else /* !BB_MMU */
7861
7862static void re_execute_shell(char ***to_free, const char *s,
7863 char *g_argv0, char **g_argv,
7864 char **builtin_argv) NORETURN;
7865static void re_execute_shell(char ***to_free, const char *s,
7866 char *g_argv0, char **g_argv,
7867 char **builtin_argv)
7868{
7869# define NOMMU_HACK_FMT ("-$%x:%x:%x:%x:%x:%llx" IF_HUSH_LOOPS(":%x"))
7870 /* delims + 2 * (number of bytes in printed hex numbers) */
7871 char param_buf[sizeof(NOMMU_HACK_FMT) + 2 * (sizeof(int)*6 + sizeof(long long)*1)];
7872 char *heredoc_argv[4];
7873 struct variable *cur;
7874# if ENABLE_HUSH_FUNCTIONS
7875 struct function *funcp;
7876# endif
7877 char **argv, **pp;
7878 unsigned cnt;
7879 unsigned long long empty_trap_mask;
7880
7881 if (!g_argv0) { /* heredoc */
7882 argv = heredoc_argv;
7883 argv[0] = (char *) G.argv0_for_re_execing;
7884 argv[1] = (char *) "-<";
7885 argv[2] = (char *) s;
7886 argv[3] = NULL;
7887 pp = &argv[3]; /* used as pointer to empty environment */
7888 goto do_exec;
7889 }
7890
7891 cnt = 0;
7892 pp = builtin_argv;
7893 if (pp) while (*pp++)
7894 cnt++;
7895
7896 empty_trap_mask = 0;
7897 if (G_traps) {
7898 int sig;
7899 for (sig = 1; sig < NSIG; sig++) {
7900 if (G_traps[sig] && !G_traps[sig][0])
7901 empty_trap_mask |= 1LL << sig;
7902 }
7903 }
7904
7905 sprintf(param_buf, NOMMU_HACK_FMT
7906 , (unsigned) G.root_pid
7907 , (unsigned) G.root_ppid
7908 , (unsigned) G.last_bg_pid
7909 , (unsigned) G.last_exitcode
7910 , cnt
7911 , empty_trap_mask
7912 IF_HUSH_LOOPS(, G.depth_of_loop)
7913 );
7914# undef NOMMU_HACK_FMT
7915 /* 1:hush 2:-$<pid>:<pid>:<exitcode>:<etc...> <vars...> <funcs...>
7916 * 3:-c 4:<cmd> 5:<arg0> <argN...> 6:NULL
7917 */
7918 cnt += 6;
7919 for (cur = G.top_var; cur; cur = cur->next) {
7920 if (!cur->flg_export || cur->flg_read_only)
7921 cnt += 2;
7922 }
Francis Laniele7ca3a32023-12-22 22:02:42 +01007923# if ENABLE_HUSH_LINENO_VAR
7924 cnt += 2;
7925# endif
Francis Laniel110b7692023-12-22 22:02:27 +01007926# if ENABLE_HUSH_FUNCTIONS
7927 for (funcp = G.top_func; funcp; funcp = funcp->next)
7928 cnt += 3;
7929# endif
7930 pp = g_argv;
7931 while (*pp++)
7932 cnt++;
7933 *to_free = argv = pp = xzalloc(sizeof(argv[0]) * cnt);
7934 *pp++ = (char *) G.argv0_for_re_execing;
7935 *pp++ = param_buf;
7936 for (cur = G.top_var; cur; cur = cur->next) {
7937 if (strcmp(cur->varstr, hush_version_str) == 0)
7938 continue;
7939 if (cur->flg_read_only) {
7940 *pp++ = (char *) "-R";
7941 *pp++ = cur->varstr;
7942 } else if (!cur->flg_export) {
7943 *pp++ = (char *) "-V";
7944 *pp++ = cur->varstr;
7945 }
7946 }
Francis Laniele7ca3a32023-12-22 22:02:42 +01007947# if ENABLE_HUSH_LINENO_VAR
7948 *pp++ = (char *) "-L";
7949 *pp++ = utoa(G.execute_lineno);
7950# endif
Francis Laniel110b7692023-12-22 22:02:27 +01007951# if ENABLE_HUSH_FUNCTIONS
7952 for (funcp = G.top_func; funcp; funcp = funcp->next) {
7953 *pp++ = (char *) "-F";
7954 *pp++ = funcp->name;
7955 *pp++ = funcp->body_as_string;
7956 }
7957# endif
7958 /* We can pass activated traps here. Say, -Tnn:trap_string
7959 *
7960 * However, POSIX says that subshells reset signals with traps
7961 * to SIG_DFL.
7962 * I tested bash-3.2 and it not only does that with true subshells
7963 * of the form ( list ), but with any forked children shells.
7964 * I set trap "echo W" WINCH; and then tried:
7965 *
7966 * { echo 1; sleep 20; echo 2; } &
7967 * while true; do echo 1; sleep 20; echo 2; break; done &
7968 * true | { echo 1; sleep 20; echo 2; } | cat
7969 *
7970 * In all these cases sending SIGWINCH to the child shell
7971 * did not run the trap. If I add trap "echo V" WINCH;
7972 * _inside_ group (just before echo 1), it works.
7973 *
7974 * I conclude it means we don't need to pass active traps here.
7975 */
7976 *pp++ = (char *) "-c";
7977 *pp++ = (char *) s;
7978 if (builtin_argv) {
7979 while (*++builtin_argv)
7980 *pp++ = *builtin_argv;
7981 *pp++ = (char *) "";
7982 }
7983 *pp++ = g_argv0;
7984 while (*g_argv)
7985 *pp++ = *g_argv++;
7986 /* *pp = NULL; - is already there */
7987 pp = environ;
7988
7989 do_exec:
7990 debug_printf_exec("re_execute_shell pid:%d cmd:'%s'\n", getpid(), s);
7991 /* Don't propagate SIG_IGN to the child */
7992 if (SPECIAL_JOBSTOP_SIGS != 0)
7993 switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS);
7994 execve(bb_busybox_exec_path, argv, pp);
7995 /* Fallback. Useful for init=/bin/hush usage etc */
7996 if (argv[0][0] == '/')
7997 execve(argv[0], argv, pp);
7998 xfunc_error_retval = 127;
7999 bb_simple_error_msg_and_die("can't re-execute the shell");
8000}
8001#endif /* !BB_MMU */
8002
Francis Laniel36836fc2023-12-22 22:02:28 +01008003#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01008004
8005static int run_and_free_list(struct pipe *pi);
8006
8007/* Executing from string: eval, sh -c '...'
8008 * or from file: /etc/profile, . file, sh <script>, sh (intereactive)
8009 * end_trigger controls how often we stop parsing
8010 * NUL: parse all, execute, return
8011 * ';': parse till ';' or newline, execute, repeat till EOF
8012 */
Francis Laniel26cafe12023-12-22 22:02:34 +01008013#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01008014static void parse_and_run_stream(struct in_str *inp, int end_trigger)
Francis Laniel26cafe12023-12-22 22:02:34 +01008015#else /* __U_BOOT__ */
8016static int parse_and_run_stream(struct in_str *inp, int end_trigger)
8017#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01008018{
8019 /* Why we need empty flag?
8020 * An obscure corner case "false; ``; echo $?":
8021 * empty command in `` should still set $? to 0.
8022 * But we can't just set $? to 0 at the start,
8023 * this breaks "false; echo `echo $?`" case.
8024 */
8025 bool empty = 1;
Francis Laniel26cafe12023-12-22 22:02:34 +01008026#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01008027 while (1) {
Francis Laniel26cafe12023-12-22 22:02:34 +01008028#else /* __U_BOOT__ */
8029 do {
8030#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01008031 struct pipe *pipe_list;
8032
8033#if ENABLE_HUSH_INTERACTIVE
8034 if (end_trigger == ';') {
8035 G.promptmode = 0; /* PS1 */
8036 debug_printf_prompt("%s promptmode=%d\n", __func__, G.promptmode);
8037 }
8038#endif
8039 pipe_list = parse_stream(NULL, NULL, inp, end_trigger);
8040 if (!pipe_list || pipe_list == ERR_PTR) { /* EOF/error */
8041 /* If we are in "big" script
8042 * (not in `cmd` or something similar)...
8043 */
8044 if (pipe_list == ERR_PTR && end_trigger == ';') {
8045 /* Discard cached input (rest of line) */
8046 int ch = inp->last_char;
8047 while (ch != EOF && ch != '\n') {
8048 //bb_error_msg("Discarded:'%c'", ch);
8049 ch = i_getch(inp);
8050 }
8051 /* Force prompt */
8052 inp->p = NULL;
8053 /* This stream isn't empty */
8054 empty = 0;
8055 continue;
8056 }
8057 if (!pipe_list && empty)
8058 G.last_exitcode = 0;
8059 break;
8060 }
8061 debug_print_tree(pipe_list, 0);
8062 debug_printf_exec("parse_and_run_stream: run_and_free_list\n");
Francis Laniel3b66e572023-12-22 22:02:32 +01008063#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01008064 run_and_free_list(pipe_list);
Francis Laniel3b66e572023-12-22 22:02:32 +01008065#else /* __U_BOOT__ */
8066 int rcode = run_and_free_list(pipe_list);
8067 /*
8068 * We reset input string to not run the following command, so running
8069 * 'exit; echo foo' does not print foo.
8070 */
8071 if (rcode <= EXIT_RET_CODE)
8072 setup_file_in_str(inp);
8073#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01008074 empty = 0;
8075 if (G_flag_return_in_progress == 1)
8076 break;
Francis Laniel26cafe12023-12-22 22:02:34 +01008077#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01008078 }
Francis Laniel26cafe12023-12-22 22:02:34 +01008079#else /* __U_BOOT__ */
8080 /*
8081 * This do/while is needed by run_command to avoid looping on a command
8082 * with syntax error.
8083 */
8084 } while (!(G.run_command_flags & FLAG_EXIT_FROM_LOOP));
8085
8086 return G.last_exitcode;
8087#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01008088}
8089
Francis Laniel36836fc2023-12-22 22:02:28 +01008090#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01008091static void parse_and_run_string(const char *s)
Francis Laniel26cafe12023-12-22 22:02:34 +01008092#else /* __U_BOOT__ */
8093static int parse_and_run_string(const char *s)
8094#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01008095{
8096 struct in_str input;
Francis Laniele7ca3a32023-12-22 22:02:42 +01008097#ifndef __U_BOOT__
8098 IF_HUSH_LINENO_VAR(unsigned sv = G.parse_lineno;)
8099#else /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01008100 //IF_HUSH_LINENO_VAR(unsigned sv = G.parse_lineno;)
Francis Laniele7ca3a32023-12-22 22:02:42 +01008101#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01008102
8103 setup_string_in_str(&input, s);
Francis Laniel26cafe12023-12-22 22:02:34 +01008104#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01008105 parse_and_run_stream(&input, '\0');
Francis Laniel26cafe12023-12-22 22:02:34 +01008106#else /* __U_BOOT__ */
8107 return parse_and_run_stream(&input, '\0');
8108#endif /* __U_BOOT__ */
Francis Laniele7ca3a32023-12-22 22:02:42 +01008109#ifndef __U_BOOT__
8110 IF_HUSH_LINENO_VAR(unsigned sv = G.parse_lineno;)
8111#else /* __U_BOOT__ */
8112 //IF_HUSH_LINENO_VAR(unsigned sv = G.parse_lineno;)
8113#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01008114}
8115
Francis Laniel26cafe12023-12-22 22:02:34 +01008116#ifdef __U_BOOT__
Francis Laniel5b64c452023-12-22 22:02:35 +01008117int parse_string_outer_modern(const char *cmd, int flags)
Francis Laniel26cafe12023-12-22 22:02:34 +01008118{
8119 int ret;
8120 int old_flags;
8121
8122 /*
8123 * Keep old values of run_command to be able to restore them once
8124 * command was executed.
8125 */
8126 old_flags = G.run_command_flags;
8127 G.run_command_flags = flags;
8128
8129 ret = parse_and_run_string(cmd);
8130
8131 G.run_command_flags = old_flags;
8132
8133 return ret;
8134}
8135#endif /* __U_BOOT__ */
Francis Laniel36836fc2023-12-22 22:02:28 +01008136#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01008137static void parse_and_run_file(HFILE *fp)
Francis Laniel36836fc2023-12-22 22:02:28 +01008138#else /* __U_BOOT__ */
8139void parse_and_run_file(void)
8140#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01008141{
8142 struct in_str input;
Francis Laniel36836fc2023-12-22 22:02:28 +01008143#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01008144 IF_HUSH_LINENO_VAR(unsigned sv = G.parse_lineno;)
8145
8146 IF_HUSH_LINENO_VAR(G.parse_lineno = 1;)
8147 setup_file_in_str(&input, fp);
Francis Laniel36836fc2023-12-22 22:02:28 +01008148#else /* __U_BOOT__ */
8149 setup_file_in_str(&input);
8150#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01008151 parse_and_run_stream(&input, ';');
Francis Laniel36836fc2023-12-22 22:02:28 +01008152#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01008153 IF_HUSH_LINENO_VAR(G.parse_lineno = sv;)
Francis Laniel36836fc2023-12-22 22:02:28 +01008154#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01008155}
8156
Francis Laniel36836fc2023-12-22 22:02:28 +01008157#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01008158#if ENABLE_HUSH_TICK
8159static int generate_stream_from_string(const char *s, pid_t *pid_p)
8160{
8161 pid_t pid;
8162 int channel[2];
8163# if !BB_MMU
8164 char **to_free = NULL;
8165# endif
8166
8167 xpipe(channel);
8168 pid = BB_MMU ? xfork() : xvfork();
8169 if (pid == 0) { /* child */
8170 disable_restore_tty_pgrp_on_exit();
8171 /* Process substitution is not considered to be usual
8172 * 'command execution'.
8173 * SUSv3 says ctrl-Z should be ignored, ctrl-C should not.
8174 */
8175 bb_signals(0
8176 + (1 << SIGTSTP)
8177 + (1 << SIGTTIN)
8178 + (1 << SIGTTOU)
8179 , SIG_IGN);
8180 close(channel[0]); /* NB: close _first_, then move fd! */
8181 xmove_fd(channel[1], 1);
8182# if ENABLE_HUSH_TRAP
8183 /* Awful hack for `trap` or $(trap).
8184 *
8185 * http://www.opengroup.org/onlinepubs/009695399/utilities/trap.html
8186 * contains an example where "trap" is executed in a subshell:
8187 *
8188 * save_traps=$(trap)
8189 * ...
8190 * eval "$save_traps"
8191 *
8192 * Standard does not say that "trap" in subshell shall print
8193 * parent shell's traps. It only says that its output
8194 * must have suitable form, but then, in the above example
8195 * (which is not supposed to be normative), it implies that.
8196 *
8197 * bash (and probably other shell) does implement it
8198 * (traps are reset to defaults, but "trap" still shows them),
8199 * but as a result, "trap" logic is hopelessly messed up:
8200 *
8201 * # trap
8202 * trap -- 'echo Ho' SIGWINCH <--- we have a handler
8203 * # (trap) <--- trap is in subshell - no output (correct, traps are reset)
8204 * # true | trap <--- trap is in subshell - no output (ditto)
8205 * # echo `true | trap` <--- in subshell - output (but traps are reset!)
8206 * trap -- 'echo Ho' SIGWINCH
8207 * # echo `(trap)` <--- in subshell in subshell - output
8208 * trap -- 'echo Ho' SIGWINCH
8209 * # echo `true | (trap)` <--- in subshell in subshell in subshell - output!
8210 * trap -- 'echo Ho' SIGWINCH
8211 *
8212 * The rules when to forget and when to not forget traps
8213 * get really complex and nonsensical.
8214 *
8215 * Our solution: ONLY bare $(trap) or `trap` is special.
8216 */
8217 s = skip_whitespace(s);
8218 if (is_prefixed_with(s, "trap")
8219 && skip_whitespace(s + 4)[0] == '\0'
8220 ) {
Francis Laniele7ca3a32023-12-22 22:02:42 +01008221 static const char *const argv[] ALIGN_PTR = { NULL, NULL };
Francis Laniel110b7692023-12-22 22:02:27 +01008222 builtin_trap((char**)argv);
8223 fflush_all(); /* important */
8224 _exit(0);
8225 }
8226# endif
8227# if BB_MMU
8228 /* Prevent it from trying to handle ctrl-z etc */
8229 IF_HUSH_JOB(G.run_list_level = 1;)
8230 CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */
8231 reset_traps_to_defaults();
8232 IF_HUSH_MODE_X(G.x_mode_depth++;)
8233 //bb_error_msg("%s: ++x_mode_depth=%d", __func__, G.x_mode_depth);
8234 parse_and_run_string(s);
8235 _exit(G.last_exitcode);
8236# else
8237 /* We re-execute after vfork on NOMMU. This makes this script safe:
8238 * yes "0123456789012345678901234567890" | dd bs=32 count=64k >BIG
8239 * huge=`cat BIG` # was blocking here forever
8240 * echo OK
8241 */
8242 re_execute_shell(&to_free,
8243 s,
8244 G.global_argv[0],
8245 G.global_argv + 1,
8246 NULL);
8247# endif
8248 }
8249
8250 /* parent */
8251 *pid_p = pid;
8252# if ENABLE_HUSH_FAST
8253 G.count_SIGCHLD++;
8254//bb_error_msg("[%d] fork in generate_stream_from_string:"
8255// " G.count_SIGCHLD:%d G.handled_SIGCHLD:%d",
8256// getpid(), G.count_SIGCHLD, G.handled_SIGCHLD);
8257# endif
8258 enable_restore_tty_pgrp_on_exit();
8259# if !BB_MMU
8260 free(to_free);
8261# endif
8262 close(channel[1]);
8263 return channel[0];
8264}
8265
8266/* Return code is exit status of the process that is run. */
8267static int process_command_subs(o_string *dest, const char *s)
8268{
8269 FILE *fp;
8270 pid_t pid;
8271 int status, ch, eol_cnt;
8272
8273 fp = xfdopen_for_read(generate_stream_from_string(s, &pid));
8274
8275 /* Now send results of command back into original context */
8276 eol_cnt = 0;
8277 while ((ch = getc(fp)) != EOF) {
8278 if (ch == '\0')
8279 continue;
8280 if (ch == '\n') {
8281 eol_cnt++;
8282 continue;
8283 }
8284 while (eol_cnt) {
8285 o_addchr(dest, '\n');
8286 eol_cnt--;
8287 }
8288 o_addQchr(dest, ch);
8289 }
8290
8291 debug_printf("done reading from `cmd` pipe, closing it\n");
8292 fclose(fp);
8293 /* We need to extract exitcode. Test case
8294 * "true; echo `sleep 1; false` $?"
8295 * should print 1 */
8296 safe_waitpid(pid, &status, 0);
8297 debug_printf("child exited. returning its exitcode:%d\n", WEXITSTATUS(status));
8298 return WEXITSTATUS(status);
8299}
8300#endif /* ENABLE_HUSH_TICK */
8301
Francis Laniel110b7692023-12-22 22:02:27 +01008302static void setup_heredoc(struct redir_struct *redir)
8303{
8304 struct fd_pair pair;
8305 pid_t pid;
8306 int len, written;
8307 /* the _body_ of heredoc (misleading field name) */
8308 const char *heredoc = redir->rd_filename;
8309 char *expanded;
8310#if !BB_MMU
8311 char **to_free;
8312#endif
8313
8314 expanded = NULL;
8315 if (!(redir->rd_dup & HEREDOC_QUOTED)) {
8316 expanded = encode_then_expand_string(heredoc);
8317 if (expanded)
8318 heredoc = expanded;
8319 }
8320 len = strlen(heredoc);
8321
8322 close(redir->rd_fd); /* often saves dup2+close in xmove_fd */
8323 xpiped_pair(pair);
8324 xmove_fd(pair.rd, redir->rd_fd);
8325
8326 /* Try writing without forking. Newer kernels have
8327 * dynamically growing pipes. Must use non-blocking write! */
8328 ndelay_on(pair.wr);
8329 while (1) {
8330 written = write(pair.wr, heredoc, len);
8331 if (written <= 0)
8332 break;
8333 len -= written;
8334 if (len == 0) {
8335 close(pair.wr);
8336 free(expanded);
8337 return;
8338 }
8339 heredoc += written;
8340 }
8341 ndelay_off(pair.wr);
8342
8343 /* Okay, pipe buffer was not big enough */
8344 /* Note: we must not create a stray child (bastard? :)
8345 * for the unsuspecting parent process. Child creates a grandchild
8346 * and exits before parent execs the process which consumes heredoc
8347 * (that exec happens after we return from this function) */
8348#if !BB_MMU
8349 to_free = NULL;
8350#endif
8351 pid = xvfork();
8352 if (pid == 0) {
8353 /* child */
8354 disable_restore_tty_pgrp_on_exit();
8355 pid = BB_MMU ? xfork() : xvfork();
8356 if (pid != 0)
8357 _exit(0);
8358 /* grandchild */
8359 close(redir->rd_fd); /* read side of the pipe */
8360#if BB_MMU
8361 full_write(pair.wr, heredoc, len); /* may loop or block */
8362 _exit(0);
8363#else
8364 /* Delegate blocking writes to another process */
8365 xmove_fd(pair.wr, STDOUT_FILENO);
8366 re_execute_shell(&to_free, heredoc, NULL, NULL, NULL);
8367#endif
8368 }
8369 /* parent */
8370#if ENABLE_HUSH_FAST
8371 G.count_SIGCHLD++;
8372//bb_error_msg("[%d] fork in setup_heredoc: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD);
8373#endif
8374 enable_restore_tty_pgrp_on_exit();
8375#if !BB_MMU
8376 free(to_free);
8377#endif
8378 close(pair.wr);
8379 free(expanded);
8380 wait(NULL); /* wait till child has died */
8381}
8382
8383struct squirrel {
8384 int orig_fd;
8385 int moved_to;
8386 /* moved_to = n: fd was moved to n; restore back to orig_fd after redir */
8387 /* moved_to = -1: fd was opened by redirect; close orig_fd after redir */
8388};
8389
8390static struct squirrel *append_squirrel(struct squirrel *sq, int i, int orig, int moved)
8391{
8392 sq = xrealloc(sq, (i + 2) * sizeof(sq[0]));
8393 sq[i].orig_fd = orig;
8394 sq[i].moved_to = moved;
8395 sq[i+1].orig_fd = -1; /* end marker */
8396 return sq;
8397}
8398
8399static struct squirrel *add_squirrel(struct squirrel *sq, int fd, int avoid_fd)
8400{
8401 int moved_to;
8402 int i;
8403
8404 i = 0;
8405 if (sq) for (; sq[i].orig_fd >= 0; i++) {
8406 /* If we collide with an already moved fd... */
8407 if (fd == sq[i].moved_to) {
Francis Laniel03061a82024-09-03 19:09:43 +02008408 moved_to = dup_CLOEXEC(sq[i].moved_to, avoid_fd);
8409 debug_printf_redir("redirect_fd %d: already busy, moving to %d\n", fd, moved_to);
8410 if (moved_to < 0) {
8411 /* "echo 2>/dev/tty 10>&9999" testcase:
8412 * We move fd 2 to 10, then discover we need to move fd 10
8413 * (and not hit 9999) and the latter fails.
8414 */
8415 return NULL; /* fcntl failed */
8416 }
8417 sq[i].moved_to = moved_to;
Francis Laniel110b7692023-12-22 22:02:27 +01008418 return sq;
8419 }
8420 if (fd == sq[i].orig_fd) {
8421 /* Example: echo Hello >/dev/null 1>&2 */
8422 debug_printf_redir("redirect_fd %d: already moved\n", fd);
8423 return sq;
8424 }
8425 }
8426
8427 /* If this fd is open, we move and remember it; if it's closed, moved_to = -1 */
8428 moved_to = dup_CLOEXEC(fd, avoid_fd);
8429 debug_printf_redir("redirect_fd %d: previous fd is moved to %d (-1 if it was closed)\n", fd, moved_to);
8430 if (moved_to < 0 && errno != EBADF)
Francis Laniel03061a82024-09-03 19:09:43 +02008431 return NULL; /* fcntl failed (not because fd is closed) */
Francis Laniel110b7692023-12-22 22:02:27 +01008432 return append_squirrel(sq, i, fd, moved_to);
8433}
8434
8435static struct squirrel *add_squirrel_closed(struct squirrel *sq, int fd)
8436{
8437 int i;
8438
8439 i = 0;
8440 if (sq) for (; sq[i].orig_fd >= 0; i++) {
8441 /* If we collide with an already moved fd... */
8442 if (fd == sq[i].orig_fd) {
8443 /* Examples:
8444 * "echo 3>FILE 3>&- 3>FILE"
8445 * "echo 3>&- 3>FILE"
8446 * No need for last redirect to insert
8447 * another "need to close 3" indicator.
8448 */
8449 debug_printf_redir("redirect_fd %d: already moved or closed\n", fd);
8450 return sq;
8451 }
8452 }
8453
8454 debug_printf_redir("redirect_fd %d: previous fd was closed\n", fd);
8455 return append_squirrel(sq, i, fd, -1);
8456}
8457
8458/* fd: redirect wants this fd to be used (e.g. 3>file).
8459 * Move all conflicting internally used fds,
8460 * and remember them so that we can restore them later.
8461 */
8462static int save_fd_on_redirect(int fd, int avoid_fd, struct squirrel **sqp)
8463{
Francis Laniel03061a82024-09-03 19:09:43 +02008464 struct squirrel *new_squirrel;
8465
Francis Laniel110b7692023-12-22 22:02:27 +01008466 if (avoid_fd < 9) /* the important case here is that it can be -1 */
8467 avoid_fd = 9;
8468
8469#if ENABLE_HUSH_INTERACTIVE
8470 if (fd != 0 /* don't trigger for G_interactive_fd == 0 (that's "not interactive" flag) */
8471 && fd == G_interactive_fd
8472 ) {
8473 /* Testcase: "ls -l /proc/$$/fd 255>&-" should work */
8474 G_interactive_fd = xdup_CLOEXEC_and_close(G_interactive_fd, avoid_fd);
8475 debug_printf_redir("redirect_fd %d: matches interactive_fd, moving it to %d\n", fd, G_interactive_fd);
8476 return 1; /* "we closed fd" */
8477 }
8478#endif
8479 /* Are we called from setup_redirects(squirrel==NULL)
8480 * in redirect in a [v]forked child?
8481 */
8482 if (sqp == NULL) {
8483 /* No need to move script fds.
8484 * For NOMMU case, it's actively wrong: we'd change ->fd
8485 * fields in memory for the parent, but parent's fds
8486 * aren't moved, it would use wrong fd!
8487 * Reproducer: "cmd 3>FILE" in script.
8488 * If we would call move_HFILEs_on_redirect(), child would:
8489 * fcntl64(3, F_DUPFD_CLOEXEC, 10) = 10
8490 * close(3) = 0
8491 * and change ->fd to 10 if fd#3 is a script fd. WRONG.
8492 */
8493 //bb_error_msg("sqp == NULL: [v]forked child");
8494 return 0;
8495 }
8496
8497 /* If this one of script's fds? */
8498 if (move_HFILEs_on_redirect(fd, avoid_fd))
8499 return 1; /* yes. "we closed fd" (actually moved it) */
8500
8501 /* Are we called for "exec 3>FILE"? Came through
8502 * redirect_and_varexp_helper(squirrel=ERR_PTR) -> setup_redirects(ERR_PTR)
8503 * This case used to fail for this script:
8504 * exec 3>FILE
8505 * echo Ok
8506 * ...100000 more lines...
8507 * echo Ok
8508 * as follows:
8509 * read(3, "exec 3>FILE\necho Ok\necho Ok"..., 1024) = 1024
8510 * open("FILE", O_WRONLY|O_CREAT|O_TRUNC|O_LARGEFILE, 0666) = 4
8511 * dup2(4, 3) = 3
8512 * ^^^^^^^^ oops, we lost fd#3 opened to our script!
8513 * close(4) = 0
8514 * write(1, "Ok\n", 3) = 3
8515 * ... = 3
8516 * write(1, "Ok\n", 3) = 3
8517 * read(3, 0x94fbc08, 1024) = -1 EBADF (Bad file descriptor)
8518 * ^^^^^^^^ oops, wrong fd!!!
8519 * With this case separate from sqp == NULL and *after* move_HFILEs,
8520 * it now works:
8521 */
8522 if (sqp == ERR_PTR) {
8523 /* Don't preserve redirected fds: exec is _meant_ to change these */
8524 //bb_error_msg("sqp == ERR_PTR: exec >FILE");
8525 return 0;
8526 }
8527
8528 /* Check whether it collides with any open fds (e.g. stdio), save fds as needed */
Francis Laniel03061a82024-09-03 19:09:43 +02008529 new_squirrel = add_squirrel(*sqp, fd, avoid_fd);
8530 if (!new_squirrel)
8531 return -1; /* redirect error */
8532 *sqp = new_squirrel;
Francis Laniel110b7692023-12-22 22:02:27 +01008533 return 0; /* "we did not close fd" */
8534}
8535
8536static void restore_redirects(struct squirrel *sq)
8537{
8538 if (sq) {
8539 int i;
8540 for (i = 0; sq[i].orig_fd >= 0; i++) {
8541 if (sq[i].moved_to >= 0) {
8542 /* We simply die on error */
8543 debug_printf_redir("restoring redirected fd from %d to %d\n", sq[i].moved_to, sq[i].orig_fd);
8544 xmove_fd(sq[i].moved_to, sq[i].orig_fd);
8545 } else {
8546 /* cmd1 9>FILE; cmd2_should_see_fd9_closed */
8547 debug_printf_redir("restoring redirected fd %d: closing it\n", sq[i].orig_fd);
8548 close(sq[i].orig_fd);
8549 }
8550 }
8551 free(sq);
8552 }
8553 if (G.HFILE_stdin
8554 && G.HFILE_stdin->fd > STDIN_FILENO
8555 /* we compare > STDIN, not == STDIN, since hfgetc()
8556 * closes fd and sets ->fd to -1 if EOF is reached.
8557 * Testcase: echo 'pwd' | hush
8558 */
8559 ) {
8560 /* Testcase: interactive "read r <FILE; echo $r; read r; echo $r".
8561 * Redirect moves ->fd to e.g. 10,
8562 * and it is not restored above (we do not restore script fds
8563 * after redirects, we just use new, "moved" fds).
8564 * However for stdin, get_user_input() -> read_line_input(),
8565 * and read builtin, depend on fd == STDIN_FILENO.
8566 */
8567 debug_printf_redir("restoring %d to stdin\n", G.HFILE_stdin->fd);
8568 xmove_fd(G.HFILE_stdin->fd, STDIN_FILENO);
8569 G.HFILE_stdin->fd = STDIN_FILENO;
8570 }
8571
8572 /* If moved, G_interactive_fd stays on new fd, not restoring it */
8573}
8574
8575#if ENABLE_FEATURE_SH_STANDALONE && BB_MMU
8576static void close_saved_fds_and_FILE_fds(void)
8577{
8578 if (G_interactive_fd)
8579 close(G_interactive_fd);
8580 close_all_HFILE_list();
8581}
8582#endif
8583
8584static int internally_opened_fd(int fd, struct squirrel *sq)
8585{
8586 int i;
8587
8588#if ENABLE_HUSH_INTERACTIVE
8589 if (fd == G_interactive_fd)
8590 return 1;
8591#endif
8592 /* If this one of script's fds? */
8593 if (fd_in_HFILEs(fd))
8594 return 1;
8595
8596 if (sq) for (i = 0; sq[i].orig_fd >= 0; i++) {
8597 if (fd == sq[i].moved_to)
8598 return 1;
8599 }
8600 return 0;
8601}
8602
Francis Laniel03061a82024-09-03 19:09:43 +02008603/* sqp != NULL means we squirrel away copies of stdin, stdout,
8604 * and stderr if they are redirected.
8605 * If redirection fails, return 1. This will make caller
8606 * skip command execution and restore already created redirect fds.
8607 */
Francis Laniel110b7692023-12-22 22:02:27 +01008608static int setup_redirects(struct command *prog, struct squirrel **sqp)
8609{
8610 struct redir_struct *redir;
8611
8612 for (redir = prog->redirects; redir; redir = redir->next) {
8613 int newfd;
8614 int closed;
8615
8616 if (redir->rd_type == REDIRECT_HEREDOC2) {
8617 /* "rd_fd<<HERE" case */
Francis Laniel03061a82024-09-03 19:09:43 +02008618 if (save_fd_on_redirect(redir->rd_fd, /*avoid:*/ 0, sqp) < 0)
8619 return 1;
Francis Laniel110b7692023-12-22 22:02:27 +01008620 /* for REDIRECT_HEREDOC2, rd_filename holds _contents_
8621 * of the heredoc */
8622 debug_printf_redir("set heredoc '%s'\n",
8623 redir->rd_filename);
8624 setup_heredoc(redir);
8625 continue;
8626 }
8627
8628 if (redir->rd_dup == REDIRFD_TO_FILE) {
8629 /* "rd_fd<*>file" case (<*> is <,>,>>,<>) */
8630 char *p;
8631 int mode;
8632
8633 if (redir->rd_filename == NULL) {
8634 /* Examples:
8635 * "cmd >" (no filename)
8636 * "cmd > <file" (2nd redirect starts too early)
8637 */
8638 syntax_error("invalid redirect");
Francis Laniel03061a82024-09-03 19:09:43 +02008639 return 1;
Francis Laniel110b7692023-12-22 22:02:27 +01008640 }
8641 mode = redir_table[redir->rd_type].mode;
8642 p = expand_string_to_string(redir->rd_filename,
8643 EXP_FLAG_ESC_GLOB_CHARS, /*unbackslash:*/ 1);
8644 newfd = open_or_warn(p, mode);
8645 free(p);
8646 if (newfd < 0) {
8647 /* Error message from open_or_warn can be lost
8648 * if stderr has been redirected, but bash
8649 * and ash both lose it as well
8650 * (though zsh doesn't!)
8651 */
8652 return 1;
8653 }
Francis Laniel03061a82024-09-03 19:09:43 +02008654 if (newfd == redir->rd_fd && sqp
8655 && sqp != ERR_PTR /* not a redirect in "exec" */
8656 ) {
Francis Laniel110b7692023-12-22 22:02:27 +01008657 /* open() gave us precisely the fd we wanted.
8658 * This means that this fd was not busy
8659 * (not opened to anywhere).
8660 * Remember to close it on restore:
8661 */
8662 *sqp = add_squirrel_closed(*sqp, newfd);
8663 debug_printf_redir("redir to previously closed fd %d\n", newfd);
8664 }
8665 } else {
8666 /* "rd_fd>&rd_dup" or "rd_fd>&-" case */
8667 newfd = redir->rd_dup;
8668 }
8669
8670 if (newfd == redir->rd_fd)
8671 continue;
8672
8673 /* if "N>FILE": move newfd to redir->rd_fd */
8674 /* if "N>&M": dup newfd to redir->rd_fd */
8675 /* if "N>&-": close redir->rd_fd (newfd is REDIRFD_CLOSE) */
8676
8677 closed = save_fd_on_redirect(redir->rd_fd, /*avoid:*/ newfd, sqp);
Francis Laniel03061a82024-09-03 19:09:43 +02008678 if (closed < 0)
8679 return 1; /* error */
Francis Laniel110b7692023-12-22 22:02:27 +01008680 if (newfd == REDIRFD_CLOSE) {
8681 /* "N>&-" means "close me" */
8682 if (!closed) {
8683 /* ^^^ optimization: saving may already
8684 * have closed it. If not... */
8685 close(redir->rd_fd);
8686 }
8687 /* Sometimes we do another close on restore, getting EBADF.
8688 * Consider "echo 3>FILE 3>&-"
8689 * first redirect remembers "need to close 3",
8690 * and second redirect closes 3! Restore code then closes 3 again.
8691 */
8692 } else {
Francis Laniel03061a82024-09-03 19:09:43 +02008693 /* if newfd is a script fd or saved fd, do not allow to use it */
Francis Laniel110b7692023-12-22 22:02:27 +01008694 if (internally_opened_fd(newfd, sqp && sqp != ERR_PTR ? *sqp : NULL)) {
Francis Laniel03061a82024-09-03 19:09:43 +02008695 bb_error_msg("fd#%d is not open", newfd);
8696 return 1;
8697 }
8698 if (dup2(newfd, redir->rd_fd) < 0) {
8699 /* "echo >&99" testcase */
8700 bb_perror_msg("dup2(%d,%d)", newfd, redir->rd_fd);
8701 return 1;
Francis Laniel110b7692023-12-22 22:02:27 +01008702 }
Francis Laniel110b7692023-12-22 22:02:27 +01008703 if (redir->rd_dup == REDIRFD_TO_FILE)
8704 /* "rd_fd > FILE" */
8705 close(newfd);
8706 /* else: "rd_fd > rd_dup" */
8707 }
8708 }
8709 return 0;
8710}
8711
8712static char *find_in_path(const char *arg)
8713{
8714 char *ret = NULL;
8715 const char *PATH = get_local_var_value("PATH");
8716
8717 if (!PATH)
8718 return NULL;
8719
8720 while (1) {
8721 const char *end = strchrnul(PATH, ':');
8722 int sz = end - PATH; /* must be int! */
8723
8724 free(ret);
8725 if (sz != 0) {
8726 ret = xasprintf("%.*s/%s", sz, PATH, arg);
8727 } else {
8728 /* We have xxx::yyyy in $PATH,
8729 * it means "use current dir" */
8730 ret = xstrdup(arg);
8731 }
8732 if (access(ret, F_OK) == 0)
8733 break;
8734
8735 if (*end == '\0') {
8736 free(ret);
8737 return NULL;
8738 }
8739 PATH = end + 1;
8740 }
8741
8742 return ret;
8743}
8744
8745static const struct built_in_command *find_builtin_helper(const char *name,
8746 const struct built_in_command *x,
8747 const struct built_in_command *end)
8748{
8749 while (x != end) {
8750 if (strcmp(name, x->b_cmd) != 0) {
8751 x++;
8752 continue;
8753 }
8754 debug_printf_exec("found builtin '%s'\n", name);
8755 return x;
8756 }
8757 return NULL;
8758}
8759static const struct built_in_command *find_builtin1(const char *name)
8760{
8761 return find_builtin_helper(name, bltins1, &bltins1[ARRAY_SIZE(bltins1)]);
8762}
8763static const struct built_in_command *find_builtin(const char *name)
8764{
8765 const struct built_in_command *x = find_builtin1(name);
8766 if (x)
8767 return x;
8768 return find_builtin_helper(name, bltins2, &bltins2[ARRAY_SIZE(bltins2)]);
8769}
8770
Francis Laniele7ca3a32023-12-22 22:02:42 +01008771#if ENABLE_HUSH_JOB && ENABLE_FEATURE_TAB_COMPLETION
8772static const char * FAST_FUNC hush_command_name(int i)
Francis Laniel110b7692023-12-22 22:02:27 +01008773{
8774 if (/*i >= 0 && */ i < ARRAY_SIZE(bltins1)) {
8775 return bltins1[i].b_cmd;
8776 }
8777 i -= ARRAY_SIZE(bltins1);
8778 if (i < ARRAY_SIZE(bltins2)) {
8779 return bltins2[i].b_cmd;
8780 }
Francis Laniele7ca3a32023-12-22 22:02:42 +01008781# if ENABLE_HUSH_FUNCTIONS
8782 {
8783 struct function *funcp;
8784 i -= ARRAY_SIZE(bltins2);
8785 for (funcp = G.top_func; funcp; funcp = funcp->next) {
8786 if (--i < 0)
8787 return funcp->name;
8788 }
8789 }
8790# endif
Francis Laniel110b7692023-12-22 22:02:27 +01008791 return NULL;
8792}
8793#endif
Francis Laniel36836fc2023-12-22 22:02:28 +01008794#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01008795
Francis Laniel36836fc2023-12-22 22:02:28 +01008796#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01008797static void remove_nested_vars(void)
8798{
8799 struct variable *cur;
8800 struct variable **cur_pp;
8801
8802 cur_pp = &G.top_var;
8803 while ((cur = *cur_pp) != NULL) {
8804 if (cur->var_nest_level <= G.var_nest_level) {
8805 cur_pp = &cur->next;
8806 continue;
8807 }
8808 /* Unexport */
8809 if (cur->flg_export) {
8810 debug_printf_env("unexporting nested '%s'/%u\n", cur->varstr, cur->var_nest_level);
8811 bb_unsetenv(cur->varstr);
8812 }
8813 /* Remove from global list */
8814 *cur_pp = cur->next;
8815 /* Free */
8816 if (!cur->max_len) {
8817 debug_printf_env("freeing nested '%s'/%u\n", cur->varstr, cur->var_nest_level);
8818 free(cur->varstr);
8819 }
8820 free(cur);
8821 }
8822}
8823
8824static void enter_var_nest_level(void)
8825{
8826 G.var_nest_level++;
8827 debug_printf_env("var_nest_level++ %u\n", G.var_nest_level);
8828
8829 /* Try: f() { echo -n .; f; }; f
8830 * struct variable::var_nest_level is uint16_t,
8831 * thus limiting recursion to < 2^16.
8832 * In any case, with 8 Mbyte stack SEGV happens
8833 * not too long after 2^16 recursions anyway.
8834 */
8835 if (G.var_nest_level > 0xff00)
8836 bb_error_msg_and_die("fatal recursion (depth %u)", G.var_nest_level);
8837}
8838
8839static void leave_var_nest_level(void)
8840{
8841 G.var_nest_level--;
8842 debug_printf_env("var_nest_level-- %u\n", G.var_nest_level);
8843 if (HUSH_DEBUG && (int)G.var_nest_level < 0)
8844 bb_simple_error_msg_and_die("BUG: nesting underflow");
8845
8846 remove_nested_vars();
8847}
Francis Laniel36836fc2023-12-22 22:02:28 +01008848#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01008849
8850#if ENABLE_HUSH_FUNCTIONS
8851static struct function **find_function_slot(const char *name)
8852{
8853 struct function *funcp;
8854 struct function **funcpp = &G.top_func;
8855
8856 while ((funcp = *funcpp) != NULL) {
8857 if (strcmp(name, funcp->name) == 0) {
8858 debug_printf_exec("found function '%s'\n", name);
8859 break;
8860 }
8861 funcpp = &funcp->next;
8862 }
8863 return funcpp;
8864}
8865
8866static ALWAYS_INLINE const struct function *find_function(const char *name)
8867{
8868 const struct function *funcp = *find_function_slot(name);
8869 return funcp;
8870}
8871
8872/* Note: takes ownership on name ptr */
8873static struct function *new_function(char *name)
8874{
8875 struct function **funcpp = find_function_slot(name);
8876 struct function *funcp = *funcpp;
8877
8878 if (funcp != NULL) {
8879 struct command *cmd = funcp->parent_cmd;
8880 debug_printf_exec("func %p parent_cmd %p\n", funcp, cmd);
8881 if (!cmd) {
8882 debug_printf_exec("freeing & replacing function '%s'\n", funcp->name);
8883 free(funcp->name);
8884 /* Note: if !funcp->body, do not free body_as_string!
8885 * This is a special case of "-F name body" function:
8886 * body_as_string was not malloced! */
8887 if (funcp->body) {
8888 free_pipe_list(funcp->body);
8889# if !BB_MMU
8890 free(funcp->body_as_string);
8891# endif
8892 }
8893 } else {
8894 debug_printf_exec("reinserting in tree & replacing function '%s'\n", funcp->name);
8895 cmd->argv[0] = funcp->name;
8896 cmd->group = funcp->body;
8897# if !BB_MMU
8898 cmd->group_as_string = funcp->body_as_string;
8899# endif
8900 }
8901 } else {
8902 debug_printf_exec("remembering new function '%s'\n", name);
8903 funcp = *funcpp = xzalloc(sizeof(*funcp));
8904 /*funcp->next = NULL;*/
8905 }
8906
8907 funcp->name = name;
8908 return funcp;
8909}
8910
8911# if ENABLE_HUSH_UNSET
8912static void unset_func(const char *name)
8913{
8914 struct function **funcpp = find_function_slot(name);
8915 struct function *funcp = *funcpp;
8916
8917 if (funcp != NULL) {
8918 debug_printf_exec("freeing function '%s'\n", funcp->name);
8919 *funcpp = funcp->next;
8920 /* funcp is unlinked now, deleting it.
8921 * Note: if !funcp->body, the function was created by
8922 * "-F name body", do not free ->body_as_string
8923 * and ->name as they were not malloced. */
8924 if (funcp->body) {
8925 free_pipe_list(funcp->body);
8926 free(funcp->name);
8927# if !BB_MMU
8928 free(funcp->body_as_string);
8929# endif
8930 }
8931 free(funcp);
8932 }
8933}
8934# endif
8935
8936# if BB_MMU
8937#define exec_function(to_free, funcp, argv) \
8938 exec_function(funcp, argv)
8939# endif
8940static void exec_function(char ***to_free,
8941 const struct function *funcp,
8942 char **argv) NORETURN;
8943static void exec_function(char ***to_free,
8944 const struct function *funcp,
8945 char **argv)
8946{
8947# if BB_MMU
8948 int n;
8949
8950 argv[0] = G.global_argv[0];
8951 G.global_argv = argv;
8952 G.global_argc = n = 1 + string_array_len(argv + 1);
8953
8954// Example when we are here: "cmd | func"
8955// func will run with saved-redirect fds open.
8956// $ f() { echo /proc/self/fd/*; }
8957// $ true | f
8958// /proc/self/fd/0 /proc/self/fd/1 /proc/self/fd/2 /proc/self/fd/255 /proc/self/fd/3
8959// stdio^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ G_interactive_fd^ DIR fd for glob
8960// Same in script:
8961// $ . ./SCRIPT
8962// /proc/self/fd/0 /proc/self/fd/1 /proc/self/fd/2 /proc/self/fd/255 /proc/self/fd/3 /proc/self/fd/4
8963// stdio^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ G_interactive_fd^ opened ./SCRIPT DIR fd for glob
8964// They are CLOEXEC so external programs won't see them, but
8965// for "more correctness" we might want to close those extra fds here:
8966//? close_saved_fds_and_FILE_fds();
8967
8968 /* "we are in a function, ok to use return" */
8969 G_flag_return_in_progress = -1;
8970 enter_var_nest_level();
8971 IF_HUSH_LOCAL(G.func_nest_level++;)
8972
8973 /* On MMU, funcp->body is always non-NULL */
8974 n = run_list(funcp->body);
8975 _exit(n);
8976# else
8977//? close_saved_fds_and_FILE_fds();
8978
8979//TODO: check whether "true | func_with_return" works
8980
8981 re_execute_shell(to_free,
8982 funcp->body_as_string,
8983 G.global_argv[0],
8984 argv + 1,
8985 NULL);
8986# endif
8987}
8988
8989static int run_function(const struct function *funcp, char **argv)
8990{
8991 int rc;
8992 save_arg_t sv;
8993 smallint sv_flg;
8994
8995 save_and_replace_G_args(&sv, argv);
8996
8997 /* "We are in function, ok to use return" */
8998 sv_flg = G_flag_return_in_progress;
8999 G_flag_return_in_progress = -1;
9000
9001 /* Make "local" variables properly shadow previous ones */
9002 IF_HUSH_LOCAL(enter_var_nest_level();)
9003 IF_HUSH_LOCAL(G.func_nest_level++;)
9004
9005 /* On MMU, funcp->body is always non-NULL */
9006# if !BB_MMU
9007 if (!funcp->body) {
9008 /* Function defined by -F */
9009 parse_and_run_string(funcp->body_as_string);
9010 rc = G.last_exitcode;
9011 } else
9012# endif
9013 {
9014 rc = run_list(funcp->body);
9015 }
9016
9017 IF_HUSH_LOCAL(G.func_nest_level--;)
9018 IF_HUSH_LOCAL(leave_var_nest_level();)
9019
9020 G_flag_return_in_progress = sv_flg;
9021# if ENABLE_HUSH_TRAP
9022 debug_printf_exec("G.return_exitcode=-1\n");
9023 G.return_exitcode = -1; /* invalidate stashed return value */
9024# endif
9025
9026 restore_G_args(&sv, argv);
9027
9028 return rc;
9029}
9030#endif /* ENABLE_HUSH_FUNCTIONS */
9031
Francis Laniel36836fc2023-12-22 22:02:28 +01009032#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01009033#if BB_MMU
9034#define exec_builtin(to_free, x, argv) \
9035 exec_builtin(x, argv)
9036#else
9037#define exec_builtin(to_free, x, argv) \
9038 exec_builtin(to_free, argv)
9039#endif
9040static void exec_builtin(char ***to_free,
9041 const struct built_in_command *x,
9042 char **argv) NORETURN;
9043static void exec_builtin(char ***to_free,
9044 const struct built_in_command *x,
9045 char **argv)
9046{
9047#if BB_MMU
9048 int rcode;
9049//? close_saved_fds_and_FILE_fds();
9050 rcode = x->b_function(argv);
9051 fflush_all();
9052 _exit(rcode);
9053#else
9054 fflush_all();
9055 /* On NOMMU, we must never block!
9056 * Example: { sleep 99 | read line; } & echo Ok
9057 */
9058 re_execute_shell(to_free,
9059 argv[0],
9060 G.global_argv[0],
9061 G.global_argv + 1,
9062 argv);
9063#endif
9064}
Francis Laniel36836fc2023-12-22 22:02:28 +01009065#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01009066
Francis Laniel36836fc2023-12-22 22:02:28 +01009067#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01009068static void execvp_or_die(char **argv) NORETURN;
9069static void execvp_or_die(char **argv)
9070{
9071 int e;
9072 debug_printf_exec("execing '%s'\n", argv[0]);
9073 /* Don't propagate SIG_IGN to the child */
9074 if (SPECIAL_JOBSTOP_SIGS != 0)
9075 switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS);
9076 execvp(argv[0], argv);
9077 e = 2;
9078 if (errno == EACCES) e = 126;
9079 if (errno == ENOENT) e = 127;
9080 bb_perror_msg("can't execute '%s'", argv[0]);
9081 _exit(e);
9082}
9083
9084#if ENABLE_HUSH_MODE_X
9085static void x_mode_print_optionally_squoted(const char *str)
9086{
9087 unsigned len;
9088 const char *cp;
9089
9090 cp = str;
9091
9092 /* the set of chars which-cause-string-to-be-squoted mimics bash */
9093 /* test a char with: bash -c 'set -x; echo "CH"' */
9094 if (str[strcspn(str, "\\\"'`$(){}[]<>;#&|~*?!^"
9095 " " "\001\002\003\004\005\006\007"
9096 "\010\011\012\013\014\015\016\017"
9097 "\020\021\022\023\024\025\026\027"
9098 "\030\031\032\033\034\035\036\037"
9099 )
9100 ] == '\0'
9101 ) {
9102 /* string has no special chars */
9103 x_mode_addstr(str);
9104 return;
9105 }
9106
9107 cp = str;
9108 for (;;) {
9109 /* print '....' up to EOL or first squote */
9110 len = (int)(strchrnul(cp, '\'') - cp);
9111 if (len != 0) {
9112 x_mode_addchr('\'');
9113 x_mode_addblock(cp, len);
9114 x_mode_addchr('\'');
9115 cp += len;
9116 }
9117 if (*cp == '\0')
9118 break;
9119 /* string contains squote(s), print them as \' */
9120 x_mode_addchr('\\');
9121 x_mode_addchr('\'');
9122 cp++;
9123 }
9124}
9125static void dump_cmd_in_x_mode(char **argv)
9126{
9127 if (G_x_mode && argv) {
9128 unsigned n;
9129
9130 /* "+[+++...][ cmd...]\n\0" */
9131 x_mode_prefix();
9132 n = 0;
9133 while (argv[n]) {
9134 x_mode_addchr(' ');
9135 if (argv[n][0] == '\0') {
9136 x_mode_addchr('\'');
9137 x_mode_addchr('\'');
9138 } else {
9139 x_mode_print_optionally_squoted(argv[n]);
9140 }
9141 n++;
9142 }
9143 x_mode_flush();
9144 }
9145}
9146#else
9147# define dump_cmd_in_x_mode(argv) ((void)0)
9148#endif
Francis Laniel36836fc2023-12-22 22:02:28 +01009149#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01009150
Francis Laniel36836fc2023-12-22 22:02:28 +01009151#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01009152#if ENABLE_HUSH_COMMAND
9153static void if_command_vV_print_and_exit(char opt_vV, char *cmd, const char *explanation)
9154{
9155 char *to_free;
9156
9157 if (!opt_vV)
9158 return;
9159
9160 to_free = NULL;
9161 if (!explanation) {
9162 char *path = getenv("PATH");
9163 explanation = to_free = find_executable(cmd, &path); /* path == NULL is ok */
9164 if (!explanation)
9165 _exit(1); /* PROG was not found */
9166 if (opt_vV != 'V')
9167 cmd = to_free; /* -v PROG prints "/path/to/PROG" */
9168 }
9169 printf((opt_vV == 'V') ? "%s is %s\n" : "%s\n", cmd, explanation);
9170 free(to_free);
9171 fflush_all();
9172 _exit(0);
9173}
9174#else
9175# define if_command_vV_print_and_exit(a,b,c) ((void)0)
9176#endif
Francis Laniel36836fc2023-12-22 22:02:28 +01009177#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01009178
9179#if BB_MMU
9180#define pseudo_exec_argv(nommu_save, argv, assignment_cnt, argv_expanded) \
9181 pseudo_exec_argv(argv, assignment_cnt, argv_expanded)
9182#define pseudo_exec(nommu_save, command, argv_expanded) \
9183 pseudo_exec(command, argv_expanded)
9184#endif
9185
Francis Laniel36836fc2023-12-22 22:02:28 +01009186#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01009187/* Called after [v]fork() in run_pipe, or from builtin_exec.
9188 * Never returns.
9189 * Don't exit() here. If you don't exec, use _exit instead.
9190 * The at_exit handlers apparently confuse the calling process,
9191 * in particular stdin handling. Not sure why? -- because of vfork! (vda)
9192 */
9193static void pseudo_exec_argv(nommu_save_t *nommu_save,
9194 char **argv, int assignment_cnt,
9195 char **argv_expanded) NORETURN;
9196static NOINLINE void pseudo_exec_argv(nommu_save_t *nommu_save,
9197 char **argv, int assignment_cnt,
9198 char **argv_expanded)
9199{
9200 const struct built_in_command *x;
9201 struct variable **sv_shadowed;
9202 char **new_env;
9203 IF_HUSH_COMMAND(char opt_vV = 0;)
9204 IF_HUSH_FUNCTIONS(const struct function *funcp;)
9205
9206 new_env = expand_assignments(argv, assignment_cnt);
9207 dump_cmd_in_x_mode(new_env);
9208
9209 if (!argv[assignment_cnt]) {
9210 /* Case when we are here: ... | var=val | ...
9211 * (note that we do not exit early, i.e., do not optimize out
9212 * expand_assignments(): think about ... | var=`sleep 1` | ...
9213 */
9214 free_strings(new_env);
Francis Laniele7ca3a32023-12-22 22:02:42 +01009215 _exit_SUCCESS();
Francis Laniel110b7692023-12-22 22:02:27 +01009216 }
9217
9218 sv_shadowed = G.shadowed_vars_pp;
9219#if BB_MMU
9220 G.shadowed_vars_pp = NULL; /* "don't save, free them instead" */
9221#else
9222 G.shadowed_vars_pp = &nommu_save->old_vars;
9223 G.var_nest_level++;
9224#endif
9225 set_vars_and_save_old(new_env);
9226 G.shadowed_vars_pp = sv_shadowed;
9227
9228 if (argv_expanded) {
9229 argv = argv_expanded;
9230 } else {
9231 argv = expand_strvec_to_strvec(argv + assignment_cnt);
9232#if !BB_MMU
9233 nommu_save->argv = argv;
9234#endif
9235 }
9236 dump_cmd_in_x_mode(argv);
9237
9238#if ENABLE_FEATURE_SH_STANDALONE || BB_MMU
9239 if (strchr(argv[0], '/') != NULL)
9240 goto skip;
9241#endif
9242
9243#if ENABLE_HUSH_FUNCTIONS
9244 /* Check if the command matches any functions (this goes before bltins) */
9245 funcp = find_function(argv[0]);
9246 if (funcp)
9247 exec_function(&nommu_save->argv_from_re_execing, funcp, argv);
9248#endif
9249
9250#if ENABLE_HUSH_COMMAND
9251 /* "command BAR": run BAR without looking it up among functions
9252 * "command -v BAR": print "BAR" or "/path/to/BAR"; or exit 1
9253 * "command -V BAR": print "BAR is {a function,a shell builtin,/path/to/BAR}"
9254 */
9255 while (strcmp(argv[0], "command") == 0 && argv[1]) {
9256 char *p;
9257
9258 argv++;
9259 p = *argv;
9260 if (p[0] != '-' || !p[1])
9261 continue; /* bash allows "command command command [-OPT] BAR" */
9262
9263 for (;;) {
9264 p++;
9265 switch (*p) {
9266 case '\0':
9267 argv++;
9268 p = *argv;
9269 if (p[0] != '-' || !p[1])
9270 goto after_opts;
9271 continue; /* next arg is also -opts, process it too */
9272 case 'v':
9273 case 'V':
9274 opt_vV = *p;
9275 continue;
9276 default:
9277 bb_error_msg_and_die("%s: %s: invalid option", "command", argv[0]);
9278 }
9279 }
9280 }
9281 after_opts:
9282# if ENABLE_HUSH_FUNCTIONS
9283 if (opt_vV && find_function(argv[0]))
9284 if_command_vV_print_and_exit(opt_vV, argv[0], "a function");
9285# endif
9286#endif
9287
9288 /* Check if the command matches any of the builtins.
9289 * Depending on context, this might be redundant. But it's
9290 * easier to waste a few CPU cycles than it is to figure out
9291 * if this is one of those cases.
9292 */
9293 /* Why "BB_MMU ? :" difference in logic? -
9294 * On NOMMU, it is more expensive to re-execute shell
9295 * just in order to run echo or test builtin.
9296 * It's better to skip it here and run corresponding
9297 * non-builtin later. */
9298 x = BB_MMU ? find_builtin(argv[0]) : find_builtin1(argv[0]);
9299 if (x) {
9300 if_command_vV_print_and_exit(opt_vV, argv[0], "a shell builtin");
9301 exec_builtin(&nommu_save->argv_from_re_execing, x, argv);
9302 }
9303
9304#if ENABLE_FEATURE_SH_STANDALONE
9305 /* Check if the command matches any busybox applets */
9306 {
9307 int a = find_applet_by_name(argv[0]);
9308 if (a >= 0) {
9309 if_command_vV_print_and_exit(opt_vV, argv[0], "an applet");
9310# if BB_MMU /* see above why on NOMMU it is not allowed */
9311 if (APPLET_IS_NOEXEC(a)) {
9312 /* Do not leak open fds from opened script files etc.
9313 * Testcase: interactive "ls -l /proc/self/fd"
9314 * should not show tty fd open.
9315 */
9316 close_saved_fds_and_FILE_fds();
9317//FIXME: should also close saved redir fds
9318//This casuses test failures in
9319//redir_children_should_not_see_saved_fd_2.tests
9320//redir_children_should_not_see_saved_fd_3.tests
9321//if you replace "busybox find" with just "find" in them
9322 /* Without this, "rm -i FILE" can't be ^C'ed: */
9323 switch_off_special_sigs(G.special_sig_mask);
9324 debug_printf_exec("running applet '%s'\n", argv[0]);
9325 run_noexec_applet_and_exit(a, argv[0], argv);
9326 }
9327# endif
9328 /* Re-exec ourselves */
9329 debug_printf_exec("re-execing applet '%s'\n", argv[0]);
9330 /* Don't propagate SIG_IGN to the child */
9331 if (SPECIAL_JOBSTOP_SIGS != 0)
9332 switch_off_special_sigs(G.special_sig_mask & SPECIAL_JOBSTOP_SIGS);
9333 execv(bb_busybox_exec_path, argv);
9334 /* If they called chroot or otherwise made the binary no longer
9335 * executable, fall through */
9336 }
9337 }
9338#endif
9339
9340#if ENABLE_FEATURE_SH_STANDALONE || BB_MMU
9341 skip:
9342#endif
9343 if_command_vV_print_and_exit(opt_vV, argv[0], NULL);
9344 execvp_or_die(argv);
9345}
9346
9347/* Called after [v]fork() in run_pipe
9348 */
9349static void pseudo_exec(nommu_save_t *nommu_save,
9350 struct command *command,
9351 char **argv_expanded) NORETURN;
9352static void pseudo_exec(nommu_save_t *nommu_save,
9353 struct command *command,
9354 char **argv_expanded)
9355{
9356#if ENABLE_HUSH_FUNCTIONS
9357 if (command->cmd_type == CMD_FUNCDEF) {
9358 /* Ignore funcdefs in pipes:
9359 * true | f() { cmd }
9360 */
9361 _exit(0);
9362 }
9363#endif
9364
9365 if (command->argv) {
9366 pseudo_exec_argv(nommu_save, command->argv,
9367 command->assignment_cnt, argv_expanded);
9368 }
9369
9370 if (command->group) {
9371 /* Cases when we are here:
9372 * ( list )
9373 * { list } &
9374 * ... | ( list ) | ...
9375 * ... | { list } | ...
9376 */
9377#if BB_MMU
9378 int rcode;
9379 debug_printf_exec("pseudo_exec: run_list\n");
9380 reset_traps_to_defaults();
9381 rcode = run_list(command->group);
9382 /* OK to leak memory by not calling free_pipe_list,
9383 * since this process is about to exit */
9384 _exit(rcode);
9385#else
9386 re_execute_shell(&nommu_save->argv_from_re_execing,
9387 command->group_as_string,
9388 G.global_argv[0],
9389 G.global_argv + 1,
9390 NULL);
9391#endif
9392 }
9393
9394 /* Case when we are here: ... | >file */
9395 debug_printf_exec("pseudo_exec'ed null command\n");
Francis Laniele7ca3a32023-12-22 22:02:42 +01009396 _exit_SUCCESS();
Francis Laniel110b7692023-12-22 22:02:27 +01009397}
9398
9399#if ENABLE_HUSH_JOB
9400static const char *get_cmdtext(struct pipe *pi)
9401{
9402 char **argv;
9403 char *p;
9404 int len;
9405
9406 /* This is subtle. ->cmdtext is created only on first backgrounding.
9407 * (Think "cat, <ctrl-z>, fg, <ctrl-z>, fg, <ctrl-z>...." here...)
9408 * On subsequent bg argv is trashed, but we won't use it */
9409 if (pi->cmdtext)
9410 return pi->cmdtext;
9411
9412 argv = pi->cmds[0].argv;
9413 if (!argv) {
9414 pi->cmdtext = xzalloc(1);
9415 return pi->cmdtext;
9416 }
9417 len = 0;
9418 do {
9419 len += strlen(*argv) + 1;
9420 } while (*++argv);
9421 p = xmalloc(len);
9422 pi->cmdtext = p;
9423 argv = pi->cmds[0].argv;
9424 do {
9425 p = stpcpy(p, *argv);
9426 *p++ = ' ';
9427 } while (*++argv);
9428 p[-1] = '\0';
9429 return pi->cmdtext;
9430}
9431
9432static void remove_job_from_table(struct pipe *pi)
9433{
9434 struct pipe *prev_pipe;
9435
9436 if (pi == G.job_list) {
9437 G.job_list = pi->next;
9438 } else {
9439 prev_pipe = G.job_list;
9440 while (prev_pipe->next != pi)
9441 prev_pipe = prev_pipe->next;
9442 prev_pipe->next = pi->next;
9443 }
9444 G.last_jobid = 0;
9445 if (G.job_list)
9446 G.last_jobid = G.job_list->jobid;
9447}
9448
9449static void delete_finished_job(struct pipe *pi)
9450{
9451 remove_job_from_table(pi);
9452 free_pipe(pi);
9453}
9454
9455static void clean_up_last_dead_job(void)
9456{
9457 if (G.job_list && !G.job_list->alive_cmds)
9458 delete_finished_job(G.job_list);
9459}
9460
9461static void insert_job_into_table(struct pipe *pi)
9462{
9463 struct pipe *job, **jobp;
9464 int i;
9465
9466 clean_up_last_dead_job();
9467
9468 /* Find the end of the list, and find next job ID to use */
9469 i = 0;
9470 jobp = &G.job_list;
9471 while ((job = *jobp) != NULL) {
9472 if (job->jobid > i)
9473 i = job->jobid;
9474 jobp = &job->next;
9475 }
9476 pi->jobid = i + 1;
9477
9478 /* Create a new job struct at the end */
9479 job = *jobp = xmemdup(pi, sizeof(*pi));
9480 job->next = NULL;
9481 job->cmds = xzalloc(sizeof(pi->cmds[0]) * pi->num_cmds);
9482 /* Cannot copy entire pi->cmds[] vector! This causes double frees */
9483 for (i = 0; i < pi->num_cmds; i++) {
9484 job->cmds[i].pid = pi->cmds[i].pid;
9485 /* all other fields are not used and stay zero */
9486 }
9487 job->cmdtext = xstrdup(get_cmdtext(pi));
9488
9489 if (G_interactive_fd)
9490 printf("[%u] %u %s\n", job->jobid, (unsigned)job->cmds[0].pid, job->cmdtext);
9491 G.last_jobid = job->jobid;
9492}
9493#endif /* JOB */
9494
9495static int job_exited_or_stopped(struct pipe *pi)
9496{
9497 int rcode, i;
9498
9499 if (pi->alive_cmds != pi->stopped_cmds)
9500 return -1;
9501
9502 /* All processes in fg pipe have exited or stopped */
9503 rcode = 0;
9504 i = pi->num_cmds;
9505 while (--i >= 0) {
9506 rcode = pi->cmds[i].cmd_exitcode;
9507 /* usually last process gives overall exitstatus,
9508 * but with "set -o pipefail", last *failed* process does */
9509 if (G.o_opt[OPT_O_PIPEFAIL] == 0 || rcode != 0)
9510 break;
9511 }
9512 IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;)
9513 return rcode;
9514}
9515
9516static int process_wait_result(struct pipe *fg_pipe, pid_t childpid, int status)
9517{
9518#if ENABLE_HUSH_JOB
9519 struct pipe *pi;
9520#endif
9521 int i, dead;
9522
9523 dead = WIFEXITED(status) || WIFSIGNALED(status);
9524
9525#if DEBUG_JOBS
9526 if (WIFSTOPPED(status))
9527 debug_printf_jobs("pid %d stopped by sig %d (exitcode %d)\n",
9528 childpid, WSTOPSIG(status), WEXITSTATUS(status));
9529 if (WIFSIGNALED(status))
9530 debug_printf_jobs("pid %d killed by sig %d (exitcode %d)\n",
9531 childpid, WTERMSIG(status), WEXITSTATUS(status));
9532 if (WIFEXITED(status))
9533 debug_printf_jobs("pid %d exited, exitcode %d\n",
9534 childpid, WEXITSTATUS(status));
9535#endif
9536 /* Were we asked to wait for a fg pipe? */
9537 if (fg_pipe) {
9538 i = fg_pipe->num_cmds;
9539
9540 while (--i >= 0) {
9541 int rcode;
9542
9543 debug_printf_jobs("check pid %d\n", fg_pipe->cmds[i].pid);
9544 if (fg_pipe->cmds[i].pid != childpid)
9545 continue;
9546 if (dead) {
9547 int ex;
9548 fg_pipe->cmds[i].pid = 0;
9549 fg_pipe->alive_cmds--;
9550 ex = WEXITSTATUS(status);
9551 /* bash prints killer signal's name for *last*
9552 * process in pipe (prints just newline for SIGINT/SIGPIPE).
9553 * Mimic this. Example: "sleep 5" + (^\ or kill -QUIT)
9554 */
9555 if (WIFSIGNALED(status)) {
9556 int sig = WTERMSIG(status);
9557#if ENABLE_HUSH_JOB
9558 if (G.run_list_level == 1
9559 /* ^^^^^ Do not print in nested contexts, example:
9560 * echo `sleep 1; sh -c 'kill -9 $$'` - prints "137", NOT "Killed 137"
9561 */
9562 && i == fg_pipe->num_cmds-1
9563 ) {
9564 /* strsignal() is for bash compat. ~600 bloat versus bbox's get_signame() */
9565 puts(sig == SIGINT || sig == SIGPIPE ? "" : strsignal(sig));
9566 }
9567#endif
9568 /* TODO: if (WCOREDUMP(status)) + " (core dumped)"; */
9569 /* MIPS has 128 sigs (1..128), if sig==128,
9570 * 128 + sig would result in exitcode 256 -> 0!
9571 */
9572 ex = 128 | sig;
9573 }
9574 fg_pipe->cmds[i].cmd_exitcode = ex;
9575 } else {
9576 fg_pipe->stopped_cmds++;
9577 }
9578 debug_printf_jobs("fg_pipe: alive_cmds %d stopped_cmds %d\n",
9579 fg_pipe->alive_cmds, fg_pipe->stopped_cmds);
9580 rcode = job_exited_or_stopped(fg_pipe);
9581 if (rcode >= 0) {
9582/* Note: *non-interactive* bash does not continue if all processes in fg pipe
9583 * are stopped. Testcase: "cat | cat" in a script (not on command line!)
9584 * and "killall -STOP cat" */
9585 if (G_interactive_fd) {
9586#if ENABLE_HUSH_JOB
9587 if (fg_pipe->alive_cmds != 0)
9588 insert_job_into_table(fg_pipe);
9589#endif
9590 return rcode;
9591 }
9592 if (fg_pipe->alive_cmds == 0)
9593 return rcode;
9594 }
9595 /* There are still running processes in the fg_pipe */
9596 return -1;
9597 }
9598 /* It wasn't in fg_pipe, look for process in bg pipes */
9599 }
9600
9601#if ENABLE_HUSH_JOB
9602 /* We were asked to wait for bg or orphaned children */
9603 /* No need to remember exitcode in this case */
9604 for (pi = G.job_list; pi; pi = pi->next) {
9605 for (i = 0; i < pi->num_cmds; i++) {
9606 if (pi->cmds[i].pid == childpid)
9607 goto found_pi_and_prognum;
9608 }
9609 }
9610 /* Happens when shell is used as init process (init=/bin/sh) */
9611 debug_printf("checkjobs: pid %d was not in our list!\n", childpid);
9612 return -1; /* this wasn't a process from fg_pipe */
9613
9614 found_pi_and_prognum:
9615 if (dead) {
9616 /* child exited */
9617 int rcode = WEXITSTATUS(status);
9618 if (WIFSIGNALED(status))
9619 /* NB: not 128 + sig, MIPS has sig 128 */
9620 rcode = 128 | WTERMSIG(status);
9621 pi->cmds[i].cmd_exitcode = rcode;
9622 if (G.last_bg_pid == pi->cmds[i].pid)
9623 G.last_bg_pid_exitcode = rcode;
9624 pi->cmds[i].pid = 0;
9625 pi->alive_cmds--;
9626 if (!pi->alive_cmds) {
9627# if ENABLE_HUSH_BASH_COMPAT
9628 G.dead_job_exitcode = job_exited_or_stopped(pi);
9629# endif
9630 if (G_interactive_fd) {
9631 printf(JOB_STATUS_FORMAT, pi->jobid,
9632 "Done", pi->cmdtext);
9633 delete_finished_job(pi);
9634 } else {
9635/*
9636 * bash deletes finished jobs from job table only in interactive mode,
9637 * after "jobs" cmd, or if pid of a new process matches one of the old ones
9638 * (see cleanup_dead_jobs(), delete_old_job(), J_NOTIFIED in bash source).
9639 * Testcase script: "(exit 3) & sleep 1; wait %1; echo $?" prints 3 in bash.
9640 * We only retain one "dead" job, if it's the single job on the list.
9641 * This covers most of real-world scenarios where this is useful.
9642 */
9643 if (pi != G.job_list)
9644 delete_finished_job(pi);
9645 }
9646 }
9647 } else {
9648 /* child stopped */
9649 pi->stopped_cmds++;
9650 }
9651#endif
9652 return -1; /* this wasn't a process from fg_pipe */
9653}
9654
9655/* Check to see if any processes have exited -- if they have,
9656 * figure out why and see if a job has completed.
9657 *
9658 * If non-NULL fg_pipe: wait for its completion or stop.
9659 * Return its exitcode or zero if stopped.
9660 *
9661 * Alternatively (fg_pipe == NULL, waitfor_pid != 0):
9662 * waitpid(WNOHANG), if waitfor_pid exits or stops, return exitcode+1,
9663 * else return <0 if waitpid errors out (e.g. ECHILD: nothing to wait for)
9664 * or 0 if no children changed status.
9665 *
9666 * Alternatively (fg_pipe == NULL, waitfor_pid == 0),
9667 * return <0 if waitpid errors out (e.g. ECHILD: nothing to wait for)
9668 * or 0 if no children changed status.
9669 */
9670static int checkjobs(struct pipe *fg_pipe, pid_t waitfor_pid)
9671{
9672 int attributes;
9673 int status;
9674 int rcode = 0;
9675
9676 debug_printf_jobs("checkjobs %p\n", fg_pipe);
9677
9678 attributes = WUNTRACED;
9679 if (fg_pipe == NULL)
9680 attributes |= WNOHANG;
9681
9682 errno = 0;
9683#if ENABLE_HUSH_FAST
9684 if (G.handled_SIGCHLD == G.count_SIGCHLD) {
9685//bb_error_msg("[%d] checkjobs: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d children?:%d fg_pipe:%p",
9686//getpid(), G.count_SIGCHLD, G.handled_SIGCHLD, G.we_have_children, fg_pipe);
9687 /* There was neither fork nor SIGCHLD since last waitpid */
9688 /* Avoid doing waitpid syscall if possible */
9689 if (!G.we_have_children) {
9690 errno = ECHILD;
9691 return -1;
9692 }
9693 if (fg_pipe == NULL) { /* is WNOHANG set? */
9694 /* We have children, but they did not exit
9695 * or stop yet (we saw no SIGCHLD) */
9696 return 0;
9697 }
9698 /* else: !WNOHANG, waitpid will block, can't short-circuit */
9699 }
9700#endif
9701
9702/* Do we do this right?
9703 * bash-3.00# sleep 20 | false
9704 * <ctrl-Z pressed>
9705 * [3]+ Stopped sleep 20 | false
9706 * bash-3.00# echo $?
9707 * 1 <========== bg pipe is not fully done, but exitcode is already known!
9708 * [hush 1.14.0: yes we do it right]
9709 */
9710 while (1) {
9711 pid_t childpid;
9712#if ENABLE_HUSH_FAST
9713 int i;
9714 i = G.count_SIGCHLD;
9715#endif
9716 childpid = waitpid(-1, &status, attributes);
9717 if (childpid <= 0) {
9718 if (childpid && errno != ECHILD)
9719 bb_simple_perror_msg("waitpid");
9720#if ENABLE_HUSH_FAST
9721 else { /* Until next SIGCHLD, waitpid's are useless */
9722 G.we_have_children = (childpid == 0);
9723 G.handled_SIGCHLD = i;
9724//bb_error_msg("[%d] checkjobs: waitpid returned <= 0, G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD);
9725 }
9726#endif
9727 /* ECHILD (no children), or 0 (no change in children status) */
9728 rcode = childpid;
9729 break;
9730 }
9731 rcode = process_wait_result(fg_pipe, childpid, status);
9732 if (rcode >= 0) {
9733 /* fg_pipe exited or stopped */
9734 break;
9735 }
9736 if (childpid == waitfor_pid) { /* "wait PID" */
9737 debug_printf_exec("childpid==waitfor_pid:%d status:0x%08x\n", childpid, status);
9738 rcode = WEXITSTATUS(status);
9739 if (WIFSIGNALED(status))
9740 rcode = 128 | WTERMSIG(status);
9741 if (WIFSTOPPED(status))
9742 /* bash: "cmd & wait $!" and cmd stops: $? = 128 | stopsig */
9743 rcode = 128 | WSTOPSIG(status);
9744 rcode++;
9745 break; /* "wait PID" called us, give it exitcode+1 */
9746 }
9747#if ENABLE_HUSH_BASH_COMPAT
9748 if (-1 == waitfor_pid /* "wait -n" (wait for any one job) */
9749 && G.dead_job_exitcode >= 0 /* some job did finish */
9750 ) {
9751 debug_printf_exec("waitfor_pid:-1\n");
9752 rcode = G.dead_job_exitcode + 1;
9753 break;
9754 }
9755#endif
9756 /* This wasn't one of our processes, or */
9757 /* fg_pipe still has running processes, do waitpid again */
9758 } /* while (waitpid succeeds)... */
9759
9760 return rcode;
9761}
9762
9763#if ENABLE_HUSH_JOB
9764static int checkjobs_and_fg_shell(struct pipe *fg_pipe)
9765{
9766 pid_t p;
9767 int rcode = checkjobs(fg_pipe, 0 /*(no pid to wait for)*/);
9768 if (G_saved_tty_pgrp) {
9769 /* Job finished, move the shell to the foreground */
9770 p = getpgrp(); /* our process group id */
9771 debug_printf_jobs("fg'ing ourself: getpgrp()=%d\n", (int)p);
9772 tcsetpgrp(G_interactive_fd, p);
9773 }
9774 return rcode;
9775}
9776#endif
Francis Laniel03061a82024-09-03 19:09:43 +02009777#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01009778
9779/* Start all the jobs, but don't wait for anything to finish.
9780 * See checkjobs().
9781 *
9782 * Return code is normally -1, when the caller has to wait for children
9783 * to finish to determine the exit status of the pipe. If the pipe
9784 * is a simple builtin command, however, the action is done by the
9785 * time run_pipe returns, and the exit code is provided as the
9786 * return value.
9787 *
9788 * Returns -1 only if started some children. IOW: we have to
9789 * mask out retvals of builtins etc with 0xff!
9790 *
9791 * The only case when we do not need to [v]fork is when the pipe
9792 * is single, non-backgrounded, non-subshell command. Examples:
9793 * cmd ; ... { list } ; ...
9794 * cmd && ... { list } && ...
9795 * cmd || ... { list } || ...
9796 * If it is, then we can run cmd as a builtin, NOFORK,
9797 * or (if SH_STANDALONE) an applet, and we can run the { list }
9798 * with run_list. If it isn't one of these, we fork and exec cmd.
9799 *
9800 * Cases when we must fork:
9801 * non-single: cmd | cmd
9802 * backgrounded: cmd & { list } &
9803 * subshell: ( list ) [&]
9804 */
Francis Laniel03061a82024-09-03 19:09:43 +02009805static void set_G_ifs(void)
9806{
9807 /* Testcase: set -- q w e; (IFS='' echo "$*"; IFS=''; echo "$*"); echo "$*"
9808 * Result should be 3 lines: q w e, qwe, q w e
9809 */
9810 if (G.ifs_whitespace != G.ifs)
9811 free(G.ifs_whitespace);
9812 G.ifs = get_local_var_value("IFS");
9813 if (G.ifs) {
9814 char *p;
9815 G.ifs_whitespace = (char*)G.ifs;
9816 p = skip_whitespace(G.ifs);
9817 if (*p) {
9818 /* Not all $IFS is whitespace */
9819 char *d;
9820 int len = p - G.ifs;
9821 p = skip_non_whitespace(p);
9822 G.ifs_whitespace = xmalloc(len + strlen(p) + 1); /* can overestimate */
9823 d = mempcpy(G.ifs_whitespace, G.ifs, len);
9824 while (*p) {
9825 if (isspace(*p))
9826 *d++ = *p;
9827 p++;
9828 }
9829 *d = '\0';
9830 }
9831 } else {
9832 G.ifs = defifs;
9833 G.ifs_whitespace = (char*)G.ifs;
9834 }
9835}
9836#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01009837#if !ENABLE_HUSH_MODE_X
9838#define redirect_and_varexp_helper(command, sqp, argv_expanded) \
9839 redirect_and_varexp_helper(command, sqp)
9840#endif
9841static int redirect_and_varexp_helper(
9842 struct command *command,
9843 struct squirrel **sqp,
9844 char **argv_expanded)
9845{
9846 /* Assignments occur before redirects. Try:
9847 * a=`sleep 1` sleep 2 3>/qwe/rty
9848 */
9849
9850 char **new_env = expand_assignments(command->argv, command->assignment_cnt);
9851 dump_cmd_in_x_mode(new_env);
9852 dump_cmd_in_x_mode(argv_expanded);
9853 /* this takes ownership of new_env[i] elements, and frees new_env: */
9854 set_vars_and_save_old(new_env);
9855
9856 return setup_redirects(command, sqp);
9857}
Francis Laniel36836fc2023-12-22 22:02:28 +01009858#endif /* !__U_BOOT__ */
9859
Francis Laniel110b7692023-12-22 22:02:27 +01009860static NOINLINE int run_pipe(struct pipe *pi)
9861{
9862 static const char *const null_ptr = NULL;
9863
9864 int cmd_no;
Francis Laniel36836fc2023-12-22 22:02:28 +01009865#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01009866 int next_infd;
Francis Laniel36836fc2023-12-22 22:02:28 +01009867#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01009868 struct command *command;
9869 char **argv_expanded;
9870 char **argv;
Francis Laniel36836fc2023-12-22 22:02:28 +01009871#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01009872 struct squirrel *squirrel = NULL;
Francis Laniel36836fc2023-12-22 22:02:28 +01009873#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01009874 int rcode;
9875
Francis Laniel36836fc2023-12-22 22:02:28 +01009876#ifdef __U_BOOT__
9877 /*
9878 * Set rcode here to avoid returning a garbage value in the middle of
9879 * the function.
9880 * Also, if an error occurs, rcode value would be changed and last
9881 * return will signal the error.
9882 */
9883 rcode = 0;
9884#endif /* __U_BOOT__ */
9885
Francis Laniel110b7692023-12-22 22:02:27 +01009886 debug_printf_exec("run_pipe start: members:%d\n", pi->num_cmds);
9887 debug_enter();
9888
Francis Laniel03061a82024-09-03 19:09:43 +02009889 set_G_ifs();
Francis Laniel110b7692023-12-22 22:02:27 +01009890
Francis Laniel36836fc2023-12-22 22:02:28 +01009891#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01009892 IF_HUSH_JOB(pi->pgrp = -1;)
9893 pi->stopped_cmds = 0;
Francis Laniel36836fc2023-12-22 22:02:28 +01009894#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01009895 command = &pi->cmds[0];
9896 argv_expanded = NULL;
9897
Francis Laniel36836fc2023-12-22 22:02:28 +01009898#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01009899 if (pi->num_cmds != 1
9900 || pi->followup == PIPE_BG
9901 || command->cmd_type == CMD_SUBSHELL
9902 ) {
9903 goto must_fork;
9904 }
9905
9906 pi->alive_cmds = 1;
Francis Laniel36836fc2023-12-22 22:02:28 +01009907#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01009908
9909 debug_printf_exec(": group:%p argv:'%s'\n",
9910 command->group, command->argv ? command->argv[0] : "NONE");
9911
9912 if (command->group) {
9913#if ENABLE_HUSH_FUNCTIONS
9914 if (command->cmd_type == CMD_FUNCDEF) {
9915 /* "executing" func () { list } */
9916 struct function *funcp;
9917
9918 funcp = new_function(command->argv[0]);
9919 /* funcp->name is already set to argv[0] */
9920 funcp->body = command->group;
9921# if !BB_MMU
9922 funcp->body_as_string = command->group_as_string;
9923 command->group_as_string = NULL;
9924# endif
9925 command->group = NULL;
9926 command->argv[0] = NULL;
9927 debug_printf_exec("cmd %p has child func at %p\n", command, funcp);
9928 funcp->parent_cmd = command;
9929 command->child_func = funcp;
9930
9931 debug_printf_exec("run_pipe: return EXIT_SUCCESS\n");
9932 debug_leave();
9933 return EXIT_SUCCESS;
9934 }
9935#endif
9936 /* { list } */
9937 debug_printf_exec("non-subshell group\n");
9938 rcode = 1; /* exitcode if redir failed */
Francis Laniel36836fc2023-12-22 22:02:28 +01009939#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01009940 if (setup_redirects(command, &squirrel) == 0) {
Francis Laniel9492c942023-12-22 22:02:39 +01009941#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01009942 debug_printf_exec(": run_list\n");
9943//FIXME: we need to pass squirrel down into run_list()
9944//for SH_STANDALONE case, or else this construct:
9945// { find /proc/self/fd; true; } >FILE; cmd2
9946//has no way of closing saved fd#1 for "find",
9947//and in SH_STANDALONE mode, "find" is not execed,
9948//therefore CLOEXEC on saved fd does not help.
9949 rcode = run_list(command->group) & 0xff;
Francis Laniel9492c942023-12-22 22:02:39 +01009950#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01009951 }
9952 restore_redirects(squirrel);
Francis Laniel36836fc2023-12-22 22:02:28 +01009953#endif /* !__U_BOOT__ */
Francis Laniel9492c942023-12-22 22:02:39 +01009954 IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;)
Francis Laniel110b7692023-12-22 22:02:27 +01009955 debug_leave();
9956 debug_printf_exec("run_pipe: return %d\n", rcode);
9957 return rcode;
9958 }
9959
9960 argv = command->argv ? command->argv : (char **) &null_ptr;
9961 {
Francis Laniel36836fc2023-12-22 22:02:28 +01009962#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01009963 const struct built_in_command *x;
9964 IF_HUSH_FUNCTIONS(const struct function *funcp;)
9965 IF_NOT_HUSH_FUNCTIONS(enum { funcp = 0 };)
9966 struct variable **sv_shadowed;
Francis Laniel36836fc2023-12-22 22:02:28 +01009967#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01009968 struct variable *old_vars;
9969
9970#if ENABLE_HUSH_LINENO_VAR
9971 G.execute_lineno = command->lineno;
9972#endif
9973
9974 if (argv[command->assignment_cnt] == NULL) {
9975 /* Assignments, but no command.
9976 * Ensure redirects take effect (that is, create files).
9977 * Try "a=t >file"
9978 */
9979 unsigned i;
9980 G.expand_exitcode = 0;
9981 only_assignments:
Francis Laniel36836fc2023-12-22 22:02:28 +01009982#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +01009983 rcode = setup_redirects(command, &squirrel);
9984 restore_redirects(squirrel);
Francis Laniel36836fc2023-12-22 22:02:28 +01009985#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +01009986
9987 /* Set shell variables */
9988 i = 0;
9989 while (i < command->assignment_cnt) {
9990 char *p = expand_string_to_string(argv[i],
9991 EXP_FLAG_ESC_GLOB_CHARS,
9992 /*unbackslash:*/ 1
9993 );
9994#if ENABLE_HUSH_MODE_X
9995 if (G_x_mode) {
9996 char *eq;
9997 if (i == 0)
9998 x_mode_prefix();
9999 x_mode_addchr(' ');
10000 eq = strchrnul(p, '=');
10001 if (*eq) eq++;
10002 x_mode_addblock(p, (eq - p));
10003 x_mode_print_optionally_squoted(eq);
10004 x_mode_flush();
10005 }
10006#endif
10007 debug_printf_env("set shell var:'%s'->'%s'\n", *argv, p);
Francis Laniel36836fc2023-12-22 22:02:28 +010010008#ifndef __U_BOOT__
Francis Laniele7ca3a32023-12-22 22:02:42 +010010009 if (set_local_var0(p)) {
Francis Laniel36836fc2023-12-22 22:02:28 +010010010#else /* __U_BOOT__ */
10011 if (set_local_var_modern(p, /*flag:*/ 0)) {
10012#endif
Francis Laniel110b7692023-12-22 22:02:27 +010010013 /* assignment to readonly var / putenv error? */
10014 rcode = 1;
10015 }
10016 i++;
10017 }
10018 /* Redirect error sets $? to 1. Otherwise,
10019 * if evaluating assignment value set $?, retain it.
10020 * Else, clear $?:
10021 * false; q=`exit 2`; echo $? - should print 2
10022 * false; x=1; echo $? - should print 0
10023 * Because of the 2nd case, we can't just use G.last_exitcode.
10024 */
10025 if (rcode == 0)
10026 rcode = G.expand_exitcode;
10027 IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;)
10028 debug_leave();
10029 debug_printf_exec("run_pipe: return %d\n", rcode);
10030 return rcode;
10031 }
10032
10033 /* Expand the rest into (possibly) many strings each */
10034#if defined(CMD_TEST2_SINGLEWORD_NOGLOB)
10035 if (command->cmd_type == CMD_TEST2_SINGLEWORD_NOGLOB)
10036 argv_expanded = expand_strvec_to_strvec_singleword_noglob(argv + command->assignment_cnt);
10037 else
10038#endif
10039#if defined(CMD_SINGLEWORD_NOGLOB)
10040 if (command->cmd_type == CMD_SINGLEWORD_NOGLOB)
10041 argv_expanded = expand_strvec_to_strvec_singleword_noglob(argv + command->assignment_cnt);
10042 else
10043#endif
10044 argv_expanded = expand_strvec_to_strvec(argv + command->assignment_cnt);
10045
10046 /* If someone gives us an empty string: `cmd with empty output` */
10047 if (!argv_expanded[0]) {
10048 free(argv_expanded);
10049 /* `false` still has to set exitcode 1 */
10050 G.expand_exitcode = G.last_exitcode;
10051 goto only_assignments;
10052 }
10053
10054 old_vars = NULL;
Francis Laniel36836fc2023-12-22 22:02:28 +010010055#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +010010056 sv_shadowed = G.shadowed_vars_pp;
10057
10058 /* Check if argv[0] matches any functions (this goes before bltins) */
10059 IF_HUSH_FUNCTIONS(funcp = find_function(argv_expanded[0]);)
10060 IF_HUSH_FUNCTIONS(x = NULL;)
10061 IF_HUSH_FUNCTIONS(if (!funcp))
10062 x = find_builtin(argv_expanded[0]);
10063 if (x || funcp) {
10064 if (x && x->b_function == builtin_exec && argv_expanded[1] == NULL) {
10065 debug_printf("exec with redirects only\n");
10066 /*
10067 * Variable assignments are executed, but then "forgotten":
10068 * a=`sleep 1;echo A` exec 3>&-; echo $a
10069 * sleeps, but prints nothing.
10070 */
10071 enter_var_nest_level();
10072 G.shadowed_vars_pp = &old_vars;
10073 rcode = redirect_and_varexp_helper(command,
10074 /*squirrel:*/ ERR_PTR,
10075 argv_expanded
10076 );
10077 G.shadowed_vars_pp = sv_shadowed;
10078 /* rcode=1 can be if redir file can't be opened */
10079
10080 goto clean_up_and_ret1;
10081 }
10082
10083 /* Bump var nesting, or this will leak exported $a:
10084 * a=b true; env | grep ^a=
10085 */
10086 enter_var_nest_level();
10087 /* Collect all variables "shadowed" by helper
10088 * (IOW: old vars overridden by "var1=val1 var2=val2 cmd..." syntax)
10089 * into old_vars list:
10090 */
10091 G.shadowed_vars_pp = &old_vars;
10092 rcode = redirect_and_varexp_helper(command, &squirrel, argv_expanded);
10093 if (rcode == 0) {
10094 if (!funcp) {
10095 /* Do not collect *to old_vars list* vars shadowed
10096 * by e.g. "local VAR" builtin (collect them
10097 * in the previously nested list instead):
10098 * don't want them to be restored immediately
10099 * after "local" completes.
10100 */
10101 G.shadowed_vars_pp = sv_shadowed;
10102
10103 debug_printf_exec(": builtin '%s' '%s'...\n",
10104 x->b_cmd, argv_expanded[1]);
10105 fflush_all();
10106 rcode = x->b_function(argv_expanded) & 0xff;
10107 fflush_all();
10108 }
10109#if ENABLE_HUSH_FUNCTIONS
10110 else {
10111 debug_printf_exec(": function '%s' '%s'...\n",
10112 funcp->name, argv_expanded[1]);
10113 rcode = run_function(funcp, argv_expanded) & 0xff;
10114 /*
10115 * But do collect *to old_vars list* vars shadowed
10116 * within function execution. To that end, restore
10117 * this pointer _after_ function run:
10118 */
10119 G.shadowed_vars_pp = sv_shadowed;
10120 }
10121#endif
10122 }
10123 } else
10124 if (ENABLE_FEATURE_SH_NOFORK && NUM_APPLETS > 1) {
10125 int n = find_applet_by_name(argv_expanded[0]);
10126 if (n < 0 || !APPLET_IS_NOFORK(n))
10127 goto must_fork;
10128
10129 enter_var_nest_level();
10130 /* Collect all variables "shadowed" by helper into old_vars list */
10131 G.shadowed_vars_pp = &old_vars;
10132 rcode = redirect_and_varexp_helper(command, &squirrel, argv_expanded);
10133 G.shadowed_vars_pp = sv_shadowed;
10134
10135 if (rcode == 0) {
10136 debug_printf_exec(": run_nofork_applet '%s' '%s'...\n",
10137 argv_expanded[0], argv_expanded[1]);
10138 /*
10139 * Note: signals (^C) can't interrupt here.
10140 * We remember them and they will be acted upon
10141 * after applet returns.
10142 * This makes applets which can run for a long time
10143 * and/or wait for user input ineligible for NOFORK:
10144 * for example, "yes" or "rm" (rm -i waits for input).
10145 */
10146 rcode = run_nofork_applet(n, argv_expanded);
10147 }
10148 } else
10149 goto must_fork;
10150
10151 restore_redirects(squirrel);
10152 clean_up_and_ret1:
10153 leave_var_nest_level();
10154 add_vars(old_vars);
10155
10156 /*
10157 * Try "usleep 99999999" + ^C + "echo $?"
10158 * with FEATURE_SH_NOFORK=y.
10159 */
10160 if (!funcp) {
10161 /* It was builtin or nofork.
10162 * if this would be a real fork/execed program,
10163 * it should have died if a fatal sig was received.
10164 * But OTOH, there was no separate process,
10165 * the sig was sent to _shell_, not to non-existing
10166 * child.
10167 * Let's just handle ^C only, this one is obvious:
10168 * we aren't ok with exitcode 0 when ^C was pressed
10169 * during builtin/nofork.
10170 */
10171 if (sigismember(&G.pending_set, SIGINT))
10172 rcode = 128 | SIGINT;
10173 }
10174 free(argv_expanded);
10175 IF_HAS_KEYWORDS(if (pi->pi_inverted) rcode = !rcode;)
10176 debug_leave();
10177 debug_printf_exec("run_pipe return %d\n", rcode);
10178 return rcode;
Francis Laniel36836fc2023-12-22 22:02:28 +010010179#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +010010180 }
10181
Francis Laniel36836fc2023-12-22 22:02:28 +010010182#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +010010183 must_fork:
10184 /* NB: argv_expanded may already be created, and that
10185 * might include `cmd` runs! Do not rerun it! We *must*
10186 * use argv_expanded if it's non-NULL */
10187
10188 /* Going to fork a child per each pipe member */
10189 pi->alive_cmds = 0;
10190 next_infd = 0;
Francis Laniel36836fc2023-12-22 22:02:28 +010010191#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +010010192
10193 cmd_no = 0;
10194 while (cmd_no < pi->num_cmds) {
Francis Laniel36836fc2023-12-22 22:02:28 +010010195#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +010010196 struct fd_pair pipefds;
10197#if !BB_MMU
10198 int sv_var_nest_level = G.var_nest_level;
10199 volatile nommu_save_t nommu_save;
10200 nommu_save.old_vars = NULL;
10201 nommu_save.argv = NULL;
10202 nommu_save.argv_from_re_execing = NULL;
10203#endif
Francis Laniel36836fc2023-12-22 22:02:28 +010010204#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +010010205 command = &pi->cmds[cmd_no];
10206 cmd_no++;
Francis Lanielbfc406a2023-12-22 22:02:33 +010010207
10208#ifdef __U_BOOT__
10209 /* Replace argv and argc by expanded if it exists. */
10210 if (argv_expanded) {
10211 /*
10212 * We need to save a pointer to argv, we will restore it
10213 * later, so it will be freed when pipe is freed.
10214 */
10215 argv = command->argv;
10216
10217 /*
10218 * After expansion, there can be more or less argument, so we need to
10219 * update argc, for example:
10220 * - More arguments:
10221 * foo='bar quuz'
10222 * echo $foo
10223 * - Less arguments:
10224 * echo $foo (if foo was never set)
10225 */
10226 command->argc = list_size(argv_expanded);
10227 command->argv = argv_expanded;
10228 }
10229#endif /* __U_BOOT__ */
10230 if (command->argv) {
Francis Laniel110b7692023-12-22 22:02:27 +010010231 debug_printf_exec(": pipe member '%s' '%s'...\n",
10232 command->argv[0], command->argv[1]);
10233 } else {
10234 debug_printf_exec(": pipe member with no argv\n");
10235 }
10236
Francis Laniel36836fc2023-12-22 22:02:28 +010010237#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +010010238 /* pipes are inserted between pairs of commands */
10239 pipefds.rd = 0;
10240 pipefds.wr = 1;
10241 if (cmd_no < pi->num_cmds)
10242 xpiped_pair(pipefds);
10243
10244#if ENABLE_HUSH_LINENO_VAR
10245 G.execute_lineno = command->lineno;
10246#endif
10247
10248 command->pid = BB_MMU ? fork() : vfork();
10249 if (!command->pid) { /* child */
10250#if ENABLE_HUSH_JOB
10251 disable_restore_tty_pgrp_on_exit();
10252 CLEAR_RANDOM_T(&G.random_gen); /* or else $RANDOM repeats in child */
10253
10254 /* Every child adds itself to new process group
10255 * with pgid == pid_of_first_child_in_pipe */
10256 if (G.run_list_level == 1 && G_interactive_fd) {
10257 pid_t pgrp;
10258 pgrp = pi->pgrp;
10259 if (pgrp < 0) /* true for 1st process only */
10260 pgrp = getpid();
10261 if (setpgid(0, pgrp) == 0
10262 && pi->followup != PIPE_BG
10263 && G_saved_tty_pgrp /* we have ctty */
10264 ) {
10265 /* We do it in *every* child, not just first,
10266 * to avoid races */
10267 tcsetpgrp(G_interactive_fd, pgrp);
10268 }
10269 }
10270#endif
10271 if (pi->alive_cmds == 0 && pi->followup == PIPE_BG) {
10272 /* 1st cmd in backgrounded pipe
10273 * should have its stdin /dev/null'ed */
10274 close(0);
10275 if (open(bb_dev_null, O_RDONLY))
10276 xopen("/", O_RDONLY);
10277 } else {
10278 xmove_fd(next_infd, 0);
10279 }
10280 xmove_fd(pipefds.wr, 1);
10281 if (pipefds.rd > 1)
10282 close(pipefds.rd);
10283 /* Like bash, explicit redirects override pipes,
10284 * and the pipe fd (fd#1) is available for dup'ing:
10285 * "cmd1 2>&1 | cmd2": fd#1 is duped to fd#2, thus stderr
10286 * of cmd1 goes into pipe.
10287 */
10288 if (setup_redirects(command, NULL)) {
10289 /* Happens when redir file can't be opened:
10290 * $ hush -c 'echo FOO >&2 | echo BAR 3>/qwe/rty; echo BAZ'
10291 * FOO
10292 * hush: can't open '/qwe/rty': No such file or directory
10293 * BAZ
10294 * (echo BAR is not executed, it hits _exit(1) below)
10295 */
10296 _exit(1);
10297 }
10298
10299 /* Stores to nommu_save list of env vars putenv'ed
10300 * (NOMMU, on MMU we don't need that) */
10301 /* cast away volatility... */
10302 pseudo_exec((nommu_save_t*) &nommu_save, command, argv_expanded);
10303 /* pseudo_exec() does not return */
10304 }
10305
10306 /* parent or error */
10307#if ENABLE_HUSH_FAST
10308 G.count_SIGCHLD++;
10309//bb_error_msg("[%d] fork in run_pipe: G.count_SIGCHLD:%d G.handled_SIGCHLD:%d", getpid(), G.count_SIGCHLD, G.handled_SIGCHLD);
10310#endif
10311 enable_restore_tty_pgrp_on_exit();
10312#if !BB_MMU
10313 /* Clean up after vforked child */
10314 free(nommu_save.argv);
10315 free(nommu_save.argv_from_re_execing);
10316 G.var_nest_level = sv_var_nest_level;
10317 remove_nested_vars();
10318 add_vars(nommu_save.old_vars);
10319#endif
10320 free(argv_expanded);
10321 argv_expanded = NULL;
10322 if (command->pid < 0) { /* [v]fork failed */
10323 /* Clearly indicate, was it fork or vfork */
10324 bb_simple_perror_msg(BB_MMU ? "vfork"+1 : "vfork");
10325 } else {
10326 pi->alive_cmds++;
10327#if ENABLE_HUSH_JOB
10328 /* Second and next children need to know pid of first one */
10329 if (pi->pgrp < 0)
10330 pi->pgrp = command->pid;
10331#endif
10332 }
10333
10334 if (cmd_no > 1)
10335 close(next_infd);
10336 if (cmd_no < pi->num_cmds)
10337 close(pipefds.wr);
10338 /* Pass read (output) pipe end to next iteration */
10339 next_infd = pipefds.rd;
Francis Laniel36836fc2023-12-22 22:02:28 +010010340#else /* __U_BOOT__ */
10341 /* Process the command */
10342 rcode = cmd_process(G.do_repeat ? CMD_FLAG_REPEAT : 0,
10343 command->argc, command->argv,
10344 &(G.flag_repeat), NULL);
Francis Lanielbfc406a2023-12-22 22:02:33 +010010345
10346 if (argv_expanded) {
10347 /*
10348 * expand_strvec_to_strvec() allocates memory to expand
10349 * argv, we need to free it.
10350 */
10351 free(argv_expanded);
10352
10353 /*
10354 * We also restore command->argv to its original value
10355 * so no memory leak happens.
10356 */
10357 command->argv = argv;
10358
10359 /*
10360 * NOTE argc exists only in U-Boot, so argv freeing does
10361 * not rely on it as this code exists in BusyBox.
10362 */
10363 }
Francis Laniel36836fc2023-12-22 22:02:28 +010010364#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +010010365 }
10366
Francis Laniel36836fc2023-12-22 22:02:28 +010010367#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +010010368 if (!pi->alive_cmds) {
10369 debug_leave();
10370 debug_printf_exec("run_pipe return 1 (all forks failed, no children)\n");
10371 return 1;
10372 }
Francis Laniel36836fc2023-12-22 22:02:28 +010010373#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +010010374
10375 debug_leave();
Francis Laniel36836fc2023-12-22 22:02:28 +010010376#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +010010377 debug_printf_exec("run_pipe return -1 (%u children started)\n", pi->alive_cmds);
10378 return -1;
Francis Laniel36836fc2023-12-22 22:02:28 +010010379#else /* __U_BOOT__ */
10380 debug_printf_exec("run_pipe return %d\n", rcode);
10381 return rcode;
10382#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +010010383}
10384
10385/* NB: called by pseudo_exec, and therefore must not modify any
10386 * global data until exec/_exit (we can be a child after vfork!) */
10387static int run_list(struct pipe *pi)
10388{
10389#if ENABLE_HUSH_CASE
10390 char *case_word = NULL;
10391#endif
10392#if ENABLE_HUSH_LOOPS
10393 struct pipe *loop_top = NULL;
10394 char **for_lcur = NULL;
10395 char **for_list = NULL;
10396#endif
10397 smallint last_followup;
10398 smalluint rcode;
10399#if ENABLE_HUSH_IF || ENABLE_HUSH_CASE
10400 smalluint cond_code = 0;
10401#else
10402 enum { cond_code = 0 };
10403#endif
10404#if HAS_KEYWORDS
10405 smallint rword; /* RES_foo */
10406 smallint last_rword; /* ditto */
10407#endif
10408
Francis Laniel36836fc2023-12-22 22:02:28 +010010409#ifndef __U_BOOT__
Francis Laniele7ca3a32023-12-22 22:02:42 +010010410 debug_printf_exec("run_list lvl %d start\n", G.run_list_level);
Francis Laniel110b7692023-12-22 22:02:27 +010010411 debug_enter();
Francis Laniel36836fc2023-12-22 22:02:28 +010010412#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +010010413
Francis Laniel03061a82024-09-03 19:09:43 +020010414 set_G_ifs();
10415
Francis Laniel110b7692023-12-22 22:02:27 +010010416#if ENABLE_HUSH_LOOPS
10417 /* Check syntax for "for" */
10418 {
10419 struct pipe *cpipe;
10420 for (cpipe = pi; cpipe; cpipe = cpipe->next) {
10421 if (cpipe->res_word != RES_FOR && cpipe->res_word != RES_IN)
10422 continue;
10423 /* current word is FOR or IN (BOLD in comments below) */
10424 if (cpipe->next == NULL) {
10425 syntax_error("malformed for");
10426 debug_leave();
10427 debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level);
10428 return 1;
10429 }
10430 /* "FOR v; do ..." and "for v IN a b; do..." are ok */
10431 if (cpipe->next->res_word == RES_DO)
10432 continue;
10433 /* next word is not "do". It must be "in" then ("FOR v in ...") */
10434 if (cpipe->res_word == RES_IN /* "for v IN a b; not_do..."? */
10435 || cpipe->next->res_word != RES_IN /* FOR v not_do_and_not_in..."? */
10436 ) {
10437 syntax_error("malformed for");
10438 debug_leave();
10439 debug_printf_exec("run_list lvl %d return 1\n", G.run_list_level);
10440 return 1;
10441 }
10442 }
10443 }
10444#endif
10445
10446 /* Past this point, all code paths should jump to ret: label
10447 * in order to return, no direct "return" statements please.
10448 * This helps to ensure that no memory is leaked. */
10449
10450#if ENABLE_HUSH_JOB
10451 G.run_list_level++;
10452#endif
10453
10454#if HAS_KEYWORDS
10455 rword = RES_NONE;
10456 last_rword = RES_XXXX;
10457#endif
10458 last_followup = PIPE_SEQ;
10459 rcode = G.last_exitcode;
10460
10461 /* Go through list of pipes, (maybe) executing them. */
Francis Laniel36836fc2023-12-22 22:02:28 +010010462#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +010010463 for (; pi; pi = IF_HUSH_LOOPS(rword == RES_DONE ? loop_top : ) pi->next) {
Francis Laniel36836fc2023-12-22 22:02:28 +010010464#else /* __U_BOOT__ */
Francis Lanield0003142023-12-22 22:02:40 +010010465 for (; pi; pi = rword == RES_DONE ? loop_top : pi->next) {
Francis Laniel36836fc2023-12-22 22:02:28 +010010466#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +010010467 int r;
10468 int sv_errexit_depth;
10469
Francis Laniel36836fc2023-12-22 22:02:28 +010010470#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +010010471 if (G.flag_SIGINT)
10472 break;
10473 if (G_flag_return_in_progress == 1)
10474 break;
Francis Laniel9492c942023-12-22 22:02:39 +010010475#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +010010476
10477 IF_HAS_KEYWORDS(rword = pi->res_word;)
Francis Laniele7ca3a32023-12-22 22:02:42 +010010478 debug_printf_exec(": rword:%d cond_code:%d last_rword:%d\n",
Francis Laniel110b7692023-12-22 22:02:27 +010010479 rword, cond_code, last_rword);
10480
10481 sv_errexit_depth = G.errexit_depth;
10482 if (
10483#if ENABLE_HUSH_IF
10484 rword == RES_IF || rword == RES_ELIF ||
10485#endif
10486 pi->followup != PIPE_SEQ
10487 ) {
10488 G.errexit_depth++;
10489 }
10490#if ENABLE_HUSH_LOOPS
10491 if ((rword == RES_WHILE || rword == RES_UNTIL || rword == RES_FOR)
10492 && loop_top == NULL /* avoid bumping G.depth_of_loop twice */
10493 ) {
10494 /* start of a loop: remember where loop starts */
10495 loop_top = pi;
10496 G.depth_of_loop++;
10497 }
10498#endif
10499 /* Still in the same "if...", "then..." or "do..." branch? */
10500 if (IF_HAS_KEYWORDS(rword == last_rword &&) 1) {
10501 if ((rcode == 0 && last_followup == PIPE_OR)
10502 || (rcode != 0 && last_followup == PIPE_AND)
10503 ) {
10504 /* It is "<true> || CMD" or "<false> && CMD"
10505 * and we should not execute CMD */
10506 debug_printf_exec("skipped cmd because of || or &&\n");
10507 last_followup = pi->followup;
10508 goto dont_check_jobs_but_continue;
10509 }
10510 }
10511 last_followup = pi->followup;
Francis Laniel110b7692023-12-22 22:02:27 +010010512#if ENABLE_HUSH_IF
Francis Laniele7ca3a32023-12-22 22:02:42 +010010513 if (cond_code != 0) {
Francis Laniel110b7692023-12-22 22:02:27 +010010514 if (rword == RES_THEN) {
10515 /* if false; then ... fi has exitcode 0! */
10516 G.last_exitcode = rcode = EXIT_SUCCESS;
10517 /* "if <false> THEN cmd": skip cmd */
Francis Laniele7ca3a32023-12-22 22:02:42 +010010518 debug_printf_exec("skipped THEN cmd because IF condition was false\n");
10519 last_rword = rword;
Francis Laniel110b7692023-12-22 22:02:27 +010010520 continue;
10521 }
10522 } else {
Francis Laniele7ca3a32023-12-22 22:02:42 +010010523 if (rword == RES_ELSE
10524 || (rword == RES_ELIF && last_rword != RES_ELIF)
10525 ) {
Francis Laniel110b7692023-12-22 22:02:27 +010010526 /* "if <true> then ... ELSE/ELIF cmd":
10527 * skip cmd and all following ones */
Francis Laniele7ca3a32023-12-22 22:02:42 +010010528 debug_printf_exec("skipped ELSE/ELIF branch because IF condition was true\n");
Francis Laniel110b7692023-12-22 22:02:27 +010010529 break;
10530 }
Francis Laniele7ca3a32023-12-22 22:02:42 +010010531 //if (rword == RES_THEN): "if <true> THEN cmd", run cmd (fall through)
Francis Laniel110b7692023-12-22 22:02:27 +010010532 }
10533#endif
Francis Laniele7ca3a32023-12-22 22:02:42 +010010534 IF_HAS_KEYWORDS(last_rword = rword;)
Francis Laniel110b7692023-12-22 22:02:27 +010010535#if ENABLE_HUSH_LOOPS
10536 if (rword == RES_FOR) { /* && pi->num_cmds - always == 1 */
10537 if (!for_lcur) {
10538 /* first loop through for */
10539
10540 static const char encoded_dollar_at[] ALIGN1 = {
10541 SPECIAL_VAR_SYMBOL, '@' | 0x80, SPECIAL_VAR_SYMBOL, '\0'
10542 }; /* encoded representation of "$@" */
Francis Laniele7ca3a32023-12-22 22:02:42 +010010543 static const char *const encoded_dollar_at_argv[] ALIGN_PTR = {
Francis Laniel110b7692023-12-22 22:02:27 +010010544 encoded_dollar_at, NULL
10545 }; /* argv list with one element: "$@" */
10546 char **vals;
10547
10548 G.last_exitcode = rcode = EXIT_SUCCESS;
10549 vals = (char**)encoded_dollar_at_argv;
10550 if (pi->next->res_word == RES_IN) {
10551 /* if no variable values after "in" we skip "for" */
10552 if (!pi->next->cmds[0].argv) {
10553 debug_printf_exec(": null FOR: exitcode EXIT_SUCCESS\n");
10554 break;
10555 }
10556 vals = pi->next->cmds[0].argv;
10557 } /* else: "for var; do..." -> assume "$@" list */
10558 /* create list of variable values */
10559 debug_print_strings("for_list made from", vals);
10560 for_list = expand_strvec_to_strvec(vals);
10561 for_lcur = for_list;
10562 debug_print_strings("for_list", for_list);
10563 }
10564 if (!*for_lcur) {
10565 /* "for" loop is over, clean up */
10566 free(for_list);
10567 for_list = NULL;
10568 for_lcur = NULL;
10569 break;
10570 }
10571 /* Insert next value from for_lcur */
10572 /* note: *for_lcur already has quotes removed, $var expanded, etc */
Francis Lanield0003142023-12-22 22:02:40 +010010573#ifndef __U_BOOT__
Francis Laniele7ca3a32023-12-22 22:02:42 +010010574 set_local_var_from_halves(pi->cmds[0].argv[0], *for_lcur++);
Francis Lanield0003142023-12-22 22:02:40 +010010575#else /* __U_BOOT__ */
10576 /* We cannot use xasprintf, so we emulate it. */
10577 char *full_var;
10578 char *var = pi->cmds[0].argv[0];
10579 char *val = *for_lcur++;
10580
10581 /* + 1 to take into account =. */
10582 full_var = xmalloc(strlen(var) + strlen(val) + 1);
10583 sprintf(full_var, "%s=%s", var, val);
10584
10585 set_local_var_modern(full_var, /*flag:*/ 0);
10586#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +010010587 continue;
10588 }
10589 if (rword == RES_IN) {
10590 continue; /* "for v IN list;..." - "in" has no cmds anyway */
10591 }
10592 if (rword == RES_DONE) {
10593 continue; /* "done" has no cmds too */
10594 }
10595#endif
10596#if ENABLE_HUSH_CASE
10597 if (rword == RES_CASE) {
10598 debug_printf_exec("CASE cond_code:%d\n", cond_code);
10599 case_word = expand_string_to_string(pi->cmds->argv[0],
10600 EXP_FLAG_ESC_GLOB_CHARS, /*unbackslash:*/ 1);
10601 debug_printf_exec("CASE word1:'%s'\n", case_word);
10602 //unbackslash(case_word);
10603 //debug_printf_exec("CASE word2:'%s'\n", case_word);
10604 continue;
10605 }
10606 if (rword == RES_MATCH) {
10607 char **argv;
10608
10609 debug_printf_exec("MATCH cond_code:%d\n", cond_code);
10610 if (!case_word) /* "case ... matched_word) ... WORD)": we executed selected branch, stop */
10611 break;
10612 /* all prev words didn't match, does this one match? */
10613 argv = pi->cmds->argv;
10614 while (*argv) {
10615 char *pattern;
10616 debug_printf_exec("expand_string_to_string('%s')\n", *argv);
10617 pattern = expand_string_to_string(*argv,
10618 EXP_FLAG_ESC_GLOB_CHARS,
10619 /*unbackslash:*/ 0
10620 );
10621 /* TODO: which FNM_xxx flags to use? */
10622 cond_code = (fnmatch(pattern, case_word, /*flags:*/ 0) != 0);
Francis Laniele7ca3a32023-12-22 22:02:42 +010010623 debug_printf_exec("cond_code=fnmatch(pattern:'%s',str:'%s'):%d\n",
Francis Laniel110b7692023-12-22 22:02:27 +010010624 pattern, case_word, cond_code);
10625 free(pattern);
10626 if (cond_code == 0) {
10627 /* match! we will execute this branch */
10628 free(case_word);
10629 case_word = NULL; /* make future "word)" stop */
10630 break;
10631 }
10632 argv++;
10633 }
10634 continue;
10635 }
10636 if (rword == RES_CASE_BODY) { /* inside of a case branch */
10637 debug_printf_exec("CASE_BODY cond_code:%d\n", cond_code);
10638 if (cond_code != 0)
10639 continue; /* not matched yet, skip this pipe */
10640 }
10641 if (rword == RES_ESAC) {
10642 debug_printf_exec("ESAC cond_code:%d\n", cond_code);
10643 if (case_word) {
10644 /* "case" did not match anything: still set $? (to 0) */
10645 G.last_exitcode = rcode = EXIT_SUCCESS;
10646 }
10647 }
10648#endif
10649 /* Just pressing <enter> in shell should check for jobs.
10650 * OTOH, in non-interactive shell this is useless
10651 * and only leads to extra job checks */
10652 if (pi->num_cmds == 0) {
Francis Laniel36836fc2023-12-22 22:02:28 +010010653#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +010010654 if (G_interactive_fd)
10655 goto check_jobs_and_continue;
Francis Laniel36836fc2023-12-22 22:02:28 +010010656#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +010010657 continue;
10658 }
10659
10660 /* After analyzing all keywords and conditions, we decided
10661 * to execute this pipe. NB: have to do checkjobs(NULL)
10662 * after run_pipe to collect any background children,
10663 * even if list execution is to be stopped. */
10664 debug_printf_exec(": run_pipe with %d members\n", pi->num_cmds);
Francis Laniel36836fc2023-12-22 22:02:28 +010010665#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +010010666#if ENABLE_HUSH_LOOPS
10667 G.flag_break_continue = 0;
10668#endif
Francis Laniel36836fc2023-12-22 22:02:28 +010010669#endif /* !__U_BOOT__ */
Francis Laniele7ca3a32023-12-22 22:02:42 +010010670#ifndef __U_BOOT__
10671 rcode = r = G.o_opt[OPT_O_NOEXEC] ? 0 : run_pipe(pi);
10672 /* NB: rcode is a smalluint, r is int */
10673#else /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +010010674 rcode = r = run_pipe(pi); /* NB: rcode is a smalluint, r is int */
Francis Laniel3b66e572023-12-22 22:02:32 +010010675 if (r <= EXIT_RET_CODE) {
10676 int previous_rcode = G.last_exitcode;
10677 /*
10678 * This magic is to get the exit code given by the user.
10679 * Contrary to old shell code, we use + EXIT_RET_CODE as EXIT_RET_CODE
10680 * equals -2.
10681 */
10682 G.last_exitcode = -r + EXIT_RET_CODE;
Francis Laniel36836fc2023-12-22 22:02:28 +010010683
Francis Laniel3b66e572023-12-22 22:02:32 +010010684 /*
10685 * This case deals with the following:
10686 * => setenv inner 'echo entry inner; exit; echo inner done'
10687 * => setenv outer 'echo entry outer; run inner; echo outer done'
10688 * => run outer
10689 * So, if we are in inner, we need to break and not run the other
10690 * commands.
10691 * Otherwise, we just continue in outer.
10692 * As return code are propagated, we use the previous value to check if
10693 * exit was just called or was propagated.
10694 */
10695 if (previous_rcode != r) {
10696 /*
10697 * If run from run_command, run_command_flags will be set, so we check
10698 * this to know if we are in main input shell.
10699 */
10700 if (!G.run_command_flags)
10701 printf("exit not allowed from main input shell.\n");
10702
10703 break;
10704 }
10705 continue;
Francis Laniel36836fc2023-12-22 22:02:28 +010010706 }
Francis Laniel3b66e572023-12-22 22:02:32 +010010707#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +010010708 if (r != -1) {
10709 /* We ran a builtin, function, or group.
10710 * rcode is already known
10711 * and we don't need to wait for anything. */
10712 debug_printf_exec(": builtin/func exitcode %d\n", rcode);
10713 G.last_exitcode = rcode;
Francis Laniel36836fc2023-12-22 22:02:28 +010010714#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +010010715 check_and_run_traps();
Francis Laniel36836fc2023-12-22 22:02:28 +010010716#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +010010717#if ENABLE_HUSH_TRAP && ENABLE_HUSH_FUNCTIONS
10718 rcode = G.last_exitcode; /* "return" in trap can change it, read back */
10719#endif
Francis Laniel36836fc2023-12-22 22:02:28 +010010720#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +010010721#if ENABLE_HUSH_LOOPS
10722 /* Was it "break" or "continue"? */
10723 if (G.flag_break_continue) {
10724 smallint fbc = G.flag_break_continue;
10725 /* We might fall into outer *loop*,
10726 * don't want to break it too */
10727 if (loop_top) {
10728 G.depth_break_continue--;
10729 if (G.depth_break_continue == 0)
10730 G.flag_break_continue = 0;
10731 /* else: e.g. "continue 2" should *break* once, *then* continue */
10732 } /* else: "while... do... { we are here (innermost list is not a loop!) };...done" */
10733 if (G.depth_break_continue != 0 || fbc == BC_BREAK) {
10734 checkjobs(NULL, 0 /*(no pid to wait for)*/);
10735 break;
10736 }
10737 /* "continue": simulate end of loop */
10738 rword = RES_DONE;
10739 continue;
10740 }
10741#endif
10742 if (G_flag_return_in_progress == 1) {
10743 checkjobs(NULL, 0 /*(no pid to wait for)*/);
10744 break;
10745 }
Francis Laniel36836fc2023-12-22 22:02:28 +010010746
Francis Laniel110b7692023-12-22 22:02:27 +010010747 } else if (pi->followup == PIPE_BG) {
10748 /* What does bash do with attempts to background builtins? */
10749 /* even bash 3.2 doesn't do that well with nested bg:
10750 * try "{ { sleep 10; echo DEEP; } & echo HERE; } &".
10751 * I'm NOT treating inner &'s as jobs */
10752#if ENABLE_HUSH_JOB
10753 if (G.run_list_level == 1)
10754 insert_job_into_table(pi);
10755#endif
10756 /* Last command's pid goes to $! */
10757 G.last_bg_pid = pi->cmds[pi->num_cmds - 1].pid;
10758 G.last_bg_pid_exitcode = 0;
10759 debug_printf_exec(": cmd&: exitcode EXIT_SUCCESS\n");
10760/* Check pi->pi_inverted? "! sleep 1 & echo $?": bash says 1. dash and ash say 0 */
10761 rcode = EXIT_SUCCESS;
10762 goto check_traps;
10763 } else {
10764#if ENABLE_HUSH_JOB
10765 if (G.run_list_level == 1 && G_interactive_fd) {
10766 /* Waits for completion, then fg's main shell */
10767 rcode = checkjobs_and_fg_shell(pi);
10768 debug_printf_exec(": checkjobs_and_fg_shell exitcode %d\n", rcode);
10769 goto check_traps;
10770 }
10771#endif
10772 /* This one just waits for completion */
10773 rcode = checkjobs(pi, 0 /*(no pid to wait for)*/);
10774 debug_printf_exec(": checkjobs exitcode %d\n", rcode);
10775 check_traps:
10776 G.last_exitcode = rcode;
10777 check_and_run_traps();
10778#if ENABLE_HUSH_TRAP && ENABLE_HUSH_FUNCTIONS
10779 rcode = G.last_exitcode; /* "return" in trap can change it, read back */
10780#endif
10781 }
Francis Laniel36836fc2023-12-22 22:02:28 +010010782#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +010010783
Francis Laniel36836fc2023-12-22 22:02:28 +010010784#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +010010785 /* Handle "set -e" */
10786 if (rcode != 0 && G.o_opt[OPT_O_ERREXIT]) {
10787 debug_printf_exec("ERREXIT:1 errexit_depth:%d\n", G.errexit_depth);
10788 if (G.errexit_depth == 0)
10789 hush_exit(rcode);
10790 }
Francis Laniel36836fc2023-12-22 22:02:28 +010010791#else /* __U_BOOT__ */
10792 } /* if (r != -1) */
10793#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +010010794 G.errexit_depth = sv_errexit_depth;
10795
10796 /* Analyze how result affects subsequent commands */
10797#if ENABLE_HUSH_IF
Francis Laniele7ca3a32023-12-22 22:02:42 +010010798 if (rword == RES_IF || rword == RES_ELIF) {
10799 debug_printf_exec("cond_code=rcode:%d\n", rcode);
Francis Laniel110b7692023-12-22 22:02:27 +010010800 cond_code = rcode;
Francis Laniele7ca3a32023-12-22 22:02:42 +010010801 }
Francis Laniel110b7692023-12-22 22:02:27 +010010802#endif
Francis Laniel36836fc2023-12-22 22:02:28 +010010803#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +010010804 check_jobs_and_continue:
10805 checkjobs(NULL, 0 /*(no pid to wait for)*/);
Francis Laniel36836fc2023-12-22 22:02:28 +010010806#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +010010807 dont_check_jobs_but_continue: ;
10808#if ENABLE_HUSH_LOOPS
10809 /* Beware of "while false; true; do ..."! */
10810 if (pi->next
10811 && (pi->next->res_word == RES_DO || pi->next->res_word == RES_DONE)
10812 /* check for RES_DONE is needed for "while ...; do \n done" case */
10813 ) {
10814 if (rword == RES_WHILE) {
10815 if (rcode) {
10816 /* "while false; do...done" - exitcode 0 */
10817 G.last_exitcode = rcode = EXIT_SUCCESS;
10818 debug_printf_exec(": while expr is false: breaking (exitcode:EXIT_SUCCESS)\n");
10819 break;
10820 }
10821 }
10822 if (rword == RES_UNTIL) {
10823 if (!rcode) {
10824 debug_printf_exec(": until expr is true: breaking\n");
10825 break;
10826 }
10827 }
10828 }
10829#endif
10830 } /* for (pi) */
10831
10832#if ENABLE_HUSH_JOB
10833 G.run_list_level--;
10834#endif
10835#if ENABLE_HUSH_LOOPS
10836 if (loop_top)
10837 G.depth_of_loop--;
10838 free(for_list);
10839#endif
10840#if ENABLE_HUSH_CASE
10841 free(case_word);
10842#endif
Francis Laniel36836fc2023-12-22 22:02:28 +010010843#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +010010844 debug_leave();
Francis Laniele7ca3a32023-12-22 22:02:42 +010010845 debug_printf_exec("run_list lvl %d return %d\n", G.run_list_level, rcode);
Francis Laniel36836fc2023-12-22 22:02:28 +010010846#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +010010847 return rcode;
10848}
10849
10850/* Select which version we will use */
10851static int run_and_free_list(struct pipe *pi)
10852{
10853 int rcode = 0;
10854 debug_printf_exec("run_and_free_list entered\n");
Francis Laniel36836fc2023-12-22 22:02:28 +010010855#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +010010856 if (!G.o_opt[OPT_O_NOEXEC]) {
Francis Laniel36836fc2023-12-22 22:02:28 +010010857#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +010010858 debug_printf_exec(": run_list: 1st pipe with %d cmds\n", pi->num_cmds);
10859 rcode = run_list(pi);
Francis Laniel36836fc2023-12-22 22:02:28 +010010860#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +010010861 }
Francis Laniel36836fc2023-12-22 22:02:28 +010010862#endif /* !__U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +010010863 /* free_pipe_list has the side effect of clearing memory.
10864 * In the long run that function can be merged with run_list,
10865 * but doing that now would hobble the debugging effort. */
10866 free_pipe_list(pi);
10867 debug_printf_exec("run_and_free_list return %d\n", rcode);
10868 return rcode;
10869}
10870
Francis Laniel36836fc2023-12-22 22:02:28 +010010871#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +010010872static void install_sighandlers(unsigned mask)
10873{
10874 sighandler_t old_handler;
10875 unsigned sig = 0;
10876 while ((mask >>= 1) != 0) {
10877 sig++;
10878 if (!(mask & 1))
10879 continue;
10880 old_handler = install_sighandler(sig, pick_sighandler(sig));
10881 /* POSIX allows shell to re-enable SIGCHLD
10882 * even if it was SIG_IGN on entry.
10883 * Therefore we skip IGN check for it:
10884 */
10885 if (sig == SIGCHLD)
10886 continue;
10887 /* Interactive bash re-enables SIGHUP which is SIG_IGNed on entry.
10888 * Try:
10889 * trap '' hup; bash; echo RET # type "kill -hup $$", see SIGHUP having effect
10890 * trap '' hup; bash -c 'kill -hup $$; echo ALIVE' # here SIGHUP is SIG_IGNed
10891 */
10892 if (sig == SIGHUP && G_interactive_fd)
10893 continue;
10894 /* Unless one of the above signals, is it SIG_IGN? */
10895 if (old_handler == SIG_IGN) {
10896 /* oops... restore back to IGN, and record this fact */
10897 install_sighandler(sig, old_handler);
10898#if ENABLE_HUSH_TRAP
10899 if (!G_traps)
10900 G_traps = xzalloc(sizeof(G_traps[0]) * NSIG);
10901 free(G_traps[sig]);
10902 G_traps[sig] = xzalloc(1); /* == xstrdup(""); */
10903#endif
10904 }
10905 }
10906}
10907
10908/* Called a few times only (or even once if "sh -c") */
10909static void install_special_sighandlers(void)
10910{
10911 unsigned mask;
10912
10913 /* Which signals are shell-special? */
10914 mask = (1 << SIGQUIT) | (1 << SIGCHLD);
10915 if (G_interactive_fd) {
10916 mask |= SPECIAL_INTERACTIVE_SIGS;
10917 if (G_saved_tty_pgrp) /* we have ctty, job control sigs work */
10918 mask |= SPECIAL_JOBSTOP_SIGS;
10919 }
10920 /* Careful, do not re-install handlers we already installed */
10921 if (G.special_sig_mask != mask) {
10922 unsigned diff = mask & ~G.special_sig_mask;
10923 G.special_sig_mask = mask;
10924 install_sighandlers(diff);
10925 }
10926}
10927
10928#if ENABLE_HUSH_JOB
10929/* helper */
10930/* Set handlers to restore tty pgrp and exit */
10931static void install_fatal_sighandlers(void)
10932{
10933 unsigned mask;
10934
10935 /* We will restore tty pgrp on these signals */
10936 mask = 0
10937 /*+ (1 << SIGILL ) * HUSH_DEBUG*/
10938 /*+ (1 << SIGFPE ) * HUSH_DEBUG*/
10939 + (1 << SIGBUS ) * HUSH_DEBUG
10940 + (1 << SIGSEGV) * HUSH_DEBUG
10941 /*+ (1 << SIGTRAP) * HUSH_DEBUG*/
10942 + (1 << SIGABRT)
10943 /* bash 3.2 seems to handle these just like 'fatal' ones */
10944 + (1 << SIGPIPE)
10945 + (1 << SIGALRM)
10946 /* if we are interactive, SIGHUP, SIGTERM and SIGINT are special sigs.
10947 * if we aren't interactive... but in this case
10948 * we never want to restore pgrp on exit, and this fn is not called
10949 */
10950 /*+ (1 << SIGHUP )*/
10951 /*+ (1 << SIGTERM)*/
10952 /*+ (1 << SIGINT )*/
10953 ;
10954 G_fatal_sig_mask = mask;
10955
10956 install_sighandlers(mask);
10957}
10958#endif
10959
10960static int set_mode(int state, char mode, const char *o_opt)
10961{
10962 int idx;
10963 switch (mode) {
10964 case 'n':
Francis Laniele7ca3a32023-12-22 22:02:42 +010010965 /* set -n has no effect in interactive shell */
10966 /* Try: while set -n; do echo $-; done */
10967 if (!G_interactive_fd)
10968 G.o_opt[OPT_O_NOEXEC] = state;
Francis Laniel110b7692023-12-22 22:02:27 +010010969 break;
10970 case 'x':
10971 IF_HUSH_MODE_X(G_x_mode = state;)
10972 IF_HUSH_MODE_X(if (G.x_mode_fd <= 0) G.x_mode_fd = dup_CLOEXEC(2, 10);)
10973 break;
10974 case 'e':
10975 G.o_opt[OPT_O_ERREXIT] = state;
10976 break;
10977 case 'o':
10978 if (!o_opt) {
10979 /* "set -o" or "set +o" without parameter.
10980 * in bash, set -o produces this output:
10981 * pipefail off
10982 * and set +o:
10983 * set +o pipefail
10984 * We always use the second form.
10985 */
10986 const char *p = o_opt_strings;
10987 idx = 0;
10988 while (*p) {
10989 printf("set %co %s\n", (G.o_opt[idx] ? '-' : '+'), p);
10990 idx++;
10991 p += strlen(p) + 1;
10992 }
10993 break;
10994 }
10995 idx = index_in_strings(o_opt_strings, o_opt);
10996 if (idx >= 0) {
10997 G.o_opt[idx] = state;
10998 break;
10999 }
11000 /* fall through to error */
11001 default:
11002 return EXIT_FAILURE;
11003 }
11004 return EXIT_SUCCESS;
11005}
11006
11007int hush_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
11008int hush_main(int argc, char **argv)
11009{
11010 pid_t cached_getpid;
11011 enum {
11012 OPT_login = (1 << 0),
11013 };
11014 unsigned flags;
11015#if !BB_MMU
11016 unsigned builtin_argc = 0;
11017#endif
11018 char **e;
11019 struct variable *cur_var;
11020 struct variable *shell_ver;
11021
11022 INIT_G();
11023 if (EXIT_SUCCESS != 0) /* if EXIT_SUCCESS == 0, it is already done */
11024 G.last_exitcode = EXIT_SUCCESS;
Francis Laniele7ca3a32023-12-22 22:02:42 +010011025#if !BB_MMU
11026 /* "Big heredoc" support via "sh -< STRING" invocation.
11027 * Check it first (do not bother to run the usual init code,
11028 * it is not needed for this case).
11029 */
11030 if (argv[1]
11031 && argv[1][0] == '-' && argv[1][1] == '<' /*&& !argv[1][2]*/
11032 /*&& argv[2] && !argv[3] - we don't check some conditions */
11033 ) {
11034 full_write1_str(argv[2]);
11035 _exit(0);
11036 }
11037 G.argv0_for_re_execing = argv[0];
11038#endif
Francis Laniel110b7692023-12-22 22:02:27 +010011039#if ENABLE_HUSH_TRAP
11040# if ENABLE_HUSH_FUNCTIONS
11041 G.return_exitcode = -1;
11042# endif
11043 G.pre_trap_exitcode = -1;
11044#endif
11045
11046#if ENABLE_HUSH_FAST
11047 G.count_SIGCHLD++; /* ensure it is != G.handled_SIGCHLD */
11048#endif
Francis Laniel110b7692023-12-22 22:02:27 +010011049
11050 cached_getpid = getpid(); /* for tcsetpgrp() during init */
11051 G.root_pid = cached_getpid; /* for $PID (NOMMU can override via -$HEXPID:HEXPPID:...) */
Francis Laniele7ca3a32023-12-22 22:02:42 +010011052 G.root_ppid = getppid(); /* for $PPID (NOMMU can override) */
Francis Laniel110b7692023-12-22 22:02:27 +010011053
11054 /* Deal with HUSH_VERSION */
11055 debug_printf_env("unsetenv '%s'\n", "HUSH_VERSION");
11056 unsetenv("HUSH_VERSION"); /* in case it exists in initial env */
11057 shell_ver = xzalloc(sizeof(*shell_ver));
11058 shell_ver->flg_export = 1;
11059 shell_ver->flg_read_only = 1;
11060 /* Code which handles ${var<op>...} needs writable values for all variables,
11061 * therefore we xstrdup: */
11062 shell_ver->varstr = xstrdup(hush_version_str);
11063
11064 /* Create shell local variables from the values
11065 * currently living in the environment */
11066 G.top_var = shell_ver;
11067 cur_var = G.top_var;
11068 e = environ;
11069 if (e) while (*e) {
11070 char *value = strchr(*e, '=');
11071 if (value) { /* paranoia */
11072 cur_var->next = xzalloc(sizeof(*cur_var));
11073 cur_var = cur_var->next;
11074 cur_var->varstr = *e;
11075 cur_var->max_len = strlen(*e);
11076 cur_var->flg_export = 1;
11077 }
11078 e++;
11079 }
11080 /* (Re)insert HUSH_VERSION into env (AFTER we scanned the env!) */
11081 debug_printf_env("putenv '%s'\n", shell_ver->varstr);
11082 putenv(shell_ver->varstr);
11083
11084 /* Export PWD */
11085 set_pwd_var(SETFLAG_EXPORT);
11086
11087#if BASH_HOSTNAME_VAR
11088 /* Set (but not export) HOSTNAME unless already set */
11089 if (!get_local_var_value("HOSTNAME")) {
11090 struct utsname uts;
11091 uname(&uts);
11092 set_local_var_from_halves("HOSTNAME", uts.nodename);
11093 }
11094#endif
11095 /* IFS is not inherited from the parent environment */
11096 set_local_var_from_halves("IFS", defifs);
11097
11098 if (!get_local_var_value("PATH"))
11099 set_local_var_from_halves("PATH", bb_default_root_path);
11100
11101 /* PS1/PS2 are set later, if we determine that we are interactive */
11102
11103 /* bash also exports SHLVL and _,
11104 * and sets (but doesn't export) the following variables:
11105 * BASH=/bin/bash
11106 * BASH_VERSINFO=([0]="3" [1]="2" [2]="0" [3]="1" [4]="release" [5]="i386-pc-linux-gnu")
11107 * BASH_VERSION='3.2.0(1)-release'
11108 * HOSTTYPE=i386
11109 * MACHTYPE=i386-pc-linux-gnu
11110 * OSTYPE=linux-gnu
11111 * PPID=<NNNNN> - we also do it elsewhere
11112 * EUID=<NNNNN>
11113 * UID=<NNNNN>
11114 * GROUPS=()
11115 * LINES=<NNN>
11116 * COLUMNS=<NNN>
11117 * BASH_ARGC=()
11118 * BASH_ARGV=()
11119 * BASH_LINENO=()
11120 * BASH_SOURCE=()
11121 * DIRSTACK=()
11122 * PIPESTATUS=([0]="0")
11123 * HISTFILE=/<xxx>/.bash_history
11124 * HISTFILESIZE=500
11125 * HISTSIZE=500
11126 * MAILCHECK=60
11127 * PATH=/usr/gnu/bin:/usr/local/bin:/bin:/usr/bin:.
11128 * SHELL=/bin/bash
11129 * SHELLOPTS=braceexpand:emacs:hashall:histexpand:history:interactive-comments:monitor
11130 * TERM=dumb
11131 * OPTERR=1
11132 * OPTIND=1
11133 * PS4='+ '
11134 */
11135
11136#if NUM_SCRIPTS > 0
11137 if (argc < 0) {
11138 char *script = get_script_content(-argc - 1);
11139 G.global_argv = argv;
11140 G.global_argc = string_array_len(argv);
11141 //install_special_sighandlers(); - needed?
11142 parse_and_run_string(script);
11143 goto final_return;
11144 }
11145#endif
11146
11147 /* Initialize some more globals to non-zero values */
11148 die_func = restore_ttypgrp_and__exit;
11149
11150 /* Shell is non-interactive at first. We need to call
11151 * install_special_sighandlers() if we are going to execute "sh <script>",
11152 * "sh -c <cmds>" or login shell's /etc/profile and friends.
11153 * If we later decide that we are interactive, we run install_special_sighandlers()
11154 * in order to intercept (more) signals.
11155 */
11156
11157 /* Parse options */
11158 /* http://www.opengroup.org/onlinepubs/9699919799/utilities/sh.html */
11159 flags = (argv[0] && argv[0][0] == '-') ? OPT_login : 0;
11160 while (1) {
11161 int opt = getopt(argc, argv, "+" /* stop at 1st non-option */
11162 "cexinsl"
11163#if !BB_MMU
Francis Laniele7ca3a32023-12-22 22:02:42 +010011164 "$:R:V:"
11165# if ENABLE_HUSH_LINENO_VAR
11166 "L:"
11167# endif
Francis Laniel110b7692023-12-22 22:02:27 +010011168# if ENABLE_HUSH_FUNCTIONS
11169 "F:"
11170# endif
11171#endif
11172 );
11173 if (opt <= 0)
11174 break;
11175 switch (opt) {
11176 case 'c':
11177 /* Note: -c is not an option with param!
11178 * "hush -c -l SCRIPT" is valid. "hush -cSCRIPT" is not.
11179 */
11180 G.opt_c = 1;
11181 break;
11182 case 'i':
11183 /* Well, we cannot just declare interactiveness,
11184 * we have to have some stuff (ctty, etc) */
11185 /* G_interactive_fd++; */
Francis Laniele7ca3a32023-12-22 22:02:42 +010011186//There are a few cases where bash -i -c 'SCRIPT'
11187//has visible effect (differs from bash -c 'SCRIPT'):
11188//it ignores TERM:
11189// bash -i -c 'kill $$; echo ALIVE'
11190// ALIVE
11191//it resets SIG_IGNed HUP to SIG_DFL:
11192// trap '' hup; bash -i -c 'kill -hup $$; echo ALIVE'
11193// Hangup [the message is not printed by bash, it's the shell which started it]
11194//is talkative about jobs and exiting:
11195// bash -i -c 'sleep 1 & exit'
11196// [1] 16170
11197// exit
11198//includes $ENV file (only if run as "sh"):
11199// echo last >/tmp/ENV; ENV=/tmp/ENV sh -i -c 'echo HERE'
11200// last: cannot open /var/log/wtmp: No such file or directory
11201// HERE
11202//(under "bash", it's the opposite: it runs $BASH_ENV file only *without* -i).
11203//
11204//ash -i -c 'sleep 3; sleep 3', on ^C, drops into a prompt instead of exiting
11205//(this may be a bug, bash does not do this).
11206//(ash -i -c 'sleep 3' won't show this, the last command gets auto-"exec"ed)
11207//
11208//None of the above feel like useful features people would rely on.
Francis Laniel110b7692023-12-22 22:02:27 +010011209 break;
11210 case 's':
11211 G.opt_s = 1;
11212 break;
11213 case 'l':
11214 flags |= OPT_login;
11215 break;
11216#if !BB_MMU
Francis Laniel110b7692023-12-22 22:02:27 +010011217 case '$': {
11218 unsigned long long empty_trap_mask;
11219
11220 G.root_pid = bb_strtou(optarg, &optarg, 16);
11221 optarg++;
11222 G.root_ppid = bb_strtou(optarg, &optarg, 16);
11223 optarg++;
11224 G.last_bg_pid = bb_strtou(optarg, &optarg, 16);
11225 optarg++;
11226 G.last_exitcode = bb_strtou(optarg, &optarg, 16);
11227 optarg++;
11228 builtin_argc = bb_strtou(optarg, &optarg, 16);
11229 optarg++;
11230 empty_trap_mask = bb_strtoull(optarg, &optarg, 16);
11231 if (empty_trap_mask != 0) {
11232 IF_HUSH_TRAP(int sig;)
11233 install_special_sighandlers();
11234# if ENABLE_HUSH_TRAP
11235 G_traps = xzalloc(sizeof(G_traps[0]) * NSIG);
11236 for (sig = 1; sig < NSIG; sig++) {
11237 if (empty_trap_mask & (1LL << sig)) {
11238 G_traps[sig] = xzalloc(1); /* == xstrdup(""); */
11239 install_sighandler(sig, SIG_IGN);
11240 }
11241 }
11242# endif
11243 }
11244# if ENABLE_HUSH_LOOPS
11245 optarg++;
11246 G.depth_of_loop = bb_strtou(optarg, &optarg, 16);
11247# endif
11248 /* Suppress "killed by signal" message, -$ hack is used
11249 * for subshells: echo `sh -c 'kill -9 $$'`
11250 * should be silent.
11251 */
11252 IF_HUSH_JOB(G.run_list_level = 1;)
11253# if ENABLE_HUSH_FUNCTIONS
11254 /* nommu uses re-exec trick for "... | func | ...",
11255 * should allow "return".
11256 * This accidentally allows returns in subshells.
11257 */
11258 G_flag_return_in_progress = -1;
11259# endif
11260 break;
11261 }
11262 case 'R':
11263 case 'V':
11264 set_local_var(xstrdup(optarg), opt == 'R' ? SETFLAG_MAKE_RO : 0);
11265 break;
Francis Laniele7ca3a32023-12-22 22:02:42 +010011266# if ENABLE_HUSH_LINENO_VAR
11267 case 'L':
11268 G.parse_lineno = xatou(optarg);
11269 break;
11270# endif
Francis Laniel110b7692023-12-22 22:02:27 +010011271# if ENABLE_HUSH_FUNCTIONS
11272 case 'F': {
11273 struct function *funcp = new_function(optarg);
11274 /* funcp->name is already set to optarg */
11275 /* funcp->body is set to NULL. It's a special case. */
11276 funcp->body_as_string = argv[optind];
11277 optind++;
11278 break;
11279 }
11280# endif
11281#endif
11282 /*case '?': invalid option encountered (set_mode('?') will fail) */
11283 /*case 'n':*/
11284 /*case 'x':*/
11285 /*case 'e':*/
11286 default:
11287 if (set_mode(1, opt, NULL) == 0) /* no error */
11288 break;
11289 bb_show_usage();
11290 }
11291 } /* option parsing loop */
11292
11293 /* Skip options. Try "hush -l": $1 should not be "-l"! */
11294 G.global_argc = argc - (optind - 1);
11295 G.global_argv = argv + (optind - 1);
11296 G.global_argv[0] = argv[0];
11297
11298 /* If we are login shell... */
11299 if (flags & OPT_login) {
11300 const char *hp = NULL;
11301 HFILE *input;
11302
11303 debug_printf("sourcing /etc/profile\n");
11304 input = hfopen("/etc/profile");
11305 run_profile:
11306 if (input != NULL) {
11307 install_special_sighandlers();
11308 parse_and_run_file(input);
11309 hfclose(input);
11310 }
11311 /* bash: after sourcing /etc/profile,
11312 * tries to source (in the given order):
11313 * ~/.bash_profile, ~/.bash_login, ~/.profile,
11314 * stopping on first found. --noprofile turns this off.
11315 * bash also sources ~/.bash_logout on exit.
11316 * If called as sh, skips .bash_XXX files.
11317 */
11318 if (!hp) { /* unless we looped on the "goto" already */
11319 hp = get_local_var_value("HOME");
11320 if (hp && hp[0]) {
11321 debug_printf("sourcing ~/.profile\n");
11322 hp = concat_path_file(hp, ".profile");
11323 input = hfopen(hp);
11324 free((char*)hp);
11325 goto run_profile;
11326 }
11327 }
11328 }
11329
Francis Laniel36836fc2023-12-22 22:02:28 +010011330#ifndef __U_BOOT__
Francis Laniel110b7692023-12-22 22:02:27 +010011331 /* -c takes effect *after* -l */
11332 if (G.opt_c) {
11333 /* Possibilities:
11334 * sh ... -c 'script'
11335 * sh ... -c 'script' ARG0 [ARG1...]
11336 * On NOMMU, if builtin_argc != 0,
11337 * sh ... -c 'builtin' BARGV... "" ARG0 [ARG1...]
11338 * "" needs to be replaced with NULL
11339 * and BARGV vector fed to builtin function.
11340 * Note: the form without ARG0 never happens:
11341 * sh ... -c 'builtin' BARGV... ""
11342 */
11343 char *script;
11344
11345 install_special_sighandlers();
11346
11347 G.global_argc--;
11348 G.global_argv++;
11349#if !BB_MMU
11350 if (builtin_argc) {
11351 /* -c 'builtin' [BARGV...] "" ARG0 [ARG1...] */
11352 const struct built_in_command *x;
11353 x = find_builtin(G.global_argv[0]);
11354 if (x) { /* paranoia */
11355 argv = G.global_argv;
11356 G.global_argc -= builtin_argc + 1; /* skip [BARGV...] "" */
11357 G.global_argv += builtin_argc + 1;
11358 G.global_argv[-1] = NULL; /* replace "" */
11359 G.last_exitcode = x->b_function(argv);
11360 }
11361 goto final_return;
11362 }
11363#endif
11364
11365 script = G.global_argv[0];
11366 if (!script)
11367 bb_error_msg_and_die(bb_msg_requires_arg, "-c");
11368 if (!G.global_argv[1]) {
11369 /* -c 'script' (no params): prevent empty $0 */
11370 G.global_argv[0] = argv[0];
11371 } else { /* else -c 'script' ARG0 [ARG1...]: $0 is ARG0 */
11372 G.global_argc--;
11373 G.global_argv++;
11374 }
11375 parse_and_run_string(script);
11376 goto final_return;
11377 }
11378
11379 /* -s is: hush -s ARGV1 ARGV2 (no SCRIPT) */
11380 if (!G.opt_s && G.global_argv[1]) {
11381 HFILE *input;
11382 /*
11383 * "bash <script>" (which is never interactive (unless -i?))
11384 * sources $BASH_ENV here (without scanning $PATH).
11385 * If called as sh, does the same but with $ENV.
11386 * Also NB, per POSIX, $ENV should undergo parameter expansion.
11387 */
11388 G.global_argc--;
11389 G.global_argv++;
11390 debug_printf("running script '%s'\n", G.global_argv[0]);
11391 xfunc_error_retval = 127; /* for "hush /does/not/exist" case */
11392 input = hfopen(G.global_argv[0]);
11393 if (!input) {
11394 bb_simple_perror_msg_and_die(G.global_argv[0]);
11395 }
11396 xfunc_error_retval = 1;
11397 install_special_sighandlers();
11398 parse_and_run_file(input);
11399#if ENABLE_FEATURE_CLEAN_UP
11400 hfclose(input);
11401#endif
11402 goto final_return;
11403 }
11404 /* "implicit" -s: bare interactive hush shows 's' in $- */
11405 G.opt_s = 1;
11406
Francis Laniel36836fc2023-12-22 22:02:28 +010011407#endif /* __U_BOOT__ */
Francis Laniel110b7692023-12-22 22:02:27 +010011408 /* Up to here, shell was non-interactive. Now it may become one.
11409 * NB: don't forget to (re)run install_special_sighandlers() as needed.
11410 */
11411
11412 /* A shell is interactive if the '-i' flag was given,
11413 * or if all of the following conditions are met:
11414 * no -c command
11415 * no arguments remaining or the -s flag given
11416 * standard input is a terminal
11417 * standard output is a terminal
11418 * Refer to Posix.2, the description of the 'sh' utility.
11419 */
11420#if ENABLE_HUSH_JOB
11421 if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) {
11422 G_saved_tty_pgrp = tcgetpgrp(STDIN_FILENO);
11423 debug_printf("saved_tty_pgrp:%d\n", G_saved_tty_pgrp);
11424 if (G_saved_tty_pgrp < 0)
11425 G_saved_tty_pgrp = 0;
11426
11427 /* try to dup stdin to high fd#, >= 255 */
11428 G_interactive_fd = dup_CLOEXEC(STDIN_FILENO, 254);
11429 if (G_interactive_fd < 0) {
11430 /* try to dup to any fd */
Francis Laniel03061a82024-09-03 19:09:43 +020011431 G_interactive_fd = dup_CLOEXEC(STDIN_FILENO, -1);
Francis Laniel110b7692023-12-22 22:02:27 +010011432 if (G_interactive_fd < 0) {
11433 /* give up */
11434 G_interactive_fd = 0;
11435 G_saved_tty_pgrp = 0;
11436 }
11437 }
11438 }
11439 debug_printf("interactive_fd:%d\n", G_interactive_fd);
11440 if (G_interactive_fd) {
Francis Laniel110b7692023-12-22 22:02:27 +010011441 if (G_saved_tty_pgrp) {
11442 /* If we were run as 'hush &', sleep until we are
11443 * in the foreground (tty pgrp == our pgrp).
11444 * If we get started under a job aware app (like bash),
11445 * make sure we are now in charge so we don't fight over
11446 * who gets the foreground */
11447 while (1) {
11448 pid_t shell_pgrp = getpgrp();
11449 G_saved_tty_pgrp = tcgetpgrp(G_interactive_fd);
11450 if (G_saved_tty_pgrp == shell_pgrp)
11451 break;
11452 /* send TTIN to ourself (should stop us) */
11453 kill(- shell_pgrp, SIGTTIN);
11454 }
11455 }
11456
11457 /* Install more signal handlers */
11458 install_special_sighandlers();
11459
11460 if (G_saved_tty_pgrp) {
11461 /* Set other signals to restore saved_tty_pgrp */
11462 install_fatal_sighandlers();
11463 /* Put ourselves in our own process group
11464 * (bash, too, does this only if ctty is available) */
11465 bb_setpgrp(); /* is the same as setpgid(our_pid, our_pid); */
11466 /* Grab control of the terminal */
11467 tcsetpgrp(G_interactive_fd, cached_getpid);
11468 }
11469 enable_restore_tty_pgrp_on_exit();
11470
11471# if ENABLE_FEATURE_EDITING
11472 G.line_input_state = new_line_input_t(FOR_SHELL);
Francis Laniele7ca3a32023-12-22 22:02:42 +010011473# if ENABLE_FEATURE_TAB_COMPLETION
11474 G.line_input_state->get_exe_name = hush_command_name;
11475# endif
11476# if EDITING_HAS_sh_get_var
11477 G.line_input_state->sh_get_var = get_local_var_value;
Francis Laniel110b7692023-12-22 22:02:27 +010011478# endif
11479# endif
11480# if ENABLE_HUSH_SAVEHISTORY && MAX_HISTORY > 0
11481 {
11482 const char *hp = get_local_var_value("HISTFILE");
11483 if (!hp) {
11484 hp = get_local_var_value("HOME");
11485 if (hp)
11486 hp = concat_path_file(hp, ".hush_history");
11487 } else {
11488 hp = xstrdup(hp);
11489 }
11490 if (hp) {
11491 G.line_input_state->hist_file = hp;
11492 //set_local_var(xasprintf("HISTFILE=%s", ...));
11493 }
11494# if ENABLE_FEATURE_SH_HISTFILESIZE
11495 hp = get_local_var_value("HISTFILESIZE");
11496 G.line_input_state->max_history = size_from_HISTFILESIZE(hp);
11497# endif
11498 }
11499# endif
11500 } else {
11501 install_special_sighandlers();
11502 }
11503#elif ENABLE_HUSH_INTERACTIVE
11504 /* No job control compiled in, only prompt/line editing */
11505 if (isatty(STDIN_FILENO) && isatty(STDOUT_FILENO)) {
11506 G_interactive_fd = dup_CLOEXEC(STDIN_FILENO, 254);
11507 if (G_interactive_fd < 0) {
11508 /* try to dup to any fd */
11509 G_interactive_fd = dup_CLOEXEC(STDIN_FILENO, -1);
11510 if (G_interactive_fd < 0)
11511 /* give up */
11512 G_interactive_fd = 0;
11513 }
11514 }
Francis Laniel110b7692023-12-22 22:02:27 +010011515 install_special_sighandlers();
11516#else
11517 /* We have interactiveness code disabled */
11518 install_special_sighandlers();
11519#endif
11520 /* bash:
11521 * if interactive but not a login shell, sources ~/.bashrc
11522 * (--norc turns this off, --rcfile <file> overrides)
11523 */
11524
11525 if (G_interactive_fd) {
11526#if ENABLE_HUSH_INTERACTIVE && ENABLE_FEATURE_EDITING_FANCY_PROMPT
11527 /* Set (but not export) PS1/2 unless already set */
11528 if (!get_local_var_value("PS1"))
11529 set_local_var_from_halves("PS1", "\\w \\$ ");
11530 if (!get_local_var_value("PS2"))
11531 set_local_var_from_halves("PS2", "> ");
11532#endif
11533 if (!ENABLE_FEATURE_SH_EXTRA_QUIET) {
11534 /* note: ash and hush share this string */
11535 printf("\n\n%s %s\n"
11536 IF_HUSH_HELP("Enter 'help' for a list of built-in commands.\n")
11537 "\n",
11538 bb_banner,
11539 "hush - the humble shell"
11540 );
11541 }
11542 }
11543
11544 parse_and_run_file(hfopen(NULL)); /* stdin */
11545
11546 final_return:
11547 hush_exit(G.last_exitcode);
11548}
11549
Francis Laniel110b7692023-12-22 22:02:27 +010011550/*
11551 * Built-ins
11552 */
11553static int FAST_FUNC builtin_true(char **argv UNUSED_PARAM)
11554{
11555 return 0;
11556}
11557
Francis Laniele7ca3a32023-12-22 22:02:42 +010011558static int FAST_FUNC builtin_false(char **argv UNUSED_PARAM)
11559{
11560 return 1;
11561}
11562
Francis Laniel110b7692023-12-22 22:02:27 +010011563#if ENABLE_HUSH_TEST || ENABLE_HUSH_ECHO || ENABLE_HUSH_PRINTF || ENABLE_HUSH_KILL
11564static NOINLINE int run_applet_main(char **argv, int (*applet_main_func)(int argc, char **argv))
11565{
11566 int argc = string_array_len(argv);
11567 return applet_main_func(argc, argv);
11568}
11569#endif
11570#if ENABLE_HUSH_TEST || BASH_TEST2
11571static int FAST_FUNC builtin_test(char **argv)
11572{
11573 return run_applet_main(argv, test_main);
11574}
11575#endif
11576#if ENABLE_HUSH_ECHO
11577static int FAST_FUNC builtin_echo(char **argv)
11578{
11579 return run_applet_main(argv, echo_main);
11580}
11581#endif
11582#if ENABLE_HUSH_PRINTF
11583static int FAST_FUNC builtin_printf(char **argv)
11584{
11585 return run_applet_main(argv, printf_main);
11586}
11587#endif
11588
11589#if ENABLE_HUSH_HELP
11590static int FAST_FUNC builtin_help(char **argv UNUSED_PARAM)
11591{
11592 const struct built_in_command *x;
11593
11594 printf(
11595 "Built-in commands:\n"
11596 "------------------\n");
11597 for (x = bltins1; x != &bltins1[ARRAY_SIZE(bltins1)]; x++) {
11598 if (x->b_descr)
11599 printf("%-10s%s\n", x->b_cmd, x->b_descr);
11600 }
11601 return EXIT_SUCCESS;
11602}
11603#endif
11604
11605#if MAX_HISTORY && ENABLE_FEATURE_EDITING
11606static int FAST_FUNC builtin_history(char **argv UNUSED_PARAM)
11607{
11608 show_history(G.line_input_state);
11609 return EXIT_SUCCESS;
11610}
11611#endif
11612
Francis Laniel110b7692023-12-22 22:02:27 +010011613static int FAST_FUNC builtin_cd(char **argv)
11614{
11615 const char *newdir;
11616
11617 argv = skip_dash_dash(argv);
11618 newdir = argv[0];
11619 if (newdir == NULL) {
11620 /* bash does nothing (exitcode 0) if HOME is ""; if it's unset,
11621 * bash says "bash: cd: HOME not set" and does nothing
11622 * (exitcode 1)
11623 */
11624 const char *home = get_local_var_value("HOME");
11625 newdir = home ? home : "/";
11626 }
11627 if (chdir(newdir)) {
11628 /* Mimic bash message exactly */
11629 bb_perror_msg("cd: %s", newdir);
11630 return EXIT_FAILURE;
11631 }
11632 /* Read current dir (get_cwd(1) is inside) and set PWD.
11633 * Note: do not enforce exporting. If PWD was unset or unexported,
11634 * set it again, but do not export. bash does the same.
11635 */
11636 set_pwd_var(/*flag:*/ 0);
11637 return EXIT_SUCCESS;
11638}
11639
11640static int FAST_FUNC builtin_pwd(char **argv UNUSED_PARAM)
11641{
11642 puts(get_cwd(0));
11643 return EXIT_SUCCESS;
11644}
11645
11646static int FAST_FUNC builtin_eval(char **argv)
11647{
11648 argv = skip_dash_dash(argv);
11649
11650 if (!argv[0])
11651 return EXIT_SUCCESS;
11652
11653 IF_HUSH_MODE_X(G.x_mode_depth++;)
11654 //bb_error_msg("%s: ++x_mode_depth=%d", __func__, G.x_mode_depth);
11655 if (!argv[1]) {
11656 /* bash:
11657 * eval "echo Hi; done" ("done" is syntax error):
11658 * "echo Hi" will not execute too.
11659 */
11660 parse_and_run_string(argv[0]);
11661 } else {
11662 /* "The eval utility shall construct a command by
11663 * concatenating arguments together, separating
11664 * each with a <space> character."
11665 */
11666 char *str, *p;
11667 unsigned len = 0;
11668 char **pp = argv;
11669 do
11670 len += strlen(*pp) + 1;
11671 while (*++pp);
11672 str = p = xmalloc(len);
11673 pp = argv;
11674 for (;;) {
11675 p = stpcpy(p, *pp);
11676 pp++;
11677 if (!*pp)
11678 break;
11679 *p++ = ' ';
11680 }
11681 parse_and_run_string(str);
11682 free(str);
11683 }
11684 IF_HUSH_MODE_X(G.x_mode_depth--;)
11685 //bb_error_msg("%s: --x_mode_depth=%d", __func__, G.x_mode_depth);
11686 return G.last_exitcode;
11687}
11688
11689static int FAST_FUNC builtin_exec(char **argv)
11690{
11691 argv = skip_dash_dash(argv);
11692 if (argv[0] == NULL)
11693 return EXIT_SUCCESS; /* bash does this */
11694
11695 /* Careful: we can end up here after [v]fork. Do not restore
11696 * tty pgrp then, only top-level shell process does that */
11697 if (G_saved_tty_pgrp && getpid() == G.root_pid)
11698 tcsetpgrp(G_interactive_fd, G_saved_tty_pgrp);
11699
11700 /* Saved-redirect fds, script fds and G_interactive_fd are still
11701 * open here. However, they are all CLOEXEC, and execv below
11702 * closes them. Try interactive "exec ls -l /proc/self/fd",
11703 * it should show no extra open fds in the "ls" process.
11704 * If we'd try to run builtins/NOEXECs, this would need improving.
11705 */
11706 //close_saved_fds_and_FILE_fds();
11707
11708 /* TODO: if exec fails, bash does NOT exit! We do.
11709 * We'll need to undo trap cleanup (it's inside execvp_or_die)
11710 * and tcsetpgrp, and this is inherently racy.
11711 */
11712 execvp_or_die(argv);
11713}
11714
11715static int FAST_FUNC builtin_exit(char **argv)
11716{
11717 debug_printf_exec("%s()\n", __func__);
11718
11719 /* interactive bash:
11720 * # trap "echo EEE" EXIT
11721 * # exit
11722 * exit
11723 * There are stopped jobs.
11724 * (if there are _stopped_ jobs, running ones don't count)
11725 * # exit
11726 * exit
11727 * EEE (then bash exits)
11728 *
11729 * TODO: we can use G.exiting = -1 as indicator "last cmd was exit"
11730 */
11731
11732 /* note: EXIT trap is run by hush_exit */
11733 argv = skip_dash_dash(argv);
11734 if (argv[0] == NULL) {
11735#if ENABLE_HUSH_TRAP
11736 if (G.pre_trap_exitcode >= 0) /* "exit" in trap uses $? from before the trap */
11737 hush_exit(G.pre_trap_exitcode);
11738#endif
11739 hush_exit(G.last_exitcode);
11740 }
11741 /* mimic bash: exit 123abc == exit 255 + error msg */
11742 xfunc_error_retval = 255;
11743 /* bash: exit -2 == exit 254, no error msg */
11744 hush_exit(xatoi(argv[0]) & 0xff);
11745}
11746
11747#if ENABLE_HUSH_TYPE
11748/* http://www.opengroup.org/onlinepubs/9699919799/utilities/type.html */
11749static int FAST_FUNC builtin_type(char **argv)
11750{
11751 int ret = EXIT_SUCCESS;
11752
11753 while (*++argv) {
11754 const char *type;
11755 char *path = NULL;
11756
11757 if (0) {} /* make conditional compile easier below */
11758 /*else if (find_alias(*argv))
11759 type = "an alias";*/
11760# if ENABLE_HUSH_FUNCTIONS
11761 else if (find_function(*argv))
11762 type = "a function";
11763# endif
11764 else if (find_builtin(*argv))
11765 type = "a shell builtin";
11766 else if ((path = find_in_path(*argv)) != NULL)
11767 type = path;
11768 else {
11769 bb_error_msg("type: %s: not found", *argv);
11770 ret = EXIT_FAILURE;
11771 continue;
11772 }
11773
11774 printf("%s is %s\n", *argv, type);
11775 free(path);
11776 }
11777
11778 return ret;
11779}
11780#endif
11781
11782#if ENABLE_HUSH_READ
11783/* Interruptibility of read builtin in bash
11784 * (tested on bash-4.2.8 by sending signals (not by ^C)):
11785 *
11786 * Empty trap makes read ignore corresponding signal, for any signal.
11787 *
11788 * SIGINT:
11789 * - terminates non-interactive shell;
11790 * - interrupts read in interactive shell;
11791 * if it has non-empty trap:
11792 * - executes trap and returns to command prompt in interactive shell;
11793 * - executes trap and returns to read in non-interactive shell;
11794 * SIGTERM:
11795 * - is ignored (does not interrupt) read in interactive shell;
11796 * - terminates non-interactive shell;
11797 * if it has non-empty trap:
11798 * - executes trap and returns to read;
11799 * SIGHUP:
11800 * - terminates shell (regardless of interactivity);
11801 * if it has non-empty trap:
11802 * - executes trap and returns to read;
11803 * SIGCHLD from children:
11804 * - does not interrupt read regardless of interactivity:
11805 * try: sleep 1 & read x; echo $x
11806 */
11807static int FAST_FUNC builtin_read(char **argv)
11808{
11809 const char *r;
11810 struct builtin_read_params params;
11811
11812 memset(&params, 0, sizeof(params));
11813
11814 /* "!": do not abort on errors.
11815 * Option string must start with "sr" to match BUILTIN_READ_xxx
11816 */
11817 params.read_flags = getopt32(argv,
11818# if BASH_READ_D
11819 IF_NOT_HUSH_BASH_COMPAT("^")
11820 "!srn:p:t:u:d:" IF_NOT_HUSH_BASH_COMPAT("\0" "-1"/*min 1 arg*/),
11821 &params.opt_n, &params.opt_p, &params.opt_t, &params.opt_u, &params.opt_d
11822# else
11823 IF_NOT_HUSH_BASH_COMPAT("^")
11824 "!srn:p:t:u:" IF_NOT_HUSH_BASH_COMPAT("\0" "-1"/*min 1 arg*/),
11825 &params.opt_n, &params.opt_p, &params.opt_t, &params.opt_u
11826# endif
11827//TODO: print "read: need variable name"
11828//for the case of !BASH "read" with no args (now it fails silently)
11829//(or maybe extend getopt32() to emit a message if "-1" fails)
11830 );
11831 if ((uint32_t)params.read_flags == (uint32_t)-1)
11832 return EXIT_FAILURE;
11833 argv += optind;
11834 params.argv = argv;
11835 params.setvar = set_local_var_from_halves;
11836 params.ifs = get_local_var_value("IFS"); /* can be NULL */
11837
11838 again:
11839 r = shell_builtin_read(&params);
11840
11841 if ((uintptr_t)r == 1 && errno == EINTR) {
11842 unsigned sig = check_and_run_traps();
11843 if (sig != SIGINT)
11844 goto again;
11845 }
11846
11847 if ((uintptr_t)r > 1) {
11848 bb_simple_error_msg(r);
11849 r = (char*)(uintptr_t)1;
11850 }
11851
11852 return (uintptr_t)r;
11853}
11854#endif
11855
11856#if ENABLE_HUSH_UMASK
11857static int FAST_FUNC builtin_umask(char **argv)
11858{
11859 int rc;
11860 mode_t mask;
11861
11862 rc = 1;
11863 mask = umask(0);
11864 argv = skip_dash_dash(argv);
11865 if (argv[0]) {
11866 mode_t old_mask = mask;
11867
11868 /* numeric umasks are taken as-is */
11869 /* symbolic umasks are inverted: "umask a=rx" calls umask(222) */
11870 if (!isdigit(argv[0][0]))
11871 mask ^= 0777;
11872 mask = bb_parse_mode(argv[0], mask);
11873 if (!isdigit(argv[0][0]))
11874 mask ^= 0777;
11875 if ((unsigned)mask > 0777) {
11876 mask = old_mask;
11877 /* bash messages:
11878 * bash: umask: 'q': invalid symbolic mode operator
11879 * bash: umask: 999: octal number out of range
11880 */
11881 bb_error_msg("%s: invalid mode '%s'", "umask", argv[0]);
11882 rc = 0;
11883 }
11884 } else {
11885 /* Mimic bash */
11886 printf("%04o\n", (unsigned) mask);
11887 /* fall through and restore mask which we set to 0 */
11888 }
11889 umask(mask);
11890
11891 return !rc; /* rc != 0 - success */
11892}
11893#endif
11894
Francis Laniele7ca3a32023-12-22 22:02:42 +010011895#if ENABLE_HUSH_EXPORT || ENABLE_HUSH_READONLY || ENABLE_HUSH_SET || ENABLE_HUSH_TRAP
Francis Laniel110b7692023-12-22 22:02:27 +010011896static void print_escaped(const char *s)
11897{
Francis Laniele7ca3a32023-12-22 22:02:42 +010011898//TODO? bash "set" does not quote variables which contain only alnums and "%+,-./:=@_~",
11899// (but "export" quotes all variables, even with only these chars).
11900// I think quoting strings with %+,=~ looks better
11901// (example: "set" printing var== instead of var='=' looks strange)
11902// IOW: do not quote "-./:@_": / is used in pathnames, : in PATH, -._ often in file names, @ in emails
11903
Francis Laniel110b7692023-12-22 22:02:27 +010011904 if (*s == '\'')
11905 goto squote;
11906 do {
11907 const char *p = strchrnul(s, '\'');
11908 /* print 'xxxx', possibly just '' */
11909 printf("'%.*s'", (int)(p - s), s);
11910 if (*p == '\0')
11911 break;
11912 s = p;
11913 squote:
11914 /* s points to '; print "'''...'''" */
11915 putchar('"');
11916 do putchar('\''); while (*++s == '\'');
11917 putchar('"');
11918 } while (*s);
11919}
11920#endif
11921
11922#if ENABLE_HUSH_EXPORT || ENABLE_HUSH_LOCAL || ENABLE_HUSH_READONLY
11923static int helper_export_local(char **argv, unsigned flags)
11924{
11925 do {
11926 char *name = *argv;
11927 const char *name_end = endofname(name);
11928
11929 if (*name_end == '\0') {
11930 struct variable *var, **vpp;
11931
Francis Laniele7ca3a32023-12-22 22:02:42 +010011932 vpp = get_ptr_to_local_var(name);
Francis Laniel110b7692023-12-22 22:02:27 +010011933 var = vpp ? *vpp : NULL;
11934
11935 if (flags & SETFLAG_UNEXPORT) {
11936 /* export -n NAME (without =VALUE) */
11937 if (var) {
11938 var->flg_export = 0;
11939 debug_printf_env("%s: unsetenv '%s'\n", __func__, name);
11940 unsetenv(name);
11941 } /* else: export -n NOT_EXISTING_VAR: no-op */
11942 continue;
11943 }
11944 if (flags & SETFLAG_EXPORT) {
11945 /* export NAME (without =VALUE) */
11946 if (var) {
11947 var->flg_export = 1;
11948 debug_printf_env("%s: putenv '%s'\n", __func__, var->varstr);
11949 putenv(var->varstr);
11950 continue;
11951 }
11952 }
11953 if (flags & SETFLAG_MAKE_RO) {
11954 /* readonly NAME (without =VALUE) */
11955 if (var) {
11956 var->flg_read_only = 1;
11957 continue;
11958 }
11959 }
11960# if ENABLE_HUSH_LOCAL
11961 /* Is this "local" bltin? */
11962 if (!(flags & (SETFLAG_EXPORT|SETFLAG_UNEXPORT|SETFLAG_MAKE_RO))) {
11963 unsigned lvl = flags >> SETFLAG_VARLVL_SHIFT;
11964 if (var && var->var_nest_level == lvl) {
11965 /* "local x=abc; ...; local x" - ignore second local decl */
11966 continue;
11967 }
11968 }
11969# endif
11970 /* Exporting non-existing variable.
11971 * bash does not put it in environment,
11972 * but remembers that it is exported,
11973 * and does put it in env when it is set later.
11974 * We just set it to "" and export.
11975 */
11976 /* Or, it's "local NAME" (without =VALUE).
11977 * bash sets the value to "".
11978 */
11979 /* Or, it's "readonly NAME" (without =VALUE).
11980 * bash remembers NAME and disallows its creation
11981 * in the future.
11982 */
11983 name = xasprintf("%s=", name);
11984 } else {
11985 if (*name_end != '=') {
11986 bb_error_msg("'%s': bad variable name", name);
11987 /* do not parse following argv[]s: */
11988 return 1;
11989 }
11990 /* (Un)exporting/making local NAME=VALUE */
11991 name = xstrdup(name);
11992 /* Testcase: export PS1='\w \$ ' */
11993 unbackslash(name);
11994 }
11995 debug_printf_env("%s: set_local_var('%s')\n", __func__, name);
11996 if (set_local_var(name, flags))
11997 return EXIT_FAILURE;
11998 } while (*++argv);
11999 return EXIT_SUCCESS;
12000}
12001#endif
12002
12003#if ENABLE_HUSH_EXPORT
12004static int FAST_FUNC builtin_export(char **argv)
12005{
12006 unsigned opt_unexport;
12007
12008# if ENABLE_HUSH_EXPORT_N
12009 /* "!": do not abort on errors */
12010 opt_unexport = getopt32(argv, "!n");
12011 if (opt_unexport == (uint32_t)-1)
12012 return EXIT_FAILURE;
12013 argv += optind;
12014# else
12015 opt_unexport = 0;
12016 argv++;
12017# endif
12018
12019 if (argv[0] == NULL) {
12020 char **e = environ;
12021 if (e) {
12022 while (*e) {
12023# if 0
12024 puts(*e++);
12025# else
12026 /* ash emits: export VAR='VAL'
12027 * bash: declare -x VAR="VAL"
12028 * we follow ash example */
12029 const char *s = *e++;
12030 const char *p = strchr(s, '=');
12031
12032 if (!p) /* wtf? take next variable */
12033 continue;
Francis Laniele7ca3a32023-12-22 22:02:42 +010012034 /* "export VAR=" */
12035 printf("%s %.*s", "export", (int)(p - s) + 1, s);
Francis Laniel110b7692023-12-22 22:02:27 +010012036 print_escaped(p + 1);
12037 putchar('\n');
12038# endif
12039 }
12040 /*fflush_all(); - done after each builtin anyway */
12041 }
12042 return EXIT_SUCCESS;
12043 }
12044
12045 return helper_export_local(argv, opt_unexport ? SETFLAG_UNEXPORT : SETFLAG_EXPORT);
12046}
12047#endif
12048
12049#if ENABLE_HUSH_LOCAL
12050static int FAST_FUNC builtin_local(char **argv)
12051{
12052 if (G.func_nest_level == 0) {
12053 bb_error_msg("%s: not in a function", argv[0]);
12054 return EXIT_FAILURE; /* bash compat */
12055 }
Francis Laniele7ca3a32023-12-22 22:02:42 +010012056//TODO? ash and bash support "local -" special form,
12057//which saves/restores $- around function call (including async returns, such as ^C)
12058//(IOW: it makes "set +/-..." effects local)
Francis Laniel110b7692023-12-22 22:02:27 +010012059 argv++;
12060 /* Since all builtins run in a nested variable level,
12061 * need to use level - 1 here. Or else the variable will be removed at once
12062 * after builtin returns.
12063 */
12064 return helper_export_local(argv, (G.var_nest_level - 1) << SETFLAG_VARLVL_SHIFT);
12065}
12066#endif
12067
12068#if ENABLE_HUSH_READONLY
12069static int FAST_FUNC builtin_readonly(char **argv)
12070{
12071 argv++;
12072 if (*argv == NULL) {
12073 /* bash: readonly [-p]: list all readonly VARs
12074 * (-p has no effect in bash)
12075 */
12076 struct variable *e;
12077 for (e = G.top_var; e; e = e->next) {
12078 if (e->flg_read_only) {
Francis Laniele7ca3a32023-12-22 22:02:42 +010012079 const char *s = e->varstr;
12080 const char *p = strchr(s, '=');
12081
12082 if (!p) /* wtf? take next variable */
12083 continue;
12084 /* "readonly VAR=" */
12085 printf("%s %.*s", "readonly", (int)(p - s) + 1, s);
12086 print_escaped(p + 1);
12087 putchar('\n');
Francis Laniel110b7692023-12-22 22:02:27 +010012088 }
12089 }
12090 return EXIT_SUCCESS;
12091 }
12092 return helper_export_local(argv, SETFLAG_MAKE_RO);
12093}
12094#endif
12095
12096#if ENABLE_HUSH_UNSET
12097/* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#unset */
12098static int FAST_FUNC builtin_unset(char **argv)
12099{
12100 int ret;
12101 unsigned opts;
12102
12103 /* "!": do not abort on errors */
12104 /* "+": stop at 1st non-option */
12105 opts = getopt32(argv, "!+vf");
12106 if (opts == (unsigned)-1)
12107 return EXIT_FAILURE;
12108 if (opts == 3) {
12109 bb_simple_error_msg("unset: -v and -f are exclusive");
12110 return EXIT_FAILURE;
12111 }
12112 argv += optind;
12113
12114 ret = EXIT_SUCCESS;
12115 while (*argv) {
12116 if (!(opts & 2)) { /* not -f */
12117 if (unset_local_var(*argv)) {
12118 /* unset <nonexistent_var> doesn't fail.
12119 * Error is when one tries to unset RO var.
12120 * Message was printed by unset_local_var. */
12121 ret = EXIT_FAILURE;
12122 }
12123 }
12124# if ENABLE_HUSH_FUNCTIONS
12125 else {
12126 unset_func(*argv);
12127 }
12128# endif
12129 argv++;
12130 }
12131 return ret;
12132}
12133#endif
12134
12135#if ENABLE_HUSH_SET
12136/* http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#set
12137 * built-in 'set' handler
12138 * SUSv3 says:
12139 * set [-abCefhmnuvx] [-o option] [argument...]
12140 * set [+abCefhmnuvx] [+o option] [argument...]
12141 * set -- [argument...]
12142 * set -o
12143 * set +o
12144 * Implementations shall support the options in both their hyphen and
12145 * plus-sign forms. These options can also be specified as options to sh.
12146 * Examples:
12147 * Write out all variables and their values: set
12148 * Set $1, $2, and $3 and set "$#" to 3: set c a b
12149 * Turn on the -x and -v options: set -xv
12150 * Unset all positional parameters: set --
12151 * Set $1 to the value of x, even if it begins with '-' or '+': set -- "$x"
12152 * Set the positional parameters to the expansion of x, even if x expands
12153 * with a leading '-' or '+': set -- $x
12154 *
12155 * So far, we only support "set -- [argument...]" and some of the short names.
12156 */
12157static int FAST_FUNC builtin_set(char **argv)
12158{
12159 int n;
12160 char **pp, **g_argv;
12161 char *arg = *++argv;
12162
12163 if (arg == NULL) {
12164 struct variable *e;
Francis Laniele7ca3a32023-12-22 22:02:42 +010012165 for (e = G.top_var; e; e = e->next) {
12166 const char *s = e->varstr;
12167 const char *p = strchr(s, '=');
12168
12169 if (!p) /* wtf? take next variable */
12170 continue;
12171 /* var= */
12172 printf("%.*s", (int)(p - s) + 1, s);
12173 print_escaped(p + 1);
12174 putchar('\n');
12175 }
Francis Laniel110b7692023-12-22 22:02:27 +010012176 return EXIT_SUCCESS;
12177 }
12178
12179 do {
12180 if (strcmp(arg, "--") == 0) {
12181 ++argv;
12182 goto set_argv;
12183 }
12184 if (arg[0] != '+' && arg[0] != '-')
12185 break;
12186 for (n = 1; arg[n]; ++n) {
12187 if (set_mode((arg[0] == '-'), arg[n], argv[1])) {
12188 bb_error_msg("%s: %s: invalid option", "set", arg);
12189 return EXIT_FAILURE;
12190 }
12191 if (arg[n] == 'o' && argv[1])
12192 argv++;
12193 }
12194 } while ((arg = *++argv) != NULL);
12195 /* Now argv[0] is 1st argument */
12196
12197 if (arg == NULL)
12198 return EXIT_SUCCESS;
12199 set_argv:
12200
12201 /* NB: G.global_argv[0] ($0) is never freed/changed */
12202 g_argv = G.global_argv;
12203 if (G.global_args_malloced) {
12204 pp = g_argv;
12205 while (*++pp)
12206 free(*pp);
12207 g_argv[1] = NULL;
12208 } else {
12209 G.global_args_malloced = 1;
12210 pp = xzalloc(sizeof(pp[0]) * 2);
12211 pp[0] = g_argv[0]; /* retain $0 */
12212 g_argv = pp;
12213 }
12214 /* This realloc's G.global_argv */
12215 G.global_argv = pp = add_strings_to_strings(g_argv, argv, /*dup:*/ 1);
12216
12217 G.global_argc = 1 + string_array_len(pp + 1);
12218
12219 return EXIT_SUCCESS;
12220}
12221#endif
12222
12223static int FAST_FUNC builtin_shift(char **argv)
12224{
12225 int n = 1;
12226 argv = skip_dash_dash(argv);
12227 if (argv[0]) {
12228 n = bb_strtou(argv[0], NULL, 10);
12229 if (errno || n < 0) {
12230 /* shared string with ash.c */
12231 bb_error_msg("Illegal number: %s", argv[0]);
12232 /*
12233 * ash aborts in this case.
12234 * bash prints error message and set $? to 1.
12235 * Interestingly, for "shift 99999" bash does not
12236 * print error message, but does set $? to 1
12237 * (and does no shifting at all).
12238 */
12239 }
12240 }
12241 if (n >= 0 && n < G.global_argc) {
12242 if (G_global_args_malloced) {
12243 int m = 1;
12244 while (m <= n)
12245 free(G.global_argv[m++]);
12246 }
12247 G.global_argc -= n;
12248 memmove(&G.global_argv[1], &G.global_argv[n+1],
12249 G.global_argc * sizeof(G.global_argv[0]));
12250 return EXIT_SUCCESS;
12251 }
12252 return EXIT_FAILURE;
12253}
12254
12255#if ENABLE_HUSH_GETOPTS
12256static int FAST_FUNC builtin_getopts(char **argv)
12257{
12258/* http://pubs.opengroup.org/onlinepubs/9699919799/utilities/getopts.html
12259
12260TODO:
12261If a required argument is not found, and getopts is not silent,
12262a question mark (?) is placed in VAR, OPTARG is unset, and a
12263diagnostic message is printed. If getopts is silent, then a
12264colon (:) is placed in VAR and OPTARG is set to the option
12265character found.
12266
12267Test that VAR is a valid variable name?
12268
12269"Whenever the shell is invoked, OPTIND shall be initialized to 1"
12270*/
12271 char cbuf[2];
12272 const char *cp, *optstring, *var;
12273 int c, n, exitcode, my_opterr;
12274 unsigned count;
12275
12276 optstring = *++argv;
12277 if (!optstring || !(var = *++argv)) {
12278 bb_simple_error_msg("usage: getopts OPTSTRING VAR [ARGS]");
12279 return EXIT_FAILURE;
12280 }
12281
12282 if (argv[1])
12283 argv[0] = G.global_argv[0]; /* for error messages in getopt() */
12284 else
12285 argv = G.global_argv;
12286 cbuf[1] = '\0';
12287
12288 my_opterr = 0;
12289 if (optstring[0] != ':') {
12290 cp = get_local_var_value("OPTERR");
12291 /* 0 if "OPTERR=0", 1 otherwise */
12292 my_opterr = (!cp || NOT_LONE_CHAR(cp, '0'));
12293 }
12294
12295 /* getopts stops on first non-option. Add "+" to force that */
12296 /*if (optstring[0] != '+')*/ {
12297 char *s = alloca(strlen(optstring) + 2);
12298 sprintf(s, "+%s", optstring);
12299 optstring = s;
12300 }
12301
12302 /* Naively, now we should just
12303 * cp = get_local_var_value("OPTIND");
12304 * optind = cp ? atoi(cp) : 0;
12305 * optarg = NULL;
12306 * opterr = my_opterr;
12307 * c = getopt(string_array_len(argv), argv, optstring);
12308 * and be done? Not so fast...
12309 * Unlike normal getopt() usage in C programs, here
12310 * each successive call will (usually) have the same argv[] CONTENTS,
12311 * but not the ADDRESSES. Worse yet, it's possible that between
12312 * invocations of "getopts", there will be calls to shell builtins
12313 * which use getopt() internally. Example:
12314 * while getopts "abc" RES -a -bc -abc de; do
12315 * unset -ff func
12316 * done
12317 * This would not work correctly: getopt() call inside "unset"
12318 * modifies internal libc state which is tracking position in
12319 * multi-option strings ("-abc"). At best, it can skip options
12320 * or return the same option infinitely. With glibc implementation
12321 * of getopt(), it would use outright invalid pointers and return
12322 * garbage even _without_ "unset" mangling internal state.
12323 *
12324 * We resort to resetting getopt() state and calling it N times,
12325 * until we get Nth result (or failure).
12326 * (N == G.getopt_count is reset to 0 whenever OPTIND is [un]set).
12327 */
12328 GETOPT_RESET();
12329 count = 0;
12330 n = string_array_len(argv);
12331 do {
12332 optarg = NULL;
12333 opterr = (count < G.getopt_count) ? 0 : my_opterr;
12334 c = getopt(n, argv, optstring);
12335 if (c < 0)
12336 break;
12337 count++;
12338 } while (count <= G.getopt_count);
12339
12340 /* Set OPTIND. Prevent resetting of the magic counter! */
12341 set_local_var_from_halves("OPTIND", utoa(optind));
12342 G.getopt_count = count; /* "next time, give me N+1'th result" */
12343 GETOPT_RESET(); /* just in case */
12344
12345 /* Set OPTARG */
12346 /* Always set or unset, never left as-is, even on exit/error:
12347 * "If no option was found, or if the option that was found
12348 * does not have an option-argument, OPTARG shall be unset."
12349 */
12350 cp = optarg;
12351 if (c == '?') {
12352 /* If ":optstring" and unknown option is seen,
12353 * it is stored to OPTARG.
12354 */
12355 if (optstring[1] == ':') {
12356 cbuf[0] = optopt;
12357 cp = cbuf;
12358 }
12359 }
12360 if (cp)
12361 set_local_var_from_halves("OPTARG", cp);
12362 else
12363 unset_local_var("OPTARG");
12364
12365 /* Convert -1 to "?" */
12366 exitcode = EXIT_SUCCESS;
12367 if (c < 0) { /* -1: end of options */
12368 exitcode = EXIT_FAILURE;
12369 c = '?';
12370 }
12371
12372 /* Set VAR */
12373 cbuf[0] = c;
12374 set_local_var_from_halves(var, cbuf);
12375
12376 return exitcode;
12377}
12378#endif
12379
12380static int FAST_FUNC builtin_source(char **argv)
12381{
12382 char *arg_path, *filename;
12383 HFILE *input;
12384 save_arg_t sv;
12385 char *args_need_save;
12386#if ENABLE_HUSH_FUNCTIONS
12387 smallint sv_flg;
12388#endif
12389
12390 argv = skip_dash_dash(argv);
12391 filename = argv[0];
12392 if (!filename) {
12393 /* bash says: "bash: .: filename argument required" */
12394 return 2; /* bash compat */
12395 }
12396 arg_path = NULL;
12397 if (!strchr(filename, '/')) {
12398 arg_path = find_in_path(filename);
12399 if (arg_path)
12400 filename = arg_path;
12401 else if (!ENABLE_HUSH_BASH_SOURCE_CURDIR) {
12402 errno = ENOENT;
12403 bb_simple_perror_msg(filename);
12404 return EXIT_FAILURE;
12405 }
12406 }
12407 input = hfopen(filename);
12408 free(arg_path);
12409 if (!input) {
12410 bb_perror_msg("%s", filename);
12411 /* POSIX: non-interactive shell should abort here,
12412 * not merely fail. So far no one complained :)
12413 */
12414 return EXIT_FAILURE;
12415 }
12416
12417#if ENABLE_HUSH_FUNCTIONS
12418 sv_flg = G_flag_return_in_progress;
12419 /* "we are inside sourced file, ok to use return" */
12420 G_flag_return_in_progress = -1;
12421#endif
12422 args_need_save = argv[1]; /* used as a boolean variable */
12423 if (args_need_save)
12424 save_and_replace_G_args(&sv, argv);
12425
12426 /* "false; . ./empty_line; echo Zero:$?" should print 0 */
12427 G.last_exitcode = 0;
12428 parse_and_run_file(input);
12429 hfclose(input);
12430
12431 if (args_need_save) /* can't use argv[1] instead: "shift" can mangle it */
12432 restore_G_args(&sv, argv);
12433#if ENABLE_HUSH_FUNCTIONS
12434 G_flag_return_in_progress = sv_flg;
12435#endif
12436
12437 return G.last_exitcode;
12438}
12439
12440#if ENABLE_HUSH_TRAP
12441static int FAST_FUNC builtin_trap(char **argv)
12442{
12443 int sig;
12444 char *new_cmd;
12445
12446 if (!G_traps)
12447 G_traps = xzalloc(sizeof(G_traps[0]) * NSIG);
12448
12449 argv++;
12450 if (!*argv) {
12451 int i;
12452 /* No args: print all trapped */
12453 for (i = 0; i < NSIG; ++i) {
12454 if (G_traps[i]) {
12455 printf("trap -- ");
12456 print_escaped(G_traps[i]);
12457 /* note: bash adds "SIG", but only if invoked
12458 * as "bash". If called as "sh", or if set -o posix,
12459 * then it prints short signal names.
12460 * We are printing short names: */
12461 printf(" %s\n", get_signame(i));
12462 }
12463 }
12464 /*fflush_all(); - done after each builtin anyway */
12465 return EXIT_SUCCESS;
12466 }
12467
12468 new_cmd = NULL;
12469 /* If first arg is a number: reset all specified signals */
12470 sig = bb_strtou(*argv, NULL, 10);
12471 if (errno == 0) {
12472 int ret;
12473 process_sig_list:
12474 ret = EXIT_SUCCESS;
12475 while (*argv) {
12476 sighandler_t handler;
12477
12478 sig = get_signum(*argv++);
12479 if (sig < 0) {
12480 ret = EXIT_FAILURE;
12481 /* Mimic bash message exactly */
12482 bb_error_msg("trap: %s: invalid signal specification", argv[-1]);
12483 continue;
12484 }
12485
12486 free(G_traps[sig]);
12487 G_traps[sig] = xstrdup(new_cmd);
12488
12489 debug_printf("trap: setting SIG%s (%i) to '%s'\n",
12490 get_signame(sig), sig, G_traps[sig]);
12491
12492 /* There is no signal for 0 (EXIT) */
12493 if (sig == 0)
12494 continue;
12495
12496 if (new_cmd)
12497 handler = (new_cmd[0] ? record_pending_signo : SIG_IGN);
12498 else
12499 /* We are removing trap handler */
12500 handler = pick_sighandler(sig);
12501 install_sighandler(sig, handler);
12502 }
12503 return ret;
12504 }
12505
12506 if (!argv[1]) { /* no second arg */
12507 bb_simple_error_msg("trap: invalid arguments");
12508 return EXIT_FAILURE;
12509 }
12510
12511 /* First arg is "-": reset all specified to default */
12512 /* First arg is "--": skip it, the rest is "handler SIGs..." */
12513 /* Everything else: set arg as signal handler
12514 * (includes "" case, which ignores signal) */
12515 if (argv[0][0] == '-') {
12516 if (argv[0][1] == '\0') { /* "-" */
12517 /* new_cmd remains NULL: "reset these sigs" */
12518 goto reset_traps;
12519 }
12520 if (argv[0][1] == '-' && argv[0][2] == '\0') { /* "--" */
12521 argv++;
12522 }
12523 /* else: "-something", no special meaning */
12524 }
12525 new_cmd = *argv;
12526 reset_traps:
12527 argv++;
12528 goto process_sig_list;
12529}
12530#endif
12531
12532#if ENABLE_HUSH_JOB
12533static struct pipe *parse_jobspec(const char *str)
12534{
12535 struct pipe *pi;
12536 unsigned jobnum;
12537
12538 if (sscanf(str, "%%%u", &jobnum) != 1) {
12539 if (str[0] != '%'
12540 || (str[1] != '%' && str[1] != '+' && str[1] != '\0')
12541 ) {
12542 bb_error_msg("bad argument '%s'", str);
12543 return NULL;
12544 }
12545 /* It is "%%", "%+" or "%" - current job */
12546 jobnum = G.last_jobid;
12547 if (jobnum == 0) {
12548 bb_simple_error_msg("no current job");
12549 return NULL;
12550 }
12551 }
12552 for (pi = G.job_list; pi; pi = pi->next) {
12553 if (pi->jobid == jobnum) {
12554 return pi;
12555 }
12556 }
12557 bb_error_msg("%u: no such job", jobnum);
12558 return NULL;
12559}
12560
12561static int FAST_FUNC builtin_jobs(char **argv UNUSED_PARAM)
12562{
12563 struct pipe *job;
12564 const char *status_string;
12565
12566 checkjobs(NULL, 0 /*(no pid to wait for)*/);
12567 for (job = G.job_list; job; job = job->next) {
12568 if (job->alive_cmds == job->stopped_cmds)
12569 status_string = "Stopped";
12570 else
12571 status_string = "Running";
12572
12573 printf(JOB_STATUS_FORMAT, job->jobid, status_string, job->cmdtext);
12574 }
12575
12576 clean_up_last_dead_job();
12577
12578 return EXIT_SUCCESS;
12579}
12580
12581/* built-in 'fg' and 'bg' handler */
12582static int FAST_FUNC builtin_fg_bg(char **argv)
12583{
12584 int i;
12585 struct pipe *pi;
12586
12587 if (!G_interactive_fd)
12588 return EXIT_FAILURE;
12589
12590 /* If they gave us no args, assume they want the last backgrounded task */
12591 if (!argv[1]) {
12592 for (pi = G.job_list; pi; pi = pi->next) {
12593 if (pi->jobid == G.last_jobid) {
12594 goto found;
12595 }
12596 }
12597 bb_error_msg("%s: no current job", argv[0]);
12598 return EXIT_FAILURE;
12599 }
12600
12601 pi = parse_jobspec(argv[1]);
12602 if (!pi)
12603 return EXIT_FAILURE;
12604 found:
12605 /* TODO: bash prints a string representation
12606 * of job being foregrounded (like "sleep 1 | cat") */
12607 if (argv[0][0] == 'f' && G_saved_tty_pgrp) {
Francis Laniele7ca3a32023-12-22 22:02:42 +010012608 /* Put the job into the foreground. */
Francis Laniel110b7692023-12-22 22:02:27 +010012609 tcsetpgrp(G_interactive_fd, pi->pgrp);
12610 }
12611
12612 /* Restart the processes in the job */
12613 debug_printf_jobs("reviving %d procs, pgrp %d\n", pi->num_cmds, pi->pgrp);
12614 for (i = 0; i < pi->num_cmds; i++) {
12615 debug_printf_jobs("reviving pid %d\n", pi->cmds[i].pid);
12616 }
12617 pi->stopped_cmds = 0;
12618
12619 i = kill(- pi->pgrp, SIGCONT);
12620 if (i < 0) {
12621 if (errno == ESRCH) {
12622 delete_finished_job(pi);
12623 return EXIT_SUCCESS;
12624 }
12625 bb_simple_perror_msg("kill (SIGCONT)");
12626 }
12627
12628 if (argv[0][0] == 'f') {
12629 remove_job_from_table(pi); /* FG job shouldn't be in job table */
12630 return checkjobs_and_fg_shell(pi);
12631 }
12632 return EXIT_SUCCESS;
12633}
12634#endif
12635
12636#if ENABLE_HUSH_KILL
12637static int FAST_FUNC builtin_kill(char **argv)
12638{
12639 int ret = 0;
12640
12641# if ENABLE_HUSH_JOB
12642 if (argv[1] && strcmp(argv[1], "-l") != 0) {
12643 int i = 1;
12644
12645 do {
12646 struct pipe *pi;
12647 char *dst;
12648 int j, n;
12649
12650 if (argv[i][0] != '%')
12651 continue;
12652 /*
12653 * "kill %N" - job kill
12654 * Converting to pgrp / pid kill
12655 */
12656 pi = parse_jobspec(argv[i]);
12657 if (!pi) {
12658 /* Eat bad jobspec */
12659 j = i;
12660 do {
12661 j++;
12662 argv[j - 1] = argv[j];
12663 } while (argv[j]);
12664 ret = 1;
12665 i--;
12666 continue;
12667 }
12668 /*
12669 * In jobs started under job control, we signal
12670 * entire process group by kill -PGRP_ID.
12671 * This happens, f.e., in interactive shell.
12672 *
12673 * Otherwise, we signal each child via
12674 * kill PID1 PID2 PID3.
12675 * Testcases:
12676 * sh -c 'sleep 1|sleep 1 & kill %1'
12677 * sh -c 'true|sleep 2 & sleep 1; kill %1'
12678 * sh -c 'true|sleep 1 & sleep 2; kill %1'
12679 */
12680 n = G_interactive_fd ? 1 : pi->num_cmds;
12681 dst = alloca(n * sizeof(int)*4);
12682 argv[i] = dst;
12683 if (G_interactive_fd)
12684 dst += sprintf(dst, " -%u", (int)pi->pgrp);
12685 else for (j = 0; j < n; j++) {
12686 struct command *cmd = &pi->cmds[j];
12687 /* Skip exited members of the job */
12688 if (cmd->pid == 0)
12689 continue;
12690 /*
12691 * kill_main has matching code to expect
12692 * leading space. Needed to not confuse
12693 * negative pids with "kill -SIGNAL_NO" syntax
12694 */
12695 dst += sprintf(dst, " %u", (int)cmd->pid);
12696 }
12697 *dst = '\0';
12698 } while (argv[++i]);
12699 }
12700# endif
12701
12702 if (argv[1] || ret == 0) {
12703 ret = run_applet_main(argv, kill_main);
12704 }
12705 /* else: ret = 1, "kill %bad_jobspec" case */
12706 return ret;
12707}
12708#endif
12709
12710#if ENABLE_HUSH_WAIT
12711/* http://www.opengroup.org/onlinepubs/9699919799/utilities/wait.html */
12712# if !ENABLE_HUSH_JOB
12713# define wait_for_child_or_signal(pipe,pid) wait_for_child_or_signal(pid)
12714# endif
12715static int wait_for_child_or_signal(struct pipe *waitfor_pipe, pid_t waitfor_pid)
12716{
12717 int ret = 0;
12718 for (;;) {
12719 int sig;
12720 sigset_t oldset;
12721
12722 if (!sigisemptyset(&G.pending_set))
12723 goto check_sig;
12724
12725 /* waitpid is not interruptible by SA_RESTARTed
12726 * signals which we use. Thus, this ugly dance:
12727 */
12728
12729 /* Make sure possible SIGCHLD is stored in kernel's
12730 * pending signal mask before we call waitpid.
12731 * Or else we may race with SIGCHLD, lose it,
12732 * and get stuck in sigsuspend...
12733 */
12734 sigfillset(&oldset); /* block all signals, remember old set */
12735 sigprocmask2(SIG_SETMASK, &oldset);
12736
12737 if (!sigisemptyset(&G.pending_set)) {
12738 /* Crap! we raced with some signal! */
12739 goto restore;
12740 }
12741
12742 /*errno = 0; - checkjobs does this */
12743/* Can't pass waitfor_pipe into checkjobs(): it won't be interruptible */
12744 ret = checkjobs(NULL, waitfor_pid); /* waitpid(WNOHANG) inside */
12745 debug_printf_exec("checkjobs:%d\n", ret);
12746# if ENABLE_HUSH_JOB
12747 if (waitfor_pipe) {
12748 int rcode = job_exited_or_stopped(waitfor_pipe);
12749 debug_printf_exec("job_exited_or_stopped:%d\n", rcode);
12750 if (rcode >= 0) {
12751 ret = rcode;
12752 sigprocmask(SIG_SETMASK, &oldset, NULL);
12753 break;
12754 }
12755 }
12756# endif
12757 /* if ECHILD, there are no children (ret is -1 or 0) */
12758 /* if ret == 0, no children changed state */
12759 /* if ret != 0, it's exitcode+1 of exited waitfor_pid child */
12760 if (errno == ECHILD || ret) {
12761 ret--;
12762 if (ret < 0) /* if ECHILD, may need to fix "ret" */
12763 ret = 0;
12764# if ENABLE_HUSH_BASH_COMPAT
12765 if (waitfor_pid == -1 && errno == ECHILD) {
12766 /* exitcode of "wait -n" with no children is 127, not 0 */
12767 ret = 127;
12768 }
12769# endif
12770 sigprocmask(SIG_SETMASK, &oldset, NULL);
12771 break;
12772 }
12773 /* Wait for SIGCHLD or any other signal */
12774 /* It is vitally important for sigsuspend that SIGCHLD has non-DFL handler! */
12775 /* Note: sigsuspend invokes signal handler */
12776 sigsuspend(&oldset);
12777 /* ^^^ add "sigdelset(&oldset, SIGCHLD)" before sigsuspend
12778 * to make sure SIGCHLD is not masked off?
12779 * It was reported that this:
12780 * fn() { : | return; }
12781 * shopt -s lastpipe
12782 * fn
12783 * exec hush SCRIPT
12784 * under bash 4.4.23 runs SCRIPT with SIGCHLD masked,
12785 * making "wait" commands in SCRIPT block forever.
12786 */
12787 restore:
12788 sigprocmask(SIG_SETMASK, &oldset, NULL);
12789 check_sig:
12790 /* So, did we get a signal? */
12791 sig = check_and_run_traps();
12792 if (sig /*&& sig != SIGCHLD - always true */) {
12793 /* Do this for any (non-ignored) signal, not only for ^C */
12794 ret = 128 | sig;
12795 break;
12796 }
12797 /* SIGCHLD, or no signal, or ignored one, such as SIGQUIT. Repeat */
12798 }
12799 return ret;
12800}
12801
12802static int FAST_FUNC builtin_wait(char **argv)
12803{
12804 int ret;
12805 int status;
12806
12807 argv = skip_dash_dash(argv);
12808# if ENABLE_HUSH_BASH_COMPAT
12809 if (argv[0] && strcmp(argv[0], "-n") == 0) {
12810 /* wait -n */
12811 /* (bash accepts "wait -n PID" too and ignores PID) */
12812 G.dead_job_exitcode = -1;
12813 return wait_for_child_or_signal(NULL, -1 /*no job, wait for one job*/);
12814 }
12815# endif
12816 if (argv[0] == NULL) {
12817 /* Don't care about wait results */
12818 /* Note 1: must wait until there are no more children */
12819 /* Note 2: must be interruptible */
12820 /* Examples:
12821 * $ sleep 3 & sleep 6 & wait
12822 * [1] 30934 sleep 3
12823 * [2] 30935 sleep 6
12824 * [1] Done sleep 3
12825 * [2] Done sleep 6
12826 * $ sleep 3 & sleep 6 & wait
12827 * [1] 30936 sleep 3
12828 * [2] 30937 sleep 6
12829 * [1] Done sleep 3
12830 * ^C <-- after ~4 sec from keyboard
12831 * $
12832 */
12833 return wait_for_child_or_signal(NULL, 0 /*no job and no pid to wait for*/);
12834 }
12835
12836 do {
12837 pid_t pid = bb_strtou(*argv, NULL, 10);
12838 if (errno || pid <= 0) {
12839# if ENABLE_HUSH_JOB
12840 if (argv[0][0] == '%') {
12841 struct pipe *wait_pipe;
12842 ret = 127; /* bash compat for bad jobspecs */
12843 wait_pipe = parse_jobspec(*argv);
12844 if (wait_pipe) {
12845 ret = job_exited_or_stopped(wait_pipe);
12846 if (ret < 0) {
12847 ret = wait_for_child_or_signal(wait_pipe, 0);
12848 } else {
12849 /* waiting on "last dead job" removes it */
12850 clean_up_last_dead_job();
12851 }
12852 }
12853 /* else: parse_jobspec() already emitted error msg */
12854 continue;
12855 }
12856# endif
12857 /* mimic bash message */
12858 bb_error_msg("wait: '%s': not a pid or valid job spec", *argv);
12859 ret = EXIT_FAILURE;
12860 continue; /* bash checks all argv[] */
12861 }
12862
12863 /* Do we have such child? */
12864 ret = waitpid(pid, &status, WNOHANG);
12865 if (ret < 0) {
12866 /* No */
12867 ret = 127;
12868 if (errno == ECHILD) {
12869 if (pid == G.last_bg_pid) {
12870 /* "wait $!" but last bg task has already exited. Try:
12871 * (sleep 1; exit 3) & sleep 2; echo $?; wait $!; echo $?
12872 * In bash it prints exitcode 0, then 3.
12873 * In dash, it is 127.
12874 */
12875 ret = G.last_bg_pid_exitcode;
12876 } else {
12877 /* Example: "wait 1". mimic bash message */
12878 bb_error_msg("wait: pid %u is not a child of this shell", (unsigned)pid);
12879 }
12880 } else {
12881 /* ??? */
12882 bb_perror_msg("wait %s", *argv);
12883 }
12884 continue; /* bash checks all argv[] */
12885 }
12886 if (ret == 0) {
12887 /* Yes, and it still runs */
12888 ret = wait_for_child_or_signal(NULL, pid);
12889 } else {
12890 /* Yes, and it just exited */
12891 process_wait_result(NULL, pid, status);
12892 ret = WEXITSTATUS(status);
12893 if (WIFSIGNALED(status))
12894 ret = 128 | WTERMSIG(status);
12895 }
12896 } while (*++argv);
12897
12898 return ret;
12899}
12900#endif
12901
12902#if ENABLE_HUSH_LOOPS || ENABLE_HUSH_FUNCTIONS
12903static unsigned parse_numeric_argv1(char **argv, unsigned def, unsigned def_min)
12904{
12905 if (argv[1]) {
12906 def = bb_strtou(argv[1], NULL, 10);
12907 if (errno || def < def_min || argv[2]) {
12908 bb_error_msg("%s: bad arguments", argv[0]);
12909 def = UINT_MAX;
12910 }
12911 }
12912 return def;
12913}
12914#endif
12915
12916#if ENABLE_HUSH_LOOPS
12917static int FAST_FUNC builtin_break(char **argv)
12918{
12919 unsigned depth;
12920 if (G.depth_of_loop == 0) {
12921 bb_error_msg("%s: only meaningful in a loop", argv[0]);
12922 /* if we came from builtin_continue(), need to undo "= 1" */
12923 G.flag_break_continue = 0;
12924 return EXIT_SUCCESS; /* bash compat */
12925 }
12926 G.flag_break_continue++; /* BC_BREAK = 1, or BC_CONTINUE = 2 */
12927
12928 G.depth_break_continue = depth = parse_numeric_argv1(argv, 1, 1);
12929 if (depth == UINT_MAX)
12930 G.flag_break_continue = BC_BREAK;
12931 if (G.depth_of_loop < depth)
12932 G.depth_break_continue = G.depth_of_loop;
12933
12934 return EXIT_SUCCESS;
12935}
12936
12937static int FAST_FUNC builtin_continue(char **argv)
12938{
12939 G.flag_break_continue = 1; /* BC_CONTINUE = 2 = 1+1 */
12940 return builtin_break(argv);
12941}
12942#endif
12943
12944#if ENABLE_HUSH_FUNCTIONS
12945static int FAST_FUNC builtin_return(char **argv)
12946{
12947 int rc;
12948
12949 if (G_flag_return_in_progress != -1) {
12950 bb_error_msg("%s: not in a function or sourced script", argv[0]);
12951 return EXIT_FAILURE; /* bash compat */
12952 }
12953
12954 G_flag_return_in_progress = 1;
12955
12956 /* bash:
12957 * out of range: wraps around at 256, does not error out
12958 * non-numeric param:
12959 * f() { false; return qwe; }; f; echo $?
12960 * bash: return: qwe: numeric argument required <== we do this
12961 * 255 <== we also do this
12962 */
12963 rc = parse_numeric_argv1(argv, G.last_exitcode, 0);
12964# if ENABLE_HUSH_TRAP
12965 if (argv[1]) { /* "return ARG" inside a running trap sets $? */
12966 debug_printf_exec("G.return_exitcode=%d\n", rc);
12967 G.return_exitcode = rc;
12968 }
12969# endif
12970 return rc;
12971}
12972#endif
12973
12974#if ENABLE_HUSH_TIMES
12975static int FAST_FUNC builtin_times(char **argv UNUSED_PARAM)
12976{
12977 static const uint8_t times_tbl[] ALIGN1 = {
12978 ' ', offsetof(struct tms, tms_utime),
12979 '\n', offsetof(struct tms, tms_stime),
12980 ' ', offsetof(struct tms, tms_cutime),
12981 '\n', offsetof(struct tms, tms_cstime),
12982 0
12983 };
12984 const uint8_t *p;
12985 unsigned clk_tck;
12986 struct tms buf;
12987
12988 clk_tck = bb_clk_tck();
12989
12990 times(&buf);
12991 p = times_tbl;
12992 do {
12993 unsigned sec, frac;
12994 unsigned long t;
12995 t = *(clock_t *)(((char *) &buf) + p[1]);
12996 sec = t / clk_tck;
12997 frac = t % clk_tck;
12998 printf("%um%u.%03us%c",
12999 sec / 60, sec % 60,
13000 (frac * 1000) / clk_tck,
13001 p[0]);
13002 p += 2;
13003 } while (*p);
13004
13005 return EXIT_SUCCESS;
13006}
13007#endif
13008
13009#if ENABLE_HUSH_MEMLEAK
13010static int FAST_FUNC builtin_memleak(char **argv UNUSED_PARAM)
13011{
13012 void *p;
13013 unsigned long l;
13014
13015# ifdef M_TRIM_THRESHOLD
13016 /* Optional. Reduces probability of false positives */
13017 malloc_trim(0);
13018# endif
13019 /* Crude attempt to find where "free memory" starts,
13020 * sans fragmentation. */
13021 p = malloc(240);
13022 l = (unsigned long)p;
13023 free(p);
13024 p = malloc(3400);
13025 if (l < (unsigned long)p) l = (unsigned long)p;
13026 free(p);
13027
Francis Laniel110b7692023-12-22 22:02:27 +010013028# if 0 /* debug */
13029 {
13030 struct mallinfo mi = mallinfo();
13031 printf("top alloc:0x%lx malloced:%d+%d=%d\n", l,
13032 mi.arena, mi.hblkhd, mi.arena + mi.hblkhd);
13033 }
13034# endif
13035
13036 if (!G.memleak_value)
13037 G.memleak_value = l;
13038
13039 l -= G.memleak_value;
13040 if ((long)l < 0)
13041 l = 0;
13042 l /= 1024;
13043 if (l > 127)
13044 l = 127;
13045
13046 /* Exitcode is "how many kilobytes we leaked since 1st call" */
13047 return l;
13048}
13049#endif
Francis Laniel36836fc2023-12-22 22:02:28 +010013050#endif /* !__U_BOOT__ */