Commit Graph

2439 Commits

Author SHA1 Message Date
Denys Vlasenko
58eb805c2c ash: parser: Fix parsing of ${}
Upstream commit:

    Date: Tue, 3 Apr 2018 00:40:25 +0800
    parser: Fix parsing of ${}

    dash -c 'echo ${}' should print "Bad subtitution" but instead
    fails with "Syntax error: Missing '}'".  This is caused by us
    reading an extra character beyond the right brace.  This patch
    fixes it so that this construct only fails during expansion rather
    than during parsing.

    Fixes: 3df3edd13389 ("[PARSER] Report substition errors at...")
    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

function                                             old     new   delta
readtoken1                                          2907    2916      +9

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-08-05 18:11:15 +02:00
Denys Vlasenko
19358cc313 ash,hush: fold shell_builtin_read() way-too-many params into a struct param
function                                             old     new   delta
getoptscmd                                           587     584      -3
readcmd                                              240     224     -16
shell_builtin_read                                  1426    1399     -27
builtin_read                                         210     182     -28
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/4 up/down: 0/-74)             Total: -74 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-08-05 18:11:15 +02:00
Denys Vlasenko
fd6f295a98 hush: set IFS to default on startup
function                                             old     new   delta
hush_main                                           1095    1110     +15

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-08-05 18:11:15 +02:00
Denys Vlasenko
e9dccab9f4 hush: fix fallout from FILE->HFILE conversion
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-08-05 18:11:15 +02:00
Denys Vlasenko
440da97ed7 ash: expand: Fix ghost fields with unquoted $@/$*
Upstream commit:

    Date: Fri, 23 Mar 2018 18:58:47 +0800
    expand: Fix ghost fields with unquoted $@/$*

    You're right.  The proper fix to this is to ensure that nulonly
    is not set in varvalue for $*.  It should only be set for $@ when
    it's inside double quotes.

    In fact there is another bug while we're playing with $@/$*.
    When IFS is set to a non-whitespace character such as :, $*
    outside quotes won't remove empty fields as it should.

    This patch fixes both problems.

    Reported-by: Martijn Dekker <martijn@inlv.org>
    Suggested-by: Harald van Dijk <harald@gigawatt.nl>
    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

function                                             old     new   delta
argstr                                              1111    1113      +2
evalvar                                              571     569      -2
varvalue                                             579     576      -3
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/2 up/down: 2/-5)               Total: -3 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-08-05 14:29:58 +02:00
Denys Vlasenko
67dae152f4 ash: var: Set IFS to fixed value at start time
Upstream commit:

    Date: Sat, 19 May 2018 02:39:43 +0800
    var: Set IFS to fixed value at start time

    This patch forces the IFS variable to always be set to its default
    value, regardless of the environment.

    It also removes the long unused IFS_BROKEN code.

    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-08-05 13:59:35 +02:00
Denys Vlasenko
9abf53beb4 ash: eval: Variable assignments on functions are no longer persistent
Upstream commit:

    Date: Wed, 4 Apr 2018 17:54:01 +0800
    eval: Variable assignments on functions are no longer persistent

    Dirk Fieldhouse <fieldhouse@gmx.net> wrote:
    > In POSIX.1-2017 ("simultaneously IEEE Std 1003.1™-2017 and The Open
    > Group Technical Standard Base Specifications, Issue 7")
    > <http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_09>,
    > we read under '2.9.1 Simple Commands'
    >
    > "Variable assignments shall be performed as follows:
    > ...
    > -    If the command name is a standard utility implemented as a function
    > (see XBD Utility), the effect of variable assignments shall be as if the
    > utility was not implemented as a function.
    > ...
    > -    If the command name is a function that is not a standard utility
    > implemented as a function, variable assignments shall affect the current
    > execution environment during the execution of the function. It is
    > unspecified:
    >
    >     *   Whether or not the variable assignments persist after the
    > completion of the function
    >
    >     *   Whether or not the variables gain the export attribute during
    > the execution of the function
    >
    >     *   Whether or not export attributes gained as a result of the
    > variable assignments persist after the completion of the function (if
    > variable assignments persist after the completion of the function)"

    POSIX used to require the current dash behaviour.  However, you're
    right that this is no longer the case.

    This patch will remove the persistence of the variable assignment.

    I have considered the exporting the variables during the function
    execution but have decided against it because:

    1) It makes the code bigger.
    2) dash has never done this in the past.
    3) You cannot use this portably anyway.

    Reported-by: Dirk Fieldhouse <fieldhouse@gmx.net>
    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

function                                             old     new   delta
evalcommand                                         1606    1635     +29
evalcase                                             313     317      +4
evalfun                                              280     268     -12
pushlocalvars                                         48       -     -48
------------------------------------------------------------------------------
(add/remove: 0/1 grow/shrink: 2/1 up/down: 33/-60)            Total: -27 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-08-05 11:14:11 +02:00
Denys Vlasenko
c2aa218f23 ash,hush: properly handle ${v//pattern/repl} if pattern starts with /
Closes 2695

function                                             old     new   delta
parse_dollar                                         762     790     +28
subevalvar                                          1258    1267      +9
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 37/0)               Total: 37 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-08-04 22:25:28 +02:00
Denys Vlasenko
54fdabda3b hush: speed up ${var:+ARG} for literal ARGs
function                                             old     new   delta
first_special_char_in_vararg                           -      52     +52
expand_one_var                                      2248    2296     +48
encode_then_expand_vararg                            357     336     -21
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 1/1 up/down: 100/-21)            Total: 79 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-31 10:36:29 +02:00
Denys Vlasenko
a5db1d7354 hush: fix another case where empty "for" wasn't setting exitcode to 0
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-28 12:42:08 +02:00
Denys Vlasenko
a8e7441176 hush: disable debug_indent increment/decrement for HUSH_DEBUG < 2 builds
function                                             old     new   delta
run_list                                            1063    1046     -17
parse_stream                                        2296    2249     -47
run_pipe                                            1890    1840     -50
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/3 up/down: 0/-114)           Total: -114 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-28 12:16:30 +02:00
Denys Vlasenko
aa449c927d hush: make "set -x" output don't redirectable when fd#2 redirected
function                                             old     new   delta
x_mode_print_optionally_squoted                        -     120    +120
x_mode_flush                                           -      68     +68
save_fd_on_redirect                                  208     243     +35
x_mode_prefix                                          -      27     +27
x_mode_addblock                                        -      23     +23
x_mode_addchr                                          -      17     +17
dump_cmd_in_x_mode                                   110      85     -25
run_pipe                                            1919    1890     -29
print_optionally_squoted                             145       -    -145
------------------------------------------------------------------------------
(add/remove: 5/1 grow/shrink: 1/2 up/down: 290/-199)           Total: 91 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-28 12:13:58 +02:00
Denys Vlasenko
4b70c926bc hush: make "set -x" output closer to bash
function                                             old     new   delta
print_optionally_squoted                               -     145    +145
run_pipe                                            1902    1919     +17
dump_cmd_in_x_mode                                   142     110     -32
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 1/1 up/down: 162/-32)           Total: 130 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-27 17:43:39 +02:00
Denys Vlasenko
9dda9270df hush: fix "set -x" output prefix overlapping for v="..cmd.." case
Was printing initial "+" prefix for the assignment, that printing

	"+ cmd"

then printing the expanded " v=VAL" string.

Delay printing of "+" prefix for the assignment to after expansion.

function                                             old     new   delta
run_pipe                                            1883    1902     +19
builtin_eval                                         127     133      +6
expand_vars_to_list                                 1103    1106      +3
dump_cmd_in_x_mode                                   144     142      -2
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/1 up/down: 28/-2)              Total: 26 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-27 14:12:05 +02:00
Denys Vlasenko
186cf49767 hush: in some cases, expand_on_ifs() relied of uninitialized memory
The n > 0 check to prevent access to the last byte of non-existing argv[-1]
wasn't enough. Switched to making sure there are initialized (zero) bytes there.

A predictable testcase is rather hard to construct, unfortunately,
contents of memory depends on allocator behavior and whatnot.

function                                             old     new   delta
o_save_ptr_helper                                    119     137     +18
expand_on_ifs                                        345     339      -6
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 18/-6)              Total: 12 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-27 12:14:39 +02:00
Denys Vlasenko
7c5f18a3ba hush: improve set -x: make "+++" indent level increase in cmd and eval.
function                                             old     new   delta
dump_cmd_in_x_mode                                   126     144     +18
run_pipe                                            1873    1883     +10
builtin_eval                                         119     127      +8
expand_vars_to_list                                 1100    1103      +3
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 4/0 up/down: 39/0)               Total: 39 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-26 15:21:50 +02:00
Denys Vlasenko
945e9b05c9 hush: fix/explain corner cases of redirection colliding with script fd
function                                             old     new   delta
save_fd_on_redirect                                  200     208      +8
run_pipe                                            1870    1873      +3
setup_redirects                                      321     322      +1
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/0 up/down: 12/0)               Total: 12 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-24 18:01:22 +02:00
Denys Vlasenko
63c42afaa4 hush: add "heredoc.tests" from ash, tweak ash "is a function" message
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-24 17:10:18 +02:00
Denys Vlasenko
41ef41b3e0 hush: fix nested redirects colliding with script fds
This necessitates switch from libc FILE api to a simple
homegrown replacement.
The change which fixes the bug here is the deleting of

	restore_redirected_FILEs();

line. It was prematurely moving (restoring) script fd#3.
The fix is: we don't even _want_ to restore scrit fds,
we are perfectly fine with them being moved.
The only reason we tried to restore them is that FILE api
did not allow moving of FILE->fd.

function                                             old     new   delta
refill_HFILE_and_getc                                  -      93     +93
hfopen                                                 -      90     +90
hfclose                                                -      66     +66
pseudo_exec_argv                                     591     597      +6
hush_main                                           1089    1095      +6
builtin_source                                       209     214      +5
save_fd_on_redirect                                  197     200      +3
setup_redirects                                      320     321      +1
fgetc_interactive                                    235     236      +1
i_peek_and_eat_bkslash_nl                             99      97      -2
expand_vars_to_list                                 1103    1100      -3
restore_redirects                                     99      52     -47
fclose_and_forget                                     57       -     -57
remember_FILE                                         63       -     -63
------------------------------------------------------------------------------
(add/remove: 3/2 grow/shrink: 6/3 up/down: 271/-172)           Total: 99 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-24 16:54:41 +02:00
Denys Vlasenko
dfc7394763 hush: handle backslash-newline in heredoc terminators
function                                             old     new   delta
fetch_heredocs                                       479     527     +48

