Commit Graph

2514 Commits

Author SHA1 Message Date
Jilles Tjoelker
b036c75b4c sh: Various simplifications to jobs.c:
* Prefer kill(-X) to killpg(X).
* Remove some dead code.
* No additional SIGINT is needed if int_pending() is already true.

No functional change is intended.
2010-12-12 22:59:34 +00:00
Jilles Tjoelker
9f5a68a002 sh: Remove the herefd hack.
The herefd hack wrote out partial here documents while expanding them. It
seems unnecessary complication given that other expansions just allocate
memory. It causes bugs because the stack is also used for intermediate
results such as arithmetic expressions. Such places should disable herefd
for the duration but not all of them do, and I prefer removing the need for
disabling herefd to disabling it everywhere needed.

Here documents larger than 1024 bytes will use a bit more CPU time and
memory.

Additionally this allows a later change to expand here documents in the
current shell environment. (This is faster for small here documents but also
changes behaviour.)

Obtained from:	dash
2010-12-12 00:07:27 +00:00
Jilles Tjoelker
f7dea8517f sh: Replace some macros and repeated code in expand.c with functions.
No functional change is intended, but the binary is about 1K smaller on
i386.
2010-12-11 22:13:29 +00:00
Jilles Tjoelker
6903c6832e sh: Use vsnprintf() rather than crafting our own in fmtstr().
Add INTOFF/INTON as longjmp out of vsnprintf may cause memory leaks or
undefined behaviour.
2010-12-11 17:47:27 +00:00
Jilles Tjoelker
c67712a089 sh: Improve internal-representation-to-text code to avoid binary output.
The code to translate the internal representation to text did not know about
various additions to the internal representation since the original ash and
therefore wrote binary stuff to the terminal.

The code is used in the jobs command and similar output.

Note that the output is far from complete and mostly serves for recognition
purposes.
2010-12-06 23:49:27 +00:00
Jilles Tjoelker
fa9e5d05a3 sh: POSIX says there should not be a space between Done and (exitstatus).
(On the other hand, (core dumped) does need a space and so does [1] +.)
2010-12-05 22:56:46 +00:00
Jilles Tjoelker
1bb49f9524 sh: Improve jobs output of pipelines.
If describing the status of a pipeline, write all elements of the pipeline
and show the status of the last process (which would also end up in $?).
Only write one report per job, not one for every process that exits.

To keep some earlier behaviour, if any process started by the shell in a
foreground job terminates because of a signal, write a message about the
signal (at most one message per job, however).

Also, do not write messages about signals in the wait builtin in
non-interactive shells. Only true foreground jobs now write such messages
(for example, "Terminated").
2010-12-05 22:37:01 +00:00
Jilles Tjoelker
ff304d3732 sh: Avoid marking a job as done before it is fully created.
In r208489, I added code to reap zombies when forking new processes, to
limit the amount of zombies. However, this can lead to marking a job as done
or stopped if it consists of multiple processes and the first process ends
very quickly. Fix this by only checking for zombies before forking the first
process of a job and not marking any jobs without processes as done or
stopped.
2010-12-05 21:53:29 +00:00
Jilles Tjoelker
5af61b5251 sh: jobs -p: Do not ask the kernel for the pgid.
The getpgid() call will fail if the first process in the job has already
terminated, resulting in output of "-1".

The pgid of a job is always the pid of the first process in the job and
other code already relies on this.
2010-12-05 16:09:03 +00:00
Ulrich Spörlein
cded07a878 Move most of the remaining USD/PSD/SMM papers into share/doc 2010-12-04 10:11:20 +00:00
Jilles Tjoelker
25f6b31fac sh(1): Clean up documentation of built-in commands.
Make sure all built-in commands are in the subsection named such, except
exp, let and wordexp which are deliberately undocumented. The text said only
built-ins that really need to be a built-in were documented there but in
fact almost all of them were already documented.
2010-12-03 23:24:27 +00:00
Jilles Tjoelker
b97d13b399 sh(1): Document that command's -p option also works with -v/-V.
This was implemented in r201343.
2010-12-01 23:26:32 +00:00
Jilles Tjoelker
9d37e15722 sh: Code size optimizations to "stack string" memory allocation:
* 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.
2010-11-23 22:17:39 +00:00
Jilles Tjoelker
0bee28331e sh: Pass multiple bytes at a time to lex.
This speeds up the expansion/arith6.0 test considerably.
2010-11-23 20:46:06 +00:00
Jilles Tjoelker
ae7f47355d sh: Fix confusing behaviour if chdir succeeded but getcwd failed in cd -P.
If getcwd fails, do not treat this as an error, but print a warning and
unset PWD. This is similar to the behaviour when starting the shell in a
directory whose name cannot be determined.
2010-11-22 23:49:06 +00:00
Rebecca Cran
1161d4202c Fix some more warnings found by clang. 2010-11-22 20:10:48 +00:00
Jilles Tjoelker
467fdf32f8 sh: Remove the check that alpha/name/in_name chars are not CTL* bytes.
Since is_alpha/is_name/is_in_name were made ASCII-only, this can no longer
happen.

