Commit Graph

3289 Commits

Author SHA1 Message Date
kre 4a9319b47c PR bin/48875 - minor correction (well, not so minor) - commands in loops
must be assumed to have something following, even if the loop itself doesn't,
so redirected fd's around func calls need to be saved.   Should fix etcupdate
2016-05-10 15:14:30 +00:00
kre 8a99e51de9 PR bin/48875 - avoid holding (replaced) file descriptors open when running a
command in the current shell (so they can be restored for the next command)
in cases where it is obvious that there is not going to be a following
command to use them.   This fixes the problem reported in the PR (though
there are still plenty of situations where a FD could be closed but isn't,
we do not do full fd flow eveluation to determine whether a fd will be
used or not).

This is the change that was just committed and then backed out again...

OK christos@
2016-05-09 21:03:10 +00:00
kre baf83b5f55 Revert previous. These changes are intended to get made (and will
be in a minute or two) but not as part of that commit...   The log
entry certainly does not apply.
2016-05-09 20:55:51 +00:00
kre 07ee700a7e Finish the fd reassignment fixes from 1.43 and 1.45 ... if we are moving
a fd to an unspecified high fd number, we certainly do not want to hand
that high fd off to other processes after an exec, so always set close-on-exec
on the result (even if lack of fd's means no fd alteration happens.)
This will (eventually) allow some other code that sets close-on-exec to
be removed, but for now, doing it twice won't hurt.   Also, in a N>&M
type redirection, do not set close-on-exec if we don't want it.

OK christos@
2016-05-09 20:50:08 +00:00
kre 183536927f PR bin/48489 -- Shell "simple commands" are now not allowed to be
completely empty, they must have something (var assignment, redirect,
or command, or multiple of those) to avoid a syntax error.  This
matches the requirements of the grammar in the standard.   Correct the
parser (using FreeBSD's sh as a guide) and update the man page to
remove text that noted a couple of places this was previously OK.

OK christos@
2016-05-09 20:36:07 +00:00
kre ed8202014d PR bin/51123 - make >&- work in all cases, and while doing that fix
things so that >/dev/stdout </dev/stdin (etc) work as well (in all cases).

ok christos@
2016-05-08 20:14:27 +00:00
kre d4f114089f Whitespace fixes. No functional change. 2016-05-08 03:51:15 +00:00
kre 09ecfab926 Slightly improve "jobs" command output in cases where a job includes
embedded background commands or pipelines.   (just slightly...)
OK christos@
2016-05-07 20:07:47 +00:00
kre 3eee147437 PR bin/51119 - don't leak FDs in unusual error cases. OK christos@ 2016-05-07 20:06:30 +00:00
kre 0fe4e12852 Unbreak build ... again... gcc is insane. 2016-05-03 23:55:12 +00:00
kre 3c6d76cd74 PR bin/51114 - print the correct values for >&- and >& N (N > 9)
in output from the "jobs" command (and other places that use the
same routines.)
2016-05-03 20:46:35 +00:00
christos ddfe742084 add missing forward declaration for the STATIC= case. 2016-05-03 17:21:02 +00:00
kre 404b1d0271 Fix things so that STATIC can me made static (-DSTATIC=static)
and have the shell still compile, link, and run...

ok christos@
2016-05-03 13:47:58 +00:00
kre a18b822d05 Allow function names to be any shell word not containing '/'.
This allows anything that could be a filesystem command to be
implemented as a function instead.  The restriction on '/'
is because of the way that functions are (required to be) searched
for relative to PATH searching - a function with a name containing '/'
could never be executed, so simply prohibit defining such a thing.

ok christos@
2016-05-03 03:16:55 +00:00
kre 1d68040618 PR bin/43639 - check that a file being read by the '.' command
is a regular file, even when it is given as a full pathname.
2016-05-03 03:12:40 +00:00
kre 54c1af73d6 Remove unnecessary extern var declaration that was a
remnant of an earlier version of the previous (fd>10) fixes.

ok christos@
2016-05-03 03:08:21 +00:00
christos 1fad4bb60c Fix handing of user file descriptors outside the 0..9 range.
Also, move (most of) the shell's internal use fd's to much
higher values (depending upon what ulimit -n allows) so they
are less likely to clash with user supplied fd numbers.  A future
patch will (hopefully) avoid this problem completely by dynamically
moving the shell's internal fds around as needed. (From kre@)
2016-05-02 01:46:31 +00:00
wiz 6b7f9f8932 Remove some double quotes.
Parity is kept.
2016-04-04 13:05:56 +00:00
christos 478a0487ac Allow a heredoc to be positioned outside a `` command substitution.
POSIX just says "here docs begin after the next newline [token]".
Nothing about "provided it is inside any `` the redirect operator
appears in...    As best I can tell, NetBSD now has the only shell to
handle this "correctly" (which raises the question whether it is
correct - but if not, only erroneous scripts are affected.)
This is required by some (probably broken) autoconfigure related
scripts. (from kre@)
2016-04-04 12:39:08 +00:00
mrg 6d27b0e86e for GCC 5.3 pass -fwrapv as this relies upon well-defined integer overflow. 2016-04-01 08:19:31 +00:00
christos 65cf828420 After discussions with Jilles Tjoelker (FreeBSD shell) and
following a suggestion from him, the way the fix to PR bin/50993
was implemented has changed a little.   There are three steps involved
in processing a here document, reading it, parsing it, and then
evaluating it before applying it to the correct file descriptor for
the command to use.  The third of those is not related to this
problem, and has not changed.  The bug was caused by combining the
first two steps into one (and not doing it correctly - which would be
hard that way.)  The fix is to split the first two stages into
separate events.   The original fix moved the 2nd stage (parsing)
to just immediately before the 3rd stage (evaluation.)  Jilles
pointed out some unwanted side effects from doing it that way, and
suggested moving the 2nd stage to immediately after the first.
This commit makes that change.  The effect is to revert the changes
to expand.c and parser.h (which are no longer needed) and simplify
slightly the change to parser.c. (from kre@)
2016-03-31 23:11:05 +00:00
christos b732ccab17 replace with standard copyright :-) 2016-03-31 16:28:23 +00:00
christos 26427ea787 Document the NETBSD_SHELL variable, the enhancements to export,
the posix option, and a whole bunch of miscellaneous updates and
corrections. (from kre@)
2016-03-31 16:18:22 +00:00
christos 9302f8ef1a Implement the NETBSD_SHELL readonly unexportable unimportable
variable (with its current value set at 20160401) as discussed on
current-users and tech-userlevel. This also includes the necessary
support to implement it properly (particularly the unexportable
part) and adds options to the export command to support unexportable
variables. Also implement the "posix" option (no single letter
equivalent) which gets its default value from whether or not
POSIXLY_CORRECT is set in the environment when the shell starts
(but can be changed just like any other option using -o and +o on
the command line, or the set builtin command.) While there, fix
all uses of options so it is possible to have options that have a
short (one char) name, and no long name, just as it has been possible
to have options with a long name and no short name, though there
are currently none (with no long name).  For now, the only use of
the posix option is to control whether ${ENV} is read at startup
by a non-interactive shell, so changing it with set is not usful
- that might change in the future. (from kre@)
2016-03-31 16:16:35 +00:00
christos ddf72776f1 PR bin/51027 - fix the parsing of references to shell parameters
when given without braces (ie: $2 etc).  Only the first 9 shell
parameters ($1 .. $9) and the special parameter ($0) can be
referenced this way, $10 is ${1}0 not ${10}.   Make it so.
This bug brought to notice by Sven Mascheck's web pages which
discuss (among other things) the history of this (and other ash
based) shells .. see http://www.in-ulm.de/~mascheck/ (from kre@)
2016-03-31 16:12:52 +00:00
christos 21cf17379f This is an internally visible change - no effect visible to the
user is expected. With the previous commits to parser.c, we no
longer need to handle reading here documents in the (massive)
readtoken1() function. That allows its code to be simplified and
made easier to read and understand (several goto's goto goto heaven.
RIP) (from kre@)
2016-03-31 16:12:09 +00:00
christos b322b670f0 After discussions with Jilles Tjoelker (FreeBSD shell) and following
a suggestion from him, the way the fix to PR bin/50993 was implemented
has changed a little.   There are three steps involved in processing
a here document, reading it, parsing it, and then evaluating it
before applying it to the correct file descriptor for the command
to use.  The third of those is not related to this problem, and
has not changed.  The bug was caused by combining the first two
steps into one (and not doing it correctly - which would be hard
that way.)  The fix is to split the first two stages into separate
events.   The original fix moved the 2nd stage (parsing) to just
immediately before the 3rd stage (evaluation.)  Jilles pointed out
some unwanted side effects from doing it that way, and suggested
moving the 2nd stage to immediately after the first.  This commit
makes that change.  The effect is to revert the changes to expand.c
and parser.h (which are no longer needed) and simplify slightly
the change to parser.c. (from kre@)
2016-03-31 13:27:44 +00:00
christos 7c8885a4e7 Move the parseredir internal subroutine out of readtoken1() into being
a real function of its own (also inspired by FreeBSD - though for
this one other sh differences require slightly different code.) (from kre@)
2016-03-27 14:40:20 +00:00
christos 1d1484aa26 PR bin/50993 - this is a significant rewrite of the way that here
documents are processed.  Now, when first detected, they are
simply read (the only change made to the text is to join lines
ended with a \ to the subsequent line, otherwise end marker detection
does not work correctly (for here docs with an unquoted endmarker
only of course.)  This patch also moves the "internal subroutine"
for looking for the end marker out of readtoken1() (which had to
happen as readtoken1 is no longer reading the here doc when it is
needed) - that uses code mostly taken from FreeBSD's sh (thanks!)
and along the way results in some restrictions on what the end
marker can be being removed.   We still do not allow all we should.
(from kre@)
2016-03-27 14:39:33 +00:00
christos 1d4fbce984 Cease "support" for <redirect> fn() { ...
Any redirect (or redirects) before a function definition were
allowed by the parser, but otherwise totally ignored.  The standard
syntax does not permit redirects there, now, neither do we. (from kre@)
2016-03-27 14:36:29 +00:00
christos f9acd92dd8 Finish constifying the new parsebackquote() function. Save a
variable or two...   Should change nothing. (from kre@)
2016-03-27 14:35:30 +00:00
christos ca12a0b88a General KNF and source code cleanups, avoid scattering the
magic string " \t\n" all over the place, slightly improved
syntax error messages, restructured some of the code for
clarity, don't allow IFS to be imported through the environment,
and remove the (never) conditionally compiled ATTY option.
Apart from one or two syntax error messages, and ignoring IFS
if present in the environment, this is intended to have no
user visible changes. (from kre@)
2016-03-27 14:34:46 +00:00
christos 29494835fd fix constness (from kre) 2016-03-21 02:37:26 +00:00
christos db56d5516c Move the command substitution "internal subroutine" part of
readtoken1() into a real function of its own (inspired by a
similar change made by FreeBSD - the other internal routines
they moved out are expected to move out here as well soon.)
This change helps avoid gcc 5.3 demanded (not required!) volatile
noise which would slow down parsing. (from kre)
2016-03-20 22:56:39 +00:00
christos fc55d6ef49 sprinkle more volatile (needed for the rescue build with gcc-5.3) 2016-03-18 18:07:28 +00:00
christos 9c9fb684e0 s/sed/${SED}/ 2016-03-17 13:59:02 +00:00
christos c6f7781d4f put back the complex sed/awk since the code can't handle unsorted or repeated
entries (Rin Okuyama)
2016-03-17 13:54:31 +00:00
christos 1a51ba6b13 Put back awk, other scripts need it. 2016-03-16 23:02:23 +00:00
christos 702bd67153 We don't need all this magic to build the signals lists. Do the work at
compile time.
2016-03-16 23:01:33 +00:00
christos 90f49946eb Sprinkle volatile for gcc 5! 2016-03-16 22:36:40 +00:00
christos e448e849b1 Avoid warning by using gcc'ism 2016-03-16 22:36:21 +00:00
christos c1d7b11ef3 add Will Robinson comment. 2016-03-16 22:35:44 +00:00
christos b4f0f90a59 Avoid gcc-5 conversion warning (|= expands to int) 2016-03-16 22:25:05 +00:00
christos 0c73873e5c Keep redirs for subshells. 2016-03-16 21:20:59 +00:00
christos 3b6be3a09c parenthesize for safety. 2016-03-16 19:02:26 +00:00
christos 6f9ac0be1e Remove wrong unsigned cast, index can be negative. Cast char to int so that
gcc does not warn. Probably better to do the offset at runtime, but that
would cost more.
2016-03-16 17:01:39 +00:00
christos 58a5df4203 factor out common code in macro. 2016-03-16 15:48:01 +00:00
christos 5f0a664efb Revert (kind of) the change in 1.12 of the ancient mksyntax.sh script
(undoing the effect of that commit on syntax.h when it was
being dynamically generated) from 1996.   This means that the shell
parser is now locale independent, so scripts that work anywhere will
work consistently everywhere.   Inspired by a similar change in
FreeBSD's sh (from 2010) - the original change in the other direction
came from FreeBSD as well....   Note that this does not in any way
add any kind of support for locales to sh (which is a whole different
problem.) (from kre)
2016-03-16 15:45:40 +00:00
christos 8c844a2ebd PR/19832, PR/35423: Fix handling 0x81 and 0x82 characters in expansions
($VAR etc) that are used to generate filenames for redirections. (from kre)
2016-03-16 15:44:35 +00:00
christos 5b34482515 PR/50960: The || and && operators in $(( )) should always have a 0 or 1
result, never anything different. (from kre)
2016-03-16 15:43:38 +00:00