(ash fails this test)

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-24 14:03:18 +02:00
Denys Vlasenko
474cb20555 hush: fix handling of heredocs not enclosed in groups where they are "declared"
function                                             old     new   delta
fetch_heredocs                                         -     479    +479
parse_and_run_stream                                 146     148      +2
parse_stream                                        2787    2296    -491
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 1/1 up/down: 481/-491)          Total: -10 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-24 13:03:03 +02:00
Denys Vlasenko
3675c37b9b hush: fix heredoc handling in the "cmd <<EOF ;<newline>" case
function                                             old     new   delta
parse_stream                                        2759    2787     +28

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-23 16:31:21 +02:00
Denys Vlasenko
d73cdbf84c hush: fix handling of heredocs starting with empty lines
function                                             old     new   delta
parse_stream                                        2748    2759     +11

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-23 15:43:57 +02:00
Denys Vlasenko
f36caa4071 hush: never glob result of dquoted "${v:+/bin/c*}"
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-20 19:29:41 +02:00
Denys Vlasenko
4c3c8a1a61 hush: tidy up code after previous commits
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-20 19:11:09 +02:00
Denys Vlasenko
1856740ec0 hush: better names for o_free_unsafe() / o_free(), no logic changes
o_free() made o_string NULL-initialized,
o_free_unsafe() did not bother reinitializing (expected caller to not need it).

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-20 17:51:31 +02:00
Denys Vlasenko
83e434d5b5 hush: fix handling of '' in ${var:+ARG}
This wasn't an ash bug in dollar_altvalue9, it was hush bug (and bash!)

function                                             old     new   delta
expand_one_var                                      2236    2254     +18
expand_vars_to_list                                 1097    1103      +6
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 24/0)               Total: 24 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-20 17:36:06 +02:00
Denys Vlasenko
294eb4612c hush: fix word splitting in ${v:+ARG} - dollar_altvalue1 test
ash might be a bit buggy, need to investigate dollar_altvalue9 test

function                                             old     new   delta
expand_one_var                                      1639    2236    +597
expand_variables                                     112     128     +16
expand_vars_to_list                                 1117    1097     -20
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/1 up/down: 613/-20)           Total: 593 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-20 16:29:43 +02:00
Denys Vlasenko
57235beb69 hush: expand_vars_to_list() should not assume it starts new word
function                                             old     new   delta
expand_variables                                     112     115      +3
expand_vars_to_list                                 1117    1108      -9
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 3/-9)               Total: -6 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-20 16:27:26 +02:00
Denys Vlasenko
18e8b61292 hush: remove pointless "next" var, simplify expand_vars_to_list()
function                                             old     new   delta
o_addstr                                               -      26     +26
expand_vars_to_list                                 1112    1117      +5
encode_then_expand_vararg                            398     382     -16
parse_dollar                                         779     762     -17
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 1/2 up/down: 31/-33)             Total: -2 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-20 16:27:26 +02:00
Denys Vlasenko
168579a34c hush: store "ended_in_ifs" flag in o_string
This simplifies function parameter passing.

function                                             old     new   delta
expand_one_var                                      1643    1639      -4
append_str_maybe_ifs_split                            64      52     -12
expand_vars_to_list                                 1125    1112     -13
expand_on_ifs                                        361     345     -16
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/4 up/down: 0/-45)             Total: -45 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-20 16:27:26 +02:00
Denys Vlasenko
8a6a461504 hush: propagate (output,n) parameters into expand_one_var()
This is necessary since expand_one_var() for ${var:+ARG} must create more than one
output word, and thus can't simply return a char*.

function                                             old     new   delta
expand_one_var                                      1610    1643     +33
expand_vars_to_list                                 1139    1125     -14
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 33/-14)             Total: 19 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-20 16:27:26 +02:00
Denys Vlasenko
116b50a5c1 hush: make expand_vars_to_list() a bit more sane
function                                             old     new   delta
append_str_maybe_ifs_split                             -      64     +64
expand_vars_to_list                                 1167    1139     -28
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 0/1 up/down: 64/-28)             Total: 36 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-20 16:27:26 +02:00
Denys Vlasenko
e36a5894bd hush: reduce indentation, no code changes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-18 16:12:23 +02:00
Denys Vlasenko
2e71101e31 hush: fix 'x=; echo ${x:-"$@"}' producing 'BUG in varexp2' message
function                                             old     new   delta
expand_string_to_string                              126     128      +2

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-18 16:02:25 +02:00
Denys Vlasenko
8b08d5a502 hush: smaller code to set o_string to ""
function                                             old     new   delta
encode_then_expand_vararg                            399     398      -1
parse_stream                                        2753    2748      -5

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-18 15:50:04 +02:00
Denys Vlasenko
4c201c00a3 whitespace fixes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-17 15:04:17 +02:00
Denys Vlasenko
0d2e0de42b hush: faster/smaller code to check for presense of multiple chars in string
Go over the string only once.

function                                             old     new   delta
encode_then_expand_string                            126     105     -21
encode_then_expand_vararg                            443     399     -44
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-65)             Total: -65 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-17 14:33:19 +02:00
Denys Vlasenko
b762c784ca hush: improve ${var#...}, ${var:+...} and ${var/.../...} - handle quoting
dollar_altvalue1 test partially fails: word splitting of unquoted ${var:+...}
is not correct

function                                             old     new   delta
encode_then_expand_vararg                              -     443    +443
expand_one_var                                      1599    1610     +11
parse_stream                                        2756    2753      -3
encode_string                                        250     242      -8
setup_heredoc                                        308     298     -10
expand_and_evaluate_arith                            106      96     -10
encode_then_expand_string                            142     126     -16
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 1/5 up/down: 454/-47)           Total: 407 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-17 14:21:38 +02:00
Denys Vlasenko
b0441a7189 hush: shrink code in builtin_eval
function                                             old     new   delta
builtin_eval                                         126     119      -7

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-07-15 18:03:56 +02:00
Denys Vlasenko
35a017c0c5 hush: unset_local_var_len is only used by unset_local_var
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-06-26 18:27:54 +02:00
Denys Vlasenko
b2b14cbd4c hush: fix compile problem found by randomconfig
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-06-26 18:09:22 +02:00
Denys Vlasenko
d1a83234c0 hush: fix dup_CLOEXEC() call without "avoid_fd" parameter
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-06-26 15:50:33 +02:00
Denys Vlasenko
c96bb2c5ab hush: fix for !ENABLE_HUSH_MODE_X configuration
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-06-26 15:43:56 +02:00
Denys Vlasenko
99496dc716 hush: variable nesting code is used also if HUSH_FUNCTIONS is not enabled
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-06-26 15:36:58 +02:00
Denys Vlasenko
817a20296f randomconfig fixes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-06-26 15:35:17 +02:00
Kartik Agaram
43b17b1cd0 restore documentation on the build config language
Kconfig-language.txt was deleted in commit 4fa499a17b back in 2006.
Move to docs/ as suggested by Xabier Oneca:
  http://lists.busybox.net/pipermail/busybox/2014-May/080914.html
Also update references to it everywhere.

Signed-off-by: Kartik Agaram <akkartik@gmail.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-06-06 15:16:48 +02:00
Denys Vlasenko
d5f5045b43 ash: expand: Fix buffer overflow in expandmeta
Upstream commit:

    Date: Sun, 25 Mar 2018 16:38:00 +0800
    expand: Fix buffer overflow in expandmeta

    The native version of expandmeta allocates a buffer that may be
    overrun for two reasons.  First of all the size is 1 byte too small
    but this is normally hidden because the minimum size is rounded
    up to 2048 bytes.  Secondly, if the directory level is deep enough,
    any buffer can be overrun.

    This patch fixes both problems by calling realloc when necessary.

    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

function                                             old     new   delta
expmeta                                              517     635    +118
expandarg                                            990     996      +6
mklocal                                              288     290      +2
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/0 up/down: 126/0)             Total: 126 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-14 14:50:47 +02:00
Denys Vlasenko
46158dc833 shell: add 6856 $IFS tests to testsuites
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-11 20:24:58 +02:00
Denys Vlasenko
f693b606b7 hush: fix recent breakage from parse_stream() changes
function                                             old     new   delta
parse_stream                                        3808    3821     +13

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-11 20:00:43 +02:00
Denys Vlasenko
44257ad5d0 hush: fix IFS handling in read
$ echo "X:Y:" | (IFS=": " read x y; echo "|$x|$y|")
|X|Y|
$ echo "X:Y  :  " | (IFS=": " read x y; echo "|$x|$y|")
|X|Y|

function                                             old     new   delta
shell_builtin_read                                  1320    1426    +106

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-11 17:18:34 +02:00
Denys Vlasenko
9678636911 hush: IFS fixes
$ IFS=": "; x=" "; set x $x; for v; do echo "|$v|"; done
|x|
$ IFS=": "; x=":"; set x $x; for v; do echo "|$v|"; done
|x|
||

function                                             old     new   delta
run_pipe                                            1789    1870     +81
expand_on_ifs                                        310     361     +51
pseudo_exec_argv                                     588     591      +3
builtin_local                                         50      53      +3
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 4/0 up/down: 138/0)             Total: 138 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-11 16:02:58 +02:00
Denys Vlasenko
34179956f9 hush: fix "$v" expansion in case patterns when v='[a]'
function                                             old     new   delta
run_list                                            1053    1063     +10
setup_redirects                                      311     320      +9
encode_then_expand_string                            135     142      +7
run_pipe                                            1784    1789      +5
expand_assignments                                    81      86      +5
expand_string_to_string                              124     125      +1
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 6/0 up/down: 37/0)               Total: 37 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-11 13:47:59 +02:00
Denys Vlasenko
680c3016a2 ash: parser: Allow newlines within parameter substitution
Upstream commit:

Date: Thu, 22 Mar 2018 21:41:24 +0800
parser: Allow newlines within parameter substitution

    On Fri, Mar 16, 2018 at 11:27:22AM +0800, Herbert Xu wrote:
    > On Thu, Mar 15, 2018 at 10:49:15PM +0100, Harald van Dijk wrote:
    > >
    > > Okay, it can be trivially modified to something that does work in other
    > > shells (even if it were actually executed), but gets rejected at parse time
    > > by dash:
    > >
    > >   if false; then
    > >     : ${$+
    > >   }
    > >   fi
    >
    > That's just a bug in dash's parser with ${} in general, because
    > it bombs out without the if clause too:
    >
    > 	: ${$+
    > 	}

    This patch fixes the parsing of newlines with parameter substitution.

    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-11 12:39:18 +02:00
Denys Vlasenko
0403bedccc hush: optimize parse_stream()
Since we check for '\' anyway when we determine whether we can look ahead,
we can just check for *and handle* it there.

function                                             old     new   delta
parse_stream                                        2751    2740     -11

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-11 01:34:46 +02:00
Denys Vlasenko
89e9d5534d hush: do not drop backslash from eval 'echo ok\'
newer bash does not drop it, most other shells too

