Commit Graph

1221 Commits

Author SHA1 Message Date
Jilles Tjoelker
da06ef25e6 sh: Remove remnants to compile out fc completely
r360139 made compiling with NO_HISTORY work. This #define does not remove
the fc and bind builtins completely but makes them always write an error
message.

However, there was also some code in builtins.def and mkbuiltins to remove
the fc builtin entirely (but not the bind builtin). The additional build
system complication to make this work seems not worth it, so remove that
code.
2020-04-22 21:45:43 +00:00
Bryan Drewery
6c346639ba Fix build with NO_HISTORY set
Reviewed by:		jilles
Differential Revision:	https://reviews.freebsd.org/D24458
2020-04-21 00:37:55 +00:00
Adrian Chadd
4d0b267a1f [sh] Fix a "may be unused" warning on mips-gcc
mips-gcc for mips32 was complaining that c was potentially used before
being set.  Setting it to 0 before calling fdgetsc() looks like the right
thing to do in this instance; there's an explicit check for c == 0 later
on.

Tested: mips-gcc mips32 build, running /bin/sh on mips32
2020-04-16 23:31:39 +00:00
Edward Tomasz Napierala
f52c431b18 Bump WARNS for sh(1).
Reviewed by:	jilles
MFC after:	2 weeks
Sponsored by:	DARPA
Differential Revision:	https://reviews.freebsd.org/D24181
2020-04-01 15:12:51 +00:00
Kyle Evans
becf15d7cb sh: remove duplicate el definition
el is declared extern in myhistedit.h and defined in histedit.c. Remove the
duplicate definition in input.c to appease the -fno-common build.

-fno-common will become the default in GCC10/LLVM11.

MFC after:	3 days
2020-03-28 17:02:32 +00:00
Kyle Evans
b63d2d683d sh: fix read builtin on 32-bit systems
Specifically, any system with a 32-bit size_t; -residue is calculated as a
32-bit *then* promoted to the 64-bit off_t and the result is ultimately
wrong. This resulted in what would appear to be truncated output, as only
the first line would be read.

Correct it by just making residue an off_t to begin with, since this is what
lseek will take anyways.

Reported by:	antoine, dim
Triaged by:	cem
Tested by:	kevans
X-MFC-With:	r358152
2020-02-22 03:14:05 +00:00
Hiroki Sato
be860ca2a7 Improve performance of "read" built-in command when using a seekable
fd.

The read built-in command calls read(2) with a 1-byte buffer because
newline characters need to be detected even on a byte stream which
comes from a non-seekable file descriptor.  Because of this, the
following script calls >6,000 read(2) to show a 6KiB file:

 while read IN; do echo "$IN"; done < /COPYRIGHT

When the input byte stream is seekable, it is possible to read a data
block and then reposition the file pointer to where a newline
character found.  This change adds a small buffer to do this and
reduces the number of read(2) calls.

Theoretically, multiple built-in commands reading the same seekable
byte stream in a single pipe chain can share the buffer.  However,
this change just makes a single invocation of the read built-in
allocate a buffer and deallocate it every time for simplicity.
Although this causes read(2) to read the same regions multiple times,
the performance penalty should be small compared to the reduction of
read(2) calls.

Reviewed by:		jilles
MFC after:		1 week
Differential Revision:	https://reviews.freebsd.org/D23747
2020-02-20 03:01:27 +00:00
Jilles Tjoelker
d3eae2a68e sh: Fix rare memory leak with SIGINT
If getcwd() failed earlier on but later succeeded in the pwd builtin,
there was no INTOFF protection between calling savestr() and storing its
result.

It is quite rare for getcwd() to fail, and rarer for it to succeed later in
the same directory.

