Commit Graph

3592 Commits

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