function                                             old     new   delta
unbackslash                                           39      57     +18
parse_stream                                        2753    2751      -2
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 18/-2)              Total: 16 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-11 01:15:33 +02:00
Denys Vlasenko
3632cb15f1 shell: add comments about [[, no code changes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-10 15:25:41 +02:00
Denys Vlasenko
4709df0f15 hush: fix handling of \<eof> in double-quoted strings
function                                             old     new   delta
encode_string                                        268     250     -18

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-10 14:49:01 +02:00
Denys Vlasenko
bcf56114fa hush: fix eval 'echo ok\'
function                                             old     new   delta
parse_stream                                        2762    2753      -9

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-10 14:40:23 +02:00
Denys Vlasenko
92a930b4e8 hush: simplify \<newline> code, part 3
function                                             old     new   delta
parse_stream                                        2780    2762     -18

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-10 14:27:08 +02:00
Denys Vlasenko
e8b1bc0481 hush: simplify \<newline> code, part 2
function                                             old     new   delta
parse_stream                                        2787    2780      -7

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-10 14:23:49 +02:00
Denys Vlasenko
1c57269b5d hush: simplify \<newline> code, part 1
function                                             old     new   delta
parse_stream                                        2919    2787    -132

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-10 14:23:49 +02:00
Denys Vlasenko
09b7a7ec0e hush: put "current word" structure into parsing context
function                                             old     new   delta
done_word                                            790     767     -23
parse_stream                                        3018    2919     -99
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-122)           Total: -122 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-10 03:22:10 +02:00
Denys Vlasenko
e93031e6dc ash: if "[[" bashism is not supported, do not handle it anywhere
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-10 01:23:19 +02:00
Denys Vlasenko
57b7efb0d5 ash: trivial code shrink
function                                             old     new   delta
parse_command                                       1677    1674      -3

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-10 01:20:26 +02:00
Denys Vlasenko
9db344a0f4 hush: fix var_leaks.tests and var_preserved.tests on NOMMU
function                                             old     new   delta
remove_nested_vars                                     -      77     +77
run_pipe                                            1756    1786     +30
pseudo_exec_argv                                     376     379      +3
leave_var_nest_level                                  98      32     -66
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 2/1 up/down: 110/-66)            Total: 44 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-09 19:05:11 +02:00
Denys Vlasenko
eb0de05d68 hush: fix func_return2.tests on NOMMU
function                                             old     new   delta
hush_main                                           1714    1718      +4

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-09 17:54:07 +02:00
Denys Vlasenko
38ccd6af8a bzip2: fix two crashes on corrupted archives
As it turns out, longjmp'ing into freed stack is not healthy...

function                                             old     new   delta
unpack_usage_messages                                  -      97     +97
unpack_bz2_stream                                    369     409     +40
get_next_block                                      1667    1677     +10
get_bits                                             156     155      -1
start_bunzip                                         212     183     -29
bb_show_usage                                        181     120     -61
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 2/3 up/down: 147/-91)            Total: 56 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-08 20:05:04 +02:00
Denys Vlasenko
7bcde5f00d libbb.h: always include sys/resource.h
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-08 17:23:27 +02:00
Denys Vlasenko
8d6eab3225 hush: fix prompt in multi-line $(())
Now shows PS2 in this case:

/path/to/dir $ a=b; echo $((
> _

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-07 17:01:31 +02:00
Denys Vlasenko
f5018dac21 hush: fix "unset PS1/PS2", and put them into initial variable set
"unset PS1/PS2" causes prompts to be empty strings

function                                             old     new   delta
hush_main                                           1031    1089     +58
goto_new_line                                         27      33      +6
fgetc_interactive                                    244     245      +1
unset_local_var                                      155     149      -6
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/1 up/down: 65/-6)              Total: 59 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-06 17:58:21 +02:00
Denys Vlasenko
00bd76728d hush: if we did match "LINENO" or "OPTIND", stop further comparisons
function                                             old     new   delta
handle_changed_special_names                          99     101      +2

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-06 14:57:53 +02:00
Denys Vlasenko
cf079ffe1c hush: consolidate handling of setting/unsetting of PSn, LINENO, OPTIND
function                                             old     new   delta
handle_changed_special_names                           -      99     +99
unset_local_var                                      256     155    -101
set_local_var                                        557     437    -120
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 0/2 up/down: 99/-221)          Total: -122 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-06 14:50:12 +02:00
Denys Vlasenko
de02625985 hush: update to correctly handle changed var_bash[346].tests
function                                             old     new   delta
expand_one_var                                      1612    1604      -8

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-05 17:04:53 +02:00
Denys Vlasenko
f2ed39b930 hush: implement "hush -s"
function                                             old     new   delta
hush_main                                           1015    1031     +16
packed_usage                                       32757   32745     -12
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 16/-12)              Total: 4 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-05 16:46:49 +02:00
Denys Vlasenko
21b7f1b6b6 hush: fix a few more corner cases with empty-expanding cmds
See added testcases

function                                             old     new   delta
run_pipe                                            1723    1784     +61

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-05 15:15:53 +02:00
Denys Vlasenko
41d8f10813 hush: fix corner cases with exec in empty expansions
Cases like these:

var=val exec >redir

var=val `` >redir

function                                             old     new   delta
run_pipe                                            1701    1723     +22
redirect_and_varexp_helper                            56      55      -1
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 22/-1)              Total: 21 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-05 14:41:21 +02:00
Denys Vlasenko
929a41d577 hush: less mind-bending set_vars_and_save_old()
function                                             old     new   delta
run_pipe                                            1651    1701     +50
set_local_var                                        510     557     +47
pseudo_exec_argv                                     544     581     +37
redirect_and_varexp_helper                            64      56      -8
set_vars_and_save_old                                164     149     -15
unset_local_var                                      274     256     -18
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/3 up/down: 134/-41)            Total: 93 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-05 14:09:14 +02:00
Denys Vlasenko
4e1dc539e9 hush: "no logic changes" in last commit was not true, fix it up
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-05 13:10:34 +02:00
Denys Vlasenko
34f6b12330 hush: make run_pipe code simpler to understand, no logic changes
function                                             old     new   delta
run_pipe                                            1641    1651     +10

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-05 11:30:17 +02:00
Denys Vlasenko
d358b0b65d hush: fix a bug where we don't properly handle f() { a=A; b=B; }; a= f
function                                             old     new   delta
unset_local_var                                       20     274    +254
leave_var_nest_level                                   -      98     +98
set_vars_and_save_old                                128     164     +36
enter_var_nest_level                                   -      32     +32
builtin_local                                         46      50      +4
pseudo_exec_argv                                     554     544     -10
redirect_and_varexp_helper                            77      64     -13
run_pipe                                            1890    1641    -249
unset_local_var_len                                  267       -    -267
------------------------------------------------------------------------------
(add/remove: 2/1 grow/shrink: 3/3 up/down: 424/-539)         Total: -115 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-05 00:51:55 +02:00
Denys Vlasenko
332e4115c9 hush: make var nesting code independent of "local" support
Also, add code to abort at ~65000 function recursion depth.
SEGVing is not as nice as exiting with a message (and restoring termios!):

$ f() { echo -n .; f; }; f
....<many dots later>....hush: fatal recursion (depth 65281)

function                                             old     new   delta
run_pipe                                            1826    1890     +64
pseudo_exec_argv                                     544     554     +10
parse_and_run_file                                    71      80      +9
i_getch                                              104     107      +3
done_command                                          99     102      +3
set_local_var                                        508     510      +2
helper_export_local                                  214     215      +1
builtin_local                                         49      46      -3
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 7/1 up/down: 92/-3)              Total: 89 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-04 22:35:13 +02:00
Denys Vlasenko
61407807ab hush: fix for readonly vars in "ro=A ro=B cmd" case
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-04 21:14:28 +02:00
Denys Vlasenko
ee1fd1246e ash: unbreak PS1 parsing after "ash: parser: Add syntax stack..." commit
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-04 13:59:53 +02:00
Denys Vlasenko
fbf44854a3 hush: support "f() (cmd)" functions
Many other shells support this construct

function                                             old     new   delta
parse_stream                                        2950    3018     +68

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-03 14:56:52 +02:00
Denys Vlasenko
49015a60cb hush: fix mishandling of "true | f() { echo QWE; }"
function                                             old     new   delta
run_pipe                                            1820    1826      +6

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-03 13:02:43 +02:00
Denys Vlasenko
5fa0505f8a hush: fix "set -e; false || x=1; echo OK"
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-03 11:21:13 +02:00
Denys Vlasenko
11752d46d1 hush: one-word, no-globbing handling of local/export/readonly args
function                                             old     new   delta
done_word                                            738     790     +52

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-03 08:20:58 +02:00
Denys Vlasenko
f50e14632f ash: parser: Fix parameter expansion inside inner double quotes
Upstream email:

    parser: Fix parameter expansion inside inner double quotes

    The parsing of parameter expansion inside inner double quotes
    breaks because we never look for ENDVAR while innerdq is true.

            echo "${x#"${x+''}"''}

    This patch fixes it by pushing the syntax stack if innerdq is
    true and we enter a new parameter expansion.

    This patch also fixes a corner case where a bad substitution error
    occurs within arithmetic expansion.

    Reported-by: Denys Vlasenko <vda.linux@googlemail.com>
    Fixes: ab1cecb40478 (" parser: Add syntax stack for recursive...")
    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

function                                             old     new   delta
readtoken1                                          2880    2898     +18

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-02 21:00:59 +02:00
Denys Vlasenko
abf755615e hush: fix a backslash-removal bug in case
function                                             old     new   delta
run_list                                            1270    1053    -217

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-02 17:25:18 +02:00
Denys Vlasenko
9a95df9046 ash: expand: Fix bugs with words connected to the right of $@
Upstream email:

    This is actually composed of two bugs.  First of all our tracking
    of quotemark is wrong so anything after "$@" becomes quoted.  Once
    we fix that then the problem is that the first space character
    after "$@" is not recognised as an IFS.

    This patch fixes both.

    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-02 14:27:50 +02:00
