aboutsummaryrefslogtreecommitdiff
path: root/shell/hush.c
diff options
context:
space:
mode:
Diffstat (limited to 'shell/hush.c')
-rw-r--r--shell/hush.c71
1 files changed, 34 insertions, 37 deletions
diff --git a/shell/hush.c b/shell/hush.c
index f6da826d3..309ed2139 100644
--- a/shell/hush.c
+++ b/shell/hush.c
@@ -101,18 +101,18 @@
101 * aaa 101 * aaa
102 */ 102 */
103//config:config HUSH 103//config:config HUSH
104//config: bool "hush" 104//config: bool "hush (64 kb)"
105//config: default y 105//config: default y
106//config: help 106//config: help
107//config: hush is a small shell (25k). It handles the normal flow control 107//config: hush is a small shell. It handles the normal flow control
108//config: constructs such as if/then/elif/else/fi, for/in/do/done, while loops, 108//config: constructs such as if/then/elif/else/fi, for/in/do/done, while loops,
109//config: case/esac. Redirections, here documents, $((arithmetic)) 109//config: case/esac. Redirections, here documents, $((arithmetic))
110//config: and functions are supported. 110//config: and functions are supported.
111//config: 111//config:
112//config: It will compile and work on no-mmu systems. 112//config: It will compile and work on no-mmu systems.
113//config: 113//config:
114//config: It does not handle select, aliases, tilde expansion, 114//config: It does not handle select, aliases, tilde expansion,
115//config: &>file and >&file redirection of stdout+stderr. 115//config: &>file and >&file redirection of stdout+stderr.
116//config: 116//config:
117//config:config HUSH_BASH_COMPAT 117//config:config HUSH_BASH_COMPAT
118//config: bool "bash-compatible extensions" 118//config: bool "bash-compatible extensions"
@@ -124,17 +124,17 @@
124//config: default y 124//config: default y
125//config: depends on HUSH_BASH_COMPAT 125//config: depends on HUSH_BASH_COMPAT
126//config: help 126//config: help
127//config: Enable {abc,def} extension. 127//config: Enable {abc,def} extension.
128//config: 128//config:
129//config:config HUSH_INTERACTIVE 129//config:config HUSH_INTERACTIVE
130//config: bool "Interactive mode" 130//config: bool "Interactive mode"
131//config: default y 131//config: default y
132//config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH 132//config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
133//config: help 133//config: help
134//config: Enable interactive mode (prompt and command editing). 134//config: Enable interactive mode (prompt and command editing).
135//config: Without this, hush simply reads and executes commands 135//config: Without this, hush simply reads and executes commands
136//config: from stdin just like a shell script from a file. 136//config: from stdin just like a shell script from a file.
137//config: No prompt, no PS1/PS2 magic shell variables. 137//config: No prompt, no PS1/PS2 magic shell variables.
138//config: 138//config:
139//config:config HUSH_SAVEHISTORY 139//config:config HUSH_SAVEHISTORY
140//config: bool "Save command history to .hush_history" 140//config: bool "Save command history to .hush_history"
@@ -146,18 +146,18 @@
146//config: default y 146//config: default y
147//config: depends on HUSH_INTERACTIVE 147//config: depends on HUSH_INTERACTIVE
148//config: help 148//config: help
149//config: Enable job control: Ctrl-Z backgrounds, Ctrl-C interrupts current 149//config: Enable job control: Ctrl-Z backgrounds, Ctrl-C interrupts current
150//config: command (not entire shell), fg/bg builtins work. Without this option, 150//config: command (not entire shell), fg/bg builtins work. Without this option,
151//config: "cmd &" still works by simply spawning a process and immediately 151//config: "cmd &" still works by simply spawning a process and immediately
152//config: prompting for next command (or executing next command in a script), 152//config: prompting for next command (or executing next command in a script),
153//config: but no separate process group is formed. 153//config: but no separate process group is formed.
154//config: 154//config:
155//config:config HUSH_TICK 155//config:config HUSH_TICK
156//config: bool "Support process substitution" 156//config: bool "Support process substitution"
157//config: default y 157//config: default y
158//config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH 158//config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
159//config: help 159//config: help
160//config: Enable `command` and $(command). 160//config: Enable `command` and $(command).
161//config: 161//config:
162//config:config HUSH_IF 162//config:config HUSH_IF
163//config: bool "Support if/then/elif/else/fi" 163//config: bool "Support if/then/elif/else/fi"
@@ -174,37 +174,37 @@
174//config: default y 174//config: default y
175//config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH 175//config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
176//config: help 176//config: help
177//config: Enable case ... esac statement. +400 bytes. 177//config: Enable case ... esac statement. +400 bytes.
178//config: 178//config:
179//config:config HUSH_FUNCTIONS 179//config:config HUSH_FUNCTIONS
180//config: bool "Support funcname() { commands; } syntax" 180//config: bool "Support funcname() { commands; } syntax"
181//config: default y 181//config: default y
182//config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH 182//config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
183//config: help 183//config: help
184//config: Enable support for shell functions. +800 bytes. 184//config: Enable support for shell functions. +800 bytes.
185//config: 185//config:
186//config:config HUSH_LOCAL 186//config:config HUSH_LOCAL
187//config: bool "local builtin" 187//config: bool "local builtin"
188//config: default y 188//config: default y
189//config: depends on HUSH_FUNCTIONS 189//config: depends on HUSH_FUNCTIONS
190//config: help 190//config: help
191//config: Enable support for local variables in functions. 191//config: Enable support for local variables in functions.
192//config: 192//config:
193//config:config HUSH_RANDOM_SUPPORT 193//config:config HUSH_RANDOM_SUPPORT
194//config: bool "Pseudorandom generator and $RANDOM variable" 194//config: bool "Pseudorandom generator and $RANDOM variable"
195//config: default y 195//config: default y
196//config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH 196//config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
197//config: help 197//config: help
198//config: Enable pseudorandom generator and dynamic variable "$RANDOM". 198//config: Enable pseudorandom generator and dynamic variable "$RANDOM".
199//config: Each read of "$RANDOM" will generate a new pseudorandom value. 199//config: Each read of "$RANDOM" will generate a new pseudorandom value.
200//config: 200//config:
201//config:config HUSH_MODE_X 201//config:config HUSH_MODE_X
202//config: bool "Support 'hush -x' option and 'set -x' command" 202//config: bool "Support 'hush -x' option and 'set -x' command"
203//config: default y 203//config: default y
204//config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH 204//config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
205//config: help 205//config: help
206//config: This instructs hush to print commands before execution. 206//config: This instructs hush to print commands before execution.
207//config: Adds ~300 bytes. 207//config: Adds ~300 bytes.
208//config: 208//config:
209//config:config HUSH_ECHO 209//config:config HUSH_ECHO
210//config: bool "echo builtin" 210//config: bool "echo builtin"
@@ -236,14 +236,14 @@
236//config: default y 236//config: default y
237//config: depends on HUSH_EXPORT 237//config: depends on HUSH_EXPORT
238//config: help 238//config: help
239//config: export -n unexports variables. It is a bash extension. 239//config: export -n unexports variables. It is a bash extension.
240//config: 240//config:
241//config:config HUSH_READONLY 241//config:config HUSH_READONLY
242//config: bool "readonly builtin" 242//config: bool "readonly builtin"
243//config: default y 243//config: default y
244//config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH 244//config: depends on HUSH || SH_IS_HUSH || BASH_IS_HUSH
245//config: help 245//config: help
246//config: Enable support for read-only variables. 246//config: Enable support for read-only variables.
247//config: 247//config:
248//config:config HUSH_KILL 248//config:config HUSH_KILL
249//config: bool "kill builtin (supports kill %jobspec)" 249//config: bool "kill builtin (supports kill %jobspec)"
@@ -1068,7 +1068,7 @@ static const struct built_in_command bltins1[] = {
1068 BLTIN("export" , builtin_export , "Set environment variables"), 1068 BLTIN("export" , builtin_export , "Set environment variables"),
1069#endif 1069#endif
1070#if ENABLE_HUSH_JOB 1070#if ENABLE_HUSH_JOB
1071 BLTIN("fg" , builtin_fg_bg , "Bring job into foreground"), 1071 BLTIN("fg" , builtin_fg_bg , "Bring job to foreground"),
1072#endif 1072#endif
1073#if ENABLE_HUSH_HELP 1073#if ENABLE_HUSH_HELP
1074 BLTIN("help" , builtin_help , NULL), 1074 BLTIN("help" , builtin_help , NULL),
@@ -1121,7 +1121,7 @@ static const struct built_in_command bltins1[] = {
1121 BLTIN("unset" , builtin_unset , "Unset variables"), 1121 BLTIN("unset" , builtin_unset , "Unset variables"),
1122#endif 1122#endif
1123#if ENABLE_HUSH_WAIT 1123#if ENABLE_HUSH_WAIT
1124 BLTIN("wait" , builtin_wait , "Wait for process"), 1124 BLTIN("wait" , builtin_wait , "Wait for process to finish"),
1125#endif 1125#endif
1126}; 1126};
1127/* These builtins won't be used if we are on NOMMU and need to re-exec 1127/* These builtins won't be used if we are on NOMMU and need to re-exec
@@ -2662,9 +2662,8 @@ static void o_delchr(o_string *o)
2662static void o_addblock(o_string *o, const char *str, int len) 2662static void o_addblock(o_string *o, const char *str, int len)
2663{ 2663{
2664 o_grow_by(o, len); 2664 o_grow_by(o, len);
2665 memcpy(&o->data[o->length], str, len); 2665 ((char*)mempcpy(&o->data[o->length], str, len))[0] = '\0';
2666 o->length += len; 2666 o->length += len;
2667 o->data[o->length] = '\0';
2668} 2667}
2669 2668
2670static void o_addstr(o_string *o, const char *str) 2669static void o_addstr(o_string *o, const char *str)
@@ -5519,17 +5518,15 @@ static char *replace_pattern(char *val, const char *pattern, const char *repl, c
5519 break; 5518 break;
5520 5519
5521 result = xrealloc(result, res_len + (s - val) + repl_len + 1); 5520 result = xrealloc(result, res_len + (s - val) + repl_len + 1);
5522 memcpy(result + res_len, val, s - val); 5521 strcpy(mempcpy(result + res_len, val, s - val), repl);
5523 res_len += s - val; 5522 res_len += (s - val) + repl_len;
5524 strcpy(result + res_len, repl);
5525 res_len += repl_len;
5526 debug_printf_varexp("val:'%s' s:'%s' result:'%s'\n", val, s, result); 5523 debug_printf_varexp("val:'%s' s:'%s' result:'%s'\n", val, s, result);
5527 5524
5528 val = s + size; 5525 val = s + size;
5529 if (exp_op == '/') 5526 if (exp_op == '/')
5530 break; 5527 break;
5531 } 5528 }
5532 if (val[0] && result) { 5529 if (*val && result) {
5533 result = xrealloc(result, res_len + strlen(val) + 1); 5530 result = xrealloc(result, res_len + strlen(val) + 1);
5534 strcpy(result + res_len, val); 5531 strcpy(result + res_len, val);
5535 debug_printf_varexp("val:'%s' result:'%s'\n", val, result); 5532 debug_printf_varexp("val:'%s' result:'%s'\n", val, result);