summary refs log tree commit diff
path: root/src/input.h (unfollow)
Commit message (Collapse)Author
2016-06-06Release 0.5.9.Herbert Xu
2015-08-13builtin: Reset t_wp_op in testcmdHerbert Xu
The global variable t_wp_op needs to be reset every time testcmd is called or it may cause incorrect parsing of the arguments. Reported-by: Martijn Dekker <martijn@inlv.org> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2015-06-11man: Fix description of getopts when last argument reachedFelix Dietrich
The description of getops in the manual incorrectly states that var will be set to "--" when no arguments remain. In fact it will be set to "?". Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2015-06-11builtin: Clear LC_ALL in mkbuiltinsFredrik Fornwall
In mkbuiltins LC_COLLATE is set, but since "The value of the LC_ALL environment variable has precedence over any of the other environment variables starting with LC_" (http://pubs.opengroup.org/onlinepubs/7908799/xbd/envvar.html), this has no effect when LC_ALL is set. This breaks when having e.g. LC_ALL=en_US.UTF-8 during make, which causes the test case dash -c : to fail, probably due to broken ordering in builtins.c. The patch corrects that by clearing LC_ALL. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2015-01-05input: Allow two consecutive calls to pungetcHerbert Xu
The commit ef91d3d6a4c39421fd3a391e02cd82f9f3aee4a8 ([PARSER] Handle backslash newlines properly after dollar sign) created cases where we make two consecutive calls to pungetc. As we don't explicitly support that there are corner cases where you end up with garbage input leading to undefined behaviour. This patch adds explicit support for two consecutive calls to pungetc. Reported-by: Jilles Tjoelker <jilles@stack.nl> Reported-by: Juergen Daubert <jue@jue.li> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2015-01-05input: Move all input state into parsefileHerbert Xu
Currently we maintain a copy of the input state outside of parsefile. This is redundant and makes reentrancy difficult. This patch kills the duplicate global states and now everyone simply uses parsefile. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2015-01-05input: Remove HETIOHerbert Xu
It hasn't been possible to build HETIO for over ten years. So let's just kill it. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2015-01-05input: Make preadbuffer staticHerbert Xu
The function preadbuffer should be static as it's only used in input.c. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2015-01-05expand: Fixed "$@" expansion when EXP_FULL is falseHerbert Xu
The commit 3c06acdac0b1ba0e0acdda513a57ee6e31385dce ([EXPAND] Split unquoted $@/$* correctly when IFS is set but empty) broke the case where $@ is in quotes and EXP_FULL is false. In that case we should still emit IFS as field splitting is not performed. Reported-by: Juergen Daubert <jue@jue.li> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-12-26builtin: create builtins.c properly on old cppkabe@sra-tohoku.co.jp
Encontered this on ancient gcc-2.95.3 environment; src/builtins.def.in -> src/builtins.def generation emitted ^ $ lines (likely by /* */), which where NOT ignored by src/mkbuiltins and generating bogus builtins.c. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-12-26man: Clarify two redirection mechanismsStéphane Aulery
Close Debian Bug #501566 Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-11-17[BUILTIN] Fix "test -x" as root on FreeBSD 8Jonathan Nieder
POSIX.1-2008 §4.4 "File Access Permission" sayeth: If execute permission is requested, access shall be granted if execute permission is granted to at least one user by the file permission bits or by an alternate access control mechanism; otherwise, access shall be denied. For historical reasons, POSIX unfortunately also allows access() and faccessat() to return success for X_OK if the current process is privileged, even when the above condition is not fulfilled and actual execution would fail. On the affected platforms, "test -x <path>" as root started returning true on nonexecutable files when dash switched from its own emulation to the true faccessat in v0.5.7~54 (2010-04-02). Work around this by checking the permissions bits when mode == X_OK and geteuid() == 0 on such platforms. Unfortunately the behavior seems to vary from one kernel version to another, so we cannot just check the behavior at compile time and rely on that. A survey of some affected kernels: - NetBSD's kernel moved to the sane semantics in 1997 - OpenBSD's kernel made the same change in version 4.4, three years ago - FreeBSD 9's kernel fixes this but hasn't been released yet It seems safe to only apply the workaround on systems using the FreeBSD kernel for now, and to push for standardization on the expected access()/faccessat() semantics so we can drop the workaround altogether in a few years. To try it on other platforms, use "./configure --enable-test-workaround". Reported-by: Christoph Egger <christoph@debian.org> Analysis-by: Petr Salinger <Petr.Salinger@seznam.cz> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-11-17[MAN] Document redirection file descriptor limitationStéphane Aulery
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-11-17[MAN] Correct typo in manual pageStéphane Aulery
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-30[PARSER] Catch variable length expansions on non-existant specialsHerbert Xu
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>
2014-10-28[PARSER] Simplify EOF/newline handling in list parserHerbert Xu
This patch simplifies the EOF and new handling in the list parser. In particular, it eliminates a case where we may leave here-documents unfinished upon EOF. It also removes special EOF/newline handling from parsecmd. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-28[PARSER] Removed unnecessary pungetc on EOF from parserHerbert Xu
Doing a pungetc on an EOF is a noop and is only useful when we don't know what character we're putting back. This patch removes an unnecessary pungetc when we know it's EOF. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-27[BUILTIN] Handle -- in dotcmdHerbert Xu
This patch adds a nextopt call in dotcmd in order to handle --. Reported-by: Stephane Chazelas <stephane_chazelas@yahoo.fr> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-27[BUILTIN] Simplify echo commandHerbert Xu
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-27[BUILTIN] Optimise handling of backslash octals in printfHerbert Xu
This patch removes the duplicate octal handling for %b by reusing the existing code in conv_escape. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-27[BUILTIN] Use error instead of warnx for fatal errors in printfHerbert Xu
This patch replaces uses of warnx where we abort with error since the effect is the same. The exit status however changes from 1 to 2. Non-fatal errors where we continue are unchanged. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-27[BUILTIN] Remove getintmax in printfHerbert Xu
This patch removes getintmax and moves its functionality into getuintmax in order to reduce code duplication. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-27[BUILTIN] Remove unnecessary restoration of format string in printfHerbert Xu
Currently we try to preserve the format string which is stored in argv after temporarily modifying it. This is unnecessary as it's only ever used once. This patch gets rid of it. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-27[OUTPUT] Add ifdefs around MEM_OUT handling in outmemHerbert Xu
MEM_OUT is only used by forkless backtick processing which we do not currently support. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-27[OUTPUT] Add likely tag in outmemHerbert Xu
The branch in outmem where the string fits in the buffer is the common case and is now marked as likely. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-27[INPUT] Replace open-coded flushall in preadbufferHerbert Xu
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-27[BUILTIN] Handle embedded NULs correctly in printfHerbert Xu
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=379227 On Sat, Jul 22, 2006 at 12:48:38PM +0200, A Mennucc wrote: > Package: dash > Version: 0.5.3-3 > Severity: normal > > hi > > here are the examples > > $ bash -c 'echo -n -e "A\0102C\00D\0E" | hexdump -c' > 0000000 A B C \0 D \0 E > 0000007 > > $ /bin/echo -n -e "A\0102C\00D\0E" | hexdump -c > 0000000 A B C \0 D \0 E > 0000007 > > $ zsh -c 'echo -n -e "A\0102C\00D\0E" | hexdump -c' > 0000000 A B C \0 D \0 E > 0000007 > > $ dash -c 'echo -n "A\0102C\00D\0E" | hexdump -c' > 0000000 A B C > 0000003 > > and also > > $ dash -c 'echo -n "ABC\0DEFGH" | hexdump -c' > 0000000 A B C > 0000003 > > As you see, dash 's builtin echo truncates the output at the first \0 > > a. > > -- System Information: > Debian Release: testing/unstable > APT prefers unstable > APT policy: (500, 'unstable'), (500, 'testing') > Architecture: i386 (i686) > Shell: /bin/sh linked to /bin/bash > Kernel: Linux 2.6.16-1-k7 > Locale: LANG=it_IT.UTF-8, LC_CTYPE=it_IT.UTF-8 (charmap=UTF-8) > > Versions of packages dash depends on: > ii libc6 2.3.6-15 GNU C Library: Shared libraries > > dash recommends no packages. > > -- debconf information: > * dash/sh: false > > -- > Andrea Mennucc > "E' un mondo difficile. Che vita intensa!" (Tonino Carotone) This patch fixes handling of embedded NULs using an approach similar to the one taken by NetBSD. In particular, we first determine the length of the output string, and then use a sequence of Xs of the same length as input to the underlying C printf to determine the amount of leading and trailing padding. Finally we replace the Xs with the actual string before writing it out. In order to print out the temporary string containing Xs and padding, a new helper xasprintf is added. Unlike asprintf though, our xasprintf prints to the ash stack rather than using straight malloc memory. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-27[BUILTIN] Add printf support for format string a, A, and FHerbert Xu
This patch adds the format string characters a, A and F to the supported set of the built-in printf command. They're already supported by the underlying printf function. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-27[CD] support drive letters on CygwinEric Blake
The Cygwin platform supports DOS style drive-letter paths such as "C:\\dir", even though the preferred form is a POSIX-style "/cygdrive/c/dir". This can be seen by doing things such as chdir("c:") (which succeeds) followed by getcwd(NULL, 0) (which returns the normalized "/cygdrive/c"). However, dash was trying to perform local manipulations on the argument to 'cd' prior to calling into libc, in order to update the state of $PWD and friends; these manipulations were assuming that the user meant to change to a relative subdirectory of the current location, as in './c:', instead of honoring the drive letter. None of the other dash builtins take a filename and manipulate it to affect shell state (some, like 'test', take a file name, but as stat("c:") works just fine, there is no need to normalize). This patch has no impact outside of cygwin; on cygwin, it takes advantage of a native function call to canonicalize any incoming name into preferred form before updating shell state. Pre-patch: $ dash -c 'cd c: && echo $PWD' dash: 1: cd: can't cd to c: Post-patch: $ dash -c 'cd c: && echo $PWD' /cygdrive/c Signed-off-by: Eric Blake <eblake@redhat.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-08[EXPAND] Optimise nulonly away and just use quoted as beforeHerbert Xu
This patch makes a small optimisation by using the same value for quoted between evalvar and varvalue by eliminating nulonly and passing along quoted instead. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-08[EXPAND] Do not split quoted VSLENGTH and VSTRIMHerbert Xu
Currently VSLENGTH and VSTRIM* are field-split even within quotes. This is obviously wrong. This patch fixes that. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-08[EXPAND] Split unquoted $@/$* correctly when IFS is set but emptyHerbert Xu
Currently we do not field-split $@/$* when it isn't quoted and IFS is set but empty. This is obviously wrong. This patch fixes this. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-07[VAR] Use setvareq to set OPTIND initiallyHerbert Xu
There is no need to setvarint to set the initial value of OPTIND of one. This patch switchs to setvareq which also lets us avoid an unnecessary memory allocation. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-06[BUILTIN] Return without arguments in a trap should use status outside trapsHerbert Xu
POSIX now requires that return without arguments in a trap should return the last command status prior to executing traps. This patch implements this behaviour. Incidentally this also changes the behaviour of return without arguments in a loop conditional to use the last exit status in the body as opposed to the last command in the conditional when there is one. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-06[BUILTIN] Allow return in loop conditional to set exit statusHerbert Xu
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=332954 When return is used in a loop conditional the exit status will be lost because we always set the exit status at the end of the loop to that of the last command executed in the body. This is counterintuitive and contrary to what most other shells do. This patch fixes this by always preserving the exit status of return when it is used in a loop conditional. The patch was originally written by Gerrit Pape <pape@smarden.org>. Reported-by: Stephane Chazelas <stephane_chazelas@yahoo.fr> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-06[EVAL] Move common skipcount logic into skiploopHerbert Xu
The functions evalloop and evalfor share the logic on checking and updating skipcount. This patch moves that into the helper function skiploop. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-06[BUILTIN] Do not allow break to break across function callsHerbert Xu
As it is if you do a multi-level break inside a function it'll actually include loops outside of the function call. This is counterintuitive. This patch changes this by saving and resetting loopnest when entering a function. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-06[BUILTIN] Exit without arguments in a trap should use status outside trapsHerbert Xu
POSIX now requires that exit without arguments in a trap should return the last command status prior to executing traps. This patch implements this behaviour. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-03[EVAL] Do not clobber exitstatus in evalcommandHerbert Xu
All originators of EXERROR have been setting the exitstatus for a while now. So it is no longer appropriate to set it explicitly in evalcommand. In fact doing so may cause the original exitstatus to be lost. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-02[ERROR] Set exitstatus in onintHerbert Xu
Currently the exit status when we receive SIGINT is set in evalcommand which means that it doesn't always get set. For example, if you press CTRL-C at the prompt of an interactive dash, the exit status is not set to 130 as it is in many other Bourne shells. This patch fixes this by moving the setting of the exit status into onint which also simplifies evalcommand. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-02[TRAP] Make sure evalskip is zero before running trapsHerbert Xu
As it is if dotrap is called with evalskip set to a nonzero value, it'll try to execute any set traps. The result is that the first command in the first set trap will be executed while the rest of the trap will be silently ignored due to evalskip. This is highly counterintuitive, even though both bash and ksh exhibit a similar behaviour. This patch fixes it by skipping trap processing if evalskip is set on entry. It also adds a dotrap call to the top of evaltree to ensure that while continue; do continue; done has a chance of running traps. Finally the pendingsigs check is moved into dotrap for compactness. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-10-02[EVAL] Fix use-after-free in dotrap/evalstringHerbert Xu
The function dotrap calls evalstring using the stored trap string. If evalstring then unsets that exact trap string then we will end up using freed memory. This patch fixes it by making evalstring always duplicate the string before using it. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-09-29[PARSER] Add nlprompt/nlnoprompt helpersHerbert Xu
This patch adds the nlprompt/nlnoprompt helpers to isolate code dealing with newlines and prompting. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-09-29[PARSER] Handle backslash newlines properly after dollar signHerbert Xu
On Tue, Aug 26, 2014 at 12:34:42PM +0000, Eric Blake wrote: > On 08/26/2014 06:15 AM, Oleg Bulatov wrote: > > Hi! > > > > While playing with sh generators I found that dash and bash have different > > interpretations for <slash><newline> sequence. > > > > $ dash -c 'EDIT=xxx; echo $EDIT\ > >> OR' > > xxxOR > > Buggy. > > > $ bash -c 'EDIT=xxx; echo $EDIT\ > > OR' > > /usr/bin/vim > > Correct behavior. > > > > > $ dash -c 'echo "$\ > > (pwd)"' > > $(pwd) > > > > Is it undefined behaviour in POSIX? > > No, it's well-defined, and dash is buggy. POSIX says: > > http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_03 > > "the shell shall break its input into tokens by applying the first > applicable rule below to the next character in its input" > > Rule 4 covers backslash handling, while rule 5 covers locating the end > of a word to be subject to $ expansion. Therefore, rule 4 should happen > first. Rule 4 defers to the section on quoting, with the caveat that > <newline> joining is the only substitution that happens immediately as > part of the parsing: > > http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_02 > > "If a <newline> follows the <backslash>, the shell shall interpret this > as line continuation. The <backslash> and <newline> shall be removed > before splitting the input into tokens. Since the escaped <newline> is > removed entirely from the input and is not replaced by any white space, > it cannot serve as a token separator." > > So the fact that dash is treating the elided backslash-newline as a > token separator, and parsing your input as if ${EDIT}OR instead of > ${EDITOR} is a bug in dash. I agree. This patch should resolve this problem and similar ones affecting blackslash newlines after we encounter a dollar sign. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-09-29[INPUT] Kill pgetc_macroHerbert Xu
pgetc_macro is identical to pgetc except that it's a macro and pgetc isn't. Since there is very little performance difference on modern systems it's time to kill pgetc_macro. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-09-28[BUILTIN] Correctly handle test ! ! = !Herbert Xu
This patch adds a special case in testcmd for the 4-argument expression beginning with a !. Without this ! ! = ! is deemed a syntax error, which breaks POSIX. Note that this special case does not extend down into subexpressions so if ! ! = ! is used inside parentheses then a syntax error will still occur as before. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-09-26Release 0.5.8.Herbert Xu
2014-09-26[MAN] Clarify "trap '' signals" syntax in manual pageJonathan Nieder
When the manpage states | <action> may be null, which cause the specified signals to be ignored. it is not immediately obvious what it means for an action to be null. Clarify by explicitly referring to an empty string, as opposed to a NULL pointer or the string "null". Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-09-26[MAN] 0 and EXIT both work for exit trapsAdam Buchbinder
I saw a discussion in which there was some confusion over whether or not you can use a symbolic name, since the manpage doesn't specifically say so. Signed-off-by: Adam Buchbinder <adam.buchbinder@gmail.com> Signed-off-by: Jonathan Nieder <jrnieder@gmail.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
2014-09-26[MAN] Change characters for printf precision to bytesHerbert Xu
On Sun, Mar 09, 2014 at 11:11:43AM +0000, Jeroen van Dijke wrote: > > There seems to be a bug in the dash man page, at least in 0.5.7. It reads: > > Precision: > An optional period, `.', followed by an optional digit string giving a precision which specifies the number of digits to appear after the decimal point, for e and f formats, or the maximum number of *characters* to be printed from a string (b and s for- > mats); if the digit string is missing, the precision is treated as zero; > > dash behaves cuts to the number of bytes > > $ length=10; printf "%.${length}s\n" "eeeeeeeeeeeeeeeeeeeeeeeee" > eeeeeeeeee > $ length=10; printf "%.${length}s\n" "ëëëëëëëëëëëëëëëëëëëëëëëëë” > ëëëëë > > > The POSIX specification (2008) says: > > precision Gives the minimum number of digits to appear for the d, o, i, u, x, or X conversion specifiers (the field is padded with leading zeros), the number of digits to appear after the radix character for the e and f conversion specifiers, the maximum number of significant digits for the g conversion specifier; or the maximum number of *bytes* to be written from a string in the s conversion specifier. The precision shall take the form of a ( '.' ) followed by a decimal digit string; a null digit string is treated as zero. > > So it seems to me that “characters” should be changed to “bytes”. Indeed and this patch makes that change. Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>