Found via code inspection for changing ckmalloc() and similar to assert
INTOFF protection instead of applying it directly (which protects against
corrupting malloc's internal state but allows memory leaks or double frees).

MFC after:	1 week
2020-01-01 12:06:37 +00:00
Jilles Tjoelker
2a55bade0e sh: Test that executing various binary files is rejected
If executing a file fails with an [ENOEXEC] error, the shell executes the
file as a shell script, except that this execution may instead result in an
error message if the file is binary.

Per a recent Austin Group interpretation, we will need to change this to
allow a concatenation of a shell script and a binary payload. See
Austin Group bugs #1226 and #1250.

MFC after:	1 week
2019-12-30 21:32:55 +00:00
Baptiste Daroussin
f370355791 Do not use our custom completion function, it is not needed anymore 2019-09-16 07:31:59 +00:00
Bryan Drewery
c63c5ab001 Fix .depend files to work for build tools.
This is somewhat of a follow-up to r335746.

MFC after:	2 weeks
Sponsored by:	DellEMC
2019-06-15 17:08:13 +00:00
Jilles Tjoelker
56ada93f8a sh/tests: Improve failure messages of expansion/arith15.0 2019-03-07 22:51:58 +00:00
Jilles Tjoelker
484160a9cf sh: Add set -o pipefail
The pipefail option allows checking the exit status of all commands in a
pipeline more easily, at a limited cost of complexity in sh itself. It works
similarly to the option in bash, ksh93 and mksh.

Like ksh93 and unlike bash and mksh, the state of the option is saved when a
pipeline is started. Therefore, even in the case of commands like
  A | B &
a later change of the option does not change the exit status, the same way
  (A | B) &
works.

Since SIGPIPE is not handled specially, more work in the script is required
for a proper exit status for pipelines containing commands such as head that
may terminate successfully without reading all input. This can be something
like

(
        cmd1
        r=$?
        if [ "$r" -gt 128 ] && [ "$(kill -l "$r")" = PIPE ]; then
                exit 0
        else
                exit "$r"
        fi
) | head

PR:		224270
Relnotes:	yes
2019-02-24 21:05:13 +00:00
Jilles Tjoelker
94b793c497 sh: Send normal output from bind builtin to stdout
PR:		233343
Submitted by:	Yuichiro NAITO (original version)
2019-02-19 21:27:30 +00:00
Jilles Tjoelker
aac5464b61 sh: Restore $((x)) error checking after fix for $((-9223372036854775808))
SVN r342880 was designed to fix $((-9223372036854775808)) and things like
$((0x8000000000000000)) but also broke error detection for values of
variables without dollar sign ($((x))).

For compatibility, overflow in plain literals continues to be ignored and
the value is clamped to the boundary (except 9223372036854775808 which is
changed to -9223372036854775808).

Reviewed by:	se (although he would like error checking to be removed)
MFC after:	2 weeks
X-MFC-with:	r342880
Differential Revision:	https://reviews.freebsd.org/D18926
2019-02-10 22:23:05 +00:00
Edward Tomasz Napierala
719fd9fb2c Comment out the default sh(1) aliases for root, introduced in r343416.
The rest of this stuff is still to be discussed, but I think at this
point we have the agreement that the aliases should go.

MFC after:	2 weeks
Sponsored by:	DARPA, AFRL
2019-01-25 17:09:26 +00:00
Edward Tomasz Napierala
60315f8f9d Install .shrc for root, and set PS1 for the toor account.
Reviewed by:	jilles
MFC after:	2 weeks
Sponsored by:	DARPA, AFRL
Differential Revision:	https://reviews.freebsd.org/D18872
2019-01-24 23:34:51 +00:00
Edward Tomasz Napierala
d81ca439e7 Make sh(1) support \u in PS1. This removes one fork/exec on interactive
shell startup.

Reviewed by:	0mp (man page), jilles
MFC after:	2 weeks
Sponsored by:	DARPA, AFRL
Differential Revision:	https://reviews.freebsd.org/D18790
2019-01-24 11:59:46 +00:00
Edward Tomasz Napierala
ed1cfd30ce Don't mess with BLOCKSIZE in shell startup files - it's set by login.conf(5);
there's no need to even mention it in shell rc files.  Not that it's wrong;
just pointless and somewhat misleading.

Reviewed by:	jilles
MFC after:	2 weeks
Sponsored by:	DARPA, AFRL
Differential Revision:	https://reviews.freebsd.org/D18809
2019-01-20 22:08:49 +00:00
Jilles Tjoelker
f91d2e2171 sh: Send libedit "ferr" output to fd 2
The libedit "fout" output must be sent to fd 2 since it contains prompts
that POSIX says must be sent to fd 2. However, the libedit "ferr" output
receives error messages such as from "bind" that make no sense to send to fd
1.
2019-01-20 14:25:25 +00:00
Dag-Erling Smørgrav
a96301b673 Fix an edge case when parsing large numbers which resulted in inconsistent
results between an expression that refers to a variable by name and the
same expression that includes the same variable by value.

Submitted by:	se@
MFC after:	1 week
2019-01-09 09:36:54 +00:00
Jilles Tjoelker
993b1e419c sh: Add test for exported but unset variables
PR:		233545
2019-01-03 20:23:12 +00:00
Jilles Tjoelker
56f33d07ce sh: Do not place exported but unset variables into the environment
PR:		233545
Submitted by:	Jan Beich
Obtained from:	NetBSD
2019-01-03 20:22:35 +00:00
Edward Tomasz Napierala
20c9381c98 Make sh(1) collapse $HOME into "~" in PS1.
Reviewed by:	jilles
MFC after:	2 weeks
Sponsored by:	DARPA, AFRL
Differential Revision:	https://reviews.freebsd.org/D18663
2018-12-28 17:51:40 +00:00
Jilles Tjoelker
1becbc64f8 sh(1): Remove -c string from set builtin documentation
Altering the -c string at run time does not make sense and is not possible.

MFC after:	1 week
2018-12-08 12:49:19 +00:00
Jilles Tjoelker
468ed39612 sh: Fix ${param?} default error message
If word in ${param?word} is missing, the shell shall write a default error
message. So expanding ${param?} when param is not set should write an error
message like

sh: param: parameter not set

This was broken by r316417.

PR:		233585
2018-11-28 20:03:53 +00:00
Jilles Tjoelker
77da4a95e8 sh: Use 126 and 127 exit status for failures opening a script
This affects scripts named on the command line, named with a '.' special
builtin and found via the PATH %func autoloading mechanism.

PR:		231986
2018-11-27 21:49:59 +00:00
Jilles Tjoelker
bb324af62a sh: Unify EXERROR and EXEXEC
The difference between EXERROR and EXEXEC was that EXEXEC passed along
exitstatus and EXERROR set exitstatus to 2 in the handling code.

By changing the places that raised EXERROR to set exitstatus to 2, the
handling of EXERROR and EXEXEC becomes the same.
2018-11-09 14:58:24 +00:00
Jilles Tjoelker
b5532964e7 sh: Use exitstatus instead of exerrno to pass EXEXEC status
No functional change is intended.
2018-10-27 20:17:57 +00:00
Jilles Tjoelker
4269bba2eb sh: Fix formal overflow in pointer arithmetic
The intention is to lower the value of the pointer, which according to ubsan
cannot be done by adding an unsigned quantity.

Reported by:	kevans
Approved by:	re (kib)
MFC after:	1 week
2018-09-05 19:16:09 +00:00
Brad Davis
94ec7ec758 Finish moving dot.cshrc and dot.profile to bin/csh/ and bin/sh/.
Approved by:	re (gjb), will (mentor)
Differential Revision:	https://reviews.freebsd.org/D16770
2018-08-29 16:59:19 +00:00
Brad Davis
3349f8bb8f Revert parts of r337849 and r337857
This fixes the build and I will redo these changes as part of a future review
that organizes them differently.  The way I tried to do it here could be done
better.  Sorry for the noise.

Approved by:	will (mentor)
Differential Revision:	https://reviews.freebsd.org/D16737
2018-08-15 23:18:34 +00:00
Brad Davis
1da0bddb6d Fix build after r337849
This moves the symlink creation to after where the files are installed.

This also inverts the shell change so that it only happens if MK_TCSH is on.

Approved by:	will (mentor)
Differential Revision:	https://reviews.freebsd.org/D16725
2018-08-15 16:22:12 +00:00
Brad Davis
b26c7f7fee Move all sh and csh files into bin/sh/ or bin/csh/
This simplifies pkgbase by migrating these to CONFS so they are properly
tagged as config files.

Approved by:	will (mentor)
Differential Revision:	https://reviews.freebsd.org/D16708
2018-08-15 14:41:24 +00:00
Mateusz Piotrowski
17e98da88e Describe how to prevent *.core files from being created using ulimit.
While here, pet mandoc.

Reviewed by:	eadler (previous revision), jilles (previous revision), mat (mentor)
Approved by:	manpages (jilles), mat (mentor)
Differential Revision:	https://reviews.freebsd.org/D15609
2018-07-19 13:09:29 +00:00
Jilles Tjoelker
4600b569bb sh: Don't treat % specially in CDPATH 2018-07-15 21:55:17 +00:00
Jilles Tjoelker
7d6f6a3532 sh: Don't use padvance() for MAIL/MAILPATH
Using padvance() requires undoing its append of '/' and prevents adjusting
its '%' logic to allow most directories with '%' in PATH.

No functional change is intended.
2018-07-15 09:14:30 +00:00
Bryan Drewery
d717a1d4bc Stop building intermediate .o files.
These are not used to link the final tool anymore.  At some point in the past
the suffix rules changed to not link these in.  The original reason for this in
r19176 is unclear but seems to be related to mkdep.  The .depend handling is
still broken here as it is for all build tool patterns like this.

Sponsored by:	Dell EMC
2018-06-27 21:36:49 +00:00
Jilles Tjoelker
dc0dbd74c4 sh: Split CNL syntax category to avoid a check on state[level].syntax
No functional change is intended.
2018-05-21 21:52:48 +00:00
Jilles Tjoelker
29988d0ef0 sh: Allow unquoted newlines in word in ${param+word} etc.
POSIX requires accepting unquoted newlines in word in parameter expansions
like ${param+word}, ${param#word}, although the Bourne shell did not support
it, it is not commonly used and might make it harder to find a missing
closing brace.

It was also strange that something like

foo="${bar#
}"

was rejected.

Reported by:	Martijn Dekker via Robert Elz
2018-05-20 17:25:52 +00:00
Jilles Tjoelker
0c6c134f79 sh: Test that backslash-newline within single-quotes is not special
This works correctly, but the test may be helpful when modifying the parser.
2018-05-11 21:56:01 +00:00
Jilles Tjoelker
4d7f36eea5 sh: Don't have [ match any [[:class:]]
Submitted by:	Robert Elz
MFC after:	3 days
2018-04-29 17:46:08 +00:00
Bryan Drewery
3cdd74bb3c Allow overriding VTABSIZE at compile-time.
Reviewed by:	jilles
Differential Revision:	https://reviews.freebsd.org/D14339
2018-02-13 16:48:57 +00:00
Alex Richardson
fb1df20368 Don't hardcode /usr/bin as the path for mktemp in build tools
It won't work e.g. when crossbuilding from Ubuntu Linux as mktemp is in
/bin there.

Reviewed By:	bdrewery
Approved By:	jhb (mentor)
Differential Revision: https://reviews.freebsd.org/D13937
2018-02-06 15:41:35 +00:00
Jilles Tjoelker
190bc94a67 sh: Refactor job status printing, preparing for -o pipefail and similar
No functional change is intended.
2018-02-02 22:53:58 +00:00
Jilles Tjoelker
b0125116ca sh: Move various structs from jobs.h to jobs.c
These implementation details of jobs.c need not be exposed.
2018-01-01 22:31:52 +00:00
Eitan Adler
6b35d82c34 Fix a few speelling errors
- man pages
- bin/sh

Reviewed by:		jilles
2017-12-28 08:22:26 +00:00
Jilles Tjoelker
c8a5f66527 sh: Don't leak wait* implementation details from jobs.c 2017-12-26 16:23:18 +00:00
Jilles Tjoelker
6986f58f53 sh(1): Markup and spelling fixes 2017-12-23 22:58:19 +00:00
Benjamin Kaduk
f3aff7c91b Fix mandoc -Tlint warnings in bin/
Many style-level issues are still reported.

Submitted by:	Yuri Pankov <yuripv@gmx.com>
Reviewed by:	jilles (previous revision)
Differential Revision:	https://reviews.freebsd.org/D13334
2017-12-07 01:57:27 +00:00
Pedro F. Giffuni
8a16b7a18f General further adoption of SPDX licensing ID tags.
Mainly focus on files that use BSD 3-Clause license.

The Software Package Data Exchange (SPDX) group provides a specification
to make it easier for automated tools to detect and summarize well known
opensource licenses. We are gradually adopting the specification, noting
that the tags are considered only advisory and do not, in any way,
superceed or replace the license texts.

Special thanks to Wind River for providing access to "The Duke of
Highlander" tool: an older (2014) run over FreeBSD tree was useful as a
starting point.
2017-11-20 19:49:47 +00:00
Bryan Drewery
ea825d0274 DIRDEPS_BUILD: Update dependencies.
Sponsored by:	Dell EMC Isilon
2017-10-31 00:07:04 +00:00
Bryan Drewery
3806950135 DIRDEPS_BUILD: Connect new directories.
Sponsored by:	Dell EMC Isilon
2017-10-31 00:04:07 +00:00
Enji Cooper
188e46ab03 Add supporting changes for Add limited sandbox capability to "make check"
Non-tests/... changes:
- Add HAS_TESTS= to Makefiles with libraries and programs to enable iteration
  and propagate the appropriate environment down to *.test.mk.

tests/... changes:
- Add appropriate support Makefile.inc's to set HAS_TESTS in a minimal manner,
  since tests/... is a special subdirectory tree compared to the others.

MFC after:	2 months
MFC with:	r322511
Reviewed by:	arch (silence), testing (silence)
Differential Revision:	D12014
2017-08-14 19:21:37 +00:00
Jilles Tjoelker
413bf7b083 sh: Add test for sh -c with missing command string.
This already works correctly.
2017-08-13 14:36:10 +00:00
Jilles Tjoelker
2cc32af06f sh: Add tests for sh -c that already pass.
PR:		220587
Submitted by:	Ryan Moeller
2017-08-12 19:17:48 +00:00
Enji Cooper
46b37aa2c4 MFhead@r321912 2017-08-02 08:38:36 +00:00
Enji Cooper
4b330699f8 Convert traditional ${MK_TESTS} conditional idiom for including test
directories to SUBDIR.${MK_TESTS} idiom

This is being done to pave the way for future work (and homogenity) in
^/projects/make-check-sandbox .

No functional change intended.

MFC after:	1 weeks
2017-08-02 08:35:51 +00:00
Enji Cooper
8c09935728 Add HAS_TESTS to all Makefiles that use the SUBDIR.${MK_TESTS}+= tests idiom 2017-08-02 08:14:06 +00:00
Jilles Tjoelker
93c3eab50c sh: Remove broken #ifdef NOHACK code (related to sh -c).
Apart from the fact that subtle syntactic changes make a poor compile-time
option, the NOHACK case has been obviously broken since it was added,
because it uses q uninitialized if (*p != '\0').

No functional change is intended.
2017-07-18 19:00:15 +00:00
Jilles Tjoelker
6f49cd266b sh: Ignore error when cd writes the directory actually switched to.
If CDPATH is used non-trivially or the operand is "-", cd writes the
directory actually switched to. (We currently do this only in interactive
shells, but POSIX requires this in non-interactive shells as well.)

As mentioned in Austin group bug #1045, cd shall not return an error while
leaving the current directory changed. Therefore, ignore any write error.
2017-06-25 21:53:08 +00:00
Jilles Tjoelker
55c2cd6f48 sh: Enable interrupts before executing EXIT trap and doing final flush. 2017-06-11 16:54:04 +00:00
Jilles Tjoelker
79fb1e455e sh: Call fc -e editor with interrupts enabled.
Starting the fc -e editor can execute arbitrary script, and executing
arbitrary script with INTOFF in effect may cause unexpected results.

This change (together with other changes) serves mainly to allow asserting
that INTOFF is not in effect when starting the evaluation of a node.
2017-06-06 21:08:05 +00:00
Jilles Tjoelker
8d4cde8e6d sh: Make sure to process SIGINT if SETINTON re-enables processing.
If INTON re-enables interrupts, it processes any interrupt that occurred
while interrupts were disabled. Make SETINTON do the same.
2017-06-04 21:58:02 +00:00
Bryan Drewery
3ecb77f014 Allow defining nofork builtins from builtins.def and move always-safe ones there.
The generated code remains the same.

Reviewed by:	jilles
Differential Revision:	https://reviews.freebsd.org/D11042
2017-06-04 21:02:48 +00:00
Jilles Tjoelker
eab4998278 sh: Fix INTOFF leak when a redirection on a compound command fails.
Reported by:	bdrewery
2017-06-04 20:52:55 +00:00
Jilles Tjoelker
29717eb029 sh: Keep output buffer across builtins.
Allocating and deallocating repeatedly the 1024-byte buffer for stdout from
builtins costs CPU time for little or no benefit.

A simple loop containing builtins that write to a file descriptor, such as
  i=0; while [ "$i" -lt 1000000 ]; do printf .; i=$((i+1)); done >/dev/null
is over 10% faster in a simple benchmark on an amd64 virtual machine.
2017-05-18 22:10:04 +00:00
Jilles Tjoelker
de29cd0869 sh: Ensure memout.bufsize matches allocated buffer, if it exists. 2017-05-18 21:44:14 +00:00
Jilles Tjoelker
5183ddf2ed sh: Simplify output buffering.
Similarly to how STPUTC was changed, change struct output to store the
pointer just past the end of the available space instead of the size of the
available space, so after writing a character it is only necessary to
increment a pointer and not to decrement a counter.
2017-05-16 21:54:51 +00:00
Jilles Tjoelker
1b21b7fa89 sh: Fix '-' from quoted arithmetic in case/glob pattern range.
It does not make much sense to generate the '-' in a pattern bracket
expression using arithmetic expansion, but it does not make sense to forbid
it either.

Try to avoid reprocessing the string if it is unnecessary.
2017-05-14 13:14:19 +00:00
Jilles Tjoelker
73a73f7bd2 sh: Add test for arithmetic expansion in [x-y] pattern range.
It does not make much sense to generate the '-' in a pattern bracket
expression using arithmetic expansion, but it does not make sense to forbid
it either.

This test already passes.
2017-05-13 20:28:32 +00:00
Jilles Tjoelker
3f2da875f7 sh: Fix INTOFF leak after a builtin with different locale settings.
After executing a builtin with different locale settings such as
  LC_ALL=C true
SIGINT handling was left disabled indefinitely.

MFC after:	1 week
2017-05-07 19:49:46 +00:00
Jilles Tjoelker
b98072777f sh: Update TOUR and comments for some code changes, some of them old.
Also, improve some terminology in TOUR and comments.
2017-05-06 13:28:42 +00:00
Jilles Tjoelker
d4993b6db2 sh: Simplify handling of newlines in command substitution.
Unless we need to split on newlines, just append them as normal and remove
them at the end.
2017-04-28 16:16:22 +00:00
Jilles Tjoelker
24b55fb76d sh: Add some tests for command substitution final newline stripping. 2017-04-27 18:52:18 +00:00
Jilles Tjoelker
53f6052862 sh: Add tests for NUL byte in command substitution output. 2017-04-23 21:58:17 +00:00
Jilles Tjoelker
3be4e97d2b sh: Simplify setinteractive().
setsignal() does nothing if the signal disposition is already set correctly.
2017-04-22 21:31:37 +00:00
Jilles Tjoelker
026dfd4aef sh: Fix use after free when resetting an in-use alias.
The special case of modifying an existing alias does not work correctly if
the alias is currently in use. Instead, handle this case by unaliasing the
old alias (if any) and then creating a new alias.
2017-04-16 22:10:02 +00:00
Jilles Tjoelker
683bf56cf6 sh: Link the new test to the build. 2017-04-16 21:57:25 +00:00
Jilles Tjoelker
773e27aeee sh: Fix unalias -a while an alias is currently in use.
It is a rare situation to modify aliases while an alias is currently in use,
but this is handled for plain unalias. Handle it for unalias -a as well.
2017-04-16 21:42:43 +00:00
Jilles Tjoelker
a9250603fd sh: Reduce size of limits table. 2017-04-12 21:15:55 +00:00
Jilles Tjoelker
5850a75952 sh: Add test for unaliasing an alias that is currently in use.
This already works correctly.
2017-04-08 21:57:59 +00:00
Jilles Tjoelker
05e626c3cf sh: Remove an XXX comment: it is normal for builtins to use argptr. 2017-04-02 15:53:11 +00:00
Jilles Tjoelker
c39d3320ab sh: Remove global state from collate_range_cmp().
The global state is not used across invocations of collate_range_cmp().
2017-04-02 14:02:10 +00:00
Jilles Tjoelker
e59833ccfe sh: Don't scan word twice in ${param=word}. 2017-04-02 13:43:45 +00:00
Jilles Tjoelker
e2708b1624 sh: Don't scan word twice in ${param#%##%%word}.
If word is expanded, use the found end instead of iterating over the data
again.
2017-04-02 13:29:27 +00:00
Jilles Tjoelker
9e1bb30e23 sh: Don't scan word twice in ${param+-word}.
There is no longer a case where argbackq has already been advanced but the
string pointer needs to be advanced.
2017-04-02 12:37:43 +00:00
Jilles Tjoelker
a54caffda0 sh: Fix build with -DDEBUG=2.
With the new expansion code (word splitting during instead of after other
expansion processing), tracing the result of command substitution is no
longer possible, so stop trying.
2017-03-19 21:18:53 +00:00
Jilles Tjoelker
0620d81993 sh: Remove unused function declaration for arith_lex_reset().
Reported by:	Robert Elz
2017-03-18 20:41:07 +00:00
Jilles Tjoelker
56bf1d616f sh: Remove unused return value of subevalvar_misc(). 2017-03-18 16:09:30 +00:00
Jilles Tjoelker
439948cdf6 sh: Get rid of global variable argbackq. 2017-03-16 21:53:55 +00:00
Jilles Tjoelker
8999a290ab sh: Fix executing wrong command with ${unsetvar#$(cmdsubst)}$(cmdsubst).
The parsed internal representation of words consists of a byte string with a
list of nodes (commands in command substitution). Each unescaped CTLBACKQ or
CTLBACKQ | CTLQUOTE byte corresponds to an entry in the list.

If param in ${param#%##%%word} is not set, the word is not expanded (in a
deviation of POSIX shared with other ash variants and ksh93). Erroneously,
the pointer in the list of commands (argbackq) was not advanced. This caused
the wrong command to be executed later if the outer word contained another
command substitution.

Example:
  echo "${unsetvar#$(echo a)}$(echo b)"
wrote "a" but should write "b".

MFC after:	1 week
2017-03-10 16:04:00 +00:00
Jilles Tjoelker
586fd24857 sh: Fix crash if a -T trap is taken during command substitution.
Code like  t=$(stat -f %m "$file")  segfaulted if -T was active and a trap
was taken while the shell was waiting for the child process to finish.

What happened was that the dotrap() call in waitforjob() was hit. This
re-entered command execution (including expand.c) at a point not expected by
expbackq(), and global state (unallocated stack string and argbackq) was
corrupted.

To fix this, change expbackq() to prepare for command execution to be
re-entered.

Reported by:	bdrewery
MFC after:	1 week
2017-03-04 22:58:34 +00:00
Enji Cooper
caf42d8126 bin: normalize paths using SRCTOP-relative paths or :H when possible
This simplifies make logic/output

MFC after:	1 month
Sponsored by:	Dell EMC Isilon
2017-03-04 11:31:08 +00:00
Jilles Tjoelker
549bbb4fa1 sh: Add some already working tests that exercise new code paths. 2017-03-03 22:46:20 +00:00
Warner Losh
fbbd9655e5 Renumber copyright clause 4
Renumber cluase 4 to 3, per what everybody else did when BSD granted
them permission to remove clause 3. My insistance on keeping the same
numbering for legal reasons is too pedantic, so give up on that point.

Submitted by:	Jan Schaumann <jschauma@stevens.edu>
Pull Request:	https://github.com/freebsd/freebsd/pull/96
2017-02-28 23:42:47 +00:00
Baptiste Daroussin
33bf6c45fb Remove empty Li
Reported by:	make manlint
MFC after:	2 days
2017-02-11 23:09:50 +00:00
Jilles Tjoelker
3c47cee5c8 sh: Add simple test for 'set -C' (noclobber).
To ensure fast test runs, race conditions are not tested.
2016-11-02 22:33:37 +00:00
Jilles Tjoelker
7cca93e61a sh: Do not import IFS's value from the environment.
Per Austin group issue #884, always set IFS to $' \t\n'. As before, IFS will
be exported iff it was in the environment.

Most shells (e.g. bash, ksh93 and mksh) already did this. This change
improves predictability, in that scripts can simply rely on the default
value.

However, the effect on security is little, since applications should not be
calling the shell with attacker-controlled environment variable names in the
first place and other security-sensitive variables such as PATH should be
and are imported by the shell.

When using a new sh with an old (before 10.2) libc wordexp(), IFS is no
longer passed on. Otherwise, wordexp() continues to pass along IFS from the
environment per its documentation.

Discussed with:	pfg
Relnotes:	yes
2016-10-08 13:40:12 +00:00
Jilles Tjoelker
681e94f4d0 sh: Add some tests for non-standard features of the echo builtin.
MFC after:	1 week
2016-09-02 21:13:46 +00:00