Denys Vlasenko
355ec353be ash: redir: Fix typo in noclobber code
Upstream commit "redir: Fix typo in noclobber code"

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-02 13:34:57 +02:00
Denys Vlasenko
c4c2012284 ash: parser: Fix single-quoted patterns in here-documents
Upstream commit:

    From: Herbert Xu <herbert@gondor.apana.org.au>
    Date: Fri, 9 Mar 2018 23:07:53 +0800
    parser: Fix single-quoted patterns in here-documents

    The script

            x=*
            cat <<- EOF
                    ${x#'*'}
            EOF

    prints * instead of nothing as it should.  The problem is that
    when we're in sqsyntax context in a here-document, we won't add
    CTLESC as we should.  This patch fixes it:

    Reported-by: Harald van Dijk <harald@gigawatt.nl>
    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-02 13:29:20 +02:00
Denys Vlasenko
8b536eb40d hush: remove stray debugging printout
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-02 13:26:16 +02:00
Denys Vlasenko
216913c290 ash: parser: Add syntax stack for recursive parsing
This closes 10821.

Upstream patch:

    From: Herbert Xu <herbert@gondor.apana.org.au>
    Date: Fri, 9 Mar 2018 00:14:02 +0800
    parser: Add syntax stack for recursive parsing

    Without a stack of syntaxes we cannot correctly these two cases
    together:

            "${a#'$$'}"
            "${a#"${b-'$$'}"}"

    A recursive parser also helps in some other corner cases such
    as nested arithmetic expansion with paratheses.

    This patch adds a syntax stack allocated from the stack using
    alloca.  As a side-effect this allows us to remove the naked
    backslashes for patterns within double-quotes, which means that
    EXP_QPAT also has to go.

    This patch also fixes removes any backslashes that precede right
    braces when they are present within a parameter expansion context,
    and backslashes that precede double quotes within inner double
    quotes inside a parameter expansion in a here-document context.

    The idea of a recursive parser is based on a patch by Harald van
    Dijk.

    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

var_bash3, var_bash4 and var_bash6 tests are updated
with the output given by bash-4.3.43

With this patch, the following tests now pass for ash:

    dollar_repl_slash_bash2.tests
    squote_in_varexp2.tests
    squote_in_varexp.tests
    var_bash4.tests

function                                             old     new   delta
readtoken1                                          2615    2874    +259
synstack_push                                          -      54     +54
evalvar                                              574     571      -3
rmescapes                                            330     310     -20
subevalvar                                          1279    1258     -21
argstr                                              1146    1107     -39
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 1/4 up/down: 313/-83)           Total: 230 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-02 13:15:37 +02:00
Denys Vlasenko
e84212f834 hush: update information comment about heredoc discrepancy
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-01 20:11:23 +02:00
Denys Vlasenko
899ae5337a libbb: new function bb_die_memory_exhausted
function                                             old     new   delta
bb_die_memory_exhausted                                -      10     +10
xstrdup                                               28      23      -5
xsetenv                                               27      22      -5
xrealloc                                              32      27      -5
xputenv                                               22      17      -5
xmalloc                                               30      25      -5
xfdopen_helper                                        40      35      -5
xasprintf                                             44      39      -5
wget_main                                           2387    2382      -5
open_socket                                           54      49      -5
glob_brace                                           419     414      -5
bb_get_chunk_from_file                               146     141      -5
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 0/11 up/down: 10/-55)           Total: -45 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-01 19:59:37 +02:00
Denys Vlasenko
bb6f573ad2 hush: add a comment where we differ from bash wrt heredoc EOF mark handling
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-01 18:55:00 +02:00
Denys Vlasenko
41fddb4372 parser: Fix backquote support in here-document EOF mark
Upstream commit:

    Author: Herbert Xu <herbert@gondor.apana.org.au>
    Date:   Thu Mar 15 18:27:30 2018 +0800
    parser: Fix backquote support in here-document EOF mark

    Currently using backquotes in a here-document EOF mark is broken
    because dash tries to do command substitution on it.  This patch
    fixes it by checking whether we're looking for an EOF mark during
    tokenisation.

    Reported-by: Harald van Dijk <harald@gigawatt.nl>
    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

With added fix for quoted-ness of the EOF mark.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-01 16:38:32 +02:00
Denys Vlasenko
1e5111b0f8 ash,hush: handle a few more bkslash-newline cases
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-04-01 03:04:55 +02:00
Denys Vlasenko
32e183e63e shells: fix var_LINENO1.tests false positive, add it to ash tests too
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-03-31 20:31:13 +02:00
Denys Vlasenko
a94eeb0b42 hush: fix heredoc_bkslash_newline1.tests failure
function                                             old     new   delta
parse_stream                                        2787    2827     +40
builtin_type                                         117     115      -2

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-03-31 20:16:31 +02:00
Denys Vlasenko
220be537a0 ash: use pgetc_eatbnl() in more places
Part of upstream commit:

    Date:   Thu Mar 8 08:37:11 2018 +0100
    Author: Harald van Dijk <harald@gigawatt.nl>
    parser: use pgetc_eatbnl() in more places

    dash has a pgetc_eatbnl function in parser.c which skips any
    backslash-newline combinations. It's not used everywhere it could be.
    There is also some duplicated backslash-newline handling elsewhere in
    parser.c. Replace most of the calls to pgetc() with calls to
    pgetc_eatbnl() and remove the duplicated backslash-newline handling.

    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Not adding "readtoken1(pgetc_eatbnl(), DQSYNTAX..." changes, since
readtoken1() handles the "starts with backslash + newline" case itself.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-03-31 19:40:56 +02:00
Martijn Dekker
ad4e961352 ash: 'nolog' and 'debug' options cause "$-" to wreak havoc
Upstream commit:

    Date:   Tue Mar 6 17:40:37 2018 +0000
    expand: 'nolog' and 'debug' options cause "$-" to wreak havoc

    Op 29-03-17 om 20:02 schreef Martijn Dekker:
    > Bug: if either the 'nolog' or the 'debug' option is set, trying to
    > expand "$-" silently aborts parsing of an entire argument.
    >
    > $ dash -o nolog -c 'set -fuC; echo "|$- are the options|"; set +o nolog; echo "|$- are the options|"'
    > |
    > |uCf are the options|
    > $ dash -o debug -c 'set -fuC; echo "|$- are the options|"; set +o debug; echo "|$- are the options|"'
    > |
    > |uCf are the options|

    This turned out to be easy to fix. The routine producing the "$-"
    expansion failed to skip options for which there is no option letter,
    but only a long-form name. In dash, 'nolog' and 'debug' are currently
    the only two such options. Patch below.

    - Martijn

    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

In bbox ash, pipefail is the option which exhibited this.

Signed-off-by: Martijn Dekker <martijn@inlv.org>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-03-31 18:30:27 +02:00
Denys Vlasenko
6ffaa00338 hush: fix a signedness bug
Testcase:

set -- a ""; space=" "; printf "<%s>\n" "$@"$space

Before:
<a >
After:
<a>
<>

It usually does not bite since bbox forces -funsigned-char build.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-03-31 00:48:18 +02:00
Denys Vlasenko
73523079a2 ash,hush: new test dollar_repl_slash_bash2.tests
This fails for ash. hush works.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-03-30 23:25:24 +02:00
Denys Vlasenko
ac61f44704 ash: fix "char == CTLfoo" comparison signedness bug
It usually does not bite since bbox forces -funsigned-char build.
But for some reason void linux people disabled that.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-03-30 23:04:39 +02:00
Denys Vlasenko
60fb98e51d ash: use F_DUPFD_CLOEXEC and O_CLOEXEC
function                                             old     new   delta
setjobctl                                            371     367      -4
setinputfile                                         226     220      -6
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-10)             Total: -10 bytes

Based on patch by Mark Marshall <mark.marshall@omicronenergy.com>

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-03-30 22:15:14 +02:00
Denys Vlasenko
d1b845706d hush: add a FIXME comment
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-03-28 18:42:54 +02:00
Denys Vlasenko
9acd63c92c ash,hush: fix "saved" redirected fds still visible in children
Based on a patch by Mark Marshall <mark.marshall@omicronenergy.com>

function                                             old     new   delta
dup_CLOEXEC                                            -      49     +49
fcntl_F_DUPFD                                         46       -     -46

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-03-28 18:35:07 +02:00
Denys Vlasenko
33f7c8f200 hush: code shrink
function                                             old     new   delta
run_pipe                                            1589    1591      +2
pseudo_exec_argv                                     374     375      +1
builtin_type                                         114     115      +1
find_function                                          8       -      -8
------------------------------------------------------------------------------
(add/remove: 0/1 grow/shrink: 3/0 up/down: 4/-8)               Total: -4 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-03-06 17:21:57 +01:00
Denys Vlasenko
d4802c6243 hush: fix a='a\\'; echo "${a%\\\\}"
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-03-02 20:48:36 +01:00
Denys Vlasenko
55f8133a4f shell: tweak bkslash_in_varexp.tests, add bkslash_in_varexp1.tests
It turns out bkslash_in_varexp.tests was a bash bug :]

ash and hush fail "corrected" bkslash_in_varexp.tests as well,
just not as badly as I thought (hush gets half of the cases right).

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-03-02 18:12:12 +01:00
Denys Vlasenko
744a20d8f9 shell: two new tests, both fail for ash and hush
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-03-02 17:13:22 +01:00
Denys Vlasenko
aa617ac097 hush: simplify process_command_subs()
Incidentally, this fixes LINENO bug here:

echo "1:${LINENO}"
echo "2:`echo; echo`"  # was counting lines in the `cmd` output as LINENO++
echo "3:${LINENO}"

function                                             old     new   delta
parse_and_run_file                                    53      71     +18
expand_vars_to_list                                 1187    1164     -23
setup_file_in_str                                     25       -     -25
------------------------------------------------------------------------------
(add/remove: 0/1 grow/shrink: 1/1 up/down: 18/-48)            Total: -30 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-02-13 15:31:19 +01:00
Denys Vlasenko
8de5b9f88b ash : fix double-quoted "\z" handling
function                                             old     new   delta
readtoken1                                          2602    2608      +6

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-02-13 14:44:11 +01:00
Denys Vlasenko
f786901c4b hush: probably fixing a bug in last LINENO fix
I don't have an example of mishandled script, but the logic looked wrong:
it could sometimes treat newlines as if they are spaces.

function                                             old     new   delta
parse_stream                                        2788    2787      -1

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-02-08 19:39:42 +01:00
Denys Vlasenko
5807e18f0c hush: LINENO fix
Script triggering the bug:

	t=0
	echo "at line ${LINENO}"
	while [ ${t} -lt 10 ]; do
		echo "at line ${LINENO}"  # LINENO was 3 instead of 4 here
		t=$((t+1))
	done