Additionally, the check was wrong because it did not include the new
CTLQUOTEEND.
2010-11-20 14:30:28 +00:00
Jilles Tjoelker
aeb5d06504 sh: Code size optimizations to buffered output.
This is mainly less use of the outc macro.

No functional change is intended, but code size is about 2K less on i386.
2010-11-20 14:14:52 +00:00
Jilles Tjoelker
9897c45f31 sh: Add printf builtin.
This was removed in 2001 but I think it is appropriate to add it back:
* I do not want to encourage people to write fragile and non-portable echo
  commands by making printf much slower than echo.
* Recent versions of Autoconf use it a lot.
* Almost no software still wants to support systems that do not have
  printf(1) at all.
* In many other shells printf is already a builtin.

Side effect: printf is now always the builtin version (which behaves
identically to /usr/bin/printf) and cannot be overridden via PATH (except
via the undocumented %builtin mechanism).

Code size increases about 5K on i386. Embedded folks might want to replace
/usr/bin/printf with a hard link to /usr/bin/alias.
2010-11-19 12:56:13 +00:00
Jilles Tjoelker
c3f57269e6 sh: Add binary buffered output for use by the printf builtin. 2010-11-14 15:31:59 +00:00
Jilles Tjoelker
d79326ecc8 sh: Update the suspend example for the change of the job control flag
from -j to -m, many years ago.

Due to r215266, this function now actually works.
2010-11-13 22:20:46 +00:00
Jilles Tjoelker
4a7b1013fb sh: Do the additional actions if 'local -' restore changes -i/-m/-E/-V.
Example:
  f() { local -; set +m; }; f
caused failure to execute external programs because the job control tty fd
was not opened.
2010-11-13 22:10:26 +00:00
Jilles Tjoelker
61aad2fe09 echo(1): Clarify portability and mention literal "--" handling.
MFC after:	1 week
2010-11-12 22:43:40 +00:00
Jilles Tjoelker
4b985a89e7 sh(1): Document r214304 (special builtin is illegal function name). 2010-11-12 22:40:18 +00:00
Jilles Tjoelker
f35d74beed sh(1): Update for r214492. "${v+"hi}there"}".
The part hi}there is not a quoted string but nevertheless the closing brace
does not terminate the expansion.
2010-11-12 22:28:47 +00:00
Jilles Tjoelker
7f39c0011f sh: Remove unused man page for echo builtin.
The information in sh(1) about the echo builtin is equivalent, though less
extensive.

The echo(1) man page (bin/echo/echo.1) is different.

Unfortunately, sh's echo builtin and /bin/echo have gone out of sync and
this probably cannot be fixed any more.

Reported by:	uqs (list of untouched files)
MFC after:	1 week
2010-11-12 15:40:00 +00:00
Jilles Tjoelker
3fdfd0a435 sh(1): Modernize the introduction a bit.
In particular, remove the text about ksh-like features, which are usually
taken for granted nowadays. The original Bourne shell is fading away and for
most users our /bin/sh is one of the most minimalistic they know.
2010-11-12 14:40:20 +00:00
Jilles Tjoelker
47fdf870a7 test: Move tests to tools/regression/bin/test.
Convert the tests to the perl prove format.
Remove obsolete TEST.README (results of an old TEST.sh for some old Unices)
and TEST.csh (old tests without correct values, far less complete than
TEST.sh).

