summaryrefslogtreecommitdiffstats
path: root/bin/sh/parser.c
Commit message (Collapse)AuthorAgeFilesLines
* sh: Remove special support for background simple commands.jilles2011-06-181-3/+2
| | | | It expands the arguments in the parent shell process, which is incorrect.
* sh: Add case statement fallthrough (with ';&' instead of ';;').jilles2011-06-171-4/+10
| | | | | | | | | | | | Replacing ;; with the new control operator ;& will cause the next list to be executed as well without checking its pattern, continuing until a list ends with ;; or until the end of the case statement. This is like omitting "break" in a C "switch" statement. The sequence ;& was formerly invalid. This feature is proposed for the next POSIX issue in Austin Group issue #449.
* sh: Do parameter expansion before printing PS4 (set -x).jilles2011-06-091-0/+44
| | | | | | | | | | | | | | | | The function name expandstr() and the general idea of doing this kind of expansion by treating the text as a here document without end marker is from dash. All variants of parameter expansion and arithmetic expansion also work (the latter is not required by POSIX but it does not take extra code and many other shells also allow it). Command substitution is prevented because I think it causes too much code to be re-entered (for example creating an unbounded recursion of trace lines). Unfortunately, our LINENO is somewhat crude, otherwise PS4='$LINENO+ ' would be quite useful.
* sh: Expand aliases after assignments and redirections.jilles2011-05-211-0/+22
|
* sh: Allow terminating a heredoc with a terminator at EOF without a newline.jilles2011-05-201-3/+5
| | | | | | | | | | | | | | | | | | | | | | This is sometimes used with eval or old-style command substitution, and most shells other than ash derivatives allow it. It can also be used with scripts that violate POSIX's requirement on the application that they end in a newline (scripts must be text files except that line length is unlimited). Example: v=`cat <<EOF foo EOF` echo $v This commit does not add support for the similar construct with new-style command substitution, like v=$(cat <<EOF foo EOF) This continues to require a newline after the terminator.
* sh: Add \u/\U support (in $'...') for UTF-8.jilles2011-05-081-0/+23
| | | | | | | | | | Because we have no iconv in base, support for other charsets is not possible. Note that \u/\U are processed using the locale that was active when the shell started. This is necessary to avoid behaviour that depends on the parse/execute split (for example when placing braces around an entire script). Therefore, UTF-8 encoding is implemented manually.
* sh: Add $'quoting' (C-style escape sequences).jilles2011-05-051-6/+139
| | | | | | | | | | | | | | | | | | | | | | | | A string between $' and ' may contain backslash escape sequences similar to the ones in a C string constant (except that a single-quote must be escaped and a double-quote need not be). Details are in the sh(1) man page. This construct is useful to include unprintable characters, tabs and newlines in strings; while this can be done with a command substitution containing a printf command, that needs ugly workarounds if the result is to end with a newline as command substitution removes all trailing newlines. The construct may also be useful in future to describe unprintable characters without needing to write those characters themselves in 'set -x', 'export -p' and the like. The implementation attempts to comply to the proposal for the next issue of the POSIX specification. Because this construct is not in POSIX.1-2008, using it in scripts intended to be portable is unwise. Matching the minimal locale support in the rest of sh, the \u and \U sequences are currently not useful. Exp-run done by: pav (with some other sh(1) changes)
* sh: Detect an error for ${#var<GARBAGE>}.jilles2011-05-041-0/+2
| | | | | | | | | In particular, this makes things like ${#foo[0]} and ${#foo[@]} errors rather than silent equivalents of ${#foo}. PR: bin/151720 Submitted by: Mark Johnston Exp-run done by: pav (with some other sh(1) changes)
* sh: Do not word split "${#parameter}".jilles2011-04-201-2/+2
| | | | | | | | | | | | | This is only a problem if IFS contains digits, which is unusual but valid. Because of an incorrect fix for PR bin/12137, "${#parameter}" was treated as ${#parameter}. The underlying problem was that "${#parameter}" erroneously added CTLESC bytes before determining the length. This was properly fixed for PR bin/56147 but the incorrect fix was not backed out. Reported by: Seeker on forums.freebsd.org MFC after: 2 weeks
* sh: Fix some parameter expansion variants ${#...}.jilles2011-03-131-19/+30
| | | | | | | | | These already worked: $# ${#} ${##} ${#-} ${#?} These now work as well: ${#+word} ${#-word} ${##word} ${#%word} There is an ambiguity in the standard with ${#?}: it could be the length of $? or it could be $# giving an error in the (impossible) case that it is not set. We continue to use the former interpretation as it seems more useful.
* sh: Fix two things about {(...)} <redir:jilles2011-02-051-1/+4
| | | | | | | | * In {(...) <redir1;} <redir2, do not drop redir1. * Maintain the difference between (...) <redir and {(...)} <redir: In (...) <redir, the redirection is performed in the child, while in {(...)} <redir it should be performed in the parent (like {(...); :;} <redir)
* sh: Allow arbitrary large numbers in CHECKSTRSPACE.jilles2010-12-261-2/+1
| | | | | | Reduce "stack string" API somewhat and simplify code. Add a check for integer overflow of the "stack string" length (probably incomplete).
* Remove duplicate check, turning dead code into live code.uqs2010-12-131-2/+0
| | | | | Coverity CID: 5114 Reviewed by: jilles
* sh: Code size optimizations to "stack string" memory allocation:jilles2010-11-231-7/+6
| | | | | | | | * Prefer one CHECKSTRSPACE with multiple USTPUTC to multiple STPUTC. * Add STPUTS macro (based on function) and use it instead of loops that add nul-terminated strings to the stack string. No functional change is intended, but code size is about 1K less on i386.
* sh: Fix some issues with aliases and case, by importing dash checkkwd code.jilles2010-11-021-50/+49
| | | | | | | | | | | This moves the function of the noaliases variable into the checkkwd variable. This way it is properly reset on errors and aliases can be used normally in the commands for each case (the case labels recognize the keyword esac but no aliases). The new code is clearer as well. Obtained from: dash
* sh: Use iteration instead of recursion to evaluate semicolon lists.jilles2010-10-311-10/+17
| | | | | This reduces CPU and memory usage when executing long lists (such as long functions).
* sh: Tweak some string constants to reduce code size.jilles2010-10-291-1/+1
| | | | | * Reduce some needless differences. * Shorten some error messages that should not happen.
* sh: Reject function names ending in one of !%*+-=?@}~jilles2010-10-291-1/+5
| | | | | | | | | These do something else in ksh: name=(...) is an array or compound variable assignment and the others are extended patterns. This is the last patch of the ones tested in the exp run. Exp-run done by: pav (with some other sh(1) changes)
* sh: Detect various additional errors in the parser.jilles2010-10-291-3/+3
| | | | | | | | | | | | | Apart from detecting breakage earlier or at all, this also fixes a segfault in the testsuite. The "handling" of the breakage left an invalid internal representation in some cases. Examples: echo a; do echo b echo `) echo a` echo `date; do do do` Exp-run done by: pav (with some other sh(1) changes)
* sh: Error out on various specials/keywords in the wrong place in backticks.jilles2010-10-291-16/+16
| | | | | | | | Example: echo `date)` Exp-run done by: pav (with some other sh(1) changes) Obtained from: NetBSD (Christos Zoulas, NetBSD PR 11317)
* sh: Do IFS splitting on word in ${v+word} and ${v-word}.jilles2010-10-291-4/+12
| | | | | | | | | | | | | | | | | The code is inspired by NetBSD sh somewhat, but different because we preserve the old Almquist/Bourne/Korn ability to have an unquoted part in a quoted ${v+word}. For example, "${v-"*"}" expands to $v as a single field if v is set, but generates filenames otherwise. Note that this is the only place where we split text literally from the script (the similar ${v=word} assigns to v and then expands $v). The parser must now add additional markers to allow the expansion code to know whether arbitrary characters in substitutions are quoted. Example: for i in ${$+a b c}; do echo $i; done Exp-run done by: pav (with some other sh(1) changes)
* sh: Only accept a '}' inside ${v+-=?...} if double-quote state matches.jilles2010-10-281-4/+4
| | | | | | | | | | | | | If double-quote state does not match, treat the '}' literally. This ensures double-quote state remains the same before and after a ${v+-=?...} which helps with expand.c. It makes things like ${foo+"\${bar}"} which I have seen in the wild work as expected. Exp-run done by: pav (with some other sh(1) changes)
* sh: Make double-quotes quote a '}' inside ${v#...} and ${v%...}.jilles2010-10-281-1/+2
| | | | | Exp-run done by: pav (with some other sh(1) changes) PR: bin/57554
* sh: Ignore double-quotes in arithmetic rather than treating them as quotes.jilles2010-10-241-4/+3
| | | | | | | | This provides similar behaviour, but allows a simpler parser. This changes r206473. Exp-run done by: pav (with some other sh(1) changes)
* sh: Do not allow overriding a special builtin with a function.jilles2010-10-241-0/+5
| | | | | | | | | | | | | | | | | This is a syntax error. POSIX does not say explicitly whether defining a function with the same name as a special builtin is allowed, but it does say that it is impossible to call such a function. A special builtin can still be overridden with an alias. This commit is part of a set of changes that will ensure that when something looks like a special builtin to the parser, it is one. (Not the other way around, as it remains possible to call a special builtin named by a variable or other substitution.) Exp-run done by: pav (with some other sh(1) changes)
* sh: Make sure defined functions can actually be called.jilles2010-10-241-3/+7
| | | | | | | | | | | | | | | | | Add some conservative checks on function names: - Disallow expansions or quoting characters; these can only be called via strange control characters - Disallow '/'; these functions cannot be called anyway, as exec.c assumes they are pathnames - Make the CTL* bytes work properly in function names. These are syntax errors. POSIX does not require us to support more than names (letters, digits and underscores, not starting with a digit), but I do not want to restrict it that much at this time. Exp-run done by: pav (with some other sh(1) changes)
* sh: Change ! within a pipeline to start a new pipeline instead.jilles2010-10-241-19/+11
| | | | | | | | | | | | | | This is how ksh93 treats ! within a pipeline and makes the ! in a | ! b | c negate the exit status of the pipeline, as if it were a | { ! b | c; } Side effect: something like f() ! a is now a syntax error, because a function definition takes a command, not a pipeline. Exp-run done by: pav (with some other sh(1) changes)
* In the spirit of r90111, depend on c89 and remove the "STATIC" macroobrien2010-10-131-44/+44
| | | | and its usage.
* Consistently use "STATIC" for all functions in order to be able to setobrien2010-10-131-13/+14
| | | | | | breakpoints with in a debugger. And use naked "static" for variables. Noticed by: bde
* sh: Add __dead2 to two functions that do not return.jilles2010-09-121-2/+2
| | | | | Apart from helping static analyzers, this also appears to reduce the size of the binary slightly.
* sh: Fix crash due to uninitialized here-document.jilles2010-07-251-0/+3
| | | | | | | | | | | | | | | If an ; or & token was followed by an EOF token, pending here-documents were left uninitialized. Execution would crash, either in the main shell process for literal here-documents or in a child process for expanded here-documents. In the latter case the problem is hard to detect apart from the core dumps and log messages. Side effect: slightly different retries on inputs where EOF is not persistent. Note that tools/regression/bin/sh/parser/heredoc6.0 still causes a similar crash in a child process. The text passed to eval is malformed and should be rejected.
* sh: Allow a background command consisting solely of redirections.jilles2010-07-181-0/+1
| | | | | | | Example: </dev/null & MFC after: 2 weeks
* sh: There cannot be a TNOT in simplecmd(), remove checks.jilles2010-07-141-19/+3
| | | | | | simplecmd() only handles simple commands and function definitions, neither of which involves the ! keyword. The initial token on entry to simplecmd() is one of the following: TSEMI, TAND, TOR, TNL, TEOF, TWORD, TRP.
* sh: Use $PWD instead of getcwd() for the \w and \W prompt expansions.jilles2010-07-021-12/+11
| | | | | | This ensures that the logical working directory (which may include symlinks) is shown and is similar to the default behaviour of the pwd builtin.
* sh: Fix compilation with -DNO_HISTORY.jilles2010-06-191-0/+1
| | | | | | | | | | | The LINENO code uses snprintf() and relied on "myhistedit.h" to pull in the necessary <stdio.h>. Compiling with -DNO_HISTORY disables all editing and history support and allows linking without -ledit -ltermcap. This may be useful for embedded systems. MFC after: 2 weeks
* sh: Fix a crash if a heredoc was not properly ended and parsing continued.jilles2010-05-301-0/+1
| | | | | | | | | Example (in interactive mode): cat <<EOF && ) The next command typed caused sh to segfault, because the state for the here document was not reset. Like parser_temp, this uses the fact that the parser is not re-entered.
* sh: Change interaction of command substitution and here documents.jilles2010-05-301-4/+13
| | | | | | | | | | | | | | | | | If a command substitution contains a newline token, this no longer starts here documents of outer commands. This way, we follow POSIX's idea of the command substitution being a separate script more closely. It also matches other shells better and is consistent with newline characters in quotes not starting here documents. The extension tested in parser/heredoc3.0 ($(cat <<EOF)\ntext\nEOF\n) continues to be supported. In particular, this change allows things like cat <<EOF && echo `pwd` (a `` command substitution after a here document) which formerly silently used an empty file as the here document, because the EOF of the inner command "pwd" also forced an empty here document.
* sh: Partially revert r206146, allowing double-quotes in arithmetic.jilles2010-04-111-1/+4
| | | | | | | | These do pretty much nothing (except that parentheses are ignored), but people seem to use them and allowing them does not hurt much. Single-quotes seem not to be used and cause silently different behaviour with ksh93 character constants.
* sh: Remove special handling for ' and " in arithmetic.jilles2010-04-031-4/+1
| | | | | | | They will be treated like normal characters, resulting in a runtime arithmetic expression error. Exp-run done by: erwin (with some other sh(1) changes)
* sh: Fix various things about expansions:jilles2010-04-031-73/+208
| | | | | | | | | | | | | | | | | | | | * remove the backslash from \} inside double quotes inside +-=? substitutions, e.g. "${$+\}a}" * maintain separate double-quote state for ${v#...} and ${v%...}; single and double quotes are special inside, even in a double-quoted string or here document * keep track of correct order of substitutions and arithmetic This is different from dash's approach, which does not track individual double quotes in the parser, trying to fix this up during expansion. This treats single quotes inside "${v#...}" incorrectly, however. This is similar to NetBSD's approach (as submitted in PR bin/57554), but recognizes the difference between +-=? and #% substitutions hinted at in POSIX and is more refined for arithmetic expansion and here documents. PR: bin/57554 Exp-run done by: erwin (with some other sh(1) changes)
* sh: Treat unexpected newlines in substitutions as a syntax error.jilles2010-04-031-0/+4
| | | | | | | The old approach was wrong because PS2 was not used and seems unlikely to parse extensions (ksh93's ${ COMMAND} may well fail to parse). Exp-run done by: erwin (with some other sh(1) changes)
* sh: Fix longjmp clobber warnings in parser.c.jilles2010-03-131-166/+162
| | | | | | Make parsebackq a function instead of an emulated nested function. This puts the setjmp usage in a smaller function where it is easier to avoid bad optimizations.
* Fix expansion of \W in prompt strings when the working directory is "/".jh2010-02-241-1/+1
| | | | | | | | The prompt string was truncated after \W when the working directory was "/". PR: bin/89410 Submitted by: Dr Balwinder Singh Dheeman MFC after: 1 week
* sh: Fix some bugs with backquoted builtins:jilles2010-01-011-2/+2
| | | | | | | | | | | | - correctly handle error output in $(builtin 2>&1), clarify out1/out2 vs output/errout in the code - treat all builtins as regular builtins so errors do not abort the shell and variable assignments do not persist - respect the caller's INTOFF Some bugs still exist: - expansion errors may still abort the shell - some side effects of expansions and builtins persist
* Fix memory leak when parsing backticks (``).jilles2009-12-301-13/+23
|
* sh: Various warning fixes (from WARNS=6 NO_WERROR=1):jilles2009-12-271-4/+5
| | | | | | | - const - initializations to silence -Wuninitialized (it was safe anyway) - remove nested extern declarations - rename "index" locals to "idx"
* sh: Constify various strings.jilles2009-12-241-2/+2
| | | | | Most of this is adding const keywords, but setvar() in var.c had to be changed somewhat more.
* Fix various things about SIGINT handling:jilles2009-11-221-1/+1
| | | | | | | | | | | | | | | | | | * exception handlers are now run with interrupts disabled, which avoids many race conditions * fix some cases where SIGINT only aborts one command and continues the script, in particular if a SIGINT causes an EINTR error which trumped the interrupt. Example: sh -c 'echo < /some/fifo; echo This should not be printed' The fifo should not have writers. When pressing ctrl+c to abort the open, the shell used to continue with the next command. Example: sh -c '/bin/echo < /some/fifo; echo This should not be printed' Similar. Note, however, that this particular case did not and does not work in interactive mode with job control enabled.
* sh: Some changes to stderr flushing:jilles2009-11-211-0/+3
| | | | | | | | | * increase buffer size from 100 to 256 bytes * remove implied flush from out2str(), in particular this avoids unnecessary flushing in the middle of a -x tracing line * rename dprintf() to out2fmt_flush(), make it flush out2 and use this function in various places where flushing is desired after an error message
* sh: Allow a newline before "in" in a for command, as required by POSIX.jilles2009-11-141-1/+3
|
OpenPOWER on IntegriCloud