function                                             old     new   delta
parse_stream                                        2754    2788     +34
done_word                                            711     738     +27
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 61/0)               Total: 61 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-02-08 19:19:04 +01:00
Denys Vlasenko
749575d3c5 hush: protect against self-modifying trap code
function                                             old     new   delta
check_and_run_traps                                  211     236     +25

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-01-30 04:29:03 +01:00
Denys Vlasenko
6f9442ff30 ash: make it possible to disable "const global ptr" optimization
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-01-28 20:41:23 +01:00
Denys Vlasenko
f19e3c1c6c shell: handle $((NUM++...) like bash does. Closes 10706
function                                             old     new   delta
evaluate_string                                      680     729     +49

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-01-28 20:13:33 +01:00
Denys Vlasenko
675d24aeaf ash: add LINENO support
This patch is a backport from dash of the combination of:
    [SHELL] Add preliminary LINENO support
    [VAR] Fix varinit ordering that broke fc
    [SHELL] Improve LINENO support

function                                             old     new   delta
parse_command                                       1604    1677     +73
calcsize                                             156     223     +67
copynode                                             196     258     +62
evalcommand                                         1546    1606     +60
ash_main                                            1046    1103     +57
lookupvar                                             51     106     +55
evalcase                                             269     317     +48
evaltree                                             501     547     +46
evalfor                                              156     200     +44
evalsubshell                                         156     195     +39
raise_error_syntax                                    11      29     +18
varinit_data                                         120     132     +12
evalfun                                              270     280     +10
funcline                                               -       4      +4
cmdtxt                                               569     572      +3
trapcmd                                              306     304      -2
ash_vmsg                                             153     150      -3
startlinno                                             4       -      -4
funcnest                                               4       -      -4
xxreadtoken                                          263     250     -13
readtoken1                                          2645    2602     -43
------------------------------------------------------------------------------
(add/remove: 1/2 grow/shrink: 14/4 up/down: 598/-69)          Total: 529 bytes
   text	   data	    bss	    dec	    hex	filename
 932834	    481	   6864	 940179	  e5893	busybox_old
 933375	    481	   6856	 940712	  e5aa8	busybox_unstripped

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-01-27 22:02:05 +01:00
Denys Vlasenko
54c2111781 hush: add HUSH_BASH_SOURCE_CURDIR config option, to be on par with ash
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-01-27 20:46:45 +01:00
Denys Vlasenko
f7e0feaf97 hush: fix dot builtin to not search current directory
function                                             old     new   delta
builtin_source                                       182     209     +27

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-01-27 19:05:59 +01:00
Denys Vlasenko
01f7b9e182 ash: introduce a config option to search current directory for sourced files
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-01-26 15:15:43 +01:00
Denys Vlasenko
043be55691 ash testsuite: add two hush tests from last commit
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-01-25 14:09:06 +01:00
Denys Vlasenko
cba79a87f8 hush: fix two corner cases in ${v/pattern/repl}. Closes 10686
function                                             old     new   delta
expand_one_var                                      1592    1618     +26

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-01-25 14:07:40 +01:00
Denys Vlasenko
0ca3198f93 hush: fix handling of $_ (so far it's an ordinary variable, no special meaning)
function                                             old     new   delta
parse_dollar                                         820     779     -41

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-01-25 13:20:50 +01:00
William Pitcock
d8fd88a091 ash: add support for command_not_found_handle hook function (bashism)
This implements support for the command_not_found_handle hook function, which is
useful for allowing package managers to suggest packages which could provide the
command.

Unlike bash, however, we ignore exit codes from the hook function and always return
the correct POSIX error code (EX_NOTFOUND).

function                                             old     new   delta
find_command                                         911     990     +79

Signed-off-by: William Pitcock <nenolod@dereferenced.org>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-01-24 18:33:18 +01:00
Denys Vlasenko
46f839c3f7 hush: fix a case where EXIT trap may modify its code mid-flight
function                                             old     new   delta
hush_exit                                             93      99      +6

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-01-19 16:58:44 +01:00
Denys Vlasenko
b8d076b00b hush: fix build if !BASH_LINENO_VAR
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-01-19 16:00:57 +01:00
Denys Vlasenko
6aad1ddcc9 hush: implement $LINENO bashism
function                                             old     new   delta
run_pipe                                            1697    1774     +77
unset_local_var_len                                  258     301     +43
hush_main                                           1009    1051     +42
set_local_var                                        516     554     +38
parse_and_run_file                                    42      75     +33
i_getch                                               96     116     +20
.rodata                                            18858   18876     +18
done_command                                         106     122     +16
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 8/0 up/down: 287/0)             Total: 287 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-01-19 15:52:36 +01:00
Denys Vlasenko
1750d3afc1 ash: a bit of NOFORK code should only be active if FEATURE_SH_STANDALONE=y
function                                             old     new   delta
evalcommand                                         1546    1420    -126

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-01-15 00:41:04 +01:00
Denys Vlasenko
aea52e7b2d shell: echo ${?:0} was fixed sometime ago, enable it in tests
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-01-13 19:28:19 +01:00
Denys Vlasenko
9809a82b59 hush: fix raw ^C handlisg in single-quoted strings
function                                             old     new   delta
parse_stream                                        2719    2754     +35

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-01-13 19:14:27 +01:00
Denys Vlasenko
6606c519ef hush: add command2.tests from ash tests
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-01-12 17:58:20 +01:00
Denys Vlasenko
afb73a25ea hush: tweak command -vV printing code, no logic changes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-01-12 16:17:59 +01:00
Denys Vlasenko
5700029c65 hush: implement "command -v -V"
function                                             old     new   delta
pseudo_exec_argv                                     231     374    +143
if_command_vV_print_and_exit                           -     127    +127
builtin_set                                          267     273      +6
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 2/0 up/down: 276/0)             Total: 276 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-01-12 14:41:45 +01:00
Denys Vlasenko
3bb3e1d0a1 hush: implement "command" builtin (no options are supported yet)
function                                             old     new   delta
pseudo_exec_argv                                     194     231     +37

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-01-11 18:05:05 +01:00
Denys Vlasenko
1f1911239c hush: fix handling of ^C in eval
function                                             old     new   delta
run_list                                            1044    1259    +215
builtin_eval                                          45     126     +81
expand_strvec_to_string                               91       -     -91
------------------------------------------------------------------------------
(add/remove: 0/1 grow/shrink: 2/0 up/down: 296/-91)           Total: 205 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-01-11 13:17:30 +01:00
Denys Vlasenko
932b9971d0 hush: fix handling of raw ^C in scripts: "echo ^C"
function                                             old     new   delta
expand_vars_to_list                                 1133    1187     +54
parse_stream                                        2690    2719     +29

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-01-11 12:39:48 +01:00
Denys Vlasenko
aaf7a2e24d hush: remove dead code
function                                             old     new   delta
done_word                                            761     711     -50

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-01-11 11:19:00 +01:00
Denys Vlasenko
03c36e0be1 ash: ALWAYS_INLINE grabstackblock()
function                                             old     new   delta
grabstackblock                                         5       -      -5

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-01-10 15:18:35 +01:00
Denys Vlasenko
baa41c7855 ash: make ${v:N:M} more robust for very large M by clamping to MIN/MAX_INT
Before this patch, "${v:2:0x100000001}" = "${v:2:1}",
and similarly, constructs like "${v:2:9999999999}" may give wrong result
due to int overflows.

function                                             old     new   delta
substr_atoi                                            -      43     +43

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-01-10 13:22:25 +01:00
Denys Vlasenko
d1df1a709f ash: add comment explaining last change
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-01-09 17:25:58 +01:00
Denys Vlasenko
740058b42b ash: fix var_bash5.tests - ${VAR/pattern/repl} construct
function                                             old     new   delta
subevalvar                                          1198    1279     +81
rmescapes                                            308     330     +22
preglob                                                8      10      +2
parsefname                                           152     154      +2
expandarg                                            973     975      +2
argstr                                              1144    1146      +2
mklocal                                              290     288      -2
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 6/1 up/down: 111/-2)            Total: 109 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-01-09 17:07:06 +01:00
Ingo van Lil
9c8e94bc0a ash: fail if 'shift' operand is out of range
If the numeric argument passed to ash's 'shift' built-in is greater than
'$#' the command performs no operation and exits successfully. It should
return a non-zero exit code instead:

http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#shift

This is consistent with bash and hush.

function                                             old     new   delta
shiftcmd                                             122     120      -2

Signed-off-by: Ingo van Lil <inguin@gmx.de>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2018-01-07 14:22:38 +01:00
Denys Vlasenko
82d1c1f84a randomconfig fixes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-12-31 17:30:02 +01:00
Denys Vlasenko
a5060b8364 ash: fix nofork bug where environment is not properly passed to a command
function                                             old     new   delta
listvars                                             144     252    +108
evalcommand                                         1500    1546     +46
showvars                                             142     147      +5
shellexec                                            242     245      +3
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 4/0 up/down: 162/0)             Total: 162 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-11-03 14:16:25 +01:00
Denys Vlasenko
9c143ce52d ash: retain envvars with bad names in initial environment. Closes 10231
Reworks "ash: [VAR] Sanitise environment variable names on entry"
commit.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-11-02 12:56:24 +01:00
Denys Vlasenko
25f3b737dc hush: fix comment parsing in cmd, closes 10421
function                                             old     new   delta
parse_stream                                        2692    2690      -2

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-10-22 15:55:48 +02:00
Denys Vlasenko
14c85eb7db whitespace fixes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-10-12 19:40:47 +02:00
Denys Vlasenko
3c183a8758 typo fix
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-10-12 19:35:42 +02:00
Denys Vlasenko
a2e32b324e ash: survive failures in $PS1 expansion. Closes 10371
function                                             old     new   delta
expandstr                                            120     209     +89

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-10-12 19:20:13 +02:00
Denys Vlasenko
6f97b30a90 use %m printf specifier where appropriate
function                                             old     new   delta
out                                                   85      75     -10
udhcpd_main                                         1472    1461     -11
open_stdio_to_tty                                     98      85     -13
init_exec                                            245     232     -13
udhcpc_main                                         2763    2749     -14
do_cmd                                              4771    4755     -16
status_line_bold_errno                                32      14     -18
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/7 up/down: 0/-95)             Total: -95 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-09-29 18:17:25 +02:00
Denys Vlasenko
0c4dbd481a regularize format of source file headers, no code changes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-09-18 16:28:43 +02:00
Denys Vlasenko
9fed83a7cc hush: fix false positive in unset.tests
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-29 14:35:58 +02:00
Denys Vlasenko
6016181b68 hush: GETOPT_RESET() _after_ getopts too.
NOEXEC'ed applets which use getopt() need this.

