summaryrefslogtreecommitdiffstats
path: root/bin/sh/parser.c
Commit message (Collapse)AuthorAgeFilesLines
* 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
|
* sh: Show more information about syntax errors in command substitution:jilles2009-10-161-0/+5
| | | | | | | the line number where the command substitution started. This applies to both the $() and `` forms but is most useful for `` because the other line number is relative to the enclosed text there. (For older versions, -v can be used as a workaround.)
* sh: Disallow mismatched quotes in backticks (`...`).jilles2009-10-011-7/+1
| | | | | | | | | Due to the amount of code removed by this, it seems that allowing unmatched quotes was a deliberate imitation of System V sh and real ksh. Most other shells do not allow unmatched quotes (e.g. bash, zsh, pdksh, NetBSD /bin/sh, dash). PR: bin/137657
* sh: Improve handling of setjmp/longjmp volatile:jilles2009-06-231-19/+1
| | | | | | | | | | | - remove ineffective and unnecessary (void) &var; [1] - remove some unnecessary volatile keywords - add a necessary volatile keyword - save the old handler before doing something that could use the saved value Submitted by: Christoph Mallon [1] Approved by: ed (mentor)
* correctly test for __GNUC__ macro (non-GCC compilers do not have it defined ↵rse2009-06-011-2/+2
| | | | at all)
* Parse 'cmd1 && ! cmd2 | cmd3' correctly, the bang should apply to the entirestefanf2009-04-131-0/+1
| | | | | | | pipeline cmd2 | cmd3 and not just cmd2. PR: 130298 Submitted by: Jilles Tjoelker
* Fix checking if a variable name is LINENO. As STPUTC changes the pointer if itstefanf2008-05-281-2/+5
| | | | | | needs to enlarge the buffer, we must not keep a pointer to the beginning. PR: ports/123879
* Expand $LINENO to the current line number. This is required by SUSv3's "Userstefanf2008-05-151-3/+22
| | | | | | | | | Portability Utilities" option. Often configure scripts generated by the autotools test if $LINENO works and refuse to use /bin/sh if not. Package test run by: pav
* When parsing an invalid parameter expansion (eg. ${} or ${foo@bar}) do notstefanf2006-11-051-9/+19
| | | | | | | | | | | issue a syntax error immediately but save the information that it is erroneous for later when the parameter expansion is actually done. This means eg. "false && ${}" will not generate an error which seems to be required by POSIX. Include the invalid parameter expansion in the error message (sometimes abbreviated with ... because recovering it would require a lot of code). PR: 105078 Submitted by: emaste
* Do not forget to increment the input line counteryar2006-07-311-0/+1
| | | | | | | when reading a word spanning multiple lines. PR: bin/101094 MFC after: 5 days
* Remove a hack for an ancient gdb.stefanf2005-08-151-13/+4
|
* Put the comparison with PEOF into a new macro is_eof(). Don't use it if thestefanf2005-08-131-3/+4
| | | | character comes from a string.
* Use assignment rather than comparison.stefanf2005-08-131-2/+2
|
* Include missing headers.stefanf2005-08-131-0/+1
|
* Support \H, \h, \w, \W, \$ string expansion in the prompt.obrien2005-03-011-4/+91
| | | | Submitted by: mini
OpenPOWER on IntegriCloud