summaryrefslogtreecommitdiffstats
path: root/bin/sh/eval.c
Commit message (Collapse)AuthorAgeFilesLines
* sh: Use vfork in a few common cases.jilles2012-02-041-0/+9
| | | | | | | | | | | | | | | | | This uses vfork() for simple commands and command substitutions containing a single simple command, invoking an external program under certain conditions (no redirections or variable assignments, non-interactive shell, no job control). These restrictions limit the amount of code executed in a vforked child. There is a large speedup (for example 35%) in microbenchmarks. The difference in buildkernel is smaller (for example 0.5%) but still statistically significant. See http://lists.freebsd.org/pipermail/freebsd-hackers/2012-January/037581.html for some numbers. The use of vfork() can be disabled by setting a variable named SH_DISABLE_VFORK.
* sh: Fix $? in the first command of a 'for'.jilles2012-01-221-1/+4
| | | | | | In the first command of a 'for', $? should be the exit status of the last pipeline (command substitution in the word list or command before 'for'), not always 0.
* sh: Fix execution of multiple statements in a trap when evalskip is setdumbbell2012-01-161-1/+1
| | | | | | | | | | | Before this fix, only the first statement of the trap was executed if evalskip was set. This is for example the case when: o "-e" is set for this shell o a trap is set on EXIT o a function returns 1 and causes the script to abort Reviewed by: jilles MFC after: 2 weeks
* sh: Fix some bugs with exit status from case containing ;&.jilles2012-01-151-9/+24
| | | | | | | | | | | | | Also, rework evalcase() to not evaluate any tree. Instead, return the NCLISTFALLTHRU node and handle it in evaltree(). Fixed bugs: * If a ;& list with non-zero exit status is followed by an empty ;; or final list, the exit status of the case command should be equal to the exit status of the ;& list, not 0. * An empty ;& case should not reset $?.
* sh: Fix two bugs with case and exit status:jilles2012-01-151-1/+3
| | | | | | | * If no pattern is matched, POSIX says the exit status shall be 0 (even if there are command substitutions). * If a pattern is matched and there are no command substitutions, the first command should see the $? from before the case command, not always 0.
* sh: Do not force special builtins non-special in optimized command subst.jilles2011-12-281-2/+1
| | | | | | | | | This is not necessary: errors are already caught in evalbackcmd() and forcelocal handles changes to variables. Note that this depends on r223024. MFC after: 4 weeks
* sh: Remove impossible evalskip check in 'for'.jilles2011-11-271-3/+0
|
* sh: Reduce one level of evaltree() recursion when executing 'case'.jilles2011-11-261-11/+9
| | | | | | | Free expanded case text before executing commands. Remove impossible evalskip checks (expanding an argument cannot set evalskip anymore since $(break) and the like are properly executed in a subshell environment).
* sh: Remove special support for background simple commands.jilles2011-06-181-5/+3
| | | | It expands the arguments in the parent shell process, which is incorrect.
* sh: Add case statement fallthrough (with ';&' instead of ';;').jilles2011-06-171-0/+8
| | | | | | | | | | | | 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: Reduce unnecessary forks with eval.jilles2011-06-161-1/+2
| | | | | | | | | | The eval special builtin now runs the code with EV_EXIT if it was run with EV_EXIT itself. In particular, this eliminates one fork when a command substitution contains an eval command that ends with an external program or a subshell. This is similar to what r220978 did for functions.
* sh: Save/restore changed variables in optimized command substitution.jilles2011-06-121-7/+11
| | | | | | | | | | | In optimized command substitution, save and restore any variables changed by expansions (${var=value} and $((var=assigned))), instead of trying to determine if an expansion may cause such changes. If $! is referenced in optimized command substitution, do not cause jobs to be remembered longer. This fixes $(jobs $!) again, simplifies the man page and shortens the code.
* sh: Do parameter expansion before printing PS4 (set -x).jilles2011-06-091-2/+3
| | | | | | | | | | | | | | | | 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: Fix $? in heredocs on simple commands.jilles2011-06-051-1/+2
| | | | PR: bin/41410
* sh: Honour -n while processing -c string.jilles2011-06-041-1/+1
|
* sh: Expand aliases after assignments and redirections.jilles2011-05-211-9/+3
|
* sh: Avoid close(-1) when evaluating a multi-command pipeline.jilles2011-05-151-1/+2
| | | | Valgrind complains about this.
* sh: Set $? to 0 for background commands.jilles2011-04-251-3/+6
| | | | | | | | | | For backgrounded pipelines and subshells, the previous value of $? was being preserved, which is incorrect. For backgrounded simple commands containing a command substitution, the status of the last command substitution was returned instead of 0. If fork() fails, this is an error.
* sh: Allow EV_EXIT through function calls, make {...} <redir more consistent.jilles2011-04-231-12/+13
| | | | | | | | | | | | | | | | | | | | | If EV_EXIT causes an exit, use the exception mechanism to unwind redirections and local variables. This way, if the final command is a redirected command, an EXIT trap now executes without the redirections. Because of these changes, EV_EXIT can now be inherited by the body of a function, so do so. This means that a function no longer prevents a fork before an exec being skipped, such as in f() { head -1 /etc/passwd; }; echo $(f) Wrapping a single builtin in a function may still cause an otherwise unnecessary fork with command substitution, however. An exit command or -e failure still invokes the EXIT trap with the original redirections and local variables in place. Note: this depends on SHELLPROC being gone. A SHELLPROC depended on keeping the redirections and local variables and only cleaning up the state to restore them.
* sh: Remove clearcmdentry()'s now unused argument.jilles2011-02-051-3/+3
|
* sh: Remove special code for shell scripts without magic number.jilles2011-02-041-18/+10
| | | | | | | | | | These are called "shell procedures" in the source. If execve() failed with [ENOEXEC], the shell would reinitialize itself and execute the program as a script. This requires a fair amount of code which is not frequently used (most scripts have a #! magic number). Therefore just execute a new instance of sh (_PATH_BSHELL) to run the script.
* sh: Do not call exitshell() from evalcommand() unless evalcommand() forkedjilles2011-01-051-4/+5
| | | | | | itself. This ensures that certain traps caused by builtins are executed.
* sh: Check readonly status for assignments on regular builtins.jilles2011-01-011-2/+1
| | | | | | | | | | An error message is written, the builtin is not executed, nonzero exit status is returned but the shell does not abort. This was already checked for special builtins and external commands, with the same consequences except that the shell aborts for special builtins. Obtained from: NetBSD
* sh: Avoid side effects from builtins in optimized command substitution.jilles2010-12-301-5/+27
| | | | | | | | | | | | | | | | | | Change the criterion for builtins to be safe to execute in the same process in optimized command substitution from a blacklist of only cd, . and eval to a whitelist. This avoids clobbering the main shell environment such as by $(exit 4) and $(set -x). The builtins jobid, jobs, times and trap can still show information not available in a child process; this is deliberately permitted. (Changing traps is not.) For some builtins, whether they are safe depends on the arguments passed to them. Some of these are always considered unsafe to keep things simple; this only harms efficiency a little in the rare case they are used alone in a command substitution.
* sh: Don't do optimized command substitution if expansions have side effects.jilles2010-12-281-1/+15
| | | | | | | | | | | | | | | | | | | | | | | | | Before considering to execute a command substitution in the same process, check if any of the expansions may have a side effect; if so, execute it in a new process just like happens if it is not a single simple command. Although the check happens at run time, it is a static check that does not depend on current state. It is triggered by: - expanding $! (which may cause the job to be remembered) - ${var=value} default value assignment - assignment operators in arithmetic - parameter substitutions in arithmetic except ${#param}, $$, $# and $? - command substitutions in arithmetic This means that $((v+1)) does not prevent optimized command substitution, whereas $(($v+1)) does, because $v might expand to something containing assignment operators. Scripts should not depend on these exact details for correctness. It is also imaginable to have the shell fork if and when a side effect is encountered or to create a new temporary namespace for variables. Due to the $! change, the construct $(jobs $!) no longer works. The value of $! should be stored in a variable outside command substitution first.
* sh: Make expansion errors in optimized command substitution non-fatal.jilles2010-12-281-1/+15
| | | | | | Command substitutions consisting of a single simple command are executed in the main shell process but this should be invisible apart from performance and very few exceptions such as $(trap).
* sh: Code size optimizations to "stack string" memory allocation:jilles2010-11-231-2/+1
| | | | | | | | * 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: Code size optimizations to buffered output.jilles2010-11-201-6/+6
| | | | | | This is mainly less use of the outc macro. No functional change is intended, but code size is about 2K less on i386.
* sh: Reindent evaltree().jilles2010-10-311-76/+76
|
* sh: Use iteration instead of recursion to evaluate semicolon lists.jilles2010-10-311-5/+10
| | | | | 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.
* In the spirit of r90111, depend on c89 and remove the "STATIC" macroobrien2010-10-131-18/+18
| | | | and its usage.
* Consistently use "STATIC" for all functions in order to be able to setobrien2010-10-131-2/+2
| | | | | | breakpoints with in a debugger. And use naked "static" for variables. Noticed by: bde
* sh: Fix exit status if return is used within a loop condition.jilles2010-09-111-0/+2
|
* sh: Get rid of some magic numbers.jilles2010-09-041-2/+2
| | | | MFC after: 1 week
* sh: Add a forgotten const.jilles2010-08-131-1/+1
|
* sh: Return 0 from eval if no command was given.jilles2010-08-031-1/+7
| | | | | | | | | | | This makes a difference if there is a command substitution. To make this work, evalstring() has been changed to set exitstatus to 0 if no command was executed (the string contained only whitespace). Example: eval $(false); echo $? should print 0.
* sh: Recognize "--" in . and exec.jilles2010-05-281-0/+6
| | | | | | | | | | | | | | | | | | Although "--" historically has not been required to be recognized for certain special builtins that do not take options in POSIX, some other implementations recognize options for them, requiring scripts to use "--" or avoid operands starting with "-". Operands starting with "-" can be avoided with eval by prepending a space, and cannot occur with break, continue, exit, return and shift as they only take numbers, nor with times as it does not take operands. With . and exec, avoiding "-" is not so easy as it may require reimplementing the PATH search; therefore the current proposal for POSIX is to require recognition of "--" for them. We continue to accept other strings starting with "-" as operands to . and exec, and also "--" if it is alone to . (which would otherwise be invalid anyway).
* sh: Have only one copy of _PATH_STDPATH in the binary.jilles2010-05-081-3/+2
|
* sh: Apply locale vars on builtins, recognize LC_MESSAGES as a locale var.jilles2010-05-051-0/+4
| | | | | | | | | | | | | | | | This allows doing things like LC_ALL=C some_builtin to run a builtin under a different locale, just like is possible with external programs. The immediate reason is that this allows making printf(1) a builtin without breaking things like LC_NUMERIC=C printf '%f\n' 1.2 This change also affects special builtins, as even though the assignment is persistent, the export is only to the builtin (unless the variable was already exported). Note: for this to work for builtins that also exist as external programs such as /bin/test, the setlocale() call must be under #ifndef SHELL. The shell will do the setlocale() calls which may not agree with the environment variables.
* sh: Do not abort on a redirection error on a compound command.jilles2010-03-141-4/+42
| | | | | | | | | | | | | | Redirection errors on subshells already did not abort the shell because the redirection is executed in the subshell. Other shells seem to agree that these redirection errors should not abort the shell. Also ensure that the redirections will be cleaned up properly in cases like command eval '{ shift x; } 2>/dev/null' Example: { echo bad; } </var/empty/x; echo good
* sh: Do not abort on a redirection error if there is no command word.jilles2010-03-131-1/+7
| | | | | | | | | | | Although simple commands without a command word (only assignments and/or redirections) are much like special builtins, POSIX and most shells seem to agree that redirection errors should not abort the shell in this case. Of course, the assignments persist and assignment errors are fatal. To get the old behaviour portably, use the ':' special builtin. To get the new behaviour portably, given that there are no assignments, use the 'true' regular builtin.
* sh: Make sure to popredir() even if a function caused an error.jilles2010-03-061-1/+3
|
* sh: Make sure to popredir() even if a special builtin caused an error.jilles2010-03-061-2/+3
|
* sh: Improve the command builtin:jilles2010-03-061-32/+63
| | | | | | | | * avoid unnecessary fork * allow executing builtins via command * executing a special builtin via command removes its special properties Obtained from: NetBSD (parts)
* sh: Send the "not found" message for builtin <cmd> to redirected fd 2.jilles2010-01-031-5/+11
|
* sh: Fix some bugs with backquoted builtins:jilles2010-01-011-11/+12
| | | | | | | | | | | | - 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
* sh: Allow command -pv and command -pV (lookup using _PATH_STDPATH).jilles2009-12-311-1/+1
|
* sh: Ensure funcnest is decremented if there was an error in the function.jilles2009-12-301-2/+3
| | | | | | This will be important when things like 'command eval f' will be possible. Currently, the funcnest = 0 assignment in RESET (called when returning to the top level after an error in interactive mode) is really sufficient.
* sh: Various warning fixes (from WARNS=6 NO_WERROR=1):jilles2009-12-271-4/+3
| | | | | | | - const - initializations to silence -Wuninitialized (it was safe anyway) - remove nested extern declarations - rename "index" locals to "idx"
OpenPOWER on IntegriCloud