function                                             old     new   delta
builtin_getopts                                      403     413     +10

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-29 14:32:17 +02:00
Denys Vlasenko
55af51c66d hush: reuse "OPTIND=..." string
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-29 13:48:49 +02:00
Denys Vlasenko
238ff98bb8 hush: fix "getopts" builtin to not be upset by other builtins calling getopt()
function                                             old     new   delta
builtin_getopts                                      363     403     +40
unset_local_var_len                                  185     215     +30
set_local_var                                        440     466     +26
reset_traps_to_defaults                              151     157      +6
pseudo_exec_argv                                     320     326      +6
install_special_sighandlers                           52      58      +6
pick_sighandler                                       62      65      +3
execvp_or_die                                         85      88      +3
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 8/0 up/down: 120/0)             Total: 120 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-29 13:38:30 +02:00
Denys Vlasenko
12ffefb549 ash: more s/error/perror/ for better error reporting
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-23 14:52:56 +02:00
Johannes Schindelin
687aac056d ash: when cd fails, say why
Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-23 14:48:25 +02:00
Johannes Schindelin
20a63b2c18 ash: report reason when a script file could not be opened
It is always nicer to give the user some sort of indication why an
operation failed.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-23 14:47:45 +02:00
Denys Vlasenko
fec2b1f79d ash: stage backported LINENO support as a separate patch
Looks biggish and not particularly useful, but may be easier to just eat
the impact if future backports from dash would be otherwise increasingly
difficult.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-17 16:43:33 +02:00
Denys Vlasenko
048491fbdc hush: trivial code shrink in builtin_getopts
function                                             old     new   delta
builtin_getopts                                      368     363      -5

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-17 12:36:39 +02:00
Denys Vlasenko
9832bbaba9 ash: unset OPTARG if getopts exits 1, support OPTERR=0 behavior
function                                             old     new   delta
getoptscmd                                           522     547     +25

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-15 15:44:41 +02:00
Denys Vlasenko
4476c70301 ash,hush: comment and debug tweaks, no code changes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-15 15:27:41 +02:00
Denys Vlasenko
0485b677d2 ash: one "current line = 1" might be missing, fix that
I'm not sure this is necessary, but dash has this init here.
Just in case, do it too.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-14 19:46:56 +02:00
Denys Vlasenko
c2aea025bb ash: update testsuite (we now error out on ${#=})
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-14 15:01:32 +02:00
Denys Vlasenko
452cc1d9bd ash: [PARSER] Catch variable length expansions on non-existant specials
Upstream commit:

    Date: Thu, 30 Oct 2014 11:53:35 +0800
    [PARSER] Catch variable length expansions on non-existant specials

    Currently we only check special variable names that follow directly
    after $ or ${.  So errors such as ${#&} are not caught.  This patch
    fixes that by moving the is_special check to just before we print out
    the special variable name.

    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

function                                             old     new   delta
readtoken1                                          2630    2635      +5

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-14 14:23:45 +02:00
Denys Vlasenko
007ce9f807 shell: tweak getopts tests, no code changes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-13 02:59:00 +02:00
Denys Vlasenko
419db0391e hush: implement "silent" optstrings of ":opts"
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-11 17:21:14 +02:00
Denys Vlasenko
129e1ce72c hush: add a test which fails due to uclibc bug in getopt()
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-11 17:00:39 +02:00
Denys Vlasenko
d16e612c93 hush: fix redirect code (was using uninitialized variables)
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-11 15:41:39 +02:00
Denys Vlasenko
9a7d0a0191 shell: add OPTARG poisoning to getopt_optarg.tests
ash fails this!

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-11 02:37:48 +02:00
Denys Vlasenko
81f962f3df hush: teach getopts to set/unset OPTARG
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-11 02:05:21 +02:00
Denys Vlasenko
74d4058928 hush: getopts builtin
function                                             old     new   delta
builtin_getopts                                        -     271    +271
bltins1                                              372     384     +12
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 1/0 up/down: 283/0)             Total: 283 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-11 01:32:46 +02:00
Denys Vlasenko
4628945cd8 ash: fix "unset OPTIND" throwing an error message
Added test was failing quite severely. Now only one subtest fails
(OPTERR=0 has no effect).

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-11 00:59:36 +02:00
Denys Vlasenko
11f2e99c13 hush: optional times builtin
function                                             old     new   delta
builtin_times                                          -     108    +108
bltins1                                              360     372     +12
static.times_tbl                                       -       9      +9
------------------------------------------------------------------------------
(add/remove: 2/0 grow/shrink: 1/0 up/down: 129/0)             Total: 129 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-10 16:34:03 +02:00
Denys Vlasenko
cde46f75cb shell: more efficient check for EOL in read
function                                             old     new   delta
shell_builtin_read                                  1334    1320     -14

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-09 14:04:07 +02:00
Denys Vlasenko
1f41c885fc hush: implement -d DELIM option for 'read'
The POSIX standard only requires the 'read' builtin to handle '-r':
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/read.html

However, Bash introduced the option '-d <DELIM>' to override IFS for
just one invocation, and it is quite useful.

We already support this in ash, let's add it to hush, too.

function                                             old     new   delta
builtin_read                                         263     284     +21
.rodata                                           163587  163589      +2
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 23/0)               Total: 23 bytes

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-09 13:52:36 +02:00
Johannes Schindelin
3bef5d89b0 ash: implement -d DELIM option for read
The POSIX standard only requires the read builtin to handle -r:
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/read.html

However, Bash introduced the option -d <DELIM> to override IFS for
just one invocation, and it is quite useful.

It is also super easy to implement in BusyBox' ash, so let's do that.

The motivation: This option is used by Git's test suite.

function                                             old     new   delta
.rodata                                           163505  163587     +82
shell_builtin_read                                  1244    1289     +45
readcmd                                              233     259     +26
builtin_read                                         258     263      +5
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 4/0 up/down: 158/0)             Total: 158 bytes

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-09 13:52:17 +02:00
Denys Vlasenko
80e8e3cc05 noexec: consolidate code
function                                             old     new   delta
run_noexec_applet_and_exit                             -      61     +61
find_applet_by_name                                  128     124      -4
run_applet_no_and_exit                               441     434      -7
tryexec                                              169     152     -17
pseudo_exec_argv                                     338     321     -17
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 0/6 up/down: 61/-48)             Total: 13 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-07 19:24:57 +02:00
Denys Vlasenko
c9c1ccc4ed noexec: do GETOPT_RESET() before entering APPLET_main()
hush -c 'yes | head -1' was not happy.

function                                             old     new   delta
tryexec                                              159     169     +10
pseudo_exec_argv                                     328     338     +10

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-07 18:59:35 +02:00
Denys Vlasenko
f2cf1cc716 noexec: set comm field for noexecs
function                                             old     new   delta
set_task_comm                                          -      18     +18
tryexec                                              152     159      +7
pseudo_exec_argv                                     321     328      +7
main                                                 106      97      -9
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 3/2 up/down: 34/-13)             Total: 23 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-07 18:45:33 +02:00
Denys Vlasenko
248a67fb75 free,stat: make NOEXEC
pkill/pgrep/pidof uncovered another quirk: what about noexec's _process names_?

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-07 18:18:09 +02:00
Denys Vlasenko
0f14f41e72 ash: do not set a signal to SIG_DFL if it already is
function                                             old     new   delta
setsignal                                            312     338     +26

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-06 20:06:19 +02:00
Denys Vlasenko
5c527dc57e make 17 state-changing execing applets (ex: "nice PROG ARGS") noexec
The applets with "<applet> [opts] PROG ARGS" API very quickly exec
another program, noexec is okay for them:

 chpst/envdir/envuidgid/softlimit/setuidgid
 chroot
 chrt
 ionice
 nice
 nohup
 setarch/linux32/linux64
 taskset
 cttyhack

"reset" and "sulogin" applets don't have this form, but also exec
another program at once, thus made noexec too.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-04 19:55:01 +02:00
Denys Vlasenko
f8cdc7a2bc ash: BASH_XTRACEFD bashism
Based on patch by Johannes Schindelin <johannes.schindelin@gmx.de>

function                                             old     new   delta
evalcommand                                         1447    1500     +53

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-04 15:24:49 +02:00
Johannes Schindelin
7344755823 ash: remove no-longer-used variable
As of 035486c75 (ash: significant overhaul of redirect saving logic,
2017-07-31), the sv_pos variable is no longer used (just assigned to,
with no further effect).

Let's just remove it.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-04 14:52:08 +02:00
Denys Vlasenko
d329e34c96 ash: INT_OFF/INT_ON around run_nofork_applet()
function                                             old     new   delta
evalcommand                                         1441    1447      +6

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-04 14:50:03 +02:00
Denys Vlasenko
49e6bf2db9 sheel: improve comments on signal handling
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-04 14:28:16 +02:00
Denys Vlasenko
cfd392bea9 ash: add a fixme comment at run_nofork_applet
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-03 19:56:29 +02:00
Denys Vlasenko
19c9f31af1 nofork: fix a bug uncovered by hush testsuite (forgotten fflush)
function                                             old     new   delta
run_nofork_applet                                    280     287      +7

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-03 19:52:47 +02:00
Denys Vlasenko
39701204cf hush: do not accept "if() { echo; }" function def
function                                             old     new   delta
parse_stream                                        2634    2692     +58
msg_and_die_if_script                                  -      21     +21
syntax_error_unexpected_ch                            41      46      +5
syntax_error_at                                       14      18      +4
die_if_script                                         31      28      -3
setup_redirects                                      319     308     -11
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 3/2 up/down: 88/-14)             Total: 74 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-02 19:46:14 +02:00
Denys Vlasenko
84ea60ed65 line editing: make read_line_input() not take timeout param
It's almost always -1.

function                                             old     new   delta
read_line_input                                     3902    3912     +10
new_line_input_t                                      24      31      +7
pgetc                                                583     585      +2
save_command_ps_at_cur_history                        80      78      -2
read_line                                             76      74      -2
fgetc_interactive                                    246     244      -2
addLines                                              84      82      -2
doCommands                                          2226    2222      -4
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/5 up/down: 19/-12)              Total: 7 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-02 17:27:28 +02:00
Denys Vlasenko
dd4b446f76 hush: make SIGINT handling visually less confusing
$ echo $$
18448
$ echo $? <wait here, run "kill -INT 18448" in other shell><press enter>
   <=== NOTHING??
$

That empty line does not look right. After this patch:

$ echo $$
18448
$ echo $? <wait here, run "kill -INT 18448" in other shell><press enter>
^C
$

function                                             old     new   delta
fgetc_interactive                                    245     246      +1

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-02 16:52:12 +02:00
Denys Vlasenko
7c40ddd950 NOFORK fixes
"rm -i FILE" and "yes" can now be interrupted by ^C in hush.
This also now works:

$ usleep 19999999
^C
$ echo $?
130

