Commit Graph

4141 Commits

Author SHA1 Message Date
cheusov f8f9f44113 Fix compilation failure with gcc-8.
Equal pointers to 'struct sigaction' should not be passed to sigaction(2).
  So, we pass NULL as an "old sigaction" structure.
2019-04-24 17:27:08 +00:00
kre bb952598c8 Bump date for previous. 2019-04-22 04:10:33 +00:00
kre 15fbfbbfd4 PR standards/40554
Update the description of the <& and >& redirection operators
(as indicated would happen in a message appended to the PR a week ago,
which received no opposition - no feedback).

Some rewriting of the section on redirects (including how the word
expansion of the "file" works) to make this simpler & more accurate.
2019-04-22 04:04:35 +00:00
uwe 2dbf991860 -compact must come last 2019-04-15 20:35:25 +00:00
kre 265b061776 PR bin/54112
Fix handling of "$@" (that is, double quoted dollar at), when it
appears in a string which will be subject to field splitting.

Eg:
	${0+"$@" }

More common usages, like the simple "$@" or ${0+"$@"} end up
being entirely quoted, so no field splitting happens, and the
problem was avoided.

See the PR for more details.

This ends up making a bunch of old hack code (and some that was
relatively new) vanish - for now it is just #if 0'd or commented out.
Cleanups of that stuff will happen later.

That some of the worst $@ hacks are now gone does not mean that processing
of "$@" does not retain a very special place in every hackers heart.
RIP extreme ugliness - long live the merely ordinary ugly.

Added a new bin/sh ATF test case to verify that all this remains fixed.
2019-04-10 08:13:11 +00:00
kre da46072561 Fix a logic botch that prevented "wait -n" (with no pid args) from
finding a job that had previously terminated.

Now in that case JOBWANTED is set on all jobs (since any will do)
which then simplifies a later test which no longer needs to special
case "wait -n".   Further, we always look to see if any wanted
job has already terminated, even if there are still running jobs
we can wait upon - if anything is already ready, that's where we start
harvesting (and finish, if -n is specified).
2019-03-26 13:32:26 +00:00
mlelstv a7b9d4bbeb When buffers are at least page sized, explicitely request page alignment. 2019-03-23 09:33:16 +00:00
gutteridge ee05db8218 pax: fix typos in comments in file_subs.c & tar.c
Stamp out "greengrocers' apostrophes" in various places (arguably there
are still more present, but style guides vary on that, and my energies
spent corralling wayward punctuation marks could be spent elsewhere).
2019-03-20 03:13:39 +00:00
gutteridge 01205dd53a pax: minor adjustments to comments in pat_rep.c
Amend several comments to match present reality (the functionality was
added back in 2007).
2019-03-20 02:50:50 +00:00
wiz c9960b6dc5 Whitespace nits. 2019-03-19 10:14:46 +00:00
gutteridge b96bf3b04c pax.1 & tar.1: add a minor clarification about "-s"
As a somewhat pedantic clarification, "-s" does not accept backslashes
as delimiters. (While here, also make the macro use of an expression
shared between pax.1 and tar.1 consistent.)
2019-03-19 00:36:14 +00:00
gutteridge 76144f4ebd pax.1: document the "s" flag of the "s" option
Note the "s" option has an "s" flag that "prevents substitutions from
being performed on symbolic link destinations". Carry over r. 1.25 from
christos@ and part of r. 1.26 from wiz@ from tar.1, since this
functionality is available in pax as well as tar.
2019-03-19 00:12:08 +00:00
kre b0172d2346 Deal with overflow when the sleep duration given is a simple
integer (previously it was just clamped at the max possible value).
This would have caused
	sleep 10000000000000000000
(or anything bigger) to have only actually slept for 9223372036854775807
secs.   Someone would have noticed that happen, one day, in some other
universe.

This is now an error, as it was previously if this had been entered as
	sleep 1e19

Also detect an attempt to sleep for so long that a time_t will no longer
be able to represent the current time when the sleep is done.

Undo the attempts to work around a broken kernel nanosleep()
implementation (by only ever issuing shortish sleep requests,
and looping).   That code was broken (idiot botch of mine) though
you would have had to wait a month to observe it happen.  I was going
to just fix it, but sanity prevailed, and the kernel got fixed instead.

That allows this to be much simplified, only looping as needed to
handle dealing with SIGINFO.   Switch to using clock_nanosleep()
to implement the delay, as while our nanosleep() uses CLOCK_MONOTONIC
the standards say it should use CLOCK_REALTIME, and if that we
ever changed that, the old way would alter "sleep 5" from
"sleep for 5 seconds" to "sleep until now + 5 secs", which is
subtly different.

Always use %g format to print the original sleep duration in reports of how
much time remains - this works best for both long and short durations.
A couple of other minor (frill) mods to the SIGINFO report message as well.
2019-03-10 15:18:45 +00:00
kre 8d9b075152 The previous commit was obviously made by a broken mindless automoton
with an IQ that underflows when one attempts to enter it as an
unnormalised 160 bit long long double...

Whoever would believe that (~0 & anything) was a meaningful thing
to write?   And three times in one #define.   That could not possibly
have been me, could it?

Simplify, simplify, simplify.		NFC.
2019-03-01 06:15:01 +00:00
kre f2dc75406d Inspired by (really the need for) Maya's patch to pkgsrc/shells/bash
to allow bash to build fdflags on Solaris 10, here are some mods that
fix that, and some other similar issues in the NetBSD version of fdflags.

The bash implementation of fdflags is based upon the one Christos did for
the NetBSD sh, so the issues are similar ... the NetBSD sh cannot yet
(easily anyway) build on anything except NetBSD, so this change makes
no current difference at all (just adds some compile time tests (#ifdef)
which always work out the way things did before, when built on NetBSD).

However, there is no system on which any modern shell can hope to work
which does not support close on exec, or fcntl(F_SETFD,...) to set it.
The O_CLOEXEC and FD_CLOEXEC definitions might not exist, but close on
exec can still be manipulated.   Since the primary rationale for
the fdflags builtin was to be able to manipulate that state bit from
scripts, it would be annoying to lose that one, and keep all the (less
important) others, just because O_CLOEXEC is not defined, so do the
fix (workaround) a different way than was done in the bash patch.

Further, more than fdflags() will fail if O_CLOEXEC is not defined,
so handle that as well.

Also fix another oddity ... (noticed by reading the code) - if
fcntl(F_GETFL,...) returned any bits set that we don't understand,
the code was supposed to simply print their values as a hex constant,
when fdflags is run with -v.    However, the getflags() function was
clearing all bits that the code did not know about ... so there is
no way any unknown bit could ever make it out to be printed.  Handle
that a different way - instead of clearing unknown bits, clear any
bits that get returned which we understand, but do not want to deal
with (stuff like O_WRONLY, which should not be returned from the
fcntl(), but who knows...)   Leave any unknown bits that happen to be
set, set, so that printone() can display them if appropriate.
(This is most likely to happen when running an older shell on a new
kernel where the kernel supports some new flag that the shell has
not been taught to understand).

NFCI that anyone should notice anytime soon.
2019-03-01 05:23:35 +00:00
kre 256d645df3 Finish the fixes from Feb 4 for handling of random data that
matches the internal CTL* chars.

The earlier fixes handled CTL* char values in var expansions,
but not in various other places they can occur (positional
parameters, $@ $* -- even potentially $0 and ~ expansions,
as well as byte strings generated from a \u in a $'' string).

These should all be correctly handled now.   There is a new
ISCTL() macro to make the test, rather than using the old
BASESYNTAX[c]==CCTL form (which us still a viable alternative)
as the new way allows compiler optimisations, and less mem
references, so it should be smaller and faster.

Also, be sure in all cases to remove any CTLESC (or other)
CTL* chars from all strings before they are made available
for any external use (there was one case missed - which didn't
matter when we weren't bothering to escape the CTL* chars at
all.)

XXX pullup-8 (will need to be via a patch) along with the Feb 4 fixes.
2019-02-27 04:10:56 +00:00
kre 2ec3f71485 DEBUG mode only change. When pretty-printing a word from a parse
tree, don't display a CTLESC which is there only to protect a CTL*
char (a data char that happens to have the same value).  No actual
CTL* chars are printed as data, so no escaping is needed to protect
data which just happens to look the same.  Dropping this avoids the
possibility of confusion/ambiguity in what the word actually contains.

NFC for any normal shell build (very little of this file gets compiled there)
2019-02-14 13:27:59 +00:00
kre 727a664bee Add the "specialvar" built-in command. Discussed (well, mentioned
anway) on tech-userlevel with no adverse response.

This allows the magic of vars like HOSTNAME SECONDS, ToD (etc) to be
restored should it be lost - perhaps by having a var of the same name
imported from the environment (which needs to remove the magic in case
a set of scripts are using the env to pass data, and the var name chosen
happens to be one of our magic ones).

No change to SMALL shells (or smaller) - none of the magic vars (except
LINENO, which is exempt from all of this) exist in those, hence such a
shell has no need for this command either.
2019-02-14 11:15:24 +00:00
kre ec83c7c484 Delete a no-longer-used #define that referred to a struct field that
no longer exists.   Also correct a couple of typos in comments.    NFC.
2019-02-13 21:40:50 +00:00
kre 72c6780ab3 Remove a function prototype which was added to <histedit.h> in 2005.
I think we can trust it to be stable by now, and doin't need the dup.
2019-02-10 19:21:52 +00:00
kre 4d2988311a Add a check that the file descriptor mentioned in a N> or N< type
redirect operator is within range of what the code tree node can
hold.   Currently this is a no-op change (the new error can never
occur) as the code already checks that N is in range for an int
(and errors if not) and the field in the node in which we store N
is also an int, so we cannot overflow - but fd's do not really need
to be that big (the max a typical kernel supports is < 10000) so
this just adds validation in case it ever happens that we decide we
can save some node size (ie: sh memory) by making that field smaller.

Note this is parse time error detection, and has no bearing upon
the execution time error that will occur if a script attempts to use
an fd that exceeds the process's max fd limit.

NFCI (for now anyway.)
2019-02-09 09:50:31 +00:00
kre 83735e242c DTRT when dynamically generated variables return "unset" instead of
a value.   There are none which do that at the minute, so this is a NFCI
change, which is just making the code correct even though nothing
currently triggers any bugs.
2019-02-09 09:38:11 +00:00
kre 733a465e66 DEBUG mode change only. Add one extra trace point. NFC for normal builds. 2019-02-09 09:34:43 +00:00
kre 750dbf249a When an interactive shell exits due to an EOF on stdin, send a newline
to stderr (to follow the previous prompt) and cleanup more nicely.
2019-02-09 09:33:20 +00:00
kre ccf5ffdbe9 In the unlikely event that restarting a job fails (the fg bg and various
%x commands) generate the most useful error message (from errno value)
rather than whichever happened last.

In posix mode, cause the "jobs" command to delete records of completed
jobs it reports on (as posix requires) as is done in interactive shells.
We don't (won't) do this in !posix mode, as the ability to throw in a
"jobs" command in a script to debug what is happening is too useful to
lose -- and any script that is relying on "jobs" instead of "wait" to
cleanup background processes (from the sh jobs table, sh always collects
zombies from the kernel) is absurd and not worth considering (besides
which I've never seen one).
2019-02-09 09:31:33 +00:00
kre b4a242b5e2 KNF - white space changes, indent using tabs not spaces. NFC. 2019-02-09 09:20:47 +00:00
kre 39879a1c65 DEBUG mode build changes - add extra trace output.
NFC for any normal shell build.
2019-02-09 09:17:59 +00:00
kre f42ddab0ac Delete extern decl for trap[] - hasn't been needed for a while now. 2019-02-09 09:15:22 +00:00
kre 3dbd860142 Allocate alias pointers for qsort() to use on the stack, rather than
directly via malloc() so they get cleaned up correctly on error/intr.

NFCI.
2019-02-09 09:11:07 +00:00
kre e8999de45c INTON / INTOFF audit and cleanup.
No visible differences expected - there is a remote chance that
some internal lossage may no longer occur in interactive shells
that receive SIGINT (untrapped) at inopportune times, but you would
have had to have been very unlucky to have ever suffered from that.
2019-02-09 03:35:55 +00:00
wiz 169c18b89d Remove leading zero from date. 2019-02-04 12:18:36 +00:00
kre 4084f829ec PR bin/53919
Suppress shell error messages while expanding $ENV (which also causes
errors while expanding $PS1 $PS2 and $PS4 to be suppressed as well).

This allows any random garbage that happens to be in ENV to not
cause noise when the shell starts (which is effectively all it did).

On a parse error (for any of those vars) we also use "" as the result,
which will be a null prompt, and avoid attempting to open any file for ENV.

This does not in any way change what happens for a correctly parsed command
substitution (either when it is executed when permitted for one of the
prompts, or when it is not (which is always for ENV)) and commands run
from those can still produce error output (but shell errors remain suppressed).
2019-02-04 11:16:41 +00:00
kre 513431119a Add a couple of comments. NFC. 2019-02-04 09:56:48 +00:00
kre 58e34de6bb Fix an old bug (very old) that was made worse in 1.128 (the "${1+$@}"
fixes) where a variable containing a CTL char (the only possibility used
to be CTLESC (0x81)) would lose that character if the variable was expanded
when "set -f" (noglob) was in effect.

1.128 made this worse by adding more 0x8z values (a couple more) which would
see the same behaviour, and one of those was noticed by Martijn Dekker.

The reasoning was that when noglob is on, when a var is expanded, there are
no magic chars, so (apparently) no need to escape anything.  Hence nothing
was escaped .. including any CTL chars that happened to be present.  When
we later rmescapes() the CTL chars that we expect might occur are summarily
removed - even if they weren't really CTL chars, but just data masquerading.

We must *always* escape any CTL char clones that are in the var value,
no matter what other conditions apply, and what we expect to happen next.

While here, fix rmescapes() (and its $(()) clone, rmescapes_nl()) to
be more robust, less likely to forget to delete anything (which was
not the issue here, just the reverse) and in a DEBUG shell, have the
shell abort() if it encounters something in rmescapes() it is not
anticipating, so the code can be made to handle it, or if it should
not happen, we can find out why it did.

XXX pullup -8 (but will need to be via patch, code is quite different).
2019-02-04 09:56:26 +00:00
mrg f0885992ce - bump buffer sizes to avoid potential truncation issues 2019-02-04 04:36:41 +00:00
mrg 1fcf7be45f - use -Wno-error=implicit-fallthrough with GCC7. 2019-02-04 04:05:15 +00:00
mrg 684b182f81 compare pointers with NULL not '\0'. 2019-02-01 08:29:03 +00:00
wiz 539951b361 Fix typos; use American spelling consistently. Remove an unnecessary macro. 2019-01-30 10:28:50 +00:00
mrg d9150b46de adjust the open flags available for dd to match actual reality
of what matters.  remove "search" for now, since O_SEARCH has
no backend.  document them all.
2019-01-30 01:40:02 +00:00
wiz d7fa63f1ef Sort sections. 2019-01-27 17:42:53 +00:00
christos 4b72dcea07 cast to intmax_t instead of long, since time_t is "long long" 2019-01-27 02:00:45 +00:00
martin 4d553ef408 Explicitly cast time_t to match format string - should fix the build on
some 32bit architectures.
2019-01-26 18:14:22 +00:00
kre a7fe3a0309 While cute, the previous version is not really safe.
After all, a system might want to sleep for several
thousand years on a spaceship headed to a distant
solar system...

So, remove the pause() code, deal with limits on the
range (it is just an int) that can be passed to sleep()
by looping, and do a much better job of checking for
out of range input values.

With this change sleep(1) should work for durations
up to something more than 250 billion years.  It
fails (at startup, with an error) if the requested
duration is beyond what can be handled.

Here no changes at all related to locales and arg
parsing.    Still for another day.
2019-01-26 15:20:50 +00:00
kre 6f62877535 Adjust the way the arg string is parsed in the "not entirely
integer" case, so we avoid adjusting the locale of sleep,
and generally be more reliable and simpler.

In addition, deal with weirdness in nanosleep() when the
interval gets long, by avoiding using it.  In this version
when the sleep interval < 10000 seconds, we use nanosleep()
as before, for delays longer than that we use sleep() instead,
and ignore any fractional seconds.

We avoid overflow problems here by not bothering to sleep
at all for delays longer than 135 years (approx) and simply
pause() instead.   That sleep never terminates in such a
case is unlikely to ever be observed.

This commit makes no decision on the question of whether
the arg should be interpreted in the locale of the user,
or always in the C locale.   That is for another day.
2019-01-26 15:19:08 +00:00
kre 4e25d54034 lexical analysis fixes. This fixes the tests just committed in
src/tests/bin/sh/t_here.sh

The "magicq" magic was all wrong - it cannot be simply a parameter
to readtoken1() as its value needs to alter during that routine
(eg: when magicq is set - processing here doc text, or whatever)
and we encountered ${var%pattern} "magicq" needs to be off for
"pattern" - and it wasn't.

To handle this magicq needs to be included in the token stack struct,
and simply init'd from the arg to readtoken1 (which we rename).
Then it can be manipulated as required.

Once we no longer have that problem, some other issues can be cleaned
up as well (some of this unbelievably fragile code was attempting to
cope with this in various ad-hoc - and mostly broken - ways).

Also, remove the magicq parameter from parsebackq() - it was not
used (at all) and should never be, a command substitution, wherever
it appears, always starts a new parsing context.  How that applies
to old style command substitutions is less clear, but until we see
some real examples where we're not doing the right thing (slightly
less likely now than before ... nothing has changed here in the
way command substitutions are parsed, but quoting in general is
slightly better) I don't plan on worrying about it.

