Commit Graph

3606 Commits

Author SHA1 Message Date
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