function                                             old     new   delta
run_pipe                                            1668    1711     +43
pseudo_exec_argv                                     312     321      +9
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 52/0)               Total: 52 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-02 16:37:39 +02:00
Denys Vlasenko
95f7953f2c do not use `a' quoting style in comments
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-08-02 14:26:33 +02:00
Denys Vlasenko
ec05df13b0 ash: align --login code with dash
Upstream commit:

    Date: Sun, 13 Jul 2008 22:34:50 +0800
    [OPTIONS] Added support for -l

    This patch adds support for the -l option (login shell) as required
    by the LSB.

    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

It's a bit bigger, but gets rid of one global variable

function                                             old     new   delta
options                                              554     576     +22

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-31 19:43:47 +02:00
Denys Vlasenko
cee603d921 hush: remove redundant "G_flag_return_in_progress = -1"
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-31 18:06:07 +02:00
Denys Vlasenko
5b3d2eb327 hush: fix "true | func_with_return" not allowing return.
function                                             old     new   delta
pseudo_exec_argv                                     305     312      +7

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-31 18:02:28 +02:00
Denys Vlasenko
d0fff9155b ash: fix display of ">&-" redirect in job strings
function                                             old     new   delta
cmdtxt                                               558     569     +11

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-31 14:32:18 +02:00
Denys Vlasenko
75481d3634 hush: functions have priority over builtins (!)
function                                             old     new   delta
pseudo_exec_argv                                     291     305     +14
run_pipe                                            1560    1555      -5
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 14/-5)               Total: 9 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-31 05:27:09 +02:00
Denys Vlasenko
bf1c344dfd hush: if STANDALONE, close interactive fd for NOEXECed children
function                                             old     new   delta
pseudo_exec_argv                                     291     305     +14

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-31 04:54:53 +02:00
Denys Vlasenko
32fdf2f9fc ash,hush: ">&10" redirects to script/tty fds should not work
The fact that shell has open fds to tty and/or scripts should be
unobservable, if possible. In particular, if redirect tries to dup
one of them via ">&script_fd", it's better to pretend that script_fd
is closed, and thus redirect fails with EBADF.

Fixes these two testcase failures:
ash-redir/redir_to_bad_fd.tests
hush-redir/redir_to_bad_fd3.tests

function                                             old     new   delta
redirect                                            1018    1129    +111
setup_redirects                                      250     359    +109
readtoken1                                          2651    2655      +4
cmdloop                                              185     187      +2
changepath                                           194     195      +1
save_fd_on_redirect                                  203     194      -9
evaltree                                             501     484     -17
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 5/2 up/down: 227/-26)           Total: 201 bytes
   text    data     bss     dec     hex filename
 914553     485    6848  921886   e111e busybox_old
 914754     485    6848  922087   e11e7 busybox_unstripped

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-31 04:35:18 +02:00
Denys Vlasenko
035486c750 ash: significant overhaul of redirect saving logic
New code is similar to what hush is doing.
Make CLOSED to -1: same as dash.
popredir() loses "restore" parameter: same as dash.
COPYFD_RESTORE bit is no longer necessary.

This change fixes this interactive bug:

	$ ls -l /proc/$$/fd 10>&-
	ash: can't set tty process group: Bad file descriptor
	ash: can't set tty process group: Bad file descriptor
	[1]+  Done(2)                    ls -l /proc/${\$}/fd 10>&4294967295

function                                             old     new   delta
unwindredir                                           29      27      -2
tryexec                                              154     152      -2
evaltree                                             503     501      -2
evalcommand                                         1369    1367      -2
cmdloop                                              187     185      -2
redirect                                            1029    1018     -11
popredir                                             153     123     -30
need_to_remember                                      36       -     -36
is_hidden_fd                                          68       -     -68
------------------------------------------------------------------------------
(add/remove: 0/2 grow/shrink: 0/7 up/down: 0/-155)           Total: -155 bytes
   text    data     bss     dec     hex filename
 914572     485    6848  921905   e1131 busybox_old
 914553     485    6848  921886   e111e busybox_unstripped

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-31 04:21:46 +02:00
Denys Vlasenko
657e9005a9 hush: massage redirect code to be slightly more like ash
function                                             old     new   delta
save_fd_on_redirect                                    -     203    +203
xdup_CLOEXEC_and_close                                 -      75     +75
setup_redirects                                      245     250      +5
xdup_and_close                                        72       -     -72
save_fds_on_redirect                                 221       -    -221
------------------------------------------------------------------------------
(add/remove: 2/2 grow/shrink: 1/0 up/down: 283/-293)          Total: -10 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-31 04:08:09 +02:00
Denys Vlasenko
d07a15bd1b ash: remove REDIR_SAVEFD2
function                                             old     new   delta
evalcommand                                         1364    1369      +5
redirect                                            1055    1014     -41
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/1 up/down: 5/-41)             Total: -36 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-31 04:05:51 +02:00
Denys Vlasenko
5f0a75f24b ash: if !ENABLE_ASH_EXPAND_PRMT, disable PSSYNTAX code
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-29 22:58:44 +02:00
Denys Vlasenko
1c79aeb6a8 ash: [REDIR] Fix popredir on abnormal exit from built-in
Upstream commit:

    Date: Thu, 27 May 2010 15:03:46 +0800
    [REDIR] Fix popredir on abnormal exit from built-in

    Just like the poplocalvar problem recently fixed, redirections
    can also be leaked in case of an abnormal exit.  This patch fixes
    it using the same method as poplocalvar, by storing the previous
    redirection state and restoring to that point.

    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-29 22:51:52 +02:00
Denys Vlasenko
469998015f ash: [PARSER] Add FAKEEOFMARK for expandstr
Upstream commit:

    Date: Thu, 27 Dec 2007 13:54:16 +1100
    [PARSER] Add FAKEEOFMARK for expandstr

    Previously expandstr used the string "" to indicate that it needs to be
    treated just like a here-doc except that there is no terminator.  However,
    the string "" is in fact a valid here-doc terminator so now that we deal
    with it correctly expandstr no longer works in the presence of new-lines
    in the prompt.

    This patch introduces the FAKEEOFMARK macro which does not equal any
    real EOF marker but is distinct from the NULL pointer which is used to
    indicate non-here-doc contexts.

    Thanks to Markus Triska for reporting this regression.

    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Unfortunately, I did not find the failing example for this old fix.

I also tweaked the code which was added by this commit:
"
Date: Mon Sep 24 18:30:02 2007 +0000
ash: fix prompt expansion (Natanael Copa <natanael.copa@gmail.com>)
"
since other parts of code do expect expandstr() to use DQSYNTAX, not PSSYNTAX.

function                                             old     new   delta
parse_stream                                        2609    2634     +25
setprompt_if                                         128     133      +5
read_profile                                          32      37      +5
evalcommand                                         1334    1339      +5
expandstr                                            122     120      -2
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 4/1 up/down: 40/-2)              Total: 38 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-29 21:12:29 +02:00
Denys Vlasenko
0f018b3070 hush: fix handling of empty heredoc EOF marker
function                                             old     new   delta
parse_stream                                        2609    2634     +25

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-29 20:43:26 +02:00
Denys Vlasenko
a732898fdd ash: [PARSER] Removed noexpand/length check on eofmark
Upstream comment:

	Date: Sun, 11 Nov 2007 14:21:23 +0800
	[PARSER] Removed noexpand/length check on eofmark

	On Tue, Oct 30, 2007 at 04:23:35AM +0000, Oleg Verych wrote:
	>
	> } 8<<""
	> ======================

	Actually this (the empty delim) only works with dash by accident.
	I've tried bash and pdksh and they both terminate on the first
	empty line which is what you would expect rather than EOF.  The
	real Korn shell does something completely different.

	I've fixed this in dash to conform to bash/pdksh.

	> In [0] it's stated, that delimiter isn't evaluated (expanded), only
	> quoiting must be checked. That if() seems to be completely bogus.

	OK I agree.  The reason it was there is because the parser would
	have already replaced the dollar sign by an internal representation.

	I've fixed it properly with this patch.

	Test case:

	        cat <<- $a
	                OK
	        $a

	        cat <<- ""
	                OK

	        echo OK

	Old result:

	        dash: Syntax error: Illegal eof marker for << redirection
	        OK

	        echo OK

	New result:

	        OK
	        OK
	        OK

function                                             old     new   delta
parsefname                                           227     152     -75
readtoken1                                          2819    2651    -168
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-243)           Total: -243 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-29 19:57:28 +02:00
Denys Vlasenko
170f93ef1b ash: "Undo all redirections" comment is wrong, delete it
No code changes.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-29 18:54:53 +02:00
Denys Vlasenko
00a1dbd230 ash: make tryexec(cmd) parameter const char
Fewer casts this way.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-29 01:20:53 +02:00
Ron Yorston
be366e5afa ash: support platforms that don't have '%m' printf specifier
The '%m' conversion specifier prints an error message based on the
current value of 'errno'.  It is available in the GNU C library,
Cygwin (since 2012), uClibc and musl.

It is not available in various BSDs, BSD-derived systems (MacOS,
Android) or Microsoft Windows.

Use a symbol defined in platform.h to control how error messages
can be formatted to display the 'errno' message.  On platforms that
support it use '%m'; on other platforms use '%s' and strerror().

On platforms that have '%m' there is essentially no change in the
size of the binary.  Otherwise:

function                                             old     new   delta
redirect                                            1287    1310     +23
xtcsetpgrp                                            27      44     +17
dup2_or_raise                                         34      51     +17
setinputfile                                         267     275      +8
.rodata                                           163379  163371      -8
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 4/1 up/down: 65/-8)              Total: 57 bytes

Signed-off-by: Ron Yorston <rmy@pobox.com>
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-28 15:39:26 +02:00
Denys Vlasenko
619d9b5e68 ash: less hackish implementation of evaltreenr()
Defining a function alias with __attribute__ ((alias("evaltree"),__noreturn__))
is not that usual, and clang had a bug which made it misunderstand
this construct.

Switch to:
ALWAYS_INLINE NORETURN evaltreenr() { evaltree(); unreachable(); }

Older gcc's do not know unreachable(), on them we pay the price of having
a few extra calls to abort():

function                                             old     new   delta
evalsubshell                                         151     156      +5
evalpipe                                             357     362      +5
argstr                                              1141    1144      +3

On newer gcc, code size does not change.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-28 15:28:33 +02:00
Denys Vlasenko
488e609203 ash: force inlining of a trivial function
function                                             old     new   delta
bltinlookup                                            5       -      -5

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-26 23:08:36 +02:00
Denys Vlasenko
b8c0bc18f0 ash: revert previous implementation of "A=1 A=2 B=$A cmd" code
Reverts this:

    commit 0e6f661e23
    Date:   Fri Feb 15 15:02:15 2008 +0000
    ash: handle "A=1 A=2 B=$A; echo $B". closes bug 947.

A different fix from upstream has been imported by previous six commits.

Last seven commits, cumulative:

function                                             old     new   delta
poplocalvars                                           -     314    +314
mklocal                                                -     288    +288
pushlocalvars                                          -      48     +48
evalcommand                                         1372    1408     +36
unwindlocalvars                                        -      22     +22
ash_main                                            1022    1029      +7
setvar                                               167     172      +5
localvar_stack                                         -       4      +4
setvareq                                             303     302      -1
evalcase                                             271     269      -2
subevalvar                                          1202    1198      -4
localvars                                              4       -      -4
cmdenviron                                             4       -      -4
expandarg                                            984     973     -11
evalvar                                              589     574     -15
argstr                                              1164    1141     -23
dotcmd                                               335     303     -32
bltinlookup                                           51       5     -46
varvalue                                             709     596    -113
evalfun                                              456     270    -186
localcmd                                             364      44    -320
------------------------------------------------------------------------------
(add/remove: 5/2 grow/shrink: 3/11 up/down: 724/-761)         Total: -37 bytes
   text	   data	    bss	    dec	    hex	filename
 915353	    485	   6888	 922726	  e1466	busybox_old
 915320	    485	   6880	 922685	  e143d	busybox_unstripped

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-26 23:03:21 +02:00
Denys Vlasenko
d04fc712e3 ash: [VAR] Fix loss of variables when hash collides
Upstream commit:

    Date: Tue, 6 Jul 2010 17:40:53 +0800
    [VAR] Fix loss of variables when hash collides

    Brian Koropoff reported that the new var patches broke the following
    script:

    #!/bin/dash
    GDM_LANG="bar"
    OPTION="foo"
    unset GDM_LANG
    # OPTION has mysteriously become unset
    echo "$OPTION"

    He correctly diagnosed this as a result of removing all variables
    in the hash chain preceding the one that should be removed in
    setvareq.

    He also provided a patch to fix this.

    This patch is based on his but without keeping the original vpp.
    As a result, we now store new variables at the end of the hash
    chain instead of the beginning.

    To make this work, setvareq/setvar now returns the vp pointer
    modified.  In case they're used to unset a variable the pointer
    returned is undefined.  This is because mklocal needs it and
    used to get it by assuming that the new variable always appear
    at the beginning of the chain.

    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-26 20:33:51 +02:00
Denys Vlasenko
85241c7b0b ash: [VAR] Do not poplocalvars prematurely on regular utilities
Upstream commit:

    Date: Thu, 27 May 2010 11:50:19 +0800
    [VAR] Do not poplocalvars prematurely on regular utilities

    The recent cmdenviron removal broke regular utilities by calling
    poplocalvars too early.  This patch fixes that by postponing the
    poplocalvars for regular utilities until they have completed.

    In order to ensure that local still works, it is now a special
    built-in.

    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-26 20:33:51 +02:00
Denys Vlasenko
484fc2056d ash: [VAR] Fix poplocalvar on abnormal exit from function
Upstream commit:

    Date: Thu, 27 May 2010 11:32:55 +0800
    [VAR] Fix poplocalvar on abnormal exit from function

    The new localvar code broke the abnormal exit from functions
    and built-ins by not restoring the original localvar state.

    This patch fixes this by storing the previous localvar state so
    that we always unwind correctly in case of an abnormal exit.

    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-26 20:33:51 +02:00
Denys Vlasenko
981a0568b3 ash: [VAR] Replace cmdenviron with localvars
Upstream commit:

    Date: Wed, 26 May 2010 18:54:19 +0800
    [VAR] Replace cmdenviron with localvars

    This patch replaces the cmdenviron mechanism for temporary command
    variables with the localvars mechanism used by functions.

    This reduces code size, and more importantly, makes the variable
    assignment take effect immediately as required by POSIX.

    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-26 20:33:51 +02:00
Denys Vlasenko
d5b500c81c ash: [VAR] Fix poplocalvar leak
Upstream commit:

    Date: Tue, 25 May 2010 18:14:32 +0800
    [VAR] Fix poplocalvar leak

    When a variable is marked as local, we set VSTRFIXED on its vp
    recored.  However, poplocalvar never clears this flag for variables
    that were unset to begin with.  Thus if you ever made an unset
    variable local, it would get the VSTRFIXED bit and stick around
    forever.

    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-26 20:33:51 +02:00
Denys Vlasenko
b8ab27bf53 ash: [VAR] Add localvars nesting
Upstream commit:

    Date: Mon, 24 May 2010 15:31:27 +0800
    [VAR] Add localvars nesting

    This patch adds localvars nesting infrastructure so we can reuse
    the localvars mechanism for command evaluation.

    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-26 20:33:51 +02:00
Denys Vlasenko
cf3a796dd1 ash: alloc slightly smaller buffer in cvtnum(); faster unsetvar()
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-26 14:38:19 +02:00
Denys Vlasenko
b31b61bb9b ash: fix redir_leak.tests if STANDALONE=y
If STANDALONE and we run a NOEXEC applet, saved copies of redirected fds
were visible for the child. They have CLOEXEC bit, yes, but we do not exec
in this case.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-26 13:42:53 +02:00
Denys Vlasenko
b0648b0e78 shell: remove ${#+} tests, it is not a valid construct
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-26 00:30:02 +02:00
Denys Vlasenko
2093ad296f hush: fix ${##}, ${#?}, ${#!} handling
function                                             old     new   delta
parse_dollar                                         786     820     +34
expand_one_var                                      1579    1592     +13
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 47/0)               Total: 47 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-26 00:07:27 +02:00
Denys Vlasenko
1e3e2ccd5d ash: [SHELL] Optimize dash -c "command" to avoid a fork
Upstream commit:

    Date: Thu, 7 Jul 2011 13:58:48 +0800
    [SHELL] Optimize dash -c "command" to avoid a fork

    On Sun, Apr 10, 2011 at 07:36:49AM +0000, Jonathan Nieder wrote:
    > From: Jilles Tjoelker <jilles@stack.nl>
    > Date: Sat, 13 Jun 2009 16:17:45 -0500
    >
    > This change only affects strings passed to -c, when the -s option is
    > not used.
    >
    > Use the EV_EXIT flag to inform the eval machinery that the string
    > being passed is the entirety of input.  This way, a fork may be
    > omitted in many special cases.
    >
    > If there are empty lines after the last command, the evalcmd will not
    > see the end early enough and forks will not be omitted. The same thing
    > seems to happen in bash.
    >
    > Example:
    >   sh -c 'ps lT'
    > No longer shows a shell process waiting for ps to finish.
    >
    > [jn: ported from FreeBSD SVN r194128.  Bugs are mine.]
    >
    > Signed-off-by: Jonathan Nieder <jrnieder@gmail.com>

    Instead of detecting EOF using the input layer, I'm going to
    use the parser instead.  In either case, we always have to read
    ahead in order to complete the parsing of the previous node.
    Therefore we always know whether there is more to come, except
    in the case where we see a newline/semicolon or similar.

    For the purposes of sh -c, this should be sufficient.

    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

function                                             old     new   delta
evalstring                                           190     224     +34
ash_main                                            1014    1022      +8
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/0 up/down: 42/0)               Total: 42 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-25 20:31:14 +02:00
Denys Vlasenko
86981e3ad2 ash: allow "trap NUM [SIG]..." syntax
While at it, make get_signum() return -1 for numeric strings >= NSIG.

function                                             old     new   delta
trapcmd                                              292     306     +14
get_signum                                           295     300      +5
builtin_trap                                         413     412      -1
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/1 up/down: 19/-1)              Total: 18 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-25 20:06:17 +02:00
Denys Vlasenko
f1a5cb0548 ash: [REDIR] Replace GPL noclobberopen code with the FreeBSD version
Upstream commit:

    Date: Thu, 10 Mar 2011 16:52:13 +0800
    [REDIR] Replace GPL noclobberopen code with the FreeBSD version

    Replace noclobberopen() from bash with the FreeBSD code for noclobber
    opens.

    This also reduces code size by eliminating an unnecessary check.

    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

function                                             old     new   delta
changepath                                           192     194      +2
localcmd                                             366     364      -2
expmeta                                              521     517      -4
redirect                                            1210    1135     -75
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 1/3 up/down: 2/-81)             Total: -79 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-25 17:47:48 +02:00
Denys Vlasenko
2990aa45d1 ash: sync up with dash with respect to redirection escaping
We fixed the problem differently than they. Let's not deviate.

Upstream commit:

    Date: Thu, 27 May 2010 20:07:29 +1000
    [EXPAND] Fix corruption of redirections with byte 0x81

    In other ash variants, a partial implementation of ksh-like cmd >file*
    adds and removes CTLESC bytes ('\x81') in redirection filenames,
    preserving 8-bit transparency. Long ago, dash removed the code to add
    the CTLESC bytes, but not the code to remove them, causing corruption of
    filenames containing CTLESC. This commit removes the code to remove the
    CTLESC bytes.

    The CTLESC byte occurs frequently in UTF-8 encoded non-Latin text.

    This bug has been reported various times to Ubuntu and Debian (e.g.
    Launchpad Ubuntu #422298). This patch is the same as the one submitted
    by Alexander Korolkov in Ubuntu #422298.

    Signed-off-by: Jilles Tjoelker <jilles@stack.nl>
    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

function                                             old     new   delta
changepath                                           194     192      -2
expandarg                                           1000     984     -16
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-18)             Total: -18 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-25 17:37:57 +02:00
Denys Vlasenko
b28d4c3462 ash: [VAR] Move unsetvar functionality into setvareq
Upstream commit:

    Date: Tue, 25 May 2010 20:55:05 +0800
    [VAR] Move unsetvar functionality into setvareq

    This patch moves the unsetvar code into setvareq so that we can
    no have a pathological case of an unset variable hanging around
    unless it has a bit pinning it like VEXPORT.

    Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>

function                                             old     new   delta
setvareq                                             227     303     +76
expmeta                                              517     521      +4
localcmd                                             364     366      +2
unsetcmd                                              96      76     -20
unsetvar                                             129       7    -122
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 3/2 up/down: 82/-142)           Total: -60 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-25 16:29:36 +02:00
Denys Vlasenko
be669fa1fd ash: import param_expand_default.tests from hush
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-25 15:25:07 +02:00
Denys Vlasenko
645c697372 hush: treat ${#?} as "length of $?"
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-25 15:18:57 +02:00
Denys Vlasenko
64925384c9 ash: add a few tests from hush-vars/*
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-25 14:55:05 +02:00
Denys Vlasenko
5c123ac208 ash: fix comment, no code changes
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-24 20:03:24 +02:00
Denys Vlasenko
8d2191c6ae ash: copy three tests from hush_test/hush-signals/*
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-24 19:42:46 +02:00
Denys Vlasenko
ca50caacad shell: some additions to *sh-misc/* tests
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-24 18:51:40 +02:00
Denys Vlasenko
4a1d8f6d6e ash: add most of hush process subst tests
ash passes these.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-24 12:49:49 +02:00
Denys Vlasenko
621fc50e83 hush: fix a case when redirect to a closed fd #1 is not restoring (closing) it
function                                             old     new   delta
setup_redirects                                      200     245     +45
append_squirrel                                        -      41     +41
save_fds_on_redirect                                 256     221     -35
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 1/1 up/down: 86/-35)             Total: 51 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-24 12:42:17 +02:00
Denys Vlasenko
e34dbc4fdc ash: add all hush parsing tests to ast tests
All pass.

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-24 02:49:56 +02:00
Denys Vlasenko
0675b03de4 hush: use mempcpy where useful
function                                             old     new   delta
o_addblock                                            58      42     -16
expand_one_var                                      1618    1555     -63
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 0/2 up/down: 0/-79)             Total: -79 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-24 02:17:05 +02:00
Denys Vlasenko
94af83eb8d ash: fix for last commit
"mempcpy(q, s, len) + len" is obviously no good :(

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-23 21:55:40 +02:00
Denys Vlasenko
5ace96a713 ash: use mempcpy() in more places
Most changes are taken from dash.

function                                             old     new   delta
single_quote                                         127     129      +2
stack_nputstr                                         28      29      +1
path_advance                                         209     202      -7
rmescapes                                            346     308     -38
------------------------------------------------------------------------------
(add/remove: 0/0 grow/shrink: 2/2 up/down: 3/-45)             Total: -42 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-23 21:46:34 +02:00
Denys Vlasenko
da2244fe48 ash: use mempcpy() where appropriate
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-21 18:51:29 +02:00
Denys Vlasenko
0eed355eac ash: suppress readonly1.tests false positive; add readonly0.tests
Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-21 13:37:25 +02:00
Denys Vlasenko
42ba757d5e ash: improve set -x to quote strings as necessary
Basen on the patch from Martijn Dekker <martijn@inlv.org>

function                                             old     new   delta
evalcommand                                         1161    1302    +141
maybe_single_quote                                     -      60     +60
getoptscmd                                           527     546     +19
readtoken1                                          2819    2823      +4
localcmd                                             366     364      -2
evaltreenr                                           495     479     -16
evaltree                                             495     479     -16
------------------------------------------------------------------------------
(add/remove: 1/0 grow/shrink: 3/3 up/down: 224/-34)           Total: 190 bytes

Signed-off-by: Denys Vlasenko <vda.linux@googlemail.com>
2017-07-21 13:20:14 +02:00