There are a couple of other minor cleanups, which make no actual
difference (like adding () around the use of the parameter in the
RETURN macro ... which is generally better, but makes no difference
here as the param is always a simple constant.

All the current ATF tests pass.
2019-01-22 14:32:17 +00:00
kre a672c6e148 NFCI - DEBUG mode only change.
Add tracing of lexical analyser operations.   This is deliberately
kept out of the normal "all on" set as it makes a *lot* of noise
when enabled (especially in verbose mode) - but when needed, it
helps (evidence for which is coming soon).

As usual, no doc, you need the sources (and of course, a specially
built sh to even be able to enable it.)
2019-01-22 13:48:28 +00:00
kre f2dc4639fa DEBUG mode shell cleanups (NFC for any normal shell).
Add an error DEBUG trace in exraise() (when the shell has detected
some error or signal, and is aborting what it is doing)

Fix an arith error in DEBUG bit assignments (harmless as we haven't
reached the limit of flags yet), and add some missing (recently added)
debug flags so they are turned on when the user (ie: me) asks for
"everything".
2019-01-21 14:29:12 +00:00
kre 9cef82b269 Fix an amazing crazy botch (of mine) when expanding prompt strings
(PS1 etc) which, if the shell were already exiting, and a prompt
were to be expanded (which only really happens if -x is enabled,
and an exit trap is set, so the commands in the trap need PS4
expanded and written, last thing, before the shell exits) the shell
would instead simply exit when it finished expanding PS4 (before
even writing it, or the xtrace output).

There were more conditions required to set up the environment for
this to actually occur (it seems to only happen when the exit trap
is set in a function, called in a command substitution) but that's
unimportant, the code was nonsense.

Problem noticed by Martijn Dekker.

XXX pullup -8
2019-01-21 14:24:44 +00:00
kre 6b3f958390 When we are about to execute something, and the traps are invalid
(which means this is the very first execution in a new subshell)
clear the traps completely, unless the command is "trap".   We were
allowing any special builtin, which was probably harmless, but not
intended.

Also (though not required) permit "command trap" and "eval trap"
and combinations thereof, because they might be useful, and there is
no particular reason why not.   This is all a part of making t=$(trap)
work as POSIX requires, but almost nothing beyond that.  The "trap"
command must be alone (modulo eval and command) in the subshell for
the exception to apply, no t=$(trap; echo) or anything like that.

Martijn Dekker asked for "command trap" to work (no idea why though,
it converts "trap" from being a special builtin, to a normal one,
which means an error won't cause the shell to exit ... if there's
an error, the "trap" command won't do anything useful, and as we
permit no more commands (for this special treatment) the shell is
going to exit anyway, this difference is not really significant.
2019-01-21 14:18:59 +00:00
kre f499a5853f Add an explanation of the error (warning)
RANDOM initialisation failed
when the shell might print after RANDOM has been reseeded
(which includes at sh startup) the next time RANDOM is accessed.
It indicates that /dev/urandom was not available or did not
provide data - in that case, sh uses a (weak) seed made out of
the pid and time (but otherwise nothing else changes).
2019-01-21 14:09:24 +00:00
kre dd761e12dc Fix an off by one buffer length problem. Fortunately, it was off by
one in the "safe" way (it was ensuring the buffer always ended in 2 \0
characters ... one is enough.)   This could affect the expansions of
LINENO RANDOM and SECONDS, though only if they have at least 8 digits
(and then, only sometimes).   RANDOM thus is safe, as it never produces
a number with more than 5 digits, you'd need a script with 10000000
lines before there might be an issue with LINENO (and even autoconf
generated scripts don't generally get that bit) and a shell would need
to be running for almost 4 months for SECONDS to climb that high.

Nevertheless: XXX pullup -8.
2019-01-21 13:27:29 +00:00
kre 66ac24c3bf When we exit from running off the end of the input file (which
includes typing ^D) make sure LINENO is set to indicate the last
(actually one past last) line in the input file, rather than
whatever it was set to by the last command that was actually
executed (which could be some line in a function defined in
some other file).

No effect on exit via an explicit exit command - that would already
set the line number correctly.
2019-01-19 14:20:22 +00:00
kre f04239a697 Allow the decimal radix character '.' to work, regardless of
what the current locale's radix character happens to be,
while still allowing locale specific entry of fractional
seconds (ie: if you're in locale where the radix character
is ',' you san use "sleep 2.5" or "sleep 2,5" and they
accomplish the same thing).

This avoids issues with the "sleep 0.05" in rc.subr which
generated usage messages when a locale that does not use
'.' as its radix character was in use.

Reported on netbsd-users by Dima Veselov, with the problem
diagnosed by Martin Husemann

While here, tighten the arg validity checking (3+4 is
no longer permitted as a synonym of 3) and allow 0.0
to mean the same thing as 0 rather than being an error.

Also, make the SIGINFO reports a little nicer (IMO).

The ATF tests for sleep all pass (not that that means a lot).
2019-01-19 13:27:12 +00:00
kre cf75e20323 Add some error checking, and stop assuming what the input
will necessarily contain.   Allow defined nodes to use any
intN_t or unintN_t (as well as plain old int) data types
in fields (along with the others that are permitted).

Note: this script is a part of the build procedure for /bin/sh,
the modified version generates the exact same output files
(for the unaltered input specifications) as the previous one
did, hence no visible change is expected (or even possible).

While there is a tiny chance that some host shell will fail
to be able to run this script while building, the script still
uses nothing even slightly exotic, and is much more conservative
than other scripts used during the build process, so there should
be no issues there either.
2019-01-19 13:08:50 +00:00
kre e8ed7c888d Finish (hopefully) the second half of 1.47 ... make sure
that when traps are marked as invalid, we never use them
for anything except output from the trap command.

Fixes issues where sub-shells of shells which use traps
(eg: to trap SIGPIPE) can end up looping forever if the
signal occurs in a sub-shell (where the trap is supposed
to be reset to its default).   Reported, and mostly
analyzed by Martijn Dekker.
2019-01-18 06:28:09 +00:00
kre 1a9b5ae61d Redo 1.65 in a simpler way. This is the bit rot avoidance code
that is #if 0'd and (still) has never been compiled (most likely
never will be.)

While here, in the same uncompiled code, deal with line number
counting.   Whether this is correct depends upon how this code
is used, and as it never is (and never has been since line numbers
first started being counted), this is somewhat speculative, but
it seems likely to be the correct way to handle things.

NFC (this code is still all #if 0).
2019-01-16 07:14:17 +00:00
kre 20c0839ae2 Don't use quoteflag when deciding if the word after an alias
should be looked up as a potential following alias - if the first
expands to a string that ends with a space (any space, quoted or
not) then the next word is to be treated as an alias candidate.
(POSIX was to specify only unquoted spaces, but is now going to
leave that unspecified, and the "any space" version turns out to
be more useful.

And besides, the quoteflag test didn't work properly, and would
have been very messy to fix ... if in a word (as if we have a
quoted space) it means that the word has been quoted, which meant
that quoted spaces were correctly detected, but it outside a word,
it just means that the previous word was quoted, so it would sometimes
reject alias lookup on the next word in cases where it is unquestioned
it should be done.
2019-01-15 14:23:56 +00:00
kre be7c7b5cbb pgetc_linecont() needs to use pgetc() rather than pgetc_macro()
so the fake char returned by the latter when an alias ends (which
is there so we can correctly avoid alias recursion) is correctly
ignored where it is not wanted.
2019-01-15 14:17:49 +00:00
kre 39225745fe Correct an (old) typo in a comment. NFC - it is just a comment. 2019-01-09 11:09:16 +00:00
kre ed405f1d4e Fix the code taken from FreeBSD 2 revisions back, which fixed
aliases, to actually do what it was supposed to do, and not just
come close by accident.   (How broken this was, while still seeming
to work perfectly most of the time was truly amazing!)

This corrects the behaviour of an alias defined with a blank char
as the last of its value, to correctly do an alias lookup on the
word that follows the alias.
2019-01-09 11:08:09 +00:00
kre 6046dde455 Update some dead (#if 0'd) code that is never called to
cope with the changes made in the previous revision, in an
attempt to avoid bit rot.

Untested (uncompiled) - though it should work.

NFC: this change doesn't get compiled, let alone used.
2019-01-09 11:04:54 +00:00
kre a559cfeaae A similar fix to that added in 1.169 of eval.c, but here for when
processing command substitutions.   If there is an error while processing,
the any pending queued input should be discarded.   From FreeBSD.
2019-01-09 10:59:20 +00:00
kre a582e232a9 When an error occurs in a builtin from which we do not exit
(a normal builtin, though those are not genrally an issue for
this problem, or a special builtin that has been prefixed by "command")
make sure that we discard any pending input that might have been
queued up, but not yet processed.

We had the mechanism to fix this from when expansion of PS1 etc
was added (which has a similar problem to deal with) - all taken
from FreeBSD - but did not bother to use it here until now...

This fixes an error detected by newly added ATF tests of the eval
builtin, where
	eval 'syntax error
		another command'
would go ahead and evaluate "another command" which should not
happen (note: only when there was a \n between the two).
2019-01-09 10:57:43 +00:00
christos ab87f51e55 PR/53837: Michael Scholz: src/bin/csh/func.c from current has a superfluous
fprintf
2019-01-06 01:22:50 +00:00
christos afec7330e3 put back x in xrealloc 2019-01-05 16:56:25 +00:00
christos 1767ce60a2 Welcome to the 21th century csh: retire "ptr_t" now that we have "void *" 2019-01-05 16:54:00 +00:00
maya bd073e6366 Remove Free, s/xfree/free/.
Standard C says that free should be a no-op for a NULL pointer, so
we don't need an extra function to do this.

While here, add an XXX about a wrong sounding comment
2019-01-05 10:51:06 +00:00
maya e814216d15 Unifdef compatibility for broken realloc.
No binary change
2019-01-04 19:13:58 +00:00
gutteridge 6326f9f26c Clarify a sentence about the error handling of cp(1)'s -p option.
Addresses PR bin/40336.
2018-12-23 01:29:23 +00:00
kre af2cfdd7cf Reverse a decision made when the printsignals() routines from
kill and sh were merged so that the shell (for trap -l) and
kill (for kill -l) can use the same routine, and site that function
in the shell, rather than in kill (use the code that is in kill as
the basis for that routine).   This allows access to sh internals,
and in particular to the posix option, so the builtin kill can
operate in posix mode where the standard requires just a single
character (space of newline) between successive signal names (and
we prefer nicely aligned columns instead)..

In a SMALL shell, use the ancient sh printsignals routine instead,
it is smaller (and very much dumber).

/bin/kill still uses the routine that is in its source, and is
not posix compliant.   A task for some other day...
2018-12-12 20:22:43 +00:00
kre 0e129c91e0 More fixes for the SYNPOSIS of the readonly built-in.
The SYNOPSIS for "readonly -q" cannot have the -q be
optional ...   Also harmonise the output appearance with
that of the export command.

wiz: have at it...
2018-12-12 12:56:17 +00:00
kre 80657b2503 Fix Oo Op Oc syntax error (which seemed to work OK to me....)
Pointed out by wiz@
2018-12-12 12:30:59 +00:00
kre 95f39b9c19 The time has come, the Walrus said... (but no shoes,
ships, or sealing wax required).   No oysters either.

I have not taken the Magical Mystery Tour, but I
say it anyway...
2018-12-12 12:16:42 +00:00
kre 150d8297c3 Implement:
readonly -q VAR...
	readonly -p VAR...
	export -q [-x] VAR...
	export -p [-x] VAR...

all available only in !SMALL shells - and while here, limit
"export -x" to full sized shells as well.

Also, do a better job of arg checking and validating of the
export and readonly commands (which is really just one built-in)
and of issuing error messages when something bogus is detected.

Since these commands are special builtin commands, any error
causes shell exit (for non-interactive shells).
2018-12-12 11:51:33 +00:00
kre 64c9a713d8 Fix a botch made in 1.70 (a bit over a week ago) where
var=foo; readonly var=new
now fails.

If var was already set, an attempt to make it readonly, and assign it
a new value at the same time, failed - the readonly flag was set too soon.

Pointed out by Martijn Dekker (thanks).

Also, while here, add a couple of comments.
2018-12-12 07:56:57 +00:00
kre e3847ee4a9 PR standards/42829
Implement parameter and arithmetic expansion of $ENV
before using it as the name of a file from which to
read startup commands for the shell.   This continues
to happen for all interactive shells, and non-interactive
shells for which the posix option is not set (-o posix).

On any actual error, or if an attempt is made to use
command substitution, then the value of ENV is used
unchanged as the file name.

The expansion complies with POSIX XCU 2.5.3, though that
only requires parameter expansion - arithmetic expansion
is an extension (but for us, it is much easier to do, than
not to do, and it allows some weird stuff, if you're so
inclined....)   Note that there is no ~ expansion (use $HOME).
2018-12-11 13:31:20 +00:00
christos eb31074ab5 comment out unused. 2018-12-09 17:33:38 +00:00
kre 78cb4ed28f evert previous, linux build problem confirmed fixed by
update to mkinit.sh (to 1.10).

Or more correctly, revert & fix - turns out that there was an off by one
(failure to adjust for other changes -- in a value printed by debug mode
trace output).

NFC.
2018-12-05 22:25:38 +00:00
kre 02e7608416 Use printf, rather than echo (via echo redefined as a function that
uses printf for simplicity).

This script runs using the build host's shell, and echo, and so must
deal with all of the absurdity that different versions of echo dumb
upon us.

This is the underlying cause of the linux build failure that gson@ reported.
2018-12-05 09:20:18 +00:00
kre 24e0ca1d07 NFC (except that it should, I am guessing, fix compilation on
some versions of liux) - DEBUG mode change:  Delete a (relatively new)
trace point (temporarily anyway) which mkinit (a script run using the
host's /bin/sh) apparently cannot handle correctly on (some release of)
linux (it is fine with the NetBSD shell).

I don't know which linux version has a shell with this problem
(or whether it is a mkinit issue that only works by fluke on NetBSD)

Problem reported by gson@
2018-12-05 03:14:28 +00:00
kre 370f00cafc Alter a design botch when magic (self modifying) variables
were added to sh ... in other shells, setting such a variable
(for most of them) causes it to lose its special properties,
and act the same as any other variable.   I had assumed that
was just implementor laziness...   I was wrong.

From now on the NetBSD shell will act like the others, and if vars
like HOSTNAME (and SECONDS, etc) are used as variables in a script
or whatever, they will act just like normal variables (and unless
this happens when they have been made local, or as a variable-assignment
as a prefix to a command, the special properties they would have had
otherwise are lost for the remainder of the life of the (sub-)shell
in which the variables were set).

Importing a value from the environment counts as setting the
value for this purpose (so if HOSTNAME is set in the environment,
the value there will be the value $HOSTNAME expands to).

The two exceptions to this are LINENO and RANDOM.   RANDOM
needs to be able to be set to (re-)set its seed.  LINENO needs to
be able to be set (at least in the "local" command) to achieve
the desired functionality.   It is unlikely that any (sane) script
is going to want to use those two as normal vars however.

While here, fix a minor bug in popping local vars (fn return) that need
to notify the shell of changes in value (like PATH).

Change sh(1) to reflect this alteration.  Also add doc of the
(forgotten) magic var EUSER (which has been there since the others
were added), and add a few more vars (which are documented
in other places in sh(1) - like ENV) into the defined or used
variable list (as well as wherever else they appear).

XXX pullup -8
2018-12-04 14:03:30 +00:00
martin 75d47b0d94 Make pendingsigs forward declaration match the definition. 2018-12-03 10:53:29 +00:00
kre 13fc5c1bcd Cleanup traps a bit - attempt to handle weird uses in traps, such
as traps that issue break/continue/return to cause the loop/function
executing when the trap occurred to break/continue/return, and
generating the correct exit code from the shell including when a
signal is caught, but the trap handler for it exits.

All that from FreeBSD.

Also make
	T=$(trap)
work as it is supposed to (also trap -p).

For now this is handled by the same technique as $(jobs) - rather
than clearing the traps in subshells, just mark them invalid, and
then whenever they're invalid, clear them before executing anything
other than the special blessed "trap" command.   Eventually we will
handle these using non-subshell command substitution instead (not
creating a subshell environ when the commands in a command-sub alter
nothing in the environment).
2018-12-03 06:43:19 +00:00
kre 522d163154 Fix "export -x" (and its consequences) to behave as originally
intended (and as documented) rather than how it has been behaving
(which was not very rational.)   Since it is unlikely that anyone
is using this, the change should be mostly invisible.

While here, a couple of other minor cleanups:
	. One call of geteuid() is enough in choose_ps1()
	. Fix a typo in a comment
	. Improve appearance (whitspace changes) in find_var()
2018-12-03 06:42:25 +00:00
kre c9f333ad14 Yet another foray into the mysterious world of $@ -- this time
to fix the (unusual) idiom "${1+$@}"  (the quotes are part of it).
This seems to have broken about 5 or 6 years ago (somewhere
between -6 and -7), I believe.

Note this is not the same as "$@" and also not the same as ${1+"$@"}
(much more common idioms) which both worked.

Also attempt to deal with "" more correctly, especially when it
appears adjacent to "$@" (or one of the similar constructs.)

This stuff is still all as ugly and hackish (and fragile) as is
possible to imagine, but in an effort to allow some of the weirdness
to eventually go away, the parser output has been made more
regular and all quoted (parts of) words always now start with
CTLQUOTEMARK and end with CTLQUOTEEND regardless of where the
quotes appear.

This allows us to tell the difference between """$@" and "$@"
which was impossible before - yet they are required to generate
different output when there are no args (when "$@" simply vanishes).

Needless to say that change had ramifications all over the place.
To simplify any similar change in the future, there are some new
macros that can generally be used to detect the "noise" data when
processing words, rather than open coding that every time (which
meant that there would *always* be one which missed getting
updated...)

Several other bugs (of my making, and older ones) are also fixed.

The aim is that (aside from anything that is detecting the cases
that were broken before - which were all unlikely uses of sh
syntax) these changes should have no external visible impact.

Sure...
2018-12-03 06:41:30 +00:00
kre 021ba5091c Revamp aliases - as dumb an idea as they are, if we're going
to have them, they should work as documented, not cause core
dumps, reference after free, incorrect replacements, failing
to implement alias after alias, ...

The big comment that ended:
	  This is a good idea ------- ***NOT***
and the hack it was describing are gone.

Note that most of this was from original CVS version 1.1
code (ie: came from the original import, even before 4.4-Lite
was merged.   That is, May 1994.  And no-one in 24.5 years
noticed (or at least complained about)  all the bugs (or at
least, most of them)).

With these changes, aliases ought to work (if you can call it
that) as they are expected to by POSIX.   Now if only we could
get POSIX to delete them (or make them optional)...

Changes partly inspired by similar changes made by FreeBSD,
(as was the previous change to alias.c, forgot ack in commit
log for that one, apologies) but done a little differently,
and perhaps with a slightly better outcome.
2018-12-03 06:40:26 +00:00
kre 7f63ac72c5 When forking a child shell, arrange for errors/exit to always unwind
to the main handler, rather than wherever the parent shell would go.

nb: not needed for vfork(), after vfork() we never go that path - which
is good or we'd be corrupting the parent's handler.

This allows the child to always exit (when it should) rather than being
caught up doing something else (and while it would eventually exit, the
status would be incorrect in some cases).

One test is:
	sh -c 'trap "(! :) && echo BUG || echo nobug" EXIT'
from Martijn Dekker

Fix from FreeBSD (missed earlier).

XXX - 2b part of the 48875 pullup to -8
2018-12-03 02:38:30 +00:00
kre 154c1ed72b Fix the worst of the bugs in alias processing. This has been in sh
since this code was first imported (May 1994) (ie: before 4.4-Lite)

There is (much) more coming soon (the big ugly comment is going away).

This one has been separated out, as it can easily cause sh
core dumps, so needs:

XXX pullup-8

(the other changes to aliases probably will not get that.)
2018-12-02 10:27:58 +00:00
kre 17e8cd6768 Rename the internal function "makename" to "makeword" to better reflect
what it actually does (makearg would have been an alternative).
While here, notice the one remaining place where it should have been
used, but was left open coded, and consume that one.

NFCI.
2018-12-01 07:02:23 +00:00
kre ae1a47886f NFC. Need a grain of const 2018-12-01 01:21:06 +00:00
kre c074191fdd NFC. KNF. return (x) -> return x 2018-12-01 01:20:05 +00:00
kre d02195817d It is not only the EXIT trap we need to check for when deciding no
fork is required, but any trap   (dumb mistake...)

XXX - include in 48875 pullup to -8
2018-11-30 23:22:45 +00:00
christos a4c163faee Add base-256 decoding support (Micha Gorny) 2018-11-30 00:53:11 +00:00
kamil e405eb35a7 Fix typo: O_ALTIO -> O_ALT_IO
Noted by @jbeich via GitHub.
2018-11-26 20:03:39 +00:00
kre d7fa5d2fb9 Make it be that an empty command is treated as a regular builtin
for the purposes of any redirects it might have -- ie: as posix
requires, make the redirects appear to have been executed in a subshell
environment, so if one fails, aside from a diagnositc msg, all the
running script sees is a command that failed ($? != 0), rather
that having the shell exit which used to happen (the empty command was
being treated as a special builtin).

Continue to treat the empty command as special for the purposes of
var assigns it might contain (those are not executed in a sub-shell
and persist) - an error there (eg: assigning to a readonly var) will
continue to cause the shell (non-interactive shell) to exit.

This makes the NetBSD shell behave like all other (reasonably modern)
shells - fix method (not the implementation, details differ) taken from
FreeBSD who fixed this back in early 2010.    Problem pointed out
in (non-list) mail by Martijn Dekker.
2018-11-26 13:47:39 +00:00
kre 00655d6c45 Fix the <> redirection operator, which has been broken since it was
first implemented in response to PR bin/4966  (PR Feb 1998, fix Feb 1999).

The file named should not be truncated.

No other shell truncates the file (<> was added to FreeBSD sh in Oct 2000,
and did not include O_TRUNC) and POSIX certainly does not suggest that
should happen (just that the file is to be created if it does not exist.)

Bug pointed out in off-list e-mail by Martijn Dekker
2018-11-23 23:41:20 +00:00
kre c43d5e9f9c Handle eval $'continue\ncommand' (and similar) in a loop correctly ...
"command" should not be executed.  (The issue affects multi-line
eval strings only - ie: commands after the next \n are not skipped).

Bug noted by Martijn Dekker in off-list e-mail.

Fix from FreeBSD:
src/bin/sh/eval.c: Revision 272983 Sun Oct 12 13:12:06 2014 UTC by jilles
2018-11-23 23:37:22 +00:00
kre 30bfb81c65 Avoid long option names that differ only in character case.
Change Xtrace (the name) to xlock instead.  Aside from the different
name, there is no change to functionality.
2018-11-23 20:40:06 +00:00
kre df073671e8 Rationalise (slightly) the way that expansions are processed
to hide meta-characters in the result when the expansion was
in (double) quotes, and so should not be further processed.

Most of this has been OK for a long while, but \ needs hiding
as well, which complicates things, as \ cannot simply be hidden
in the syntax tables as one of the group of random special characters.

This was fixed earlier for simple variable expansions, but
every variety has its own code path ($var uses different code
than $n which is different than $(...), which is different
again from ~ expansions, and also from what $'...' produces).

This could be fixed by moving them all to a common code path,
but that's harder than it seems.  The form in which the data
is made available differs, so one common routine would need
a whole bunch of different "get the next char or indicate end"
methods - probably via passing in an accessor function.
That's all a lot of churn, and would probably slow the shell.

Instead, just make macros for doing the standard tests, and
use those instead of open coding (differently) each time.
This way some of the code paths don't end up forgetting to
handle '\' (which is different than all the others).

This removes one optimisation ... when no escaping is needed
(like just $var (unquoted) where magic chars (think '*') in
the value are intended to remain magic), the code avoided doing
two tests for each char ("do we need escapes" and "is this char
one that needs escaping") by choosing two different syntax
tables (choice made outside the loop) - one of which never
returns the magic "needs escaping" result, and the other does
when appropriate, and then just avoiding the "do we need escapes"
test for each character processed.   Then when '\' was fixed,
there needed to be another test for it, as it cannot (for other
reasons) be the same as all the others for which "this char
need escaping" is true.   So that added a 2nd test for each char...
Not all the code paths were updated.   Hence the bugs...

nb: this is all rarely seen in the wild, so it is no big
surprised that no-one ever noticed.

Now the "use two different syntax tables" is gone (the two
returned the same for '\' which is why '\' needed special
processing) - and in order to avoid two tests for each
char (plus the \ test) we duplicate the loops, one of which
tests each char to see if it needs an escape, the 2nd just
copies them.   This should be faster in the "no escapes"
code path (though that is not the point) and perhaps also
in the "escapes needed" path (no indirect reference to
the syntax table - though that would probably be in a
register) but makes the code slightly bigger.  For /bin/sh
the text segment (on amd64) has grown by 48 bytes.  But
it still uses the same number of 512 byte pages (and hence
also any bigger page size).  The resulting file size
(/bin/sh) is identical before and after.  So is /rescue/sh
(or /rescue/anything-else).
2018-11-18 17:23:37 +00:00
kre 7ba0d30a60 PR bin/53712
Avoid crash from redirect on null compound command.
2018-11-09 02:11:04 +00:00
kre 375f4ceb14 Allow shells forked to run command substitutions while expanding
prompts to exit when they're done, rather than forcing them to
turn into interactive shells and start reading input ...

Completes a part of the previous changes (just 10+ weeks late...)

Should fix the prompt expansion issue reported by Caóc on
current-users.
2018-11-08 18:37:42 +00:00
kre 33a05de6b0 Switch from using two printsignals() functions, one in trap.c
and one in (the included from bin/kill) kill.c and use just
the one in kill.c (which is amended slightly so it can work
the way that trap.c needs it to work).    This one is chosen as
it was a much nicer implementation, and because while kill is
always built into the shell, kill also exists without the shell.

Leave the old implementation #if 0'd in trap.c (but updated to
match the calling convention of the one in kill.c) - for now.

Delete references of sys_signame[] from sh/trap.c and along with
that several uses of NSIG (unfortunately, there are still more)
and replace them with the newer libc functional interfaces.
2018-10-28 18:26:52 +00:00
kre 185226c2be Use strsignal() rather than direct reference to sys_siglist[]
(apart from being cleaner, it also simplifies the code, as
strsignal() never fails ... it also removes one reference to NSIG).
2018-10-28 18:16:01 +00:00
kre 7800af09a4 Change the (commented out) setting of -DDEBUG to the form that
is most likely to be useful if someone other than me wants to
build a DEBUG shell.   NFC (it is a comment in a Makefile!)
2018-10-28 18:13:47 +00:00
kre a7fa4685d3 Delete the old style (no longer used) DEBUG mode TRACE compat macro
definitions (just to avoid any temptation to ever use them again).

Update a comment which would make no sense without following the
preceding comment which is being deleted with the macros it describes.

While here, remove another comment that referred to events that have
long past as if they were still to come.   Also a grammatical comment
correction - paragraphs start with capital letters...

NFC (even with DEBUG defined).
2018-10-18 05:28:45 +00:00
kre bc7bc5b29e Remove a DEBUG mode transition mechanism (for the transition from
the ancient DEBUG TRACE() method, to the newer CTRACE() et. al.)
that turns out never really needed committing - the mechanism, and
the code that obsoleted it, were committed together (May 2017).
[It was useful to me while getting to that state...]

NFC.   Not even with DEBUG shells.
2018-10-18 04:44:27 +00:00
kre e5830c7775 Dynamically detect the way the shell matches \ in a pattern,
and use whatever works for the sh running this script.  Previously
we were using the (broken, and incorrect) method that worked in
old broken NetBSD sh's (and some others) and not the method that
works with the current (fixed) /bin/sh and other correct shells
(like bash).   (For an exotic reason, in the particular use case,
both methods work with ksh93, but it is also generally correct).

This hasn't really mattered, as the difference is only significant
(only causes actual issues - the build fails) when compiling with DEBUG
enabled, which is something that most sane humans would never do, if they
want to retain that sanity.

The problem was detected by Patrick Welche when looking for an
unrelated problem, which was once considered to be a possible sh
problem, but turned out to be something entirely different.

XXX pullup -8
2018-10-18 04:24:43 +00:00
kre 0db5b60ce4 When (about to) send the -x output for the end of a compound command
(which has redirects, and so is included in -x output) use the -x/+x
setting that existed when the comoound started, so if the state of
xtrace changes during the command we don't end up with just half of
the -x output (either the intro, or the conclusion, depending on
which way the change happened).   [this also happens to avoid a core
dump in the previous code, but that could have been done other ways,
this way actually simplifies things (less code)]
2018-10-09 02:43:41 +00:00
rillig 32c623ff75 When listing aliases, sort them alphabetically. 2018-10-07 23:17:52 +00:00
maxv a51b00c421 Don't display l_wchan, either there is something in l_wmesg and we display
it, or there's nothing and we print "-".
2018-09-19 15:20:39 +00:00
kre cf19d2f1a5 Begone unspecified test-a ... and be more obious what it was doing. 2018-09-16 22:31:30 +00:00
rillig a8fe3451b9 Fix "every" typo in quote from The Mythical Man-Month 2018-09-14 05:59:10 +00:00
kre feb6abd7ba A change in rev 1.91 interacted badly with the way that showjobs()
worked, preventing $(jobs) (and more usefully $(jobs -p) from
working.   Fix that.

XXX pullup -8
2018-09-13 22:12:35 +00:00
kre df8e475ae0 Allow SMALL (and TINY) builds of test (for SMALL/TINY builds of sh)
which support only the defined modes of operation of test, to allow
the version of sh on small install media be kept as small as possible.
2018-09-13 22:00:58 +00:00
kre ec9262987a PR standards/34646
Make test(1) always use the POSIX "number of args" evaluation rules
when they apply.

Only fall back to the old expression evaluation when there are more
than 4 args, or when the args given cannot work as a test expression
using the POSIX rules.  That is when the result is unspecified.

Also fix old bug where a string of whitespace is considered to be a
valid number (at least one digit is needed amongst it somewhere...)

XXX pullup -8
2018-09-12 23:33:31 +00:00
kre 3bdbd8da34 Whitespace cleanup from last update. NFC. 2018-09-11 03:30:40 +00:00
kre f53fd6e91f Change the way the pipefail option works. Now it is the setting of
the option when a pipeline is created that controls the way the exit
status of the pipeline is calculated.  Previously it was the state of
the option when the exit status of the pipeline was collected.

This makes no difference at all for foreground pipelines (there is
no way to change the option between starting and completing the
pipeline) but it does for asynchronous (background) pipelines.

This was always the right way to implement it - it was originally
done the other way as I could not find any other shell implemented
this way - they all seemed to do it our previous way, and I could
not see a good reason to be the sole different shell.

However, now I know that ksh93 works as we will now work, and I
am told that if the option is added to the FreeBSD shell (apparently
the code exists, uncommitted) it will be the same.
2018-09-04 23:16:30 +00:00
kre ee301070ed PR bin/38004
Save more characters of command in non-interactive jobs, in case of
core dumps and similar (16 effective chars was a few too little).

Arrange for number to increase if command buffer size increases.
2018-09-04 01:09:28 +00:00
wiz aa29c993fa Bump date for previous. 2018-09-01 07:26:56 +00:00
mlelstv c2909ab6c6 Make tape cache ioctls available to userland command. 2018-09-01 06:56:23 +00:00
sevan 3b26941a44 -G cannot be specified alongside -i or -P.
Heads up by <leot>
2018-08-26 23:34:52 +00:00
sevan 60a4b4b431 Update usage to include -w
Match sequence as per SYNOPSIS in manual
2018-08-26 23:01:06 +00:00
sevan 1f921aefc9 Add -l to SYNOPSIS 2018-08-26 22:52:34 +00:00
sevan 6fc63b1f88 Match SYNOPSIS with usage() 2018-08-26 22:25:37 +00:00
kre b92473bc91 PR bin/48875
Add a paragraph (briefer than previously posted to mailing lists)
to explain that there is no guarantee that the results of a command
substitution will be available before all commands started by the
cmdsub have completed.

Include the original proposed text (much longer) as *roff comments, so
it will at least be available to those who browse the man page sources.

While here, clean up the existing text about command substitutions to
make it a little more accurate (and to advise against using the `` form).
2018-08-25 17:35:31 +00:00
kre ce53a30d9a PR bin/53548
Deal with the new shell internal exit reason EXEXIT in the case of
a shell which has vfork()'d.   It takes a peculiar set of circumstances
to get into a situation where this is ever relevant, but it can be
done.   See the PR for details.
2018-08-25 02:42:49 +00:00
kre 70696c0161 Fix (hopefully) the problem reported on current-users by Patrick Welche.
we had incorrect usage of setstackmark()/popstackmark()

There was an ancient idiom (imported from CSRG in 1993) where code
can do:
	setstackmark(&smark); loop until whatever condition {
		/* do lots of code */ popstackmark(&smark);
	} popstackmark(&smark);

The 1st (inner) popstackmark() resets the stack, conserving memory,
The 2nd one is needed just in case the "whatever condition" was never
true, and the first one was never executed.

This is (was) safe as all popstackmark() did was reset the stack.
That could be done over and over again with no harm.

That is, until 2000 when a fix from FreeBSD for another problem was
imported.  That connected all the stack marks as a list (so they can be
located).  That caused the problem, as the idiom was not changed, now
there is this list of marks, and popstackmark() was removing an entry.

It rarely (never?) caused any problems as the idiom was rarely used
(the shell used to do loops like above, mostly, without the inner
popstackmark()).  Further, the stack mark list is only ever used when
a memory block is realloc'd.

That is, until last weekend - with the recent set of changes.

Part of that copied code from FreeBSD introduced the idiom above
into more functions - functions used much more, and with a greater
possibility of stack marks being set on blocks that are realloc'd
and so cause the problem.   In the FreeBSD code, they changed the idiom,
and always do a setstackmark() immediately after the inner popstackmark().
But not for reasons related to a list of stack marks, as in the
intervening period, FreeBSD deleted that, but for another reason.

We do not have their issue, and I did not believe that their
updated idiom was needed (I did some analysis of exactly this issue -
just missed the important part!), and just continued using the old one.
Hence Patrick's core dump....

The solution used here is to split popstackmark() into 2 halves,
popstackmark() continues to do what it has (recently) done,
but is now implemented as a call of (a new func) rststackmark()
which does all the original work of popstackmark - but not removing
the entry from the stack mark list (which remains in popstackmark()).
Then in the idiom above, the inner popstackmark() turns into a call of
rststackmark() so the stack is reset, but the stack mark list is
unchanged.  Tail recursion elimination makes this essentially free.
2018-08-22 20:08:54 +00:00
kre 8a9a96192a PR bin/48875 (is related, and ameliorated, but not exactly "fixed")
Import a whole set of tree evaluation enhancements from FreeBSD.

With these, before forking, the shell predicts (often) when all it will
have to do after forking (in the parent) is wait for the child and then
exit with the status from the child, and in such a case simply does not
fork, but rather allows the child to take over the parent's role.

This turns out to handle the particular test case from PR bin/48875 in
such a way that it works as hoped, rather than as it did (the delay there
was caused by an extra copy of the shell hanging around waiting for the
background child to complete ... and keeping the command substitution
stdout open, so the "real" parent had to wait in case more output appeared).

As part of doing this, redirection processing for compound commands gets
moved out of evalsubshell() and into a new evalredir(), which allows us
to properly handle errors occurring while performing those redirects,
and not mishandle (as in simply forget) fd's which had been moved out
of the way temporarily.

evaltree() has its degree of recursion reduced by making it loop to
handle the subsequent operation: that is instead of (for any binop
like ';' '&&' (etc)) where it used to
	evaltree(node->left);
	evaltree(node->right);
	return;
it now does (kind of)
	next = node;
	while ((node = next) != NULL) {
		next = NULL;

		if (node is a binary op) {
			evaltree(node->left);
			if appropriate /* if && test for success, etc */
				next = node->right;
			continue;
		}
		/* similar for loops, etc */
	}
which can be a good saving, as while the left side (now) tends to be
(usually) a simple (or simpleish) command, the right side can be many
commands (in a command sequence like a; b; c; d; ...  the node at the
top of the tree will now have "a" as its left node, and the tree for
b; c; d; ... as its right node - until now everything was evaluated
recursively so it made no difference, and the tree was constructed
the other way).

if/while/... statements are done similarly, recurse to evaluate the
condition, then if the (or one of the) body parts is to be evaluated,
set next to that, and loop (previously it recursed).

There is more to do in this area (particularly in the way that case
statements are processed - we can avoid recursion there as well) but
that can wait for another day.

While doing all of this we keep much better track of when the shell is
just going to exit once the current tree is evaluated (with a new
predicate at_eof() to tell us that we have, for sure, reached the end
of the input stream, that is, this shell will, for certain, not be reading
more command input) and use that info to avoid unneeded forks.   For that
we also need another new predicate (have_traps()) to determine of there
are any caught traps which might occur - if there are, we need to remain
to (potentially) handle them, so these optimisations will not occur (to
make the issue in PR 48875 appear again, run the same code, but with a
trap set to execute some code when a signal (or EXIT) occurs - note that
the trap must be set in the appropriate level of sub-shell to have this
effect, any caught traps are cleared in a subshell whenever one is created).

There is still work to be done to handle traps properly, whatever
weirdness they do (some of which is related to some of this.)

These changes do not need man page updates, but 48875 does - an update
to sh.1 will be forthcoming once it is decided what it should say...

Once again, all the heavy lifting for this set of changes comes directly
(with thanks) from the FreeBSD shell.

XXX pullup-8 (but not very soon)
2018-08-19 23:50:27 +00:00
kre 120267c338 PR bin/48875
Revert the changes that were made 19 May 2016 (principally eval.c 1.125)
and the bug fixes in subsequent days (eval.c 1.126 and 1.127) and also
update some newer code that was added more recently which acted in
accordance with those changes (make that code be as it would have been
if the changes now being reverted had never been made).

While the changes made did solve the problem, in a sense, they were
never correct (see the PR for some discussion) and it had always been
intended that they be reverted.   However, in practical sh code, no
issues were reported - until just recently - so nothing was done,
until now...

After this commit, the validate_fn_redirects test case of the sh ATF
test t_redir will fail.   In particular, the subtest of that test
case which is described in the source (of the test) as:
	This one is the real test for PR bin/48875
will fail.

Alternative changes, not to "fix" the problem in the PR, but to
often avoid it will be coming very soon - after which that ATF
test will succeed again.

XXX pullup-8
2018-08-19 11:16:13 +00:00
kre 3a41fe18d0 NFC: DEBUG mode only change. When tracing, show >&- as ">&-"
rather than ">&-1" (the same op as >&n where internally n < 0
means "close")
2018-08-19 10:47:45 +00:00
kre 5f8ba934de In the older debug code (not using the new macros added in the
previous rev) the two values (node name, and node number) were
arbitrarily printed in different formats and orders (depending
upon my mood at the time I guess...)   The new macros will standardise
that usage (in the debug output) once some use of them actually begins.

When the macros were added, I arbitrarily copied the format of one
use I was looking at at that instant (the one which inspired the change),
but after gazing at DEBUG mode output over the intervening time, I
have concluded that I did not pick the easiest to read/follow format.

So, even before they are used, change the style...    Also, conform
to standard PRIxxxx macro style by omitting the leading '%'.

NFC (since they aren't used at all, anywhere, yet, not even the
possibility of anything changing!)
2018-08-18 03:09:37 +00:00
kre ac2f5f225c NFC - a build structure code layout change only.
This generates nodenames.h which is a file that used to begin
	#ifdef DEBUG
(line 1) and end with
	#endif
(last line) with no intervening (matching) #else ... ie: for DEBUG use only.

That led to situations where non-debug code would like to make use
of the info provided, if DEBUG was enabled, needed to add #ifdef DEBUG
at the point of use.

Avoid that by providing new macros that are always defined (DEBUG or not,
so now we have a #else) which allow code to be written to make use of
the extra DEBUG info, if it is available, or not, if not.

While here, add double-include protection on the generated .h file
(just being cautious - nothing is ever going to cause it to get
included anywhere twice - or it shouldn't) and add the traditional
comments on the #else and #endif stuff (which is also really useless
as no-one is really expected to ever read the generated file).  Never mind.

Nothing yet (elsewhere in the sh source) uses the new macros, so there's
even less chance of this changing anything than there would otherwise be.
2018-08-16 15:02:05 +00:00
kre 16ccf0eeff PR bin/42184 PR bin/52687 (detailing the same bug).
Fix "command not found" handling so that the error message
goes to stderr (after any redirections are applied).

More importantly, in

	foo > /tmp/junk

/tmp/junk should be created, before any attempt is made
to execute (the assumed non-existing) "foo".

All this was always true for any command (not found command)
containing a / in its name

	foo/bar >/tmp/junk  2>>/tmp/errs

would have created /tmp/junk, then complained (in /tmp/errs)
about foo/bar not being found.   Now that happens for ordinary
commands as well.

The fix (which I found when I saw differences between our
code and FreeBSD's, where, for the benefit of PR 42184,
this has been fixed, sometime in the past 9 years) is
frighteningly simple.   Simply do not short circuit execution
(or print any error) when the initial lookup fails to
find the command - it will fail anyway when we actually
try running it.   The cost is a (seemingly unnecessary,
except that it really is) fork in this case.

This is what I had been planning, but I expected it would
be much more difficult than it turned out....

XXX pullup-8
2018-08-14 13:36:42 +00:00
kre ab6821e070 NFC: DEBUG (compile time) mode only change:
Add some extra redirection (fd manipulation) tracing.
While here, some white space fixes, and very minor KNF.
2018-08-13 22:13:02 +00:00
kre e2f17f9a72 Fix several bugs in the command / type builtin ( including PR bin/48499 )
1. Make command -pv (and -pV) work (which is not as easy as the PR
   suggests it might be (the "check and cause error" was there because
   it did not work, not in order to prevent it from working).

2. Stop -v and -V being both used (that makes no sense).

3. Stop the "type" builtin inheriting the args (-pvV) that "command" has
   (which it did, as when -v -or -V is used with command, it and type are
    implemented using the same code).

4. make "command -v word" DTRT for sh keywords (was treating them as an error).

5. Require at least one arg for "command -[vV]" or "type" else usage & error.
   Strictly this should also apply to "command" and "command -p" (no -v)
   but that's handled elsewhere, so perhaps some other time.   Perhaps
   "command -v" (and -V) should be limited to 1 command name (where "type"
   can have many) as in the POSIX definitions, but I don't think that matters.

6. With "command -V alias", (or "type alias" which is the same thing),
   (but not "command -v alias") alter the output format, so we get
	ll is an alias for: ls -al
   instead of the old
	ll is an alias for
	ls -al
   (and note there was a space, for some reason, after "for")

   That is, unless the alias value contains any \n characters, in which
   case (something approximating) the old multi-line format is retained.
   Also note: that if code wants to parse/use the value of an alias, it
   should be using the output of "alias name", not command or type.

Note that none of the above affects "command [-p] cmd" (no -v or -V options)
only "command -[vV]" and "type".

Note also that the changes to eval.[ch] are merely to make syspath()
visible in exec.c rather than static in eval.c
2018-07-25 14:42:50 +00:00
kre 14482abc9a Part 2 of pattern matching (glob etc) fixes.
Attempt to correctly deal with \ (both when it is a literal,
in appropriate cases, and when it appears as CTLESC when it was
detected as a quoting character during parsing).

In a pattern, in sh, no quoted character can ever be anything other
than a literal character.   This is quite different than regular
expressions, and even different than other uses of glob matching,
where shell quoting is not an issue.

In something like

	ls ?\*.c

the ? is a meta-character, the * is a literal (it was quoted).  This
is nothing new, sh has handled that properly for ever.

But the same happens with
	VAR='?\*.c'
and
	ls $VAR

which has not always been handled correctly.   Of course, in

	ls "$VAR"

nothing in VAR is a meta-character (the entire expansion is quoted)
so even the '\' must match literally (or more accurately, no matching
happens - VAR simply contains an "unusual" filename).  But if it had
been

	ls *"$VAR"

then we would be looking for filenames that end with the literal 5
characters that make up $VAR.

The same kinds of things are requires of matching patterns in case
statements, and sub-strings with the % and # operators in variable
expansions.

While here, the final remnant of the ancient !! pattern matching
hack has been removed (the code that actually implemented it was
long gone, but one small piece remained, not doing any real harm,
but potentially wasting time - if someone gave a pattern which would
once have invoked that hack.)
2018-07-22 23:07:48 +00:00
kre d211c89f40 NFC: Whitespace cleanups 2018-07-22 21:16:58 +00:00
kre ee4a694a18 PR bin/36532 (perhaps)
This is more or less the same patch as provided in the PR
(just 11 years later, so changed a bit) by woods@...

Since there is no known way to actually cause the reported crash,
we may never know if this change actually fixes anything.   But
even if it doesn't it certainly cannot hurt.

There is a potential race which could possibly explain the issue
(see commentary in the PR) which is not easy to avoid - if that is
the actual cause, this should provide a defence, if not really a fix.
2018-07-22 20:43:58 +00:00
kre 144cb20913 Revert previous, change has nothing to do with DEBUG mode.
COming again (correctly) in a few seconds.
2018-07-22 20:42:50 +00:00
kre bcacfd9a45 DEBUG mode only change (ie: no effect to any normal shell).
Add tracing of pattern matching (aid in debugging various issues.)
2018-07-22 20:38:06 +00:00
kre e442a1a054 Minor cleanups to growstackblock(). This should really change
nothing that matters, but might be slightly more robust/complete.
2018-07-22 20:37:57 +00:00
kre 57731ef9bb NFC: better protect the INTON (etc) macros ( do { } while(0) )
This is just a kind of precaution, none of the uses actually
require the extra protection.
2018-07-22 20:37:52 +00:00
kre c83568a7dc First pass at fixing some of the more arcane pattern matching
possibilities that we do not currently handle all that well.

This mostly means (for now) making sure that quoted pattern
magic characters (as well as quoted sh syntax magic chars)
are properly marked, so they remain known as being quoted,
and do not turn into pattern magic.   Also, make sure that an
unquoted \ in a pattern always quotes whatever comes next
(which, unlike in regular expressions, includes inside []
matches),
2018-07-20 22:47:26 +00:00
darcy 22057652ff Re-order the code to short circuit all the opens and closes if linking.
Discussed on tech-userlevel and agreed to by christos@.
Testing suggests an order of magnitude improvement when linking.
2018-07-17 13:04:58 +00:00
kre c6c29888c4 Remove atoi()
Mostly use number() (no longer implemented using atoi()) when an
unsigned integer is required, but use strtoXXX() when a conversion
is wanted, without the possibility or error (like setting OPTIND
and RANDOM).   Always init OPTIND to 1 when sh starts (overriding
anything in environ.)
2018-07-13 22:43:44 +00:00
kamil 2597218cc4 Add a missing check to handle correctly 0 * 0 in expr(1) 2018-06-27 17:23:36 +00:00
kamil 2b5da4d00d Improve the * operator handling in expr(1)
Fixes overflow detection in expressions INT * -UINT.

Detected with libFuzzer & UBSan.
2018-06-27 17:12:49 +00:00
kre b81009ce62 When processing character classes ([:xxx:] inside []), treat a class name
that is longer than we can handle the same way we treat an unknown
class name (as a valid char class which contains nothing, so never
matches).   Previously a "too long" class name invalidated the
class, so [:very-long-name:] would match any of  '[' ':' 'v'  ...
(note: "very-long-name" is not long enough to trigger this, but you
get the idea!)

However, the name itself has a restricted syntax ([[:***:]] is not a
character class, it is a match for one of a '[' ':' or '*', followed by
a ']') which we did not implement - check the syntax of the name before
treating it as a character class (but we do add '_' to alphanumerics
as legal class name characters).
2018-06-22 18:19:41 +00:00
kre 829cc62a58 When matching a char class ([[:name:]]) in a pattern (for filename
expansion, case patterrns, etc) do not force '[' to be a member of
every class.

Before this fix, try:
	case [ in [[:alpha:]]) echo Huh\?;; esac

XXX pullup-8    (Perhaps -7 as well, though that shell version has
much more relevant bugs than this one.)  This bug is not in -6 as
that has no charclass support.
2018-06-22 17:22:34 +00:00
kre c7c0722af5 Deal with ref after free found by ASAN when a function redefines
itself, or some other function which is still active.
This was a long known bug (fixed ages ago in the FreeBSD sh) which
hadn't been fixed as in practice, the situation that causes the
problem simply doesn't arise .. ASAN found it in the sh dotcmd
tests which do have this odd "feature" in the way they are written
(but where it never caused a problem, as the tests are so simple
that no mem is ever allocated between when the old version of the
function was deleted, and when it finished executing, so its code
all remained intact, despite having been freed.)

The fix is taken from the FreeBSD sh.

XXX -- pullup-8 (after a while to ensure no other problems arise).
2018-06-22 11:04:55 +00:00
christos 8adf28c539 Prevent shell execution also in the 'r ! dobad' case, pointed out by
Martijn van Duren, thanks!

XXX: pullup-8
2018-06-18 14:56:24 +00:00
kre 765ad10c6a NFC: correct typo in a comment. 2018-06-17 17:19:06 +00:00
christos 3ae7054da4 remove notreached 2018-06-14 02:46:56 +00:00
christos 721572cdb2 return to caller instead of exit(2) 2018-06-14 02:44:16 +00:00
kamil 2537a420e6 Detect properly overflow in expr(1) for 0 + INT 2018-06-13 17:35:15 +00:00
christos 448c6863b6 PR/53362: Thomas Barabosch: Use strlcpy() to prevent theoretical buffer
overflow.
2018-06-13 15:14:40 +00:00
kamil 7806b47917 Rework perform_arith_op() in expr(1) to omit Undefined Behavior
The current implementation of operations - + * / % could cause Undefined
Behavior and in narrow cases (INT64_MIN / -1 and INT64_MIN % -1) SIGFPE
and crash duping core.

Detected with MKSANITIZER enabled for the Undefined Behavior variation:
# eval expr '4611686018427387904 + 4611686018427387904'
/public/src.git/bin/expr/expr.y:315:12: runtime error: signed integer overflow: 4611686018427387904 + 4611686018427387904 cannot be represented in type 'long'

All bin/t_expr ATF tests pass now in a sanitized userland.

Sponsored by <The NetBSD Foundation>
2018-06-12 18:12:18 +00:00
kamil d6d5f49c60 Change typ of tilde_ok from int to unsigned int in ksh(1)
UBSan can detect that during switching a login to root there is unportable
left shift operation:

$ su -
Password:
/public/src.git/bin/ksh/eval.c:598:13: runtime error: left shift of 1073741824 by 1 places cannot be represented in type 'int'
#

Sponsored by <The NetBSD Foundation>
2018-06-12 14:13:55 +00:00
christos 2357fc5dd5 use SUBDIR.roff suggested by uwe@ 2018-06-10 17:55:11 +00:00
kamil b9cf72ac86 ksh: Remove symbol clash with libc
Rename local function glob() to ksh_glob().
This is needed for installing interceptors in sanitizers.

Sponsored by <The NetBSD Foundation>
2018-06-03 16:09:31 +00:00
kamil 3eb9a6b77e ksh: Remove symbol clash with libc
Rename local function twalk() to ksh_twak().
This is needed for installing interceptors in sanitizers.

Sponsored by <The NetBSD Foundation>
2018-06-03 12:18:29 +00:00
kamil 0f2b5450fb Stop using the register keyword in ksh(1)
ksh also does some strange things with it, like put it in argument lists.

No functional change intended.

PR bin/53237 ksh: remove register keyword by Nia Alarie
2018-05-08 16:37:59 +00:00
wiz b105f2c163 Remove Pps without effect. 2018-05-03 05:11:43 +00:00
kre 5cf0b51195 Simplify convoluted language, and remove incorrect statement
(that I added a while ago) about what is required by POSIX.
2018-05-03 00:32:11 +00:00
pgoyette d54998bbfe Minor grammatical correction (don't end a sentence/phrase with a
preposition).
2018-05-02 21:43:38 +00:00
kre e76b58b2db In uses like $(( var )) (un-dollared vars in arithmetic) we allow
leading whitespace in the value of var (because strtoimax() does)
but did not allow trailing whitespace.   The effect is that some
cases where $(( ${var:-0} )) would work do not work without the $
expansion.

Fix that - allow trailing whitespace.   However, continue to insist
upon at least one digit (a non-null var that contains nothing but
whitespace is still an error).

Note: posix is not helpful here, it simply requires that the variable
contain "a value that forms a valid integer constant" (with an optional
+ or - sign).
2018-04-21 23:01:29 +00:00
kre d6d059edc2 PR bin/53201
Don't synerr on
	${var-anything
	more}

The newline in the middle of the var expansion is permitted.

Bug reported by Martijn Dekker from his modernish tests.

XXX pullup-8
2018-04-21 21:32:14 +00:00
christos ea7b28924d kvm_geterr() already contains errno, use errx. 2018-04-11 18:52:29 +00:00
christos b02b35c97e use EXIT_FAILURE instead of 1 2018-04-11 18:52:05 +00:00
wiz 883cee82b3 Sort option descripts, fix markups, fix typos. 2018-04-09 06:57:01 +00:00
wiz 051f873718 New sentence, new line. Sort SEE ALSO. Remove Xr to non-existing man page. 2018-04-09 06:54:47 +00:00
snj 107d499d1f fix date 2018-04-08 06:01:04 +00:00
christos c02518ebec add -S to disable ! commands. 2018-04-05 18:44:57 +00:00
msaitoh f96d27e840 s/copy copy/copy/ in comment. 2018-03-19 03:11:39 +00:00
uwe c23d0de35c Drop "show or set the limit on" legalese from the description of each
and every option to ulimit built-in.  The show-or-set text is already
supplied *both* before and after the list.  Pedantically repeating it
for each option just adds a lot of visual clutter that gets in the way
of actually using this fragment of the manual page as a quick
reference.
2018-03-17 01:53:06 +00:00
uwe f46f1cb2a2 Tweak "ulimit" synopsis. 2018-03-17 01:40:28 +00:00
uwe 8b4ce5dc95 Cleanup markup in the "Command Line Editing" section. 2018-03-17 01:32:42 +00:00
uwe 252ed5cb12 Cleanup markup in the "Job Control" section. 2018-03-17 01:03:08 +00:00
uwe e2342ea669 Use .Dv, not .Ev, to refer to LINENO, it's not an environment variable. 2018-03-17 00:03:25 +00:00
uwe 1a2e30dffb Default values of PS1 and friends have only single space. Use .Li to
typeset them to make that space more visible in PostScript output.
2018-03-16 23:56:13 +00:00
uwe 455a88217c Use .Bd -literal for code example. 2018-03-16 23:36:13 +00:00
kre 1b14ecae60 Markup fixes (partly from uwe@) and change some tabs to spaces, they
seem to work better...
2018-03-16 12:06:18 +00:00
kre f993985a6e Restore some (*roff) comments deleted in previous (partially unshave
the yak) for which the purpose was misunderstood.   But trim one more hair.
2018-03-16 11:53:57 +00:00
kre f1861a8088 Give the yak a quick trim and shave, and make one or two minor
wording changes (which are, hopefully, improvements).
2018-03-16 11:19:24 +00:00
uwe 613a287d51 Start adding more gaudy markup. Use .Li or .Dv when referring to
parameters.  Use more .Ic and .Ar when defining syntax.

The manual is still rather inconsistent e.g. when referring to
parameters where it randomly uses both $0 and 0 or $@ and @ - but I'm
not shaving that yak at least for now.
2018-03-15 01:20:43 +00:00
uwe abdb117cdc Compute tag width for the list of options in Argument List Processing,
mandoc *is* up to that.  Remove the part of the comment before the
list that was wondering about that.
2018-03-14 10:38:52 +00:00
uwe db273842a9 Small markup tweaks in Argument List Processing 2018-03-14 10:30:40 +00:00
uwe 7a8dcc3884 Instead of .Oo/.Oc use .Op directly where possible. 2018-03-14 09:46:45 +00:00
uwe 484f53204c Revert previous. Fix the real problem properly. 2018-03-14 09:42:37 +00:00
wiz b30e74889c Remove Ic macro without effect. 2018-03-14 07:53:14 +00:00
uwe caedaa46ad Try to improve markup in the Built-ins section.
Mostly sprinkle missing .Ic and .Ar
2018-03-13 23:03:21 +00:00
uwe f81efe159b Try to improve markup in the Parameter Expansion section. 2018-03-13 21:49:15 +00:00
uwe 0054fb0db8 Try to improve markup of the redirections definitions. 2018-03-13 21:04:57 +00:00
uwe 08ffbb4888 Fix horrendous markup abuse in the here-document example.
Consistently spell "here-document" in full.
2018-03-13 20:48:00 +00:00
uwe 121a1034cd Spell "here-document" with a hyphen, don't mark it up as a command. 2018-03-13 20:40:52 +00:00
uwe 9453809f25 Mark up "in" (of the "for" command) appropriately. 2018-03-13 20:39:25 +00:00
uwe 194117b0aa Use \(or not \*(Ba when discussing case patterns. 2018-03-13 20:29:13 +00:00
uwe 0bbb85ef5e Use \(em for em-dash 2018-03-13 20:18:16 +00:00
uwe 882c5e3aae Standalone | means \[ba] while we want \[or] so add \& protection to
the few places where it was missing.
2018-03-13 20:08:11 +00:00
uwe 4243789c21 .Dl is a a single line .Bd -literal -offset indent so don't abuse
multiple consecutive .Dl and use proper .Bd instead.
2018-03-13 19:43:52 +00:00
uwe e0885dda62 .Bd expects the display type to come first, so move -compact to the end. 2018-03-13 19:35:46 +00:00
uwe 5e8101b3eb Add missing word. 2018-03-13 19:18:53 +00:00
dholland 32cded6cc9 Typos. 2018-02-08 09:05:16 +00:00
mrg 6d188dd0d7 convert HAVE_GCC handling to modern GCC release numbering:
- HAVE_GCC=5 is now the default (vs. HAVE_GCC=53 we've been using for
  GCC 5.4 and GCC 5.5.)
- remove some more GCC 4.8 code.  we don't support GCC 4 here.
- adjust set lists to gcc=5 from gcc=53.

add some basic HAVE_GCC=6 handling (totally unused so far.)
2018-02-02 01:02:39 +00:00
wiz e793fb4654 Remove trailing whitespace and Tn macro. 2018-01-27 18:59:38 +00:00
christos a61f9804b8 sync with tzcode-2018c 2018-01-25 18:07:17 +00:00
kamil 081e832953 ksh: Rename a local function tsearch to mytsearch
This removes a clash with well-known libc function tsearch(3) from POSIX.

This allows to build ksh against MSan.

The new name might not be perfect, but long term ksh should be switched to
the libc version.

Sponsored by <The NetBSD Foundation>
2018-01-24 09:53:20 +00:00
kamil 6d8c9dfa44 ksh: Rename a local function tdelete to mytdelete
This removes a clash with well-known libc function tdelete(3) from POSIX.

This allows to build ksh against MSan.

The new name might not be perfect, but long term ksh should be switched to
the libc version.

Sponsored by <The NetBSD Foundation>
2018-01-24 09:21:20 +00:00
sevan 7a75668892 drop main() prototype 2018-01-23 22:12:52 +00:00
maya ee3bb8535b Use 0600 as the mode for histfile here too.
pointed out by John D. Baker in PR bin/52480
2018-01-17 00:29:22 +00:00
kre d4d1419421 Paul Goyette suggested improvements to parts of the description of
LINENO ... this is what resulted (with thanks for the grammar lessons,
and sundry references provided!)

No date (Dd) bump - there is no change of substance here, just (hopefully)
a clearer way of saying the same thing.
2018-01-15 11:27:39 +00:00
kamil 0e68b604ab Fix an unitialized memory read bug in ps(1)
rawcpu of type int, is declared inside main(){} and it can be passed as
uninitialized to setpinfo().
The setpinfo() function has a switch checking the value of rawcpu:

  if (!rawcpu)
    pi[i].pcpu /= 1.0 - exp(ki[i].p_swtime * log_ccpu);

rawcpu is set to 1 with the command line argument "-C".

   -C           Change the way the CPU percentage is calculated by using a
                "raw" CPU calculation that ignores "resident" time (this
                normally has no effect).

Bug reproducible with an invocation: "ps u". It hides with "ps uC".

Initialize rawcpu by default to 0, before the getopt(3) machinery.

Detected with MSan running on NetBSD/amd64.

Sponsored by <The NetBSD Foundation>
2018-01-12 23:01:14 +00:00
kre 13689a6c8a In addition to previous the which fixed a (harmless) MSAN detected ref
of uninit'd field also fix a couple more (still harmless) related
technical C usage bugs.

Explaining why these issues were harmless would take too long to include here.
2017-12-30 23:24:19 +00:00
christos d27db487e5 initialize just used and prev_job 2017-12-30 20:42:28 +00:00
christos e11969ea5d initialize the jobtab; it is easier than putting checks for used everywhere. 2017-12-30 01:21:25 +00:00
kre 84c609adca Do a better job of reporting invalid numeric constants in arithmetic exprs.
For example, given $(( 08 + 1 )) (or similar) instead of reporting
"expecting end of expression"  - the generic error for parse failed,
which happened as this was parsed as $(( 0 8 + 1 )) because the 8
could not be a part of an octal constant, and that expr makes no sense -
instead say "unexpected '8' (out of range) in numeric constant: 08"
which makes the cause of the error more obvious.

NFC for valid expressions, just the error message (and the way the
error is detected).
2017-12-17 04:06:03 +00:00
kamil b42c7b02bc ps: Rename local routine name from uname to usrname
This removes symbol name routine clash with libc's uname(3).
This allows to build ps(1) against LLVM Sanitizers.

Sponsored by <The NetBSD Foundation>
2017-12-09 14:56:54 +00:00
kre ae40879b6d Remove the -X option from SMALL shells (as used on boot floppies,
some other install media, mini-roots, etc.)   It is unlikely that
such a shell will be used for much script debugging (and the old -x
still exists of course) and it adds a little bloat, so, zap...

The ancient unused (unrelated) xioctl() function is gone as well
(from all shells).
2017-11-21 03:42:39 +00:00
kre ffc64c6374 Implement the -X option - an apparent variant of -x which sends all trace
output to the stderr which existed when the -X option was (last) enabled.
It also enables tracing by enabling -x (and when reset, +X, also resets
the 'x' flag (+x)).  Note that it is still -x/+x which actually
enables/disables the trace output.   Hence "apparent variant" - what -X
actually does (aside from setting -x) is just to lock the trace output,
rather than having it follow wherever stderr is later redirected.
2017-11-19 03:23:01 +00:00
kre bfa0b331f0 Rearrange "struct output" to be slightly more friendly (I think)
to I32 P64 systems - keep nextc first, as that's used in macros,
and nleft next, as that's used (and both are updated) in the same macro,
which is used frequently, this increases the chance they're in the
same cache line (unchanged from before).   Beyond that it matters less,
so just shuffle a bit to avoid internal padding when pointers are 64 bits.
Note that there are just 3 of these structs (currently), even if there was
to be a memory saving (there probably won't be, trailing padding will eat it)
it would be of the order of 12 or 24 bytes total, so all this really
just panders to my sense of rightness....

Note to anyone who might be tempted, please don't update the struct
initializers to use newer C forms - eventually sh is planned to become
a host tool, and a separable package, so it wants to remain able to be
compiled using older (though at least ansi) compilers that implement only
older C variants.
2017-11-19 03:22:55 +00:00
kre f5deb3ba25 Improve quoting in xtrace (-x) output ... if a string ("word") to be
output includes a single quote (') then see if using double-quotes
to quote it is reasonable (if no chars that are magic in " also appear).
If so, and if the string is not entirely the ' character, then
use " quoting.  This avoids some ugly looking results (occasionally).

Also, fix a bug introduced about 20 months ago where null strings
in xtrace output are dropped, instead of made explicit ('').
To observe this, before you get the fix: set -x; echo ''   (or similar.)

Move a comment from the wrong place to the right place.
2017-11-16 19:41:02 +00:00
kre b86d4a7424 DEBUG mode only change. Add some tracing. NFC (without DEBUG). 2017-11-15 09:21:48 +00:00
kre 20c8eb69ec Sort options with long names that differ only by char case in
the same order that option flags with a similar property are sorted.
This corresponds with the change made to the sort order of the short
names made in the previous update (1.4).

Right now, this change makes no difference at all, as there are no
long option names that differ only in char case (yet.)
2017-11-15 09:21:19 +00:00
kre e965a70935 Correct a typo: s/ at / an / 2017-11-15 08:50:07 +00:00
kre d1b3ee239b PR bin/52715
Correct a (relatively harmless) use after free in prompt expansion
processing [detected by asan.]

Relatively harmless: as (while incorrect) the way the data is (was)
used more or less guaranteed that the buffer contents would be
unaltered until well after they are (were) no longer wanted (this
is the expanded prompt string, it is just output (or copied into
libedit internal storage) and forgotten.

This should make no visible difference to anyone (not using asan or
similar.)

XXX pullup -8
2017-11-10 17:31:12 +00:00
wiz 7a66d2e690 Remove Tn. 2017-10-30 15:38:52 +00:00
wiz ee76eb57bf Minor spellchecking changes. 2017-10-30 15:37:41 +00:00
kre dbad4c9d2d Correct a markup typo (Sv -> Dv) 2017-10-29 00:20:42 +00:00
kre 4e9fc30dca Add '-n' and '-p var' args to the wait command (-n: wait for any,
-p var: set var to identifier, from arg list, or PID if no job args)
of the job for which status is returned (becomes $? after wait.)

Note: var is unset if the status returned from wait came from wait
itself rather than from some job exiting (so it is now possible to
tell whether 127 means "no such job" or "job did exit(127)", and
whether $? > 128 means "wait was interrupted" or "job was killed
by a signal or did exit(>128)".   ($? is too limited to to allow
indicating whether the job died with a signal, or exited with a
status such that it looks like it did...)
2017-10-28 06:36:17 +00:00
kre 786714f50f Another %zu for size_t (this one in a DEBUG mode trace call, so it
doesn't actually ever bother anyone in practice.)
2017-10-28 04:50:38 +00:00
kre 4512d56802 Extract the variable name validity test from setname() into a
function of its own.  It will soon be needed from another source.
2017-10-28 03:59:11 +00:00
martin 6df8f46e34 Use %zu for size_t 2017-10-25 08:50:05 +00:00
kre 673e27def2 Implement tcgetwinsize() and tcsetwinsize() in libc, with access
via <termios.h> (and document them.)   Bump libc minor number for them.

Arrange for "struct winsize" to become visible in <termios.h>

Fix stty(1) so that "cols" is reported as the arg to set number of columns,
and "columns" is the alias, rather than the other way around, as "cols" is
what has been added to POSIX.

This is to conform with updates to be included in 1003.1 issue 8
(whenever that gets published) currently available at:
  http://austingroupbugs.net/view.php?id=1053   (see note 3863)
  http://austingroupbugs.net/view.php?id=1151   (see note 3856)
2017-10-25 06:32:59 +00:00
kre f697d47ee3 Add options to the builtin jobid command to allow discovering the
process group (-g), the process leader pid (-p) ($! if the job was &'d)
and the job identifier (-j) (the %n that refers to the job) in addition to
(default) the list of all pids in the job (which it has always done).
No change to the (single) "job" arg, which is a specifier of the job:
the process leader pid, or one of the % forms, and defaults to %% (aka %+).
(This is all now documented in sh(1))

Also document the jobs command properly (no change to the command, just
document what it actually is.)

And while here, a whole new section in sh(1) "Job Control".  It probably
needs better wording, but this is (perhaps) better than the nothing that
was there before.
2017-10-25 05:42:56 +00:00
kre 9572a5c228 PR bin/52640 PR bin/52641
Don't delete jobs from the jobs table merely because they finished,
if they are not the job we are waiting upon.   (bin/52640 part 1)

In a sub-shell environment, don't allow wait to find jobs from the
parent shell that had already exited (before the sub-shell was
created) and return status for them as if they are our children.
(bin/52640 part 2)

Don't have the "jobs" command also be an implicit "wait" command
in non-interactive shells.  (bin/52641)

Use WCONTINUED (when it exists) so we can report on stopped jobs that
"mysteriously" move back to running state without the user issuing
a "bg" command (eg: kill -CONT <pid>)   Previously they would keep
being reported as stopped until they exited.

When a job is detected as having changed status just as we're
issuing a "jobs" command (i.e.: the change occurred between the last
prompt and the jobs command being entered) don't report it twice,
once from the status change, and then again in the jobs command
output.   Once is enough (keep the jobs output, suppress the other).

Apply some sanity to the way jobs_invalid is processed - ignore it
in getjob() instead of just ignoring it most of the time there, and
instead always check it before calling getjob() in situations where
we can handle only children of the current shell.  This allows the
(totally broken) save/clear/restore of jobs_invalid in jobscmd() to
be done away with (previously an error while in the clear state would
have left jobs_invalid incorrectly cleared - shouldn't have mattered
since jobs_invalid => subshell => error causes exit, but better to be safe).

Add/improve the DEBUG more tracing.

XXX pullup -8
2017-10-23 10:52:07 +00:00
kre bffe519047 Re-factor the code that extracts status from exited jobs, avoiding
code duplication, and reducing the size of /bin/sh by a trivial amount.

NFCI.

This is being done now as there are two other changes forthcoming, both
of which benefit - one would result in even more code duplication without
this, the other might need to alter how this is done, and doing it after this
means there's just one place to change (if required).
2017-10-19 01:57:18 +00:00
wiz 14b00a757c Remove unnecessary Tn. 2017-10-18 18:11:54 +00:00
kre 9bbf3fccba I have seen (way too often) uses of test(1) that are not defined to
actually work (but just happen to, today, and in some cases, even
that trusts to some luck.)

It has been recently pointed out to me that the man page (ie: this
file) doesn't give any real guidance to what is really acceptable,
and what is not.

The CAVEATS section does note that the grammar is ambiguous, but then
just says that test(1) implements what POSIX requires, and refers
readers to the relevant section of the POSIX standard for more details.
That is probably asking too much of the average reader...

So, add some extra information in the CAVEATS with what is defined to work,
and what should be avoided.   Not all of the POSIX rules are here, but this
might hopefully help script authors avoid some of the pitfalls.
2017-10-18 08:54:59 +00:00
jnemeth 869f53c4eb typo: unvalid -> invalid 2017-10-16 17:08:35 +00:00
pgoyette ae677eba76 Fix typo: s/one or mode/one or more/ 2017-10-15 12:01:43 +00:00
kre bd208c6933 Three fixes and a change to ~ expansions
1. A serious bug introduced 3 1/2 months ago (approx) (rev 1.116) which
   broke all but the simple cases of ~ expansions is fixed (amazingly,
   given the magnitude of this problem, no-one noticed!)

2. An ancient bug (probably from when ~ expansion was first addedin 1994, and
   certainly is in NetBSD-6 vintage shells) where ${UnSeT:-~} (and similar)
   does not expand the ~ is fixed (note that ${UnSeT:-~/} does expand,
   this should give a clue to the cause of the problem.

3. A fix/change to make the effects of ~ expansions on ${UnSeT:=whatever}
   identical to those in UnSeT=whatever   In particular, with HOME=/foo
   ${UnSeT:=~:~} now assigns, and expands to, /foo:/foo rather than ~:~
   just as VAR=~:~ assigns /foo:/foo to VAR.   Note this is even after the
   previous fix (ie: appending a '/' would not change the results here.)

   It is hard to call this one a bug fix for certain (though I believe it is)
   as many other shells also produce different results for the ${V:=...}
   expansions than  they do for V=... (though not all the same as we did).

   POSIX is not clear about this, expanding ~ after : in VAR=whatever
   assignments is clear, whether ${U:=whatever} assignments should be
   treated the same way is not stated, one way or the other.

4. Change to make ':' terminate the user name in a ~ expansion in all cases,
   not only in assignments.   This makes sense, as ':' is one character that
   cannot occur in user names, no matter how otherwise weird they become.
   bash (incl in posix mode) ksh93 and bosh all act this way, whereas most
   other shells (and POSIX) do not.   Because this is clearly an extension
   to POSIX, do this one only when not in posix mode (not set -o posix).
2017-10-06 21:09:45 +00:00
joerg 481188eabd Include time.h for time(2). Sort. 2017-10-02 21:55:35 +00:00
wiz cb8c8a822a Add cross reference to tac(1). 2017-10-02 08:24:17 +00:00
kre f07d3f9b6b DEBUG only changes (non-debug, ie: normal, shell unaffected)
Add a little extra info in a few of the trace messages.
2017-09-29 17:53:57 +00:00
kre e7beee7b61 Fix a bug noticed by Soren Jacobsen running the netbsd-6-0 build.sh which
causes a core dump in some exotic circumstances (when restoring local
variables when a function returns).  ("build.sh makewrapper" exposed it.)

This was introduced in 1.63 - not as part of the substance of that
change (addition) but as an unrelated "must be the right thing to do"
cleanup, which wasn't...
2017-08-31 05:09:38 +00:00
wiz 21f891a499 Bump date for previous. Remove/replace bogus Tn uses. 2017-08-28 05:57:37 +00:00
kamil a69b333e73 Remove the filesystem tracing feature
This is a legacy interface from 4.4BSD, and it was
introduced to overcome shortcomings of ptrace(2) at that time, which are
no longer relevant (performance). Today /proc/#/ctl offers a narrow
subset of ptrace(2) commands and is not applicable for modern
applications use beyond simplistic tracing scenarios.

This removal will simplify kernel internals. Users will still be able to
use all the other /proc files.

This change won't affect other procfs files neither Linux compat
features within mount_procfs(8). /proc/#/ctl isn't available on Linux.

Remove:
 - /proc/#/ctl from mount_procfs(8)
 - P_FSTRACE note from the documentation of ps(1)
 - /proc/#/ctl and filesystem tracing documentation from mount_procfs(8)
 - KAUTH_REQ_PROCESS_PROCFS_CTL documentation from kauth(9)
 - source code file miscfs/procfs/procfs_ctl.c
 - PFSctl and procfs_doctl() from sys/miscfs/procfs/procfs.h
 - KAUTH_REQ_PROCESS_PROCFS_CTL from sys/sys/kauth.h
 - PSL_FSTRACE (0x00010000) from sys/sys/proc.h
 - P_FSTRACE (0x00010000) from sys/sys/sysctl.h

Reduce code complexity after removal of this functionality.

Update TODO.ptrace accordingly: remove two entries about /proc tracing.

Do not keep legacy notes as comments in the headers about removed
PSL_FSTRACE / P_FSTRACE, as this interface had little number of users
(close or equal to zero).

Proposed on tech-kern@.

All filesystem tracing utility users are encouraged to switch to ptrace(2).

Sponsored by <The NetBSD Foundation>
2017-08-28 00:46:06 +00:00
wiz 4d62ff150c Whitespace fixes. Fix a typo. Refer to emacs using Ic, since emacs(1)
does not exist in the base system.
2017-08-27 20:37:59 +00:00
wiz fcc473e50d Remove unnecessary Tn macro. 2017-08-27 20:32:20 +00:00
kre 5f92382c9a Add support for $'...' quoting (based upon C "..." strings, with \ expansions.)
Implementation largely obtained from FreeBSD, with adaptations to meet the
needs and style of this sh, some updates to agree with the current POSIX spec,
and a few other minor changes.

The POSIX spec for this ( http://austingroupbugs.net/view.php?id=249 )
[see note 2809 for the current proposed text] is yet to be approved,
so might change.  It currently leaves several aspects as unspecified,
this implementation handles those as:

Where more than 2 hex digits follow \x this implementation processes the
first two as hex, the following characters are processed as if the \x
sequence was not present.  The value obtained from a \nnn octal sequence
is truncated to the low 8 bits (if a bigger value is written, eg: \456.)
Invalid escape sequences are errors.  Invalid \u (or \U) code points are
errors if known to be invalid, otherwise can generate a '?' character.
Where any escape sequence generates nul ('\0') that char, and the rest of
the $'...' string is discarded, but anything remaining in the word is
processed, ie: aaa$'bbb\0ccc'ddd produces the same as aaa'bbb'ddd.

Differences from FreeBSD:
  FreeBSD allows only exactly 4 or 8 hex digits for \u and \U (as does C,
  but the current sh proposal differs.) reeBSD also continues consuming
  as many hex digits as exist after \x (permitted by the spec, but insane),
  and reject \u0000 as invalid).  Some of this is possibly because that
  their implementation is based upon an earlier proposal, perhaps note 590 -
  though that has been updated several times.

Differences from the current POSIX proposal:
  We currently always generate UTF-8 for the \u & \U escapes.   We should
  generate the equivalent character from the current locale's character set
  (and UTF8 only if that is what the current locale uses.)
  If anyone would like to correct that, go ahead.

  We (and FreeBSD) generate (X & 0x1F) for \cX escapes where we should generate
  the appropriate control character (SOH for \cA for example) with whatever
  value that has in the current character set.   Apart from EBCDIC, which
  we do not support, I've never seen a case where they differ, so ...
2017-08-21 13:20:49 +00:00
kre a9e6fc0b06 NFC - DEBUG mode change only - add some sanity to a debug printf format string 2017-08-19 21:13:11 +00:00
ginsbach 6066c254d7 PR/48182: Fix rmdir -p handling of top-level (root) directory.
XXX: pullup-8
XXX: pullup-7
XXX: pullup-6
2017-08-10 22:52:13 +00:00
kre 70a37837ef PR bin/52458
Avoid mangling history when editing is enabled, and the prompt contains a \n

Also, allow empty input lines into history when they are being appended to
a previous (partial) command (but not when they would just make an empty entry).

For all the gory details, see the PR.

Note nothing here actually makes prompts containing \n work correctly
when editing is enabled, that's a libedit issue, which will be addressed
some other time.
2017-08-05 11:33:05 +00:00
kre fab2da9b21 PR bin/48498 PR bin/52426
Don't ignore unexpected reserved words after ';'
Don't allow any random token type as a case stmt pattern, only a word.
	Those are ancient ash bugs and do not affect correct scripts.

Don't ignore redirects in a case stmt list where the list is nothing but
redirects (if the pattern matches, the redirects should be performed).
	That was introduced when a redirect only case stmt list was allowed
	(older shells had generated a syntax error.)

Random cleanups/refactoring taken from or inspired by the FreeBSD sh
parser ...  use makename() consistently to create a NARG node - we
were using it in a couple of places but most NARG node creation was open
coded.  Introduce consumetoken() (from FreeBSD) to handle the fairly
common case where exactly one token type must come next, and we need to
check that, and skip past it when found (or error) and linebreak() (new)
to handle places where optional \n's are permitted.
Both previously open coded.

Simplify list() by removing its second arg, which was only ever used when
handling the end of a `` (old style command substitution).  Simply move
the code from inside list() to just after its call in the `` case (from
FreeBSD.)
2017-07-26 23:09:41 +00:00
kre 0915509c52 Add a comment explaining the ordering reltionship of the tokens
(operators all come first, then TWORD, then keywords), and switch
from using TIF to define KWDOFFSET to using TWORD (the barrier,
rather than the token that happens to be first after it.)
2017-07-26 03:46:54 +00:00
kre 4e19051c4d NFC: DEBUG mode change - add a couple more TRACE macros to allow for
conditional (apart from the normal debug conditions) tracing.
2017-07-26 03:44:43 +00:00
wiz bbff3c2815 Remove trailing whitespace. 2017-07-25 08:37:48 +00:00
kre ed2c7aaa15 Implement the "pipefail" option (same semantics as in other shells)
to cause (when set, which it is not by default) the exit status of a
pipe to be 0 iff all commands in the pipe exited with status 0, and
otherwise, the status of the rightmost command to exit with a non-0
status.

In the doc, while describing this, also reword some of the text about
commands in general, how they are structured, and when they are executed.
2017-07-24 14:17:11 +00:00
kre 59695ae2d7 NFC: DEBUG mode only change - add a little more tracing. 2017-07-24 13:36:15 +00:00
kre e79133b55e Add support for ++ and -- (pre & post) and ',' to arithmetic. 2017-07-24 13:21:14 +00:00
kre a304c3b3a5 Document the times builtin command, reported as lost in space
by rudolf at eq.cz on tech-userlevel (July 15, 2017.)

Also correct a typo, de-correct some entirely proper English so
the doc remains written in American instead.  And note that
interactive mode is set when stdin & stderr are terminals, not
stding and stdout.
2017-07-24 12:36:02 +00:00
kre a373e69a19 PR standards/52406
Absent other information, the shell should be interactive if reading
from stdin, and stdin and stderr are ttys, not stdin and stdout.

So sayeth the great lord posix.
2017-07-24 12:35:37 +00:00
kre a2d92a0059 PR bin/52348
Avoid a reference after free (detected by asan) - harmless here, but
easy to fix.
2017-07-24 12:35:12 +00:00
kre 4401d6e524 PR bin/52348
Silence nuisance testing environments - avoid << of a negative number
(a signed char -- in a hash function, the result is irrelevant, as long
as it is repeatable).
2017-07-24 12:34:45 +00:00
christos 397b110260 Add a variant that passes the format, so that time(1) can use it. 2017-07-15 14:35:55 +00:00
kre cdfea65894 Fix a reference after free (and consequent nonsense diagnostic for
attempts to set readonly variables) I added in 1.60 by incompletely
copying the FreeBSD fix for the lost memory issue.
2017-07-12 19:06:16 +00:00
kre 2eafa75298 DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format.   NFC.
2017-07-05 20:00:27 +00:00
kre 9006b74112 DEBUG changes: convert DEBUG TRACE() calls to new format.
ALso, cause exec failures to always cause the shell to exit with
status 126 or 127, whatever the cause.  127 is intended for lookup
failures (and is used that way), 126 is used for anything else that
goes wrong (as in several other shells.)  We no longer use 2 (more easily
confused with an exit status of the command exec'd) for shell exec failures.
2017-07-05 19:58:10 +00:00
kre ae375f0127 Mostly DEBUG and white space changes. Convert DEEBUG TRACE() calls to
the new format.   Also #if 0 a function definition that is used nowhere.
While here, change the function of pushfile() slightly - it now sets
the buf pointer in the top (new) input descriptor to NULL, instead of
simply leaving it - code that needs a buffer always (before and after)
must malloc() one and assign it after the call.  But code which does not
(which will be reading from a string or similar) now does not have to
explicitly set it to NULL (cleaner interface.)   NFC intended (or observed.)
2017-07-05 19:54:21 +00:00
kre 2e1a04e9a7 DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style.   NFC (when not debugging sh).
2017-07-05 19:47:11 +00:00
wiz ee3d656c82 Use Ex. 2017-07-04 06:50:04 +00:00
wiz 285e86b675 Use Ex. Remove superfluous Pp. 2017-07-04 06:49:35 +00:00
wiz 39b251a71c Use Ex. 2017-07-04 06:46:51 +00:00
wiz 01869ca4d2 Remove workaround for ancient HTML generation code. 2017-07-03 21:28:48 +00:00
kre a64e63ed28 Do a better job of detecting the error in pkgsrc/devel/libbson-1.6.3's
configure script, ie: $(( which is intended to be a sub-shell in a
command substitution, but is an arith subst instead, it needs to be
written $( ( to do as intended.   Instead of just blindly carrying on to
find the missing )) somewhere, anywhere, give up as soon as we have seen
an unbalanced ')' that isn't immediately followed by another ')' which
in a valid arith subst it always would be.

While here, there has been a comment in the code for quite a while noting a
difference in the standard between the text descr & grammar when it comes to
the syntax of case statements.   Add more comments to explain why parsing it
as we do is in fact definitely the correct way (ie: the grammar wins arguments
like this...).
2017-07-03 20:16:44 +00:00
joerg da656e3d1c Kill enough K&R cruft to build with clang again. 2017-07-01 23:12:08 +00:00
kre 2ff69d1b74 Make $- list flags in the same order they appear in sh(1) 2017-07-01 06:14:51 +00:00
wiz ea9a6a6434 Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
2017-07-01 05:11:57 +00:00
kre 8d40ff9951 Correct a markup typo (why did I not see this before the prev commit??) 2017-06-30 23:48:50 +00:00
kre 3b0318c107 Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
2017-06-30 23:07:29 +00:00
kre dc83385122 Implement a bunch of new shell environment variables. many mostly useful
in prompts when expanded at prompt time, but all available for general use.
Many of the new ones are not available in SMALL shells (they work as normal
if assigned, but the shell does not set or use them - and there is no magic
in a SMALL shell (usually for install media.))
2017-06-30 23:05:45 +00:00
kre 1fca9bbf62 Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
2017-06-30 23:02:56 +00:00
kre 3b297678bf Include redirections in trace output from "set -x" 2017-06-30 23:01:21 +00:00
kre 19986c5f4a NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
2017-06-30 23:00:40 +00:00
kamil 6327936549 ksh: Eliminate dead function x_complete_word() 2017-06-30 05:18:36 +00:00
kamil dda96fab20 ksh: Eliminate dead code from expand.h 2017-06-30 05:14:12 +00:00
kamil 00cd1c66a2 ksh: Drop old hack for FreeBSD 1.1.5 and CLK_TCK 2017-06-30 04:44:46 +00:00
kamil dd8a75d57f ksh: Upgrade to C99 <stdbool.h>
This shell already used C99 functions.
2017-06-30 04:41:19 +00:00
kamil c2a517fe40 ksh: Eliminate dead MEM_DEBUG sections, there is missing code for it
MEM_DEBUG used to contain malloc(3) debugging facilities.
2017-06-30 04:30:26 +00:00
kamil a5a27bbf6f ksh: Drop support for OSes without POSIX sigaction(2) 2017-06-30 04:22:22 +00:00
kamil 2537c199ea ksh: Drop support for OSes that don't map /dev/fd 2017-06-30 04:11:57 +00:00
kamil 91e15fa768 ksh: Drop old hack fo SunOS 4.1.x (1990-1994) 2017-06-30 04:01:48 +00:00
kamil fac4b39487 ksh: Drop support for systems without <sys/stat.h>
In future the order of includes will be normalized with KNF.

No functional change intended.
2017-06-30 03:56:12 +00:00
kamil 1dacfa0dd2 ksh: Drop support for systems with broken <sys/time.h> / <time.h> 2017-06-30 03:43:57 +00:00
kamil e6ed5dea98 ksh: Drop support for systems without POSIX lstat(2) 2017-06-30 03:35:16 +00:00
kamil a396fc9936 ksh: Drop support for systems without POSIX <sys/wait.h> 2017-06-30 03:32:28 +00:00
kamil 3e5f067b68 ksh: Drop support for systems with broken times(3)
This fallback code wouldn't work anyway.

times(3) is an obsolete interface by getrusage(2) and gettimeofday(2).
In future it will be swiched to more modern interfaces.

No functional change intended.
2017-06-30 03:23:18 +00:00
kamil 2c22621e8f ksh: Assume ANSI C prototypes 2017-06-30 02:51:14 +00:00
kamil 81c962c859 ksh: Drop support for systems without functional waitpid(2) 2017-06-30 02:38:09 +00:00
kamil 8ea5ae4cd1 ksh: Drop support for systems without POSIX signal routines 2017-06-30 02:20:47 +00:00
kamil 306d025987 ksh: Drop support for systems without implementation of EINTR
These systems weren't handled anyway.
2017-06-30 02:13:29 +00:00
kamil 51ebc97c9d ksh: Drop support for systems without handling shebang 2017-06-30 02:06:59 +00:00
kamil 5c93126131 ksh: Drop support for OSes without POSIX types
Assume available: mode_t, off_t, pid_t, uid_t, rlim_t, sigset_t.
2017-06-30 02:02:41 +00:00
kamil 9d5663e055 ksh: Drop support for systems without <sys/wait.h> 2017-06-30 01:56:34 +00:00
kamil 3e63fce1f9 ksh: Drop ksh_wait.h that reinvents <sys/wait.h> (POSIX header)
Switch jobs.c to <sys/wait.h>.

No functional change intended.
2017-06-30 01:52:34 +00:00
kamil d0aa393796 ksh: Drop support for systems without gid_t 2017-06-30 01:36:30 +00:00
kre 4754b1e8f4 Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
2017-06-28 13:46:06 +00:00
christos 650f73e320 Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
2017-06-27 23:27:03 +00:00
kre 8ca5565bc1 Make one example more like a real world possibility (it still isn't, but
is closer) - though the actual content is irrelevant to the point being made.
2017-06-27 12:43:44 +00:00
wiz 30b9dfc881 Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
2017-06-27 08:30:40 +00:00
kre 3c2922e71c Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
2017-06-27 02:22:08 +00:00
kre a3be5e86c2 Make arg parsing in kill POSIX compatible with POSIX (XBD 2.12) by
parsing the way getopt(3) would, if only it could handle the (required)
-signumber and -signame options.  This adds two "features" to kill,
-ssigname and -lstatus now work (ie: one word with all of the '-', the
option letter, and its value) and "--" also now works (kill -- -pid1 pid2
will not attempt to send the pid1 signal to pid2, but rather SIGTERM
to the pid1 process group and pid2).  It is still the case that (apart
from --) at most 1 option is permitted (-l, -s, -signame, or -signumber.)

Note that we now have an ambiguity, -sname might mean "-s name" or
send the signal "sname" - if one of those turns out to be valid, that
will be accepted, otherwise the error message will indicate that "sname"
is not a valid signal name, not that "name" is not.   Keeping the "-s"
and signal name as separate words avoids this issue.

Also caution: should someone be weird enough to define a new signal
name (as in the part after SIG) which is almost the same name as an
existing name that starts with 'S' by adding an extra 'S' prepended
(eg: adding a SIGSSYS) then the ambiguity problem becomes much worse.
In that case "kill -ssys" will be resolved in favour of the "-s"
flag being used (the more modern syntax) and would send a SIGSYS, rather
that a SIGSSYS.    So don't do that.

While here, switch to using signalname(3) (bye bye NSIG, et. al.), add
some constipation, and show a little pride in formatting the signal names
for "kill -l" (and in the usage when appropriate -- same routine.)   Respect
COLUMNS (POSIX XBD 8.3) as primary specification of the width (terminal width,
not number of columns to print) for kill -l, a very small value for COLUMNS
will cause kill -l output to list signals one per line, a very large
value will cause them all to be listed on one line.) (eg: "COLUMNS=1 kill -l")

TODO: the signal printing for "trap -l" and that for "kill -l"
should be switched to use a common routine (for the sh builtin versions.)

All changes of relevance here are to bin/kill - the (minor) changes to bin/sh
are only to properly expose the builtin version of getenv(3) so the builtin
version of kill can use it (ie: make its prototype available.)
2017-06-26 22:09:16 +00:00
christos 57d721417b source .editrc after we initialize so that commands persist! 2017-06-26 20:28:01 +00:00
kre 7f75cc46b5 Another ancient (highly improbable) bug bites the dust. This one
caused by incorrect macro usage (ie: using the wrong one) which has
been in the sources since version 1.1 (ie: forever).

Like the previous (STACKSTRNUL) bug, the probability of this one
actually occurring has been infinitesimal but the LINENO code increases
that to infinitesimal and a smidgen... (or a few, depending upon usage).

Still, apparently that was enough, Kamil Rytarowski discovered that the
zsh configure script (damn competition!) managed to trigger this problem.
2017-06-24 11:23:35 +00:00
kamil c6c3f8f1f5 ksh: Drop support for systems without clock_t type 2017-06-23 00:35:20 +00:00
kamil b831372d44 ksh: Replace homegrown int_least32_t with the C99 version 2017-06-23 00:29:42 +00:00
kamil 730f723665 ksh: Drop support for systems that return void for closedir(2) 2017-06-23 00:20:22 +00:00
kamil acc2fa794c ksh: Use ANSI C varargs, drop support for older version <varargs.h> 2017-06-23 00:18:01 +00:00
kamil cb1c866326 ksh: Remove remnant hack for SCO UNIX in tty code 2017-06-23 00:11:01 +00:00
kamil a761aa7e3f ksh: Remove support for NeXT Operating System 2017-06-23 00:09:36 +00:00
kamil 0e8072bdff ksh: Drop the latest ifdef for BSD4.1 and eliminate dead code around it 2017-06-23 00:07:15 +00:00
kamil c65b0df2e9 ksh: Drop BSD4.3 temporary hack in tty code 2017-06-23 00:04:20 +00:00
kamil 14ae4d8526 ksh: Drop support for UNIX V7-style signal routines 2017-06-23 00:00:58 +00:00
kamil 0a9965d29a ksh: Drop fallback for BSD4.2 signal routines 2017-06-22 23:59:28 +00:00
kamil 53e78cf286 ksh: Remove fallback to BSD4.1 signal routines 2017-06-22 23:56:24 +00:00
kamil 3b7ff6c991 ksh: Drop support for systems without mmap(2) 2017-06-22 23:54:13 +00:00
kamil 52b3355e26 ksh: Drop support for systems without dup2(2) 2017-06-22 23:50:24 +00:00
kamil 237b342911 ksh: Drop support for ISC UNIX 2017-06-22 23:47:29 +00:00
kamil eed5a31e9b ksh: Drop support for systems without offsetof(3) 2017-06-22 23:42:35 +00:00
kamil bdcebfe8f5 ksh: Drop support for systems without <limits.h> 2017-06-22 23:38:49 +00:00
kamil accac1300c ksh: Drop support for systems without <fcntl.h> 2017-06-22 23:37:00 +00:00
kamil fb30a30908 ksh: Drop support for systems without memmove(3) and stop using bcopy(3) 2017-06-22 23:33:36 +00:00
kamil 99f9f7cfb5 ksh: Drop support for systems without memset(3) 2017-06-22 23:30:42 +00:00
kamil 98fef8f2dd ksh: Drop usage of SVID header <memory.h>, it's legacy standard 2017-06-22 23:29:35 +00:00
kamil 00f99741e1 ksh: Drop support for systems without strcasecmp(3) and strncasecmp(3) 2017-06-22 23:27:53 +00:00
kamil 05a5edb66a ksh: Drop support for systems without strstr(3) 2017-06-22 23:26:17 +00:00
kamil 8f43bfe473 ksh: Drop support for systems without <string.h> 2017-06-22 23:23:27 +00:00
kamil ba6c50ffa7 ksh: Drop support for systems without <stddef.h>
No functional change.
2017-06-22 23:19:53 +00:00
kamil 6f9fbdf520 ksh: Drop support for systems without <stdlib.h>
No functional change.
2017-06-22 23:17:50 +00:00
kamil 17e1427bb7 ksh: Drop support for systems without <unistd.h>
This code wouldn't work for them anyway.

No functional change.
2017-06-22 23:15:05 +00:00
kamil b42d86bc68 ksh: Kill switch for CLOSEDIR_VOID (closedir(2) returns void)
No users in the code. No functional change.
2017-06-22 23:11:48 +00:00
kamil 9d04c96c0d ksh: Assume that $CC handles const (ANSI C89)
No functional change.
2017-06-22 23:09:32 +00:00
kamil af6175f2e1 Drop support for $CC incapable to use void* / volatile* (pre ANSI C89) 2017-06-22 19:46:54 +00:00
kamil d6fe16556c Drop Ultrix support from ksh(1), a DEC UNIX for VAX 2017-06-22 19:41:07 +00:00
kamil eb13ce8ce3 Reapply removal of code from 90ties for OS/2 Cygwin AIX HPUX SCOUnix
Added missing #endif terminator in emacs.c
2017-06-22 14:20:46 +00:00
kamil 266bea007e Temporarily revert previous.
emacs.* gets wrong code in generation
2017-06-22 14:11:27 +00:00
kamil ffa8eeb219 Remove code for AIX, including hack for 3.2.5 (from 1997) - from ksh(1)
OK by <kre>
2017-06-22 13:37:16 +00:00
kamil 2f8869f916 Drop HP-UX support from ksh(1)
OK by <kre>
2017-06-22 13:35:47 +00:00
kamil 4ae20c97a6 Remove sco unix 3.2v4.1 support (from 1992) from ksh(1)
OK by <kre>
2017-06-22 13:34:48 +00:00
kamil e5604d4f02 Remove os2 support in ksh(1)
OK by <kre>
2017-06-22 13:33:39 +00:00