MFC after:	1 week
2010-11-08 23:15:10 +00:00
Jilles Tjoelker
135ff4b5b0 sh: Fix some issues with aliases and case, by importing dash checkkwd code.
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
2010-11-02 23:44:29 +00:00
Ed Schouten
736fc28680 Add a new libc function: cfmakesane(3).
I've noticed various terminal emulators that need to obtain a sane
default termios structure use very complex `hacks'. Even though POSIX
doesn't provide any functionality for this, extend our termios API with
cfmakesane(3), which is similar to the commonly supported cfmakeraw(3),
except that it fills the termios structure with sane defaults.

Change all code in our base system to use this function, instead of
depending on <sys/ttydefaults.h> to provide TTYDEF_*.
2010-11-02 17:00:56 +00:00
Jilles Tjoelker
57a40f7d08 sh(1): Correct synopsis and make precise how $0 is set.
In particular, the extra argument to set $0 with -c was not documented.

MFC after:	1 week
2010-10-31 23:03:11 +00:00
Ulrich Spörlein
ec1e236534 Fix typo and grammar nit
Submitted by:	arundel
MFC after:	7 days (or when the bikeshed has abated)
2010-10-31 19:16:54 +00:00
Jilles Tjoelker
4c4164f9a7 sh: Reindent evaltree(). 2010-10-31 12:08:16 +00:00
Jilles Tjoelker
dca867f1c9 sh: Use iteration instead of recursion to evaluate semicolon lists.
This reduces CPU and memory usage when executing long lists (such
as long functions).
2010-10-31 12:06:02 +00:00
Ulrich Spörlein
c2c9a54067 Elaborate some more on the non-security implications of using -P
Submitted by:	delphij
Discussion at:	svn-src-all
2010-10-31 09:21:27 +00:00
Jilles Tjoelker
274110df0a sh: Tweak some string constants to reduce code size.
* Reduce some needless differences.
* Shorten some error messages that should not happen.
2010-10-29 21:44:43 +00:00
Jilles Tjoelker
a1251487f4 sh: Reject function names ending in one of !%*+-=?@}~
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)
2010-10-29 21:20:56 +00:00
Jilles Tjoelker
e20776d503 sh: Detect various additional errors in the parser.
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)
2010-10-29 21:06:57 +00:00
Jilles Tjoelker
33582ce055 sh: Error out on various specials/keywords in the wrong place in backticks.
Example:
  echo `date)`

Exp-run done by:	pav (with some other sh(1) changes)
Obtained from:		NetBSD (Christos Zoulas, NetBSD PR 11317)
2010-10-29 20:23:41 +00:00
Jilles Tjoelker
60f7eec450 sh: Fix some issues with CTL* bytes and ${var#pat}.
subevalvar() incorrectly assumed that CTLESC bytes were present iff the
expansion was quoted. However, they are present iff various processing such
as word splitting is to be done later on.

Example:
  v=@$e@$e@$e@
  y="${v##*"$e"}"
  echo "$y"
failed if $e contained the magic CTLESC byte.

Exp-run done by:	pav (with some other sh(1) changes)
2010-10-29 19:34:57 +00:00
Jilles Tjoelker
048f26671a sh: Do IFS splitting on word in ${v+word} and ${v-word}.
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)
2010-10-29 13:42:18 +00:00
Jilles Tjoelker
6c38071288 sh: Only accept a '}' inside ${v+-=?...} if double-quote state matches.
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)
2010-10-28 22:34:49 +00:00
Jilles Tjoelker
9cec947f3f sh: Make double-quotes quote a '}' inside ${v#...} and ${v%...}.
Exp-run done by:	pav (with some other sh(1) changes)
PR:			bin/57554
2010-10-28 21:51:14 +00:00
Dag-Erling Smørgrav
1f9d174116 Language cleanup. 2010-10-27 18:48:23 +00:00
Jilles Tjoelker
d94c867339 sh: Ignore double-quotes in arithmetic rather than treating them as quotes.
This provides similar behaviour, but allows a simpler parser.

This changes r206473.

Exp-run done by:	pav (with some other sh(1) changes)
2010-10-24 22:25:38 +00:00
Jilles Tjoelker
67e109adbe sh: Do not allow overriding a special builtin with a function.
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)
2010-10-24 22:03:21 +00:00
Jilles Tjoelker
074e83b14e sh: Make sure defined functions can actually be called.
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)
2010-10-24 20:45:13 +00:00
Jilles Tjoelker
3dec7d0c15 sh: Check whether dup2 was successful for >&FD and <&FD.
A failure (usually caused by FD not being open) is a redirection error.

Exp-run done by:	pav (with some other sh(1) changes)
2010-10-24 20:09:49 +00:00
Jilles Tjoelker
ba08f69b5c sh: Change ! within a pipeline to start a new pipeline instead.
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)
2010-10-24 17:06:49 +00:00
Jilles Tjoelker
f1ec058177 sh(1): Clarify subshells/processes for pipelines.
For multi-command pipelines,
1. all commands are direct children of the shell (unlike the original
   Bourne shell)
2. all commands are executed in a subshell (unlike the real Korn shell)

MFC after:	1 week
2010-10-16 14:37:56 +00:00