Commit Graph

851 Commits

Author SHA1 Message Date
abhinav 056b8cc767 Fix grammar in couple of sentences. 2016-10-25 13:01:59 +00:00
abhinav 602eb66c16 Remove unused variables.
Fixes the sh(1) build when DEBUG is enabled.
2016-10-23 08:24:27 +00:00
kre 09ccb20143 PR bin/51207 Only check for ELF bnaries in regular files. 2016-06-01 05:11:52 +00:00
kre cdb38c6b4d PR bin/43639
Redo earlier fix to only prohibit sourcing directories and block special files.
char specials (/dev/tty, /dev/null, ... incl /dev/rwd0a) and fifos are OK.

Posix actually requires that we find only readable files - that is not yet
implemented (doing it sanely, without opening the file twice, is going to
take some more modifications to code elsewhere).
2016-06-01 05:10:41 +00:00
kre 919211f1ab PR bin/47065 PR bin/39466
When the shell exits after an error (when that is the right thing for
it to do) ensure that it never does exit(0).
2016-06-01 02:50:02 +00:00
kre 95edbff793 PR bin/51145 PR bin/48489
More fixes to the shell parser to prevent empty simple commands (where
empty means no significant text at all) - as discussed on tech-userlevel
this still allows { } (which can be useful in function definitions, not
really anywhere else though) except in posix mode.  ( ) now generates
a syntax error, as should any other place where commands are required but
nothing is present.  (nb, redirections, var assignments, even var expansions
that expand to nothing, are all OK, and avoid the error - just comments, or
whits space, are not.)    This is (aside from allowing { } at all) all in
accordance with the posix spec.
2016-06-01 02:47:05 +00:00
kre 3b5786dccc More fallout from the fix for PR bin/48875 - this one found just by
code reading, rather than any actual real use case failing.

With this script
	f()
	{
		echo hello $1
	}

	exec 3>&1
	echo $(
		for i in a b c
		do
			echo @$i
			f >&3
		done >/tmp/foo
	)
	echo foo= $(cat /tmp/foo)

what should be output is

	hello
	hello
	hello

	foo= @a @b @c

but since the (my) 48875 fix the other day, we've been getting

	hello
	@b
	hello
	@c
	hello

	foo= @a

This fixes that.   I think (hope) this is the last of these fixes...
2016-05-13 10:32:52 +00:00
kre 698541ceb7 More work on file descriptors... This is the copyfd() cleanup.
copyfd() duplicates file descriptors - it used to be widely used,
but these days has seen its popularity dwindle.   Strip it of an
option that ceased to be variable (simplifying code) and cause all
its users to check its result, so it does not need to handle errors
itself (simplifying code further), and make it become a private inernal
routine in redir.c (all callers from other places have switched to a
more modern interface.)  Make sure we error() if N>&N fails (if N is closed.)
2016-05-12 13:31:37 +00:00
kre f112b7e1a3 Document that a N>&N (or N<&N) redirection turns off close-on-exec for N
(where N is a decimal fd number) either when used as
	some-command N>&N
(where fd N is passed, open, to some-command - which is obviously what is
wanted)

Or as
	exec N>&N
which effects fd N for all future commands.

Note that this means
	exec N>foo N>&N
returns to the old behaviour of leaving the file descriptor open
when commands are run (as do most shells, other than ksh) and works for
both new and old NetBSD shells (old ones never set close-on-exec, and treat
N>&N as a rather meaingless no-op request, and just ignore it), new ones
set close-on-exec on the first redirection, then disable it again on the
second.

Everything here about >& for output fds applies to <& for input ones.

OK christos@
2016-05-12 13:15:43 +00:00
kre 1755d8e4a6 Fix the implementation of the ?: $(( )) operator. It is right associative...
ok christos@
2016-05-12 13:05:18 +00:00
kre fa4e47f7be It was twenty(-two) years ago today
J.T. Conklin told us not this way
Berkeley 4.4 lite's changed which file
And it's traced differently all this while
2016-05-11 17:28:30 +00:00
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
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 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 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