Home | History | Annotate | only in /src/bin
History log of /src/bin
RevisionDateAuthorComments
 1.23 16-May-2020  christos Add ACL support for FFS. From FreeBSD.
 1.22 31-Dec-2007  ad branches: 1.22.70;
Remove systrace. Ok core@.
 1.21 08-Feb-2007  tls branches: 1.21.4;
Remove 'l'. Those who do not learn from history are doomed to repeat it.
 1.20 07-Feb-2007  tls Many users needlessly maintain the alias 'l' (or 'll') for 'ls -l'. We
should ease the burden on our users and supply a default system which is
modern and has a full complement of the features they expect (or even some
they don't -- more features don't hurt any one after all).

Suggested by perry@ in <87wt2uxhbx.fsf@snark.piermont.com> and submitted
for discussion to some NetBSD developers, who suggested that rather than
my own idiosyncratic 'll', 'l' was a much better name.

It may prove possible to merge this code with 'ls' in the future.
 1.19 17-Jun-2002  christos Add userland portion of systrace.
 1.18 23-Nov-1999  mrg move rcorder to sbin.
 1.17 19-Nov-1999  mrg add rcorder
 1.16 29-May-1997  veego branches: 1.16.8;
Add rcmd to the SUBDIR list.
 1.15 13-Mar-1997  perry Remove rmail -- it has moved to libexec.
 1.14 16-Jan-1997  perry add ksh to Makefile per pr-3051 -- approved by thorpej
 1.13 23-Apr-1996  thorpej Oops, forgot to add "chio" to the subdirs list.
 1.12 21-Mar-1995  cgd clean up id tags, etc.
 1.11 13-Jun-1994  jtc add pax
 1.10 30-Jan-1994  mycroft Alphabetize.
 1.9 01-Aug-1993  mycroft Add RCS identifiers.
 1.8 04-May-1993  glass added dd(1) from ftp.uu.net:bsd-sources; torching GNU dd
 1.7 29-Apr-1993  cgd dd is gnu software
 1.6 02-Apr-1993  deraadt Build domainname binary for us.
 1.5 01-Apr-1993  cgd added 'ed'
 1.4 25-Mar-1993  cgd added mt to SUBDIRS
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.16.8.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.21.4.1 09-Jan-2008  matt sync with HEAD
 1.22.70.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.22.70.1 21-Apr-2020  martin Sync with HEAD
 1.20 12-May-2024  rillig lint: suppress "used but not defined" for bin, sbin, usr.bin, usr.sbin

These diagnostics are emitted by lint2, and like all diagnostics from
lint2, they don't affect the exit status.

In libc, lint cannot handle the renamings from namespace.h, generating
many false positive diagnostics since 'libc_function' is used but
'_libc_function' is defined. Until this situation is handled properly,
suppress this diagnostic.
 1.19 20-Apr-2024  rillig {usr.,}{s,}bin: replace LINT_SUPPORTED with the standard NOLINT

While here, re-enable lint in those cases where lint was skipped due to
a bug in interpreting abstract types, which was fixed in cgram.y 1.469
from 2023-08-02.
 1.18 09-Oct-2021  rillig bin: in builds with MKLINT=yes, run lint on all programs
 1.17 21-Mar-2012  matt branches: 1.17.34;
Default to WARNS=5
 1.16 14-Feb-2009  lukem branches: 1.16.6;
Default to WARNS=4 (except for rcp & ksh)
 1.15 26-Jun-2005  christos branches: 1.15.28; 1.15.32;
sprinkle a little const, and now everything compiles with WARNS=3
 1.14 27-Sep-2002  thorpej Revert previous; the change that required it will be done differently.
 1.13 27-Sep-2002  thorpej Only use the MKDYNAMICROOT semantics (i.e. -rpath=/lib,/usr/lib and
-dynamic-linker=/libexec/ld.elf_so) if the BINDIR of the program being
built is /bin or /sbin.

The reason we do this is because now all programs *except* those in
/bin and /sbin (i.e. the "special cases") match the default the compiler
uses, which is what is used for things in e.g. xsrc, pkgsrc, and other
random 3rd party programs.
 1.12 27-Aug-2002  lukem Implement MKDYNAMICROOT, which currently defaults to "no", but will
be changed in the future to "yes".

If MKDYNAMICROOT == "no", there is no change from existing behaviour
of a static /bin and /sbin (and a few programs in elsewhere).

If MKDYNAMICROOT == "yes", the following changes occur:
in <bsd.own.mk>:
SHLIBDIR?= /lib
SHLINKDIR?= /lib
in various Makefiles, the following entry is DISABLED.
LDSTATIC?=-static
This results in all programs (except those "standalone" programs built
in sys/arch/*/stand) are linked dynamically, the shared linker is moved
from /usr/libexec to /lib (with a compat symlink), and the shared
libraries used by /bin and /sbin programs are moved from /usr/lib to
/lib (with compat symlinks).
 1.11 03-Nov-2001  lukem change from WARNS?=1 to WARNS?=2 for bin/
 1.10 02-Mar-1998  cgd pull bsd.own.mk in before LDSTATIC?=-static, so that LDSTATIC can be
overridden by bsd.own.mk/mk.conf.
 1.9 10-Oct-1997  mikel allow overrides of LDSTATIC, from Soren S. Jorvang in PR bin/4178
 1.8 20-Jul-1997  christos Remove WARNS=1 from all the subdirectory Makefiles, and add it to Makefile.inc
now that all /bin has been cleaned.
 1.7 21-Mar-1995  cgd clean up id tags, etc.
 1.6 08-Nov-1993  cgd use LDSTATIC, not LDFLAGS any more
 1.5 03-Nov-1993  cgd add 'LDFLAGS+= -static'
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.15.32.1 27-Aug-2009  matt Change LDSTATIC?= -static to LDSTATIC+= -static so MKDYNAMICROOT=no works again.
 1.15.28.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.16.6.1 17-Apr-2012  yamt sync with head
 1.17.34.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.17.34.1 21-Apr-2020  martin Sync with HEAD
 1.12 18-May-2003  lukem branches: 1.12.98;
Now that <bsd.prog.mk> DTRT if HOSTPROG is defined (i.e, it is a no-op),
there's no need to special-case .include-ing it.
 1.11 10-Jun-2002  mason Add wrapper around bsd.prog.mk include in preparation for this becoming a
host tool.
 1.10 03-Nov-2001  lukem branches: 1.10.2;
change from WARNS?=1 to WARNS?=2 for bin/
 1.9 08-Jul-1999  christos Make this compile with WARNS = 2
 1.8 20-Jul-1997  christos Remove WARNS=1 from all the subdirectory Makefiles, and add it to Makefile.inc
now that all /bin has been cleaned.
 1.7 20-Jul-1997  thorpej Add WARNS.
 1.6 23-Apr-1995  cgd clean up RCS Ids slightly, etc.
 1.5 20-Sep-1994  mycroft Merge with 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.10.2.1 11-Jun-2002  lukem Pull up revision 1.11 (requested by mason in ticket #244):
Add wrapper around bsd.prog.mk include in preparation for this becoming a
host tool.
 1.12.98.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.12.98.1 21-Apr-2020  martin Sync with HEAD
 1.41 02-Oct-2017  wiz branches: 1.41.4;
Add cross reference to tac(1).
 1.40 04-Jul-2017  wiz Use Ex.
 1.39 03-Jul-2017  wiz Remove workaround for ancient HTML generation code.
 1.38 15-Jun-2014  wiz Bump date for previous.
 1.37 15-Jun-2014  dbj update list of flags which are extensions to the specification
 1.36 10-Nov-2012  wiz branches: 1.36.8;
Sort options (no-argument ones first, then argument-taking ones).
 1.35 10-Nov-2012  christos Add an option to read with a different buffer size, and document the
buffer size we use. This allows us to cat -B 10000000 /proc/<pid>/maps
for example which cannot handle seeking.
 1.34 09-Aug-2012  dholland branches: 1.34.2;
Use "e.g.", not "i.e.", to introduce an example.
 1.33 06-Apr-2012  wiz Describe behaviour when no arguments are given.
Add comma.
Bump date.

From Bug Hunting.
 1.32 23-Sep-2006  wiz branches: 1.32.40;
Use macro for HTML output.
 1.31 23-Sep-2006  elad PR/16827: Giorgos Keramidas: addition of EXAMPLES section to
basesrc/bin/cat.1
Applied patch, thanks!
 1.30 27-Aug-2003  junyoung Add missing "."
 1.29 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.28 25-Sep-2002  wiz New policy: New sentences start on a new line.
Patches by Robert Elz <kre at munnari oz au>, with minimal changes by me.
 1.27 03-Jul-2002  enami The el option is an extention.
 1.26 10-Jun-2002  mason Add -l option to cat(1), as discussed on tech-userlevel. This option
causes cat(1) to use fcntl(2) to set an exclusive advisory lock on stdout.
While being useful in its own right, this will shortly be used to
guarantee orderly writing to METALOG in the case of unprivileged builds
with NBUILDJOBS > 1.
 1.25 15-May-2002  kleink branches: 1.25.2;
Consistent wording: `regular' files, not `plain' ones; from Giorgos
Keramidas in PR bin/16824.
 1.24 08-Feb-2002  ross Generate <>& symbolically. I'm avoiding .../dist/... directories for now.
 1.23 20-Dec-2001  wiz Sort sections, drop unnecessary .Pps.
 1.22 20-Dec-2001  wiz Whitespace nits.
 1.21 04-Sep-2000  kleink For commands and utilities, use EXIT STATUS rather than RETURN VALUES as
appropriate (and documented in mdoc(7)).
 1.20 28-Aug-2000  hubertf Add 'RETURN VALUE' section header.
 1.19 02-Aug-2000  jwise Use .At v1 macro instead of spelling out AT&T Unix version 1.
 1.18 15-Jan-2000  christos branches: 1.18.4;
Add a -f fflag that makes sure that we only try to read from plain files
so that there is no chance to block.
 1.17 09-Jan-2000  abs Slightly modified small fixes from Richard Rauch in PR bin/9150
 1.16 22-Mar-1999  garbled More and more .Os cleanups. .Os is defined in the tmac.doc-common file,
so we shouldn't override it with versions in the manpages. Many more to
come.
 1.15 11-Mar-1999  fair Patch from PR#5156 to adjust buffer size to match filesystem block size
when "-u" option used, plus some additional documentation.
 1.14 28-Apr-1998  fair Change occurrences of "UNIX" to .Ux or .At as appropriate.
 1.13 20-Oct-1997  enami Fix .Nm usage.
 1.12 27-Sep-1995  cgd kill an extr space in a (literally-printed) example
 1.11 07-Sep-1995  jtc Sync with 4.4lite2
 1.10 25-Jul-1995  jtc Use "utility" instead of "command". Modern definitions of these terms
are distinct (See POSIX.2 glossary).

A utility is a executable, script or shell builtin; while a command
can be any of those things plus lists, pipelines, compound commands
(if, for, while) and shell function definitions.
 1.9 21-Mar-1995  cgd convert to new RCS id conventions.
 1.8 04-Nov-1994  cgd bugs don't get any dumber than this.
 1.7 20-Sep-1994  mycroft Merge with 4.4-Lite version.
 1.6 30-Nov-1993  jtc Use ".At v6" macro instead of spelling out "Version 6 AT&T UNIX".
 1.5 16-Aug-1993  jtc Document the fact that cat is a superset of 1003.2's cat.
 1.4 01-Aug-1993  mycroft Add RCS indentifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.3 07-Sep-1995  jtc imported from 44lite2
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.18.4.1 28-Aug-2000  hubertf Pull up to netbsd-1-5 branch, OK'd by thorpej:

Log Message:
> Add 'RETURN VALUE' section header.

Files & Revisionis:
> cvs rdiff -r1.19 -r1.20 basesrc/bin/cat/cat.1
> cvs rdiff -r1.12 -r1.13 basesrc/bin/chmod/chmod.1
> cvs rdiff -r1.14 -r1.15 basesrc/bin/cp/cp.1
> cvs rdiff -r1.8 -r1.9 basesrc/bin/dd/dd.1
> cvs rdiff -r1.9 -r1.10 basesrc/bin/echo/echo.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/expr/expr.1
> cvs rdiff -r1.25 -r1.26 basesrc/bin/ls/ls.1
> cvs rdiff -r1.10 -r1.11 basesrc/bin/mkdir/mkdir.1
> cvs rdiff -r1.23 -r1.24 basesrc/bin/mt/mt.1
> cvs rdiff -r1.12 -r1.13 basesrc/bin/mv/mv.1
> cvs rdiff -r1.16 -r1.17 basesrc/bin/pwd/pwd.1
> cvs rdiff -r1.9 -r1.10 basesrc/bin/rm/rm.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/rmdir/rmdir.1
> cvs rdiff -r1.35 -r1.36 basesrc/bin/sh/sh.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/sleep/sleep.1
> cvs rdiff -r1.20 -r1.21 basesrc/bin/stty/stty.1
 1.25.2.2 24-Sep-2003  tron Pull up revision 1.30 (requested by junyoung in ticket #1469):
Add missing "."
 1.25.2.1 11-Jun-2002  lukem Pull up revision 1.26 (requested by mason in ticket #243):
Add -l option to cat(1), as discussed on tech-userlevel. This option
causes cat(1) to use fcntl(2) to set an exclusive advisory lock on stdout.
While being useful in its own right, this will shortly be used to
guarantee orderly writing to METALOG in the case of unprivileged builds
with NBUILDJOBS > 1.
 1.32.40.3 16-Jan-2013  yamt sync with (a bit old) head
 1.32.40.2 30-Oct-2012  yamt sync with head
 1.32.40.1 17-Apr-2012  yamt sync with head
 1.34.2.2 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.34.2.1 20-Nov-2012  tls Resync to 2012-11-19 00:00:00 UTC
 1.36.8.1 10-Aug-2014  tls Rebase.
 1.41.4.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.41.4.1 21-Apr-2020  martin Sync with HEAD
 1.60 10-Dec-2023  rillig cat: repeat storage class 'static' for local functions

No binary change.
 1.59 07-Dec-2023  rillig cat: reduce visibility of local functions

No functional change.
 1.58 19-May-2023  rillig cat: fix misleading indentation

Found by indent, but not by GCC 10.

No binary change.
 1.57 16-Jun-2016  sevan branches: 1.57.16;
When invoked with -se, print a '$' on blank lines
Obtained from OpenBSD r1.13 of src/bin/cat/cat.c

Closes PR bin/51250
Reviewed by Christos@
 1.56 16-Jun-2016  sevan Revert previous change made by me in r1.55
 1.55 25-Jul-2015  sevan Drop the case '?' which is defined after case 'default'.
case 'default' statement introduced in the last commit to cat.c in the CSRG archive.
Obtained from OpenBSD bin/cat/cat.c r1.3.
 1.54 08-Dec-2013  spz branches: 1.54.4;
more complete fix for Coverity issue 976653
 1.53 03-Dec-2013  spz Coverity complaint fixes:
bin/cat/cat.c 976654 Argument cannot be negative
(missing check for fileno result, stdout)
bin/cat/cat.c 976653 Improper use of negative value
(missing check for fileno result, stdin)
 1.52 19-Nov-2012  christos only allocate if it would not fit in our buffer.
 1.51 19-Nov-2012  christos fix bogus warning for zero-sized files.
 1.50 10-Nov-2012  wiz Sync usage with man page.
 1.49 10-Nov-2012  christos Add an option to read with a different buffer size, and document the
buffer size we use. This allows us to cat -B 10000000 /proc/<pid>/maps
for example which cannot handle seeking.
 1.48 17-Mar-2012  christos branches: 1.48.2;
fix signed blksize_t lossage.
 1.47 20-Jul-2008  lukem branches: 1.47.2; 1.47.20;
Remove the \n and tabs from the __COPYRIGHT() strings.
 1.46 13-Jan-2007  cbiere branches: 1.46.12;
- Applied patch from PR bin/35408 by Andreas Wiese.
- Use EXIT_FAILURE instead of 1.
- Use return instead of exit() at the end of main().
- Use sizeof() instead of BUFSIZ.
 1.45 08-Oct-2006  elad PR/32759: Ulrich Heilmann: Output of cat -s -n is wrong

Applied patch from David A. Holland, thanks!
 1.44 26-Feb-2006  christos PR/32931: Johan Veenhuizen: cat(1) does not clear the EOF indicator for
stdin in cooked mode.
 1.43 04-Jan-2004  jschauma Remove unused code left over from temporary printescaped functionality.
Noted by Jeff Ito in PR bin/23966.
 1.42 27-Oct-2003  lukem Overhaul how `build.sh tools' are used:

* Rename "config.h" to "nbtool_config.h" and
HAVE_CONFIG_H to HAVE_NBTOOL_CONFIG_H.
This makes in more obvious in the source when we're using
tools/compat/config.h versus "standard autoconf" config.h

* Consistently move the inclusion of nbtool_config.h to before
<sys/cdefs.h> so that the former can provide __RCSID() (et al),
and there's no need to protect those macros any more.

These changes should make it easier to "tool-ify" a program by adding:
#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
to the top of the source files (for the general case).
 1.41 14-Sep-2003  jschauma Following private discussion with kleink@ and hubertf@ and public discussion
on tech-userlevel@, back out printescaped() functionality.
kleink: ``We sell rope.''
 1.40 27-Aug-2003  uwe Include "config.h" if building as a host program.
Need this to get SIZE_MAX defintion from compat_defs.h on FreeBSD.
 1.39 25-Aug-2003  kleink This is a host tool, so for the time being change SIZE_T_MAX to the
more portable SIZE_MAX; partially addresses PR toolchain/22504 from
Christian Limpach.
 1.38 13-Aug-2003  itojun use bounded string op
 1.37 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.36 04-Aug-2003  jschauma As discusses a little while back on tech-userlevel:

If stdout is a tty, use vis(3) to print any filenames to prevent garbage
from being printed if the filename contains control- or other non-printable
characters.

While here, sprinkle some EXIT_FAILURE and NOTREACHED where appropriate.
 1.35 13-Sep-2002  thorpej Don't use MAX(); not all systems have it, and this is a host tool.
 1.34 11-Jun-2002  bjh21 Rather than (ab)using the standard error message for EFTYPE, print our own
message when "cat -f" is used on a non-regular file. This is necessary to
make cat(1) portable to systems that don't have EFTYPE (like Linux).
 1.33 11-Jun-2002  bjh21 Only use __RCSID() and __COPYRIGHT() if they're defined.
 1.32 10-Jun-2002  mason Add -l option to cat(1), as discussed on tech-userlevel. This option
causes cat(1) to use fcntl(2) to set an exclusive advisory lock on stdout.
While being useful in its own right, this will shortly be used to
guarantee orderly writing to METALOG in the case of unprivileged builds
with NBUILDJOBS > 1.
 1.31 09-May-2002  simonb branches: 1.31.2;
Simplify previous - sbuf.st_blksize is a uint32_t, so just use
bsize = MAX(sbuf.st_blksize, BUFSIZ);
and skip playing with SSIZE_MAX altogether.
 1.30 09-May-2002  thorpej Fix oversight in last.
 1.29 09-May-2002  thorpej Skip a check that is always true if _LP64 is defined.
 1.28 16-Sep-2001  wiz Some KNF, via patch by Petri Koistinen in private mail.
 1.27 29-Jul-2001  wiz Some style improvements. [Nearly] #13592 by Petri Koistinen.
 1.26 03-Jan-2001  mjl Un-__P and ANSIfy
 1.25 03-Jan-2001  mjl Sync usage w/ options, remove bogus cast.
 1.24 31-Jul-2000  christos don't use the "f" flag to fopen when -f is not specified (from Chuck Cranor)
 1.23 14-Apr-2000  simonb Don't declare 'extern opt*' getopt variables.
 1.22 15-Jan-2000  christos Add a -f fflag that makes sure that we only try to read from plain files
so that there is no chance to block.
 1.21 08-Jul-1999  christos Make this compile with WARNS = 2
 1.20 26-Apr-1999  hannken Fix buffer allocation in function `raw_cat'. It was possible to get a
buffer size of zero if stdout has a block size of 0.
The command `rsh <host> cat <files>' gives no output and no error.

- Allocate a minimum of BUFSIZ bytes for buffer.
- Use a static buffer if either `fstat' or `malloc' fails.
- Do the allocation once since stdout will not change block size.
 1.19 11-Mar-1999  fair branches: 1.19.2;
Patch from PR#5156 to adjust buffer size to match filesystem block size
when "-u" option used, plus some additional documentation.
 1.18 28-Jul-1998  mycroft Delint.
 1.17 27-Jan-1998  kleink Fix the EOL-indicators ('$') toggled by the `-e' option to be lined up
correctly when used in conjunction with the `-b' option; from Giles Lean
in PR bin/4841, modified by me to retain the line-counting semantics of `-b'.
 1.16 05-Nov-1997  cgd lint
 1.15 20-Jul-1997  thorpej branches: 1.15.2;
__COPYRIGHT()/__RCSID()/prototype police.
 1.14 26-Jun-1997  kleink When allocating a buffer failed, don't fail silently.
 1.13 27-Apr-1997  kleink Indicate file handling failures by exit codes >0; fixes PR/3538 from
David Eckhardt <davide@piper.nectar.cs.cmu.edu>.
 1.12 09-Jan-1997  tls kill use of register
 1.11 07-Sep-1995  jtc Sync with 4.4lite2
 1.10 21-Mar-1995  cgd convert to new RCS id conventions.
 1.9 20-Sep-1994  mycroft Merge with 4.4-Lite version.
 1.8 19-Nov-1993  jtc POSIX.2 utilities must call setlocale(LC_ALL, "");
 1.7 10-Sep-1993  jtc Too few arguments for err() format specification.
 1.6 01-Aug-1993  mycroft Add RCS identifiers.
 1.5 11-Jul-1993  cgd fix the cat bug found by mycroft in a more asthetically pleasing
way... also turn "Header" into "Id" for RCS...
 1.4 11-Jul-1993  mycroft Work correctly if a write() comes up short.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.3 07-Sep-1995  jtc imported from 44lite2
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.15.2.1 08-Feb-1998  mellon Pull up 1.16 and 1.17 (kleink)
 1.19.2.1 26-Apr-1999  perry pullup 1.19->1.20 (Juergen Hannken-Illjes)
 1.31.2.3 16-Jun-2004  jmc Missing config.h bring-in. Add for PR#24481
 1.31.2.2 03-Nov-2002  he Pull up revision 1.35 (requested by thorpej in ticket #793):
Don't use MAX(); not all systems have it, and this is a host
tool.
 1.31.2.1 11-Jun-2002  lukem Pull up revision 1.32 (requested by mason in ticket #243):
Add -l option to cat(1), as discussed on tech-userlevel. This option
causes cat(1) to use fcntl(2) to set an exclusive advisory lock on stdout.
While being useful in its own right, this will shortly be used to
guarantee orderly writing to METALOG in the case of unprivileged builds
with NBUILDJOBS > 1.
 1.46.12.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.47.20.1 25-Mar-2017  snj Pull up following revision(s) (requested by sevan in ticket #1387):
bin/cat/cat.c: revision 1.57
When invoked with -se, print a '$' on blank lines
Obtained from OpenBSD r1.13 of src/bin/cat/cat.c
Closes PR bin/51250
Reviewed by Christos@
 1.47.2.3 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.47.2.2 16-Jan-2013  yamt sync with (a bit old) head
 1.47.2.1 17-Apr-2012  yamt sync with head
 1.48.2.3 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.48.2.2 25-Feb-2013  tls resync with head
 1.48.2.1 20-Nov-2012  tls Resync to 2012-11-19 00:00:00 UTC
 1.54.4.1 05-Jul-2016  snj Pull up following revision(s) (requested by sevan in ticket #1182):
bin/cat/cat.c: revision 1.57
distrib/sets/lists/tests/mi: revision 1.675
tests/bin/cat/Makefile: revision 1.2
tests/bin/cat/d_se_output.in: revision 1.1
tests/bin/cat/d_se_output.out: revision 1.1
tests/bin/cat/t_cat.sh: revision 1.3
When invoked with -se, print a '$' on blank lines
Obtained from OpenBSD r1.13 of src/bin/cat/cat.c
Closes PR bin/51250
Reviewed by Christos@
--
Add a unit test for PR bin/51250 called se_output. se_output checks
the output of cat when invoked with '-se', to ensure that a '$' is
printed on blank lines.
--
Add input/output files for new test case - fix build
 1.57.16.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.57.16.1 21-Apr-2020  martin Sync with HEAD
 1.6 18-Jul-2003  lukem branches: 1.6.98;
doc2html works here now, so remove NOHTML
 1.5 26-Sep-2000  phil Don't generate html yet. doc2html needs a fix.
 1.4 20-Jul-1997  christos Remove WARNS=1 from all the subdirectory Makefiles, and add it to Makefile.inc
now that all /bin has been cleaned.
 1.3 20-Jul-1997  thorpej Build with WARNS.
 1.2 06-Aug-1996  mrg this program does not need to be setuid root.
 1.1 03-Apr-1996  thorpej branches: 1.1.1;
Initial revision
 1.1.1.1 03-Apr-1996  thorpej branches: 1.1.1.1.4;
Utility to control media changers from the command line; corresponds
to new SCSI changer driver.
 1.1.1.1.4.1 16-Aug-1996  jtc Pulled up from rev 1.2 by request from mrg
 1.6.98.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.6.98.1 21-Apr-2020  martin Sync with HEAD
 1.20 03-Jul-2017  wiz branches: 1.20.6;
Remove workaround for ancient HTML generation code.
 1.19 25-Aug-2016  sevan Move the description of CHANGER variable to ENVIRONMENT section
Bump date.
From OpenBSD src/bin/chio/chio.1 r1.23
 1.18 10-Aug-2016  sevan Document the first version chio appeared in.
Bump date.
 1.17 10-Aug-2016  sevan Mark Jason's name with An macro in AUTHORS section, warned about missing macro
by mandoc lint.
 1.16 17-Mar-2012  wiz Use Lk.
 1.15 30-Apr-2008  martin branches: 1.15.4;
Convert TNF licenses to new 2 clause variant
 1.14 25-Sep-2002  wiz branches: 1.14.32;
New policy: New sentences start on a new line.
Patches by Robert Elz <kre at munnari oz au>, with minimal changes by me.
 1.13 08-Feb-2002  ross Generate <>& symbolically. I'm avoiding .../dist/... directories for now.
 1.12 21-Jan-2002  wiz Use mdoc macros instead of man ones.
 1.11 20-Dec-2001  wiz Sort sections, drop unnecessary .Pps, use standard headers.
 1.10 20-Dec-2001  wiz Whitespace nits.
 1.9 09-Sep-1999  thorpej Update for changes to the changer driver.
 1.8 05-Aug-1999  hubertf Some code cleanup to collect all the usage strings in one place.
Asked for and reviewd by Jason thorpe.
 1.7 22-Mar-1999  garbled More and more .Os cleanups. .Os is defined in the tmac.doc-common file,
so we shouldn't override it with versions in the manpages. Many more to
come.
 1.6 13-Jul-1998  hpeyerl userland interface to ATA CD changer devices by Jason Thorpe.
 1.5 22-May-1998  msaitoh sort entries and delete extra periods in SEE also section.
 1.4 02-Oct-1997  hubertf Fixed some typos pointed out by Dave Huang <khym@bga.com>
Closes PR bin/4201.
 1.3 29-Sep-1997  mjacob Add 'initialize element status' command; note (for future fixing in ch.c)
that a number of jukeboxes can't cope with DBD (disable block descriptors)-
we'll have to fix that some day.
 1.2 15-Sep-1997  lukem cleanup .Nm usage
 1.1 03-Apr-1996  thorpej branches: 1.1.1;
Initial revision
 1.1.1.1 03-Apr-1996  thorpej Utility to control media changers from the command line; corresponds
to new SCSI changer driver.
 1.14.32.1 18-May-2008  yamt sync with head.
 1.15.4.1 17-Apr-2012  yamt sync with head
 1.20.6.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.20.6.1 21-Apr-2020  martin Sync with HEAD
 1.33 16-Oct-2017  jnemeth branches: 1.33.4;
typo: unvalid -> invalid
 1.32 23-Aug-2016  sevan Remove redundant main() prototype which survived a K&R to ANSI transition.
 1.31 29-Aug-2011  joerg Use __dead
 1.30 20-Jul-2008  lukem Remove the \n and tabs from the __COPYRIGHT() strings.
 1.29 28-Apr-2008  martin branches: 1.29.2;
Remove clause 3 and 4 from TNF licenses
 1.28 29-Apr-2007  msaitoh branches: 1.28.10;
fix typos
 1.27 01-Jun-2005  lukem appease gcc -Wuninitialized
 1.26 29-Oct-2004  dsl Add (unsigned char) cast to ctype function
 1.25 25-Jun-2004  wiz Sync usage with man page. Closes PR 25861 by Kouichirou Hiratsuka.
 1.24 05-Jan-2004  jmmv Homogenize usage messages: make the 'usage' word all lowercase, as this seems
to be the most common practice in our tree.
 1.23 04-Jan-2004  jschauma Remove unused code left over from temporary printescaped functionality.
Noted by Jeff Ito in PR bin/23967.
 1.22 14-Sep-2003  jschauma Following private discussion with kleink@ and hubertf@ and public discussion
on tech-userlevel@, back out printescaped() functionality.
kleink: ``We sell rope.''
 1.21 21-Aug-2003  jschauma Fix two more memory leaks found by Hubert Feyrer.
 1.20 04-Aug-2003  jschauma As discusses a little while back on tech-userlevel:

If stdout is a tty, use vis(3) to print any filenames to prevent garbage
from being printed if the filename contains control- or other non-printable
characters.

While here, sprinkle some EXIT_FAILURE and NOTREACHED where appropriate.
 1.19 11-Jun-2002  itojun no need for \n on errx
 1.18 02-May-2002  enami - Don't put space before function call operator.
- Remove trailing whitespace.
- Put an empty line if function doesn't have local variables.
- Use tab instead of spaces.
 1.17 16-Sep-2001  wiz Add setprogname call. Patch from Petri Koistinen.
 1.16 13-Sep-2001  wiz ANSIfication and KNF fixes by Petri Koistinen via bin/13690.
 1.15 19-Feb-2001  cgd convert to use getprogname()
 1.14 09-Sep-1999  thorpej Report vendor-specific data length, if any.
 1.13 09-Sep-1999  thorpej Update for changes to the changer driver.
 1.12 08-Sep-1999  thorpej formatting nits.
 1.11 05-Aug-1999  hubertf Some code cleanup to collect all the usage strings in one place.
Asked for and reviewd by Jason thorpe.
 1.10 28-Jul-1998  mycroft Delint.
 1.9 28-Jul-1998  mycroft Delint.
 1.8 27-Jul-1998  mycroft const propagation.
 1.7 13-Jul-1998  hpeyerl userland interface to ATA CD changer devices by Jason Thorpe.
 1.6 04-Jan-1998  thorpej Make lint(1) happy.
 1.5 29-Sep-1997  mjacob Add 'initialize element status' command; note (for future fixing in ch.c)
that a number of jukeboxes can't cope with DBD (disable block descriptors)-
we'll have to fix that some day.
 1.4 15-Sep-1997  lukem use memset instead of bzero
 1.3 20-Jul-1997  thorpej Make sure printf field widths are "int"s.
 1.2 20-Jul-1997  thorpej - Use __COPYRIGHT() and __RCSID().
- Fix compiler warnings.
 1.1 03-Apr-1996  thorpej branches: 1.1.1;
Initial revision
 1.1.1.1 03-Apr-1996  thorpej Utility to control media changers from the command line; corresponds
to new SCSI changer driver.
 1.28.10.1 18-May-2008  yamt sync with head.
 1.29.2.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.33.4.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.33.4.1 21-Apr-2020  martin Sync with HEAD
 1.6 28-Apr-2008  martin branches: 1.6.64;
Remove clause 3 and 4 from TNF licenses
 1.5 09-Mar-2002  wiz branches: 1.5.34;
Remove a __P().
 1.4 13-Sep-2001  wiz ANSIfication and KNF fixes by Petri Koistinen via bin/13690.
 1.3 09-Sep-1999  thorpej Update for changes to the changer driver.
 1.2 05-Aug-1999  hubertf Some code cleanup to collect all the usage strings in one place.
Asked for and reviewd by Jason thorpe.
 1.1 03-Apr-1996  thorpej branches: 1.1.1;
Initial revision
 1.1.1.1 03-Apr-1996  thorpej Utility to control media changers from the command line; corresponds
to new SCSI changer driver.
 1.5.34.1 18-May-2008  yamt sync with head.
 1.6.64.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.6.64.1 21-Apr-2020  martin Sync with HEAD
 1.4 28-Apr-2008  martin branches: 1.4.64;
Remove clause 3 and 4 from TNF licenses
 1.3 13-Sep-2001  wiz branches: 1.3.34;
ANSIfication and KNF fixes by Petri Koistinen via bin/13690.
 1.2 09-Sep-1999  thorpej Update for changes to the changer driver.
 1.1 03-Apr-1996  thorpej branches: 1.1.1;
Initial revision
 1.1.1.1 03-Apr-1996  thorpej Utility to control media changers from the command line; corresponds
to new SCSI changer driver.
 1.3.34.1 18-May-2008  yamt sync with head.
 1.4.64.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.4.64.1 21-Apr-2020  martin Sync with HEAD
 1.8 20-Jul-1997  christos branches: 1.8.112;
Remove WARNS=1 from all the subdirectory Makefiles, and add it to Makefile.inc
now that all /bin has been cleaned.
 1.7 20-Jul-1997  thorpej Build with WARNS.
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 20-Sep-1994  mycroft Merge with 4.4-Lite.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.8.112.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.8.112.1 21-Apr-2020  martin Sync with HEAD
 1.29 05-May-2023  kre If chown and chgrp can grow -d flags to suppress performing the
operation when it will have no effect (other than changing the
inode's ctime value) then chmod and chflags should also have -d
flags for the same purpose. Make it so.
 1.28 04-Jul-2017  wiz branches: 1.28.6;
Use Ex.
 1.27 03-Jul-2017  wiz Remove workaround for ancient HTML generation code.
 1.26 11-Aug-2016  sevan Document the version chmod first appeared.
Bump date.
 1.25 17-Dec-2013  apb In all man pages that say:

The -H, -L and -P options are ignored unless the -R option is
specified. In addition, these options override each other and the
command's actions are determined by the last one specified.

Add:

The default is as if the -P option had been specified.
 1.24 22-Oct-2012  christos add --reference=rfile
 1.23 22-Jan-2010  snj branches: 1.23.6; 1.23.12;
Document "-f". From Andreas Wiese in PR bin/35409, slightly modified.
 1.22 24-Mar-2009  joerg Remove physical markup.
 1.21 09-Dec-2005  wiz branches: 1.21.28;
Add xref to chflags(1). From Andre Luiz de Oliveira in PR 32265.
 1.20 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.19 20-Jan-2003  enami Start new paragraph after options are listed.
 1.18 07-Jul-2002  bjh21 Align default behaviour of chmod, chgrp, chown, and chflags with symlink(7),
IEEE 1003.1-2001 (where applicable) and other systems, by follwoing symlinks
on the command line and changing their targets' modes/ownership/flags, rather
than ignoring them.

This fixes PR standards/563 (at last).
 1.17 08-Feb-2002  ross Generate <>& symbolically. I'm avoiding .../dist/... directories for now.
 1.16 20-Dec-2001  wiz Sort sections, drop unnecessary .Pps.
 1.15 14-Oct-2000  bjh21 The "X" permission _is_ specified by POSIX.2.
 1.14 04-Sep-2000  kleink For commands and utilities, use EXIT STATUS rather than RETURN VALUES as
appropriate (and documented in mdoc(7)).
 1.13 28-Aug-2000  hubertf Add 'RETURN VALUE' section header.
 1.12 05-Feb-2000  enami branches: 1.12.4;
Sync wi/ what mdoc.samples(7) says:
- The default argument of Ar macro is file ...
- Use or Ap ing.
 1.11 20-Oct-1997  enami Fix .Nm usage.
 1.10 06-Oct-1997  enami Document -h flag, option to manupilate symbolic link.
 1.9 23-Jun-1997  pk Use .Xr (from PR#3627)
 1.8 21-Mar-1995  cgd convert to new RCS id conventions.
 1.7 20-Sep-1994  mycroft Merge with 4.4-Lite.
 1.6 10-Feb-1994  cgd add some info about u, g, o perm specs
 1.5 11-Jan-1994  jtc Fix spelling errors.
 1.4 01-Aug-1993  mycroft Add RCS indentifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.12.4.1 28-Aug-2000  hubertf Pull up to netbsd-1-5 branch, OK'd by thorpej:

Log Message:
> Add 'RETURN VALUE' section header.

Files & Revisionis:
> cvs rdiff -r1.19 -r1.20 basesrc/bin/cat/cat.1
> cvs rdiff -r1.12 -r1.13 basesrc/bin/chmod/chmod.1
> cvs rdiff -r1.14 -r1.15 basesrc/bin/cp/cp.1
> cvs rdiff -r1.8 -r1.9 basesrc/bin/dd/dd.1
> cvs rdiff -r1.9 -r1.10 basesrc/bin/echo/echo.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/expr/expr.1
> cvs rdiff -r1.25 -r1.26 basesrc/bin/ls/ls.1
> cvs rdiff -r1.10 -r1.11 basesrc/bin/mkdir/mkdir.1
> cvs rdiff -r1.23 -r1.24 basesrc/bin/mt/mt.1
> cvs rdiff -r1.12 -r1.13 basesrc/bin/mv/mv.1
> cvs rdiff -r1.16 -r1.17 basesrc/bin/pwd/pwd.1
> cvs rdiff -r1.9 -r1.10 basesrc/bin/rm/rm.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/rmdir/rmdir.1
> cvs rdiff -r1.35 -r1.36 basesrc/bin/sh/sh.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/sleep/sleep.1
> cvs rdiff -r1.20 -r1.21 basesrc/bin/stty/stty.1
 1.21.28.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.23.12.2 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.23.12.1 20-Nov-2012  tls Resync to 2012-11-19 00:00:00 UTC
 1.23.6.2 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.23.6.1 30-Oct-2012  yamt sync with head
 1.28.6.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.28.6.1 21-Apr-2020  martin Sync with HEAD
 1.39 05-May-2023  kre If chown and chgrp can grow -d flags to suppress performing the
operation when it will have no effect (other than changing the
inode's ctime value) then chmod and chflags should also have -d
flags for the same purpose. Make it so.
 1.38 22-Oct-2012  christos branches: 1.38.32;
fix argument parsing.
 1.37 22-Oct-2012  christos add --reference=rfile
 1.36 29-Aug-2011  joerg branches: 1.36.2; 1.36.8;
Use __dead
 1.35 22-Jan-2010  snj Document "-f". From Andreas Wiese in PR bin/35409, slightly modified.
 1.34 20-Jul-2008  lukem Remove the \n and tabs from the __COPYRIGHT() strings.
 1.33 01-Oct-2005  christos branches: 1.33.20;
Now that setmode() is fixed, handle errors with err instead errx.
 1.32 04-Jan-2004  jschauma Remove unused code left over from temporary printescaped functionality.
Noted by Jeff Ito in PR bin/23967.
 1.31 14-Sep-2003  jschauma Following private discussion with kleink@ and hubertf@ and public discussion
on tech-userlevel@, back out printescaped() functionality.
kleink: ``We sell rope.''
 1.30 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.29 04-Aug-2003  jschauma As discusses a little while back on tech-userlevel:

If stdout is a tty, use vis(3) to print any filenames to prevent garbage
from being printed if the filename contains control- or other non-printable
characters.

While here, sprinkle some EXIT_FAILURE and NOTREACHED where appropriate.
 1.28 07-Jul-2002  bjh21 Align default behaviour of chmod, chgrp, chown, and chflags with symlink(7),
IEEE 1003.1-2001 (where applicable) and other systems, by follwoing symlinks
on the command line and changing their targets' modes/ownership/flags, rather
than ignoring them.

This fixes PR standards/563 (at last).
 1.27 31-May-2002  bjh21 Fix a comment: POSIX doesn't specify -h.
 1.26 16-Sep-2001  wiz ANSIfy, use {s,g}etprogname and other KNF. Patch by Petri Koistinen.
 1.25 10-Oct-2000  enami Printing argv[0] on fts_open failure doesn't make sense. Just print
``fts_open'' instead.
 1.24 06-Sep-2000  enami Remove a hack to prevent unwanted gcc -Wuninitialized warning; it's now
unnecessary since the code path is simple enough after rev. 1.21.
 1.23 07-Jul-2000  itojun do not pass user-supplied string alone, to errx?(). use "%s".
from openbsd.
 1.22 20-Jan-2000  mycroft branches: 1.22.4;
Oops; remove unused variables.
 1.21 20-Jan-2000  mycroft Just use setmode(3); don't special-case numeric values.
 1.20 28-Jul-1998  mycroft Delint.
 1.19 28-Jul-1998  mycroft Delint.
 1.18 28-Jul-1998  mycroft Delint.
 1.17 11-Oct-1997  enami Add -h to usage.
 1.16 06-Oct-1997  enami Change noop option -h to do the real work. Now mode of symbolic link
is changed if -h option is given.
 1.15 06-Oct-1997  enami Don't pass the pathname itself as format string of warn(). Instead,
use "%s".
 1.14 20-Jul-1997  thorpej - Use __COPYRIGHT() and __RCSID().
- Fix compiler warnings.
 1.13 26-Jun-1997  kleink POSIX.2 utilities must call setlocale(LC_ALL, "");
 1.12 21-Mar-1995  cgd convert to new RCS id conventions.
 1.11 15-Jan-1995  mycroft Change directory permissions in pre-order.
 1.10 20-Sep-1994  mycroft Need limits.h.
 1.9 20-Sep-1994  mycroft Merge with 4.4-Lite.
 1.8 11-Feb-1994  cgd don't barf over strange mode specs
 1.7 06-Aug-1993  mycroft Incorporate new version from uunet, with local changes.
 1.6 05-Aug-1993  deraadt change for new fts library
 1.5 01-Aug-1993  mycroft Add RCS identifiers.
 1.4 07-Jun-1993  deraadt chmod.0 says:
Symbolic links are not indirected through, nor are their modes altered.
things were much worse. it would try to chmod() symbolic links, in some cases
applying the modes of the symbolic link to the file that it pointed to.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.22.4.1 27-Jul-2000  itojun pullup (approved by releng-1-5)
printf-like format pedant. do not pass string variable alone. use "%s".
from openbsd.

/cvsroot/basesrc/bin/dd/conv.c 1.8 -> 1.9
/cvsroot/basesrc/bin/cp/cp.c 1.29 -> 1.30
/cvsroot/basesrc/bin/chmod/chmod.c 1.22 -> 1.23
 1.33.20.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.36.8.1 20-Nov-2012  tls Resync to 2012-11-19 00:00:00 UTC
 1.36.2.1 30-Oct-2012  yamt sync with head
 1.38.32.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.38.32.1 21-Apr-2020  martin Sync with HEAD
 1.10 17-May-2020  christos No ACL support for install media (SMALLPROG)
 1.9 20-Jul-1997  christos branches: 1.9.112;
Remove WARNS=1 from all the subdirectory Makefiles, and add it to Makefile.inc
now that all /bin has been cleaned.
 1.8 20-Jul-1997  thorpej Build with WARNS.
 1.7 21-Mar-1995  cgd convert to new RCS id conventions.
 1.6 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.5 07-Aug-1993  mycroft New version from uunet. Uses mmap().
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.9.112.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.9.112.1 21-Apr-2020  martin Sync with HEAD
 1.46 23-Dec-2018  gutteridge Clarify a sentence about the error handling of cp(1)'s -p option.
Addresses PR bin/40336.
 1.45 11-Aug-2016  sevan branches: 1.45.12; 1.45.14;
Document the version cp first appeared.
Bump date.
 1.44 28-Jun-2015  wiz Add serial comma, use upper case for abbreviation, and remove apostrophe for plural.
 1.43 17-Dec-2013  apb In all man pages that say:

The -H, -L and -P options are ignored unless the -R option is
specified. In addition, these options override each other and the
command's actions are determined by the last one specified.

Add:

The default is as if the -P option had been specified.
 1.42 25-Mar-2012  wiz branches: 1.42.2;
From patch by Bug Hunting:
- bump the manpage's date (because of the next change);
- note that cp(1) does not preserve hard links (even with `-R'),
and refer to pax(1) there as well as in the `SEE ALSO' section
for such functionality (this change is based upon a similar note
in FreeBSD's version of the manpage);
- change a wording, for more overall consistency.
 1.41 03-Aug-2011  dholland branches: 1.41.2;
Adjust wording of previous and fix a couple grammar slipups/typos.
 1.40 03-Aug-2011  wiz New sentence, new line. Remove trailing whitespace. Bump date for previous.
 1.39 03-Aug-2011  manu iExplain what happens to extended attriutes when -p is used.
 1.38 02-Apr-2011  mbalmer Wording fixes from Ryo HAYASAKA, thanks.
 1.37 06-Feb-2011  wiz Put -l compatibility note in STANDARDS section.
 1.36 06-Feb-2011  darcy Add -l option to copy a tree as links.
Non-standard option similar to Gnutools cp(1)
Approved by core.
 1.35 24-Jan-2011  wiz Use Fx.
 1.34 24-Jan-2011  yamt document what -a is for
 1.33 22-Dec-2010  wiz branches: 1.33.2;
Sort option descriptions, bump date for -a.
 1.32 22-Dec-2010  christos Say that -a is non-standard and -P is the default.
 1.31 21-Dec-2010  christos Add -a archive flag. from Aleksey Cheusov
 1.30 26-Oct-2007  hira Sync SYNOPSIS with usage() (correct location of -P).
 1.29 16-Jul-2006  jschauma branches: 1.29.10;
return functionality backed out yesterday after fixing the bug I had
introduced before. A regression test will be added to src/regress
shortly.
 1.28 15-Jul-2006  jschauma The last commit introduced a bug. Revert until that's found and fixed.
 1.27 15-Jul-2006  jschauma As suggested on tech-userlevel:
"-R" claims:
-R [...] Created directories have the same mode as the corre-
sponding source directory, unmodified
by the process' umask.

Make this actually true.

In addition, make '-P' (no symbolic links are followed) apply even if
'-R' is not specified. This allows users to overwrite symbolic links
with files and/or to copy symbolic links over a file without indirecting
through the link (ie a copy of a link turns the target into a link, not
a copy of the file pointed to by the source).
 1.26 10-Sep-2005  wiz Sort options. Simplify macro usage. Use standard EXIT STATUS section.
 1.25 15-Aug-2005  elad Add and document -N: When used with -p, it will not copy the file flags.
PR 13645.
 1.24 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.23 21-Dec-2002  wiz For each, not Foreach; from Adrian Mrva. Begin new sentence on new line.
 1.22 18-Dec-2002  wiz Drop trailing whitespace.
 1.21 18-Dec-2002  kleink Add -v to SYNOPSIS, clean up STANDARDS a little. Also, bump date for -v.
 1.20 16-Dec-2002  jrf Added a -v verbose option to the cp(1) command. Updated man page and utils as well. This applies to PR bin/16834. This change was approved by Christos.
 1.19 08-Feb-2002  ross Generate <>& symbolically. I'm avoiding .../dist/... directories for now.
 1.18 20-Dec-2001  wiz Drop unnecessary .Pps.
 1.17 20-Dec-2001  wiz Whitespace nits.
 1.16 04-Sep-2000  kleink For commands and utilities, use EXIT STATUS rather than RETURN VALUES as
appropriate (and documented in mdoc(7)).
 1.15 28-Aug-2000  hubertf Add 'RETURN VALUE' section header.
 1.14 22-Mar-1999  garbled branches: 1.14.10;
More and more .Os cleanups. .Os is defined in the tmac.doc-common file,
so we shouldn't override it with versions in the manpages. Many more to
come.
 1.13 08-Oct-1998  wsanchez Fix bug where "cp -f" didn't work.
It's suppsed to remove the file and then copy, which it wasn't doing.
But no wait, it turns out that the described behaviour in the manual doesn't
agree with POSIX. So we change the above fix and the manual to "try copy, and
if fail, try remove, then copy".
Fix bug where "cp -R" didn't work on read-only directories:
It would make the directory, set the mode, and not be able to write files into it.
Don't bother mmap()ing files of zero length. Was a workaround for a bug in Rhapsody
mmap(), which didn't get along with such files, but makes sense anyway.
Fix race condition where "cp -p" would set the mod time of a file before close()ing
the file, which would update the mod time and therefore screw up the "-p" idea,
except, of course, while running in gdb, which sucked.
Add -f option to usage message in binary and man page. Already documented in man page.
 1.12 11-Oct-1997  enami Use .Nm "" instead of .Nm cp in SYNOPSIS.
 1.11 10-Oct-1997  lukem all but the first .Nm in SYNOPSIS need to be spelt out. [bin/4256]
 1.10 14-Sep-1997  lukem * getopt returns -1 not EOF
* cleanup use of .Nm in manpage
 1.9 25-Jul-1995  jtc Use "utility" instead of "command". Modern definitions of these terms
are distinct (See POSIX.2 glossary).

A utility is a executable, script or shell builtin; while a command
can be any of those things plus lists, pipelines, compound commands
(if, for, while) and shell function definitions.
 1.8 25-Jul-1995  jtc s/HISTORY/STANDARDS/; noted by Masanobu Saitoh in PR#1269
 1.7 21-Mar-1995  cgd convert to new RCS id conventions.
 1.6 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.5 07-Aug-1993  mycroft New version from uunet. Uses mmap().
 1.4 01-Aug-1993  mycroft Add RCS indentifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.14.10.1 28-Aug-2000  hubertf Pull up to netbsd-1-5 branch, OK'd by thorpej:

Log Message:
> Add 'RETURN VALUE' section header.

Files & Revisionis:
> cvs rdiff -r1.19 -r1.20 basesrc/bin/cat/cat.1
> cvs rdiff -r1.12 -r1.13 basesrc/bin/chmod/chmod.1
> cvs rdiff -r1.14 -r1.15 basesrc/bin/cp/cp.1
> cvs rdiff -r1.8 -r1.9 basesrc/bin/dd/dd.1
> cvs rdiff -r1.9 -r1.10 basesrc/bin/echo/echo.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/expr/expr.1
> cvs rdiff -r1.25 -r1.26 basesrc/bin/ls/ls.1
> cvs rdiff -r1.10 -r1.11 basesrc/bin/mkdir/mkdir.1
> cvs rdiff -r1.23 -r1.24 basesrc/bin/mt/mt.1
> cvs rdiff -r1.12 -r1.13 basesrc/bin/mv/mv.1
> cvs rdiff -r1.16 -r1.17 basesrc/bin/pwd/pwd.1
> cvs rdiff -r1.9 -r1.10 basesrc/bin/rm/rm.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/rmdir/rmdir.1
> cvs rdiff -r1.35 -r1.36 basesrc/bin/sh/sh.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/sleep/sleep.1
> cvs rdiff -r1.20 -r1.21 basesrc/bin/stty/stty.1
 1.29.10.1 06-Nov-2007  matt sync with HEAD
 1.33.2.1 08-Feb-2011  bouyer Sync with HEAD
 1.41.2.2 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.41.2.1 17-Apr-2012  yamt sync with head
 1.42.2.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.45.14.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.45.14.2 21-Apr-2020  martin Sync with HEAD
 1.45.14.1 10-Jun-2019  christos Sync with HEAD
 1.45.12.1 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.63 07-Jun-2024  andvar s/concatentation/concatenation/ in comment.
 1.62 22-May-2020  christos fix reversed test.
 1.61 17-May-2020  christos No ACL support for install media (SMALLPROG)
 1.60 16-May-2020  christos Add ACL support for FFS. From FreeBSD.
 1.59 05-Mar-2016  uwe branches: 1.59.16;
The '-i' flag should work regardless of whether the standard input is
a terminal. The Open Group notes this historic behavior and correctly
notes that it doesn't make much sense. Note also, that mv(1) has
always respected its '-i' regardless of whether the standard input is
a terminal.

From Timo Buhrmester.
 1.58 04-Jan-2012  christos branches: 1.58.2; 1.58.20;
Add siginfo support from Daniel Loffgren.
 1.57 18-Aug-2011  manu branches: 1.57.2;
Remove warning about system extended attributes that cannot be copied, since
it will always be displayed when an unprivilegied user moves files across
filesystems (mv(1) uses cp -p in that case). After all, there is no warning
that we loose a setuid bit during a move or copy, so this makes sense.

Fixes bin/45259
 1.56 03-Aug-2011  manu Make cp -p and mv preverve extended attributes, and complain if they cannot.

Also introduce library functions for copying extended attributes from one
file to another:
- extattr_copy_file, extattr_copy_fd, extattr_copy_link, with FreeBSD style,
where a namespace is to be supplied
- cpxattr, fcpxattr, lcpxattr, with Linux style, where all namespaces
accessible to the caller are copied, and the others are silently ignored.
 1.55 06-Feb-2011  darcy Add -l option to copy a tree as links.
Non-standard option similar to Gnutools cp(1)
Approved by core.
 1.54 21-Dec-2010  christos branches: 1.54.2;
Add -a archive flag. from Aleksey Cheusov
 1.53 08-Oct-2009  pooka Fix the "dne" handling and chmod behaviour properly: values of dne
need to be on a stack instead of being a single variable since
directories are processed depth-first. Otherwise dne randomly
depends on the previously processed entry.

This fixes both chmod of non-created directories (they used to be
chmod'd even when not created if their last child element did not
exist in the target subtree) and a "foo exists" bug exposed by my
last commit which removed directory sorting.

all regression tests passed
 1.52 29-Sep-2009  pooka Remove fts sorting. It was originally put there to copy files
before directories since files (usually) are in the same cylinder
group and subdirectories aren't. However, this mostly changed with
the new ffs dirpref algorithm in 2001.

No sorting has two effects:
1) copy appears to be somewhat faster (e.g. on my laptop cp'ing build
objdir to tmpfs is 7% faster after the change)
2) source file parameters no longer get randomly shuffled due to
fts doing an unstable sort of them. this means that
"cp 1 2 3 4 dest/" will copy the files in that order instead
of e.g. 3 4 1 2.
 1.51 20-Jul-2008  lukem Remove the \n and tabs from the __COPYRIGHT() strings.
 1.50 26-May-2008  dholland Distinguish "this file failed" from "any file failed", to make cp -v
output behave itself. PR bin/37018 from Dieter Roelants.

(I used a slightly different patch to make sure "any file failed"
didn't get lost in the shuffle, and renamed the variables in the name
of greater clarity.)
 1.49 26-May-2008  christos PR/38755: murray armfield: /bin posix programs missing setprogname and/or
setlocale
 1.48 26-Dec-2006  alc branches: 1.48.10; 1.48.12;
fix whitespace nits (space -> tab)
 1.47 15-Oct-2006  christos c99 initializers
 1.46 15-Aug-2006  jschauma initialize dne variable in the beginning, then move the resetting of
this variable down into the second (ie post-order) pass to make cp
pass all regression tests again.
 1.45 15-Aug-2006  tron Avoid errors like "cp: /foo/bar/directory" exists if cp(1) is used to
copy multiple directories and the first one doesn't exist in the
target directiry but another one (e.g. the second) does.
 1.44 16-Jul-2006  jschauma branches: 1.44.2;
return functionality backed out yesterday after fixing the bug I had
introduced before. A regression test will be added to src/regress
shortly.
 1.43 15-Jul-2006  jschauma The last commit introduced a bug. Revert until that's found and fixed.
 1.42 15-Jul-2006  jschauma As suggested on tech-userlevel:
"-R" claims:
-R [...] Created directories have the same mode as the corre-
sponding source directory, unmodified
by the process' umask.

Make this actually true.

In addition, make '-P' (no symbolic links are followed) apply even if
'-R' is not specified. This allows users to overwrite symbolic links
with files and/or to copy symbolic links over a file without indirecting
through the link (ie a copy of a link turns the target into a link, not
a copy of the file pointed to by the source).
 1.41 17-Mar-2006  erh Fix Coverity issue 1754. Call fts_close() after we're done copying.
 1.40 16-Nov-2005  christos - Better detect pathname overflow (from FreeBSD)
- Change destination normal file detection to match with FreeBSD
- Trim trailing slashes from source filenames so that cp foo/ == cp foo
 1.39 24-Oct-2005  kleink Remove the "" -> "." target conversion kludge.
 1.38 15-Oct-2005  christos - Don't output 'src -> dst' when -v and there was an error.
- Mark usage as noreturn, and remove extraneous exit.
- Pass lint.
Reported by Liam Foy.
 1.37 15-Aug-2005  elad Add and document -N: When used with -p, it will not copy the file flags.
PR 13645.
 1.36 26-Jun-2005  christos sprinkle a little const, and now everything compiles with WARNS=3
 1.35 14-Sep-2003  jschauma Following private discussion with kleink@ and hubertf@ and public discussion
on tech-userlevel@, back out printescaped() functionality.
kleink: ``We sell rope.''
 1.34 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.33 04-Aug-2003  jschauma As discusses a little while back on tech-userlevel:

If stdout is a tty, use vis(3) to print any filenames to prevent garbage
from being printed if the filename contains control- or other non-printable
characters.

While here, sprinkle some EXIT_FAILURE and NOTREACHED where appropriate.
 1.32 16-Dec-2002  jrf Added a -v verbose option to the cp(1) command. Updated man page and utils as well. This applies to PR bin/16834. This change was approved by Christos.
 1.31 13-Sep-2001  wiz ANSIfication and some KNF fixes by Petri Koistinen from bin/13681.
 1.30 07-Jul-2000  itojun errx?/warnx? audit. do not pass variable alone, use %s. idea from openbsd
 1.29 03-Jul-2000  matt include <stdlib.h>, <string.h>, or whatever as appropriate to shut up
gcc 2.96
 1.28 05-Sep-1999  kleink branches: 1.28.8;
Initialize locale on startup.
 1.27 01-Mar-1999  mjl Correct behaviour in case of dangling symlinks: they would be (contrary
to what the manpage and POSIX say) copied as symlinks, even without -R.
Return ENOENT instead. Closes PR 6975 by Johan Danielsson <joda@pdc.kth.se>.

From FreeBSD: return correct error message if source directory is
unreadable; remove unnecessarily included headers.
 1.26 25-Feb-1999  dean Got rid of redundant error message (X is a directory (not copied). was
printed twice)
 1.25 08-Oct-1998  wsanchez Fix bug where "cp -f" didn't work.
It's suppsed to remove the file and then copy, which it wasn't doing.
But no wait, it turns out that the described behaviour in the manual doesn't
agree with POSIX. So we change the above fix and the manual to "try copy, and
if fail, try remove, then copy".
Fix bug where "cp -R" didn't work on read-only directories:
It would make the directory, set the mode, and not be able to write files into it.
Don't bother mmap()ing files of zero length. Was a workaround for a bug in Rhapsody
mmap(), which didn't get along with such files, but makes sense anyway.
Fix race condition where "cp -p" would set the mod time of a file before close()ing
the file, which would update the mod time and therefore screw up the "-p" idea,
except, of course, while running in gdb, which sucked.
Add -f option to usage message in binary and man page. Already documented in man page.
 1.24 19-Aug-1998  thorpej Add some braces to keep egcs happy.
 1.23 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.22 28-Jul-1998  mycroft Delint.
 1.21 28-Jul-1998  mycroft Delint.
 1.20 28-Jul-1998  mycroft Delint.
 1.19 14-Sep-1997  lukem * getopt returns -1 not EOF
* cleanup use of .Nm in manpage
 1.18 20-Jul-1997  thorpej - Use __COPYRIGHT() and __RCSID()
- Fix compiler warnings.
 1.17 21-May-1997  kleink Fix one remaining botch when copying directory trees using -R.
 1.16 19-May-1997  kleink Fix thinko in last commit, causing "cp file /" to succeed but
"cp file1 file2 /" to fail badly; fixes PR/3646 from Paul Kranenburg.
 1.15 09-Apr-1997  kleink Fix lossage with new namei() when the destination given is "/";
reported by Bernd Ernesti.
 1.14 07-Sep-1995  jtc Sync with 4.4lite2
 1.13 02-Apr-1995  mycroft Fix typo in S_FIFO case when copying recursively.
 1.12 21-Mar-1995  cgd convert to new RCS id conventions.
 1.11 26-Jan-1995  mycroft Fix core dump reported by Wolfram Schneider.
 1.10 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.9 28-Mar-1994  cgd no more MAP_FILE
 1.8 25-Feb-1994  jtc POSIX.2 (and common sense) says that you can't overwrite a directory with
a non-directory.
 1.7 09-Nov-1993  jtc Check user's response against both 'Y' and 'y' --- this is really supposed
to be a locale specific regular expression. This change hard codes POSIX
locale behavior, and will be replaced by a locale independant equivalent
as soon as locales are fully implemented.
 1.6 30-Oct-1993  mycroft From Lennart Augustsson:
If we mmap() the file, make sure we munmap() when we're done.
 1.5 07-Aug-1993  mycroft New version from uunet. Uses mmap().
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.3 07-Sep-1995  jtc imported from 44lite2
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.28.8.1 27-Jul-2000  itojun pullup (approved by releng-1-5)
printf-like format pedant. do not pass string variable alone. use "%s".
from openbsd.

/cvsroot/basesrc/bin/dd/conv.c 1.8 -> 1.9
/cvsroot/basesrc/bin/cp/cp.c 1.29 -> 1.30
/cvsroot/basesrc/bin/chmod/chmod.c 1.22 -> 1.23
 1.44.2.1 21-Aug-2006  ghen Pull up following revision(s) (requested by tron in ticket #30):
regress/bin/cp/cp.sh: revision 1.2
regress/bin/cp/cp.sh: revision 1.3
bin/cp/cp.c: revision 1.45
bin/cp/cp.c: revision 1.46
Avoid errors like "cp: /foo/bar/directory" exists if cp(1) is used to
copy multiple directories and the first one doesn't exist in the
target directiry but another one (e.g. the second) does.
initialize dne variable in the beginning, then move the resetting of
this variable down into the second (ie post-order) pass to make cp
pass all regression tests again.
allow each dir_to_dir test to be run individually without having to
rely on previous tests that set up the files and directories;
while here, clarify in each error message where exactly the error occurred
add a regression test for copying several subdirs into a target directory
where some subdirs already exist
 1.48.12.2 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.48.12.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.48.10.1 04-Jun-2008  yamt sync with head
 1.54.2.1 08-Feb-2011  bouyer Sync with HEAD
 1.57.2.1 17-Apr-2012  yamt sync with head
 1.58.20.1 06-Mar-2016  martin Pull up following revision(s) (requested by uwe in ticket #1131):
bin/cp/cp.c: revision 1.59
The '-i' flag should work regardless of whether the standard input is
a terminal. The Open Group notes this historic behavior and correctly
notes that it doesn't make much sense. Note also, that mv(1) has
always respected its '-i' regardless of whether the standard input is
a terminal.
From Timo Buhrmester.
 1.58.2.1 27-Aug-2016  bouyer Pull up following revision(s) (requested by uwe in ticket #1372):
bin/cp/cp.c: revision 1.59
The '-i' flag should work regardless of whether the standard input is
a terminal. The Open Group notes this historic behavior and correctly
notes that it doesn't make much sense. Note also, that mv(1) has
always respected its '-i' regardless of whether the standard input is
a terminal.
From Timo Buhrmester.
 1.59.16.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.59.16.1 21-Apr-2020  martin Sync with HEAD
 1.5 07-Aug-1993  mycroft Clean up deleted files.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.18 16-May-2020  christos Add ACL support for FFS. From FreeBSD.
 1.17 04-Jan-2012  christos branches: 1.17.42;
Add siginfo support from Daniel Loffgren.
 1.16 06-Feb-2011  darcy branches: 1.16.4;
Add -l option to copy a tree as links.
Non-standard option similar to Gnutools cp(1)
Approved by core.
 1.15 16-Jul-2006  jschauma branches: 1.15.38;
return functionality backed out yesterday after fixing the bug I had
introduced before. A regression test will be added to src/regress
shortly.
 1.14 15-Jul-2006  jschauma The last commit introduced a bug. Revert until that's found and fixed.
 1.13 15-Jul-2006  jschauma As suggested on tech-userlevel:
"-R" claims:
-R [...] Created directories have the same mode as the corre-
sponding source directory, unmodified
by the process' umask.

Make this actually true.

In addition, make '-P' (no symbolic links are followed) apply even if
'-R' is not specified. This allows users to overwrite symbolic links
with files and/or to copy symbolic links over a file without indirecting
through the link (ie a copy of a link turns the target into a link, not
a copy of the file pointed to by the source).
 1.12 15-Oct-2005  christos - Don't output 'src -> dst' when -v and there was an error.
- Mark usage as noreturn, and remove extraneous exit.
- Pass lint.
Reported by Liam Foy.
 1.11 15-Aug-2005  elad Add and document -N: When used with -p, it will not copy the file flags.
PR 13645.
 1.10 26-Jun-2005  christos sprinkle a little const, and now everything compiles with WARNS=3
 1.9 14-Sep-2003  jschauma Following private discussion with kleink@ and hubertf@ and public discussion
on tech-userlevel@, back out printescaped() functionality.
kleink: ``We sell rope.''
 1.8 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.7 04-Aug-2003  jschauma As discusses a little while back on tech-userlevel:

If stdout is a tty, use vis(3) to print any filenames to prevent garbage
from being printed if the filename contains control- or other non-printable
characters.

While here, sprinkle some EXIT_FAILURE and NOTREACHED where appropriate.
 1.6 13-Sep-2001  wiz ANSIfication and some KNF fixes by Petri Koistinen from bin/13681.
 1.5 08-Oct-1998  wsanchez Fix bug where "cp -f" didn't work.
It's suppsed to remove the file and then copy, which it wasn't doing.
But no wait, it turns out that the described behaviour in the manual doesn't
agree with POSIX. So we change the above fix and the manual to "try copy, and
if fail, try remove, then copy".
Fix bug where "cp -R" didn't work on read-only directories:
It would make the directory, set the mode, and not be able to write files into it.
Don't bother mmap()ing files of zero length. Was a workaround for a bug in Rhapsody
mmap(), which didn't get along with such files, but makes sense anyway.
Fix race condition where "cp -p" would set the mod time of a file before close()ing
the file, which would update the mod time and therefore screw up the "-p" idea,
except, of course, while running in gdb, which sucked.
Add -f option to usage message in binary and man page. Already documented in man page.
 1.4 28-Jul-1998  mycroft Delint.
 1.3 21-Mar-1995  cgd convert to new RCS id conventions.
 1.2 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.1 07-Aug-1993  mycroft branches: 1.1.1;
New version from uunet. Uses mmap().
 1.1.1.1 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.15.38.1 08-Feb-2011  bouyer Sync with HEAD
 1.16.4.1 17-Apr-2012  yamt sync with head
 1.17.42.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.17.42.1 21-Apr-2020  martin Sync with HEAD
 1.6 22-Sep-1994  mycroft Clean up deleted files.
 1.5 07-Aug-1993  mycroft New version from uunet. Uses mmap().
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.50 15-Jan-2024  christos PR/57857: Ricardo Branco: Always copy regular files, even if they appear to
be zero-sized.
 1.49 17-May-2020  christos branches: 1.49.6;
No ACL support for install media (SMALLPROG)
 1.48 16-May-2020  christos Add ACL support for FFS. From FreeBSD.
 1.47 23-Sep-2019  christos PR/54564: Jan Schaumann: cp of a fifo yields an empty file
Don't short-circuit 0 sized stat entries if they don't belong to regular files.
Also don't try to mmap non-regular files.
 1.46 17-Jul-2018  darcy branches: 1.46.2;
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.
 1.45 29-Feb-2016  mrg branches: 1.45.14; 1.45.16;
for display in the (post) SIGINFO handler use off_t not size_t for
file sizes. fixes incorrect reporting errors on 32 bit platforms
with >4GB file sizes.
 1.44 03-Mar-2015  enami Fix the name of failed function in warning message.
 1.43 02-Mar-2015  enami Don't truncate at microseconds while preserving timestamps.

One of motivation of this change is to make the behavior of test(1)
-nt/ot with preserved copy (like cp -p) closer to the NetBSD 6.
Of course whether full timestamps are kept or not depends also on
underlying file system.

The ifdef added in mv(1) since existing ifdefs was our local change
to compile it on solaris (though I couldn't test it):
http://mail-index.netbsd.org/tech-userlevel/2014/11/28/msg008831.html
 1.42 11-Dec-2013  dholland branches: 1.42.4; 1.42.6;
Remove entirely bogus /* NOTREACHED */ annotation.
 1.41 04-Jan-2012  christos branches: 1.41.6;
Add siginfo support from Daniel Loffgren.
 1.40 03-Aug-2011  manu branches: 1.40.2;
Make cp -p and mv preverve extended attributes, and complain if they cannot.

Also introduce library functions for copying extended attributes from one
file to another:
- extattr_copy_file, extattr_copy_fd, extattr_copy_link, with FreeBSD style,
where a namespace is to be supplied
- cpxattr, fcpxattr, lcpxattr, with Linux style, where all namespaces
accessible to the caller are copied, and the others are silently ignored.
 1.39 06-Feb-2011  darcy Add -l option to copy a tree as links.
Non-standard option similar to Gnutools cp(1)
Approved by core.
 1.38 04-Jan-2011  wiz branches: 1.38.2;
Fix fd leak in error case. Found by cppcheck.
 1.37 21-Dec-2010  christos Add -a archive flag. from Aleksey Cheusov
 1.36 25-Oct-2010  tron Re-enable support for reading files of at most 8MB via mmap(2).
Write out the mmap-ed data in small chunks to avoid locking the output
file for a long time.

Suggested by David Holland on "source-changes-d" mailing list.
 1.35 22-Oct-2010  pooka Disable mmap path. With the current vnode locking scheme it has
a very annoying property: if the source media is slow (like a slow
network), the target file will be locked for the duration of the
entire max 8MB write and cause processes attempting to e.g. stat()
it to "tstile" (for several minutes in the worst case). Revisit
this if/when vnode locking gets a little smarter.

chs ok (although he would rather see vnode locking improvements,
but that's a slightly larger project)
 1.34 26-Oct-2007  hira Add -N to usage.
 1.33 25-Oct-2007  jld Touching up the permissions to preserve set-ID bits is appropriate
only in the case where the destination did not previously exist, and
necessary only when -p isn't given.

Closes PR bin/30188.
 1.32 16-Jul-2006  jschauma branches: 1.32.10;
return functionality backed out yesterday after fixing the bug I had
introduced before. A regression test will be added to src/regress
shortly.
 1.31 15-Jul-2006  jschauma The last commit introduced a bug. Revert until that's found and fixed.
 1.30 15-Jul-2006  jschauma As suggested on tech-userlevel:
"-R" claims:
-R [...] Created directories have the same mode as the corre-
sponding source directory, unmodified
by the process' umask.

Make this actually true.

In addition, make '-P' (no symbolic links are followed) apply even if
'-R' is not specified. This allows users to overwrite symbolic links
with files and/or to copy symbolic links over a file without indirecting
through the link (ie a copy of a link turns the target into a link, not
a copy of the file pointed to by the source).
 1.29 15-Oct-2005  christos - Don't output 'src -> dst' when -v and there was an error.
- Mark usage as noreturn, and remove extraneous exit.
- Pass lint.
Reported by Liam Foy.
 1.28 15-Aug-2005  elad Add and document -N: When used with -p, it will not copy the file flags.
PR 13645.
 1.27 04-Jan-2004  jschauma remove unused code left from printescaped() backput as pointed out by Jeff
Ito in PR bin/23969 and PR bin/23970.
 1.26 14-Sep-2003  jschauma Following private discussion with kleink@ and hubertf@ and public discussion
on tech-userlevel@, back out printescaped() functionality.
kleink: ``We sell rope.''
 1.25 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.24 04-Aug-2003  jschauma As discusses a little while back on tech-userlevel:

If stdout is a tty, use vis(3) to print any filenames to prevent garbage
from being printed if the filename contains control- or other non-printable
characters.

While here, sprinkle some EXIT_FAILURE and NOTREACHED where appropriate.
 1.23 20-Jan-2003  simonb The Double-Semi-Colon Police.
 1.22 16-Dec-2002  jrf Added a -v verbose option to the cp(1) command. Updated man page and utils as well. This applies to PR bin/16834. This change was approved by Christos.
 1.21 19-Oct-2002  provos use readlink with bufsize - 1; approved thorpej.
 1.20 13-Sep-2001  wiz ANSIfication and some KNF fixes by Petri Koistinen from bin/13681.
 1.19 30-Aug-2001  chs enable use of mmap(), falling back to read()/write() if mmap() fails.
 1.18 18-Jul-2001  tron Use madvise(2) after mmap(2) in case "VM_AND_BUFFER_CACHE_SYNCHRONIZED"
is defined which it is not right now.
 1.17 01-Mar-1999  mjl Correct behaviour in case of dangling symlinks: they would be (contrary
to what the manpage and POSIX say) copied as symlinks, even without -R.
Return ENOENT instead. Closes PR 6975 by Johan Danielsson <joda@pdc.kth.se>.

From FreeBSD: return correct error message if source directory is
unreadable; remove unnecessarily included headers.
 1.16 08-Oct-1998  wsanchez Fix bug where "cp -f" didn't work.
It's suppsed to remove the file and then copy, which it wasn't doing.
But no wait, it turns out that the described behaviour in the manual doesn't
agree with POSIX. So we change the above fix and the manual to "try copy, and
if fail, try remove, then copy".
Fix bug where "cp -R" didn't work on read-only directories:
It would make the directory, set the mode, and not be able to write files into it.
Don't bother mmap()ing files of zero length. Was a workaround for a bug in Rhapsody
mmap(), which didn't get along with such files, but makes sense anyway.
Fix race condition where "cp -p" would set the mod time of a file before close()ing
the file, which would update the mod time and therefore screw up the "-p" idea,
except, of course, while running in gdb, which sucked.
Add -f option to usage message in binary and man page. Already documented in man page.
 1.15 19-Aug-1998  thorpej Add some braces to keep egcs happy.
 1.14 28-Jul-1998  mycroft Delint.
 1.13 28-Jul-1998  mycroft Delint.
 1.12 20-Feb-1998  mycroft Nit: Use MAP_SHARED for read-only mappings where appropriate.
 1.11 17-Feb-1998  augustss Use MAP_PRIVATE|MAP_FILE instead of 0 in mmap().
 1.10 19-Oct-1997  mycroft Use futimes(2) if possible. Also correct an error message.
 1.9 19-Oct-1997  enami Preserve owner/group/mode/atime/mtime of symlink if -p flag is given.
 1.8 20-Jul-1997  thorpej - Use __COPYRIGHT() and __RCSID()
- Fix compiler warnings.
 1.7 15-May-1997  mikel add missing close-brackets; from Greg Oster in PR bin/3613.
 1.6 26-Feb-1997  cgd Patch from BSDI (via Keith Bostic):
>NFS doesn't support chflags; ignore errors unless there's reason
>to believe we're losing bits. (Note, this still won't be right
>if the server supports flags and we were trying to *remove* flags
>on a file that we copied, i.e., that we didn't create.)
 1.5 09-Jan-1997  tls kill use of register
 1.4 02-Aug-1995  jtc Don't remove the destination file if there is a write error. This is
very bad if it is actually a device or fifo. POSIX.2 requires that a
diagnostic message be written, the file descriptor be closed, and for
cp to continue on with the rest of the files.
 1.3 21-Mar-1995  cgd convert to new RCS id conventions.
 1.2 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.1 22-Sep-1994  mycroft branches: 1.1.1;
Initial revision
 1.1.1.1 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.32.10.1 06-Nov-2007  matt sync with HEAD
 1.38.2.1 08-Feb-2011  bouyer Sync with HEAD
 1.40.2.2 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.40.2.1 17-Apr-2012  yamt sync with head
 1.41.6.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.42.6.1 03-Mar-2016  martin Pull up following revision(s) (requested by nakayama in ticket #1096):
bin/mv/mv.c: revision 1.44
bin/cp/utils.c: revision 1.43-1.44
lib/librumphijack/hijack.c: revision 1.112-1.115
usr.bin/touch/touch.c: revision 1.33
sbin/restore/tape.c: revision 1.68
sbin/restore/dirs.c: revision 1.51
Don't truncate at sub-microsecond while preserving timestamps.

One of motivation of this change is to make the behavior of test(1)
-nt/ot with preserved copy (like cp -p) closer to the NetBSD 6.
Of course whether full timestamps are kept or not depends also on
underlying file system.

The ifdef added in mv(1) since existing ifdefs was our local change
to compile it on solaris (though I couldn't test it):
http://mail-index.netbsd.org/tech-userlevel/2014/11/28/msg008831.html

Fix the name of failed function in warning message.

Hijack utimensat(2) so that t_vfs test passes after cp(1)/mv(1) are
changed to use the system call. Linux also has this system call, but
not tested this on linux.

Also hijack futimens(2) so that t_sh test passes.

Define a generic ATCALL() and use it to implement utimensat()
Make ATCALL() behave for absolute paths too.
 1.42.4.2 06-Mar-2016  martin Pull up following revision(s) (requested by mrg in ticket #1130):
bin/cp/utils.c: revision 1.45
for display in the (post) SIGINFO handler use off_t not size_t for
file sizes. fixes incorrect reporting errors on 32 bit platforms
with >4GB file sizes.
 1.42.4.1 03-Mar-2016  martin Pull up following revision(s) (requested by nakayama in ticket #1096):
bin/mv/mv.c: revision 1.44
bin/cp/utils.c: revision 1.43-1.44
lib/librumphijack/hijack.c: revision 1.112-1.115
usr.bin/touch/touch.c: revision 1.33
sbin/restore/tape.c: revision 1.68
sbin/restore/dirs.c: revision 1.51
Don't truncate at sub-microsecond while preserving timestamps.

One of motivation of this change is to make the behavior of test(1)
-nt/ot with preserved copy (like cp -p) closer to the NetBSD 6.
Of course whether full timestamps are kept or not depends also on
underlying file system.

The ifdef added in mv(1) since existing ifdefs was our local change
to compile it on solaris (though I couldn't test it):
http://mail-index.netbsd.org/tech-userlevel/2014/11/28/msg008831.html

Fix the name of failed function in warning message.

Hijack utimensat(2) so that t_vfs test passes after cp(1)/mv(1) are
changed to use the system call. Linux also has this system call, but
not tested this on linux.

Also hijack futimens(2) so that t_sh test passes.

Define a generic ATCALL() and use it to implement utimensat()
Make ATCALL() behave for absolute paths too.
 1.45.16.4 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.45.16.3 21-Apr-2020  martin Sync with HEAD
 1.45.16.2 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.45.16.1 10-Jun-2019  christos Sync with HEAD
 1.45.14.1 28-Jul-2018  pgoyette Sync with HEAD
 1.46.2.1 23-Sep-2021  martin Pull up following revision(s) (requested by skrll in ticket #1348):

bin/cp/utils.c: revision 1.47

PR/54564: Jan Schaumann: cp of a fifo yields an empty file

Don't short-circuit 0 sized stat entries if they don't belong to regular
files.

Also don't try to mmap non-regular files.
 1.49.6.1 20-Jul-2024  martin Pull up following revision(s) (requested by rin in ticket #742):

bin/cp/utils.c: revision 1.50

PR/57857: Ricardo Branco: Always copy regular files, even if they appear to
be zero-sized.
 1.43 18-Sep-2025  mrg introduce a couple of new turn-off-gcc-warning variables and use them.

GCC 14 has a new annoying calloc() checker that we turn off in a bunch
of places, and there are a few more dangling-pointer issuse that come up,
but seem bogus.
 1.42 10-Jun-2018  christos branches: 1.42.2;
use SUBDIR.roff suggested by uwe@
 1.41 05-Jul-2014  dholland branches: 1.41.22;
remove .if make(install)
these are seriously bad juju
 1.40 13-Jun-2014  mrg remove the build support for GCC 4.1, and any hacks i found for it.

there could still be more -- i only looked for lines matching
both "HAVE_GCC" and "4" as words (grep -w.)
 1.39 16-Jul-2013  christos branches: 1.39.4;
WARNS=6 [-Wconversion]
 1.38 25-Jan-2013  he When using -ledit, also use -lterminfo, to allow static linking.
 1.37 25-Jan-2013  christos Obey SMALLPROG and don't enable the editor
 1.36 23-Jan-2013  christos make history kind of work :-), turn libedit support on.
 1.35 22-Jan-2013  christos fix compilation without -DEDIT
 1.34 22-Jan-2013  christos Add a little libedit front end. Could be used as someone's pet project to
learn how to program. It is not enabled in the Makefile, and it states why
there.
 1.33 28-Aug-2011  christos branches: 1.33.2; 1.33.8;
use strpct(3) from libutil.
 1.32 25-Aug-2011  joerg Uses non-literal format strings in err.c
 1.31 14-Aug-2011  christos Document non-literal formats
 1.30 01-Jul-2011  mrg add a comment to confirm a workaround as only being a GCC 4.1 issue.
 1.29 25-Oct-2008  apb Use ${TOOL_SED} instead if plain sed in Makefiles.
 1.28 29-Aug-2008  gmcgarry Wrap compiler-specific flags with HAVE_GCC and HAVE_PCC as necessary. Add a few flags for PCC.
 1.27 01-Jul-2006  mrg branches: 1.27.20;
apply a little -O0 with GCC4 and vax...
 1.26 21-Oct-2003  lukem Rework how MAKEVERBOSE operates:

* Don't bother prefixing commands with a line of ${_MKCMD}\
and instead rely upon "make -s". This is less intrusive on
all the Makefiles than the former. Idea from David Laight.

* Rename the variables use to print messages. The scheme now is:
_MKMSG_FOO Run _MKMSG 'foo'
_MKTARGET_FOO Run _MKMSG_FOO ${.TARGET}
From discussion with Alistair Crooks.
 1.25 19-Oct-2003  lukem rework to use the newer _MKMSGCREATE (et al) macros
 1.24 19-Oct-2003  lukem support MAKEVERBOSE
 1.23 01-Aug-2003  lukem Rework how dependency generation is performed:

* DPSRCS contains extra dependencies, but is _NOT_ added to CLEANFILES.
This is a change of behaviour. If a Makefile wants the clean semantics
it must specifically append to CLEANFILES.
Resolves PR toolchain/5204.

* To recap: .d (depend) files are generated for all files in SRCS and DPSRCS
that have a suffix of: .c .m .s .S .C .cc .cpp .cxx

* If YHEADER is set, automatically add the .y->.h to DPSRCS & CLEANFILES

* Ensure that ${OBJS} ${POBJS} ${LOBJS} ${SOBJS} *.d depend upon ${DPSRCS}

* Deprecate the (short lived) DEPENDSRCS


Update the various Makefiles to these new semantics; generally either
adding to CLEANFILES (because DPSRCS doesn't do that anymore), or replacing
specific .o dependencies with DPSRCS entries.

Tested with "make -j 8 distribution" and "make distribution".
 1.22 29-Jul-2003  lukem move ${DEPENDSRCS} target to after .include <bsd.*.mk>
 1.21 28-Jul-2003  lukem Use ${DEPENDSRCS} as a target instead of .depend, now that
<bsd.dep.mk> builds .depend from .dep files.
 1.20 19-Aug-2002  lukem Use ${NETBSDSRCDIR}/some/path instead of ${.CURDIR}/../../some/path (etc).
 1.19 13-Apr-1998  lukem refer to COPTS not CFLAGS
 1.18 08-Apr-1998  fair Fix PR#3585 - csh printed a negative percentage of CPU used when the total
CPU usage was more than 6 hours (integer overflow problem).
 1.17 22-Oct-1997  lukem add dependancy line to fix parallel compilation
 1.16 11-Oct-1997  mycroft branches: 1.16.2;
Use bsd.subdir.mk as appropriate.
 1.15 10-Oct-1997  christos CFLAGS->CPPFLAGS
 1.14 20-Jul-1997  christos Remove WARNS=1 from all the subdirectory Makefiles, and add it to Makefile.inc
now that all /bin has been cleaned.
 1.13 04-Jul-1997  christos Rename err.h to errnum.h to avoid collision with err.h
Fix compiler warnings.
 1.12 08-May-1997  gwr Back out the .PATH.c changes. The .depend problem (and others)
will be fixed using the new .NOPATH make feature instead.
 1.11 06-May-1997  gwr Use .PATH.c: ...
 1.10 30-Sep-1996  christos - Use ../usr.bin/printf/printf.c
- Fix built-in which to set the status code correctly.
 1.9 21-Mar-1995  cgd convert to new RCS id conventions.
 1.8 22-Dec-1994  cgd new way of specifying man pages.
 1.7 18-Dec-1994  cgd add USD.doc to subdir list on install
 1.6 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.5 01-Aug-1993  mycroft Add RCS identifiers.
 1.4 26-May-1993  proven Compiles with gcc-2.3.3 now.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.16.2.2 08-May-1998  mycroft Pull up 1.18, per request of fair.
 1.16.2.1 07-Nov-1997  lukem pull up rev 1.17 from trunk (approval from thorpej)
 1.27.20.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.33.8.2 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.33.8.1 25-Feb-2013  tls resync with head
 1.33.2.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.39.4.1 10-Aug-2014  tls Rebase.
 1.41.22.1 25-Jun-2018  pgoyette Sync with HEAD
 1.42.2.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.42.2.1 21-Apr-2020  martin Sync with HEAD
 1.16 24-Apr-2024  nia csh: add a reallocarray function for using inside csh
 1.15 05-Jan-2019  christos Welcome to the 21th century csh: retire "ptr_t" now that we have "void *"
 1.14 05-Jan-2019  maya 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
 1.13 22-Jan-2013  christos branches: 1.13.30; 1.13.32;
Remove alloc builtin, it did not work anyway since most modern malloc
implementation use a combination of sbrk/mmap.
 1.12 07-Aug-2003  agc branches: 1.12.54; 1.12.60;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.11 25-May-2002  wiz __STDC__ is always defined on NetBSD.
 1.10 14-Sep-2001  wiz ANSIfication and KNF improvements by Petri Koistinen in bin/13689,
with some fixes by me.
 1.9 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.8 28-Jul-1998  mycroft Delint (partially).
 1.7 04-Jul-1997  christos Rename err.h to errnum.h to avoid collision with err.h
Fix compiler warnings.
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.12.60.1 25-Feb-2013  tls resync with head
 1.12.54.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.13.32.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.13.32.2 21-Apr-2020  martin Sync with HEAD
 1.13.32.1 10-Jun-2019  christos Sync with HEAD
 1.13.30.1 18-Jan-2019  pgoyette Synch with HEAD
 1.10 19-Jan-2012  christos branches: 1.10.42;
PR/45856: Bernhard "Burnhard" Riedel: Infinite loop on &nbsp; input. Sending
char 160 in the input to csh, lead it to an infinite loop, because tcsh tables
counted this as a space character, but the word logic switch does not. Change
that character tables, so that this does not count as a spacing character
anymore, by syncing the table with the one from tcsh.
 1.9 07-Aug-2003  agc branches: 1.9.54;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.8 14-Sep-2001  wiz ANSIfication and KNF improvements by Petri Koistinen in bin/13689,
with some fixes by me.
 1.7 04-Jul-1997  christos Rename err.h to errnum.h to avoid collision with err.h
Fix compiler warnings.
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.9.54.1 17-Apr-2012  yamt sync with head
 1.10.42.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.10.42.1 21-Apr-2020  martin Sync with HEAD
 1.9 19-Jan-2012  christos branches: 1.9.42;
PR/45856: Bernhard "Burnhard" Riedel: Infinite loop on &nbsp; input. Sending
char 160 in the input to csh, lead it to an infinite loop, because tcsh tables
counted this as a space character, but the word logic switch does not. Change
that character tables, so that this does not count as a spacing character
anymore, by syncing the table with the one from tcsh.
 1.8 07-Aug-2003  agc branches: 1.8.54;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.7 14-Sep-2001  wiz ANSIfication and KNF improvements by Petri Koistinen in bin/13689,
with some fixes by me.
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.8.54.1 17-Apr-2012  yamt sync with head
 1.9.42.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.9.42.1 21-Apr-2020  martin Sync with HEAD
 1.11 11-Sep-2021  christos like zsh, have jobs -Z do setproctitle
 1.10 22-Jan-2013  christos branches: 1.10.32;
Add a little libedit front end. Could be used as someone's pet project to
learn how to program. It is not enabled in the Makefile, and it states why
there.
 1.9 07-Aug-2003  agc branches: 1.9.54; 1.9.60;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.8 14-Sep-2001  wiz ANSIfication and KNF improvements by Petri Koistinen in bin/13689,
with some fixes by me.
 1.7 04-Jul-1997  christos Rename err.h to errnum.h to avoid collision with err.h
Fix compiler warnings.
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.9.60.1 25-Feb-2013  tls resync with head
 1.9.54.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.10.32.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.10.32.1 21-Apr-2020  martin Sync with HEAD
 1.59 25-Mar-2025  riastradh csh(1): Nix trailing whitespace in man page.

No functional change intended.
 1.58 22-Mar-2025  christos Improve the description of the jobs built-in.
 1.57 28-Aug-2022  hgutch branches: 1.57.4;
Change back various occurrences of \*[Le], \*[Ge] (less/greater equal)
and \*(ua (upwards arrow) to literal "<=", ">=" and "^" whenever
appropriate (e.g., in code examples).
 1.56 09-Jul-2022  uwe csh(1): fix punctuation at the end of an .Xr line
 1.55 11-Sep-2021  christos like zsh, have jobs -Z do setproctitle
 1.54 03-Jul-2017  wiz branches: 1.54.6;
Remove workaround for ancient HTML generation code.
 1.53 10-Aug-2016  sevan C Shell appeared in 2BSD, not 3BSD
 1.52 22-Jan-2013  wiz Bump date for previous.
 1.51 22-Jan-2013  christos Remove alloc builtin, it did not work anyway since most modern malloc
implementation use a combination of sbrk/mmap.
 1.50 22-Mar-2012  wiz branches: 1.50.2;
Fix whitespace nits. Suggested by Bug Hunting.
 1.49 09-Mar-2011  njoly branches: 1.49.4;
Fix sub-section references.
 1.48 22-Mar-2010  joerg Use .In instead of .Aq .Pa for signal.h.
 1.47 29-Mar-2009  wiz Bump date for previous.
 1.46 29-Mar-2009  mrg - add new RLIMIT_AS (aka RLIMIT_VMEM) resource that limits the total
address space available to processes. this limit exists in most other
modern unix variants, and like most of them, our defaults are unlimited.
remove the old mmap / rlimit.datasize hack.

- adds the VMCMD_STACK flag to all the stack-creation vmcmd callers.
it is currently unused, but was added a few years ago.

- add a pair of new process size values to kinfo_proc2{}. one is the
total size of the process memory map, and the other is the total size
adjusted for unused stack space (since most processes have a lot of
this...)

- patch sh, and csh to notice RLIMIT_AS. (in some cases, the alias
RLIMIT_VMEM was already present and used if availble.)

- patch ps, top and systat to notice the new k_vm_vsize member of
kinfo_proc2{}.

- update irix, svr4, svr4_32, linux and osf1 emulations to support
this information. (freebsd could be done, but that it's best left
as part of the full-update of compat/freebsd.)


this addresses PR 7897. it also gives correct memory usage values,
which have never been entirely correct (since mmap), and have been
very incorrect since jemalloc() was enabled.

tested on i386 and sparc64, build tested on several other platforms.

thanks to many folks for feedback and testing but most espcially
chuq and yamt for critical suggestions that lead to this patch not
having a special ugliness i wasn't happy with anyway :-)
 1.45 13-Mar-2009  joerg Use .Rs/.Re for reference to C shell introduction.
 1.44 03-Mar-2009  wiz Remove weird backslash that somehow must have crept in.
Found by joerg and mdocml.
 1.43 20-Apr-2004  jschauma branches: 1.43.32; 1.43.34;
The correct syntax for csh's pushd with a numeric argument is
pushd +n
not
pushd n
 1.42 17-Apr-2004  wiz Document rlimit sbsize; case consistency; bump date.
 1.41 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.40 14-Jul-2003  wiz cpu -> CPU.
 1.39 01-May-2003  wiz Some grammar and punctuation fixes from jmc@openbsd.
 1.38 05-Apr-2003  wiz Bump date for last.
 1.37 05-Apr-2003  fair Additional text & formatting for the csh "limit" command;
hopefully this satisfies PR 11658
 1.36 25-Feb-2003  wiz .Nm does not need a dummy argument ("") before punctuation or
for correct formatting of the SYNOPSIS any longer.
 1.35 25-Sep-2002  wiz New policy: New sentences start on a new line.
Patches by Robert Elz <kre at munnari oz au>, with minimal changes by me.
 1.34 08-Feb-2002  ross branches: 1.34.2;
Generate <>& symbolically. I'm avoiding .../dist/... directories for now.
 1.33 06-Feb-2002  ross type in ^ directly, instead of \(ua which isn't in all the fonts
 1.32 12-Dec-2001  wiz Document set time variables, and the format strings recognized.
Based on the patch provided by Jonathan Perkins in PR 14465.
 1.31 17-Oct-2001  wiz Whitespace, punctuation fixes. Sort sections.
 1.30 16-Mar-2001  fair Add references to getrlimit(2) and setrlimit(2) under the csh built-in
"limit" command, so as to more completely document the workings of this
command, per PR 11658.
 1.29 07-Nov-2000  lukem fix borken Xr
 1.28 05-Oct-2000  ad insure -> ensure
 1.27 21-May-2000  mason Improved style: "For definiteness" becomes "For example".
 1.26 28-Sep-1999  bouyer xref sysctl(8) (for proc.<pid>.rlimits)
 1.25 30-Jul-1999  hubertf branches: 1.25.2;
Document csh's -m switch.

Patch submitted in PR 5802 by Greg A. Woods <woods@planix.com>
 1.24 20-Apr-1999  mycroft More punctuation glitches.
 1.23 20-Apr-1999  mycroft Fix formatting of kill commands.
 1.22 20-Apr-1999  mycroft Fix a few obvious formatting glitches: missing paragraph break, highlighted
parens.
 1.21 22-Mar-1999  garbled More and more .Os cleanups. .Os is defined in the tmac.doc-common file,
so we shouldn't override it with versions in the manpages. Many more to
come.
 1.20 24-Jan-1999  ross Document the bizarre right-to-left grouping of csh expression terms.
 1.19 11-Jun-1998  msaitoh fix some bugs.
 1.18 28-May-1998  msaitoh fix a bug.
 1.17 28-May-1998  msaitoh fix "/bin/login" to '/usr/bin/login"
 1.16 10-May-1998  kleink * Add support for POSIX "kill [-s signame] pid" syntax; actually fixes PR 5327.
* Complete the "kill -l [exitstatus]" syntax.
* Sync with the documentation and no longer permit the use of full signal
names in the "kill [-sig] pid" syntax, e.g. -SIGHUP.
 1.15 20-Oct-1997  enami - Fix .Nm usage (same of .Nm is replaced with .Sx, .Em, .Ic etc).
- Use .Sx to refer section.
 1.14 23-Jun-1997  pk Use .Xr (from PR#3627)
 1.13 12-May-1997  mikel fix xrefs: sigvec(2) -> sigaction(2), killpg(2) -> killpg(3).
 1.12 08-Mar-1997  mouse alternate -> alternative, per PR 2643
 1.11 30-Jun-1996  jtc Fix formatting of description of @ command; From PR #2577
 1.10 21-Mar-1995  cgd branches: 1.10.6;
convert to new RCS id conventions.
 1.9 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.8 03-Feb-1994  jtc spelling mistakes
 1.7 01-Aug-1993  mycroft Add RCS indentifiers.
 1.6 13-Jul-1993  cgd fix some spacing/character problems...
 1.5 22-Apr-1993  mycroft Fix various bugs in man pages (from 386BSD patch 130).
 1.4 22-Apr-1993  mycroft Fix various bugs in man pages (from 386BSD patch 130).
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.10.6.1 30-Jun-1996  jtc Pull up rev 1.11; Even this late in the game, documentation fixes can't hurt
 1.25.2.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.34.2.1 15-Jun-2003  tron Pull up revision 1.37 (requested by fair in ticket #1243):
Additional text & formatting for the csh "limit" command;
hopefully this satisfies PR 11658
 1.43.34.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.43.32.1 01-Apr-2009  snj Pull up following revision(s) (requested by mrg in ticket #622):
bin/csh/csh.1: revision 1.46
bin/csh/func.c: revision 1.37
bin/ps/print.c: revision 1.111
bin/ps/ps.c: revision 1.74
bin/sh/miscbltin.c: revision 1.38
bin/sh/sh.1: revision 1.92 via patch
external/bsd/top/dist/machine/m_netbsd.c: revision 1.7
lib/libkvm/kvm_proc.c: revision 1.82
sys/arch/mips/mips/cpu_exec.c: revision 1.55
sys/compat/darwin/darwin_exec.c: revision 1.57
sys/compat/ibcs2/ibcs2_exec.c: revision 1.73
sys/compat/irix/irix_resource.c: revision 1.15
sys/compat/linux/arch/amd64/linux_exec_machdep.c: revision 1.16
sys/compat/linux/arch/i386/linux_exec_machdep.c: revision 1.12
sys/compat/linux/common/linux_limit.h: revision 1.5
sys/compat/osf1/osf1_resource.c: revision 1.14
sys/compat/svr4/svr4_resource.c: revision 1.18
sys/compat/svr4_32/svr4_32_resource.c: revision 1.17
sys/kern/exec_subr.c: revision 1.62
sys/kern/init_sysctl.c: revision 1.160
sys/kern/kern_exec.c: revision 1.288
sys/kern/kern_resource.c: revision 1.151
sys/sys/param.h: patch
sys/sys/resource.h: revision 1.31
sys/sys/sysctl.h: revision 1.184
sys/uvm/uvm_extern.h: revision 1.153
sys/uvm/uvm_glue.c: revision 1.136
sys/uvm/uvm_mmap.c: revision 1.128
usr.bin/systat/ps.c: revision 1.32
- - add new RLIMIT_AS (aka RLIMIT_VMEM) resource that limits the total
address space available to processes. this limit exists in most other
modern unix variants, and like most of them, our defaults are unlimited.
remove the old mmap / rlimit.datasize hack.
- - adds the VMCMD_STACK flag to all the stack-creation vmcmd callers.
it is currently unused, but was added a few years ago.
- - add a pair of new process size values to kinfo_proc2{}. one is the
total size of the process memory map, and the other is the total size
adjusted for unused stack space (since most processes have a lot of
this...)
- - patch sh, and csh to notice RLIMIT_AS. (in some cases, the alias
RLIMIT_VMEM was already present and used if availble.)
- - patch ps, top and systat to notice the new k_vm_vsize member of
kinfo_proc2{}.
- - update irix, svr4, svr4_32, linux and osf1 emulations to support
this information. (freebsd could be done, but that it's best left
as part of the full-update of compat/freebsd.)
this addresses PR 7897. it also gives correct memory usage values,
which have never been entirely correct (since mmap), and have been
very incorrect since jemalloc() was enabled.
tested on i386 and sparc64, build tested on several other platforms.
thanks to many folks for feedback and testing but most espcially
chuq and yamt for critical suggestions that lead to this patch not
having a special ugliness i wasn't happy with anyway :-)
 1.49.4.2 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.49.4.1 17-Apr-2012  yamt sync with head
 1.50.2.1 25-Feb-2013  tls resync with head
 1.54.6.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.54.6.1 21-Apr-2020  martin Sync with HEAD
 1.57.4.1 02-Aug-2025  perseant Sync with HEAD
 1.56 15-Sep-2022  martin Fix the build for variants that do not define EDIT.
 1.55 14-Sep-2022  christos Update editing before prompt printing.
 1.54 14-Sep-2022  christos defer editing setup/cleanup to when we are interactive.
 1.53 09-Aug-2020  dholland Use the right size for several calloc calls.

When allocating for a Char **, it should use sizeof(Char *), not
sizeof(Char **). This doesn't actually affect the results except
on DS9000 though :-)

(part 2, the instance in this file was as far as I can tell
inexplicably missed by CVS on the first go...)
 1.52 09-Aug-2020  dholland Clarify some comments.
 1.51 09-Aug-2020  dholland Don't cast the value returned from *malloc. No change to compiler output.
 1.50 03-Apr-2020  joerg Don't depend on common declarations.
 1.49 12-Jan-2020  christos remove unused
 1.48 05-Jan-2019  christos Welcome to the 21th century csh: retire "ptr_t" now that we have "void *"
 1.47 05-Jan-2019  maya 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
 1.46 16-Jul-2013  christos branches: 1.46.26; 1.46.28;
WARNS=6 [-Wconversion]
 1.45 23-Jan-2013  christos make history kind of work :-), turn libedit support on.
 1.44 22-Jan-2013  christos Add a little libedit front end. Could be used as someone's pet project to
learn how to program. It is not enabled in the Makefile, and it states why
there.
 1.43 22-Jan-2012  christos branches: 1.43.6;
From tnozaki@: make fpos_t a complex object that keeps track of the parse
state of the stream. Change argument of the seek function to funopen() from
fpos_t to off_t. Make f{g,s}etpos() use the new fpos_t struct, while providing
backwards compatible entry points. Approved by releng@
 1.42 29-Aug-2011  joerg branches: 1.42.2;
Use __dead
 1.41 20-Jul-2008  lukem Remove the \n and tabs from the __COPYRIGHT() strings.
 1.40 16-Jul-2007  christos branches: 1.40.12;
no need to have cshbool; just make them int
 1.39 16-Jul-2007  christos PR/36650: Michael van Elst: Get rid of bool, because csh uses bool as a small
int.
 1.38 16-Jul-2007  dogcow Rename bool -> cshbool; despite the name, bool actually holds values other
than 0 or 1. Fixes PR/36650.
 1.37 24-Apr-2006  snj It's "its."
 1.36 26-Jun-2005  christos sprinkle a little const, and now everything compiles with WARNS=3
 1.35 19-Aug-2004  christos Deal with RESCUE
 1.34 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.33 16-Jan-2003  kleink Rename `sigset' locals to avoid symbol shadowing warning.
 1.32 06-Jan-2003  wiz descriptor, not decriptor.
 1.31 10-Jul-2002  wiz Fix a typo in a comment. moritz@jodeit.org via OpenBSD.
 1.30 25-May-2002  wiz __STDC__ is always defined on NetBSD.
 1.29 08-Mar-2002  christos % set verbose echo
% echo foo >& bar
% cat bar
guess what you get:
echo foo
foo
 1.28 16-Sep-2001  wiz Use setprogname. Patch by Petri Koistinen.
 1.27 14-Sep-2001  wiz ANSIfication and KNF improvements by Petri Koistinen in bin/13689,
with some fixes by me.
 1.26 04-Sep-2000  christos fix globbing problem when importing $PATH from environment. Reported by
Michael Shalayeff.
 1.25 31-May-2000  christos branches: 1.25.2;
decouple us from stdio's BUFSIZ and boost BUFSIZE to 4K
 1.24 28-Jul-1998  mycroft branches: 1.24.8;
Be more retentive about use of NOTREACHED and noreturn.
 1.23 28-Jul-1998  mycroft Delint.
 1.22 28-Jul-1998  mycroft Delint (partially).
 1.21 28-Jul-1998  mycroft Delint (partially).
 1.20 27-Jul-1998  mycroft const propagation.
 1.19 08-Apr-1998  fair Fix csh to use <paths.h> _PATH_DEFPATH as the default path, absent a PATH
from the environment.
 1.18 01-Apr-1998  kleink Need <time.h> for time() prototype.
 1.17 04-Jul-1997  christos branches: 1.17.2;
Rename err.h to errnum.h to avoid collision with err.h
Fix compiler warnings.
 1.16 13-Jan-1997  tls kill register
 1.15 31-Oct-1996  christos Quote all imported environment variables, so that we don't core dump with
set term='unknown*'
./csh
or we don't have security problems with:
set path=('`/bin/rm -fr *`' /usr/local)
./csh
Reported by Donn Seeley...
 1.14 29-Apr-1995  mycroft Use POSIX signal semantics. Change p_pid and p_jobid to pid_t's.
 1.13 21-Mar-1995  mycroft Use POSIX signals.
 1.12 21-Mar-1995  cgd convert to new RCS id conventions.
 1.11 30-Jan-1995  mycroft Use S_IS*().
 1.10 20-Jan-1995  mycroft Remove `.' from default PATH.
 1.9 15-Jan-1995  mycroft Remove the gratuitous `security' warnings.
 1.8 23-Sep-1994  mycroft Eliminate uses of some obsolete functions.
 1.7 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.6 12-Feb-1994  cgd minor cleanup
 1.5 12-Nov-1993  cgd be rational about foreground process groups; pull code in from tcsh!
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.17.2.1 08-May-1998  mycroft Pull up 1.18-1.19, per request of fair.
 1.24.8.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.25.2.1 04-Sep-2000  christos pullup 1.25 -> 1.26: don't glob $PATH when importing it.
[approved by jhawk]
 1.40.12.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.42.2.2 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.42.2.1 17-Apr-2012  yamt sync with head
 1.43.6.2 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.43.6.1 25-Feb-2013  tls resync with head
 1.46.28.4 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.46.28.3 21-Apr-2020  martin Sync with HEAD
 1.46.28.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.46.28.1 10-Jun-2019  christos Sync with HEAD
 1.46.26.1 18-Jan-2019  pgoyette Synch with HEAD
 1.30 24-Apr-2024  nia csh: add a helper definition for the reallocarray function
 1.29 03-Apr-2020  joerg Don't depend on common declarations.
 1.28 05-Jan-2019  christos Welcome to the 21th century csh: retire "ptr_t" now that we have "void *"
 1.27 05-Jan-2019  maya 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
 1.26 16-Jul-2013  christos branches: 1.26.26; 1.26.28;
WARNS=6 [-Wconversion]
 1.25 23-Jan-2013  christos make history kind of work :-), turn libedit support on.
 1.24 22-Jan-2013  christos fix compilation without -DEDIT
 1.23 22-Jan-2013  christos Add a little libedit front end. Could be used as someone's pet project to
learn how to program. It is not enabled in the Makefile, and it states why
there.
 1.22 09-Nov-2011  christos branches: 1.22.6;
sync with /usr/bin/time, use CLOCK_MONOTONIC
 1.21 16-Jul-2007  christos branches: 1.21.34;
no need to have cshbool; just make them int
 1.20 16-Jul-2007  christos PR/36650: Michael van Elst: Get rid of bool, because csh uses bool as a small
int.
 1.19 16-Jul-2007  dogcow Rename bool -> cshbool; despite the name, bool actually holds values other
than 0 or 1. Fixes PR/36650.
 1.18 10-Jul-2007  dogcow exposure of bool elsewhere in the code requires the addition of stdbool;
in addition, proc.c cleverly has a variable that was initially defined "int",
but had in the actual declaration "bool". oops.
 1.17 29-Apr-2007  msaitoh fix typos
 1.16 26-Jun-2005  christos sprinkle a little const, and now everything compiles with WARNS=3
 1.15 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.14 14-Sep-2001  wiz ANSIfication and KNF improvements by Petri Koistinen in bin/13689,
with some fixes by me.
 1.13 13-Dec-2000  scw Sprinkle some `extern' qualifiers for initialised globals.
 1.12 31-May-2000  christos decouple us from stdio's BUFSIZ and boost BUFSIZE to 4K
 1.11 28-Jul-1998  mycroft branches: 1.11.8;
Delint (partially).
 1.10 04-Jul-1997  christos Rename err.h to errnum.h to avoid collision with err.h
Fix compiler warnings.
 1.9 21-Mar-1995  cgd convert to new RCS id conventions.
 1.8 30-Jan-1995  mycroft Use S_IS*().
 1.7 23-Sep-1994  mycroft Eliminate uses of some obsolete functions.
 1.6 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.5 27-Sep-1993  mycroft #include <sys/types.h>, for magnum stuff.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.11.8.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.21.34.2 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.21.34.1 10-Nov-2011  yamt sync with head
 1.22.6.2 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.22.6.1 25-Feb-2013  tls resync with head
 1.26.28.4 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.26.28.3 21-Apr-2020  martin Sync with HEAD
 1.26.28.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.26.28.1 10-Jun-2019  christos Sync with HEAD
 1.26.26.1 18-Jan-2019  pgoyette Synch with HEAD
 1.36 24-Apr-2024  nia csh: replace malloc(x * y) and realloc(x * y) with reallocarray
 1.35 09-Aug-2020  dholland Use the right size for several calloc calls.

When allocating for a Char **, it should use sizeof(Char *), not
sizeof(Char **). This doesn't actually affect the results except
on DS9000 though :-)
 1.34 09-Aug-2020  dholland Don't cast the value returned from *malloc. No change to compiler output.
 1.33 03-Apr-2020  joerg Don't depend on common declarations.
 1.32 05-Jan-2019  christos Welcome to the 21th century csh: retire "ptr_t" now that we have "void *"
 1.31 05-Jan-2019  maya 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
 1.30 16-Jul-2013  christos branches: 1.30.26; 1.30.28;
WARNS=6 [-Wconversion]
 1.29 16-Jul-2007  christos branches: 1.29.34; 1.29.40;
no need to have cshbool; just make them int
 1.28 16-Jul-2007  christos PR/36650: Michael van Elst: Get rid of bool, because csh uses bool as a small
int.
 1.27 16-Jul-2007  dogcow Rename bool -> cshbool; despite the name, bool actually holds values other
than 0 or 1. Fixes PR/36650.
 1.26 05-Apr-2006  christos save the home directory.
 1.25 18-Mar-2006  christos Coverity CID 1760: Plug memory leak.
 1.24 26-Jun-2005  christos sprinkle a little const, and now everything compiles with WARNS=3
 1.23 10-May-2004  christos avoid overflow in strcpy.
 1.22 07-Aug-2003  agc branches: 1.22.2;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.21 19-Oct-2002  provos use readlink with bufsize - 1; approved thorpej.
 1.20 12-Aug-2002  itojun calloc() arg mistake. it's (nelem, size). from openbsd
 1.19 25-May-2002  wiz __STDC__ is always defined on NetBSD.
 1.18 03-Nov-2001  lukem branches: 1.18.2;
fix WARNS=2
 1.17 14-Sep-2001  wiz ANSIfication and KNF improvements by Petri Koistinen in bin/13689,
with some fixes by me.
 1.16 08-Oct-2000  is Format string auditing by Bill Sommerfeld.
 1.15 19-Mar-1999  christos branches: 1.15.2; 1.15.10;
braces for gcc-2.8.1
 1.14 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.13 28-Jul-1998  mycroft Delint (partially).
 1.12 27-Jul-1998  mycroft const propagation.
 1.11 04-Jul-1997  christos Rename err.h to errnum.h to avoid collision with err.h
Fix compiler warnings.
 1.10 13-Jan-1997  tls kill register
 1.9 21-Mar-1995  cgd convert to new RCS id conventions.
 1.8 23-Sep-1994  mycroft Eliminate uses of some obsolete functions.
 1.7 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.6 31-Jul-1994  mycroft Fix bogus freeing of memory.
 1.5 01-Aug-1993  mycroft branches: 1.5.2;
Add RCS identifiers.
 1.4 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.3 22-Mar-1993  cgd added rcs ids to all files
 1.2 21-Mar-1993  cgd after 0.2.2 "stable" patches applied
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.5.2.1 01-Aug-1994  cgd from trunk.
 1.15.10.2 12-May-2004  jmc Pullup patch (requested by kleink in ticket #147)

Use readlink with bufsize - 1.
 1.15.10.1 08-Oct-2000  is Pullup from trunc, requested by is, approved by jhawk:
dir.c 1.16, dig.c 1.2, host.c 1.3
Format string cleanups by sommerfeld.
 1.15.2.1 08-Oct-2000  he Pull up revision 1.16 (requested by is):
Format string cleanup.
 1.18.2.2 11-May-2004  tron Pull up revision 1.23 (requested by kleink in ticket #1688):
avoid overflow in strcpy.
 1.18.2.1 11-May-2004  tron Pull up revision 1.21 (requested by kleink in ticket #1688):
use readlink with bufsize - 1; approved thorpej.
 1.22.2.1 11-May-2004  tron Pull up revision 1.23 (requested by kleink in ticket #306):
avoid overflow in strcpy.
 1.29.40.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.29.34.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.30.28.4 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.30.28.3 21-Apr-2020  martin Sync with HEAD
 1.30.28.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.30.28.1 10-Jun-2019  christos Sync with HEAD
 1.30.26.1 18-Jan-2019  pgoyette Synch with HEAD
 1.9 03-Apr-2020  joerg Don't depend on common declarations.
 1.8 07-Aug-2003  agc branches: 1.8.98;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.7 14-Sep-2001  wiz ANSIfication and KNF improvements by Petri Koistinen in bin/13689,
with some fixes by me.
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.8.98.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.8.98.2 21-Apr-2020  martin Sync with HEAD
 1.8.98.1 08-Apr-2020  martin Merge changes from current as of 20200406
 1.31 05-Jan-2019  christos Welcome to the 21th century csh: retire "ptr_t" now that we have "void *"
 1.30 05-Jan-2019  maya 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
 1.29 16-Jul-2013  christos branches: 1.29.26; 1.29.28;
WARNS=6 [-Wconversion]
 1.28 09-Nov-2011  christos branches: 1.28.6;
sync with /usr/bin/time, use CLOCK_MONOTONIC
 1.27 29-Aug-2011  joerg branches: 1.27.2;
Use __dead
 1.26 16-Jul-2007  christos no need to have cshbool; just make them int
 1.25 16-Jul-2007  christos PR/36650: Michael van Elst: Get rid of bool, because csh uses bool as a small
int.
 1.24 16-Jul-2007  dogcow Rename bool -> cshbool; despite the name, bool actually holds values other
than 0 or 1. Fixes PR/36650.
 1.23 18-Mar-2006  christos Coverity CID 1763: Plug memory leak.
 1.22 18-Mar-2006  christos Coverity CID 1766: Plug memory leak.
 1.21 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.20 25-May-2002  wiz __STDC__ is always defined on NetBSD.
 1.19 30-Jan-2002  christos stderror does not return.
 1.18 30-Jan-2002  itohy Fix problem where
% echo $20000000000
Segmentation fault (core dumped)
 1.17 14-Sep-2001  wiz ANSIfication and KNF improvements by Petri Koistinen in bin/13689,
with some fixes by me.
 1.16 03-Nov-2000  christos Correct stupid cast problem. Pointed out by simon burge.
 1.15 31-Oct-2000  christos Avoid turning into a random file writing tool when using here-doc's.
Reported to BUGTRAQ by proton <proton@ENERGYMECH.NET>.
 1.14 31-May-2000  christos branches: 1.14.2;
decouple us from stdio's BUFSIZ and boost BUFSIZE to 4K
 1.13 19-Aug-1998  thorpej branches: 1.13.2; 1.13.8;
Add some braces to make egcs happy.
 1.12 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.11 28-Jul-1998  mycroft Delint (partially).
 1.10 04-Jul-1997  christos Rename err.h to errnum.h to avoid collision with err.h
Fix compiler warnings.
 1.9 13-Jan-1997  tls kill register
 1.8 27-Sep-1995  jtc Update lseek() constants:
L_SET -> SEEK_SET
L_INCR -> SEEK_CUR
L_XTND -> SEEK_END
 1.7 21-Mar-1995  cgd convert to new RCS id conventions.
 1.6 23-Sep-1994  mycroft Eliminate uses of some obsolete functions.
 1.5 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.13.8.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.13.2.1 04-Nov-2000  he Pull up revisions 1.15-1.16 (requested by christos):
Avoid overwriting existing files when creating files used
for ``here'' documents, thereby closing a security hole.
 1.14.2.2 05-Nov-2000  tv Pullup 1.16 [christos]:
Correct stupid cast problem. Pointed out by simon burge.
 1.14.2.1 01-Nov-2000  tv Pullup 1.15 [christos]:
Avoid turning into a random file writing tool when using here-doc's.
Reported to BUGTRAQ by proton <proton@ENERGYMECH.NET>.
 1.27.2.2 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.27.2.1 10-Nov-2011  yamt sync with head
 1.28.6.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.29.28.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.29.28.2 21-Apr-2020  martin Sync with HEAD
 1.29.28.1 10-Jun-2019  christos Sync with HEAD
 1.29.26.1 18-Jan-2019  pgoyette Synch with HEAD
 1.26 20-Mar-2025  buhrow Cause jobs -l to actually show process ID's again.
Thanks to rvp for the initial patch.
See: http://mail-index.NetBSD.org/current-users/2025/03/17/msg046284.html
for the full discussion of this fix.
-Brian
 1.25 08-Apr-2022  andvar branches: 1.25.4;
fix various typos, mainly in comments, but also log messages, docs, game text.
 1.24 11-Sep-2021  christos like zsh, have jobs -Z do setproctitle
 1.23 05-Jan-2019  christos Welcome to the 21th century csh: retire "ptr_t" now that we have "void *"
 1.22 05-Jan-2019  maya 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
 1.21 16-Jul-2013  christos branches: 1.21.26; 1.21.28;
WARNS=6 [-Wconversion]
 1.20 14-Feb-2009  lukem branches: 1.20.6; 1.20.12;
fix -Wsign-compare issues
 1.19 17-Mar-2006  dmcmahill branches: 1.19.28;
Correct a check for an index being in bounds for a static array.
 1.18 05-Jan-2004  jmmv Homogenize usage messages: make the 'usage' word all lowercase, as this seems
to be the most common practice in our tree.
 1.17 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.16 25-May-2002  wiz __STDC__ is always defined on NetBSD.
 1.15 14-Sep-2001  wiz ANSIfication and KNF improvements by Petri Koistinen in bin/13689,
with some fixes by me.
 1.14 31-May-2000  christos decouple us from stdio's BUFSIZ and boost BUFSIZE to 4K
 1.13 28-Jul-1998  mycroft branches: 1.13.8;
Be more retentive about use of NOTREACHED and noreturn.
 1.12 28-Jul-1998  mycroft Delint (partially).
 1.11 28-Jul-1998  mycroft Delint (partially).
 1.10 27-Jul-1998  mycroft Slight code reduction.
 1.9 27-Jul-1998  mycroft const propagation.
 1.8 04-Jul-1997  christos Rename err.h to errnum.h to avoid collision with err.h
Fix compiler warnings.
 1.7 13-Jan-1997  tls kill register
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.13.8.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.19.28.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.20.12.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.20.6.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.21.28.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.21.28.2 21-Apr-2020  martin Sync with HEAD
 1.21.28.1 10-Jun-2019  christos Sync with HEAD
 1.21.26.1 18-Jan-2019  pgoyette Synch with HEAD
 1.25.4.1 02-Aug-2025  perseant Sync with HEAD
 1.33 05-Jan-2019  christos Welcome to the 21th century csh: retire "ptr_t" now that we have "void *"
 1.32 05-Jan-2019  maya 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
 1.31 16-Mar-2016  christos branches: 1.31.14; 1.31.16;
add Will Robinson comment.
 1.30 16-Mar-2016  christos Avoid gcc-5 conversion warning (|= expands to int)
 1.29 16-Jul-2013  christos WARNS=6 [-Wconversion]
 1.28 14-Feb-2009  lukem branches: 1.28.6; 1.28.12;
fix -Wsign-compare issues
 1.27 15-Dec-2007  perry branches: 1.27.12;
convert __attribute__s to applicable cdefs.h macros
 1.26 16-Jul-2007  christos branches: 1.26.4;
no need to have cshbool; just make them int
 1.25 16-Jul-2007  christos PR/36650: Michael van Elst: Get rid of bool, because csh uses bool as a small
int.
 1.24 16-Jul-2007  dogcow Rename bool -> cshbool; despite the name, bool actually holds values other
than 0 or 1. Fixes PR/36650.
 1.23 13-May-2006  christos Coverity CID 3330: Initialize uninitialized variable.
 1.22 17-Feb-2005  xtraeme Kill __P().
 1.21 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.20 16-Jan-2003  kleink Rename `sigset' locals to avoid symbol shadowing warning.
 1.19 25-May-2002  wiz __STDC__ is always defined on NetBSD.
 1.18 08-Mar-2002  christos % set verbose echo
% echo foo >& bar
% cat bar
guess what you get:
echo foo
foo
 1.17 03-Nov-2001  lukem fix WARNS=2
 1.16 14-Sep-2001  wiz ANSIfication and KNF improvements by Petri Koistinen in bin/13689,
with some fixes by me.
 1.15 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.14 28-Jul-1998  mycroft Delint (partially).
 1.13 28-Jul-1998  mycroft Delint (partially).
 1.12 26-Jul-1998  mycroft const poisoning.
 1.11 04-Jul-1997  christos Rename err.h to errnum.h to avoid collision with err.h
Fix compiler warnings.
 1.10 13-Jan-1997  tls kill register
 1.9 30-Sep-1996  christos - Use ../usr.bin/printf/printf.c
- Fix built-in which to set the status code correctly.
 1.8 23-May-1995  christos Fixed bug where 'which /bin/csh' reports .//bin/csh, by copying the
current tcsh code.
 1.7 21-Mar-1995  mycroft Use POSIX signals.
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.26.4.1 09-Jan-2008  matt sync with HEAD
 1.27.12.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.28.12.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.28.6.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.31.16.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.31.16.2 21-Apr-2020  martin Sync with HEAD
 1.31.16.1 10-Jun-2019  christos Sync with HEAD
 1.31.14.1 18-Jan-2019  pgoyette Synch with HEAD
 1.22 05-Jan-2019  christos Welcome to the 21th century csh: retire "ptr_t" now that we have "void *"
 1.21 05-Jan-2019  maya 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
 1.20 14-Feb-2009  lukem branches: 1.20.46; 1.20.48;
fix -Wsign-compare issues
 1.19 16-Jul-2007  christos branches: 1.19.20;
no need to have cshbool; just make them int
 1.18 16-Jul-2007  christos PR/36650: Michael van Elst: Get rid of bool, because csh uses bool as a small
int.
 1.17 16-Jul-2007  dogcow Rename bool -> cshbool; despite the name, bool actually holds values other
than 0 or 1. Fixes PR/36650.
 1.16 13-May-2006  christos Coverity CID 3331: Remove impossible test.
 1.15 10-May-2006  mrg rename exp2() to csh_exp2(). (bah, we don't even have an exp2())
 1.14 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.13 25-May-2002  wiz __STDC__ is always defined on NetBSD.
 1.12 14-Sep-2001  wiz ANSIfication and KNF improvements by Petri Koistinen in bin/13689,
with some fixes by me.
 1.11 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.10 28-Jul-1998  mycroft Delint (partially).
 1.9 19-Oct-1997  mycroft Remove bogus use of S_IFLNK.
 1.8 04-Jul-1997  christos Rename err.h to errnum.h to avoid collision with err.h
Fix compiler warnings.
 1.7 13-Jan-1997  tls kill register
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.19.20.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.20.48.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.20.48.2 21-Apr-2020  martin Sync with HEAD
 1.20.48.1 10-Jun-2019  christos Sync with HEAD
 1.20.46.1 18-Jan-2019  pgoyette Synch with HEAD
 1.35 24-Apr-2024  nia csh: add a reallocarray function for using inside csh
 1.34 15-Sep-2022  martin Fix the build for variants that do not define EDIT.
 1.33 14-Sep-2022  christos defer editing setup/cleanup to when we are interactive.
 1.32 23-Apr-2020  simonb Add '-t' option for tcsh-style time output.
 1.31 05-Jan-2019  christos branches: 1.31.2;
Welcome to the 21th century csh: retire "ptr_t" now that we have "void *"
 1.30 15-Jul-2017  christos branches: 1.30.2; 1.30.4; 1.30.6;
Add a variant that passes the format, so that time(1) can use it.
 1.29 16-Jul-2013  christos WARNS=6 [-Wconversion]
 1.28 23-Jan-2013  christos make history kind of work :-), turn libedit support on.
 1.27 22-Jan-2013  christos Add a little libedit front end. Could be used as someone's pet project to
learn how to program. It is not enabled in the Makefile, and it states why
there.
 1.26 22-Jan-2013  christos Remove alloc builtin, it did not work anyway since most modern malloc
implementation use a combination of sbrk/mmap.
 1.25 27-Dec-2012  christos sprinkle const, no functional change intended.
 1.24 09-Nov-2011  christos branches: 1.24.6;
sync with /usr/bin/time, use CLOCK_MONOTONIC
 1.23 29-Aug-2011  joerg branches: 1.23.2;
Use __dead
 1.22 24-Dec-2007  perry __attribute__((__noreturn__)) to __dead
 1.21 16-Jul-2007  christos branches: 1.21.4;
no need to have cshbool; just make them int
 1.20 16-Jul-2007  christos PR/36650: Michael van Elst: Get rid of bool, because csh uses bool as a small
int.
 1.19 16-Jul-2007  dogcow Rename bool -> cshbool; despite the name, bool actually holds values other
than 0 or 1. Fixes PR/36650.
 1.18 24-Feb-2007  matt No functional changes. Cleanup time printing code so it can be used
elsewhere in the tree.
 1.17 26-Jun-2005  christos sprinkle a little const, and now everything compiles with WARNS=3
 1.16 17-Feb-2005  xtraeme Kill __P().
 1.15 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.14 14-Sep-2001  wiz ANSIfication and KNF improvements by Petri Koistinen in bin/13689,
with some fixes by me.
 1.13 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.12 28-Jul-1998  mycroft Delint (partially).
 1.11 28-Jul-1998  mycroft Delint (partially).
 1.10 26-Jul-1998  mycroft const poisoning.
 1.9 04-Jul-1997  christos Rename err.h to errnum.h to avoid collision with err.h
Fix compiler warnings.
 1.8 31-Oct-1996  christos Quote all imported environment variables, so that we don't core dump with
set term='unknown*'
./csh
or we don't have security problems with:
set path=('`/bin/rm -fr *`' /usr/local)
./csh
Reported by Donn Seeley...
 1.7 21-Mar-1995  mycroft Remove dead externs.
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.21.4.1 09-Jan-2008  matt sync with HEAD
 1.23.2.3 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.23.2.2 23-Jan-2013  yamt sync with head
 1.23.2.1 10-Nov-2011  yamt sync with head
 1.24.6.2 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.24.6.1 25-Feb-2013  tls resync with head
 1.30.6.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.30.6.2 21-Apr-2020  martin Sync with HEAD
 1.30.6.1 10-Jun-2019  christos Sync with HEAD
 1.30.4.1 18-Jan-2019  pgoyette Synch with HEAD
 1.30.2.2 15-Jul-2017  christos 1624
 1.30.2.1 15-Jul-2017  christos file extern.h was added on branch perseant-stdc-iso10646 on 2017-07-15 14:35:56 +0000
 1.31.2.1 25-Apr-2020  martin Pull up following revision(s) (requested by simonb in ticket #853):

bin/csh/time.c: revision 1.22
usr.bin/time/time.c: revision 1.24
bin/csh/extern.h: revision 1.32
usr.bin/time/time.1: revision 1.29
usr.bin/time/ext.h: revision 1.4

Add '-t' option for tcsh-style time output.
 1.34 24-Apr-2024  nia csh: replace malloc(x * y) and realloc(x * y) with reallocarray
 1.33 29-Sep-2020  msaitoh s/occurence/occurrence/
 1.32 05-Jan-2019  christos Welcome to the 21th century csh: retire "ptr_t" now that we have "void *"
 1.31 05-Jan-2019  maya 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
 1.30 16-Jul-2013  christos branches: 1.30.26; 1.30.28;
WARNS=6 [-Wconversion]
 1.29 24-Sep-2011  christos branches: 1.29.2; 1.29.8;
csh has no business using TTYHOG
 1.28 14-Feb-2009  lukem fix -Wsign-compare issues
 1.27 16-Jul-2007  christos branches: 1.27.20;
no need to have cshbool; just make them int
 1.26 16-Jul-2007  christos PR/36650: Michael van Elst: Get rid of bool, because csh uses bool as a small
int.
 1.25 16-Jul-2007  dogcow Rename bool -> cshbool; despite the name, bool actually holds values other
than 0 or 1. Fixes PR/36650.
 1.24 24-Apr-2006  snj It's "its."
 1.23 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.22 08-Feb-2003  christos remove maxitems limit (from Todd Miller)
 1.21 16-Jan-2003  kleink Rename `sigset' locals to avoid symbol shadowing warning.
 1.20 12-Aug-2002  itojun calloc() arg mistake. it's (nelem, size). from openbsd
 1.19 25-May-2002  wiz __STDC__ is always defined on NetBSD.
 1.18 14-Sep-2001  wiz ANSIfication and KNF improvements by Petri Koistinen in bin/13689,
with some fixes by me.
 1.17 31-May-2000  christos decouple us from stdio's BUFSIZ and boost BUFSIZE to 4K
 1.16 23-Mar-1999  itohy branches: 1.16.8;
cleanup
 1.15 26-Dec-1998  itohy Fixed ancient bug in csh(1) "filec" implementation.
We need not wait for a while any longer after pressing ESC or ^D.

Problem description:
If a user types characters during performing completion (ESC)
or listings (^D), the chars are inserted at weird position of
the command line.
This makes the command line unusable and irritates the user.
I noticed this on 386BSD(98), probably six years ago.

This change fixes the bug by implementing salvage of the characters.
The salvaged chars are added at the end of the command line.
An extra ioctl(FIONREAD) is required to work-around a glitch on
kernel tty driver.
 1.14 28-Jul-1998  mycroft Delint (partially).
 1.13 04-Jul-1997  christos Rename err.h to errnum.h to avoid collision with err.h
Fix compiler warnings.
 1.12 13-Jan-1997  tls kill register
 1.11 08-Nov-1996  christos Fix bug reported by Chris Torek: use TCSADRAIN instead of TCSANOW, to avoid
dropping characters.
 1.10 21-Mar-1995  mycroft Use POSIX signals.
 1.9 21-Mar-1995  cgd convert to new RCS id conventions.
 1.8 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.7 28-Apr-1994  pk Use TCSADRAIN to avoid loosing chars on slow lines.
 1.6 09-Mar-1994  cgd tchars needn't be static any more
 1.5 07-Mar-1994  cgd i'm surprised this ever worked right before...
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.16.8.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.27.20.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.29.8.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.29.2.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.30.28.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.30.28.2 21-Apr-2020  martin Sync with HEAD
 1.30.28.1 10-Jun-2019  christos Sync with HEAD
 1.30.26.1 18-Jan-2019  pgoyette Synch with HEAD
 1.45 24-Apr-2024  nia csh: replace malloc(x * y) and realloc(x * y) with reallocarray
 1.44 09-Aug-2020  dholland Don't cast the value returned from *malloc. No change to compiler output.
 1.43 06-Jan-2019  christos PR/53837: Michael Scholz: src/bin/csh/func.c from current has a superfluous
fprintf
 1.42 05-Jan-2019  christos Welcome to the 21th century csh: retire "ptr_t" now that we have "void *"
 1.41 05-Jan-2019  maya 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
 1.40 16-Jul-2013  christos branches: 1.40.26; 1.40.28;
WARNS=6 [-Wconversion]
 1.39 09-Jun-2012  christos branches: 1.39.2;
support RLIMIT_NTHR
 1.38 31-Aug-2011  plunky branches: 1.38.2;
NULL does not need a cast
 1.37 29-Mar-2009  mrg - add new RLIMIT_AS (aka RLIMIT_VMEM) resource that limits the total
address space available to processes. this limit exists in most other
modern unix variants, and like most of them, our defaults are unlimited.
remove the old mmap / rlimit.datasize hack.

- adds the VMCMD_STACK flag to all the stack-creation vmcmd callers.
it is currently unused, but was added a few years ago.

- add a pair of new process size values to kinfo_proc2{}. one is the
total size of the process memory map, and the other is the total size
adjusted for unused stack space (since most processes have a lot of
this...)

- patch sh, and csh to notice RLIMIT_AS. (in some cases, the alias
RLIMIT_VMEM was already present and used if availble.)

- patch ps, top and systat to notice the new k_vm_vsize member of
kinfo_proc2{}.

- update irix, svr4, svr4_32, linux and osf1 emulations to support
this information. (freebsd could be done, but that it's best left
as part of the full-update of compat/freebsd.)


this addresses PR 7897. it also gives correct memory usage values,
which have never been entirely correct (since mmap), and have been
very incorrect since jemalloc() was enabled.

tested on i386 and sparc64, build tested on several other platforms.

thanks to many folks for feedback and testing but most espcially
chuq and yamt for critical suggestions that lead to this patch not
having a special ugliness i wasn't happy with anyway :-)
 1.36 15-Sep-2007  ragge branches: 1.36.12; 1.36.14;
Needs errno.h if !gcc.
 1.35 16-Jul-2007  christos branches: 1.35.4;
no need to have cshbool; just make them int
 1.34 16-Jul-2007  christos PR/36650: Michael van Elst: Get rid of bool, because csh uses bool as a small
int.
 1.33 16-Jul-2007  dogcow Rename bool -> cshbool; despite the name, bool actually holds values other
than 0 or 1. Fixes PR/36650.
 1.32 29-Apr-2007  msaitoh fix typos
 1.31 18-Mar-2006  christos branches: 1.31.4; 1.31.6;
Coverity CID 1228: protect against calling close with negative value.
 1.30 26-Jun-2005  christos sprinkle a little const, and now everything compiles with WARNS=3
 1.29 13-May-2004  christos Keep track of the while level, when popping loops because of a breaksw.
This is nasty...
 1.28 17-Apr-2004  christos understand rlimit sbsize
 1.27 17-Dec-2003  christos Handle breaksw inside while loop (from tcsh)
Really: test commits on new cvs binary.
 1.26 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.25 16-Jan-2003  kleink Rename `sigset' locals to avoid symbol shadowing warning.
 1.24 28-May-2002  wiz Since we have rlim_t, use it.
Approved by kleink.
 1.23 25-May-2002  wiz __STDC__ is always defined on NetBSD.
 1.22 17-Dec-2001  christos make sure that rlim_cur >= rlim_max in all the setrlimit(2) cases.
 1.21 14-Sep-2001  wiz ANSIfication and KNF improvements by Petri Koistinen in bin/13689,
with some fixes by me.
 1.20 14-Mar-2001  christos echo;echo;echo; should echo
 1.19 06-Jan-2001  christos eliminate nested extern decls.
 1.18 31-May-2000  christos decouple us from stdio's BUFSIZ and boost BUFSIZE to 4K
 1.17 19-Aug-1998  thorpej branches: 1.17.8;
Add some braces to make egcs happy.
 1.16 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.15 28-Jul-1998  mycroft Delint (partially).
 1.14 28-Jul-1998  mycroft Delint (partially).
 1.13 04-Jul-1997  christos Rename err.h to errnum.h to avoid collision with err.h
Fix compiler warnings.
 1.12 13-Jan-1997  tls kill register
 1.11 09-Feb-1996  christos fix bug where
child% suspend
parent% bg
parent% fg
Fg would not work anymore, it would say Suspended (tty input).
 1.10 21-Mar-1995  mycroft Use POSIX signals.
 1.9 21-Mar-1995  cgd convert to new RCS id conventions.
 1.8 04-Dec-1994  cgd from James Jegers <jimj@miller.cs.uwm.edu>: quiet -Wall, and squelch
some of the worst style errors.
 1.7 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.6 05-May-1994  cgd rlimit foo
 1.5 23-Aug-1993  mycroft RLIMIT_OFILE --> RLIMIT_NOFILE
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.17.8.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.31.6.1 06-Jan-2008  wrstuden Catch up to netbsd-4.0 release.
 1.31.4.2 13-Dec-2007  gmcgarry Wrong branch.
 1.31.4.1 12-Dec-2007  gmcgarry pcc says we need errno.h
 1.35.4.1 06-Nov-2007  matt sync with HEAD
 1.36.14.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.36.12.1 01-Apr-2009  snj Pull up following revision(s) (requested by mrg in ticket #622):
bin/csh/csh.1: revision 1.46
bin/csh/func.c: revision 1.37
bin/ps/print.c: revision 1.111
bin/ps/ps.c: revision 1.74
bin/sh/miscbltin.c: revision 1.38
bin/sh/sh.1: revision 1.92 via patch
external/bsd/top/dist/machine/m_netbsd.c: revision 1.7
lib/libkvm/kvm_proc.c: revision 1.82
sys/arch/mips/mips/cpu_exec.c: revision 1.55
sys/compat/darwin/darwin_exec.c: revision 1.57
sys/compat/ibcs2/ibcs2_exec.c: revision 1.73
sys/compat/irix/irix_resource.c: revision 1.15
sys/compat/linux/arch/amd64/linux_exec_machdep.c: revision 1.16
sys/compat/linux/arch/i386/linux_exec_machdep.c: revision 1.12
sys/compat/linux/common/linux_limit.h: revision 1.5
sys/compat/osf1/osf1_resource.c: revision 1.14
sys/compat/svr4/svr4_resource.c: revision 1.18
sys/compat/svr4_32/svr4_32_resource.c: revision 1.17
sys/kern/exec_subr.c: revision 1.62
sys/kern/init_sysctl.c: revision 1.160
sys/kern/kern_exec.c: revision 1.288
sys/kern/kern_resource.c: revision 1.151
sys/sys/param.h: patch
sys/sys/resource.h: revision 1.31
sys/sys/sysctl.h: revision 1.184
sys/uvm/uvm_extern.h: revision 1.153
sys/uvm/uvm_glue.c: revision 1.136
sys/uvm/uvm_mmap.c: revision 1.128
usr.bin/systat/ps.c: revision 1.32
- - add new RLIMIT_AS (aka RLIMIT_VMEM) resource that limits the total
address space available to processes. this limit exists in most other
modern unix variants, and like most of them, our defaults are unlimited.
remove the old mmap / rlimit.datasize hack.
- - adds the VMCMD_STACK flag to all the stack-creation vmcmd callers.
it is currently unused, but was added a few years ago.
- - add a pair of new process size values to kinfo_proc2{}. one is the
total size of the process memory map, and the other is the total size
adjusted for unused stack space (since most processes have a lot of
this...)
- - patch sh, and csh to notice RLIMIT_AS. (in some cases, the alias
RLIMIT_VMEM was already present and used if availble.)
- - patch ps, top and systat to notice the new k_vm_vsize member of
kinfo_proc2{}.
- - update irix, svr4, svr4_32, linux and osf1 emulations to support
this information. (freebsd could be done, but that it's best left
as part of the full-update of compat/freebsd.)
this addresses PR 7897. it also gives correct memory usage values,
which have never been entirely correct (since mmap), and have been
very incorrect since jemalloc() was enabled.
tested on i386 and sparc64, build tested on several other platforms.
thanks to many folks for feedback and testing but most espcially
chuq and yamt for critical suggestions that lead to this patch not
having a special ugliness i wasn't happy with anyway :-)
 1.38.2.2 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.38.2.1 30-Oct-2012  yamt sync with head
 1.39.2.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.40.28.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.40.28.2 21-Apr-2020  martin Sync with HEAD
 1.40.28.1 10-Jun-2019  christos Sync with HEAD
 1.40.26.1 18-Jan-2019  pgoyette Synch with HEAD
 1.32 24-Apr-2024  nia csh: replace malloc(x * y) and realloc(x * y) with reallocarray
 1.31 05-Jan-2019  christos put back x in xrealloc
 1.30 05-Jan-2019  christos Welcome to the 21th century csh: retire "ptr_t" now that we have "void *"
 1.29 05-Jan-2019  maya 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
 1.28 27-Apr-2017  christos branches: 1.28.8; 1.28.10;
switch to a backtracking instead of a recursive pattern matcher.
 1.27 16-Jul-2013  christos branches: 1.27.18;
WARNS=6 [-Wconversion]
 1.26 22-Jan-2013  christos Add a little libedit front end. Could be used as someone's pet project to
learn how to program. It is not enabled in the Makefile, and it states why
there.
 1.25 16-Jul-2007  christos branches: 1.25.34; 1.25.40;
no need to have cshbool; just make them int
 1.24 16-Jul-2007  christos PR/36650: Michael van Elst: Get rid of bool, because csh uses bool as a small
int.
 1.23 16-Jul-2007  dogcow Rename bool -> cshbool; despite the name, bool actually holds values other
than 0 or 1. Fixes PR/36650.
 1.22 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.21 25-May-2002  wiz __STDC__ is always defined on NetBSD.
 1.20 08-Mar-2002  christos Fix:
% set N2=`echo 1; echo 2; \
? echo 3`
% echo $N2
1 2
so that
% echo $N2
1 2 3
 1.19 03-Nov-2001  lukem fix WARNS=2
 1.18 14-Sep-2001  wiz ANSIfication and KNF improvements by Petri Koistinen in bin/13689,
with some fixes by me.
 1.17 31-May-2000  christos decouple us from stdio's BUFSIZ and boost BUFSIZE to 4K
 1.16 28-Jul-1998  mycroft branches: 1.16.8;
Be more retentive about use of NOTREACHED and noreturn.
 1.15 28-Jul-1998  mycroft Delint (partially).
 1.14 19-Jun-1998  kleink GLOB_QUOTE is dead; per POSIX, backslash quoting of special characters being
enabled is the default behaviour.
 1.13 31-Mar-1998  kleink GLOB_ABEND -> GLOB_ABORTED
 1.12 04-Jul-1997  christos Rename err.h to errnum.h to avoid collision with err.h
Fix compiler warnings.
 1.11 13-Jan-1997  tls kill register
 1.10 21-Mar-1995  cgd convert to new RCS id conventions.
 1.9 21-Sep-1994  mycroft Remove a couple of changes we no longer need.
 1.8 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.7 01-Jul-1994  pk save state of `gflag' across calls to backeval(), allowing globbing to
continue after backquote expansion. (fix for bug #309)
 1.6 07-Feb-1994  cgd don't core when handed "grep \'{\' *". also, fix the error message
when that happens.
 1.5 03-Nov-1993  mycroft Patch from Dan Muntz to mimic original csh when doing multiple globbings, some
of which don't match anything.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.16.8.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.25.40.2 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.25.40.1 25-Feb-2013  tls resync with head
 1.25.34.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.27.18.1 02-May-2017  pgoyette Sync with HEAD - tag prg-localcount2-base1
 1.28.10.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.28.10.2 21-Apr-2020  martin Sync with HEAD
 1.28.10.1 10-Jun-2019  christos Sync with HEAD
 1.28.8.1 18-Jan-2019  pgoyette Synch with HEAD
 1.22 05-Jan-2019  christos Welcome to the 21th century csh: retire "ptr_t" now that we have "void *"
 1.21 05-Jan-2019  maya 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
 1.20 16-Jul-2013  christos branches: 1.20.26; 1.20.28;
WARNS=6 [-Wconversion]
 1.19 23-Jan-2013  christos make history kind of work :-), turn libedit support on.
 1.18 16-Jul-2007  christos branches: 1.18.34; 1.18.40;
no need to have cshbool; just make them int
 1.17 16-Jul-2007  christos PR/36650: Michael van Elst: Get rid of bool, because csh uses bool as a small
int.
 1.16 16-Jul-2007  dogcow Rename bool -> cshbool; despite the name, bool actually holds values other
than 0 or 1. Fixes PR/36650.
 1.15 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.14 16-Jan-2003  kleink Rename `sigset' locals to avoid symbol shadowing warning.
 1.13 25-May-2002  wiz __STDC__ is always defined on NetBSD.
 1.12 14-Sep-2001  wiz ANSIfication and KNF improvements by Petri Koistinen in bin/13689,
with some fixes by me.
 1.11 28-Jul-1998  mycroft Delint (partially).
 1.10 28-Jul-1998  mycroft Delint (partially).
 1.9 04-Jul-1997  christos Rename err.h to errnum.h to avoid collision with err.h
Fix compiler warnings.
 1.8 13-Jan-1997  tls kill register
 1.7 21-Mar-1995  mycroft Use POSIX signals.
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.18.40.2 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.18.40.1 25-Feb-2013  tls resync with head
 1.18.34.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.20.28.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.20.28.2 21-Apr-2020  martin Sync with HEAD
 1.20.28.1 10-Jun-2019  christos Sync with HEAD
 1.20.26.1 18-Jan-2019  pgoyette Synch with HEAD
 1.12 11-Sep-2021  christos like zsh, have jobs -Z do setproctitle
 1.11 22-Jan-2013  christos branches: 1.11.32;
Remove alloc builtin, it did not work anyway since most modern malloc
implementation use a combination of sbrk/mmap.
 1.10 07-Aug-2003  agc branches: 1.10.54; 1.10.60;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.9 25-May-2002  wiz __STDC__ is always defined on NetBSD.
 1.8 14-Sep-2001  wiz ANSIfication and KNF improvements by Petri Koistinen in bin/13689,
with some fixes by me.
 1.7 04-Jul-1997  christos Rename err.h to errnum.h to avoid collision with err.h
Fix compiler warnings.
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.10.60.1 25-Feb-2013  tls resync with head
 1.10.54.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.11.32.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.11.32.1 21-Apr-2020  martin Sync with HEAD
 1.38 02-Oct-2020  christos undo previous for 'r' and 'e' modifiers; they should no go further than
the last '/'.
 1.37 30-Sep-2020  christos Fix
% set x='a/b c/d.e'
% echo $x:q:h
 1.36 09-Aug-2020  dholland Use the right size for several calloc calls.

When allocating for a Char **, it should use sizeof(Char *), not
sizeof(Char **). This doesn't actually affect the results except
on DS9000 though :-)
 1.35 09-Aug-2020  dholland Don't cast the value returned from *malloc. No change to compiler output.
 1.34 01-Feb-2019  mrg compare pointers with NULL not '\0'.
 1.33 05-Jan-2019  christos Welcome to the 21th century csh: retire "ptr_t" now that we have "void *"
 1.32 05-Jan-2019  maya 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
 1.31 06-Aug-2013  christos branches: 1.31.24; 1.31.26;
CID 1060854: Wrong sizeof argument (SIZEOF_MISMATCH)
 1.30 16-Jul-2013  christos WARNS=6 [-Wconversion]
 1.29 23-Jan-2013  christos make history kind of work :-), turn libedit support on.
 1.28 22-Jan-2013  christos Add a little libedit front end. Could be used as someone's pet project to
learn how to program. It is not enabled in the Makefile, and it states why
there.
 1.27 17-Jan-2010  wiz branches: 1.27.6; 1.27.12;
Match curly braces in undef(FILEC) case. Found by cppcheck.
 1.26 16-Jul-2007  christos no need to have cshbool; just make them int
 1.25 16-Jul-2007  christos PR/36650: Michael van Elst: Get rid of bool, because csh uses bool as a small
int.
 1.24 16-Jul-2007  dogcow Rename bool -> cshbool; despite the name, bool actually holds values other
than 0 or 1. Fixes PR/36650.
 1.23 17-Mar-2006  christos branches: 1.23.4; 1.23.6;
Coverity CID 1762. Fix resource leak.
 1.22 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.21 25-May-2002  wiz __STDC__ is always defined on NetBSD.
 1.20 03-Nov-2001  lukem fix WARNS=2
 1.19 14-Sep-2001  wiz ANSIfication and KNF improvements by Petri Koistinen in bin/13689,
with some fixes by me.
 1.18 14-Dec-2000  simonb Need alvec global variable now.
 1.17 31-May-2000  christos decouple us from stdio's BUFSIZ and boost BUFSIZE to 4K
 1.16 19-Mar-1999  christos branches: 1.16.8;
printf format portability.
 1.15 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.14 28-Jul-1998  mycroft Delint (partially).
 1.13 28-Jul-1998  mycroft Delint (partially).
 1.12 29-Mar-1998  mrg make sincereal "static int" rather than just "static"
 1.11 04-Jul-1997  christos Rename err.h to errnum.h to avoid collision with err.h
Fix compiler warnings.
 1.10 13-Jan-1997  tls kill register
 1.9 27-Sep-1995  jtc Update lseek() constants:
L_SET -> SEEK_SET
L_INCR -> SEEK_CUR
L_XTND -> SEEK_END
 1.8 29-Apr-1995  mycroft Use POSIX signal semantics. Change p_pid and p_jobid to pid_t's.
 1.7 21-Mar-1995  cgd convert to new RCS id conventions.
 1.6 23-Sep-1994  mycroft Eliminate uses of some obsolete functions.
 1.5 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.16.8.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.23.6.1 06-Jan-2008  wrstuden Catch up to netbsd-4.0 release.
 1.23.4.2 13-Dec-2007  gmcgarry Wrong branch.
 1.23.4.1 12-Dec-2007  gmcgarry compile without FILEC defined.
 1.27.12.2 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.27.12.1 25-Feb-2013  tls resync with head
 1.27.6.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.31.26.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.31.26.2 21-Apr-2020  martin Sync with HEAD
 1.31.26.1 10-Jun-2019  christos Sync with HEAD
 1.31.24.1 18-Jan-2019  pgoyette Synch with HEAD
 1.23 24-Apr-2024  nia csh: replace malloc(x * y) and realloc(x * y) with reallocarray
 1.22 05-Jan-2019  christos Welcome to the 21th century csh: retire "ptr_t" now that we have "void *"
 1.21 05-Jan-2019  maya 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
 1.20 16-Jul-2013  christos branches: 1.20.26; 1.20.28;
WARNS=6 [-Wconversion]
 1.19 18-Mar-2006  christos branches: 1.19.42; 1.19.48;
Coverity CID 872: Possible NULL deref.
 1.18 17-Mar-2006  christos Coverity CID 1226: Remove bogus close call.
 1.17 26-Jun-2005  christos sprinkle a little const, and now everything compiles with WARNS=3
 1.16 06-Jan-2004  christos Simplify previous; if F_CLOSEM fails, just fall back doing the old thing.
Calling stderror here is not safe, because we might have not setjmp()'ed
yet.
 1.15 05-Jan-2004  christos use F_CLOSEM if it is available.
 1.14 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.13 25-May-2002  wiz __STDC__ is always defined on NetBSD.
 1.12 14-Sep-2001  wiz ANSIfication and KNF improvements by Petri Koistinen in bin/13689,
with some fixes by me.
 1.11 28-Jul-1998  mycroft Delint (partially).
 1.10 28-Jul-1998  mycroft Delint (partially).
 1.9 04-Jul-1997  christos Rename err.h to errnum.h to avoid collision with err.h
Fix compiler warnings.
 1.8 13-Jan-1997  tls kill register
 1.7 31-Oct-1996  christos Quote all imported environment variables, so that we don't core dump with
set term='unknown*'
./csh
or we don't have security problems with:
set path=('`/bin/rm -fr *`' /usr/local)
./csh
Reported by Donn Seeley...
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.19.48.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.19.42.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.20.28.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.20.28.2 21-Apr-2020  martin Sync with HEAD
 1.20.28.1 10-Jun-2019  christos Sync with HEAD
 1.20.26.1 18-Jan-2019  pgoyette Synch with HEAD
 1.21 09-Aug-2020  dholland Use the right size for several calloc calls.

When allocating for a Char **, it should use sizeof(Char *), not
sizeof(Char **). This doesn't actually affect the results except
on DS9000 though :-)
 1.20 09-Aug-2020  dholland Don't cast the value returned from *malloc. No change to compiler output.
 1.19 05-Jan-2019  christos Welcome to the 21th century csh: retire "ptr_t" now that we have "void *"
 1.18 05-Jan-2019  maya 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
 1.17 16-Jul-2007  christos branches: 1.17.76; 1.17.78;
no need to have cshbool; just make them int
 1.16 16-Jul-2007  christos PR/36650: Michael van Elst: Get rid of bool, because csh uses bool as a small
int.
 1.15 16-Jul-2007  dogcow Rename bool -> cshbool; despite the name, bool actually holds values other
than 0 or 1. Fixes PR/36650.
 1.14 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.13 25-May-2002  wiz __STDC__ is always defined on NetBSD.
 1.12 03-Nov-2001  lukem fix WARNS=2
 1.11 14-Sep-2001  wiz ANSIfication and KNF improvements by Petri Koistinen in bin/13689,
with some fixes by me.
 1.10 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.9 28-Jul-1998  mycroft Delint (partially).
 1.8 04-Jul-1997  christos Rename err.h to errnum.h to avoid collision with err.h
Fix compiler warnings.
 1.7 13-Jan-1997  tls kill register
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.17.78.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.17.78.2 21-Apr-2020  martin Sync with HEAD
 1.17.78.1 10-Jun-2019  christos Sync with HEAD
 1.17.76.1 18-Jan-2019  pgoyette Synch with HEAD
 1.8 07-Aug-2003  agc branches: 1.8.98;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.7 14-Sep-2001  wiz ANSIfication and KNF improvements by Petri Koistinen in bin/13689,
with some fixes by me.
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.8.98.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.8.98.1 21-Apr-2020  martin Sync with HEAD
 1.5 21-Sep-1994  mycroft Clean up deleted files.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.7 30-Sep-1996  christos - Use ../usr.bin/printf/printf.c
- Fix built-in which to set the status code correctly.
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.44 05-Jul-2025  mlelstv Indentation.
 1.43 20-Mar-2025  buhrow Cause jobs -l to actually show process ID's again.
Thanks to rvp for the initial patch.
See: http://mail-index.NetBSD.org/current-users/2025/03/17/msg046284.html
for the full discussion of this fix.
-Brian
 1.42 16-Sep-2021  christos branches: 1.42.4;
provide a way to reset setproctitle.
 1.41 11-Sep-2021  christos like zsh, have jobs -Z do setproctitle
 1.40 09-Aug-2020  dholland Don't cast the value returned from *malloc. No change to compiler output.
 1.39 03-Apr-2020  joerg Don't depend on common declarations.
 1.38 05-Jan-2019  christos Welcome to the 21th century csh: retire "ptr_t" now that we have "void *"
 1.37 05-Jan-2019  maya 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
 1.36 16-Jul-2013  christos branches: 1.36.26; 1.36.28;
WARNS=6 [-Wconversion]
 1.35 09-Nov-2011  christos branches: 1.35.6;
sync with /usr/bin/time, use CLOCK_MONOTONIC
 1.34 16-Jul-2007  christos branches: 1.34.34;
no need to have cshbool; just make them int
 1.33 16-Jul-2007  christos PR/36650: Michael van Elst: Get rid of bool, because csh uses bool as a small
int.
 1.32 16-Jul-2007  dogcow Rename bool -> cshbool; despite the name, bool actually holds values other
than 0 or 1. Fixes PR/36650.
 1.31 10-Jul-2007  dogcow exposure of bool elsewhere in the code requires the addition of stdbool;
in addition, proc.c cleverly has a variable that was initially defined "int",
but had in the actual declaration "bool". oops.
 1.30 24-Feb-2007  matt No functional changes. Cleanup time printing code so it can be used
elsewhere in the tree.
 1.29 26-Jun-2005  christos sprinkle a little const, and now everything compiles with WARNS=3
 1.28 19-Sep-2003  itojun make signal range check consistent.
 1.27 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.26 08-Feb-2003  christos use strtol() for better error handling in builtin kill (Peter Jeremy)
 1.25 16-Jan-2003  kleink Rename `sigset' locals to avoid symbol shadowing warning.
 1.24 25-May-2002  wiz __STDC__ is always defined on NetBSD.
 1.23 03-Nov-2001  lukem fix WARNS=2
 1.22 14-Sep-2001  wiz ANSIfication and KNF improvements by Petri Koistinen in bin/13689,
with some fixes by me.
 1.21 06-Jan-2001  christos eliminate nested extern decls.
 1.20 19-May-1999  kleink Remove a bogus whitespace match in the builtin kill(1)'s `-s' parsing;
noticed by Chris Demetriou.
 1.19 09-May-1999  cgd fix incorrect operation caused by the previous checkin.
 1.18 08-May-1999  christos Fix unaligned access memory problem introduced from the -s fix.
 1.17 19-Mar-1999  christos branches: 1.17.2;
- don't use union wait
- printf portability
- add braces for gcc-2.8.1
 1.16 11-Dec-1998  kleink Built-in kill: also accept symbolic signal names prefixed with "SIG".
 1.15 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.14 28-Jul-1998  mycroft Delint (partially).
 1.13 28-Jul-1998  mycroft Delint (partially).
 1.12 10-May-1998  kleink * Add support for POSIX "kill [-s signame] pid" syntax; actually fixes PR 5327.
* Complete the "kill -l [exitstatus]" syntax.
* Sync with the documentation and no longer permit the use of full signal
names in the "kill [-sig] pid" syntax, e.g. -SIGHUP.
 1.11 04-Jul-1997  christos Rename err.h to errnum.h to avoid collision with err.h
Fix compiler warnings.
 1.10 13-Jan-1997  tls kill register
 1.9 29-Apr-1995  mycroft Use POSIX signal semantics. Change p_pid and p_jobid to pid_t's.
 1.8 21-Mar-1995  mycroft Use POSIX signals.
 1.7 21-Mar-1995  mycroft Update to use timer{add,sub}().
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.17.2.1 21-Jun-1999  perry pullup 1.17->1.20 (cgd): fix potential memory corruption with "kill" builtin
 1.34.34.2 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.34.34.1 10-Nov-2011  yamt sync with head
 1.35.6.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.36.28.4 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.36.28.3 21-Apr-2020  martin Sync with HEAD
 1.36.28.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.36.28.1 10-Jun-2019  christos Sync with HEAD
 1.36.26.1 18-Jan-2019  pgoyette Synch with HEAD
 1.42.4.1 02-Aug-2025  perseant Sync with HEAD
 1.15 03-Apr-2020  joerg Don't depend on common declarations.
 1.14 16-Jul-2013  christos branches: 1.14.28;
WARNS=6 [-Wconversion]
 1.13 09-Nov-2011  christos branches: 1.13.6;
sync with /usr/bin/time, use CLOCK_MONOTONIC
 1.12 16-Jul-2007  christos branches: 1.12.34;
no need to have cshbool; just make them int
 1.11 16-Jul-2007  christos PR/36650: Michael van Elst: Get rid of bool, because csh uses bool as a small
int.
 1.10 16-Jul-2007  dogcow Rename bool -> cshbool; despite the name, bool actually holds values other
than 0 or 1. Fixes PR/36650.
 1.9 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.8 14-Sep-2001  wiz ANSIfication and KNF improvements by Petri Koistinen in bin/13689,
with some fixes by me.
 1.7 29-Apr-1995  mycroft Use POSIX signal semantics. Change p_pid and p_jobid to pid_t's.
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.12.34.2 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.12.34.1 10-Nov-2011  yamt sync with head
 1.13.6.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.14.28.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.14.28.2 21-Apr-2020  martin Sync with HEAD
 1.14.28.1 08-Apr-2020  martin Merge changes from current as of 20200406
 1.32 05-Feb-2020  fox bin/csh: Fix the -Wclobber warning.

Mark the variable as volatile as it can be clobbered when a vfork occurs.

Error was reported when build.sh was run with MKLIBCSANITIZER=yes flag.

Reviewed by: kamil@
 1.31 05-Jan-2019  christos Welcome to the 21th century csh: retire "ptr_t" now that we have "void *"
 1.30 05-Jan-2019  maya 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
 1.29 29-Aug-2011  joerg branches: 1.29.42; 1.29.44;
Use __dead
 1.28 16-Jul-2007  christos no need to have cshbool; just make them int
 1.27 16-Jul-2007  christos PR/36650: Michael van Elst: Get rid of bool, because csh uses bool as a small
int.
 1.26 16-Jul-2007  dogcow Rename bool -> cshbool; despite the name, bool actually holds values other
than 0 or 1. Fixes PR/36650.
 1.25 15-Oct-2006  christos sprinkle volatile
 1.24 28-Sep-2004  christos Don't forget to FIOCLEX the internal copies of stdout,stderr,stddiag when
we invoke a subshell. Otherwise the executed command gets invoked with
17,18,19 open.
 1.23 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.22 12-Jul-2003  itojun strlcpy
 1.21 16-Jan-2003  kleink Rename `sigset' locals to avoid symbol shadowing warning.
 1.20 25-May-2002  wiz __STDC__ is always defined on NetBSD.
 1.19 14-Sep-2001  wiz ANSIfication and KNF improvements by Petri Koistinen in bin/13689,
with some fixes by me.
 1.18 02-Nov-1999  tron Avoid segmenation fault if an external command is invoked in parse only
mode. Problem noted by Josh Lynch PR bin/8732.
 1.17 19-Mar-1999  christos branches: 1.17.4;
add braces for gcc-2.8.1
 1.16 19-Aug-1998  thorpej Add some braces to make egcs happy.
 1.15 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.14 28-Jul-1998  mycroft Delint.
 1.13 28-Jul-1998  mycroft Delint (partially).
 1.12 28-Jul-1998  mycroft Delint (partially).
 1.11 04-Jul-1997  christos Rename err.h to errnum.h to avoid collision with err.h
Fix compiler warnings.
 1.10 13-Jan-1997  tls kill register
 1.9 27-Sep-1995  jtc Update lseek() constants:
L_SET -> SEEK_SET
L_INCR -> SEEK_CUR
L_XTND -> SEEK_END
 1.8 21-Mar-1995  mycroft Use POSIX signals.
 1.7 21-Mar-1995  cgd convert to new RCS id conventions.
 1.6 23-Sep-1994  mycroft Eliminate uses of some obsolete functions.
 1.5 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.17.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.29.44.4 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.29.44.3 21-Apr-2020  martin Sync with HEAD
 1.29.44.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.29.44.1 10-Jun-2019  christos Sync with HEAD
 1.29.42.1 18-Jan-2019  pgoyette Synch with HEAD
 1.40 15-Sep-2022  martin Fix the build for variants that do not define EDIT.
 1.39 14-Sep-2022  christos defer editing setup/cleanup to when we are interactive.
 1.38 15-Aug-2021  christos Turn on EL_SAFEREAD
 1.37 12-Jan-2020  christos Add file completion.
 1.36 12-Jan-2020  christos PR/54853: Greg Oster: unable to 'unset filec' or 'unset edit' in csh
While here allow set edit=vi
 1.35 05-Jan-2019  christos Welcome to the 21th century csh: retire "ptr_t" now that we have "void *"
 1.34 05-Jan-2019  maya 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
 1.33 16-Jul-2013  christos branches: 1.33.26; 1.33.28;
WARNS=6 [-Wconversion]
 1.32 03-Apr-2013  christos Make shifting variables reflect their environment counterparts. Makes
"shift path" work as expected.
 1.31 23-Jan-2013  christos make history kind of work :-), turn libedit support on.
 1.30 22-Jan-2013  christos Add a little libedit front end. Could be used as someone's pet project to
learn how to program. It is not enabled in the Makefile, and it states why
there.
 1.29 16-Jul-2007  christos branches: 1.29.34; 1.29.40;
no need to have cshbool; just make them int
 1.28 16-Jul-2007  christos PR/36650: Michael van Elst: Get rid of bool, because csh uses bool as a small
int.
 1.27 16-Jul-2007  dogcow Rename bool -> cshbool; despite the name, bool actually holds values other
than 0 or 1. Fixes PR/36650.
 1.26 29-Apr-2007  msaitoh fix typos
 1.25 14-May-2006  christos Fix two uninitialized variables with XXX: GCC.
 1.24 24-Apr-2006  snj It's "its."
 1.23 21-Mar-2006  christos Coverity CID 2736: Remove 16 bit int compat code.
 1.22 17-Mar-2006  christos Coverity CID 1324: Check adrof return; not really possible.
 1.21 17-Mar-2006  christos Coverity CID 610: Dead code
 1.20 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.19 16-Jan-2003  kleink Rename `sigset' locals to avoid symbol shadowing warning.
 1.18 25-May-2002  wiz __STDC__ is always defined on NetBSD.
 1.17 03-Nov-2001  lukem fix WARNS=2
 1.16 14-Sep-2001  wiz ANSIfication and KNF improvements by Petri Koistinen in bin/13689,
with some fixes by me.
 1.15 31-May-2000  christos decouple us from stdio's BUFSIZ and boost BUFSIZE to 4K
 1.14 19-Mar-1999  christos branches: 1.14.8;
add braces for gcc-2.8.1
 1.13 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.12 28-Jul-1998  mycroft Delint (partially).
 1.11 28-Jul-1998  mycroft Delint (partially).
 1.10 04-Jul-1997  christos Rename err.h to errnum.h to avoid collision with err.h
Fix compiler warnings.
 1.9 13-Jan-1997  tls kill register
 1.8 21-Mar-1995  mycroft Use POSIX signals.
 1.7 21-Mar-1995  cgd convert to new RCS id conventions.
 1.6 15-Jan-1995  mycroft Remove the gratuitous `security' warnings.
 1.5 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.14.8.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.29.40.3 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.29.40.2 23-Jun-2013  tls resync from head
 1.29.40.1 25-Feb-2013  tls resync with head
 1.29.34.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.33.28.4 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.33.28.3 21-Apr-2020  martin Sync with HEAD
 1.33.28.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.33.28.1 10-Jun-2019  christos Sync with HEAD
 1.33.26.1 18-Jan-2019  pgoyette Synch with HEAD
 1.17 24-Apr-2024  nia csh: replace malloc(x * y) and realloc(x * y) with reallocarray
 1.16 05-Jan-2019  christos Welcome to the 21th century csh: retire "ptr_t" now that we have "void *"
 1.15 16-Jul-2013  christos branches: 1.15.26; 1.15.28;
WARNS=6 [-Wconversion]
 1.14 27-Dec-2012  christos sprinkle const, no functional change intended.
 1.13 07-Aug-2003  agc branches: 1.13.54; 1.13.60;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.12 25-May-2002  wiz __STDC__ is always defined on NetBSD.
 1.11 14-Sep-2001  wiz ANSIfication and KNF improvements by Petri Koistinen in bin/13689,
with some fixes by me.
 1.10 28-Jul-1998  mycroft Delint (partially).
 1.9 26-Jul-1998  mycroft const poisoning.
 1.8 04-Jul-1997  christos Rename err.h to errnum.h to avoid collision with err.h
Fix compiler warnings.
 1.7 13-Jan-1997  tls kill register
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.13.60.2 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.13.60.1 25-Feb-2013  tls resync with head
 1.13.54.2 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.13.54.1 23-Jan-2013  yamt sync with head
 1.15.28.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.15.28.2 21-Apr-2020  martin Sync with HEAD
 1.15.28.1 10-Jun-2019  christos Sync with HEAD
 1.15.26.1 18-Jan-2019  pgoyette Synch with HEAD
 1.9 28-Aug-2011  christos use strpct(3) from libutil.
 1.8 13-Feb-2009  abs Use u_int counter to count from 0 to u_int: for (u = 0; u < digits; u++)
 1.7 28-Apr-2008  martin branches: 1.7.8;
Remove clause 3 and 4 from TNF licenses
 1.6 12-Dec-2007  lukem branches: 1.6.4;
use __RCSID()
 1.5 14-Sep-2001  wiz branches: 1.5.26;
ANSIfication and KNF improvements by Petri Koistinen in bin/13689,
with some fixes by me.
 1.4 19-Mar-1999  christos include <limits.h> instead of <machine/limits.h>
 1.3 31-Jan-1999  mrg minor KNF nits, space damage.
 1.2 08-May-1998  fair branches: 1.2.2;
KNF + copyright assignment to TNF
 1.1 08-Apr-1998  fair Fix PR#3585 - csh printed a negative percentage of CPU used when the total
CPU usage was more than 6 hours (integer overflow problem).
 1.2.2.2 08-May-1998  mycroft Pull up 1.1-1.2, per request of fair.
 1.2.2.1 08-May-1998  mycroft file strpct.c was added on branch netbsd-1-3 on 1998-05-08 22:21:06 +0000
 1.5.26.1 09-Jan-2008  matt sync with HEAD
 1.6.4.1 18-May-2008  yamt sync with head.
 1.7.8.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.26 02-Mar-2025  riastradh bin/csh/time.c: Break overlong line.
 1.25 28-Feb-2025  simonb Cast a time_t explicity to (double) to avoid gcc conversion warning.
 1.24 28-Feb-2025  simonb Fix delta time display when prec != 1.
 1.23 17-Oct-2020  mlelstv branches: 1.23.8;
Print real maxrss value like other shells.
 1.22 23-Apr-2020  simonb Add '-t' option for tcsh-style time output.
 1.21 15-Jul-2017  christos branches: 1.21.2; 1.21.6; 1.21.8;
Add a variant that passes the format, so that time(1) can use it.
 1.20 16-Jul-2013  christos WARNS=6 [-Wconversion]
 1.19 09-Nov-2011  christos branches: 1.19.6;
sync with /usr/bin/time, use CLOCK_MONOTONIC
 1.18 28-Aug-2011  christos branches: 1.18.2;
use strpct(3) from libutil.
 1.17 24-Feb-2008  dholland Dividing by a time_t makes the result time_t, so it should be cast to long
when being printf'd as a long.
 1.16 24-Feb-2007  matt branches: 1.16.4; 1.16.8;
No functional changes. Cleanup time printing code so it can be used
elsewhere in the tree.
 1.15 26-Jun-2005  christos sprinkle a little const, and now everything compiles with WARNS=3
 1.14 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.13 25-May-2002  wiz __STDC__ is always defined on NetBSD.
 1.12 14-Sep-2001  wiz ANSIfication and KNF improvements by Petri Koistinen in bin/13689,
with some fixes by me.
 1.11 12-Jun-2001  wiz receive, not recieve
 1.10 08-Apr-1998  fair Fix PR#3585 - csh printed a negative percentage of CPU used when the total
CPU usage was more than 6 hours (integer overflow problem).
 1.9 04-Jul-1997  christos branches: 1.9.2;
Rename err.h to errnum.h to avoid collision with err.h
Fix compiler warnings.
 1.8 13-Jan-1997  tls kill register
 1.7 21-Mar-1995  mycroft Update to use timer{add,sub}().
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.9.2.1 08-May-1998  mycroft Pull up 1.10, per request of fair.
 1.16.8.1 24-Mar-2008  keiichi sync with head.
 1.16.4.1 23-Mar-2008  matt sync with HEAD
 1.18.2.2 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.18.2.1 10-Nov-2011  yamt sync with head
 1.19.6.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.21.8.1 25-Apr-2020  martin Pull up following revision(s) (requested by simonb in ticket #853):

bin/csh/time.c: revision 1.22
usr.bin/time/time.c: revision 1.24
bin/csh/extern.h: revision 1.32
usr.bin/time/time.1: revision 1.29
usr.bin/time/ext.h: revision 1.4

Add '-t' option for tcsh-style time output.
 1.21.6.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.21.6.1 21-Apr-2020  martin Sync with HEAD
 1.21.2.2 15-Jul-2017  christos 1624
 1.21.2.1 15-Jul-2017  christos file time.c was added on branch perseant-stdc-iso10646 on 2017-07-15 14:35:56 +0000
 1.23.8.1 02-Aug-2025  perseant Sync with HEAD
 1.9 05-Jul-2014  dholland branches: 1.9.24;
Reorg docs, part 1:
Move all the reference manuals to subdirs of /usr/share/doc/reference.
We have subdirs ref1-ref9, corresponding to man page sections 1-9.

Everything that's the reference manual for a program (sections 1, 6,
8), C interface (sections 2, 3), driver or file system (section 4),
format or configuration (section 5), or kernel internal interface
(section 9) belongs in here.

Section 7 is a little less clear: some things that might go in section
7 if they were a man page aren't really reference manuals. So I'm only
putting things in reference section 7 that are (to me) clearly
reference material, rather than e.g. tutorials, guides, FAQs, etc.
This obviously leaves some room for debate, especially without first
editing the docs with this distinction in mind, but if people hate
what I've done things can always be moved again.

Note also that while roff macro man pages traditionally go in section
7, I have put all the roff documentation (macros, tools, etc.) in one
place in reference/ref1/roff. This will make it easier to find and
also easier to edit it into some kind of coherent form.
 1.8 05-Jul-2014  dholland Rework /usr/share/doc.

Update the <bsd.doc.mk> infrastructure, and update the docs to match
the new infrastructure.

- Build and install text, ps, pdf, and/or html, not roff sources.

- Don't wire the chapter numbers into the build system, or use them in
the installed pathnames. This didn't matter much when the docs were a
museum, but now that we're theoretically going to start maintaining
them again, we're going to add and remove documents periodically and
having the chapter numbers baked in creates a lot of thrashing for no
purpose.

- Specify the document name explicitly, rather than implicitly in a
path. Use this name (instead of other random strings) as the name
of the installed files.

- Specify the document section, which is the subdirectory of
/usr/share/doc to install into.

- Allow multiple subdocuments. (That is, multiple documents in one
output directory.)

- Enumerate the .png files groff emits along with html so they can be
installed.

- Remove assorted hand-rolled rules for running roff and roff widgetry
and add enough variable settings to make these unnecessary. This
includes support for
- explicit use of soelim
- refer
- tbl
- pic
- eqn

- Forcibly apply at least minimal amounts of sanity to certain
autogenerated roff files.

- Don't exclude USD.doc, SMM.doc, and PSD.doc directories from the
build, as they now actually do stuff.

Note: currently we can't generate pdf. This turns out to be a
nontrivial problem with no immediate solution forthcoming. So for now,
as a workaround, install compressed .ps as the printable form.
 1.7 18-Oct-2007  tls branches: 1.7.34; 1.7.48;
Rename "csh.a" (the appendix of the document) to csh.ap: the .a suffix
is a bit of a nuisance when using cvs import and export and, of course,
clashes with .a for library archive as well.
 1.6 10-Jul-2003  lukem branches: 1.6.22;
Rename a large chunk of the make(1) variables which refer to a
program/tool from "FOO" to "TOOL_FOO". The new variables are:
TOOL_ASN1_COMPILE TOOL_CAP_MKDB TOOL_CAT TOOL_CKSUM TOOL_COMPILE_ET
TOOL_CONFIG TOOL_CRUNCHGEN TOOL_CTAGS TOOL_DB TOOL_EQN TOOL_FGEN
TOOL_GENCAT TOOL_GROFF TOOL_HEXDUMP TOOL_INDXBIB TOOL_INSTALLBOOT
TOOL_INSTALL_INFO TOOL_M4 TOOL_MAKEFS TOOL_MAKEINFO TOOL_MAKEWHATIS
TOOL_MDSETIMAGE TOOL_MENUC TOOL_MKCSMAPPER TOOL_MKESDB
TOOL_MKLOCALE TOOL_MKMAGIC TOOL_MKTEMP TOOL_MSGC TOOL_MTREE
TOOL_PAX TOOL_PIC TOOL_PREPMKBOOTIMAGE TOOL_PWD_MKDB TOOL_REFER
TOOL_ROFF_ASCII TOOL_ROFF_DVI TOOL_ROFF_HTML TOOL_ROFF_PS
TOOL_ROFF_RAW TOOL_RPCGEN TOOL_SOELIM TOOL_SUNLABEL TOOL_TBL
TOOL_UUDECODE TOOL_VGRIND TOOL_ZIC

For each, provide default in <bsd.sys.mk> of the form:
TOOL_FOO?= foo
and for the ${USETOOLS}=="yes" case in <bsd.own.mk>, provide override:
TOOL_FOO= ${TOOLDIR}/bin/${_TOOL_PREFIX}foo

Document all of these in bsd.README.

This cleans up a chunk of potential (and actual) namespace collision
within our build infrastructure, as well as improves consistency in
the share/mk documentation and provision of appropriate defaults for
each of these variables.
 1.5 05-Jan-2003  pooka Add ${MACROS} to ${ROFF} usage to make output readable

from jbernard@mines.edu in misc/19685
 1.4 21-Dec-1997  christos use obj dirs.
 1.3 21-Mar-1995  cgd convert to new RCS id conventions.
 1.2 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.1 20-Sep-1994  mycroft branches: 1.1.1;
Initial revision
 1.1.1.1 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.6.22.1 06-Nov-2007  matt sync with HEAD
 1.7.48.1 10-Aug-2014  tls Rebase.
 1.7.34.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.9.24.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.9.24.1 21-Apr-2020  martin Sync with HEAD
 1.5 07-Aug-2003  agc branches: 1.5.98;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.4 20-Dec-2001  wiz Whitespace nits.
 1.3 21-Mar-1995  cgd convert to new RCS id conventions.
 1.2 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.1 20-Sep-1994  mycroft branches: 1.1.1;
Initial revision
 1.1.1.1 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.5.98.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.5.98.1 21-Apr-2020  martin Sync with HEAD
 1.8 19-Dec-2021  andvar s/backgound/background/
 1.7 07-Aug-2003  agc branches: 1.7.98;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.6 20-Dec-2001  wiz Whitespace nits.
 1.5 09-Jul-1997  jtc /var/spool/mail -> /var/mail
 1.4 09-Jul-1997  jtc /usr/spool/mail -> /var/spool/mail
 1.3 21-Mar-1995  cgd convert to new RCS id conventions.
 1.2 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.1 20-Sep-1994  mycroft branches: 1.1.1;
Initial revision
 1.1.1.1 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.7.98.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.7.98.1 21-Apr-2020  martin Sync with HEAD
 1.5 07-Aug-2003  agc branches: 1.5.98;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.4 20-Dec-2001  wiz Whitespace nits.
 1.3 21-Mar-1995  cgd convert to new RCS id conventions.
 1.2 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.1 20-Sep-1994  mycroft branches: 1.1.1;
Initial revision
 1.1.1.1 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.5.98.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.5.98.1 21-Apr-2020  martin Sync with HEAD
 1.4 07-Aug-2003  agc branches: 1.4.98;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.3 21-Mar-1995  cgd convert to new RCS id conventions.
 1.2 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.1 20-Sep-1994  mycroft branches: 1.1.1;
Initial revision
 1.1.1.1 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.4.98.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.4.98.1 21-Apr-2020  martin Sync with HEAD
 1.6 18-Oct-2007  tls Rename "csh.a" (the appendix of the document) to csh.ap: the .a suffix
is a bit of a nuisance when using cvs import and export and, of course,
clashes with .a for library archive as well.
 1.5 07-Aug-2003  agc branches: 1.5.22;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.4 27-Apr-1998  fair fix nroff error per PR#5347
 1.3 21-Mar-1995  cgd convert to new RCS id conventions.
 1.2 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.1 20-Sep-1994  mycroft branches: 1.1.1;
Initial revision
 1.1.1.1 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.5.22.1 06-Nov-2007  matt sync with HEAD
 1.1 18-Oct-2007  tls branches: 1.1.2; 1.1.74;
Rename "csh.a" (the appendix of the document) to csh.ap: the .a suffix
is a bit of a nuisance when using cvs import and export and, of course,
clashes with .a for library archive as well.
 1.1.74.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.74.1 21-Apr-2020  martin Sync with HEAD
 1.1.2.2 06-Nov-2007  matt sync with HEAD
 1.1.2.1 18-Oct-2007  matt file csh.ap was added on branch matt-armv6 on 2007-11-06 23:07:16 +0000
 1.8 08-Sep-2024  rillig fix a/an grammar in obvious cases
 1.7 02-Jun-2024  andvar branches: 1.7.2;
Fix various typos, mainly triple letters.
 1.6 05-Dec-2021  msaitoh s/commmand/command/ in comment.
 1.5 07-Aug-2003  agc branches: 1.5.98;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.4 05-Feb-2003  perry "Utilize" has exactly the same meaning as "use," but it is more
difficult to read and understand. Most manuals of English style
therefore say that you should use "use".
 1.3 21-Mar-1995  cgd convert to new RCS id conventions.
 1.2 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.1 20-Sep-1994  mycroft branches: 1.1.1;
Initial revision
 1.1.1.1 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.5.98.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.5.98.1 21-Apr-2020  martin Sync with HEAD
 1.7.2.1 02-Aug-2025  perseant Sync with HEAD
 1.4 07-Aug-2003  agc branches: 1.4.98;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.3 21-Mar-1995  cgd convert to new RCS id conventions.
 1.2 21-Sep-1994  mycroft Merge 4.4-Lite version.
 1.1 20-Sep-1994  mycroft branches: 1.1.1;
Initial revision
 1.1.1.1 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.4.98.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.4.98.1 21-Apr-2020  martin Sync with HEAD
 1.16 26-May-2021  christos tool changes
 1.15 14-Aug-2011  christos branches: 1.15.44; 1.15.46; 1.15.50;
Document non-literal formats
 1.14 17-Nov-2006  christos getdate is in libutil.
 1.13 15-Nov-2006  christos fix build with objdirs
 1.12 15-Nov-2006  christos Add -d option, similar to gnu date.
 1.11 18-Jul-2003  lukem doc2html works here now, so remove NOHTML
 1.10 03-Jul-2001  lukem doc2hmtl is broken for recursive Oo ... Oc at this time
 1.9 20-Jul-1997  christos Remove WARNS=1 from all the subdirectory Makefiles, and add it to Makefile.inc
now that all /bin has been cleaned.
 1.8 20-Jul-1997  thorpej Build with WARNS
 1.7 21-Mar-1995  cgd convert to new RCS id conventions.
 1.6 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.5 09-Aug-1993  mycroft Minor tweak for cross-compiling.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.15.50.1 31-May-2021  cjep sync with head
 1.15.46.1 03-Jun-2023  martin Pull up the following revisions, requested by kim in ticket #1640:

bin/date/Makefile up to 1.16
bin/date/date.1 up to 1.54
bin/date/date.c up to 1.65

Add -R option for displaying time in RFC 5322 format, similar to GNU date.
Add -f option to set the time. From FreeBSD.
 1.15.44.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.15.44.1 21-Apr-2020  martin Sync with HEAD
 1.57 17-Sep-2024  kre date(1) says:

STANDARDS
The date utility is expected to be compatible with IEEE Std 1003.2
(“POSIX.2”).

yet the format used for the date string arg is:

[[[[[[CC]yy]mm]dd]HH]MM[.SS]]

whereas POSIX demands

mmddHHMM[[CC]yy]

Why anyone would ever want to use that archaic form is incomprehensible
to me, yet, that is what is required.

Implement support for the POSIX format if POSIXLY_CORRECT is set in
the environment (in the full date(1) build only) in a rather crude
way that relies upon the user getting the format correct.

Tools builds are unaffected - setting the time is not supported there.
(It could be made to work, half setting the time, using -j, is possible
in tools builds, but pointless, and this cheap implementation uses
strptime() which is not necessarily available to a tools build.)

Mention this (very briefly) in the man page.
 1.56 17-Sep-2024  kre Add -U and -z options to date(1).

-U allows the (internal) TZ setting to be returned
to what it was when date started running.

-z zone sets TZ to be "zone" unless that is an empty
string, in which case it causes TZ to be removed from
the environment.

These can be used together to manipulate TZ around
parsing of a -d arg, to allow its parsing to be done
in a different zone than the one used for output, and
all unrelated to the initial TZ setting (or system default).

Note that these new options only appear in the first
synopsis form in both the man page and usage(), as -d
also appears only there, and these options make little
sense without also using -d.

This is a very simple change which amounts to no more
that a few setenv() and unsetenv() calls.
 1.55 17-Sep-2024  kre Note that the order in which the -u and -d options are given matters.
 1.54 31-May-2023  uwe branches: 1.54.2;
date(1): sync two [[[[[CC]yy]mm]dd]HH]MM[.SS] instances

The markup was the same (modulo Li vs Cm for the dot before the
seconds), but use the same source markup grouping/layout in both to
make this fact more obvious.
 1.53 31-May-2023  uwe date(1): minor markup fixes
 1.52 31-May-2023  kim Add -R option for displaying time in RFC 5322 format, similar to GNU date.
 1.51 22-Oct-2022  christos branches: 1.51.2;
Add -f option to set the time. From FreeBSD.
 1.50 10-May-2022  uwe date(1): Use .Dl for one-liners. Same output is generated.
 1.49 10-May-2022  uwe date(1): Fix the offset in previous.
 1.48 10-May-2022  wiz date(1): add example for how to get seconds since the Epoch output
 1.47 27-Jan-2018  wiz branches: 1.47.4; 1.47.6;
Remove trailing whitespace and Tn macro.
 1.46 25-Jan-2018  christos sync with tzcode-2018c
 1.45 03-Jul-2017  wiz Remove workaround for ancient HTML generation code.
 1.44 03-Jan-2017  abhinav The range of the SS field is [0,60]

From the POSIX time.h man page:
"The formal definition of UTC does not permit double leap seconds, so all mention of double leap seconds has been removed, and the range shortened from the former [0,61] seconds seen in previous versions of POSIX."
 1.43 11-Aug-2016  sevan Document the version date first appeared.
Bump date.
 1.42 06-Apr-2012  wiz branches: 1.42.16;
- fix timed(8) references;
- fix capitalization;
- add reference to environ(7) in `SEE ALSO' section, as it is
mentioned in the text.

From patch by Bug Hunting.
 1.41 04-Feb-2010  wiz branches: 1.41.6;
Remove trailing whitespace.
 1.40 04-Feb-2010  reed For the -d option, refer to parsedate(3) for examples.
 1.39 30-Jan-2009  reed Show that "seconds" is the expected argument for -r.
(I didn't bump the manual page date for this minor change. If I
should let me know.)
 1.38 26-Nov-2006  wiz Drop trailing whitespace.
 1.37 20-Nov-2006  christos getdate -> parsedate
 1.36 15-Nov-2006  christos Add -d option, similar to gnu date.
 1.35 15-Nov-2006  jdarrow Add -j flag to not actually change the clock, just parse the date given
(if any) and then display it in the format given. Matches similar -j
flag functionality in FreeBSD/OpenBSD.

Change requested by George Georgalis on netbsd-users.

While I'm here, disambiguate the letters in the date string, by using
the same option letters as used by strftime(3).
 1.34 18-Apr-2005  dsl Add a '-a' option to set the time using adjtime(2) instead of settimeofday(2).
Fix usage so that the options which apply to dat/time setting are on the
correct line.
 1.33 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.32 25-Feb-2003  wiz .Nm does not need a dummy argument ("") before punctuation or
for correct formatting of the SYNOPSIS any longer.
 1.31 25-Sep-2002  wiz New policy: New sentences start on a new line.
Patches by Robert Elz <kre at munnari oz au>, with minimal changes by me.
 1.30 08-Feb-2002  ross Generate <>& symbolically. I'm avoiding .../dist/... directories for now.
 1.29 20-Dec-2001  wiz Sort sections.
 1.28 20-Dec-2001  wiz Whitespace nits.
 1.27 09-Mar-2001  hubertf xref settimeofday(2)
 1.26 18-Dec-2000  hubertf Document /etc/localtime, per PR 8099 by Charles Hannum.
 1.25 08-Oct-2000  bjh21 Formatting cleanup: more fonts, less punctuation.
 1.24 29-Dec-1999  hubertf branches: 1.24.4;
make example cut&paste-ready
 1.23 09-Mar-1999  ross branches: 1.23.2;
Work around a spurious warning.
 1.22 08-Jun-1998  lukem remove unnecessary comment
 1.21 20-Jan-1998  mycroft Make the documented parsing order for the date match reality.
 1.20 20-Jan-1998  mycroft Fix documentation of `-r'.
 1.19 20-Jan-1998  mycroft Allow a century to be specified.
Cut and paste some text from touch(1) about date parsing.
 1.18 03-Nov-1997  kleink s/UCT/UTC/
 1.17 20-Oct-1997  enami branches: 1.17.2;
Fix .Nm usage.
 1.16 11-May-1997  mikel fix broken timed(8) xrefs, noted by David Brownlee.
 1.15 11-Mar-1997  perry Add documentation of the fact that date does indeed understand years
after 2000 (years 00 through 68 are interpreted as meaning 2000-2068;
this should be updated sometime before 2068 :-)
 1.14 25-Jan-1997  perry nuke the BUGS section -- it was vax specific
 1.13 24-Jan-1997  perry remove obsolete -d and -t options -- kernel timezone handling is long gone
 1.12 12-Mar-1996  phil a a => a (close pr 2204).
 1.11 05-Dec-1995  jtc ENVIRONMENTAL -> ENVIRONMENT
 1.10 07-Sep-1995  jtc Sync with 4.4lite2
 1.9 25-Jul-1995  jtc Use "utility" instead of "command". Modern definitions of these terms
are distinct (See POSIX.2 glossary).

A utility is a executable, script or shell builtin; while a command
can be any of those things plus lists, pipelines, compound commands
(if, for, while) and shell function definitions.
 1.8 21-Mar-1995  cgd convert to new RCS id conventions.
 1.7 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.6 19-Aug-1993  jtc Print correct default output format.
Note that date (with a suitable strftime()), is 1003.2 compliant.
 1.5 16-Aug-1993  jtc 1003.2: 4.15.4: The output always shall be terminated by a newline character.
 1.4 01-Aug-1993  mycroft Add RCS indentifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.3 07-Sep-1995  jtc imported from 44lite2
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.17.2.2 29-Jan-1998  mellon Pull up 1.19-1.21 (mycroft)
 1.17.2.1 04-Nov-1997  mellon Pull rev 1.18 up from trunk (kleink)
 1.23.2.1 08-Jan-2000  he Pull up revision 1.24 (requested by hubertf):
Make the example ready for cut & paste.
 1.24.4.2 13-Mar-2001  he Pull up revision 1.26 (requested by he):
Document /etc/localtime symlink.
 1.24.4.1 11-Mar-2001  he Pull up revision 1.27 (requested by hubertf):
Cross-reference settimeofday(2), so it's obvious what system call
is used to set the system (and hardware!) clock.
 1.41.6.1 17-Apr-2012  yamt sync with head
 1.42.16.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.47.6.1 03-Jun-2023  martin Pull up the following revisions, requested by kim in ticket #1640:

bin/date/Makefile up to 1.16
bin/date/date.1 up to 1.54
bin/date/date.c up to 1.65

Add -R option for displaying time in RFC 5322 format, similar to GNU date.
Add -f option to set the time. From FreeBSD.
 1.47.4.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.47.4.1 21-Apr-2020  martin Sync with HEAD
 1.51.2.1 03-Jun-2023  martin Pull up following revision(s) (requested by kim in ticket #192):

bin/date/date.1: revision 1.52
bin/date/date.1: revision 1.53
bin/date/date.1: revision 1.54
bin/date/date.c: revision 1.64
bin/date/date.c: revision 1.65

Add -R option for displaying time in RFC 5322 format, similar to GNU date.

date(1): minor markup fixes

Add -R to usage

date(1): sync two [[[[[CC]yy]mm]dd]HH]MM[.SS] instances
The markup was the same (modulo Li vs Cm for the dot before the
seconds), but use the same source markup grouping/layout in both to
make this fact more obvious.
 1.54.2.1 02-Aug-2025  perseant Sync with HEAD
 1.70 17-Sep-2024  kre date(1) says:

STANDARDS
The date utility is expected to be compatible with IEEE Std 1003.2
(“POSIX.2”).

yet the format used for the date string arg is:

[[[[[[CC]yy]mm]dd]HH]MM[.SS]]

whereas POSIX demands

mmddHHMM[[CC]yy]

Why anyone would ever want to use that archaic form is incomprehensible
to me, yet, that is what is required.

Implement support for the POSIX format if POSIXLY_CORRECT is set in
the environment (in the full date(1) build only) in a rather crude
way that relies upon the user getting the format correct.

Tools builds are unaffected - setting the time is not supported there.
(It could be made to work, half setting the time, using -j, is possible
in tools builds, but pointless, and this cheap implementation uses
strptime() which is not necessarily available to a tools build.)

Mention this (very briefly) in the man page.
 1.69 17-Sep-2024  kre Add -U and -z options to date(1).

-U allows the (internal) TZ setting to be returned
to what it was when date started running.

-z zone sets TZ to be "zone" unless that is an empty
string, in which case it causes TZ to be removed from
the environment.

These can be used together to manipulate TZ around
parsing of a -d arg, to allow its parsing to be done
in a different zone than the one used for output, and
all unrelated to the initial TZ setting (or system default).

Note that these new options only appear in the first
synopsis form in both the man page and usage(), as -d
also appears only there, and these options make little
sense without also using -d.

This is a very simple change which amounts to no more
that a few setenv() and unsetenv() calls.
 1.68 17-Sep-2024  kre PR lib/58674 (and I suspect, several others)

Stop expecting strptime(3) in the tools build, it is an XSI
function, and the tools builds do not set up the environment
to expect XSI functions to be available.

This means dropping support for the -f option in the tools date
(which shouldn't matter, -f sets the format for the string used
to set the time, which the tools date does not support), and
open coding parsing for the -d option in the tools build.

There should be no changes to the date(1) that is installed in /bin

As a (minor) benefit, the tools -d support has become somewhat
more flexible than the previous simple strptime() implementation
allowed, and also does better error checking (no more Feb 30 etc).

Note: no change to the usage message, if -f is passed to the tools
date it will elicit a usage which says that -f is supported...
(that's just laziness...)
 1.67 17-Sep-2024  kre isleap() is not a standard function/macro, so if none of the include
files have defined a macro of that name, define it ourselves.
 1.66 21-Jan-2024  christos branches: 1.66.2;
Handle -d %Y%m%d in the tools version. This is used in the release notes
Makefile.inc when BUILDID is specified. Consider providing parsedate(3)
in libcompat, but then we'd need yacc...
 1.65 31-May-2023  kim Add -R to usage
 1.64 31-May-2023  kim Add -R option for displaying time in RFC 5322 format, similar to GNU date.
 1.63 22-Oct-2022  christos branches: 1.63.2;
Add -f option to set the time. From FreeBSD.
 1.62 26-May-2021  christos tool changes
 1.61 01-Sep-2014  dholland branches: 1.61.16; 1.61.18; 1.61.22;
Provide real error messages.
Inspired by PR 49169 from David H. Gutteridge, but a much broader patch :-)
 1.60 27-Aug-2011  joerg Annotate dead functions
 1.59 29-Jan-2011  christos revert the revert and fix the code properly.
 1.58 28-Jan-2011  drochner roll back to 20101210 -- the current version just hung if one tried
to set the date
 1.57 12-Dec-2010  christos branches: 1.57.2;
check return value of localtime.
 1.56 11-Dec-2010  christos - check return of strtoll
- misc cleanup
 1.55 03-Oct-2010  gson Simplify by using setenv() instead of putenv().
 1.54 02-Oct-2010  gson putenv() no longer accepts a const string
 1.53 29-May-2010  dholland Don't blow up on date +''. Patch from Robert Elz in followup to PR 39392.
 1.52 20-Jul-2008  lukem branches: 1.52.4;
Remove the \n and tabs from the __COPYRIGHT() strings.
 1.51 24-Feb-2008  dholland branches: 1.51.4;
Use strtoll rather than strtol to read the -r argument, so it will work
for wider time_t. Also mark a timed-related y2038 lossage for whenever
someone figures out the best way to shoot timed.
 1.50 04-Feb-2007  cbiere branches: 1.50.4; 1.50.8;
Use size_t not int for strlen().
 1.49 20-Nov-2006  christos branches: 1.49.2;
getdate -> parsedate
 1.48 17-Nov-2006  christos getdate is in libutil.
 1.47 15-Nov-2006  christos Add -d option, similar to gnu date.
 1.46 15-Nov-2006  jdarrow Add -j flag to not actually change the clock, just parse the date given
(if any) and then display it in the format given. Matches similar -j
flag functionality in FreeBSD/OpenBSD.

Change requested by George Georgalis on netbsd-users.

While I'm here, disambiguate the letters in the date string, by using
the same option letters as used by strftime(3).
 1.45 07-Oct-2006  elad PR/28450: David H. Gutteridge: date(1) does not validate its input and
accepts and processes impossible dates

Patch applied, thanks!
 1.44 17-Aug-2006  jnemeth don't include program name when calling err()
 1.43 23-Jul-2006  christos PR/34055: Kevin Massey: Date uses a fixed 1K buffer for strftime
but does not check when strftime fails because the buffer is too small.
Our implementation of strftime does not NUL terminat the buffer
on failure (it does not have to; the results are undefined), so
garbage gets printed. Use a dynamically allocated buffer instead.
 1.42 22-Jul-2005  peter More minor cleanup:
- remove unnecessary prototype for main()
- use symbolic exit code
- KNF & wrap a long line
 1.41 22-Jul-2005  hubertf Minor code cleanup:

* remove unneeded cast
* use err() instead of perror() + exit()
* use symbolic exit code

Patch contributed by Liam J. Foy <liamfoy@sepulcrum.org> in private mail.
 1.40 26-Jun-2005  christos sprinkle a little const, and now everything compiles with WARNS=3
 1.39 15-May-2005  dsl Set (and report) the new time (not the old one).
I broke it in rev 1.38, someone has finally noticed....
 1.38 18-Apr-2005  dsl Add a '-a' option to set the time using adjtime(2) instead of settimeofday(2).
Fix usage so that the options which apply to dat/time setting are on the
correct line.
 1.37 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.36 04-Aug-2003  jschauma As discusses a little while back on tech-userlevel:

If stdout is a tty, use vis(3) to print any filenames to prevent garbage
from being printed if the filename contains control- or other non-printable
characters.

While here, sprinkle some EXIT_FAILURE and NOTREACHED where appropriate.
 1.35 16-Sep-2001  wiz Use setprogname. Patch by Petri Koistinen.
 1.34 13-Sep-2001  wiz ANSIfication and KNF fixes by Petri Koistinen in bin/13691.
 1.33 08-Jul-2001  gmcgarry setthetime() is prototyped static; make it so.
 1.32 23-Oct-2000  simonb Use strtol() to convert the -r parameter so we can use alternate bases
for that input.
 1.31 08-Oct-2000  bjh21 When setting the date, ask mktime() to work out whether we're in DST or not.
Seems to fix PR bin/8750.
 1.30 14-Apr-2000  simonb branches: 1.30.4;
Don't declare 'extern opt*' getopt variables.
 1.29 04-Jan-2000  kleink Make this slightly more portable by using putenv().
 1.28 04-Nov-1998  christos cast to unsigned char for is*()
 1.27 24-Oct-1998  mycroft GMT -> UTC
 1.26 14-Oct-1998  wsanchez For the case where mktime() fails exit with "illegal time" error rather than
"illegal time format", which is misleading.
 1.25 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.24 28-Jul-1998  mycroft Delint.
 1.23 27-Jul-1998  mycroft __AUDIT__ cleanup.
 1.22 01-Apr-1998  kleink Need <time.h> for time(), strftime(), localtime() and mktime() prototypes.
 1.21 21-Jan-1998  mycroft Slight code rearrangement.
 1.20 20-Jan-1998  mycroft Don't validate the mmddhhmm.ss values here. mktime(3) does it.
 1.19 20-Jan-1998  mycroft Make the documented parsing order for the date match reality.
 1.18 20-Jan-1998  mycroft Allow a century to be specified.
Cut and paste some text from touch(1) about date parsing.
 1.17 20-Jan-1998  mycroft Use TM_YEAR_BASE to adjust tm_year, not 1900.
 1.16 05-Nov-1997  cgd lint
 1.15 20-Jul-1997  thorpej branches: 1.15.2;
__RCSID()/__COPYRIGHT()/prototype police.
 1.14 26-Mar-1997  cgd pull in util.h rather than prototypeing logtwmp() locally.
 1.13 24-Jan-1997  perry remove obsolete -d and -t options -- kernel timezone handling is long gone
 1.12 09-Jan-1997  tls kill use of register
 1.11 07-Sep-1995  jtc Sync with 4.4lite2
 1.10 23-Apr-1995  cgd clean up RCS Ids slightly, etc.
 1.9 21-Mar-1995  cgd convert to new RCS id conventions.
 1.8 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.7 31-Dec-1993  jtc Minor tweaks.
 1.6 16-Aug-1993  jtc 1003.2: 4.15.4: The output always shall be terminated by a newline character.
 1.5 01-Aug-1993  mycroft Add RCS identifiers.
 1.4 21-Jun-1993  deraadt date comamnd ignored seconds value. patch from <mhw@cs.brown.edu>
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.3 07-Sep-1995  jtc imported from 44lite2
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.15.2.1 29-Jan-1998  mellon Pull up 1.16 (cgd) and 1.17-1.21 (mycroft)
 1.30.4.3 08-Jul-2001  gmcgarry backout previous; wrong branch
 1.30.4.2 08-Jul-2001  gmcgarry setthetime() is prototyped as static; make it so.
 1.30.4.1 09-Oct-2000  bjh21 Pull up revision 1.31 (approved by jhawk):
Fix off-by-one-hour bug when using date(1) to move between DST and non-DST.
Fixes PR#8750.
 1.49.2.1 13-Jun-2010  riz Pull up following revision(s) (requested by dholland in ticket #1396):
bin/date/date.c: revision 1.53
Don't blow up on date +''. Patch from Robert Elz in followup to PR 39392.
 1.50.8.1 24-Mar-2008  keiichi sync with head.
 1.50.4.1 23-Mar-2008  matt sync with HEAD
 1.51.4.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.52.4.1 21-Nov-2010  riz Pull up following revision(s) (requested by dholland in ticket #1411):
bin/date/date.c: revision 1.53
Don't blow up on date +''. Patch from Robert Elz in followup to PR 39392.
 1.57.2.1 08-Feb-2011  bouyer Sync with HEAD
 1.61.22.1 31-May-2021  cjep sync with head
 1.61.18.2 15-Oct-2024  martin Pull up following revision(s) (requested by kre in ticket #1914):

bin/date/date.c (apply patch)
usr.bin/sed/process.c: revision 1.54
sys/sys/signal.h: revision 1.77
sys/sys/signal.h: revision 1.78
external/gpl2/gmake/dist/main.c: revision 1.2

PR lib/58674

When building the tools version of sed, treat all wide characters
as if they occupy just one column for the purposes of sed's 'l'
command (which it is very unlikely to be used from the tools sed).
wdwidth() is another XSI function, not necessarily available everywhere.

PR lib/58674
bsd_signal should only be visible with _NETBSD_SOURCE - it isn't any kind of
standard function (despite also existing in other systems).

This change inspired by the PR, but doesn't fix it in any way, the tools
config script for gmake doesn't care if the function is visible in any
header, merely if present in libc.

PR lib/58674 (not really so much any more)
Correct previous.
bsd_signal should only be visible with _NETBSD_SOURCE - it isn't any kind of
standard function (despite also existing in other systems).

Turns out that it used to be an XSI function, back in the dark ages
('twas removed in POSIX issue 7, back in 2008, after being marked
obsolete in issue 6 (2001)).

So, make it visible to any applications that request a suitable
X/Open version (and of course, for _NETBSD_SOURCE).
Still no effect on the issue for the PR.

PR lib/58674
Hopefully allow the tools gmake to build (everywhere).

Don't use the system bsd_signal() function, even if one is
defined, use a locally defined one instead. Note that it
cannot be declared static (which the code would do) as it
is possible that system header files might define the function,
if it exists on the host system, and that prototype would not
(cannot) be static.

This is a horrible hack, feel free to do something better.

Note: this version of gmake is (currently anyway) used only
as part of the tools used for building NetBSD - apart from that
it is used for nothing.
 1.61.18.1 03-Jun-2023  martin Pull up the following revisions, requested by kim in ticket #1640:

bin/date/Makefile up to 1.16
bin/date/date.1 up to 1.54
bin/date/date.c up to 1.65

Add -R option for displaying time in RFC 5322 format, similar to GNU date.
Add -f option to set the time. From FreeBSD.
 1.61.16.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.61.16.1 21-Apr-2020  martin Sync with HEAD
 1.63.2.2 14-Oct-2024  martin Pull up following revision(s) (requested by kre in ticket #978):

bin/date/date.c (apply patch)
usr.bin/sed/process.c: revision 1.54
sys/sys/signal.h: revision 1.77
sys/sys/signal.h: revision 1.78
external/gpl2/gmake/dist/main.c: revision 1.2

PR lib/58674
When building the tools version of sed, treat all wide characters
as if they occupy just one column for the purposes of sed's 'l'
command (which it is very unlikely to be used from the tools sed).
wdwidth() is another XSI function, not necessarily available everywhere.

PR lib/58674
bsd_signal should only be visible with _NETBSD_SOURCE - it isn't any kind =
of
standard function (despite also existing in other systems).
This change inspired by the PR, but doesn't fix it in any way, the tools
config script for gmake doesn't care if the function is visible in any
header, merely if present in libc.

PR lib/58674 (not really so much any more)
Correct previous.
bsd_signal should only be visible with _NETBSD_SOURCE - it isn't any kind =
of
standard function (despite also existing in other systems).

Turns out that it used to be an XSI function, back in the dark ages
('twas removed in POSIX issue 7, back in 2008, after being marked
obsolete in issue 6 (2001)).
So, make it visible to any applications that request a suitable
X/Open version (and of course, for _NETBSD_SOURCE).
Still no effect on the issue for the PR.

PR lib/58674
Hopefully allow the tools gmake to build (everywhere).
Don't use the system bsd_signal() function, even if one is
defined, use a locally defined one instead. Note that it
cannot be declared static (which the code would do) as it
is possible that system header files might define the function,
if it exists on the host system, and that prototype would not
(cannot) be static.

This is a horrible hack, feel free to do something better.

Note: this version of gmake is (currently anyway) used only
as part of the tools used for building NetBSD - apart from that
it is used for nothing.
 1.63.2.1 03-Jun-2023  martin Pull up following revision(s) (requested by kim in ticket #192):

bin/date/date.1: revision 1.52
bin/date/date.1: revision 1.53
bin/date/date.1: revision 1.54
bin/date/date.c: revision 1.64
bin/date/date.c: revision 1.65

Add -R option for displaying time in RFC 5322 format, similar to GNU date.

date(1): minor markup fixes

Add -R to usage

date(1): sync two [[[[[CC]yy]mm]dd]HH]MM[.SS] instances
The markup was the same (modulo Li vs Cm for the dot before the
seconds), but use the same source markup grouping/layout in both to
make this fact more obvious.
 1.66.2.1 02-Aug-2025  perseant Sync with HEAD
 1.8 17-Nov-2006  christos branches: 1.8.84;
getdate is in libutil.
 1.7 15-Nov-2006  christos no names in params.
 1.6 15-Nov-2006  christos Add -d option, similar to gnu date.
 1.5 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.4 13-Sep-2001  wiz ANSIfication and KNF fixes by Petri Koistinen in bin/13691.
 1.3 21-Mar-1995  cgd convert to new RCS id conventions.
 1.2 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.1 22-Sep-1994  mycroft branches: 1.1.1;
Initial revision
 1.1.1.1 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.8.84.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.8.84.1 21-Apr-2020  martin Sync with HEAD
 1.2 17-Nov-2006  christos getdate is in libutil.
 1.1 15-Nov-2006  christos Add -d option, similar to gnu date.
 1.30 29-Jan-2011  christos branches: 1.30.46;
revert the revert and fix the code properly.
 1.29 28-Jan-2011  drochner roll back to 20101210 -- the current version just hung if one tried
to set the date
 1.28 11-Dec-2010  christos branches: 1.28.2;
- check return of strtoll
- misc cleanup
 1.27 24-Feb-2008  dholland Use strtoll rather than strtol to read the -r argument, so it will work
for wider time_t. Also mark a timed-related y2038 lossage for whenever
someone figures out the best way to shoot timed.
 1.26 04-Feb-2007  cbiere branches: 1.26.4; 1.26.8;
* Prevent out-of-bounds read tsptype if a packet with
an unknown msg_type is received.
* Check the received packet size.
* Use strncpy() instead of strlcpy() so that we don't
send gibberish from the stack.
* No need to bind().
* htons()/htonl() use uint16_t/uint32_t not u_short/u_long.
 1.25 14-Jun-2006  ginsbach Adapt date(1), the netsettime function, to new return value from socket(2)
when the address family is unsupported.
 1.24 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.23 12-Jul-2003  itojun strlcpy
 1.22 21-Sep-2002  mycroft select() -> poll()
 1.21 03-Nov-2001  lukem fix WARNS=2
 1.20 13-Sep-2001  wiz ANSIfication and KNF fixes by Petri Koistinen in bin/13691.
 1.19 08-Jul-2001  gmcgarry Use socklen_t; for compilers with strict type checking.
 1.18 19-Dec-1998  kristerw Fixed off-by-one error where HOSTNAME+1 characters was copied into
tsp_name that has size HOSTNAME.
 1.17 04-Nov-1998  christos #ifdef BSD4_4 for sin_len
 1.16 28-Jul-1998  mycroft Delint.
 1.15 06-Jul-1998  mrg - use an array MAXHOSTNAMELEN+1 size to hold hostnames
- ensure hostname from gethostname() is nul-terminated in all cases
- minor KNF
- use MAXHOSTNAMELEN over various other values/defines
- be safe will buffers that hold hostnames
 1.14 20-Jan-1998  mycroft Allow a century to be specified.
Cut and paste some text from touch(1) about date parsing.
 1.13 10-Jan-1998  lukem use IP_PORTRANGE_LOW sockopt rather than home-grown code
 1.12 05-Nov-1997  cgd lint
 1.11 20-Jul-1997  thorpej branches: 1.11.2;
__RCSID()/__COPYRIGHT()/prototype police.
 1.10 07-Sep-1995  jtc Sync with 4.4lite2
 1.9 03-Jun-1995  mycroft Fill in sin_len.
 1.8 21-Mar-1995  cgd convert to new RCS id conventions.
 1.7 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.6 03-Feb-1994  deraadt typo fix
 1.5 05-Dec-1993  deraadt should bzero automatic sockaddr's before use
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.3 07-Sep-1995  jtc imported from 44lite2
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.11.2.2 29-Jan-1998  mellon Back out previous changes - 1.14 simply made 1.13 not break on older systems, and we can do without 1.13 and 1.12.
 1.11.2.1 29-Jan-1998  mellon Pull up 1.12 (cgd), 1.13 (lukem) and 1.14 (mycroft)
 1.26.8.1 24-Mar-2008  keiichi sync with head.
 1.26.4.1 23-Mar-2008  matt sync with HEAD
 1.28.2.1 08-Feb-2011  bouyer Sync with HEAD
 1.30.46.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.30.46.1 21-Apr-2020  martin Sync with HEAD
 1.20 04-Oct-2019  mrg copy libc's swab.c into dd as dd_swab(), and remove the restrict.

our implementation was fine, but the restrict marker is problematic
as gcc 8 is now more strict about checking for restrict issues.

this is the only actual consumer of swab(3) in our tree, though,
besides the test for it. oh well.
 1.19 15-Sep-2019  kamil Drop -D_INCOMPLETE_XOPEN_C063 from dd(1)
 1.18 18-Mar-2015  manu branches: 1.18.16;
Add iflag and oflag operands to dd(1)

Like GNU dd(1) similar operands, iflag and oflag allow specifying the
O_* flags given to open(2) for the input and the output file. The values
are comma-sepratated, lower-case, O_ prefix-stripped constants documented
in open(2).

Since iflag and oflag override default values, specifying oflag means
O_CREATE is not set by default and must be specified explicitely.

Some values do not make sense (e.g.: iflag=directory) but are still used
and will raise a warning. For oflag, values rdonly, rdwr and wronly are
filtered out with a warning (dd(1) attempts open(2) with O_RDWR and
then O_WRONLY on failure).

Specifying oflag=trunc along with (seek, oseek or conv=notrunc) is
contradictory and will raise an error.

iflag and oflag are disabled if building with -DMALLPROG
 1.17 08-Aug-2012  christos branches: 1.17.12;
let the standard rules deal with librumpclient
 1.16 06-Nov-2011  jym Add a new command to dd(1): msgfmt. The command modifies the
output of the information summary returned by dd(1). This can be used
to specify messages in a more usable (or parseable) format like
human-readable values.

My intent is to re-use this for building image files and quick I/O
benchmarking.

Reviewed by tsutsui@ on tech-userlevel. See also
http://mail-index.netbsd.org/tech-userlevel/2010/12/03/msg004179.html

Some examples:

$ dd if=/dev/zero of=/dev/null bs=1m count=1 msgfmt=human
1+0 records in
1+0 records out
1048576 bytes (1,0 MB) transferred in 0.001 secs (1048576000 bytes/sec - 1,0 GB/sec)

$ dd if=/dev/zero of=/dev/null count=1 msgfmt='
> <speed>%E</speed>
> <time>%s</time>
> <bytes>%b</bytes>
> '
<speed>500 KB/sec</speed>
<time>0.001</time>
<bytes>512</bytes>
 1.15 04-Feb-2011  pooka branches: 1.15.4;
Remove the rif/rof options and add rump.dd. This makes usage
consistent with other rump clients. Copying between kernels is
done using the host pipe, e.g.:

dd if=foo rof=bar skip=1 seek=1 => dd if=foo skip=1 | rump.dd of=bar seek=1

Also, the pipe idiom extends to copying between different rump
kernels, e.g.:

env RUMP_SERVER=unix://srv1 rump.dd if=thefile \
| env RUMP_SERVER=unix://srv2 rump.dd of=thefile

Pipe approach suggested by yamt (thanks!)
 1.14 22-Nov-2010  pooka branches: 1.14.2;
extra crunchgen hoops
 1.13 22-Nov-2010  pooka Add two new operands: "rif" and "rof". They operate exactly like
"if" and "of" with the exception that the communicate with a rump
kernel instead of the host kernel.

For example, to write stdout to /tmp/file.txt in a rump kernel namespace:
dd rof=/tmp/file.txt

copy /file1 to /file2 inside a rump kernel:
dd rif=/file1 rof=/file2

copy a snippet from /dev/rmd0d on the rump kernel to the host fs:
dd rif=/dev/rmd0d of=save seek=1000 count=3

Eat that, usermode OS.

(I'll document the operands one I have some manpage to refer to
for rump client use).
 1.12 05-Oct-2007  lukem Convert to using raise_default_signal(3).
 1.11 26-Jun-2005  christos branches: 1.11.10;
sprinkle a little const, and now everything compiles with WARNS=3
 1.10 29-Nov-2002  lukem convert to using strsuftoll(3) from libc
 1.9 02-Feb-2002  lukem .ifdef SMALLPROG, compile with -DNO_CONV and don't compile in conv_tab.o
 1.8 25-Nov-2001  lukem - Use u_longlong_t instead of u_quad_t, u_long, or int for various buffer sizes
- Add strsuftoull(), which parses a number into a u_longlong_t, with
multiplication support, and support for 'g' (GB) and 't' (TB) suffices.
If an error occurs, print to stderr and exit.
Based on get_blk() from args.c and strsufto*() (in other programs)
- Add strsuftoullx(), which acts as per strsuftoull() but returns the
error in the supplied buffer instead (if the returned buffer != "", an
error occurred)
- Replace get_bsz() use with strsuftoull()
- Remove (now) unnecessary argument validation
- Remove unused {f,p,s,t}_stats fields in struct IO
 1.7 25-Nov-2001  lukem - ANSI KNF
- WARNS=2 (mainly converting some "char *" -> "const char *")
 1.6 20-Jul-1997  christos Remove WARNS=1 from all the subdirectory Makefiles, and add it to Makefile.inc
now that all /bin has been cleaned.
 1.5 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.4 21-Mar-1995  cgd convert to new RCS id conventions.
 1.3 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.2 01-Aug-1993  mycroft Add RCS identifiers.
 1.1 04-May-1993  glass branches: 1.1.1;
added dd(1) from ftp.uu.net:bsd-sources; torching GNU dd
 1.1.1.1 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.11.10.1 06-Nov-2007  matt sync with HEAD
 1.14.2.1 08-Feb-2011  bouyer Sync with HEAD
 1.15.4.2 30-Oct-2012  yamt sync with head
 1.15.4.1 10-Nov-2011  yamt sync with head
 1.17.12.1 26-Mar-2015  martin Pull up following revision(s) (requested by manu in ticket #640):
bin/dd/extern.h: revision 1.23
bin/dd/dd.1: revision 1.26
bin/dd/dd.1: revision 1.27
bin/dd/dd.h: revision 1.16
bin/dd/dd.c: revision 1.50
bin/dd/Makefile: revision 1.18
bin/dd/args.c: revision 1.39
Add iflag and oflag operands to dd(1)

Like GNU dd(1) similar operands, iflag and oflag allow specifying the
O_* flags given to open(2) for the input and the output file. The values
are comma-sepratated, lower-case, O_ prefix-stripped constants documented
in open(2).

Since iflag and oflag override default values, specifying oflag means
O_CREATE is not set by default and must be specified explicitely.
Some values do not make sense (e.g.: iflag=directory) but are still used
and will raise a warning. For oflag, values rdonly, rdwr and wronly are
filtered out with a warning (dd(1) attempts open(2) with O_RDWR and
then O_WRONLY on failure).

Specifying oflag=trunc along with (seek, oseek or conv=notrunc) is
contradictory and will raise an error.

iflag and oflag are disabled if building with -DMALLPROG

New sentence, new line. Fix typos. Bump date for previous.
 1.18.16.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.18.16.2 21-Apr-2020  martin Sync with HEAD
 1.18.16.1 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.43 26-Jan-2024  mlelstv Don't use the signal handler to terminate when nothing is to do (count=0, files=0).

The signal handler tries to raise the signal again, so that a parent can retrieve
the signal from the exit code. Calling the terminate handler with signal code 0
doesn't raise a signal and dd continues with exit(127) making this case an error.
 1.42 14-Jan-2022  christos The "ibm" and "oldibm" tables are identical, because POSIX just
standardised the table from V7. Nobody, including the original authors,
seems to have noticed this. Merge them and update the documentation.
Also fix the odd, inconsistent, spelling of "pre-4.3BSD-Reno").
(From nabijaczleweli)
 1.41 09-Oct-2021  rillig dd: fix lint warnings about effectively discarding 'const'

args.c(153): warning: call to 'bsearch' effectively discards 'const'
from argument [346]
args.c(383): dito
args.c(475): dito

No functional change.
 1.40 30-Jan-2019  mrg 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.
 1.39 18-Mar-2015  manu branches: 1.39.16;
Add iflag and oflag operands to dd(1)

Like GNU dd(1) similar operands, iflag and oflag allow specifying the
O_* flags given to open(2) for the input and the output file. The values
are comma-sepratated, lower-case, O_ prefix-stripped constants documented
in open(2).

Since iflag and oflag override default values, specifying oflag means
O_CREATE is not set by default and must be specified explicitely.

Some values do not make sense (e.g.: iflag=directory) but are still used
and will raise a warning. For oflag, values rdonly, rdwr and wronly are
filtered out with a warning (dd(1) attempts open(2) with O_RDWR and
then O_WRONLY on failure).

Specifying oflag=trunc along with (seek, oseek or conv=notrunc) is
contradictory and will raise an error.

iflag and oflag are disabled if building with -DMALLPROG
 1.38 17-Jul-2013  christos branches: 1.38.6;
PR/48057: psi: skip and msgfmt cannot be used together. Looks like a pasto;
fixed.
 1.37 07-Nov-2011  jym branches: 1.37.6;
As suggested by Enami, rework the msgfmt logic a bit:
- error out when an unknown specifier is used. Do this in f_msgfmt(),
before dd(1) starts operation.
- allow buffer_write() to flush the internal buffer even when NULL is
passed as parameter.

Some whitespace fixes too.
 1.36 06-Nov-2011  jym Add a new command to dd(1): msgfmt. The command modifies the
output of the information summary returned by dd(1). This can be used
to specify messages in a more usable (or parseable) format like
human-readable values.

My intent is to re-use this for building image files and quick I/O
benchmarking.

Reviewed by tsutsui@ on tech-userlevel. See also
http://mail-index.netbsd.org/tech-userlevel/2010/12/03/msg004179.html

Some examples:

$ dd if=/dev/zero of=/dev/null bs=1m count=1 msgfmt=human
1+0 records in
1+0 records out
1048576 bytes (1,0 MB) transferred in 0.001 secs (1048576000 bytes/sec - 1,0 GB/sec)

$ dd if=/dev/zero of=/dev/null count=1 msgfmt='
> <speed>%E</speed>
> <time>%s</time>
> <bytes>%b</bytes>
> '
<speed>500 KB/sec</speed>
<time>0.001</time>
<bytes>512</bytes>
 1.35 16-Sep-2011  joerg branches: 1.35.2;
Use __dead for the small build too
 1.34 04-Feb-2011  pooka Remove the rif/rof options and add rump.dd. This makes usage
consistent with other rump clients. Copying between kernels is
done using the host pipe, e.g.:

dd if=foo rof=bar skip=1 seek=1 => dd if=foo skip=1 | rump.dd of=bar seek=1

Also, the pipe idiom extends to copying between different rump
kernels, e.g.:

env RUMP_SERVER=unix://srv1 rump.dd if=thefile \
| env RUMP_SERVER=unix://srv2 rump.dd of=thefile

Pipe approach suggested by yamt (thanks!)
 1.33 13-Jan-2011  jym branches: 1.33.2;
No need to cast with bsearch(), it returns a void *. Use __arraycount().
 1.32 22-Dec-2010  enami Add iseek and oseek option as aliases for skip and seek respectively.
These options exist in dd of solaris or svr3. From FreeBSD.
 1.31 22-Dec-2010  enami Copy argument before modifying it so that ps shows entire argument.
From OpenBSD via FreeBSD.
 1.30 14-Dec-2010  pooka Make compile on non-NetBSD.
 1.29 09-Dec-2010  enami Wrap long line.
 1.28 05-Dec-2010  jym Hmm, for 'rof', I believe that C_ROF|C_OF is invalid rather than
C_ROF|C_ROF.

Hi pooka :)
 1.27 22-Nov-2010  pooka Add two new operands: "rif" and "rof". They operate exactly like
"if" and "of" with the exception that the communicate with a rump
kernel instead of the host kernel.

For example, to write stdout to /tmp/file.txt in a rump kernel namespace:
dd rof=/tmp/file.txt

copy /file1 to /file2 inside a rump kernel:
dd rif=/file1 rof=/file2

copy a snippet from /dev/rmd0d on the rump kernel to the host fs:
dd rif=/dev/rmd0d of=save seek=1000 count=3

Eat that, usermode OS.

(I'll document the operands one I have some manpage to refer to
for rump client use).
 1.26 09-Jan-2006  apb In "dd progress=N", let the value of N control how often
to print a "." to stderr. Previously, any non-zero
value behaved like "progress=1".

PR 24300

Approved by christos
 1.25 17-Jan-2004  dbj add support for conv=sparse
inspired by freebsd, although this patch attempts
to avoid some potential bugs in their implementation.
 1.24 17-Jan-2004  dbj Add C_ASCII and C_EBCDIC to list of options which unset C_BS semantics.
Without this fix, the bs option would cause any of the ascii<->ebcdic
conv options to be ignored.
 1.23 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.22 04-Aug-2003  jschauma As discusses a little while back on tech-userlevel:

If stdout is a tty, use vis(3) to print any filenames to prevent garbage
from being printed if the filename contains control- or other non-printable
characters.

While here, sprinkle some EXIT_FAILURE and NOTREACHED where appropriate.
 1.21 29-Nov-2002  lukem convert to using strsuftoll(3) from libc
 1.20 26-Nov-2001  enami a bit more cosmetic changes.
 1.19 25-Nov-2001  lukem - Use u_longlong_t instead of u_quad_t, u_long, or int for various buffer sizes
- Add strsuftoull(), which parses a number into a u_longlong_t, with
multiplication support, and support for 'g' (GB) and 't' (TB) suffices.
If an error occurs, print to stderr and exit.
Based on get_blk() from args.c and strsufto*() (in other programs)
- Add strsuftoullx(), which acts as per strsuftoull() but returns the
error in the supplied buffer instead (if the returned buffer != "", an
error occurred)
- Replace get_bsz() use with strsuftoull()
- Remove (now) unnecessary argument validation
- Remove unused {f,p,s,t}_stats fields in struct IO
 1.18 25-Nov-2001  lukem fix WARNS=2 for -DNO_CONV
 1.17 25-Nov-2001  lukem - ANSI KNF
- WARNS=2 (mainly converting some "char *" -> "const char *")
 1.16 22-Jul-2001  wiz seperate -> separate
 1.15 28-Apr-2001  ross In the words of Our Fearless Leader, I am "utterly sick" of dd(1)
lying to me about transfer rates. Do some fixed point frobbing to get
stuff like: 2486864 bytes transferred in 0.272 secs (9142882 bytes/sec)
 1.14 29-Jul-1999  hubertf Add "progress=1" switch to show a sign of life by printing a '.' for
every block written to the output file.
 1.13 28-Jul-1998  mycroft Delint.
 1.12 09-Jul-1998  msaitoh "static" should be located at the beginning of declaration.
 1.11 28-Feb-1998  enami Lineup `case' label and `default' label with `switch'.
 1.10 28-Feb-1998  enami Put space after the keyword `switch'.
 1.9 25-Jul-1997  phil Do multiply in lseek calls as off_t arithmetic. This allows for seeks
of more than INT_MAX. Removed Check for INT_MAX as maximum seeks.
Fixes PR 2101.
 1.8 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.7 01-Mar-1996  jtc add const qualifier to args and clist tables
 1.6 20-Feb-1996  jtc Re-do change to use const qualifer with conversion tables.

Fixed bug where pre-computed upper/lower case conversion tables were
used. This won't work if user selected a different locale.
 1.5 08-Oct-1995  gwr Add -DNO_CONV to make this smaller in my ramdisk.
 1.4 21-Mar-1995  cgd convert to new RCS id conventions.
 1.3 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.2 01-Aug-1993  mycroft Add RCS identifiers.
 1.1 04-May-1993  glass branches: 1.1.1;
added dd(1) from ftp.uu.net:bsd-sources; torching GNU dd
 1.1.1.1 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.33.2.1 08-Feb-2011  bouyer Sync with HEAD
 1.35.2.2 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.35.2.1 10-Nov-2011  yamt sync with head
 1.37.6.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.38.6.1 26-Mar-2015  martin Pull up following revision(s) (requested by manu in ticket #640):
bin/dd/extern.h: revision 1.23
bin/dd/dd.1: revision 1.26
bin/dd/dd.1: revision 1.27
bin/dd/dd.h: revision 1.16
bin/dd/dd.c: revision 1.50
bin/dd/Makefile: revision 1.18
bin/dd/args.c: revision 1.39
Add iflag and oflag operands to dd(1)

Like GNU dd(1) similar operands, iflag and oflag allow specifying the
O_* flags given to open(2) for the input and the output file. The values
are comma-sepratated, lower-case, O_ prefix-stripped constants documented
in open(2).

Since iflag and oflag override default values, specifying oflag means
O_CREATE is not set by default and must be specified explicitely.
Some values do not make sense (e.g.: iflag=directory) but are still used
and will raise a warning. For oflag, values rdonly, rdwr and wronly are
filtered out with a warning (dd(1) attempts open(2) with O_RDWR and
then O_WRONLY on failure).

Specifying oflag=trunc along with (seek, oseek or conv=notrunc) is
contradictory and will raise an error.

iflag and oflag are disabled if building with -DMALLPROG

New sentence, new line. Fix typos. Bump date for previous.
 1.39.16.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.39.16.2 21-Apr-2020  martin Sync with HEAD
 1.39.16.1 10-Jun-2019  christos Sync with HEAD
 1.17 07-Aug-2003  agc branches: 1.17.98;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.16 05-Aug-2003  erh Include stdlib.h so EXIT_FAILURE is defined.
 1.15 04-Aug-2003  jschauma As discusses a little while back on tech-userlevel:

If stdout is a tty, use vis(3) to print any filenames to prevent garbage
from being printed if the filename contains control- or other non-printable
characters.

While here, sprinkle some EXIT_FAILURE and NOTREACHED where appropriate.
 1.14 26-Nov-2001  lukem Replace {u_,}longlong_t usage with {u,}int64_t, as the former has
"issues" and really shouldn't be used outside of the Sun XDR stuff.
Thanks again to Chuck Silvers for reminding me.
 1.13 25-Nov-2001  lukem - Use u_longlong_t instead of u_quad_t, u_long, or int for various buffer sizes
- Add strsuftoull(), which parses a number into a u_longlong_t, with
multiplication support, and support for 'g' (GB) and 't' (TB) suffices.
If an error occurs, print to stderr and exit.
Based on get_blk() from args.c and strsufto*() (in other programs)
- Add strsuftoullx(), which acts as per strsuftoull() but returns the
error in the supplied buffer instead (if the returned buffer != "", an
error occurred)
- Replace get_bsz() use with strsuftoull()
- Remove (now) unnecessary argument validation
- Remove unused {f,p,s,t}_stats fields in struct IO
 1.12 25-Nov-2001  lukem - ANSI KNF
- WARNS=2 (mainly converting some "char *" -> "const char *")
 1.11 28-Apr-2001  ross In the words of Our Fearless Leader, I am "utterly sick" of dd(1)
lying to me about transfer rates. Do some fixed point frobbing to get
stuff like: 2486864 bytes transferred in 0.272 secs (9142882 bytes/sec)
 1.10 16-Jan-2001  cgd avoid C sequence point issues warned about by development version of gcc.
 1.9 07-Jul-2000  itojun errx?/warnx? audit. do not pass variable alone, use %s. idea from openbsd
 1.8 28-Jul-1998  mycroft branches: 1.8.10;
Delint.
 1.7 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.6 20-Feb-1996  jtc Re-do change to use const qualifer with conversion tables.

Fixed bug where pre-computed upper/lower case conversion tables were
used. This won't work if user selected a different locale.
 1.5 08-Oct-1995  gwr Add -DNO_CONV to make this smaller in my ramdisk.
 1.4 21-Mar-1995  cgd convert to new RCS id conventions.
 1.3 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.2 01-Aug-1993  mycroft Add RCS identifiers.
 1.1 04-May-1993  glass branches: 1.1.1;
added dd(1) from ftp.uu.net:bsd-sources; torching GNU dd
 1.1.1.1 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.8.10.1 27-Jul-2000  itojun pullup (approved by releng-1-5)
printf-like format pedant. do not pass string variable alone. use "%s".
from openbsd.

/cvsroot/basesrc/bin/dd/conv.c 1.8 -> 1.9
/cvsroot/basesrc/bin/cp/cp.c 1.29 -> 1.30
/cvsroot/basesrc/bin/chmod/chmod.c 1.22 -> 1.23
 1.17.98.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.17.98.1 21-Apr-2020  martin Sync with HEAD
 1.10 14-Jan-2022  christos The "ibm" and "oldibm" tables are identical, because POSIX just
standardised the table from V7. Nobody, including the original authors,
seems to have noticed this. Merge them and update the documentation.
Also fix the odd, inconsistent, spelling of "pre-4.3BSD-Reno").
(From nabijaczleweli)
 1.9 07-Aug-2003  agc branches: 1.9.98;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.8 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.7 20-Feb-1996  jtc Re-do change to use const qualifer with conversion tables.

Fixed bug where pre-computed upper/lower case conversion tables were
used. This won't work if user selected a different locale.
 1.6 20-Feb-1996  cgd Previous change (Use const qualifier with conversion tables) is broken.
(1) It won't compile, and (2) there's code in dd.c which actually
does modify (some, all, used?) conversion tables, so they can't _all_ be
const (if any can).
 1.5 19-Feb-1996  jtc Use const qualifier with conversion tables
 1.4 21-Mar-1995  cgd convert to new RCS id conventions.
 1.3 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.2 01-Aug-1993  mycroft Add RCS identifiers.
 1.1 04-May-1993  glass branches: 1.1.1;
added dd(1) from ftp.uu.net:bsd-sources; torching GNU dd
 1.1.1.1 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.9.98.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.9.98.1 21-Apr-2020  martin Sync with HEAD
 1.38 14-Jan-2022  christos The "ibm" and "oldibm" tables are identical, because POSIX just
standardised the table from V7. Nobody, including the original authors,
seems to have noticed this. Merge them and update the documentation.
Also fix the odd, inconsistent, spelling of "pre-4.3BSD-Reno").
(From nabijaczleweli)
 1.37 18-May-2021  nia dd.1: add an example of writing a NetBSD image
 1.36 30-Jan-2019  wiz branches: 1.36.6;
Fix typos; use American spelling consistently. Remove an unnecessary macro.
 1.35 30-Jan-2019  mrg 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.
 1.34 04-Jul-2017  wiz branches: 1.34.6;
Use Ex.
 1.33 03-Jul-2017  wiz Remove workaround for ancient HTML generation code.
 1.32 18-Aug-2016  sevan bump date
 1.31 18-Aug-2016  sevan dd first appeared in V5, not V1
Heads up by Ingo Schwarze
http://www.tuhs.org/cgi-bin/utree.pl?file=V5/usr/source/s1/dd.c
 1.30 11-Aug-2016  sevan Document the version dd first appeared.
Bump date.
 1.29 11-Aug-2016  sevan Pp not required before Bd, picked up by mandoc lint
 1.28 11-Aug-2016  sevan The macro to cross-reference is Xr, not Xt.
Fix typo error picked up by mandoc lint.
 1.27 18-Mar-2015  wiz New sentence, new line. Fix typos. Bump date for previous.
 1.26 18-Mar-2015  manu Add iflag and oflag operands to dd(1)

Like GNU dd(1) similar operands, iflag and oflag allow specifying the
O_* flags given to open(2) for the input and the output file. The values
are comma-sepratated, lower-case, O_ prefix-stripped constants documented
in open(2).

Since iflag and oflag override default values, specifying oflag means
O_CREATE is not set by default and must be specified explicitely.

Some values do not make sense (e.g.: iflag=directory) but are still used
and will raise a warning. For oflag, values rdonly, rdwr and wronly are
filtered out with a warning (dd(1) attempts open(2) with O_RDWR and
then O_WRONLY on failure).

Specifying oflag=trunc along with (seek, oseek or conv=notrunc) is
contradictory and will raise an error.

iflag and oflag are disabled if building with -DMALLPROG
 1.25 20-Jun-2012  wiz branches: 1.25.12;
- sync `SYNOPSIS' with dd.c's `usage' line;
- correct spelling (prepositions).

From Bug Hunting.
 1.24 06-Nov-2011  jym Add a new command to dd(1): msgfmt. The command modifies the
output of the information summary returned by dd(1). This can be used
to specify messages in a more usable (or parseable) format like
human-readable values.

My intent is to re-use this for building image files and quick I/O
benchmarking.

Reviewed by tsutsui@ on tech-userlevel. See also
http://mail-index.netbsd.org/tech-userlevel/2010/12/03/msg004179.html

Some examples:

$ dd if=/dev/zero of=/dev/null bs=1m count=1 msgfmt=human
1+0 records in
1+0 records out
1048576 bytes (1,0 MB) transferred in 0.001 secs (1048576000 bytes/sec - 1,0 GB/sec)

$ dd if=/dev/zero of=/dev/null count=1 msgfmt='
> <speed>%E</speed>
> <time>%s</time>
> <bytes>%b</bytes>
> '
<speed>500 KB/sec</speed>
<time>0.001</time>
<bytes>512</bytes>
 1.23 22-Dec-2010  enami branches: 1.23.6;
Add iseek and oseek option as aliases for skip and seek respectively.
These options exist in dd of solaris or svr3. From FreeBSD.
 1.22 11-Mar-2009  joerg Don't workaround macro argument limit in ancient troff.
 1.21 13-Apr-2007  lukem branches: 1.21.20;
Use IEC 60027-2 prefixes for 2^n based prefixes.
 1.20 12-Jan-2006  wiz Bump date for progress=n description change.
 1.19 09-Jan-2006  apb In "dd progress=N", let the value of N control how often
to print a "." to stderr. Previously, any non-zero
value behaved like "progress=1".

PR 24300

Approved by christos
 1.18 13-Feb-2004  wiz Spell the plural of suffix "suffixes", not "suffices".
Inspired by PR 24400 by Todd Vierling.
 1.17 17-Jan-2004  dbj add support for conv=sparse
inspired by freebsd, although this patch attempts
to avoid some potential bugs in their implementation.
 1.16 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.15 25-Feb-2003  wiz .Nm does not need a dummy argument ("") before punctuation or
for correct formatting of the SYNOPSIS any longer.
 1.14 08-Feb-2002  ross Generate <>& symbolically. I'm avoiding .../dist/... directories for now.
 1.13 25-Nov-2001  wiz Drop a space and an unnecessary .Pp.
 1.12 25-Nov-2001  lukem fix conv= description
 1.11 25-Nov-2001  lukem - Use u_longlong_t instead of u_quad_t, u_long, or int for various buffer sizes
- Add strsuftoull(), which parses a number into a u_longlong_t, with
multiplication support, and support for 'g' (GB) and 't' (TB) suffices.
If an error occurs, print to stderr and exit.
Based on get_blk() from args.c and strsufto*() (in other programs)
- Add strsuftoullx(), which acts as per strsuftoull() but returns the
error in the supplied buffer instead (if the returned buffer != "", an
error occurred)
- Replace get_bsz() use with strsuftoull()
- Remove (now) unnecessary argument validation
- Remove unused {f,p,s,t}_stats fields in struct IO
 1.10 04-Sep-2000  kleink For commands and utilities, use EXIT STATUS rather than RETURN VALUES as
appropriate (and documented in mdoc(7)).
 1.9 28-Aug-2000  hubertf Add 'RETURN VALUE' section header.
 1.8 29-Jul-1999  hubertf branches: 1.8.8;
Add "progress=1" switch to show a sign of life by printing a '.' for
every block written to the output file.
 1.7 06-Feb-1998  perry macroize BSD, NetBSD, FreeBSD and misc cleanup
 1.6 20-Oct-1997  enami Fix .Nm usage.
 1.5 21-Mar-1995  cgd convert to new RCS id conventions.
 1.4 23-Jan-1995  hpeyerl ECBDIC->EBCDIC
 1.3 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.2 01-Aug-1993  mycroft Add RCS indentifiers.
 1.1 04-May-1993  glass branches: 1.1.1;
added dd(1) from ftp.uu.net:bsd-sources; torching GNU dd
 1.1.1.1 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.8.8.1 28-Aug-2000  hubertf Pull up to netbsd-1-5 branch, OK'd by thorpej:

Log Message:
> Add 'RETURN VALUE' section header.

Files & Revisionis:
> cvs rdiff -r1.19 -r1.20 basesrc/bin/cat/cat.1
> cvs rdiff -r1.12 -r1.13 basesrc/bin/chmod/chmod.1
> cvs rdiff -r1.14 -r1.15 basesrc/bin/cp/cp.1
> cvs rdiff -r1.8 -r1.9 basesrc/bin/dd/dd.1
> cvs rdiff -r1.9 -r1.10 basesrc/bin/echo/echo.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/expr/expr.1
> cvs rdiff -r1.25 -r1.26 basesrc/bin/ls/ls.1
> cvs rdiff -r1.10 -r1.11 basesrc/bin/mkdir/mkdir.1
> cvs rdiff -r1.23 -r1.24 basesrc/bin/mt/mt.1
> cvs rdiff -r1.12 -r1.13 basesrc/bin/mv/mv.1
> cvs rdiff -r1.16 -r1.17 basesrc/bin/pwd/pwd.1
> cvs rdiff -r1.9 -r1.10 basesrc/bin/rm/rm.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/rmdir/rmdir.1
> cvs rdiff -r1.35 -r1.36 basesrc/bin/sh/sh.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/sleep/sleep.1
> cvs rdiff -r1.20 -r1.21 basesrc/bin/stty/stty.1
 1.21.20.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.23.6.2 30-Oct-2012  yamt sync with head
 1.23.6.1 10-Nov-2011  yamt sync with head
 1.25.12.1 26-Mar-2015  martin Pull up following revision(s) (requested by manu in ticket #640):
bin/dd/extern.h: revision 1.23
bin/dd/dd.1: revision 1.26
bin/dd/dd.1: revision 1.27
bin/dd/dd.h: revision 1.16
bin/dd/dd.c: revision 1.50
bin/dd/Makefile: revision 1.18
bin/dd/args.c: revision 1.39
Add iflag and oflag operands to dd(1)

Like GNU dd(1) similar operands, iflag and oflag allow specifying the
O_* flags given to open(2) for the input and the output file. The values
are comma-sepratated, lower-case, O_ prefix-stripped constants documented
in open(2).

Since iflag and oflag override default values, specifying oflag means
O_CREATE is not set by default and must be specified explicitely.
Some values do not make sense (e.g.: iflag=directory) but are still used
and will raise a warning. For oflag, values rdonly, rdwr and wronly are
filtered out with a warning (dd(1) attempts open(2) with O_RDWR and
then O_WRONLY on failure).

Specifying oflag=trunc along with (seek, oseek or conv=notrunc) is
contradictory and will raise an error.

iflag and oflag are disabled if building with -DMALLPROG

New sentence, new line. Fix typos. Bump date for previous.
 1.34.6.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.34.6.2 21-Apr-2020  martin Sync with HEAD
 1.34.6.1 10-Jun-2019  christos Sync with HEAD
 1.36.6.1 31-May-2021  cjep sync with head
 1.53 04-Oct-2019  mrg copy libc's swab.c into dd as dd_swab(), and remove the restrict.

our implementation was fine, but the restrict marker is problematic
as gcc 8 is now more strict about checking for restrict issues.

this is the only actual consumer of swab(3) in our tree, though,
besides the test for it. oh well.
 1.52 23-Mar-2019  mlelstv When buffers are at least page sized, explicitely request page alignment.
 1.51 05-Sep-2016  sevan branches: 1.51.14;
Drop main() prototype.
 1.50 18-Mar-2015  manu Add iflag and oflag operands to dd(1)

Like GNU dd(1) similar operands, iflag and oflag allow specifying the
O_* flags given to open(2) for the input and the output file. The values
are comma-sepratated, lower-case, O_ prefix-stripped constants documented
in open(2).

Since iflag and oflag override default values, specifying oflag means
O_CREATE is not set by default and must be specified explicitely.

Some values do not make sense (e.g.: iflag=directory) but are still used
and will raise a warning. For oflag, values rdonly, rdwr and wronly are
filtered out with a warning (dd(1) attempts open(2) with O_RDWR and
then O_WRONLY on failure).

Specifying oflag=trunc along with (seek, oseek or conv=notrunc) is
contradictory and will raise an error.

iflag and oflag are disabled if building with -DMALLPROG
 1.49 21-Feb-2012  matt branches: 1.49.12;
Fix a very old bug. When allocating the buffer and doing just a bs= transfer,
hen we only need a single buffer equal to that blocksize in length.
 1.48 06-Nov-2011  jym Add a new command to dd(1): msgfmt. The command modifies the
output of the information summary returned by dd(1). This can be used
to specify messages in a more usable (or parseable) format like
human-readable values.

My intent is to re-use this for building image files and quick I/O
benchmarking.

Reviewed by tsutsui@ on tech-userlevel. See also
http://mail-index.netbsd.org/tech-userlevel/2010/12/03/msg004179.html

Some examples:

$ dd if=/dev/zero of=/dev/null bs=1m count=1 msgfmt=human
1+0 records in
1+0 records out
1048576 bytes (1,0 MB) transferred in 0.001 secs (1048576000 bytes/sec - 1,0 GB/sec)

$ dd if=/dev/zero of=/dev/null count=1 msgfmt='
> <speed>%E</speed>
> <time>%s</time>
> <bytes>%b</bytes>
> '
<speed>500 KB/sec</speed>
<time>0.001</time>
<bytes>512</bytes>
 1.47 04-Feb-2011  pooka branches: 1.47.4;
Remove the rif/rof options and add rump.dd. This makes usage
consistent with other rump clients. Copying between kernels is
done using the host pipe, e.g.:

dd if=foo rof=bar skip=1 seek=1 => dd if=foo skip=1 | rump.dd of=bar seek=1

Also, the pipe idiom extends to copying between different rump
kernels, e.g.:

env RUMP_SERVER=unix://srv1 rump.dd if=thefile \
| env RUMP_SERVER=unix://srv2 rump.dd of=thefile

Pipe approach suggested by yamt (thanks!)
 1.46 23-Dec-2010  riz branches: 1.46.2;
Fix speling ("deferred," not "defered") as reported in #netbsd-code on
freenode IRC. While I'm here, clean up the wording later in the comment.
 1.45 14-Dec-2010  pooka remove some leftover development garbage
 1.44 22-Nov-2010  pooka Add two new operands: "rif" and "rof". They operate exactly like
"if" and "of" with the exception that the communicate with a rump
kernel instead of the host kernel.

For example, to write stdout to /tmp/file.txt in a rump kernel namespace:
dd rof=/tmp/file.txt

copy /file1 to /file2 inside a rump kernel:
dd rif=/file1 rof=/file2

copy a snippet from /dev/rmd0d on the rump kernel to the host fs:
dd rif=/dev/rmd0d of=save seek=1000 count=3

Eat that, usermode OS.

(I'll document the operands one I have some manpage to refer to
for rump client use).
 1.43 14-Feb-2009  lukem fix -Wsign-compare issues
 1.42 20-Jul-2008  lukem branches: 1.42.6; 1.42.10;
Remove the \n and tabs from the __COPYRIGHT() strings.
 1.41 26-May-2008  christos PR/38755: murray armfield: /bin posix programs missing setprogname and/or
setlocale
 1.40 29-Apr-2007  msaitoh branches: 1.40.10; 1.40.12;
fix typos
 1.39 09-Jan-2006  apb In "dd progress=N", let the value of N control how often
to print a "." to stderr. Previously, any non-zero
value behaved like "progress=1".

PR 24300

Approved by christos
 1.38 20-Apr-2005  rillig Fixed an off-by-one error with ASCII <-> EBCDIC conversion combined with
lcase/ucase. Approved by mrg.
 1.37 17-Jan-2004  dbj minor nit, set out.dbp correctly when constructing final null block
for sparse files.
 1.36 17-Jan-2004  dbj add support for conv=sparse
inspired by freebsd, although this patch attempts
to avoid some potential bugs in their implementation.
 1.35 15-Nov-2003  dsainty Optimise previous change, using fcntl(F_DUPFD) to locate a free descriptor
for us, rather than iterating until satisfied.

Suggested by David Laight.
 1.34 15-Nov-2003  dsainty Avoid corrupting the dd(1) IO streams. This would happen by accidentally
outputting to the files being manipulated by opening a file in the standard IO
descriptor space. In particular, an output file unlucky enough to be sitting
on descriptor 2 (stderr) is certain to be corrupted.

Addresses PR bin/8521, and passes the recently committed regression test
"bin/dd".
 1.33 14-Sep-2003  jschauma Following private discussion with kleink@ and hubertf@ and public discussion
on tech-userlevel@, back out printescaped() functionality.
kleink: ``We sell rope.''
 1.32 20-Aug-2003  jschauma Fix memory leak noted by Hubert Feyrer in private email.
Patch by Hubert Feyrer as well, OK by kleink.
(I'm just fixing it b/c it's my fault to begin with.)
 1.31 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.30 04-Aug-2003  jschauma As discusses a little while back on tech-userlevel:

If stdout is a tty, use vis(3) to print any filenames to prevent garbage
from being printed if the filename contains control- or other non-printable
characters.

While here, sprinkle some EXIT_FAILURE and NOTREACHED where appropriate.
 1.29 04-Sep-2002  enami Add comment about my rev. 1.27 change.
 1.28 03-Sep-2002  tron Ignore EINVAL returned from fsync(2) because it is the expected error code
if we try to use it on a pipe.
 1.27 01-Sep-2002  enami Detect the last nfs write error.
 1.26 10-Jan-2002  lukem Convert some variables that were signed ints before my update to 64 bit sizes
from uint64_t to int64_t. Fixes [bin/15187]
 1.25 26-Nov-2001  enami a bit more cosmetic changes.
 1.24 26-Nov-2001  lukem Replace {u_,}longlong_t usage with {u,}int64_t, as the former has
"issues" and really shouldn't be used outside of the Sun XDR stuff.
Thanks again to Chuck Silvers for reminding me.
 1.23 25-Nov-2001  lukem - Use u_longlong_t instead of u_quad_t, u_long, or int for various buffer sizes
- Add strsuftoull(), which parses a number into a u_longlong_t, with
multiplication support, and support for 'g' (GB) and 't' (TB) suffices.
If an error occurs, print to stderr and exit.
Based on get_blk() from args.c and strsufto*() (in other programs)
- Add strsuftoullx(), which acts as per strsuftoull() but returns the
error in the supplied buffer instead (if the returned buffer != "", an
error occurred)
- Replace get_bsz() use with strsuftoull()
- Remove (now) unnecessary argument validation
- Remove unused {f,p,s,t}_stats fields in struct IO
 1.22 25-Nov-2001  lukem fix WARNS=2 for -DNO_CONV
 1.21 25-Nov-2001  lukem - ANSI KNF
- WARNS=2 (mainly converting some "char *" -> "const char *")
 1.20 28-Apr-2001  ross In the words of Our Fearless Leader, I am "utterly sick" of dd(1)
lying to me about transfer rates. Do some fixed point frobbing to get
stuff like: 2486864 bytes transferred in 0.272 secs (9142882 bytes/sec)
 1.19 25-Oct-2000  kleink KNF nit in previous.
 1.18 16-Oct-2000  kleink Make sure we accept the getopt(3) 'last option' delimiter;
fixes PR standards/11229.
 1.17 25-Aug-2000  matt Fix conv=swab. Don't swab the entire input buffer every time we read
another block, just swab the block we just read.
 1.16 02-Aug-2000  christos rename nset to infoset from jhawk.
 1.15 02-Aug-2000  christos block siginfo during writes.
 1.14 09-Nov-1999  drochner branches: 1.14.4;
Since our gcc doesn't warn about NULL format strings anymore, we can
fix the incorrect err(1, "%s", "") et al.
Closes PR bin/7592 by cgd.
 1.13 29-Jul-1999  hubertf branches: 1.13.4;
Add "progress=1" switch to show a sign of life by printing a '.' for
every block written to the output file.
 1.12 19-Aug-1998  thorpej Add some braces to make egcs happy.
 1.11 28-Jul-1998  mycroft Delint.
 1.10 28-Jul-1998  mycroft Delint.
 1.9 29-Jun-1998  gwr Fix a minor bug that made conv=sync pad partial blocks with garbage,
when it is supposd to pad with zero or space as with osync.
 1.8 01-Apr-1998  kleink Need <time.h> fo time() prototype.
 1.7 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.6 20-Feb-1996  jtc Re-do change to use const qualifer with conversion tables.

Fixed bug where pre-computed upper/lower case conversion tables were
used. This won't work if user selected a different locale.
 1.5 08-Oct-1995  gwr Add -DNO_CONV to make this smaller in my ramdisk.
 1.4 21-Mar-1995  cgd convert to new RCS id conventions.
 1.3 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.2 01-Aug-1993  mycroft Add RCS identifiers.
 1.1 04-May-1993  glass branches: 1.1.1;
added dd(1) from ftp.uu.net:bsd-sources; torching GNU dd
 1.1.1.1 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.13.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.14.4.2 03-Feb-2001  he Pull up revisions 1.18-1.19 (requested by kleink):
Accept the getopt(3) ``last option'' delimiter. Fixes PR#11229.
 1.14.4.1 26-Aug-2000  matt Pull up dd.c 1.17, extern.h 1.10, position 1.9. approved by thorpej:
>fix conv=swap when ibs!=obs
>block SIGINFO when writing
 1.40.12.2 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.40.12.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.40.10.1 04-Jun-2008  yamt sync with head
 1.42.10.1 24-Feb-2012  matt pull in revision 1.49:
Fix a very old bug. When allocating the buffer and doing just a bs= transfer,
hen we only need a single buffer equal to that blocksize in length.
 1.42.6.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.46.2.1 08-Feb-2011  bouyer Sync with HEAD
 1.47.4.2 17-Apr-2012  yamt sync with head
 1.47.4.1 10-Nov-2011  yamt sync with head
 1.49.12.1 26-Mar-2015  martin Pull up following revision(s) (requested by manu in ticket #640):
bin/dd/extern.h: revision 1.23
bin/dd/dd.1: revision 1.26
bin/dd/dd.1: revision 1.27
bin/dd/dd.h: revision 1.16
bin/dd/dd.c: revision 1.50
bin/dd/Makefile: revision 1.18
bin/dd/args.c: revision 1.39
Add iflag and oflag operands to dd(1)

Like GNU dd(1) similar operands, iflag and oflag allow specifying the
O_* flags given to open(2) for the input and the output file. The values
are comma-sepratated, lower-case, O_ prefix-stripped constants documented
in open(2).

Since iflag and oflag override default values, specifying oflag means
O_CREATE is not set by default and must be specified explicitely.
Some values do not make sense (e.g.: iflag=directory) but are still used
and will raise a warning. For oflag, values rdonly, rdwr and wronly are
filtered out with a warning (dd(1) attempts open(2) with O_RDWR and
then O_WRONLY on failure).

Specifying oflag=trunc along with (seek, oseek or conv=notrunc) is
contradictory and will raise an error.

iflag and oflag are disabled if building with -DMALLPROG

New sentence, new line. Fix typos. Bump date for previous.
 1.51.14.4 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.51.14.3 21-Apr-2020  martin Sync with HEAD
 1.51.14.2 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.51.14.1 10-Jun-2019  christos Sync with HEAD
 1.16 18-Mar-2015  manu branches: 1.16.16;
Add iflag and oflag operands to dd(1)

Like GNU dd(1) similar operands, iflag and oflag allow specifying the
O_* flags given to open(2) for the input and the output file. The values
are comma-sepratated, lower-case, O_ prefix-stripped constants documented
in open(2).

Since iflag and oflag override default values, specifying oflag means
O_CREATE is not set by default and must be specified explicitely.

Some values do not make sense (e.g.: iflag=directory) but are still used
and will raise a warning. For oflag, values rdonly, rdwr and wronly are
filtered out with a warning (dd(1) attempts open(2) with O_RDWR and
then O_WRONLY on failure).

Specifying oflag=trunc along with (seek, oseek or conv=notrunc) is
contradictory and will raise an error.

iflag and oflag are disabled if building with -DMALLPROG
 1.15 04-Feb-2011  pooka branches: 1.15.24;
Remove the rif/rof options and add rump.dd. This makes usage
consistent with other rump clients. Copying between kernels is
done using the host pipe, e.g.:

dd if=foo rof=bar skip=1 seek=1 => dd if=foo skip=1 | rump.dd of=bar seek=1

Also, the pipe idiom extends to copying between different rump
kernels, e.g.:

env RUMP_SERVER=unix://srv1 rump.dd if=thefile \
| env RUMP_SERVER=unix://srv2 rump.dd of=thefile

Pipe approach suggested by yamt (thanks!)
 1.14 06-Dec-2010  pooka branches: 1.14.2;
Fix most inopportune typo. from gson & jmmv.
 1.13 22-Nov-2010  pooka Add two new operands: "rif" and "rof". They operate exactly like
"if" and "of" with the exception that the communicate with a rump
kernel instead of the host kernel.

For example, to write stdout to /tmp/file.txt in a rump kernel namespace:
dd rof=/tmp/file.txt

copy /file1 to /file2 inside a rump kernel:
dd rif=/file1 rof=/file2

copy a snippet from /dev/rmd0d on the rump kernel to the host fs:
dd rif=/dev/rmd0d of=save seek=1000 count=3

Eat that, usermode OS.

(I'll document the operands one I have some manpage to refer to
for rump client use).
 1.12 17-Jan-2004  dbj add support for conv=sparse
inspired by freebsd, although this patch attempts
to avoid some potential bugs in their implementation.
 1.11 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.10 10-Jan-2002  lukem Convert some variables that were signed ints before my update to 64 bit sizes
from uint64_t to int64_t. Fixes [bin/15187]
 1.9 26-Nov-2001  lukem Replace {u_,}longlong_t usage with {u,}int64_t, as the former has
"issues" and really shouldn't be used outside of the Sun XDR stuff.
Thanks again to Chuck Silvers for reminding me.
 1.8 25-Nov-2001  lukem - Use u_longlong_t instead of u_quad_t, u_long, or int for various buffer sizes
- Add strsuftoull(), which parses a number into a u_longlong_t, with
multiplication support, and support for 'g' (GB) and 't' (TB) suffices.
If an error occurs, print to stderr and exit.
Based on get_blk() from args.c and strsufto*() (in other programs)
- Add strsuftoullx(), which acts as per strsuftoull() but returns the
error in the supplied buffer instead (if the returned buffer != "", an
error occurred)
- Replace get_bsz() use with strsuftoull()
- Remove (now) unnecessary argument validation
- Remove unused {f,p,s,t}_stats fields in struct IO
 1.7 25-Nov-2001  lukem - ANSI KNF
- WARNS=2 (mainly converting some "char *" -> "const char *")
 1.6 28-Apr-2001  ross In the words of Our Fearless Leader, I am "utterly sick" of dd(1)
lying to me about transfer rates. Do some fixed point frobbing to get
stuff like: 2486864 bytes transferred in 0.272 secs (9142882 bytes/sec)
 1.5 04-Feb-1998  enami Use u_quad_t instead of u_long to define struct STAT.bytes, which
is used to count bytes transfered, so that it won't overflow even if
transfer is more than 4Gbytes.
 1.4 21-Mar-1995  cgd convert to new RCS id conventions.
 1.3 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.2 01-Aug-1993  mycroft Add RCS identifiers.
 1.1 04-May-1993  glass branches: 1.1.1;
added dd(1) from ftp.uu.net:bsd-sources; torching GNU dd
 1.1.1.1 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.14.2.1 08-Feb-2011  bouyer Sync with HEAD
 1.15.24.1 26-Mar-2015  martin Pull up following revision(s) (requested by manu in ticket #640):
bin/dd/extern.h: revision 1.23
bin/dd/dd.1: revision 1.26
bin/dd/dd.1: revision 1.27
bin/dd/dd.h: revision 1.16
bin/dd/dd.c: revision 1.50
bin/dd/Makefile: revision 1.18
bin/dd/args.c: revision 1.39
Add iflag and oflag operands to dd(1)

Like GNU dd(1) similar operands, iflag and oflag allow specifying the
O_* flags given to open(2) for the input and the output file. The values
are comma-sepratated, lower-case, O_ prefix-stripped constants documented
in open(2).

Since iflag and oflag override default values, specifying oflag means
O_CREATE is not set by default and must be specified explicitely.
Some values do not make sense (e.g.: iflag=directory) but are still used
and will raise a warning. For oflag, values rdonly, rdwr and wronly are
filtered out with a warning (dd(1) attempts open(2) with O_RDWR and
then O_WRONLY on failure).

Specifying oflag=trunc along with (seek, oseek or conv=notrunc) is
contradictory and will raise an error.

iflag and oflag are disabled if building with -DMALLPROG

New sentence, new line. Fix typos. Bump date for previous.
 1.16.16.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.16.16.1 21-Apr-2020  martin Sync with HEAD
 1.1 04-Feb-2011  pooka branches: 1.1.2; 1.1.48;
Remove the rif/rof options and add rump.dd. This makes usage
consistent with other rump clients. Copying between kernels is
done using the host pipe, e.g.:

dd if=foo rof=bar skip=1 seek=1 => dd if=foo skip=1 | rump.dd of=bar seek=1

Also, the pipe idiom extends to copying between different rump
kernels, e.g.:

env RUMP_SERVER=unix://srv1 rump.dd if=thefile \
| env RUMP_SERVER=unix://srv2 rump.dd of=thefile

Pipe approach suggested by yamt (thanks!)
 1.1.48.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.48.1 21-Apr-2020  martin Sync with HEAD
 1.1.2.2 08-Feb-2011  bouyer Sync with HEAD
 1.1.2.1 04-Feb-2011  bouyer file dd_hostops.c was added on branch bouyer-quota2 on 2011-02-08 16:18:27 +0000
 1.1 04-Feb-2011  pooka branches: 1.1.2; 1.1.48;
Remove the rif/rof options and add rump.dd. This makes usage
consistent with other rump clients. Copying between kernels is
done using the host pipe, e.g.:

dd if=foo rof=bar skip=1 seek=1 => dd if=foo skip=1 | rump.dd of=bar seek=1

Also, the pipe idiom extends to copying between different rump
kernels, e.g.:

env RUMP_SERVER=unix://srv1 rump.dd if=thefile \
| env RUMP_SERVER=unix://srv2 rump.dd of=thefile

Pipe approach suggested by yamt (thanks!)
 1.1.48.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.48.1 21-Apr-2020  martin Sync with HEAD
 1.1.2.2 08-Feb-2011  bouyer Sync with HEAD
 1.1.2.1 04-Feb-2011  bouyer file dd_rumpops.c was added on branch bouyer-quota2 on 2011-02-08 16:18:27 +0000
 1.2 08-Oct-2021  christos PR/56443: RVP: dd conv=swab doesn't always work
 1.1 04-Oct-2019  mrg branches: 1.1.4;
copy libc's swab.c into dd as dd_swab(), and remove the restrict.

our implementation was fine, but the restrict marker is problematic
as gcc 8 is now more strict about checking for restrict issues.

this is the only actual consumer of swab(3) in our tree, though,
besides the test for it. oh well.
 1.1.4.4 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.4.3 21-Apr-2020  martin Sync with HEAD
 1.1.4.2 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.1.4.1 04-Oct-2019  martin file dd_swab.c was added on branch phil-wifi on 2020-04-13 07:45:05 +0000
 1.25 14-Jan-2022  christos update header too.
 1.24 04-Oct-2019  mrg copy libc's swab.c into dd as dd_swab(), and remove the restrict.

our implementation was fine, but the restrict marker is problematic
as gcc 8 is now more strict about checking for restrict issues.

this is the only actual consumer of swab(3) in our tree, though,
besides the test for it. oh well.
 1.23 18-Mar-2015  manu branches: 1.23.16;
Add iflag and oflag operands to dd(1)

Like GNU dd(1) similar operands, iflag and oflag allow specifying the
O_* flags given to open(2) for the input and the output file. The values
are comma-sepratated, lower-case, O_ prefix-stripped constants documented
in open(2).

Since iflag and oflag override default values, specifying oflag means
O_CREATE is not set by default and must be specified explicitely.

Some values do not make sense (e.g.: iflag=directory) but are still used
and will raise a warning. For oflag, values rdonly, rdwr and wronly are
filtered out with a warning (dd(1) attempts open(2) with O_RDWR and
then O_WRONLY on failure).

Specifying oflag=trunc along with (seek, oseek or conv=notrunc) is
contradictory and will raise an error.

iflag and oflag are disabled if building with -DMALLPROG
 1.22 07-Nov-2011  jym branches: 1.22.20;
As suggested by Enami, rework the msgfmt logic a bit:
- error out when an unknown specifier is used. Do this in f_msgfmt(),
before dd(1) starts operation.
- allow buffer_write() to flush the internal buffer even when NULL is
passed as parameter.

Some whitespace fixes too.
 1.21 06-Nov-2011  jym Add a new command to dd(1): msgfmt. The command modifies the
output of the information summary returned by dd(1). This can be used
to specify messages in a more usable (or parseable) format like
human-readable values.

My intent is to re-use this for building image files and quick I/O
benchmarking.

Reviewed by tsutsui@ on tech-userlevel. See also
http://mail-index.netbsd.org/tech-userlevel/2010/12/03/msg004179.html

Some examples:

$ dd if=/dev/zero of=/dev/null bs=1m count=1 msgfmt=human
1+0 records in
1+0 records out
1048576 bytes (1,0 MB) transferred in 0.001 secs (1048576000 bytes/sec - 1,0 GB/sec)

$ dd if=/dev/zero of=/dev/null count=1 msgfmt='
> <speed>%E</speed>
> <time>%s</time>
> <bytes>%b</bytes>
> '
<speed>500 KB/sec</speed>
<time>0.001</time>
<bytes>512</bytes>
 1.20 16-Sep-2011  joerg branches: 1.20.2;
Use __dead for the small build too
 1.19 29-Aug-2011  joerg Use __dead
 1.18 22-Nov-2010  pooka Add two new operands: "rif" and "rof". They operate exactly like
"if" and "of" with the exception that the communicate with a rump
kernel instead of the host kernel.

For example, to write stdout to /tmp/file.txt in a rump kernel namespace:
dd rof=/tmp/file.txt

copy /file1 to /file2 inside a rump kernel:
dd rif=/file1 rof=/file2

copy a snippet from /dev/rmd0d on the rump kernel to the host fs:
dd rif=/dev/rmd0d of=save seek=1000 count=3

Eat that, usermode OS.

(I'll document the operands one I have some manpage to refer to
for rump client use).
 1.17 09-Jan-2006  apb In "dd progress=N", let the value of N control how often
to print a "." to stderr. Previously, any non-zero
value behaved like "progress=1".

PR 24300

Approved by christos
 1.16 14-Sep-2003  jschauma Following private discussion with kleink@ and hubertf@ and public discussion
on tech-userlevel@, back out printescaped() functionality.
kleink: ``We sell rope.''
 1.15 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.14 04-Aug-2003  jschauma As discusses a little while back on tech-userlevel:

If stdout is a tty, use vis(3) to print any filenames to prevent garbage
from being printed if the filename contains control- or other non-printable
characters.

While here, sprinkle some EXIT_FAILURE and NOTREACHED where appropriate.
 1.13 26-Nov-2001  lukem Replace {u_,}longlong_t usage with {u,}int64_t, as the former has
"issues" and really shouldn't be used outside of the Sun XDR stuff.
Thanks again to Chuck Silvers for reminding me.
 1.12 25-Nov-2001  lukem - Use u_longlong_t instead of u_quad_t, u_long, or int for various buffer sizes
- Add strsuftoull(), which parses a number into a u_longlong_t, with
multiplication support, and support for 'g' (GB) and 't' (TB) suffices.
If an error occurs, print to stderr and exit.
Based on get_blk() from args.c and strsufto*() (in other programs)
- Add strsuftoullx(), which acts as per strsuftoull() but returns the
error in the supplied buffer instead (if the returned buffer != "", an
error occurred)
- Replace get_bsz() use with strsuftoull()
- Remove (now) unnecessary argument validation
- Remove unused {f,p,s,t}_stats fields in struct IO
 1.11 25-Nov-2001  lukem - ANSI KNF
- WARNS=2 (mainly converting some "char *" -> "const char *")
 1.10 02-Aug-2000  christos block siginfo during writes.
 1.9 29-Jul-1999  hubertf branches: 1.9.8;
Add "progress=1" switch to show a sign of life by printing a '.' for
every block written to the output file.
 1.8 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.7 20-Feb-1996  jtc Re-do change to use const qualifer with conversion tables.

Fixed bug where pre-computed upper/lower case conversion tables were
used. This won't work if user selected a different locale.
 1.6 20-Feb-1996  cgd Previous change (Use const qualifier with conversion tables) is broken.
(1) It won't compile, and (2) there's code in dd.c which actually
does modify (some, all, used?) conversion tables, so they can't _all_ be
const (if any can).
 1.5 19-Feb-1996  jtc Use const qualifier with conversion tables
 1.4 21-Mar-1995  cgd convert to new RCS id conventions.
 1.3 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.2 01-Aug-1993  mycroft Add RCS identifiers.
 1.1 04-May-1993  glass branches: 1.1.1;
added dd(1) from ftp.uu.net:bsd-sources; torching GNU dd
 1.1.1.1 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.9.8.1 26-Aug-2000  matt Pull up dd.c 1.17, extern.h 1.10, position 1.9. approved by thorpej:
>fix conv=swap when ibs!=obs
>block SIGINFO when writing
 1.20.2.1 10-Nov-2011  yamt sync with head
 1.22.20.1 26-Mar-2015  martin Pull up following revision(s) (requested by manu in ticket #640):
bin/dd/extern.h: revision 1.23
bin/dd/dd.1: revision 1.26
bin/dd/dd.1: revision 1.27
bin/dd/dd.h: revision 1.16
bin/dd/dd.c: revision 1.50
bin/dd/Makefile: revision 1.18
bin/dd/args.c: revision 1.39
Add iflag and oflag operands to dd(1)

Like GNU dd(1) similar operands, iflag and oflag allow specifying the
O_* flags given to open(2) for the input and the output file. The values
are comma-sepratated, lower-case, O_ prefix-stripped constants documented
in open(2).

Since iflag and oflag override default values, specifying oflag means
O_CREATE is not set by default and must be specified explicitely.
Some values do not make sense (e.g.: iflag=directory) but are still used
and will raise a warning. For oflag, values rdonly, rdwr and wronly are
filtered out with a warning (dd(1) attempts open(2) with O_RDWR and
then O_WRONLY on failure).

Specifying oflag=trunc along with (seek, oseek or conv=notrunc) is
contradictory and will raise an error.

iflag and oflag are disabled if building with -DMALLPROG

New sentence, new line. Fix typos. Bump date for previous.
 1.23.16.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.23.16.2 21-Apr-2020  martin Sync with HEAD
 1.23.16.1 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.26 09-Oct-2021  rillig dd: clean up function summary

No functional change. Ideally the binary would have stayed the same, but
GCC 10.3.0 inserts a nop and reorders the code.
 1.25 09-Oct-2021  rillig dd: fix lint error about 'return expr' from void function

No change to the resulting binary. The 'return' statements are necessary
for GCC to generate the exact same object code, even though they can be
removed without affecting the functionality, as seen before the 'else'.
 1.24 10-Sep-2021  rillig bin: remove unnecessary lint comment CONSTCOND

Since 2021-01-31, lint no longer warns about 'do ... while (0)'.

No functional change.
 1.23 07-Nov-2011  jym branches: 1.23.42;
As suggested by Enami, rework the msgfmt logic a bit:
- error out when an unknown specifier is used. Do this in f_msgfmt(),
before dd(1) starts operation.
- allow buffer_write() to flush the internal buffer even when NULL is
passed as parameter.

Some whitespace fixes too.
 1.22 06-Nov-2011  jym Add a new command to dd(1): msgfmt. The command modifies the
output of the information summary returned by dd(1). This can be used
to specify messages in a more usable (or parseable) format like
human-readable values.

My intent is to re-use this for building image files and quick I/O
benchmarking.

Reviewed by tsutsui@ on tech-userlevel. See also
http://mail-index.netbsd.org/tech-userlevel/2010/12/03/msg004179.html

Some examples:

$ dd if=/dev/zero of=/dev/null bs=1m count=1 msgfmt=human
1+0 records in
1+0 records out
1048576 bytes (1,0 MB) transferred in 0.001 secs (1048576000 bytes/sec - 1,0 GB/sec)

$ dd if=/dev/zero of=/dev/null count=1 msgfmt='
> <speed>%E</speed>
> <time>%s</time>
> <bytes>%b</bytes>
> '
<speed>500 KB/sec</speed>
<time>0.001</time>
<bytes>512</bytes>
 1.21 05-Oct-2007  lukem branches: 1.21.28;
Convert to using raise_default_signal(3).
 1.20 03-Jul-2007  lukem branches: 1.20.4;
In the SIGINT handler, explicitly display a summary and then raise the
default SIGINT handler to exit, rather than exit(0) and relying upon the
atexit() to display the summary.
Per SUS6.
 1.19 17-Jan-2004  dbj add support for conv=sparse
inspired by freebsd, although this patch attempts
to avoid some potential bugs in their implementation.
 1.18 04-Jan-2004  jschauma remove unused code left from printescaped() backput as pointed out by Jeff
Ito in PR bin/23969 and PR bin/23970.
 1.17 14-Sep-2003  jschauma Following private discussion with kleink@ and hubertf@ and public discussion
on tech-userlevel@, back out printescaped() functionality.
kleink: ``We sell rope.''
 1.16 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.15 04-Aug-2003  jschauma As discusses a little while back on tech-userlevel:

If stdout is a tty, use vis(3) to print any filenames to prevent garbage
from being printed if the filename contains control- or other non-printable
characters.

While here, sprinkle some EXIT_FAILURE and NOTREACHED where appropriate.
 1.14 26-Nov-2001  enami a bit more cosmetic changes.
 1.13 25-Nov-2001  lukem - Use u_longlong_t instead of u_quad_t, u_long, or int for various buffer sizes
- Add strsuftoull(), which parses a number into a u_longlong_t, with
multiplication support, and support for 'g' (GB) and 't' (TB) suffices.
If an error occurs, print to stderr and exit.
Based on get_blk() from args.c and strsufto*() (in other programs)
- Add strsuftoullx(), which acts as per strsuftoull() but returns the
error in the supplied buffer instead (if the returned buffer != "", an
error occurred)
- Replace get_bsz() use with strsuftoull()
- Remove (now) unnecessary argument validation
- Remove unused {f,p,s,t}_stats fields in struct IO
 1.12 25-Nov-2001  lukem - ANSI KNF
- WARNS=2 (mainly converting some "char *" -> "const char *")
 1.11 28-Apr-2001  ross In the words of Our Fearless Leader, I am "utterly sick" of dd(1)
lying to me about transfer rates. Do some fixed point frobbing to get
stuff like: 2486864 bytes transferred in 0.272 secs (9142882 bytes/sec)
 1.10 04-Jan-2001  lukem use more standard %ll_ in favour of %q_
 1.9 29-Jul-1999  hubertf Add "progress=1" switch to show a sign of life by printing a '.' for
every block written to the output file.
 1.8 28-Jul-1998  mycroft Delint.
 1.7 06-Feb-1998  thorpej quads must be cast to long long for printing.
 1.6 04-Feb-1998  enami Use u_quad_t instead of u_long to define struct STAT.bytes, which
is used to count bytes transfered, so that it won't overflow even if
transfer is more than 4Gbytes.
 1.5 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.4 21-Mar-1995  cgd convert to new RCS id conventions.
 1.3 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.2 01-Aug-1993  mycroft Add RCS identifiers.
 1.1 04-May-1993  glass branches: 1.1.1;
added dd(1) from ftp.uu.net:bsd-sources; torching GNU dd
 1.1.1.1 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.20.4.1 06-Nov-2007  matt sync with HEAD
 1.21.28.1 10-Nov-2011  yamt sync with head
 1.23.42.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.23.42.1 21-Apr-2020  martin Sync with HEAD
 1.18 22-Nov-2010  pooka branches: 1.18.48;
Add two new operands: "rif" and "rof". They operate exactly like
"if" and "of" with the exception that the communicate with a rump
kernel instead of the host kernel.

For example, to write stdout to /tmp/file.txt in a rump kernel namespace:
dd rof=/tmp/file.txt

copy /file1 to /file2 inside a rump kernel:
dd rif=/file1 rof=/file2

copy a snippet from /dev/rmd0d on the rump kernel to the host fs:
dd rif=/dev/rmd0d of=save seek=1000 count=3

Eat that, usermode OS.

(I'll document the operands one I have some manpage to refer to
for rump client use).
 1.17 14-Feb-2009  lukem fix -Wsign-compare issues
 1.16 14-Sep-2003  jschauma branches: 1.16.40;
Following private discussion with kleink@ and hubertf@ and public discussion
on tech-userlevel@, back out printescaped() functionality.
kleink: ``We sell rope.''
 1.15 20-Aug-2003  kleink Minor KNF nit from rev. 1.13.
 1.14 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.13 04-Aug-2003  jschauma As discusses a little while back on tech-userlevel:

If stdout is a tty, use vis(3) to print any filenames to prevent garbage
from being printed if the filename contains control- or other non-printable
characters.

While here, sprinkle some EXIT_FAILURE and NOTREACHED where appropriate.
 1.12 26-Nov-2001  enami a bit more cosmetic changes.
 1.11 25-Nov-2001  lukem - ANSI KNF
- WARNS=2 (mainly converting some "char *" -> "const char *")
 1.10 28-Apr-2001  ross In the words of Our Fearless Leader, I am "utterly sick" of dd(1)
lying to me about transfer rates. Do some fixed point frobbing to get
stuff like: 2486864 bytes transferred in 0.272 secs (9142882 bytes/sec)
 1.9 02-Aug-2000  christos block siginfo during writes.
 1.8 09-Oct-1999  jtk branches: 1.8.6;
fix comment regarding when we attempt to seek
 1.7 04-Oct-1999  mycroft Use lseek(2) on character devices, damn it.
 1.6 25-Jul-1997  phil branches: 1.6.4; 1.6.6;
Do multiply in lseek calls as off_t arithmetic. This allows for seeks
of more than INT_MAX. Removed Check for INT_MAX as maximum seeks.
Fixes PR 2101.
 1.5 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.4 21-Mar-1995  cgd convert to new RCS id conventions.
 1.3 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.2 01-Aug-1993  mycroft Add RCS identifiers.
 1.1 04-May-1993  glass branches: 1.1.1;
added dd(1) from ftp.uu.net:bsd-sources; torching GNU dd
 1.1.1.1 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.6.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.6.4.1 30-Apr-2000  he Pull up revisions 1.7-1.8 (requested by jhawk):
Correctly lseek() on non-tape character devices (e.g. disks)
as the manual page documents.
 1.8.6.1 26-Aug-2000  matt Pull up dd.c 1.17, extern.h 1.10, position 1.9. approved by thorpej:
>fix conv=swap when ibs!=obs
>block SIGINFO when writing
 1.16.40.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.18.48.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.18.48.1 21-Apr-2020  martin Sync with HEAD
 1.9 29-Nov-2002  lukem convert to using strsuftoll(3) from libc
 1.8 09-Jun-2002  matt Add LL to 1099511627776 since it won't fit in a long on ILP32 systems.
 1.7 31-Jan-2002  tv branches: 1.7.2;
Roll in fixes to permit cross-compiling from non-NetBSD hosts. This
round has been tested on Solaris/x86 and Linux hosts.

* Add host tools cap_mkdb, ctags, m4, uudecode.
* Protect __RCSID() and __COPYRIGHT() better.
* Reduce the number of places that need to include "config.h", to keep
sources closer to their "vanilla" versions.
* Add more compat #defines and autoconf-checked functions.
 1.6 29-Jan-2002  tv Blah, oversight. Revert previous; clean up to proper syntax, and add
conditional for <err.h>.
 1.5 29-Jan-2002  tv This doesn't appear to be used in src/tools any longer; remove the config.h
glue bits.
 1.4 22-Jan-2002  is add config.h when compiled as host tool.
 1.3 30-Nov-2001  thorpej Deal with LP64 printf format issues.
 1.2 26-Nov-2001  lukem Replace {u_,}longlong_t usage with {u,}int64_t, as the former has
"issues" and really shouldn't be used outside of the Sun XDR stuff.
Thanks again to Chuck Silvers for reminding me.
 1.1 25-Nov-2001  lukem - Use u_longlong_t instead of u_quad_t, u_long, or int for various buffer sizes
- Add strsuftoull(), which parses a number into a u_longlong_t, with
multiplication support, and support for 'g' (GB) and 't' (TB) suffices.
If an error occurs, print to stderr and exit.
Based on get_blk() from args.c and strsufto*() (in other programs)
- Add strsuftoullx(), which acts as per strsuftoull() but returns the
error in the supplied buffer instead (if the returned buffer != "", an
error occurred)
- Replace get_bsz() use with strsuftoull()
- Remove (now) unnecessary argument validation
- Remove unused {f,p,s,t}_stats fields in struct IO
 1.7.2.1 23-Jul-2004  tron Apply patch (requested by dbj in ticket 1724):
Fix problems when building the netbsd-1-6 branch with GCC 3.3.3.
 1.3 29-Nov-2002  lukem convert to using strsuftoll(3) from libc
 1.2 26-Nov-2001  lukem Replace {u_,}longlong_t usage with {u,}int64_t, as the former has
"issues" and really shouldn't be used outside of the Sun XDR stuff.
Thanks again to Chuck Silvers for reminding me.
 1.1 25-Nov-2001  lukem - Use u_longlong_t instead of u_quad_t, u_long, or int for various buffer sizes
- Add strsuftoull(), which parses a number into a u_longlong_t, with
multiplication support, and support for 'g' (GB) and 't' (TB) suffices.
If an error occurs, print to stderr and exit.
Based on get_blk() from args.c and strsufto*() (in other programs)
- Add strsuftoullx(), which acts as per strsuftoull() but returns the
error in the supplied buffer instead (if the returned buffer != "", an
error occurred)
- Replace get_bsz() use with strsuftoull()
- Remove (now) unnecessary argument validation
- Remove unused {f,p,s,t}_stats fields in struct IO
 1.19 28-Aug-2011  christos branches: 1.19.44;
- static/__dead
- use strpct from libutil
- fix off by one in format
 1.18 14-Aug-2011  christos Document non-literal formats
 1.17 14-Feb-2009  lukem Default to WARNS=4 (except for rcp & ksh)
 1.16 04-Mar-2008  christos branches: 1.16.10;
PR/36817: Zafer Aydogan: Don't use -g to implement the same named option
in solaris to display statvfs output, use -G instead. I researched this
and all other implementations except Solaris (OS/X, FreeBSD, us before June
24, 2007) use -g to signify gigabytes. So revert to that.
PR/38154: YAMAMOTO Takashi: df -P doesn't work without -k. Fix from Anon Ymous
together with passing lint, and exiting with the right values.
 1.15 19-Aug-2002  lukem branches: 1.15.24; 1.15.30;
Use ${NETBSDSRCDIR}/some/path instead of ${.CURDIR}/../../some/path (etc).
 1.14 02-Aug-2002  soren Following FreeBSD's example, remove the old pre-statfs(2) code for
examining FFS filesystems directly. This also gets rid of df(1)'s
setgid operator bit.
 1.13 08-Apr-1998  fair Fix PR#3590 - remove floating point calculation from df to avoid faulting
on machines that don't yet have complete FP support (e.g. MC68LC040)
 1.12 20-Jul-1997  christos Remove WARNS=1 from all the subdirectory Makefiles, and add it to Makefile.inc
now that all /bin has been cleaned.
 1.11 20-Jul-1997  thorpej Build with WARNS
 1.10 09-Jan-1997  tls Sync to 4.4BSD-Lite2; really just the SCCS ID, because we don't have vfslist.c in our mount sources.
 1.9 21-Mar-1995  cgd convert to new RCS id conventions.
 1.8 16-Sep-1994  mycroft Deal with large file systems, and clean up a bit.
 1.7 28-Jan-1994  cgd branches: 1.7.2;
don't need -lutil
 1.6 09-Aug-1993  mycroft Minor tweak for cross-compiling.
 1.5 06-Aug-1993  mycroft Incorporate new version using getbsize(), with -k flag added.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.3 09-Jan-1997  tls Import from 4.4BSD-Lite
 1.1.1.2 07-Sep-1995  jtc imported from 44lite2
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.7.2.1 16-Sep-1994  cgd From trunk, per mycroft.
 1.15.30.1 24-Mar-2008  keiichi sync with head.
 1.15.24.1 23-Mar-2008  matt sync with HEAD
 1.16.10.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.19.44.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.19.44.1 21-Apr-2020  martin Sync with HEAD
 1.60 03-Aug-2024  kre Add -M and -q options to df(1).

-q suppresses warning output (rather than redirecting stderr, which
also buries error messages, which -q does not).

-M requires each arg given to be the name of a mount point, and
issues a warning, and ignores the arg, if it is not. This allows
scripts (etc) to have a whole list of places for which to produce df
output, while only listing the ones that are actually mounted (rather
than simply listing the filesystem containing the mount point instead -
perhaps several times). (If there are no args, -M is a no-op).

If the options aren't given, nothing alters.
 1.59 03-Aug-2024  kre Typo correction: s/givem/given/.
 1.58 09-Aug-2022  wiz branches: 1.58.4;
Sort options, fix macro, add RCS Id.
 1.57 08-Aug-2022  kre Add 4 new flags:
-b (from FreeBSD) - set blocksize to blocks (512 bytes)
(overrides a contrary setting in BLOCKSIZE)
-H (from FreeBSD and Linux): -h using SI units (powers of 10). Ugh.
-N suppress the header line (except with -P which requires it).
-f show only free space (or inodes) in a minimal format (implies -N)
(that is, with one file[system] specified, print 1 number only)
With -c, show only the total.
Intended to be useful for scripting (aka, I needed it.)

While here, improve the usage message (group options where they apply,
there is no reason, for example, that -g should be shown differently
to -k -m ..., and those options aren't at all useful with -G)

Update the man page to match.
 1.56 06-Jan-2021  ginsbach df: bump man page date
 1.55 03-Jan-2021  ginsbach df: add grand total option

Add a grand total option, -c, similar to the du(1) -c option. Adapted from
the same option (-c) in FreeBSD df(1).
 1.54 23-Sep-2019  christos we don't need root anymore.
 1.53 20-Sep-2019  wiz file system police. Fix typo. Fix macro use.
 1.52 18-Sep-2019  christos It is not just root, it is device read access (kre)
 1.51 18-Sep-2019  christos mention that -W needs root.
 1.50 18-Sep-2019  christos Print the wedge name with -W instead of mntfrom
 1.49 26-Aug-2018  sevan -G cannot be specified alongside -i or -P.

Heads up by <leot>
 1.48 26-Aug-2018  sevan Match SYNOPSIS with usage()
 1.47 03-Jul-2017  wiz branches: 1.47.4; 1.47.6;
Remove workaround for ancient HTML generation code.
 1.46 10-Aug-2016  sevan branches: 1.46.6;
Bump date
Reminded by wiz@
 1.45 10-Aug-2016  sevan df was there from v1.
Confirmed from the TUHS & cat-v.org hosted copies of man pages.
 1.44 05-Apr-2010  joerg \\ -> \e
 1.43 04-Mar-2008  christos sort options from anon ymous
 1.42 04-Mar-2008  christos PR/36817: Zafer Aydogan: Don't use -g to implement the same named option
in solaris to display statvfs output, use -G instead. I researched this
and all other implementations except Solaris (OS/X, FreeBSD, us before June
24, 2007) use -g to signify gigabytes. So revert to that.
PR/38154: YAMAMOTO Takashi: df -P doesn't work without -k. Fix from Anon Ymous
together with passing lint, and exiting with the right values.
 1.41 24-Jun-2007  christos branches: 1.41.4; 1.41.8;
PR/36541: Greg A. Woods: fix -P option, and add solaris -g option.
 1.40 28-Jan-2007  wiz Improve wording of -a description. From FreeBSD via Zafer Aydogan.
 1.39 18-Nov-2005  christos PR/32103: Zafer Aydogan: Fix references to obsolete fstatfs and getfsstat
 1.38 04-Jun-2004  cjep Mention -g in synopsis. PR#25800 from Kouichirou Hiratsuka.
 1.37 21-Apr-2004  wiz Refer statvfs instead of removed statfs.
 1.36 10-Jan-2004  wiz branches: 1.36.2;
Replace hard to understand wording with a different
one from further down in the same page. Suggested by jmc@openbsd.
 1.35 08-Jan-2004  wiz comma-separated with a "-" in between. From Jared Yanovich via jmc@openbsd.
 1.34 07-Dec-2003  perry mention that -h also overrides BLOCKSIZE, and sort around there
 1.33 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.32 18-Apr-2003  grant add -g to ENVIRONMENT.
 1.31 18-Apr-2003  grant change -G arg to -g.

-G was chosen as SuSv2 specified -g was already used in ls(1), but ls
has no blocksize options like df/du, so this was redundant.

bump date in man page.
 1.30 16-Apr-2003  grant add -G argument for output in gigabytes.

ok'd by perry@, mrg@
 1.29 16-Apr-2003  grant grammar fix.
 1.28 29-Mar-2003  perry matche->match, from Igor Sobrado in PR 19670
 1.27 26-Sep-2002  provos human-readable output via -h flag. output matches Linux and other BSDs.
partly from kstailey@openbsd.org. approved itojun and perry.
 1.26 25-Sep-2002  wiz New policy: New sentences start on a new line.
Patches by Robert Elz <kre at munnari oz au>, with minimal changes by me.
 1.25 08-Feb-2002  ross Generate <>& symbolically. I'm avoiding .../dist/... directories for now.
 1.24 20-Dec-2001  wiz Use standard headers, sort SEE ALSO.
 1.23 05-Nov-2001  jdolecek put -l explanation before -m, so that the options are in alphabet order
Fixes misc/14471 by Hiramatsu Yoshifumi.
 1.22 20-Aug-2001  wiz precede, not preceed.
 1.21 08-Jul-2001  simonb Xref getbsize(3).
 1.20 30-Dec-2000  hubertf Add -m option, similar to -k, but display output in 1024*1024 byte units.

Patch submitted by Chris Pinnock <C.J.E.Pinnock@qmw.ac.uk> in PR 11849,
adjusted for NetBSD-current by me.
 1.19 15-Oct-2000  kleink Support the 1003.2-92 -P flag for a `portable output format'; based on
Ben Harris' patch in standards/11208 but extended about output alignment
and documentation.
 1.18 26-Jun-2000  christos Add -a flag now that we have support for MNT_IGNORE
 1.17 22-Mar-1999  garbled More and more .Os cleanups. .Os is defined in the tmac.doc-common file,
so we shouldn't override it with versions in the manpages. Many more to
come.
 1.16 01-Feb-1999  bouyer tunefs lives in section 8, not 1.
 1.15 01-Feb-1999  cjs Close PR 6919, submitted by Jaromir Dolecek <jdolecek@per4mance.cz>.
 1.14 20-Oct-1997  enami branches: 1.14.2;
Fix .Nm usage.
 1.13 11-Dec-1996  thorpej Make df(1) more liberal (and intelligent) about what it accepts
as arguments (for -t and -l flags, specifically).
From Hubert Feyrer <feyrer@rfhs8002.fh-regensburg.de>, PR #2869.
 1.12 05-Dec-1995  jtc ENVIRONMENTAL -> ENVIRONMENT
 1.11 25-Jul-1995  jtc Use "utility" instead of "command". Modern definitions of these terms
are distinct (See POSIX.2 glossary).

A utility is a executable, script or shell builtin; while a command
can be any of those things plus lists, pipelines, compound commands
(if, for, while) and shell function definitions.
 1.10 21-Mar-1995  cgd convert to new RCS id conventions.
 1.9 30-Jan-1995  mycroft Document the `-t' option as in umount(8).
 1.8 13-Jan-1995  mycroft Add `-t' option from Matthew Green, hacked a bit by me.
 1.7 16-Sep-1994  mycroft Deal with large file systems, and clean up a bit.
 1.6 11-Mar-1994  mycroft branches: 1.6.2;
Add `-l' option.
 1.5 06-Aug-1993  mycroft Incorporate new version using getbsize(), with -k flag added.
 1.4 01-Aug-1993  mycroft Add RCS indentifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.3 09-Jan-1997  tls Import from 4.4BSD-Lite
 1.1.1.2 07-Sep-1995  jtc imported from 44lite2
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.6.2.1 16-Sep-1994  cgd From trunk, per mycroft.
 1.14.2.1 03-Feb-1999  cgd pull up revs 1.15-1.16 from trunk (bouyer)
 1.36.2.1 05-Jun-2004  jmc Pullup patch (requested by cjep in ticket #439)

Mention -g in synopsis. PR#25800.
 1.41.8.1 24-Mar-2008  keiichi sync with head.
 1.41.4.1 23-Mar-2008  matt sync with HEAD
 1.46.6.1 28-Aug-2018  martin Pull up following revision(s) (requested by sevan in ticket #992):

external/bsd/top/dist/top.1.in: revision 1.11
sbin/gpt/main.c: revision 1.12
sbin/amrctl/amrctl.c: revision 1.11
bin/df/df.c: revision 1.93
sbin/fsck_ext2fs/fsck_ext2fs.8: revision 1.21
sbin/fsck_ext2fs/main.c: revision 1.38
bin/ksh/ksh.Man: revision 1.26
bin/ln/ln.c: revision 1.40
bin/df/df.1: revision 1.48
bin/df/df.1: revision 1.49

Document the WCPU field.

Match SYNOPSIS with usage()

-G cannot be specified alongside -i or -P.
Heads up by <leot>

Add -l to SYNOPSIS

Update usage to include -w

Match sequence as per SYNOPSIS in manual

Remove reference to -c flag which was never implemented.

Remove references to -c flag which was never included.

Add the -T flag to usage()
 1.47.6.4 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.47.6.3 21-Apr-2020  martin Sync with HEAD
 1.47.6.2 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.47.6.1 10-Jun-2019  christos Sync with HEAD
 1.47.4.1 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.58.4.1 02-Aug-2025  perseant Sync with HEAD
 1.104 06-Oct-2024  christos PR/58718: Christof Meerwald: "total files" and "free files" values swapped
in df -G output
 1.103 03-Aug-2024  kre Add -M and -q options to df(1).

-q suppresses warning output (rather than redirecting stderr, which
also buries error messages, which -q does not).

-M requires each arg given to be the name of a mount point, and
issues a warning, and ignores the arg, if it is not. This allows
scripts (etc) to have a whole list of places for which to produce df
output, while only listing the ones that are actually mounted (rather
than simply listing the filesystem containing the mount point instead -
perhaps several times). (If there are no args, -M is a no-op).

If the options aren't given, nothing alters.
 1.102 18-Dec-2023  kre branches: 1.102.2;
Correctly compute the "Filesystem" column with when using -W
(that added "NAME=" occupies space!)

XXX pullup -10
 1.101 09-Aug-2022  wiz branches: 1.101.2;
Sync usage with man page.
 1.100 08-Aug-2022  kre Add 4 new flags:
-b (from FreeBSD) - set blocksize to blocks (512 bytes)
(overrides a contrary setting in BLOCKSIZE)
-H (from FreeBSD and Linux): -h using SI units (powers of 10). Ugh.
-N suppress the header line (except with -P which requires it).
-f show only free space (or inodes) in a minimal format (implies -N)
(that is, with one file[system] specified, print 1 number only)
With -c, show only the total.
Intended to be useful for scripting (aka, I needed it.)

While here, improve the usage message (group options where they apply,
there is no reason, for example, that -g should be shown differently
to -k -m ..., and those options aren't at all useful with -G)

Update the man page to match.
 1.99 29-Nov-2021  simonb Set totals.f_frsize to DEV_BSIZE instead of totals.f_bsize so that
addstat() uses an initialised value for total size calculations.
Fixes core dump for "df -c".
 1.98 03-Jan-2021  ginsbach df: add grand total option

Add a grand total option, -c, similar to the du(1) -c option. Adapted from
the same option (-c) in FreeBSD df(1).
 1.97 21-Aug-2020  ryo - Bump Size/Used/Avail from 10 to 12, and iUsed/iAvail from 8 to 10 for double-digit terabyte storage.
- set the field width to the optimal, if "-h" is specified.
- if blocksize is greater than M or G, reduce the size field width.
 1.96 21-Aug-2020  ryo field width of blocksize and inode can be changed by #define
 1.95 22-Sep-2019  christos Add a new member to struct vfsstat and grow the unused members
The new member is caled f_mntfromlabel and it is the dkw_wname
of the corresponding wedge. This is now used by df -W to display
the mountpoint name as NAME=
 1.94 18-Sep-2019  christos Print the wedge name with -W instead of mntfrom
 1.93 26-Aug-2018  sevan -G cannot be specified alongside -i or -P.

Heads up by <leot>
 1.92 05-Mar-2016  kamil branches: 1.92.8; 1.92.14; 1.92.16;
Correct display of df(1) with the -i parameter

Now the "Mounted on" column should be aligned with /entries.

Patch by Michal Mazurek.
 1.91 12-Nov-2015  christos PR/50422: Robert Elz: df -G prints the wrong value for fragsize (+FIX)
For df -G, print the block and fragment size instead of the iosize
and the blocksize. If we need to print the iosize, it should be done
in a different field. Nevertheless printing the blocksize in the fragment
size field is just wrong.
XXX: pullup-6, pullup-7
 1.90 07-Jan-2012  christos use strspct.
 1.89 29-Aug-2011  gson branches: 1.89.2;
Use the same column spacing for the -h output as with the normal output,
as the same header line format is used in both cases and it can't line
up correctly with both of them otherwise.
 1.88 29-Aug-2011  enami No need to print internal state once debug is done.
 1.87 28-Aug-2011  christos - static/__dead
- use strpct from libutil
- fix off by one in format
 1.86 06-Jun-2009  mlelstv block numbers are measured in f_frsize units. Make -P option
use this instead of f_bsize. Also account for reserved blocks
like normal non-P output.
Fixes PR bin/41541.
 1.85 25-Jan-2009  lukem sign-compare fix for amd64
 1.84 18-Jan-2009  lukem fix -Wsign-compare issues
 1.83 20-Jul-2008  lukem branches: 1.83.4; 1.83.8;
Remove the \n and tabs from the __COPYRIGHT() strings.
 1.82 26-May-2008  christos PR/38755: murray armfield: /bin posix programs missing setprogname and/or
setlocale
 1.81 04-Mar-2008  christos branches: 1.81.2; 1.81.4;
do the -g -G swap for real.
 1.80 04-Mar-2008  christos PR/36817: Zafer Aydogan: Don't use -g to implement the same named option
in solaris to display statvfs output, use -G instead. I researched this
and all other implementations except Solaris (OS/X, FreeBSD, us before June
24, 2007) use -g to signify gigabytes. So revert to that.
PR/38154: YAMAMOTO Takashi: df -P doesn't work without -k. Fix from Anon Ymous
together with passing lint, and exiting with the right values.
 1.79 04-Mar-2008  yamt restore whitespace between 'Filesystem' and 'blocks'.
 1.78 22-Dec-2007  yamt branches: 1.78.2;
prtstat: PRIdMAX -> jd as suggested by Joerg Sonnenberger.
 1.77 21-Dec-2007  yamt don't cast 64bit values to u_long.
 1.76 17-Jul-2007  christos branches: 1.76.4;
kill MFSNAMELEN
 1.75 16-Jul-2007  christos Fix '-Ph' segfault; from Greg Woods
 1.74 03-Jul-2007  christos spacing fixes from Geoff Wing
 1.73 24-Jun-2007  christos PR/36541: Greg A. Woods: fix -P option, and add solaris -g option.
 1.72 04-Sep-2006  christos fix formatting of df -h; from Zafer.
 1.71 21-Aug-2006  christos -h and -[mgk] are mutually exclusive. Only use the last specified. [from zafer]
Use one variable instead of 3 flags to hold the scale, so that we don't have
to reset each flag.
 1.70 17-Mar-2006  rumble branches: 1.70.2;
Handle malloc returning NULL.
 1.69 13-Jan-2006  elad PR/23688: don't say we're printing in 1k-blocks if -h was also specified.
 1.68 26-Jun-2005  christos sprinkle a little const, and now everything compiles with WARNS=3
 1.67 31-Dec-2004  augustss Use the same width (9) for both the Used and Avail columns.
 1.66 17-Jul-2004  enami Drop LSBs when denominator is big.
 1.65 07-Jul-2004  enami Remove now unnecesary/incorrect cast.
 1.64 07-Jul-2004  enami - Remove unnecessary empty line.
- Wrap long line.
 1.63 04-Jul-2004  martin Available block count may be negative - so make the calculation signed.
This avoids output like:
Filesystem 1K-blocks Used Avail Capacity Mounted on
/dev/wd1a 11799 11276 9223372036854775741 100% /boot
 1.62 21-Apr-2004  christos Replace the statfs() family of system calls with statvfs().
Retain binary compatibility.
 1.61 26-Mar-2004  enami Combine two printf()s back to one. There was a reason to split in my
old patch but no reason to do so in the final version.
 1.60 26-Mar-2004  enami Prevent sign extension for positive value, and f_bavail if it is considered
as positive value.
 1.59 26-Mar-2004  enami Fix an output of df -h when the value is negative; we need to provide
space for minus sign.
 1.58 26-Mar-2004  enami No need to reference through a pointer; just refer the array of
character directly.
 1.57 26-Mar-2004  enami Don't put space before close paren.
 1.56 07-Mar-2004  enami Use signed 64bit integer type instead of unsigned long long
since f_bavail may be negative.
 1.55 02-Mar-2004  enami Backout recent undocumented change to keep an output same as before.
 1.54 02-Mar-2004  christos fix compilation on LP64 systems.
 1.53 02-Mar-2004  itojun cast to unsigned long long before computing to avoid overflow
 1.52 02-Mar-2004  itojun use unsigned long long to print block count.
 1.51 05-Jan-2004  jmmv Homogenize usage messages: make the 'usage' word all lowercase, as this seems
to be the most common practice in our tree.
 1.50 07-Dec-2003  perry Some file systems, like FAT, have no inodes in the ffs sense and don't
run out of inodes. df -i was saying, however, that such file systems
had 100% of their inodes in use, which would do things like trigger
alarms in scripts looking for file systems that have run out.

Instead, say 0% are in use, which although not strictly true is at
least less wrong, fixes scripts and is less worrying in nightly reports.
 1.49 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.48 12-Jul-2003  simonb Bump ifree/iused field width.
 1.47 18-Apr-2003  grant change -G arg to -g.

-G was chosen as SuSv2 specified -g was already used in ls(1), but ls
has no blocksize options like df/du, so this was redundant.

bump date in man page.
 1.46 16-Apr-2003  grant add -G argument for output in gigabytes.

ok'd by perry@, mrg@
 1.45 16-Apr-2003  grant sync usage args with reality.
 1.44 26-Sep-2002  provos human-readable output via -h flag. output matches Linux and other BSDs.
partly from kstailey@openbsd.org. approved itojun and perry.
 1.43 02-Aug-2002  soren Following FreeBSD's example, remove the old pre-statfs(2) code for
examining FFS filesystems directly. This also gets rid of df(1)'s
setgid operator bit.
 1.42 11-Oct-2001  christos branches: 1.42.2;
if -a is not given, we need to walk the filesystem list to check the
flags in order to get the correct count (from enami)
 1.41 11-Oct-2001  enami KNF.
 1.40 20-Feb-2001  cgd use getprogname()
 1.39 01-Feb-2001  tsutsui Adjust output column in printf().
 1.38 07-Jan-2001  christos eliminate nested extern
 1.37 04-Jan-2001  lukem use more standard %ll_ in favour of %q_
 1.36 30-Dec-2000  hubertf Add -m option, similar to -k, but display output in 1024*1024 byte units.

Patch submitted by Chris Pinnock <C.J.E.Pinnock@qmw.ac.uk> in PR 11849,
adjusted for NetBSD-current by me.
 1.35 15-Oct-2000  kleink Support the 1003.2-92 -P flag for a `portable output format'; based on
Ben Harris' patch in standards/11208 but extended about output alignment
and documentation.
 1.34 26-Jun-2000  christos Add -a flag now that we have support for MNT_IGNORE
 1.33 24-Sep-1999  hubertf Fix overflow in df on big partitions' raw device.
Supplied by Greg Oster, problem noted in OpenBSD PR#924.
 1.32 27-Jul-1999  sommerfeld branches: 1.32.2;
Fix PR3474: df -t/df -l confused by stacked local mounts.
(Ross's patch in the PR appears to Just Work).
 1.31 08-Oct-1998  wsanchez branches: 1.31.2;
Cast args to strpct() u_long, as in the prototype, not ulong.
 1.30 28-Jul-1998  mycroft Delint.
 1.29 28-Jul-1998  mycroft Delint.
 1.28 27-Jul-1998  mycroft Use mkdtemp(3).
 1.27 04-Jul-1998  mrg minor KNF.
 1.26 08-Apr-1998  fair Fix PR#3590 - remove floating point calculation from df to avoid faulting
on machines that don't yet have complete FP support (e.g. MC68LC040)
 1.25 01-Mar-1998  fvdl Merge with Lite2 + local changes
 1.24 20-Jul-1997  thorpej - Use __COPYRIGHT() and __RCSID().
- Fix compiler warnings.
 1.23 11-Dec-1996  thorpej Make df(1) more liberal (and intelligent) about what it accepts
as arguments (for -t and -l flags, specifically).
From Hubert Feyrer <feyrer@rfhs8002.fh-regensburg.de>, PR #2869.
 1.22 28-Nov-1995  jtc merge in changes from 1.1 release branch
 1.21 11-Aug-1995  jtc branches: 1.21.2;
lose unused variable; PR #1297
 1.20 18-Jun-1995  cgd don't assume f_fstypename is larger than MFSNAMELEN or is nul-terminated
 1.19 21-Mar-1995  cgd convert to new RCS id conventions.
 1.18 30-Jan-1995  mycroft Use S_IS*().
 1.17 30-Jan-1995  mycroft Various:
* Redo `-t' handling like mount(8) and umount(8).
* Don't force a stat of every fs unless we need it.
* When using an explicit list, stat everything before printing anything, and
minimize the column width.
 1.16 13-Jan-1995  mycroft Add `-t' option from Matthew Green, hacked a bit by me.
 1.15 16-Sep-1994  mycroft Deal with large file systems, and clean up a bit.
 1.14 12-Jul-1994  glass fix typo
 1.13 13-Jun-1994  chopps branches: 1.13.2;
fixup for new fs code.
 1.12 17-May-1994  cgd copyright foo
 1.11 21-Apr-1994  cgd queue/struct changes
 1.10 14-Apr-1994  cgd fs type names will soon be strings
 1.9 11-Apr-1994  glass check return code. noticed while discovering a already fixed bug
 1.8 07-Apr-1994  cgd when you move a fn. call to another function, you shouldn't depend
on the locals' values it used to set...
 1.7 11-Mar-1994  mycroft Add `-l' option.
 1.6 25-Jan-1994  cgd new definition of getbsize
 1.5 06-Aug-1993  mycroft Incorporate new version using getbsize(), with -k flag added.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.3 09-Jan-1997  tls Import from 4.4BSD-Lite
 1.1.1.2 07-Sep-1995  jtc imported from 44lite2
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.13.2.1 16-Sep-1994  cgd From trunk, per mycroft.
 1.21.2.1 01-Nov-1995  jtc complete ufs -> ffs change (From John Kohl; PR #1403)
 1.31.2.1 26-Sep-1999  cgd pull up rev 1.32 from trunk (requested by hubertf):
Fix overflow on big partition's raw device.
 1.32.2.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.42.2.1 31-Aug-2003  tron Pull up revision 1.48 (requested by simonb in ticket #1378):
Bump ifree/iused field width.
 1.70.2.1 27-Aug-2006  riz Pull up following revision(s) (requested by christos in ticket #53):
bin/df/df.c: revision 1.71
bin/mv/mv.c: revision 1.36
bin/ls/ls.1: revision 1.61
bin/ls/ls.c: revision 1.60
-h and -[mgk] are mutually exclusive. Only use the last specified. [from zafer]
Use one variable instead of 3 flags to hold the scale, so that we don't have
to reset each flag.
from zafer: you can have either -k or -h but not both since it does not make
sense.
- detect buffer overflow.
- if we have a trailing slash, don't add another one.
 1.76.4.2 23-Mar-2008  matt sync with HEAD
 1.76.4.1 09-Jan-2008  matt sync with HEAD
 1.78.2.1 24-Mar-2008  keiichi sync with head.
 1.81.4.2 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.81.4.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.81.2.1 04-Jun-2008  yamt sync with head
 1.83.8.1 21-Apr-2010  matt sync to netbsd-5
 1.83.4.1 09-Jun-2009  snj Pull up following revision(s) (requested by mlelstv in ticket #797):
bin/df/df.c: revision 1.86
block numbers are measured in f_frsize units. Make -P option
use this instead of f_bsize. Also account for reserved blocks
like normal non-P output.
Fixes PR bin/41541.
 1.89.2.1 17-Apr-2012  yamt sync with head
 1.92.16.4 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.92.16.3 21-Apr-2020  martin Sync with HEAD
 1.92.16.2 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.92.16.1 10-Jun-2019  christos Sync with HEAD
 1.92.14.1 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.92.8.1 28-Aug-2018  martin Pull up following revision(s) (requested by sevan in ticket #992):

external/bsd/top/dist/top.1.in: revision 1.11
sbin/gpt/main.c: revision 1.12
sbin/amrctl/amrctl.c: revision 1.11
bin/df/df.c: revision 1.93
sbin/fsck_ext2fs/fsck_ext2fs.8: revision 1.21
sbin/fsck_ext2fs/main.c: revision 1.38
bin/ksh/ksh.Man: revision 1.26
bin/ln/ln.c: revision 1.40
bin/df/df.1: revision 1.48
bin/df/df.1: revision 1.49

Document the WCPU field.

Match SYNOPSIS with usage()

-G cannot be specified alongside -i or -P.
Heads up by <leot>

Add -l to SYNOPSIS

Update usage to include -w

Match sequence as per SYNOPSIS in manual

Remove reference to -c flag which was never implemented.

Remove references to -c flag which was never included.

Add the -T flag to usage()
 1.101.2.1 18-Dec-2023  martin Pull up following revision(s) (requested by kre in ticket #506):

bin/df/df.c: revision 1.102

Correctly compute the "Filesystem" column with when using -W
(that added "NAME=" occupies space!)
 1.102.2.1 02-Aug-2025  perseant Sync with HEAD
 1.6 20-Jul-1997  christos branches: 1.6.112;
Remove WARNS=1 from all the subdirectory Makefiles, and add it to Makefile.inc
now that all /bin has been cleaned.
 1.5 20-Jul-1997  thorpej Build with WARNS.
 1.4 21-Mar-1995  cgd convert to new RCS id conventions.
 1.3 22-Sep-1994  mycroft New version, derived from hostname(1), with a corrected man page.
 1.2 01-Aug-1993  mycroft Add RCS identifiers.
 1.1 02-Apr-1993  deraadt added: the entire domainanme set. domainmame(1), domainname(2), and a spiffy
man page.
 1.6.112.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.6.112.1 21-Apr-2020  martin Sync with HEAD
 1.15 15-Jun-2004  jmmv branches: 1.15.92;
Drop extra space.
 1.14 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.13 25-Sep-2002  wiz New policy: New sentences start on a new line.
Patches by Robert Elz <kre at munnari oz au>, with minimal changes by me.
 1.12 27-Feb-2001  mason Update to reflect reality - s,/etc/netstart,/etc/rc.d/network,
 1.11 22-Mar-1999  garbled More and more .Os cleanups. .Os is defined in the tmac.doc-common file,
so we shouldn't override it with versions in the manpages. Many more to
come.
 1.10 23-Jan-1998  lukem use .Tn YP instead of YP
 1.9 20-Oct-1997  enami Fix .Nm usage.
 1.8 23-Jun-1997  pk Xr to [gs]etdomainname in section 3 (from PR#3627)
 1.7 25-Jul-1995  jtc Use "utility" instead of "command". Modern definitions of these terms
are distinct (See POSIX.2 glossary).

A utility is a executable, script or shell builtin; while a command
can be any of those things plus lists, pipelines, compound commands
(if, for, while) and shell function definitions.
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 26-Sep-1994  mycroft Clarify the history a bit.
 1.4 22-Sep-1994  mycroft New version, derived from hostname(1), with a corrected man page.
 1.3 10-Sep-1993  jtc Include appropriate header files to bring prototypes into scope.
Cleaned up man page.
 1.2 01-Aug-1993  mycroft Add RCS indentifiers.
 1.1 02-Apr-1993  deraadt added: the entire domainanme set. domainmame(1), domainname(2), and a spiffy
man page.
 1.15.92.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.15.92.1 21-Apr-2020  martin Sync with HEAD
 1.15 29-Aug-2011  joerg branches: 1.15.44;
Use __dead
 1.14 20-Jul-2008  lukem Remove the \n and tabs from the __COPYRIGHT() strings.
 1.13 19-Apr-2004  kleink branches: 1.13.26;
Un-__P(), ANSIfy, use EXIT_{SUCCESS,FAILURE}, use [gs]etprocname().
From Stefan Kr�ger in PR bin/24878.
 1.12 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.11 28-Jul-1998  mycroft Delint.
 1.10 28-Jul-1998  mycroft Delint.
 1.9 05-Nov-1997  cgd lint
 1.8 20-Jul-1997  thorpej __RCSID()/__COPYRIGHT()/prototype police.
 1.7 21-Mar-1995  cgd convert to new RCS id conventions.
 1.6 26-Sep-1994  mycroft Make the usage messages more closely match the man pages.
 1.5 22-Sep-1994  mycroft New version, derived from hostname(1), with a corrected man page.
 1.4 23-Feb-1994  cgd add correct copyright, per deraadt
 1.3 10-Sep-1993  jtc Include appropriate header files to bring prototypes into scope.
Cleaned up man page.
 1.2 02-Aug-1993  mycroft domainname.c
 1.1 02-Apr-1993  deraadt added: the entire domainanme set. domainmame(1), domainname(2), and a spiffy
man page.
 1.13.26.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.15.44.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.15.44.1 21-Apr-2020  martin Sync with HEAD
 1.9 10-Oct-2021  rillig echo: clean up, increase WARNS to 6

Lint can handle __COPYRIGHT and __RCSID, so there is no need to hide
them anymore.

Use proper type 'bool' for nflag, ensure correct types via lint's strict
bool mode.

Remove unnecessary call to exit(0); returning from main is equivalent
since C99.

No functional change.
 1.8 20-Jul-1997  christos branches: 1.8.112;
Remove WARNS=1 from all the subdirectory Makefiles, and add it to Makefile.inc
now that all /bin has been cleaned.
 1.7 20-Jul-1997  thorpej Build with WARNS
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.8.112.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.8.112.1 21-Apr-2020  martin Sync with HEAD
 1.20 14-May-2024  gutteridge echo.1: borrow advice about printf(1) from the OpenBSD man page
 1.19 26-Feb-2020  wiz Add some commas.
 1.18 26-Feb-2020  gutteridge echo.1: add a note about other echo implementations

The other BSDs all have a note reminding that many shells have their
own internal echo implementations which may vary from this utility, so
add one. (Much of the wording is borrowed from FreeBSD's man page.)

(The other BSDs also have notes about the -n option not really being
portable, and printf[1] being preferable, we might want to add
something about that, too.)
 1.17 04-Jul-2017  wiz branches: 1.17.6;
Use Ex.
 1.16 03-Jul-2017  wiz Remove workaround for ancient HTML generation code.
 1.15 14-Aug-2016  sevan While the v2 source and man pages are incomplete, TUHS has a scanned copy of the
printed version of the 2nd edition manual. echo is featured in this copy.
http://www.tuhs.org/Archive/PDP-11/Distributions/research/1972_stuff/unix_2nd_edition_manual.pdf
Ammend HISTORY and bump date.
 1.14 11-Aug-2016  sevan Document the version echo first appeared.
Bump date.
 1.13 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.12 08-Feb-2002  ross Generate <>& symbolically. I'm avoiding .../dist/... directories for now.
 1.11 04-Sep-2000  kleink For commands and utilities, use EXIT STATUS rather than RETURN VALUES as
appropriate (and documented in mdoc(7)).
 1.10 28-Aug-2000  hubertf Add 'RETURN VALUE' section header.
 1.9 25-Jan-2000  enami branches: 1.9.4;
Use .Ar macro for argument.
 1.8 20-Oct-1997  enami Fix .Nm usage.
 1.7 21-Mar-1995  cgd convert to new RCS id conventions.
 1.6 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.5 28-Aug-1993  jtc Document the fact that echo is POSIX.2 compliant.
 1.4 01-Aug-1993  mycroft Add RCS indentifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.9.4.1 28-Aug-2000  hubertf Pull up to netbsd-1-5 branch, OK'd by thorpej:

Log Message:
> Add 'RETURN VALUE' section header.

Files & Revisionis:
> cvs rdiff -r1.19 -r1.20 basesrc/bin/cat/cat.1
> cvs rdiff -r1.12 -r1.13 basesrc/bin/chmod/chmod.1
> cvs rdiff -r1.14 -r1.15 basesrc/bin/cp/cp.1
> cvs rdiff -r1.8 -r1.9 basesrc/bin/dd/dd.1
> cvs rdiff -r1.9 -r1.10 basesrc/bin/echo/echo.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/expr/expr.1
> cvs rdiff -r1.25 -r1.26 basesrc/bin/ls/ls.1
> cvs rdiff -r1.10 -r1.11 basesrc/bin/mkdir/mkdir.1
> cvs rdiff -r1.23 -r1.24 basesrc/bin/mt/mt.1
> cvs rdiff -r1.12 -r1.13 basesrc/bin/mv/mv.1
> cvs rdiff -r1.16 -r1.17 basesrc/bin/pwd/pwd.1
> cvs rdiff -r1.9 -r1.10 basesrc/bin/rm/rm.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/rmdir/rmdir.1
> cvs rdiff -r1.35 -r1.36 basesrc/bin/sh/sh.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/sleep/sleep.1
> cvs rdiff -r1.20 -r1.21 basesrc/bin/stty/stty.1
 1.17.6.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.17.6.2 21-Apr-2020  martin Sync with HEAD
 1.17.6.1 08-Apr-2020  martin Merge changes from current as of 20200406
 1.23 16-Nov-2021  rillig echo: fix lint error due to strict bool mode

No functional change.
 1.22 10-Nov-2021  kre With -Wall compiling this was giving:

echo.c: In function 'main':
echo.c:74:1: warning: control reaches end of non-void function

This raises 2 issues.

First, why with WARNS set to 6, which should include just about
everything, was this not causing problems with everyday builds?
Surely falling off the end of a non-void function without returning
a specific value is one of the more basic errors that should be fixed.
(Whatever the name of the function). Is there a missing -Wxxx option?

And second, does C99 really promise:
Remove unnecessary call to exit(0); returning from main is equivalent
since C99.
in the sense that simply falling out of main() is exit(0)? Or is it
simply saying that the return value of main() is the exit status (which
has been true for much longer than since c99)?
 1.21 10-Oct-2021  rillig echo: clean up, increase WARNS to 6

Lint can handle __COPYRIGHT and __RCSID, so there is no need to hide
them anymore.

Use proper type 'bool' for nflag, ensure correct types via lint's strict
bool mode.

Remove unnecessary call to exit(0); returning from main is equivalent
since C99.

No functional change.
 1.20 19-May-2021  kre Catch up with /bin/sh built-in echo ... if a write error occurs,
don't just exit(1), write an error message to stderr as well
(required for POSIX conformance).
 1.19 05-Sep-2016  sevan branches: 1.19.14; 1.19.20;
Drop main() prototype.
 1.18 18-Sep-2008  dholland Check ferror(stdout) and exit(1) if there's been a problem.
Fixes PR bin/39574 from Ed Ravin, but with a simpler patch.
 1.17 20-Jul-2008  lukem Remove the \n and tabs from the __COPYRIGHT() strings.
 1.16 26-May-2008  christos PR/38755: murray armfield: /bin posix programs missing setprogname and/or
setlocale
 1.15 25-Nov-2003  simonb branches: 1.15.16; 1.15.30; 1.15.32;
Put a few headers back so this compiles again.
Compile problems noticed by Dan Carosone.
 1.14 24-Nov-2003  jschauma Also remove headers that are no longer needed. Also pointed out by Mike
Small.
 1.13 24-Nov-2003  jschauma Remove unused variable stdout_ok, which was left over from my changes
in September. Pointed out by Mike Small in a private email.
 1.12 14-Sep-2003  jschauma Following private discussion with kleink@ and hubertf@ and public discussion
on tech-userlevel@, back out printescaped() functionality.
kleink: ``We sell rope.''
 1.11 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.10 04-Aug-2003  jschauma As discusses a little while back on tech-userlevel:

If stdout is a tty, use vis(3) to print any filenames to prevent garbage
from being printed if the filename contains control- or other non-printable
characters.

While here, sprinkle some EXIT_FAILURE and NOTREACHED where appropriate.
 1.9 29-Jul-2001  wiz ANSIfy, KNF. Parts from #13590 by Petri Koistinen.
 1.8 05-Nov-1997  cgd lint
 1.7 20-Jul-1997  thorpej __RCSID()/__COPYRIGHT()/prototype police.
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.15.32.3 24-Sep-2008  wrstuden Merge in changes between wrstuden-revivesa-base-2 and
wrstuden-revivesa-base-3.
 1.15.32.2 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.15.32.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.15.30.1 04-Jun-2008  yamt sync with head
 1.15.16.1 18-Nov-2008  bouyer Pull up following revision(s) (requested by dholland in ticket #1232):
bin/echo/echo.c: revision 1.18
bin/sh/bltin/bltin.h: revision 1.13
bin/sh/bltin/echo.c: revision 1.14
bin/sh/output.c: revision 1.30
bin/sh/output.h: revision 1.19
Check ferror(stdout) and exit(1) if there's been a problem.
Fixes PR bin/39574 from Ed Ravin, but with a simpler patch.
output.c output.h: expose OUTPUT_ERR (flag for an exposed flags variable)
bltin.h: support ferror()
echo.c: use ferror() to fail on output write errors
Another piece of PR bin/39574.
 1.19.20.1 31-May-2021  cjep sync with head
 1.19.14.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.19.14.1 21-Apr-2020  martin Sync with HEAD
 1.37 21-May-2017  riastradh branches: 1.37.10;
Remove MKCRYPTO option.

Originally, MKCRYPTO was introduced because the United States
classified cryptography as a munition and restricted its export. The
export controls were substantially relaxed fifteen years ago, and are
essentially irrelevant for software with published source code.

In the intervening time, nobody bothered to remove the option after
its motivation -- the US export restriction -- was eliminated. I'm
not aware of any other operating system that has a similar option; I
expect it is mainly out of apathy for churn that we still have it.
Today, cryptography is an essential part of modern computing -- you
can't use the internet responsibly without cryptography.

The position of the TNF board of directors is that TNF makes no
representation that MKCRYPTO=no satisfies any country's cryptography
regulations.

My personal position is that the availability of cryptography is a
basic human right; that any local laws restricting it to a privileged
few are fundamentally immoral; and that it is wrong for developers to
spend effort crippling cryptography to work around such laws.

As proposed on tech-crypto, tech-security, and tech-userlevel to no
objections:

https://mail-index.netbsd.org/tech-crypto/2017/05/06/msg000719.html
https://mail-index.netbsd.org/tech-security/2017/05/06/msg000928.html
https://mail-index.netbsd.org/tech-userlevel/2017/05/06/msg010547.html

P.S. Reviewing all the uses of MKCRYPTO in src revealed a lot of
*bad* crypto that was conditional on it, e.g. DES in telnet... That
should probably be removed too, but on the grounds that it is bad,
not on the grounds that it is (nominally) crypto.
 1.36 26-Jul-2009  dholland Remove the need for -Wno-pointer-sign. Does not change the output .o file.
 1.35 29-Aug-2008  gmcgarry Wrap compiler-specific flags with HAVE_GCC and HAVE_PCC as necessary. Add a few flags for PCC.
 1.34 12-Jul-2008  gmcgarry Add test for HAVE_PCC with HAVE_GCC which turn off compiler warning flags.
 1.33 11-May-2006  mrg branches: 1.33.20;
sprinkle some -fno-strict-aliasing and -Wno-pointer-sign with GCC4.
 1.32 23-Jun-2000  thorpej Add MK... variables to enable/disable various aspects of building
crypto support into the system. See share/mk/bsd.README for more
a full description.
 1.31 16-Jun-2000  thorpej branches: 1.31.2;
libcrypt can now always decrypt, so always -DDES, and eliminate the
<bsd.crypto.mk> song-and-dance.
 1.30 20-Jul-1999  mrg branches: 1.30.6;
optionally include CRYPTOPATH Makefile.frag files.
 1.29 12-Jul-1999  thorpej Use bsd.crypto.mk.
 1.28 10-Oct-1997  christos CFLAGS->CPPFLAGS
 1.27 03-Aug-1997  mikel kill extra space
 1.26 20-Jul-1997  christos Remove WARNS=1 from all the subdirectory Makefiles, and add it to Makefile.inc
now that all /bin has been cleaned.
 1.25 20-Jul-1997  christos Avoid the last warnings and add WARNS=1
 1.24 21-Mar-1995  cgd convert to new RCS id conventions.
 1.23 23-Mar-1994  alm Renamed insque
 1.22 20-Feb-1994  cgd the insque and remque in libcompat don't seem to work (!)
 1.21 20-Feb-1994  alm insque is in libcompat
 1.20 20-Feb-1994  alm `s3' applies the last substitution to the 3rd (or nth) match.
Split up the code to several files.
 1.19 23-Nov-1993  alm Added a line-addressing test so this isn't overlooked again.
Added BACKWARDS directive to Makefile for compatibility.
Completely changed the name space as per FSF's request.
Fixed a line-addressing bug introduced by previous `fix', i.e.,
space preceding a digit acts a plus (+) operator, as in `1 1' == `2',
and a plus operator by itself acts as +1, but a space by itself
should not equal a plus operator, i.e., `1 ' != `1+' == `2'.
 1.18 11-Nov-1993  jtc Use new POSIX.2 compatible regex routines in C library instead of -lgnuregex.
 1.17 28-Oct-1993  cgd don't hard-link to 'red' -- that's not a standard BSD thang. If people
want it, they can make the link themselves.
 1.16 07-Oct-1993  cgd always include libcrypt
 1.15 30-Aug-1993  alm Repeating a search requires only `/' (or `?'), not `//' (or `??')
But `s/' is still invalid, use `s//' instead.
If first regex is invalid, regex pointer is now set to NULL so that repeating
the search correctly reports "No previous pattern".
 1.14 02-Aug-1993  mycroft Add RCS identifiers, remove some completely useless RCS logs and patchkit
headers, and a few other insignificant changes.
 1.13 28-Jul-1993  cgd incorporate changes from 0-9-base to 0-9-ALPHA
 1.12 02-Jul-1993  alm branches: 1.12.2;
fixed undo within a global command (would corrupt the buffer)
 1.11 16-Jun-1993  alm fixed regex initialization
added more support for BACKWARDS option
 1.10 27-May-1993  alm Added a restricted mode in the form of a new command: red
 1.9 08-May-1993  alm added support for lines of arbitrary length
added support for 8-bit data, including NULs
 1.8 01-May-1993  mycroft Use global value of CFLAGS for optimization.
 1.7 28-Apr-1993  alm rearranged headers to compile cleanly
minor corrections in man page
 1.6 23-Apr-1993  alm added man page (draft)
fixed `!' command
final code cleanup
 1.5 16-Apr-1993  alm Removed getrange() hack from undo()
 1.4 15-Apr-1993  alm added an efficient and relatively clean move (`m' command)
added ustack checking
 1.3 14-Apr-1993  alm implemented line marks as pointers
marks to lines that are moved, deleted or otherwise changed are cleared
undo restores cleared marks
added strerror diagnostics
 1.2 10-Apr-1993  cgd updated to alm's ed-0.2
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.12.2.1 21-Jul-1993  cgd deal properly with libcrypt; i.e. link if it's not there.
 1.30.6.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.31.2.1 24-Jun-2000  thorpej Update from trunk:
Add MK... variables to enable/disable various aspects of building
crypto support into the system. See share/mk/bsd.README for more
a full description.
 1.33.20.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.37.10.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.37.10.1 21-Apr-2020  martin Sync with HEAD
 1.11 20-May-2022  andvar fix some misspellings and remove trailing whitespaces.
 1.10 18-Nov-1999  kristerw branches: 1.10.104;
Spelling ("occurences" -> "occurrences")
 1.9 21-Mar-1995  cgd branches: 1.9.12;
convert to new RCS id conventions.
 1.8 20-Feb-1994  alm `s3' applies the last substitution to the 3rd (or nth) match.
Split up the code to several files.
 1.7 23-Nov-1993  alm Added a line-addressing test so this isn't overlooked again.
Added BACKWARDS directive to Makefile for compatibility.
Completely changed the name space as per FSF's request.
Fixed a line-addressing bug introduced by previous `fix', i.e.,
space preceding a digit acts a plus (+) operator, as in `1 1' == `2',
and a plus operator by itself acts as +1, but a space by itself
should not equal a plus operator, i.e., `1 ' != `1+' == `2'.
 1.6 12-Nov-1993  alm fix line addressing so that 1,2, == 2,2 (as per SunOS ed) not 1,2
add global-active routines to replace some bad hacks
remove obsolescent Addison-Wesley copyrights
 1.5 02-Jul-1993  alm fixed undo within a global command (would corrupt the buffer)
 1.4 16-Jun-1993  alm fixed regex initialization
added more support for BACKWARDS option
 1.3 27-May-1993  alm Added a restricted mode in the form of a new command: red
 1.2 08-May-1993  alm added support for lines of arbitrary length
added support for 8-bit data, including NULs
 1.1 23-Apr-1993  alm added man page (draft)
fixed `!' command
final code cleanup
 1.9.12.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.10.104.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.10.104.1 21-Apr-2020  martin Sync with HEAD
 1.10 04-Jan-2019  maya Unifdef compatibility for broken realloc.

No binary change
 1.9 21-Mar-1995  cgd branches: 1.9.114; 1.9.116;
convert to new RCS id conventions.
 1.8 20-Feb-1994  alm `s3' applies the last substitution to the 3rd (or nth) match.
Split up the code to several files.
 1.7 23-Nov-1993  alm Added a line-addressing test so this isn't overlooked again.
Added BACKWARDS directive to Makefile for compatibility.
Completely changed the name space as per FSF's request.
Fixed a line-addressing bug introduced by previous `fix', i.e.,
space preceding a digit acts a plus (+) operator, as in `1 1' == `2',
and a plus operator by itself acts as +1, but a space by itself
should not equal a plus operator, i.e., `1 ' != `1+' == `2'.
 1.6 12-Nov-1993  alm fix line addressing so that 1,2, == 2,2 (as per SunOS ed) not 1,2
add global-active routines to replace some bad hacks
remove obsolescent Addison-Wesley copyrights
 1.5 16-Jun-1993  alm fixed regex initialization
added more support for BACKWARDS option
 1.4 08-May-1993  alm added support for lines of arbitrary length
added support for 8-bit data, including NULs
 1.3 28-Apr-1993  alm rearranged headers to compile cleanly
minor corrections in man page
 1.2 23-Apr-1993  alm added man page (draft)
fixed `!' command
final code cleanup
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.9.116.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.9.116.2 21-Apr-2020  martin Sync with HEAD
 1.9.116.1 10-Jun-2019  christos Sync with HEAD
 1.9.114.1 18-Jan-2019  pgoyette Synch with HEAD
 1.28 17-Sep-2025  rillig ed: fix search-and-replace typos in comments, clean up whitespace

No binary change.
 1.27 23-Mar-2014  dholland branches: 1.27.26;
sprintf considered harmful
 1.26 17-Mar-2006  rumble branches: 1.26.42; 1.26.48;
Handle asprintf returning NULL.
 1.25 26-Jun-2005  christos sprinkle a little const, and now everything compiles with WARNS=3
 1.24 17-Feb-2005  xtraeme Kill __P(), use ANSI function declarations.
 1.23 19-Oct-2003  wiz Fix typos in comments. From Jared Yanovich via jmc@openbsd.
 1.22 05-Sep-2003  itojun plug memory leak. PR bin/22694
 1.21 17-Apr-2000  christos Obey $TMPDIR for creating the scratch file
 1.20 04-Apr-2000  thorpej Back out the incorrect change in revision 1.11 of main.c, and actually
fix the problem it meant to address by putting extern declarations into
ed.h.
 1.19 04-Nov-1998  christos add braces
 1.18 28-Jul-1998  mycroft Delint.
 1.17 20-Jul-1997  thorpej - Use __RCSID() and __COPYRIGHT().
- Fix (all but two) compiler warnings.
 1.16 22-Mar-1997  lukem use mkstemp() instead of mktemp()
 1.15 23-Apr-1995  cgd clean up RCS Ids slightly, etc.
 1.14 21-Mar-1995  cgd convert to new RCS id conventions.
 1.13 23-Mar-1994  alm Renamed insque
 1.12 23-Mar-1994  alm Use umask 077 to open buffer file
 1.11 20-Feb-1994  alm `s3' applies the last substitution to the 3rd (or nth) match.
Split up the code to several files.
 1.10 23-Nov-1993  alm Added a line-addressing test so this isn't overlooked again.
Added BACKWARDS directive to Makefile for compatibility.
Completely changed the name space as per FSF's request.
Fixed a line-addressing bug introduced by previous `fix', i.e.,
space preceding a digit acts a plus (+) operator, as in `1 1' == `2',
and a plus operator by itself acts as +1, but a space by itself
should not equal a plus operator, i.e., `1 ' != `1+' == `2'.
 1.9 12-Nov-1993  alm fix line addressing so that 1,2, == 2,2 (as per SunOS ed) not 1,2
add global-active routines to replace some bad hacks
remove obsolescent Addison-Wesley copyrights
 1.8 01-Aug-1993  mycroft Add RCS identifiers.
 1.7 02-Jul-1993  alm fixed undo within a global command (would corrupt the buffer)
 1.6 08-May-1993  alm added support for lines of arbitrary length
added support for 8-bit data, including NULs
 1.5 23-Apr-1993  alm added man page (draft)
fixed `!' command
final code cleanup
 1.4 15-Apr-1993  alm added an efficient and relatively clean move (`m' command)
added ustack checking
 1.3 14-Apr-1993  alm implemented line marks as pointers
marks to lines that are moved, deleted or otherwise changed are cleared
undo restores cleared marks
added strerror diagnostics
 1.2 10-Apr-1993  cgd updated to alm's ed-0.2
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.26.48.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.26.42.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.27.26.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.27.26.1 21-Apr-2020  martin Sync with HEAD
 1.27 17-Sep-2025  rillig ed: fix search-and-replace typos in comments, clean up whitespace

No binary change.
 1.26 01-Aug-2023  mrg fix simple mis-matched function prototype and definitions.

most of these are like, eg

void foo(int[2]);

with either of these

void foo(int*) { ... }
void foo(int[]) { ... }

in some cases (such as stat or utimes* calls found in our header files),
we now match standard definition from opengroup.

found by GCC 12.
 1.25 08-Feb-2018  dholland branches: 1.25.4;
Typos.
 1.24 01-Feb-2016  christos PR/50734: David Binderman: check bounds before dereferencing.
 1.23 23-Mar-2014  dholland sprintf considered harmful
 1.22 09-Jun-2010  riz branches: 1.22.6; 1.22.12;
If the definition of a static function is #if DES, the declaration
of it should be too. Fixes build of ed where MKCRYPTO=no.
 1.21 26-Jul-2009  dholland remove unused global variable
 1.20 26-Jul-2009  dholland sprinkle static in this file
 1.19 26-Jul-2009  dholland Remove the need for -Wno-pointer-sign. Does not change the output .o file.
 1.18 26-Jun-2005  christos sprinkle a little const, and now everything compiles with WARNS=3
 1.17 17-Feb-2005  xtraeme Kill __P(), use ANSI function declarations.
 1.16 19-Oct-2003  wiz Fix typos in comments. From Jared Yanovich via jmc@openbsd.
 1.15 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.14 03-Nov-2001  lukem fix minor WARNS=2
 1.13 28-Jul-1998  mycroft Delint.
 1.12 03-Feb-1998  perry change includes to fix compiler warning
 1.11 20-Jul-1997  thorpej - Use __RCSID() and __COPYRIGHT().
- Fix (all but two) compiler warnings.
 1.10 09-Jan-1997  tls kill use of register
 1.9 21-Mar-1995  cgd convert to new RCS id conventions.
 1.8 20-Feb-1994  alm `s3' applies the last substitution to the 3rd (or nth) match.
Split up the code to several files.
 1.7 23-Nov-1993  alm Added a line-addressing test so this isn't overlooked again.
Added BACKWARDS directive to Makefile for compatibility.
Completely changed the name space as per FSF's request.
Fixed a line-addressing bug introduced by previous `fix', i.e.,
space preceding a digit acts a plus (+) operator, as in `1 1' == `2',
and a plus operator by itself acts as +1, but a space by itself
should not equal a plus operator, i.e., `1 ' != `1+' == `2'.
 1.6 12-Nov-1993  alm fix line addressing so that 1,2, == 2,2 (as per SunOS ed) not 1,2
add global-active routines to replace some bad hacks
remove obsolescent Addison-Wesley copyrights
 1.5 01-Aug-1993  mycroft Add RCS identifiers.
 1.4 08-May-1993  alm added support for lines of arbitrary length
added support for 8-bit data, including NULs
 1.3 15-Apr-1993  alm added an efficient and relatively clean move (`m' command)
added ustack checking
 1.2 10-Apr-1993  cgd updated to alm's ed-0.2
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.22.12.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.22.6.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.25.4.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.25.4.1 21-Apr-2020  martin Sync with HEAD
 1.35 09-Apr-2018  wiz branches: 1.35.2;
Sort option descripts, fix markups, fix typos.
 1.34 09-Apr-2018  wiz New sentence, new line. Sort SEE ALSO. Remove Xr to non-existing man page.
 1.33 08-Apr-2018  snj fix date
 1.32 05-Apr-2018  christos add -S to disable ! commands.
 1.31 03-Jul-2017  wiz branches: 1.31.4;
Remove workaround for ancient HTML generation code.
 1.30 14-May-2010  joerg branches: 1.30.26; 1.30.28; 1.30.36; 1.30.40;
Quote punctation.
 1.29 26-Mar-2009  joerg Avoid .Xo/.Xc. Fix quoting of commands to give the intended effect.
 1.28 08-Sep-2003  wiz branches: 1.28.40;
Reinstate paragraph about -E, sort option descriptions, and bump date for -E addition.
 1.27 08-Sep-2003  wiz Some more fixes: sort sections, escape some stuff for HTML output.
 1.26 08-Sep-2003  wiz Improved version of OpenBSD's mdoc'ed version of ed(1) (v1.42).
 1.25 01-May-2003  wiz Some grammar and punctuation fixes from jmc@openbsd.
 1.24 29-Mar-2003  perry occurences->occurrences, from Igor Sobrado in PR 19670
 1.23 02-Oct-2002  wiz additional and positive with two is. From Adrian Mrva.
 1.22 25-Sep-2002  wiz New policy: New sentences start on a new line.
Patches by Robert Elz <kre at munnari oz au>, with minimal changes by me.
 1.21 08-Feb-2002  ross Generate <>& symbolically. I'm avoiding .../dist/... directories for now.
 1.20 23-Jan-2002  atatat Add a -E flag to ed(1) and sed(1) so that they can use extended
regular expressions instead of just basic regular expressions.
 1.19 03-Jan-2002  wiz Fix a typo, and improve grammar.
 1.18 20-Dec-2001  wiz Whitespace nits.
 1.17 07-Nov-2000  lukem be consistent with man page xrefs (this is a pre mandoc man page)
 1.16 12-May-2000  christos mention use of TMPDIR fixed in previous commit
 1.15 09-Apr-1998  fair makewhatis doesn't quite handle nroff comments (.\") correctly,
so let's accomodate it by moving the stuff in "NAME" section around a bit.
 1.14 11-Nov-1996  lukem fix the the typos
 1.13 21-Mar-1995  cgd convert to new RCS id conventions.
 1.12 20-Feb-1994  alm `s3' applies the last substitution to the 3rd (or nth) match.
Split up the code to several files.
 1.11 11-Jan-1994  jtc Fix spelling errors.
 1.10 10-Dec-1993  cgd comment out references to red; we don't install it
 1.9 12-Nov-1993  alm fix line addressing so that 1,2, == 2,2 (as per SunOS ed) not 1,2
add global-active routines to replace some bad hacks
remove obsolescent Addison-Wesley copyrights
 1.8 02-Aug-1993  mycroft Makefile
 1.7 02-Jul-1993  alm fixed undo within a global command (would corrupt the buffer)
 1.6 16-Jun-1993  alm fixed regex initialization
added more support for BACKWARDS option
 1.5 27-May-1993  alm Added a restricted mode in the form of a new command: red
 1.4 08-May-1993  alm added support for lines of arbitrary length
added support for 8-bit data, including NULs
 1.3 28-Apr-1993  cgd 386BSD -> NetBSD
 1.2 28-Apr-1993  alm rearranged headers to compile cleanly
minor corrections in man page
 1.1 23-Apr-1993  alm branches: 1.1.1;
added man page (draft)
fixed `!' command
final code cleanup
 1.1.1.1 08-Sep-2003  wiz Import OpenBSD's mdoc version of ed(1), v1.42, for reference purposes.
 1.28.40.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.30.40.1 08-Apr-2018  snj Pull up following revision(s) (requested by christos in ticket #699):
bin/ed/ed.1: 1.32-1.33
bin/ed/main.c: 1.29
usr.bin/patch/pch.c: 1.29
Pass -S to ed(1) so that patches containing ! commands don't run commands.
Real cause of CVE-2018-0492:
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=894667)
--
add -S to disable ! commands.
--
Fix date.
 1.30.36.1 09-Apr-2018  martin Pull up following revision(s) (requested by christos in ticket #1596):
bin/ed/ed.1: revision 1.32
bin/ed/main.c: revision 1.29
usr.bin/patch/pch.c: revision 1.29
Pass -S to ed(1) so that patches containing ! commands don't run commands.
Real cause of CVS-2018-0492:
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=894667)
add -S to disable ! commands.
 1.30.28.1 09-Apr-2018  martin Pull up following revision(s) (requested by christos in ticket #1596):
bin/ed/ed.1: revision 1.32
bin/ed/main.c: revision 1.29
usr.bin/patch/pch.c: revision 1.29
Pass -S to ed(1) so that patches containing ! commands don't run commands.
Real cause of CVS-2018-0492:
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=894667)
add -S to disable ! commands.
 1.30.26.1 09-Apr-2018  martin Pull up following revision(s) (requested by christos in ticket #1596):
bin/ed/ed.1: revision 1.32
bin/ed/main.c: revision 1.29
usr.bin/patch/pch.c: revision 1.29
Pass -S to ed(1) so that patches containing ! commands don't run commands.
Real cause of CVS-2018-0492:
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=894667)
add -S to disable ! commands.
 1.31.4.2 16-Apr-2018  pgoyette Sync with HEAD, resolve some conflicts
 1.31.4.1 07-Apr-2018  pgoyette Sync with HEAD. 77 conflicts resolved - all of them $NetBSD$
 1.35.2.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.35.2.1 21-Apr-2020  martin Sync with HEAD
 1.20 20-Feb-1994  mycroft Clean up deleted files.
 1.19 23-Nov-1993  alm Added a line-addressing test so this isn't overlooked again.
Added BACKWARDS directive to Makefile for compatibility.
Completely changed the name space as per FSF's request.
Fixed a line-addressing bug introduced by previous `fix', i.e.,
space preceding a digit acts a plus (+) operator, as in `1 1' == `2',
and a plus operator by itself acts as +1, but a space by itself
should not equal a plus operator, i.e., `1 ' != `1+' == `2'.
 1.18 12-Nov-1993  alm fix line addressing so that 1,2, == 2,2 (as per SunOS ed) not 1,2
add global-active routines to replace some bad hacks
remove obsolescent Addison-Wesley copyrights
 1.17 30-Aug-1993  alm Repeating a search requires only `/' (or `?'), not `//' (or `??')
But `s/' is still invalid, use `s//' instead.
If first regex is invalid, regex pointer is now set to NULL so that repeating
the search correctly reports "No previous pattern".
 1.16 01-Aug-1993  mycroft Add RCS identifiers.
 1.15 02-Jul-1993  alm corrected return type
 1.14 02-Jul-1993  alm fixed undo within a global command (would corrupt the buffer)
 1.13 16-Jun-1993  alm fixed regex initialization
added more support for BACKWARDS option
 1.12 27-May-1993  alm Added a restricted mode in the form of a new command: red
 1.11 20-May-1993  alm fixed regex bug s/[[:digit:][]// incorrectly reported unbalanced brackets
 1.10 12-May-1993  alm Fixed regex bug: a character class of the form [IC[],
where IC is of the form [::] , [..], or [==], would incorrectly
report an error.
Fixed input mode bug: a literal ^J(i.e., ^V^J) would discard text
following it. Now, a literal ^J is treated as an ordinary ^J - i.e, it
splits a line in two.
 1.9 08-May-1993  alm added support for lines of arbitrary length
added support for 8-bit data, including NULs
 1.8 28-Apr-1993  alm rearranged headers to compile cleanly
minor corrections in man page
 1.7 23-Apr-1993  alm added man page (draft)
fixed `!' command
final code cleanup
 1.6 16-Apr-1993  alm Removed getrange() hack from undo()
 1.5 15-Apr-1993  alm added an efficient and relatively clean move (`m' command)
added ustack checking
 1.4 14-Apr-1993  alm implemented line marks as pointers
marks to lines that are moved, deleted or otherwise changed are cleared
undo restores cleared marks
added strerror diagnostics
 1.3 10-Apr-1993  cgd ed core dumped on blank lines.
 1.2 10-Apr-1993  cgd updated to alm's ed-0.2
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.39 17-Sep-2025  rillig ed: fix search-and-replace typos in comments, clean up whitespace

No binary change.
 1.38 04-Jan-2019  maya Unifdef compatibility for broken realloc.

No binary change
 1.37 25-Mar-2014  joerg branches: 1.37.24; 1.37.26;
Use __printflike.
 1.36 23-Mar-2014  dholland sprintf considered harmful
 1.35 29-Aug-2011  joerg branches: 1.35.2; 1.35.8;
Use __dead
 1.34 26-Jul-2009  dholland sprinkle static in this file
 1.33 26-Jun-2005  christos sprinkle a little const, and now everything compiles with WARNS=3
 1.32 17-Feb-2005  xtraeme Kill __P(), use ANSI function declarations.
 1.31 17-Apr-2003  lukem clear errno before strtol() if we're going to test it for ERANGE afterwards
 1.30 25-May-2002  wiz __STDC__ is always defined on NetBSD.
 1.29 23-Jan-2002  atatat Add a -E flag to ed(1) and sed(1) so that they can use extended
regular expressions instead of just basic regular expressions.
 1.28 07-Jan-2001  christos remove redundant declaration of errmsg
 1.27 04-Apr-2000  thorpej Back out the incorrect change in revision 1.11 of main.c, and actually
fix the problem it meant to address by putting extern declarations into
ed.h.
 1.26 04-Nov-1998  christos fix sys_errlist define to work with solaris
 1.25 13-Oct-1998  wsanchez Apple like limits.h, too.
 1.24 20-Jul-1997  thorpej - Use __RCSID() and __COPYRIGHT().
- Fix (all but two) compiler warnings.
 1.23 21-Mar-1995  cgd convert to new RCS id conventions.
 1.22 23-Dec-1994  cgd be careful with pointer types.
 1.21 23-Mar-1994  alm Renamed insque
 1.20 20-Feb-1994  alm `s3' applies the last substitution to the 3rd (or nth) match.
Split up the code to several files.
 1.19 04-Dec-1993  cgd delete definition of sys_errlist; our headers supply it
 1.18 01-Dec-1993  pk __386BSD__ ==> __NetBSD__
 1.17 23-Nov-1993  alm Added a line-addressing test so this isn't overlooked again.
Added BACKWARDS directive to Makefile for compatibility.
Completely changed the name space as per FSF's request.
Fixed a line-addressing bug introduced by previous `fix', i.e.,
space preceding a digit acts a plus (+) operator, as in `1 1' == `2',
and a plus operator by itself acts as +1, but a space by itself
should not equal a plus operator, i.e., `1 ' != `1+' == `2'.
 1.16 12-Nov-1993  alm fix line addressing so that 1,2, == 2,2 (as per SunOS ed) not 1,2
add global-active routines to replace some bad hacks
remove obsolescent Addison-Wesley copyrights
 1.15 30-Aug-1993  alm Repeating a search requires only `/' (or `?'), not `//' (or `??')
But `s/' is still invalid, use `s//' instead.
If first regex is invalid, regex pointer is now set to NULL so that repeating
the search correctly reports "No previous pattern".
 1.14 01-Aug-1993  mycroft Add RCS identifiers.
 1.13 02-Jul-1993  alm corrected return type
 1.12 02-Jul-1993  alm fixed undo within a global command (would corrupt the buffer)
 1.11 16-Jun-1993  alm fixed regex initialization
added more support for BACKWARDS option
 1.10 27-May-1993  alm Added a restricted mode in the form of a new command: red
 1.9 08-May-1993  alm added support for lines of arbitrary length
added support for 8-bit data, including NULs
 1.8 28-Apr-1993  cgd 386BSD -> NetBSD
 1.7 28-Apr-1993  alm rearranged headers to compile cleanly
minor corrections in man page
 1.6 23-Apr-1993  alm added man page (draft)
fixed `!' command
final code cleanup
 1.5 16-Apr-1993  alm Removed getrange() hack from undo()
 1.4 15-Apr-1993  alm added an efficient and relatively clean move (`m' command)
added ustack checking
 1.3 14-Apr-1993  alm implemented line marks as pointers
marks to lines that are moved, deleted or otherwise changed are cleared
undo restores cleared marks
added strerror diagnostics
 1.2 10-Apr-1993  cgd updated to alm's ed-0.2
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.35.8.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.35.2.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.37.26.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.37.26.2 21-Apr-2020  martin Sync with HEAD
 1.37.26.1 10-Jun-2019  christos Sync with HEAD
 1.37.24.1 18-Jan-2019  pgoyette Synch with HEAD
 1.12 17-Sep-2025  rillig ed: fix search-and-replace typos in comments, clean up whitespace

No binary change.
 1.11 10-Apr-2024  rillig ed: fix inconsistency in comment
 1.10 04-Jan-2019  maya Unifdef compatibility for broken realloc.

No binary change
 1.9 28-Aug-2015  joerg branches: 1.9.14; 1.9.16;
Be explicit about what is negated.
 1.8 31-Mar-2014  christos PR/48692: Henning Petersen: Missplaced paranthesis in bin/ed gbl.c and main.c
 1.7 23-Mar-2014  dholland sprintf considered harmful
 1.6 26-Jun-2005  christos branches: 1.6.42; 1.6.48;
sprinkle a little const, and now everything compiles with WARNS=3
 1.5 17-Feb-2005  xtraeme Kill __P(), use ANSI function declarations.
 1.4 19-Aug-1998  thorpej Add some braces to make egcs happy.
 1.3 20-Jul-1997  thorpej - Use __RCSID() and __COPYRIGHT().
- Fix (all but two) compiler warnings.
 1.2 21-Mar-1995  cgd convert to new RCS id conventions.
 1.1 20-Feb-1994  alm `s3' applies the last substitution to the 3rd (or nth) match.
Split up the code to several files.
 1.6.48.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.6.42.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.9.16.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.9.16.2 21-Apr-2020  martin Sync with HEAD
 1.9.16.1 10-Jun-2019  christos Sync with HEAD
 1.9.14.1 18-Jan-2019  pgoyette Synch with HEAD
 1.12 17-Sep-2025  rillig ed: fix search-and-replace typos in comments, clean up whitespace

No binary change.
 1.11 03-Oct-2024  rillig bin: fix lint warning "effectively discards 'const'"

For example: src/bin/ed/io.c(339): warning: call to 'strchr' effectively
discards 'const' from argument [346]

No binary change.
 1.10 23-Mar-2014  dholland branches: 1.10.26; 1.10.38;
sprintf considered harmful
 1.9 23-May-2011  joerg branches: 1.9.4; 1.9.10;
Conditionalize printfs instead of using conditional empty format
strings.
 1.8 26-Jun-2005  christos sprinkle a little const, and now everything compiles with WARNS=3
 1.7 17-Feb-2005  xtraeme Kill __P(), use ANSI function declarations.
 1.6 17-Apr-2000  christos make this compile without -DBACKWARDS [someone deleted an unused variable
that was not always unused]
 1.5 04-Apr-2000  thorpej Back out the incorrect change in revision 1.11 of main.c, and actually
fix the problem it meant to address by putting extern declarations into
ed.h.
 1.4 20-Jul-1997  thorpej - Use __RCSID() and __COPYRIGHT().
- Fix (all but two) compiler warnings.
 1.3 09-Jan-1997  tls kill use of register
 1.2 21-Mar-1995  cgd convert to new RCS id conventions.
 1.1 20-Feb-1994  alm `s3' applies the last substitution to the 3rd (or nth) match.
Split up the code to several files.
 1.9.10.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.9.4.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.10.38.1 02-Aug-2025  perseant Sync with HEAD
 1.10.26.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.10.26.1 21-Apr-2020  martin Sync with HEAD
 1.31 17-Sep-2025  rillig ed: fix search-and-replace typos in comments, clean up whitespace

No binary change.
 1.30 18-Jun-2018  christos branches: 1.30.2;
Prevent shell execution also in the 'r ! dobad' case, pointed out by
Martijn van Duren, thanks!

XXX: pullup-8
 1.29 05-Apr-2018  christos add -S to disable ! commands.
 1.28 02-Mar-2016  christos branches: 1.28.8; 1.28.14;
PR/50879: David Binderman: remove redundant code.
 1.27 31-Mar-2014  christos branches: 1.27.4; 1.27.6; 1.27.10;
PR/48692: Henning Petersen: Missplaced paranthesis in bin/ed gbl.c and main.c
 1.26 23-Mar-2014  dholland sprintf considered harmful
 1.25 21-Aug-2011  christos branches: 1.25.2; 1.25.8;
use const char [] for format.
 1.24 14-Aug-2011  christos eliminate non-literal format string.
 1.23 23-May-2011  joerg Conditionalize printfs instead of using conditional empty format
strings.
 1.22 10-Jun-2009  ginsbach Make sure that buffer is NULL terminated when input to strip_escapes() is
too long. (Thanks jnemeth!)
 1.21 10-Jun-2009  ginsbach Fix potential buffer overflow in filename escape processing
'ed [MAXPATHLEN + 1 characters]'. [From FreeBSD]
 1.20 20-Jul-2008  lukem Remove the \n and tabs from the __COPYRIGHT() strings.
 1.19 16-Oct-2006  christos branches: 1.19.18;
sprinkle volatile
 1.18 11-Jun-2006  christos PR/33699: Kevin Massey: use strlcpy to prevent stack overflows.
 1.17 26-Jun-2005  christos sprinkle a little const, and now everything compiles with WARNS=3
 1.16 23-May-2005  rillig Fixed another instance of isspace(char).
 1.15 19-Apr-2005  rillig Fixed wrong use of the <ctype.h> functions by adding an explicit conversion
to unsigned char. Approved by christos.
 1.14 17-Feb-2005  xtraeme Kill __P(), use ANSI function declarations.
 1.13 23-Jan-2002  atatat Add a -E flag to ed(1) and sed(1) so that they can use extended
regular expressions instead of just basic regular expressions.
 1.12 04-Apr-2000  thorpej Back out the incorrect change in revision 1.11 of main.c, and actually
fix the problem it meant to address by putting extern declarations into
ed.h.
 1.11 01-Apr-2000  mjl Correct data type mismatch of "rows" with extern declaration. Possible
LP64 issue (long vs int). Fixes OpenBSD PR/1167 :)
 1.10 04-Nov-1998  christos - add termios.h for winsize
- unsigned cast for is*()
 1.9 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.8 28-Jul-1998  mycroft Delint.
 1.7 14-Sep-1997  lukem getopt returns -1 not EOF
 1.6 23-Jul-1997  augustss Fix paren typo so `ed' can edit files again.
 1.5 20-Jul-1997  christos Avoid the last warnings and add WARNS=1
 1.4 20-Jul-1997  thorpej - Use __RCSID() and __COPYRIGHT().
- Fix (all but two) compiler warnings.
 1.3 21-Mar-1995  cgd convert to new RCS id conventions.
 1.2 23-Dec-1994  cgd be careful with pointer types.
 1.1 20-Feb-1994  alm `s3' applies the last substitution to the 3rd (or nth) match.
Split up the code to several files.
 1.19.18.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.25.8.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.25.2.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.27.10.1 09-Apr-2018  martin Pull up following revision(s) (requested by christos in ticket #1596):
bin/ed/ed.1: revision 1.32
bin/ed/main.c: revision 1.29
usr.bin/patch/pch.c: revision 1.29
Pass -S to ed(1) so that patches containing ! commands don't run commands.
Real cause of CVS-2018-0492:
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=894667)
add -S to disable ! commands.
 1.27.6.1 09-Apr-2018  martin Pull up following revision(s) (requested by christos in ticket #1596):
bin/ed/ed.1: revision 1.32
bin/ed/main.c: revision 1.29
usr.bin/patch/pch.c: revision 1.29
Pass -S to ed(1) so that patches containing ! commands don't run commands.
Real cause of CVS-2018-0492:
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=894667)
add -S to disable ! commands.
 1.27.4.1 09-Apr-2018  martin Pull up following revision(s) (requested by christos in ticket #1596):
bin/ed/ed.1: revision 1.32
bin/ed/main.c: revision 1.29
usr.bin/patch/pch.c: revision 1.29
Pass -S to ed(1) so that patches containing ! commands don't run commands.
Real cause of CVS-2018-0492:
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=894667)
add -S to disable ! commands.
 1.28.14.2 25-Jun-2018  pgoyette Sync with HEAD
 1.28.14.1 07-Apr-2018  pgoyette Sync with HEAD. 77 conflicts resolved - all of them $NetBSD$
 1.28.8.2 22-Jun-2018  martin Pull up following revision(s) (requested by christos in ticket #887):

bin/ed/main.c: revision 1.30

Prevent shell execution also in the 'r ! dobad' case, pointed out by
Martijn van Duren, thanks!

XXX: pullup-8
 1.28.8.1 08-Apr-2018  snj Pull up following revision(s) (requested by christos in ticket #699):
bin/ed/ed.1: 1.32-1.33
bin/ed/main.c: 1.29
usr.bin/patch/pch.c: 1.29
Pass -S to ed(1) so that patches containing ! commands don't run commands.
Real cause of CVE-2018-0492:
https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=894667)
--
add -S to disable ! commands.
--
Fix date.
 1.30.2.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.30.2.1 21-Apr-2020  martin Sync with HEAD
 1.22 17-Sep-2025  rillig ed: fix search-and-replace typos in comments, clean up whitespace

No binary change.
 1.21 23-Mar-2014  dholland branches: 1.21.26;
sprintf considered harmful
 1.20 28-Jun-2013  joerg Fix violations of the sequence point rule.
 1.19 17-Feb-2005  xtraeme branches: 1.19.48; 1.19.54;
Kill __P(), use ANSI function declarations.
 1.18 06-Dec-2002  thorpej Avoid conflict with reserved identifier "exp".
 1.17 23-Jan-2002  atatat branches: 1.17.2;
Add a -E flag to ed(1) and sed(1) so that they can use extended
regular expressions instead of just basic regular expressions.
 1.16 04-Apr-2000  thorpej Back out the incorrect change in revision 1.11 of main.c, and actually
fix the problem it meant to address by putting extern declarations into
ed.h.
 1.15 20-Jul-1997  thorpej - Use __RCSID() and __COPYRIGHT().
- Fix (all but two) compiler warnings.
 1.14 21-Mar-1995  cgd convert to new RCS id conventions.
 1.13 20-Feb-1994  alm `s3' applies the last substitution to the 3rd (or nth) match.
Split up the code to several files.
 1.12 23-Nov-1993  alm Added a line-addressing test so this isn't overlooked again.
Added BACKWARDS directive to Makefile for compatibility.
Completely changed the name space as per FSF's request.
Fixed a line-addressing bug introduced by previous `fix', i.e.,
space preceding a digit acts a plus (+) operator, as in `1 1' == `2',
and a plus operator by itself acts as +1, but a space by itself
should not equal a plus operator, i.e., `1 ' != `1+' == `2'.
 1.11 12-Nov-1993  alm fix line addressing so that 1,2, == 2,2 (as per SunOS ed) not 1,2
add global-active routines to replace some bad hacks
remove obsolescent Addison-Wesley copyrights
 1.10 30-Aug-1993  alm Repeating a search requires only `/' (or `?'), not `//' (or `??')
But `s/' is still invalid, use `s//' instead.
If first regex is invalid, regex pointer is now set to NULL so that repeating
the search correctly reports "No previous pattern".
 1.9 01-Aug-1993  mycroft Add RCS identifiers.
 1.8 02-Jul-1993  alm fixed undo within a global command (would corrupt the buffer)
 1.7 16-Jun-1993  alm fixed regex initialization
added more support for BACKWARDS option
 1.6 12-May-1993  alm Fixed regex bug: a character class of the form [IC[],
where IC is of the form [::] , [..], or [==], would incorrectly
report an error.
Fixed input mode bug: a literal ^J(i.e., ^V^J) would discard text
following it. Now, a literal ^J is treated as an ordinary ^J - i.e, it
splits a line in two.
 1.5 08-May-1993  alm added support for lines of arbitrary length
added support for 8-bit data, including NULs
 1.4 23-Apr-1993  alm added man page (draft)
fixed `!' command
final code cleanup
 1.3 14-Apr-1993  alm implemented line marks as pointers
marks to lines that are moved, deleted or otherwise changed are cleared
undo restores cleared marks
added strerror diagnostics
 1.2 10-Apr-1993  cgd updated to alm's ed-0.2
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.17.2.1 23-Jul-2004  tron Apply patch (requested by dbj in ticket 1724):
Fix problems when building the netbsd-1-6 branch with GCC 3.3.3.
 1.19.54.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.19.48.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.21.26.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.21.26.1 21-Apr-2020  martin Sync with HEAD
 1.8 17-Sep-2025  rillig ed: fix search-and-replace typos in comments, clean up whitespace

No binary change.
 1.7 23-Mar-2014  dholland branches: 1.7.26;
sprintf considered harmful
 1.6 17-Feb-2005  xtraeme branches: 1.6.48; 1.6.54;
Kill __P(), use ANSI function declarations.
 1.5 20-Jul-1997  thorpej - Use __RCSID() and __COPYRIGHT().
- Fix (all but two) compiler warnings.
 1.4 21-Mar-1995  cgd convert to new RCS id conventions.
 1.3 15-Jan-1995  mycroft Stay on the correct line at the end of a substitute command. From Andrew Moore.
 1.2 23-Dec-1994  cgd be careful with pointer types.
 1.1 20-Feb-1994  alm `s3' applies the last substitution to the 3rd (or nth) match.
Split up the code to several files.
 1.6.54.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.6.48.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.7.26.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.7.26.1 21-Apr-2020  martin Sync with HEAD
 1.8 02-Nov-2021  nia ed(1): use reallocarr instead of realloc(x * y)
 1.7 04-Jan-2019  maya Unifdef compatibility for broken realloc.

No binary change
 1.6 23-Mar-2014  dholland branches: 1.6.24; 1.6.26;
sprintf considered harmful
 1.5 17-Mar-2007  msaitoh branches: 1.5.34; 1.5.40;
s/intialized/initialized/
 1.4 17-Feb-2005  xtraeme Kill __P(), use ANSI function declarations.
 1.3 20-Jul-1997  thorpej - Use __RCSID() and __COPYRIGHT().
- Fix (all but two) compiler warnings.
 1.2 21-Mar-1995  cgd convert to new RCS id conventions.
 1.1 20-Feb-1994  alm `s3' applies the last substitution to the 3rd (or nth) match.
Split up the code to several files.
 1.5.40.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.5.34.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.6.26.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.6.26.2 21-Apr-2020  martin Sync with HEAD
 1.6.26.1 10-Jun-2019  christos Sync with HEAD
 1.6.24.1 18-Jan-2019  pgoyette Synch with HEAD
 1.2 12-Nov-1993  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.2 10-Apr-1993  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.2 02-Jul-1993  mycroft Clean up deleted files.
 1.1 10-Apr-1993  cgd updated to alm's ed-0.2
 1.2 10-Apr-1993  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.2 12-Nov-1993  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.2 12-Nov-1993  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.2 10-Apr-1993  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.2 12-Nov-1993  mycroft Clean up deleted files.
 1.1 08-May-1993  alm added support for lines of arbitrary length
added support for 8-bit data, including NULs
 1.2 12-Nov-1993  mycroft Clean up deleted files.
 1.1 23-Apr-1993  alm added more tests
 1.2 12-Nov-1993  mycroft Clean up deleted files.
 1.1 08-May-1993  alm added support for lines of arbitrary length
added support for 8-bit data, including NULs
 1.2 12-Nov-1993  mycroft Clean up deleted files.
 1.1 08-May-1993  alm added support for lines of arbitrary length
added support for 8-bit data, including NULs
 1.2 12-Nov-1993  mycroft Clean up deleted files.
 1.1 08-May-1993  alm added support for lines of arbitrary length
added support for 8-bit data, including NULs
 1.2 12-Nov-1993  mycroft Clean up deleted files.
 1.1 23-Apr-1993  alm added more tests
 1.2 12-Nov-1993  mycroft Clean up deleted files.
 1.1 08-May-1993  alm added support for lines of arbitrary length
added support for 8-bit data, including NULs
 1.2 12-Nov-1993  mycroft Clean up deleted files.
 1.1 08-May-1993  alm added support for lines of arbitrary length
added support for 8-bit data, including NULs
 1.1 10-Apr-1993  cgd branches: 1.1.120;
updated to alm's ed-0.2
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.12 26-Oct-2003  lukem branches: 1.12.98;
use ${HOST_SH} instead of /bin/sh
 1.11 21-Mar-1995  cgd convert to new RCS id conventions.
 1.10 20-Feb-1994  alm `s3' applies the last substitution to the 3rd (or nth) match.
Split up the code to several files.
 1.9 28-Dec-1993  davidb Changed to use make's new .OBJDIR
 1.8 10-Dec-1993  cgd use ./ before invoked programs. this makefile should really be made ro-safe
 1.7 12-Nov-1993  alm rename !* -> bang* and \n* -> nl*
 1.6 02-Aug-1993  jtc Make paths explicit to handle case where . is not in PATH.
Locate ed executible wrt obj/noobj.
 1.5 02-Aug-1993  mycroft Makefile
 1.4 16-Jun-1993  alm fixed regex initialization
added more support for BACKWARDS option
 1.3 08-May-1993  alm added support for lines of arbitrary length
added support for 8-bit data, including NULs
 1.2 10-Apr-1993  cgd updated to alm's ed-0.2
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.12.98.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.12.98.1 21-Apr-2020  martin Sync with HEAD
 1.8 21-Mar-1995  cgd branches: 1.8.116;
convert to new RCS id conventions.
 1.7 20-Feb-1994  alm `s3' applies the last substitution to the 3rd (or nth) match.
Split up the code to several files.
 1.6 23-Nov-1993  alm Added a line-addressing test so this isn't overlooked again.
Added BACKWARDS directive to Makefile for compatibility.
Completely changed the name space as per FSF's request.
Fixed a line-addressing bug introduced by previous `fix', i.e.,
space preceding a digit acts a plus (+) operator, as in `1 1' == `2',
and a plus operator by itself acts as +1, but a space by itself
should not equal a plus operator, i.e., `1 ' != `1+' == `2'.
 1.5 12-Nov-1993  alm rename !* -> bang* and \n* -> nl*
 1.4 27-May-1993  alm Added more tests...
 1.3 08-May-1993  alm added support for lines of arbitrary length
added support for 8-bit data, including NULs
 1.2 10-Apr-1993  cgd updated to alm's ed-0.2
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.8.116.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.8.116.1 21-Apr-2020  martin Sync with HEAD
 1.3 21-Mar-1995  cgd branches: 1.3.116;
convert to new RCS id conventions.
 1.2 16-Jun-1993  alm fixed regex initialization
added more support for BACKWARDS option
 1.1 27-May-1993  alm Added more tests...
 1.3.116.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.3.116.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 10-Apr-1993  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.2 10-Apr-1993  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 10-Apr-1993  cgd branches: 1.1.120;
updated to alm's ed-0.2
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 10-Apr-1993  cgd branches: 1.1.120;
updated to alm's ed-0.2
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 23-Nov-1993  alm branches: 1.1.118;
Added a line-addressing test so this isn't overlooked again.
Added BACKWARDS directive to Makefile for compatibility.
Completely changed the name space as per FSF's request.
Fixed a line-addressing bug introduced by previous `fix', i.e.,
space preceding a digit acts a plus (+) operator, as in `1 1' == `2',
and a plus operator by itself acts as +1, but a space by itself
should not equal a plus operator, i.e., `1 ' != `1+' == `2'.
 1.1.118.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.118.1 21-Apr-2020  martin Sync with HEAD
 1.1 23-Nov-1993  alm branches: 1.1.118;
Added a line-addressing test so this isn't overlooked again.
Added BACKWARDS directive to Makefile for compatibility.
Completely changed the name space as per FSF's request.
Fixed a line-addressing bug introduced by previous `fix', i.e.,
space preceding a digit acts a plus (+) operator, as in `1 1' == `2',
and a plus operator by itself acts as +1, but a space by itself
should not equal a plus operator, i.e., `1 ' != `1+' == `2'.
 1.1.118.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.118.1 21-Apr-2020  martin Sync with HEAD
 1.1 23-Nov-1993  alm branches: 1.1.118;
Added a line-addressing test so this isn't overlooked again.
Added BACKWARDS directive to Makefile for compatibility.
Completely changed the name space as per FSF's request.
Fixed a line-addressing bug introduced by previous `fix', i.e.,
space preceding a digit acts a plus (+) operator, as in `1 1' == `2',
and a plus operator by itself acts as +1, but a space by itself
should not equal a plus operator, i.e., `1 ' != `1+' == `2'.
 1.1.118.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.118.1 21-Apr-2020  martin Sync with HEAD
 1.1 27-May-1993  alm branches: 1.1.120;
Added more tests...
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 27-May-1993  alm branches: 1.1.120;
Added more tests...
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-May-1993  alm branches: 1.1.120;
added support for lines of arbitrary length
added support for 8-bit data, including NULs
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-May-1993  alm branches: 1.1.120;
added support for lines of arbitrary length
added support for 8-bit data, including NULs
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-May-1993  alm branches: 1.1.120;
added support for lines of arbitrary length
added support for 8-bit data, including NULs
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 12-Nov-1993  alm branches: 1.1.118;
rename !* -> bang* and \n* -> nl*
 1.1.118.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.118.1 21-Apr-2020  martin Sync with HEAD
 1.1 12-Nov-1993  alm branches: 1.1.118;
rename !* -> bang* and \n* -> nl*
 1.1.118.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.118.1 21-Apr-2020  martin Sync with HEAD
 1.1 12-Nov-1993  alm branches: 1.1.118;
rename !* -> bang* and \n* -> nl*
 1.1.118.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.118.1 21-Apr-2020  martin Sync with HEAD
 1.1 12-Nov-1993  alm branches: 1.1.118;
rename !* -> bang* and \n* -> nl*
 1.1.118.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.118.1 21-Apr-2020  martin Sync with HEAD
 1.2 20-Feb-1994  mycroft Clean up deleted files.
 1.1 12-Nov-1993  alm rename !* -> bang* and \n* -> nl*
 1.1 12-Nov-1993  alm branches: 1.1.118;
rename !* -> bang* and \n* -> nl*
 1.1.118.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.118.1 21-Apr-2020  martin Sync with HEAD
 1.2 20-Feb-1994  mycroft Clean up deleted files.
 1.1 12-Nov-1993  alm rename !* -> bang* and \n* -> nl*
 1.2 20-Feb-1994  mycroft Clean up deleted files.
 1.1 12-Nov-1993  alm rename !* -> bang* and \n* -> nl*
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 10-Apr-1993  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.2 20-Feb-1994  mycroft Clean up deleted files.
 1.1 10-Apr-1993  cgd updated to alm's ed-0.2
 1.2 10-Apr-1993  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-May-1993  alm branches: 1.1.120;
added support for lines of arbitrary length
added support for 8-bit data, including NULs
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-May-1993  alm branches: 1.1.120;
added support for lines of arbitrary length
added support for 8-bit data, including NULs
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.9 23-Apr-1995  cgd branches: 1.9.116;
clean up RCS Ids slightly, etc.
 1.8 21-Mar-1995  cgd convert to new RCS id conventions.
 1.7 20-Feb-1994  alm `s3' applies the last substitution to the 3rd (or nth) match.
Split up the code to several files.
 1.6 16-Jun-1993  alm fixed regex initialization
added more support for BACKWARDS option
 1.5 08-May-1993  alm added support for lines of arbitrary length
added support for 8-bit data, including NULs
 1.4 13-Apr-1993  alm cleaned up mkscripts.sh and ckscripts.sh
 1.3 13-Apr-1993  alm Added tests: \n[12].[drt] [ik]3.err
 1.2 10-Apr-1993  cgd updated to alm's ed-0.2
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.9.116.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.9.116.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 10-Apr-1993  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 10-Apr-1993  cgd branches: 1.1.120;
updated to alm's ed-0.2
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 10-Apr-1993  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 13-Apr-1993  alm branches: 1.2.120;
Added tests: \n[12].[drt] [ik]3.err
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.2.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.2.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 10-Apr-1993  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 10-Apr-1993  cgd branches: 1.1.120;
updated to alm's ed-0.2
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 10-Apr-1993  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 10-Apr-1993  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 10-Apr-1993  cgd branches: 1.1.120;
updated to alm's ed-0.2
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 10-Apr-1993  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 10-Apr-1993  cgd branches: 1.1.120;
updated to alm's ed-0.2
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 16-Jun-1993  alm branches: 1.1.120;
fixed regex initialization
added more support for BACKWARDS option
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 02-Jul-1993  mycroft Clean up deleted files.
 1.1 08-May-1993  alm added support for lines of arbitrary length
added support for 8-bit data, including NULs
 1.1 16-Jun-1993  alm branches: 1.1.120;
fixed regex initialization
added more support for BACKWARDS option
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 16-Jun-1993  alm branches: 1.1.120;
fixed regex initialization
added more support for BACKWARDS option
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 10-Apr-1993  cgd branches: 1.1.120;
updated to alm's ed-0.2
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 10-Apr-1993  cgd branches: 1.1.120;
updated to alm's ed-0.2
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 10-Apr-1993  cgd branches: 1.1.120;
updated to alm's ed-0.2
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 10-Apr-1993  cgd branches: 1.1.120;
updated to alm's ed-0.2
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 02-Jul-1993  alm branches: 1.1.120;
fixed undo within a global command (would corrupt the buffer)
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 23-Apr-1993  alm branches: 1.1.120;
added more tests
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 02-Jul-1993  alm branches: 1.1.120;
fixed undo within a global command (would corrupt the buffer)
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 02-Jul-1993  alm branches: 1.1.120;
fixed undo within a global command (would corrupt the buffer)
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 02-Jul-1993  alm branches: 1.1.120;
fixed undo within a global command (would corrupt the buffer)
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 02-Jul-1993  alm branches: 1.1.120;
fixed undo within a global command (would corrupt the buffer)
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 02-Jul-1993  alm branches: 1.1.120;
fixed undo within a global command (would corrupt the buffer)
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 12-Nov-1993  alm branches: 1.1.118;
rename !* -> bang* and \n* -> nl*
 1.1.118.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.118.1 21-Apr-2020  martin Sync with HEAD
 1.1 12-Nov-1993  alm branches: 1.1.118;
rename !* -> bang* and \n* -> nl*
 1.1.118.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.118.1 21-Apr-2020  martin Sync with HEAD
 1.1 12-Nov-1993  alm branches: 1.1.118;
rename !* -> bang* and \n* -> nl*
 1.1.118.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.118.1 21-Apr-2020  martin Sync with HEAD
 1.1 10-Apr-1993  cgd branches: 1.1.120;
updated to alm's ed-0.2
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 10-Apr-1993  cgd branches: 1.1.120;
updated to alm's ed-0.2
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 10-Apr-1993  cgd branches: 1.1.120;
updated to alm's ed-0.2
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 13-Apr-1993  alm branches: 1.1.120;
Added tests: \n[12].[drt] [ik]3.err
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 14-Apr-1993  alm branches: 1.1.120;
added tests for the `k' and `w' commands
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 14-Apr-1993  alm branches: 1.1.120;
added tests for the `k' and `w' commands
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 14-Apr-1993  alm branches: 1.1.120;
added tests for the `k' and `w' commands
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 10-Apr-1993  cgd branches: 1.1.120;
updated to alm's ed-0.2
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 10-Apr-1993  cgd branches: 1.1.120;
updated to alm's ed-0.2
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 13-Apr-1993  alm branches: 1.1.120;
Added tests: \n[12].[drt] [ik]3.err
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 14-Apr-1993  alm branches: 1.1.120;
added tests for the `k' and `w' commands
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 10-Apr-1993  cgd branches: 1.1.120;
updated to alm's ed-0.2
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.10 23-Apr-1995  cgd branches: 1.10.116;
clean up RCS Ids slightly, etc.
 1.9 21-Mar-1995  cgd convert to new RCS id conventions.
 1.8 20-Feb-1994  alm `s3' applies the last substitution to the 3rd (or nth) match.
Split up the code to several files.
 1.7 16-Jun-1993  alm fixed regex initialization
added more support for BACKWARDS option
 1.6 08-May-1993  alm added support for lines of arbitrary length
added support for 8-bit data, including NULs
 1.5 14-Apr-1993  alm added tests for the `k' and `w' commands
 1.4 13-Apr-1993  alm cleaned up mkscripts.sh and ckscripts.sh
 1.3 13-Apr-1993  alm Added tests: \n[12].[drt] [ik]3.err
 1.2 10-Apr-1993  cgd updated to alm's ed-0.2
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.10.116.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.10.116.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 12-Nov-1993  mycroft Clean up deleted files.
 1.1 10-Apr-1993  cgd updated to alm's ed-0.2
 1.1 12-Nov-1993  alm branches: 1.1.118;
rename !* -> bang* and \n* -> nl*
 1.1.118.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.118.1 21-Apr-2020  martin Sync with HEAD
 1.3 01-Mar-1995  mycroft Clean up deleted files.
 1.2 13-Apr-1993  alm Added tests: \n[12].[drt] [ik]3.err
 1.1 10-Apr-1993  cgd updated to alm's ed-0.2
 1.3 01-Mar-1995  mycroft Clean up deleted files.
 1.2 13-Apr-1993  alm Added tests: \n[12].[drt] [ik]3.err
 1.1 10-Apr-1993  cgd updated to alm's ed-0.2
 1.1 12-Nov-1993  alm branches: 1.1.118;
rename !* -> bang* and \n* -> nl*
 1.1.118.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.118.1 21-Apr-2020  martin Sync with HEAD
 1.1 12-Nov-1993  alm branches: 1.1.118;
rename !* -> bang* and \n* -> nl*
 1.1.118.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.118.1 21-Apr-2020  martin Sync with HEAD
 1.1 12-Nov-1993  alm branches: 1.1.118;
rename !* -> bang* and \n* -> nl*
 1.1.118.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.118.1 21-Apr-2020  martin Sync with HEAD
 1.1 12-Nov-1993  alm branches: 1.1.118;
rename !* -> bang* and \n* -> nl*
 1.1.118.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.118.1 21-Apr-2020  martin Sync with HEAD
 1.1 12-Nov-1993  alm branches: 1.1.118;
rename !* -> bang* and \n* -> nl*
 1.1.118.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.118.1 21-Apr-2020  martin Sync with HEAD
 1.1 12-Nov-1993  alm branches: 1.1.118;
rename !* -> bang* and \n* -> nl*
 1.1.118.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.118.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 23-Apr-1993  alm branches: 1.1.120;
added more tests
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 20-Feb-1994  mycroft Clean up deleted files.
 1.1 10-Apr-1993  cgd updated to alm's ed-0.2
 1.1 23-Apr-1993  alm branches: 1.1.120;
added more tests
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 23-Apr-1993  alm branches: 1.1.120;
added more tests
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-May-1993  alm branches: 1.1.120;
added support for lines of arbitrary length
added support for 8-bit data, including NULs
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 10-Apr-1993  cgd branches: 1.1.120;
updated to alm's ed-0.2
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 10-Apr-1993  cgd branches: 1.1.120;
updated to alm's ed-0.2
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 10-Apr-1993  cgd branches: 1.1.120;
updated to alm's ed-0.2
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 27-May-1993  alm branches: 1.1.120;
Added more tests...
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 10-Apr-1993  cgd branches: 1.1.120;
updated to alm's ed-0.2
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 27-May-1993  alm branches: 1.1.120;
Added more tests...
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 10-Apr-1993  cgd branches: 1.1.120;
updated to alm's ed-0.2
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 27-May-1993  alm branches: 1.1.120;
Added more tests...
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 27-May-1993  alm branches: 1.1.120;
Added more tests...
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 10-Apr-1993  cgd branches: 1.1.120;
updated to alm's ed-0.2
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 10-Apr-1993  cgd branches: 1.1.120;
updated to alm's ed-0.2
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 23-Apr-1993  alm branches: 1.1.120;
added more tests
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 23-Apr-1993  alm branches: 1.1.120;
added more tests
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 27-May-1993  alm branches: 1.1.120;
Added more tests...
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 27-May-1993  alm branches: 1.1.120;
Added more tests...
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 10-Apr-1993  cgd branches: 1.1.120;
updated to alm's ed-0.2
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 10-Apr-1993  cgd branches: 1.1.120;
updated to alm's ed-0.2
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 10-Apr-1993  cgd branches: 1.1.120;
updated to alm's ed-0.2
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 10-Apr-1993  cgd branches: 1.1.120;
updated to alm's ed-0.2
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 10-Apr-1993  cgd branches: 1.1.120;
updated to alm's ed-0.2
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 10-Apr-1993  cgd branches: 1.1.120;
updated to alm's ed-0.2
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 10-Apr-1993  cgd branches: 1.1.120;
updated to alm's ed-0.2
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 10-Apr-1993  cgd branches: 1.1.120;
updated to alm's ed-0.2
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 10-Apr-1993  cgd branches: 1.1.120;
updated to alm's ed-0.2
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.2 20-Feb-1994  mycroft Clean up deleted files.
 1.1 10-Apr-1993  cgd updated to alm's ed-0.2
 1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1 08-Apr-1993  cgd added ed, from Andrew Moore, alm@netcom.com
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 08-Apr-1993  cgd branches: 1.1.120;
added ed, from Andrew Moore, alm@netcom.com
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 14-Apr-1993  alm branches: 1.1.120;
added tests for the `k' and `w' commands
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 14-Apr-1993  alm branches: 1.1.120;
added tests for the `k' and `w' commands
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 14-Apr-1993  alm branches: 1.1.120;
added tests for the `k' and `w' commands
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 10-Apr-1993  cgd branches: 1.1.120;
updated to alm's ed-0.2
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.1 10-Apr-1993  cgd branches: 1.1.120;
updated to alm's ed-0.2
 1.1.120.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.120.1 21-Apr-2020  martin Sync with HEAD
 1.16 02-Feb-2018  mrg branches: 1.16.4;
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.)
 1.15 01-Apr-2016  mrg for GCC 5.3 pass -fwrapv as this relies upon well-defined integer overflow.
 1.14 19-Sep-2000  jdolecek reimplement expr using lexical parser generated by yacc
highlights:
* / is treated correctly depending upon context (addresses PR # 10995)
* use 64 bit arithmetic, so expr is able to process integer values from
-(2**63) to (2**63 - 1)
* checks for integer over- & underflows added
* error messages improved, more error checking added

add AUTHOR section to manpage
add BUGS section, and mention possible drawbacks with other expr implementations

XXX the old expr treated empty string in arithmetic expressions as if it was 0
XXX this behaviour has been retained
 1.13 20-Jul-1997  christos Remove WARNS=1 from all the subdirectory Makefiles, and add it to Makefile.inc
now that all /bin has been cleaned.
 1.12 20-Jul-1997  christos Fix compiler warnings; add WARNS=1
 1.11 21-Mar-1995  cgd convert to new RCS id conventions.
 1.10 16-Jul-1994  jtc Re-implemented with a recursive descent parser.
Fixed subtle bug wrt. comparing integers when either one hasn't been
coersed from a string --- while avoiding an even subtler bug found in
GNU expr.
 1.9 11-Nov-1993  jtc Use new POSIX.2 compatible regex routines in C library instead of -lgnuregex.
 1.8 06-Aug-1993  mycroft Oops. This one does need a single-entry SRCS, because it's generated at
compile time. Lose.
 1.7 02-Aug-1993  mycroft Add RCS identifiers, remove some completely useless RCS logs and patchkit
headers, and a few other insignificant changes.
 1.6 20-Jul-1993  jtc Add a manual page.
 1.5 14-Jun-1993  jtc Use POSIX regular expression routines.
Remove TODO file, as I've allready done everything that there was to do.
 1.4 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.3 22-Mar-1993  cgd added rcs ids to all files
 1.2 21-Mar-1993  cgd after 0.2.2 "stable" patches applied
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.16.4.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.16.4.1 21-Apr-2020  martin Sync with HEAD
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 01-Mar-1995  mycroft Clean up deleted files.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.41 28-Jun-2025  rillig expr.1: delete misleading example

The result of the example expression would be 0 even if the operator
precedence were inverted.

Fixes PR bin/57670.
 1.40 15-Mar-2025  rillig expr: improve portability, fix typo

Suggested by Robert Elz.
 1.39 15-Mar-2025  rillig expr.1: sync with latest changes, improve wording
 1.38 28-Aug-2022  hgutch branches: 1.38.4;
Change back various occurrences of \*[Le], \*[Ge] (less/greater equal)
and \*(ua (upwards arrow) to literal "<=", ">=" and "^" whenever
appropriate (e.g., in code examples).
 1.37 03-Jul-2017  wiz branches: 1.37.6;
Remove workaround for ancient HTML generation code.
 1.36 23-Aug-2016  sevan Mark email addresses as mailto links, heads up by Sascha Wildner.
Move email addresses to same line as author name.
Do not split the third author entry on to a new line.
Public domain is not licensed by definition, heads up by Robert Elz.
 1.35 23-Aug-2016  sevan Instruction to not split the line needs to be stated separately.
 1.34 23-Aug-2016  sevan Add HISTORY section
Credit author of initial implementation in AUTHORS section
Bump date
Remove contraction highlighted by textproc/igor
 1.33 12-Aug-2012  wiz Improvements: wording, punctuation, macro usage.

From patch by Bug Hunting.
 1.32 09-May-2012  jdf Remove an unnecessary space in the manpage. Patch provided by Bug Hunting.
 1.31 23-Mar-2011  dholland branches: 1.31.4;
minor usage nit
 1.30 24-May-2010  joerg Explicitly quote | and : meant as separate operator. Kill trailing
whitespace. Don't use \Z' with obscure character entity. Reorder sections
to canonical order.
 1.29 30-Apr-2008  martin Convert TNF licenses to new 2 clause variant
 1.28 23-Apr-2004  wiz branches: 1.28.24;
Grammar improvement by Patrick Welche.
 1.27 20-Apr-2004  wiz Add some articles; use No instead of Li in one place
(more correct, see e.g. PostScript output); bump date for previous.
 1.26 20-Apr-2004  jdolecek add support for 'length' keyword, for compatibility with GNU expr
 1.25 21-Dec-2003  wiz Add some articles.
 1.24 21-Dec-2003  jdolecek add a note explaining empty string matching via :
this adresses bin/23810 by VaX#n8

bump date and update copyright
 1.23 29-Mar-2003  perry spelling errors, some from Igor Sobrado in PR 19670
 1.22 14-Feb-2003  grant 'NetBSD.org'
 1.21 25-Sep-2002  wiz New policy: New sentences start on a new line.
Patches by Robert Elz <kre at munnari oz au>, with minimal changes by me.
 1.20 19-Feb-2002  pooka rewritten in NetBSD 1.6 -> rewritten for NetBSD 1.6
 1.19 08-Feb-2002  ross Generate <>& symbolically. I'm avoiding .../dist/... directories for now.
 1.18 20-Dec-2001  wiz Use standard headers, sort sections.
 1.17 18-Oct-2001  wiz Quote `:' correctly.
 1.16 07-May-2001  jdolecek Fix the operator precedence list to match reality. Operators were incorrectly
grouped there.
 1.15 30-Oct-2000  jdolecek list parenthesis usage similar way as other operators
document operator precedence
rename section BUGS to COMPATIBILITY, add paragraph about -- handling
 1.14 19-Sep-2000  jdolecek reimplement expr using lexical parser generated by yacc
highlights:
* / is treated correctly depending upon context (addresses PR # 10995)
* use 64 bit arithmetic, so expr is able to process integer values from
-(2**63) to (2**63 - 1)
* checks for integer over- & underflows added
* error messages improved, more error checking added

add AUTHOR section to manpage
add BUGS section, and mention possible drawbacks with other expr implementations

XXX the old expr treated empty string in arithmetic expressions as if it was 0
XXX this behaviour has been retained
 1.13 04-Sep-2000  kleink For commands and utilities, use EXIT STATUS rather than RETURN VALUES as
appropriate (and documented in mdoc(7)).
 1.12 28-Aug-2000  hubertf Add 'RETURN VALUE' section header.
 1.11 14-Jan-2000  mjl branches: 1.11.4;
On memory allocation failure, return 3 as per POSIX. (from OpenBSD)
 1.10 20-Oct-1997  enami Fix .Nm usage.
 1.9 28-Apr-1995  jtc Winning Strategies has placed this code into the Public Domain
 1.8 21-Mar-1995  cgd convert to new RCS id conventions.
 1.7 29-Jan-1994  jtc Fix spelling error in Copyright notice
 1.6 11-Jan-1994  jtc Fix spelling errors.
 1.5 04-Oct-1993  jtc Document the new, less restrictive, behavior of the matching operator.
 1.4 28-Aug-1993  jtc Document the fact that expr is POSIX.2 compliant.
 1.3 16-Aug-1993  jtc Document that string comparisons are done using the locale specific
collation sequence.
 1.2 02-Aug-1993  mycroft Makefile
 1.1 20-Jul-1993  jtc Add a manual page.
 1.11.4.1 28-Aug-2000  hubertf Pull up to netbsd-1-5 branch, OK'd by thorpej:

Log Message:
> Add 'RETURN VALUE' section header.

Files & Revisionis:
> cvs rdiff -r1.19 -r1.20 basesrc/bin/cat/cat.1
> cvs rdiff -r1.12 -r1.13 basesrc/bin/chmod/chmod.1
> cvs rdiff -r1.14 -r1.15 basesrc/bin/cp/cp.1
> cvs rdiff -r1.8 -r1.9 basesrc/bin/dd/dd.1
> cvs rdiff -r1.9 -r1.10 basesrc/bin/echo/echo.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/expr/expr.1
> cvs rdiff -r1.25 -r1.26 basesrc/bin/ls/ls.1
> cvs rdiff -r1.10 -r1.11 basesrc/bin/mkdir/mkdir.1
> cvs rdiff -r1.23 -r1.24 basesrc/bin/mt/mt.1
> cvs rdiff -r1.12 -r1.13 basesrc/bin/mv/mv.1
> cvs rdiff -r1.16 -r1.17 basesrc/bin/pwd/pwd.1
> cvs rdiff -r1.9 -r1.10 basesrc/bin/rm/rm.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/rmdir/rmdir.1
> cvs rdiff -r1.35 -r1.36 basesrc/bin/sh/sh.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/sleep/sleep.1
> cvs rdiff -r1.20 -r1.21 basesrc/bin/stty/stty.1
 1.28.24.1 18-May-2008  yamt sync with head.
 1.31.4.2 30-Oct-2012  yamt sync with head
 1.31.4.1 23-May-2012  yamt sync with head.
 1.37.6.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.37.6.1 21-Apr-2020  martin Sync with HEAD
 1.38.4.1 02-Aug-2025  perseant Sync with HEAD
 1.13 19-Sep-2000  jdolecek reimplement expr using lexical parser generated by yacc
highlights:
* / is treated correctly depending upon context (addresses PR # 10995)
* use 64 bit arithmetic, so expr is able to process integer values from
-(2**63) to (2**63 - 1)
* checks for integer over- & underflows added
* error messages improved, more error checking added

add AUTHOR section to manpage
add BUGS section, and mention possible drawbacks with other expr implementations

XXX the old expr treated empty string in arithmetic expressions as if it was 0
XXX this behaviour has been retained
 1.12 14-Jan-2000  mjl On memory allocation failure, return 3 as per POSIX. (from OpenBSD)
 1.11 09-Nov-1999  drochner Since our gcc doesn't warn about NULL format strings anymore, we can
fix the incorrect err(1, "%s", "") et al.
Closes PR bin/7592 by cgd.
 1.10 04-Nov-1998  christos branches: 1.10.6;
cast is*() to unsigned char
 1.9 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.8 28-Jul-1998  mycroft Delint.
 1.7 27-Jul-1998  mycroft Slight code reduction.
 1.6 27-Jul-1998  mycroft __AUDIT__ cleanup.
 1.5 20-Jul-1997  christos Fix compiler warnings; add WARNS=1
 1.4 04-Jun-1996  cgd don't spew an error message if setlocale(LC_ALL, ""); fails. This is for
consistency with other programs, and because currently it can never
actually succeeed if LANG is set to something other than "C". Sent
in by me in PR 2486, approved by jtc).
 1.3 28-Apr-1995  jtc branches: 1.3.6;
Winning Strategies has placed this code into the Public Domain
 1.2 21-Mar-1995  cgd convert to new RCS id conventions.
 1.1 16-Jul-1994  jtc Re-implemented with a recursive descent parser.
Fixed subtle bug wrt. comparing integers when either one hasn't been
coersed from a string --- while avoiding an even subtler bug found in
GNU expr.
 1.3.6.1 04-Jun-1996  cgd pull up from trunk:
>don't spew an error message if setlocale(LC_ALL, ""); fails. This is for
>consistency with other programs, and because currently it can never
>actually succeeed if LANG is set to something other than "C".
 1.10.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.55 29-Jun-2025  rillig expr: use characters instead of bytes for ":" and "length"

Required by POSIX.
 1.54 12-Apr-2025  rillig expr: sort grammar rules by operator precedence

For easier reading; no functional change intended.
 1.53 15-Mar-2025  rillig expr: ensure that the result of '|' is never empty

Required by POSIX since at least 2004.
 1.52 15-Mar-2025  rillig expr: only skip '--' once
 1.51 15-Mar-2025  rillig expr: clean up
 1.50 15-Mar-2025  rillig expr: don't evaluate irrelevant conditions
 1.49 15-Mar-2025  rillig expr: clean up grammar and code
 1.48 15-Mar-2025  rillig expr: extract expression evaluation functions from the grammar
 1.47 14-Mar-2025  rillig expr: fix out-of-bounds memory read for empty command line argument
 1.46 11-Jun-2020  kamil branches: 1.46.8;
Fix typo
 1.45 27-Jun-2018  kamil branches: 1.45.2;
Add a missing check to handle correctly 0 * 0 in expr(1)
 1.44 27-Jun-2018  kamil Improve the * operator handling in expr(1)

Fixes overflow detection in expressions INT * -UINT.

Detected with libFuzzer & UBSan.
 1.43 14-Jun-2018  christos remove notreached
 1.42 14-Jun-2018  christos return to caller instead of exit(2)
 1.41 13-Jun-2018  kamil Detect properly overflow in expr(1) for 0 + INT
 1.40 12-Jun-2018  kamil 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>
 1.39 05-Sep-2016  sevan branches: 1.39.12;
Drop main() prototype.
 1.38 15-Mar-2012  joerg Add __printflike attribution to use vprintf and friends with an argument
as format string.
 1.37 25-Aug-2011  joerg branches: 1.37.2;
Mark yyerror as dead.
 1.36 20-Jan-2009  joerg Revert for now, pending discussion of how expr should behave in the case
of pre-POSIX expressions.
 1.35 20-Jan-2009  joerg SUS says that expr must support "--" to prevent interpretation of
negative numbers as options.
 1.34 30-Apr-2008  martin Normalize TNF license, move to 2 clause.
 1.33 17-Mar-2006  rumble branches: 1.33.18;
Handle asprintf failing to allocate.
 1.32 01-Jun-2005  lukem appease gcc -Wuninitialized
 1.31 20-Apr-2004  jdolecek add support for 'length' keyword, for compatibility with GNU expr
 1.30 20-Mar-2004  jdolecek explicitly use REG_BASIC when calling regcomp(3), instead of 0
 1.29 14-Feb-2003  grant 'NetBSD.org'
 1.28 16-Sep-2001  wiz Use {g,s}etprogname, and some other KNF stuff. Patch by Petri Koistinen.
 1.27 06-May-2001  jmc Various cleanups/fixes.
Change the add/subtract overflow checks to use an unsigned to do the op
and then cast back into the signed var to check the signs.

Make multiply shortcut on either right or left being 0.
Make multiply's overflow test not fail on simple cases like 1 * -1
Make the multiple overflow test pass all the regress tests.
 1.26 05-May-2001  jmc Various changes to fix bugs in PR bin/12838.

Make sure all precendence is spelled out correctly (comparison does not
have the same level as the arithmetic operators..)
Break the arithemtic operators into 2 classes (+- are lower than */%)
Restructure the arithmetic code into a function either class above can call.
Finally, add a whole suite of regression tests (checked in separately) which
the previous code failed on 3 of them.
 1.25 25-Apr-2001  simonb Spell occurred correctly.
 1.24 04-Feb-2001  christos remove redundant declaration of yyparse
 1.23 30-Oct-2000  jdolecek add a hack to properly handle '--' as first argument -
it's ignored if it would cause syntax error, otherwise treated as common
string; this is so that both 'expr -- : .' and 'expr -- foo : .' works
This addresses standards/11230 by Ben Harris.

while here, make all global variables but main() static, use const more
 1.22 29-Oct-2000  thorpej The "&" operator has a higer precedence than "|".
 1.21 27-Oct-2000  jdolecek also credit J.T. Conklin in copyright message
 1.20 26-Oct-2000  jdolecek fix operator precedence - | or & have lower priority than
arithmetic operations, compare, or :
This should fix bin/11318 by Love <lha@stacken.kth.se>
 1.19 29-Sep-2000  jdolecek in regexp handling code, fix the format passed to asprintf() to be
actually in intended form - use "%.*s" and not incorrect "%*s"

Bug found by Launey Thomas <ljt@alum.mit.edu> and reported in private e-mail.
 1.18 21-Sep-2000  jdolecek Fix bug in regexp handling, caused not quite complete conversion
from old expr.c. This fixes PR # 11060.

While here, convert the code to use asprintf() instead of strdup()
followed by truncating of new string and move definition of errstr
to block where it's used.
 1.17 19-Sep-2000  jdolecek reimplement expr using lexical parser generated by yacc
highlights:
* / is treated correctly depending upon context (addresses PR # 10995)
* use 64 bit arithmetic, so expr is able to process integer values from
-(2**63) to (2**63 - 1)
* checks for integer over- & underflows added
* error messages improved, more error checking added

add AUTHOR section to manpage
add BUGS section, and mention possible drawbacks with other expr implementations

XXX the old expr treated empty string in arithmetic expressions as if it was 0
XXX this behaviour has been retained
 1.16 16-Jul-1994  mycroft Clean up deleted files.
 1.15 10-Jul-1994  cgd don't forget a cast, and thereby fix the regexp problems on big-endian machines
 1.14 16-Nov-1993  jtc branches: 1.14.2;
Fix insignificant memory leak.
 1.13 04-Oct-1993  jtc Allow expressions like "expr 'ABC' : '^.*$' to work as is done in other
expr implementations.
 1.12 14-Sep-1993  jtc Fix grammar to eliminate support for unary minus expressions -- they
weren't supported, they aren't standard, and they caused expr to dump
core.
 1.11 17-Aug-1993  jtc Use err() routine for error messages.
If string does not match \( \) subexpression, return a null string.
 1.10 16-Aug-1993  jtc expr is supposed to do string comparisons using the locale specific
collation sequence, so use strcoll instead of strcmp.
 1.9 20-Jul-1993  jtc Now truely POSIX 1003.2 (D11.2) compliant.
 1.8 20-Jul-1993  jtc Don't coerce strings to int in comparisons.
 1.7 20-Jul-1993  jtc integer arguments were not coerced to strings for the `:' operator.
bad things: core dumps, etc. will happen if integer values make it
up the parse tree.
 1.6 14-Jun-1993  jtc Updated to POSIX regular expression routines.
(Previous checkin was aborted by stray ^C)
 1.5 05-Jun-1993  cgd update for latest version of patches from jtconklin@kaleida.com
 1.4 23-Mar-1993  cgd added J.T. Conklin's changes to get parens working fine, etc.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.14.2.1 10-Jul-1994  cgd fix regexp problems. from trunk.
 1.33.18.1 18-May-2008  yamt sync with head.
 1.37.2.1 17-Apr-2012  yamt sync with head
 1.39.12.2 28-Jul-2018  pgoyette Sync with HEAD
 1.39.12.1 25-Jun-2018  pgoyette Sync with HEAD
 1.45.2.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.45.2.1 21-Apr-2020  martin Sync with HEAD
 1.46.8.1 02-Aug-2025  perseant Sync with HEAD
 1.1 16-May-2020  christos Add ACL support for FFS. From FreeBSD.
 1.2 18-Jun-2020  wiz Fix xrefs, comment out acl(9) which we don't have.
 1.1 16-May-2020  christos Add ACL support for FFS. From FreeBSD.
 1.2 22-May-2020  joerg Don't return from dead functions.
 1.1 16-May-2020  christos Add ACL support for FFS. From FreeBSD.
 1.8 20-Jul-1997  christos branches: 1.8.112;
Remove WARNS=1 from all the subdirectory Makefiles, and add it to Makefile.inc
now that all /bin has been cleaned.
 1.7 20-Jul-1997  christos Add WARNS=1
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.8.112.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.8.112.1 21-Apr-2020  martin Sync with HEAD
 1.20 13-Feb-2014  elric branches: 1.20.26;
Remove options added in 1.18, commitid: UhxHPgtT2Pzeg4Yw due to some
level of controversy about their inclusion.
 1.19 19-Jul-2013  wiz Sort.
 1.18 19-Jul-2013  roy Add the following options
-A Display the FQDN of each address on all interfaces.
-a Display alias name(s) of the host.
-d Display the DNS domain.
-f Display the FQDN for the hostname.
-I Display each IP address on all interfaces.
-i Display the IP address(es) for the hostname.
 1.17 07-Aug-2003  agc branches: 1.17.66;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.16 25-Sep-2002  wiz New policy: New sentences start on a new line.
Patches by Robert Elz <kre at munnari oz au>, with minimal changes by me.
 1.15 27-Feb-2001  mason Update to reflect reality - s,/etc/netstart,/etc/rc.d/network,
 1.14 22-Mar-1999  garbled More and more .Os cleanups. .Os is defined in the tmac.doc-common file,
so we shouldn't override it with versions in the manpages. Many more to
come.
 1.13 20-Oct-1997  enami Fix .Nm usage.
 1.12 23-Jun-1997  pk Xr to [gs]ethostname in section 3 (from PR#3627)
 1.11 07-Sep-1995  jtc Sync with 4.4lite2
 1.10 25-Jul-1995  jtc Use "utility" instead of "command". Modern definitions of these terms
are distinct (See POSIX.2 glossary).

A utility is a executable, script or shell builtin; while a command
can be any of those things plus lists, pipelines, compound commands
(if, for, while) and shell function definitions.
 1.9 21-Mar-1995  cgd convert to new RCS id conventions.
 1.8 26-Sep-1994  mycroft Small grammar fix.
 1.7 22-Sep-1994  mycroft Reference domainname(1), and check for too many arguments.
 1.6 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.5 10-Sep-1993  jtc Include appropriate header files to bring prototypes into scope.
Cleaned up man page.
 1.4 01-Aug-1993  mycroft Add RCS indentifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.3 07-Sep-1995  jtc imported from 44lite2
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.17.66.1 23-Jul-2013  riastradh sync with HEAD
 1.20.26.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.20.26.1 21-Apr-2020  martin Sync with HEAD
 1.21 13-Feb-2014  elric branches: 1.21.26;
Remove options added in 1.18, commitid: UhxHPgtT2Pzeg4Yw due to some
level of controversy about their inclusion.
 1.20 19-Jul-2013  christos fix error handling.
 1.19 19-Jul-2013  wiz Sort.
 1.18 19-Jul-2013  roy Add the following options
-A Display the FQDN of each address on all interfaces.
-a Display alias name(s) of the host.
-d Display the DNS domain.
-f Display the FQDN for the hostname.
-I Display each IP address on all interfaces.
-i Display the IP address(es) for the hostname.
 1.17 29-Aug-2011  joerg branches: 1.17.12;
Use __dead
 1.16 20-Jul-2008  lukem Remove the \n and tabs from the __COPYRIGHT() strings.
 1.15 07-Aug-2003  agc branches: 1.15.32;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.14 16-Sep-2001  wiz ANSIfy and KNF fixes, use {g,s}etprogname. Patch by Petri Koistinen.
 1.13 28-Jul-1998  mycroft Delint.
 1.12 06-Jul-1998  mrg - use an array MAXHOSTNAMELEN+1 size to hold hostnames
- ensure hostname from gethostname() is nul-terminated in all cases
- minor KNF
- use MAXHOSTNAMELEN over various other values/defines
- be safe will buffers that hold hostnames
 1.11 20-Jul-1997  christos Add WARNS=1
 1.10 07-Sep-1995  jtc Sync with 4.4lite2
 1.9 21-Mar-1995  cgd convert to new RCS id conventions.
 1.8 26-Sep-1994  mycroft Make the usage messages more closely match the man pages.
 1.7 22-Sep-1994  mycroft Reference domainname(1), and check for too many arguments.
 1.6 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.5 10-Sep-1993  jtc Include appropriate header files to bring prototypes into scope.
Cleaned up man page.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.3 07-Sep-1995  jtc imported from 44lite2
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.15.32.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.17.12.1 23-Jul-2013  riastradh sync with HEAD
 1.21.26.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.21.26.1 21-Apr-2020  martin Sync with HEAD
 1.8 20-Jul-1997  christos branches: 1.8.112;
Remove WARNS=1 from all the subdirectory Makefiles, and add it to Makefile.inc
now that all /bin has been cleaned.
 1.7 20-Jul-1997  christos Add WARNS=1
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.8.112.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.8.112.1 21-Apr-2020  martin Sync with HEAD
 1.35 07-Aug-2022  andvar kill(1): s/proceses/processes/
 1.34 15-Aug-2021  christos restore a bit of history.
 1.33 30-Aug-2020  wiz Mark up path with Pa and use Ev for environment variable.
 1.32 30-Aug-2020  kre Note that negative pid args are permitted (indicating to send to the pgrp
abs(pid)) and indicate that -- is (strictly) needed if the first pid arg
(there often is only one) is negative - though this implementation works
without it if a signal to send has been explicitly given, but whereas
'kill 1234" is valid (send SIGTERM to pid 1234) "kill -1234" will generate
a usage error from the attempt to send signal 1234 to nothing, to send
SIGTERM to pgrp 1234 it needs to be "kill -- -1234" (or "kill -s term -1234").

While here do a couple of markup improvements, and allow for the
possibility that users might be running the builtin kill from some
shell other than csh or sh.
 1.31 30-Aug-2020  kre Use the POSIX specified format if POSIXLY_CORRECT is set in the
environment, rather than the nicer layout that is normally used.

Note this applies to /bin/kill only, the builtin kill in sh uses its
"posix" option for the same purpose, the one in csh only ever uses
POSIX format.
 1.30 06-May-2020  kre Bump date for previous change... I frequently forget that bit.
 1.29 06-May-2020  kre kill is built-in to more than just csh(1).
While here, add missing Xr sh 1 (which was previously needed, moreso now)
and also include STOP and CONT in the list of common signals.
 1.28 22-Apr-2017  christos branches: 1.28.12;
Clarify 0
 1.27 22-Apr-2017  riastradh Clarify `kill -0'. Note standard exit codes.
 1.26 02-Oct-2016  abhinav kill(1) is a utility, not a function.
 1.25 11-Aug-2016  sevan Bump date
 1.24 11-Aug-2016  sevan Earliest reference of kill in the TUHS archive is v3 AT&T UNIX.
Unfortunately only the source code for the compiler is present for that
release. The v2 archive is missing the manuals & only contains a some of the
commands.
Switch to v3 and reference manual category eight.
 1.23 11-Aug-2016  sevan Pp not required before Bd, picked up by mandoc lint
 1.22 22-Mar-2012  wiz branches: 1.22.16;
Fix whitespace nits. Suggested by Bug Hunting.
 1.21 28-Dec-2007  wiz branches: 1.21.26;
Wordo fix, noted by jmc@openbsd.
 1.20 27-Dec-2007  wiz Fix argument confusion, noted by drochner.
 1.19 07-Nov-2007  wiz Improve -l description. From jmc@openbsd.
 1.18 07-Aug-2003  agc branches: 1.18.22;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.17 25-Feb-2003  wiz .Nm does not need a dummy argument ("") before punctuation or
for correct formatting of the SYNOPSIS any longer.
 1.16 04-Mar-2002  ad Xref pkill/pgrep.
 1.15 20-Dec-2001  wiz Whitespace nits.
 1.14 20-Dec-2001  kleink Deocument the meaning of a pid value of 0 and remove the BUGS section;
mostly from Jonathan Perkin in resolution of PR bin/15008.
 1.13 13-May-2000  christos s/exit_status/signal_number/
 1.12 06-Oct-1999  jdolecek xref signal(7)
 1.11 18-Sep-1999  ad branches: 1.11.2;
Consistancy nits.
 1.10 20-Oct-1997  enami Fix .Nm usage.
 1.9 26-Dec-1996  mikel reference sigaction(2) instead of the obsolete sigvec(3); fixes PR 3058.
 1.8 07-Sep-1995  jtc Sync with 4.4lite2
 1.7 21-Mar-1995  cgd convert to new RCS id conventions.
 1.6 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.5 01-Aug-1993  mycroft Add RCS indentifiers.
 1.4 22-Jul-1993  jtc Make POSIX 1003.2 (D11.2) compliant.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.3 07-Sep-1995  jtc imported from 44lite2
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.11.2.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.18.22.1 09-Jan-2008  matt sync with HEAD
 1.21.26.1 17-Apr-2012  yamt sync with head
 1.22.16.2 26-Apr-2017  pgoyette Sync with HEAD
 1.22.16.1 04-Nov-2016  pgoyette Sync with HEAD
 1.28.12.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.28.12.1 21-Apr-2020  martin Sync with HEAD
 1.33 16-May-2022  kre Alter error messages so they no longer claim that bad input is illegal.
 1.32 30-Aug-2020  kre Every integer that fits within a pid_t is a potential "pid" arg to kill.
That means we cannot use (pid_t)-1 as an error indicator, as that's a
valid pid to use (described as working in kill(1) - yet it wasn't working
in /bin/kill (nor sh's builtin kill, which is essentially the same code).
This is even required to work by POSIX.

So change processnum() (the parser/validator for the pid args) to take
a pointer to a pid_t and return the pid that way, leaving the return value
of the (now int) function to indicate just ok/error. While here, fix
the validation a little ('' is no longer an accepted alias for 0) and in
case of an error from kill(2) have the error message indicate whether the
kill was targeted at a pid of a pgrp.
 1.31 30-Aug-2020  kre Use the POSIX specified format if POSIXLY_CORRECT is set in the
environment, rather than the nicer layout that is normally used.

Note this applies to /bin/kill only, the builtin kill in sh uses its
"posix" option for the same purpose, the one in csh only ever uses
POSIX format.
 1.30 12-Dec-2018  kre 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...
 1.29 28-Oct-2018  kre 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.
 1.28 26-Jun-2017  kre branches: 1.28.4; 1.28.6;
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.)
 1.27 29-Aug-2011  joerg branches: 1.27.36;
Use __dead
 1.26 01-Oct-2009  spz Make sure that numerical signals and pids are in range for their types.
Fixes PR bin/42143
 1.25 20-Jul-2008  lukem branches: 1.25.4; 1.25.10;
Remove the \n and tabs from the __COPYRIGHT() strings.
 1.24 17-Mar-2006  christos branches: 1.24.4; 1.24.20;
Coverity CID 2356: Remove bogus test !*argv; also cleanup string usage.
 1.23 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.22 04-Aug-2003  jschauma As discusses a little while back on tech-userlevel:

If stdout is a tty, use vis(3) to print any filenames to prevent garbage
from being printed if the filename contains control- or other non-printable
characters.

While here, sprinkle some EXIT_FAILURE and NOTREACHED where appropriate.
 1.21 25-Nov-2002  christos sent cont to jobs so that they can exit without an fg when killed.
 1.20 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.19 16-Sep-2001  wiz Use {g,s}etprogname. Patch by Petri Koistinen.
 1.18 29-Jul-2001  wiz ANSIfy, some style improvements. Some parts from #13593 by Petri Koistinen.
 1.17 04-Nov-1998  christos unsigned char casts for is*()
 1.16 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.15 28-Jul-1998  mycroft Delint.
 1.14 28-Jul-1998  mycroft Delint.
 1.13 20-Jul-1997  christos Add WARNS=1
 1.12 22-Feb-1997  thorpej Fix a typo that caused lack of argument to be misinterpreted as "0".
From Arne H. Juul <arnej@pvv.ntnu.no>, PR #3236.
 1.11 07-Sep-1995  jtc Sync with 4.4lite2
 1.10 21-Mar-1995  cgd convert to new RCS id conventions.
 1.9 17-Mar-1995  jtc Changed to accept ``kill -0 pid'' (PR #842).
 1.8 04-Dec-1994  cgd from James Jegers <jimj@miller.cs.uwm.edu>: quiet -Wall, and squelch
some of the worst style errors.
 1.7 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.6 06-Aug-1993  mycroft Use sys_signame[].
 1.5 01-Aug-1993  mycroft Add RCS identifiers.
 1.4 22-Jul-1993  jtc Make POSIX 1003.2 (D11.2) compliant.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.3 07-Sep-1995  jtc imported from 44lite2
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.24.20.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.24.4.1 18-Oct-2009  bouyer Pull up following revision(s) (requested by spz in ticket #1361):
bin/kill/kill.c: revision 1.26
Make sure that numerical signals and pids are in range for their types.
Fixes PR bin/42143
 1.25.10.1 21-Apr-2010  matt sync to netbsd-5
 1.25.4.1 03-Oct-2009  snj Pull up following revision(s) (requested by spz in ticket #1043):
bin/kill/kill.c: revision 1.26
Make sure that numerical signals and pids are in range for their types.
Fixes PR bin/42143
 1.27.36.1 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.28.6.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.28.6.2 21-Apr-2020  martin Sync with HEAD
 1.28.6.1 10-Jun-2019  christos Sync with HEAD
 1.28.4.2 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.28.4.1 26-Nov-2018  pgoyette Sync with HEAD, resolve a couple of conflicts
 1.39 05-Dec-2024  rillig ksh: suppress the 435 lint warnings about identifier list definitions

Leave the actual work of modernizing the function definitions to some
later day.
 1.38 05-Dec-2024  rillig ksh: clean up Makefile
 1.37 03-Jun-2023  lukem branches: 1.37.2;
adapt to ${CC_WNO_IMPLICIT_FALLTHROUGH}

Use ${CC_WNO_IMPLICIT_FALLTHROUGH} instead of
the older style more complex expressions.
 1.36 29-Sep-2019  mrg convert HAVE_GCC == 7 to HAVE_GCC >= 7.
 1.35 04-Feb-2019  mrg - use -Wno-error=implicit-fallthrough with GCC7.
 1.34 30-Jun-2017  kamil branches: 1.34.6;
ksh: Drop support for OSes without POSIX sigaction(2)
 1.33 16-Mar-2016  christos Put back awk, other scripts need it.
 1.32 16-Mar-2016  christos We don't need all this magic to build the signals lists. Do the work at
compile time.
 1.31 29-Jan-2015  joerg Make cast warnings for clang non-fatal.
 1.30 16-Oct-2011  joerg branches: 1.30.22;
Don't use non-literal format strings.
 1.29 14-Aug-2011  christos kill gcc-4.5 hack.
 1.28 22-Jun-2011  mrg apply some -Wno-error and/or -fno-strict-aliasing.


all of this should be looked at closer, but some of them are not
very trivial.
 1.27 26-May-2011  joerg Default to -Wno-sign-compare -Wno-pointer-sign for clang.
Push -Wno-array-bounds down to the cases that depend on it.
Selectively disable warnings for 3rd party software or non-trivial
issues to be reviewed later to get clang -Werror to build most of the
tree.
 1.26 14-Feb-2009  lukem Default to WARNS=4 (except for rcp & ksh)
 1.25 25-Oct-2008  apb branches: 1.25.4;
Pass SED=${TOOL_SED:Q} in the environment to scripts run during the build.
 1.24 20-Oct-2008  apb Pass AWK in environment to shell scripts run during the build.
 1.23 26-Jun-2005  christos warns=3
- remove param names from function decls
- sprinkle const
- rename shadowed variables
- XXX: there is a lot of fishy __UNCONST going on.
 1.22 26-Oct-2003  lukem don't need -DHAVE_CONFIG_H here
 1.21 26-Oct-2003  lukem Use ${HOST_SH} instead of `sh'.

If necessary, pull in <bsd.sys.mk> to get the definition of HOST_SH;
Makefiles that pull in one of (most of) <bsd.*.mk> will get this anyway.
 1.20 21-Oct-2003  lukem Rework how MAKEVERBOSE operates:

* Don't bother prefixing commands with a line of ${_MKCMD}\
and instead rely upon "make -s". This is less intrusive on
all the Makefiles than the former. Idea from David Laight.

* Rename the variables use to print messages. The scheme now is:
_MKMSG_FOO Run _MKMSG 'foo'
_MKTARGET_FOO Run _MKMSG_FOO ${.TARGET}
From discussion with Alistair Crooks.
 1.19 19-Oct-2003  lukem rework to use the newer _MKMSGCREATE (et al) macros
 1.18 19-Oct-2003  lukem support MAKEVERBOSE
 1.17 01-Aug-2003  lukem Rework how dependency generation is performed:

* DPSRCS contains extra dependencies, but is _NOT_ added to CLEANFILES.
This is a change of behaviour. If a Makefile wants the clean semantics
it must specifically append to CLEANFILES.
Resolves PR toolchain/5204.

* To recap: .d (depend) files are generated for all files in SRCS and DPSRCS
that have a suffix of: .c .m .s .S .C .cc .cpp .cxx

* If YHEADER is set, automatically add the .y->.h to DPSRCS & CLEANFILES

* Ensure that ${OBJS} ${POBJS} ${LOBJS} ${SOBJS} *.d depend upon ${DPSRCS}

* Deprecate the (short lived) DEPENDSRCS


Update the various Makefiles to these new semantics; generally either
adding to CLEANFILES (because DPSRCS doesn't do that anymore), or replacing
specific .o dependencies with DPSRCS entries.

Tested with "make -j 8 distribution" and "make distribution".
 1.16 03-Nov-2001  lukem default to WARNS=1 if not set
 1.15 15-May-2001  simonb Don't build ksh.1 if MKMAN == "no".
 1.14 26-Jun-2000  kleink $(CPP) -> $(CC) -E
 1.13 22-Jun-2000  soren CLEANFILES+= siglist.out emacs.out
 1.12 20-Oct-1999  hubertf branches: 1.12.6;
resolve conflicts
 1.11 22-Oct-1997  lukem branches: 1.11.4;
add dependancies to fix parallel compilation
 1.10 10-Oct-1997  christos branches: 1.10.2;
CFLAGS->CPPFLAGS
 1.9 20-Jul-1997  christos Remove WARNS=1 from all the subdirectory Makefiles, and add it to Makefile.inc
now that all /bin has been cleaned.
 1.8 20-Jul-1997  christos Fix compiler warnings
Add WARNS=1
 1.7 30-Jun-1997  phil Define USETBL to get tbl used in making man page. (Closed PR 3144.)
 1.6 09-May-1997  mycroft Use DPSRCS.
 1.5 09-May-1997  mycroft Eliminate bogus redefinitions of standard targets.
 1.4 16-Jan-1997  perry close pr-3111 -- use sh to invoke script instead of executing
 1.3 12-Jan-1997  tls RCS ID Police
 1.2 10-Oct-1996  jtc Removed missing.c beacuse NetBSD is not missing any of the functions
that it provides.
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.10.2.1 07-Nov-1997  lukem pull up rev 1.11 from trunk (approval by thorpej)
 1.11.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.12.6.2 26-Feb-2002  he Pull up revisions 1.14-1.16 (requested by dogcow):
Synchronize with current development version:
o fix problem with ``set -x'' causing a core dump; fixes PR#11361
o fix problem with ``set -e'' causing early exit; fixes PR#11542
o fix various misspellings and nested extern declarations
 1.12.6.1 03-Sep-2000  soren Pull up rev 1.13:
> date: 2000/06/22 05:42:22; author: soren; state: Exp; lines: +3 -1
> CLEANFILES+= siglist.out emacs.out
 1.25.4.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.30.22.1 14-May-2015  snj Pull up following revision(s) (requested by joerg in ticket #758):
bin/ksh/Makefile: revision 1.31
bin/ps/print.c: revision 1.123
external/bsd/iscsi/dist/src/lib/parameters.c: revision 1.4
external/bsd/iscsi/dist/src/lib/target.c: revision 1.9
external/gpl2/lvm2/dist/lib/commands/toolcontext.c: revision 1.8
external/gpl2/lvm2/dist/lib/format1/import-export.c: revision 1.2
gnu/dist/texinfo/makeinfo/xml.c: revision 1.2
sbin/bioctl/bioctl.c: revision 1.16
usr.sbin/isdn/isdnd/msghdl.c: revision 1.12
Drop pointer checks that are always true.
--
Comment out impossible code.
--
Remove tautologic check.
--
Skip vendor if the string is empty.
Use l_wmesg if the string is not empty. Don't bother checking l_name for
nullness.
--
Array can't be null.
--
Show display if it is not empty.
--
Make cast warnings for clang non-fatal.
 1.34.6.4 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.34.6.3 21-Apr-2020  martin Sync with HEAD
 1.34.6.2 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.34.6.1 10-Jun-2019  christos Sync with HEAD
 1.37.2.1 02-Aug-2025  perseant Sync with HEAD
 1.10 12-Dec-2007  lukem branches: 1.10.70;
use __RCSID()
 1.9 01-Apr-2006  christos branches: 1.9.10;
Coverity CID 1844: Add annotations for aresize.
 1.8 26-Mar-2006  christos correct coverity model.
 1.7 21-Mar-2006  christos Add coverity model annotations
 1.6 07-Jul-2004  mycroft Cleanup of ksh memory handling from OpenBSD, via Stefan Krueger in PR 24962.
Should also fix:
PR 8153
PR 13301
PR 15256
PR 25084
 1.5 23-Jun-2003  agc Add NetBSD RCS Ids.
 1.4 20-Oct-1999  hubertf resolve conflicts
 1.3 20-Jul-1997  christos branches: 1.3.6;
Fix compiler warnings
Add WARNS=1
 1.2 12-Jan-1997  tls RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.2 20-Oct-1999  hubertf Import pdksh V5.2.14.
Includes lots of bugfixes.
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.3.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.9.10.1 09-Jan-2008  matt sync with HEAD
 1.10.70.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.10.70.1 21-Apr-2020  martin Sync with HEAD
 1.30 16-Sep-2021  christos support -Z
 1.29 03-Jun-2018  kamil branches: 1.29.2;
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>
 1.28 08-May-2018  kamil 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
 1.27 24-Jan-2018  kamil branches: 1.27.2;
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>
 1.26 30-Jun-2017  kamil ksh: Upgrade to C99 <stdbool.h>

This shell already used C99 functions.
 1.25 30-Jun-2017  kamil ksh: Drop support for systems without <sys/stat.h>

In future the order of includes will be normalized with KNF.

No functional change intended.
 1.24 22-Jun-2017  kamil Drop Ultrix support from ksh(1), a DEC UNIX for VAX
 1.23 22-Jun-2017  kamil Reapply removal of code from 90ties for OS/2 Cygwin AIX HPUX SCOUnix

Added missing #endif terminator in emacs.c
 1.22 22-Jun-2017  kamil Temporarily revert previous.

emacs.* gets wrong code in generation
 1.21 22-Jun-2017  kamil Drop HP-UX support from ksh(1)

OK by <kre>
 1.20 22-Jun-2017  kamil Remove os2 support in ksh(1)

OK by <kre>
 1.19 22-Jun-2017  kamil Remove ancient cygwin support in ksh(1)

OK by <kre>
 1.18 16-Oct-2011  joerg Don't use non-literal format strings.
 1.17 31-Aug-2011  plunky NULL does not need a cast
 1.16 25-Apr-2009  lukem Fix most of the -Wsign-compare issues.
 1.15 24-Apr-2006  christos branches: 1.15.28;
Coverity CID 2993: Fix memory leak.
 1.14 20-Mar-2006  christos Coverity CID 1827: Plug memory leak.
 1.13 26-Jun-2005  christos warns=3
- remove param names from function decls
- sprinkle const
- rename shadowed variables
- XXX: there is a lot of fishy __UNCONST going on.
 1.12 28-Oct-2004  dsl Add (unsigned char) casts to tolower/toupper and iscntrl calls.
Fixes PR 27593
 1.11 07-Jul-2004  mycroft Cleanup of ksh memory handling from OpenBSD, via Stefan Krueger in PR 24962.
Should also fix:
PR 8153
PR 13301
PR 15256
PR 25084
 1.10 05-Jan-2004  jmmv Homogenize usage messages: make the 'usage' word all lowercase, as this seems
to be the most common practice in our tree.
 1.9 25-Aug-2003  wiz From OpenBSD:
date: 2000/11/21 22:41:03; author: millert; state: Exp; lines: +2 -1
Add missing break that broke 'kill -s SIGNAME', bug report #1495
Fix from Hiroyuki.HORINOKUCHI@nrj.ericsson.se
 1.8 23-Jun-2003  agc Add NetBSD RCS Ids.
 1.7 25-Sep-2002  provos change complete-list to first complete and then print a list,
also complete after '=' (dd), and ':' (ssh) and ` (backtick)
improvements from camield@openbsd.org. approved by perry and thorpej.
 1.6 11-Jun-2001  wiz Fix various misspellings of compatible/compatibility.
 1.5 20-Oct-1999  hubertf branches: 1.5.6;
resolve conflicts
 1.4 04-Nov-1998  christos branches: 1.4.4;
char->unsigned char casts for is*()
 1.3 19-Aug-1998  thorpej Add some braces to make egcs happy.
 1.2 12-Jan-1997  tls RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.4 20-Oct-1999  hubertf Import pdksh V5.2.14.
Includes lots of bugfixes.
 1.1.1.3 18-Dec-1996  jtc import pdksh 5.2.12
 1.1.1.2 09-Oct-1996  jtc import pdksh 5.2.9
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.4.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.5.6.1 26-Feb-2002  he Pull up revision 1.6 (requested by dogcow):
Synchronize with current development version:
o fix problem with ``set -x'' causing a core dump; fixes PR#11361
o fix problem with ``set -e'' causing early exit; fixes PR#11542
o fix various misspellings and nested extern declarations
 1.15.28.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.27.2.2 25-Jun-2018  pgoyette Sync with HEAD
 1.27.2.1 21-May-2018  pgoyette Sync with HEAD
 1.29.2.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.29.2.1 21-Apr-2020  martin Sync with HEAD
 1.24 08-May-2018  kamil branches: 1.24.2;
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
 1.23 30-Jun-2017  kamil branches: 1.23.4;
ksh: Upgrade to C99 <stdbool.h>

This shell already used C99 functions.
 1.22 30-Jun-2017  kamil ksh: Drop support for systems without <sys/stat.h>

In future the order of includes will be normalized with KNF.

No functional change intended.
 1.21 30-Jun-2017  kamil ksh: Drop support for systems with broken <sys/time.h> / <time.h>
 1.20 30-Jun-2017  kamil 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.
 1.19 22-Jun-2017  kamil Reapply removal of code from 90ties for OS/2 Cygwin AIX HPUX SCOUnix

Added missing #endif terminator in emacs.c
 1.18 22-Jun-2017  kamil Temporarily revert previous.

emacs.* gets wrong code in generation
 1.17 22-Jun-2017  kamil Remove os2 support in ksh(1)

OK by <kre>
 1.16 11-Oct-2016  dholland PR 49595 William Ahern: The exit status of "unset NOTSET" should be 0, not 1.
(like 48312 but for ksh)
 1.15 18-Oct-2013  christos branches: 1.15.8;
Fixed unused warnings.
 1.14 31-Aug-2011  plunky branches: 1.14.2; 1.14.8;
NULL does not need a cast
 1.13 25-Apr-2009  lukem Fix most of the -Wsign-compare issues.
 1.12 01-Apr-2006  christos branches: 1.12.28;
Coverity CID 1836: Free Source after return from shell.
 1.11 18-Mar-2006  christos Coverity CID 877: Prevent NULL deref.
 1.10 26-Jun-2005  christos warns=3
- remove param names from function decls
- sprinkle const
- rename shadowed variables
- XXX: there is a lot of fishy __UNCONST going on.
 1.9 07-Jul-2004  mycroft Cleanup of ksh memory handling from OpenBSD, via Stefan Krueger in PR 24962.
Should also fix:
PR 8153
PR 13301
PR 15256
PR 25084
 1.8 13-Feb-2004  wiz Uppercase CPU, plural is CPUs.
 1.7 23-Jun-2003  agc Add NetBSD RCS Ids.
 1.6 20-Jan-2003  dsl Make 'times' be a proper posix special builtin.
(change agreed by christos and mailed to Michael Rendell)
 1.5 04-Jan-2003  wiz Spell output with two ts.
 1.4 04-Feb-2001  christos remove duplicate declarations, and fix nested externs.
 1.3 20-Oct-1999  hubertf branches: 1.3.6;
resolve conflicts
 1.2 12-Jan-1997  tls branches: 1.2.6;
RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.3 20-Oct-1999  hubertf Import pdksh V5.2.14.
Includes lots of bugfixes.
 1.1.1.2 09-Oct-1996  jtc import pdksh 5.2.11
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.2.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.3.6.1 26-Feb-2002  he Pull up revision 1.4 (requested by dogcow):
Synchronize with current development version:
o fix problem with ``set -x'' causing a core dump; fixes PR#11361
o fix problem with ``set -e'' causing early exit; fixes PR#11542
o fix various misspellings and nested extern declarations
 1.12.28.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.14.8.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.14.2.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.15.8.1 04-Nov-2016  pgoyette Sync with HEAD
 1.23.4.1 21-May-2018  pgoyette Sync with HEAD
 1.24.2.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.24.2.1 21-Apr-2020  martin Sync with HEAD
 1.11 14-Jun-2025  christos PR/59210: Collin Funk: Use more precise time test
 1.10 16-Sep-2021  christos branches: 1.10.4;
It is either teaching gcc about aaah or adding FALLTHROUGH.
 1.9 30-Jun-2017  kamil branches: 1.9.6;
ksh: Upgrade to C99 <stdbool.h>

This shell already used C99 functions.
 1.8 30-Jun-2017  kamil ksh: Drop support for OSes that don't map /dev/fd
 1.7 30-Jun-2017  kamil ksh: Drop support for systems without <sys/stat.h>

In future the order of includes will be normalized with KNF.

No functional change intended.
 1.6 26-Jun-2005  christos warns=3
- remove param names from function decls
- sprinkle const
- rename shadowed variables
- XXX: there is a lot of fishy __UNCONST going on.
 1.5 07-Jul-2004  mycroft Cleanup of ksh memory handling from OpenBSD, via Stefan Krueger in PR 24962.
Should also fix:
PR 8153
PR 13301
PR 15256
PR 25084
 1.4 23-Jun-2003  agc Add NetBSD RCS Ids.
 1.3 20-Oct-1999  hubertf resolve conflicts
 1.2 12-Jan-1997  tls branches: 1.2.6;
RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.2 20-Oct-1999  hubertf Import pdksh V5.2.14.
Includes lots of bugfixes.
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.2.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.9.6.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.9.6.1 21-Apr-2020  martin Sync with HEAD
 1.10.4.1 02-Aug-2025  perseant Sync with HEAD
 1.3 07-Jul-2004  mycroft branches: 1.3.92;
Cleanup of ksh memory handling from OpenBSD, via Stefan Krueger in PR 24962.
Should also fix:
PR 8153
PR 13301
PR 15256
PR 25084
 1.2 12-Jan-1997  tls RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.3.92.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.3.92.1 21-Apr-2020  martin Sync with HEAD
 1.16 30-Jun-2017  kamil branches: 1.16.6;
ksh: Drop support for systems with broken <sys/time.h> / <time.h>
 1.15 22-Jun-2017  kamil Reapply removal of code from 90ties for OS/2 Cygwin AIX HPUX SCOUnix

Added missing #endif terminator in emacs.c
 1.14 22-Jun-2017  kamil Temporarily revert previous.

emacs.* gets wrong code in generation
 1.13 22-Jun-2017  kamil Drop HP-UX support from ksh(1)

OK by <kre>
 1.12 09-May-2015  christos CID 1225088: check return of getrlimit
 1.11 09-May-2015  christos CID 1225077: check getrlimit return
 1.10 09-Jun-2012  christos support RLIMIT_NTHR
 1.9 14-Sep-2008  sjg branches: 1.9.2;
Avoid sign extention problems leading to:
$ ulimit -dH
3145728
$ ulimit -d 3145728
ksh: ulimit: bad limit: Invalid argument
$
 1.8 16-Oct-2006  christos branches: 1.8.18;
use c99 initializers
 1.7 07-Jul-2004  mycroft Cleanup of ksh memory handling from OpenBSD, via Stefan Krueger in PR 24962.
Should also fix:
PR 8153
PR 13301
PR 15256
PR 25084
 1.6 17-Apr-2004  christos understand rlimit sbsize
 1.5 23-Jun-2003  agc Add NetBSD RCS Ids.
 1.4 20-Oct-1999  hubertf resolve conflicts
 1.3 03-Feb-1998  mycroft branches: 1.3.4;
Deal with GCC warnings.
 1.2 12-Jan-1997  tls RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.2 20-Oct-1999  hubertf Import pdksh V5.2.14.
Includes lots of bugfixes.
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.3.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.8.18.1 24-Sep-2008  wrstuden Merge in changes between wrstuden-revivesa-base-2 and
wrstuden-revivesa-base-3.
 1.9.2.1 30-Oct-2012  yamt sync with head
 1.16.6.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.16.6.1 21-Apr-2020  martin Sync with HEAD
 1.7 30-Jun-2017  kamil branches: 1.7.6;
ksh: Drop support for systems without functional waitpid(2)
 1.6 30-Jun-2017  kamil ksh: Drop support for systems without POSIX signal routines
 1.5 30-Jun-2017  kamil ksh: Drop support for systems without implementation of EINTR

These systems weren't handled anyway.
 1.4 22-Jun-2017  kamil ksh: Drop fallback for BSD4.2 signal routines
 1.3 22-Jun-2017  kamil ksh: Drop support for systems without mmap(2)
 1.2 12-Jan-1997  tls RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.7.6.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.7.6.1 21-Apr-2020  martin Sync with HEAD
 1.53 30-Jun-2017  kamil branches: 1.53.6;
ksh: Drop support for OSes without POSIX sigaction(2)
 1.52 30-Jun-2017  kamil ksh: Drop support for OSes that don't map /dev/fd
 1.51 30-Jun-2017  kamil ksh: Drop support for systems without <sys/stat.h>

In future the order of includes will be normalized with KNF.

No functional change intended.
 1.50 30-Jun-2017  kamil ksh: Drop support for systems with broken <sys/time.h> / <time.h>
 1.49 30-Jun-2017  kamil ksh: Drop support for systems without POSIX lstat(2)
 1.48 30-Jun-2017  kamil ksh: Drop support for systems without POSIX <sys/wait.h>
 1.47 30-Jun-2017  kamil 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.
 1.46 30-Jun-2017  kamil ksh: Assume ANSI C prototypes
 1.45 30-Jun-2017  kamil ksh: Drop support for systems without functional waitpid(2)
 1.44 30-Jun-2017  kamil ksh: Drop support for systems without POSIX signal routines
 1.43 30-Jun-2017  kamil ksh: Drop support for systems without implementation of EINTR

These systems weren't handled anyway.
 1.42 30-Jun-2017  kamil ksh: Drop support for systems without handling shebang
 1.41 30-Jun-2017  kamil ksh: Drop support for OSes without POSIX types

Assume available: mode_t, off_t, pid_t, uid_t, rlim_t, sigset_t.
 1.40 30-Jun-2017  kamil ksh: Drop support for systems without <sys/wait.h>
 1.39 30-Jun-2017  kamil ksh: Drop support for systems without gid_t
 1.38 23-Jun-2017  kamil ksh: Drop support for systems without clock_t type
 1.37 23-Jun-2017  kamil ksh: Replace homegrown int_least32_t with the C99 version
 1.36 23-Jun-2017  kamil ksh: Drop support for systems that return void for closedir(2)
 1.35 23-Jun-2017  kamil ksh: Drop support for UNIX V7-style signal routines
 1.34 22-Jun-2017  kamil ksh: Drop fallback for BSD4.2 signal routines
 1.33 22-Jun-2017  kamil ksh: Remove fallback to BSD4.1 signal routines
 1.32 22-Jun-2017  kamil ksh: Drop support for systems without mmap(2)
 1.31 22-Jun-2017  kamil ksh: Drop support for systems without dup2(2)
 1.30 22-Jun-2017  kamil ksh: Drop support for ISC UNIX
 1.29 22-Jun-2017  kamil ksh: Drop support for systems without <limits.h>
 1.28 22-Jun-2017  kamil ksh: Drop support for systems without <fcntl.h>
 1.27 22-Jun-2017  kamil ksh: Drop support for systems without memmove(3) and stop using bcopy(3)
 1.26 22-Jun-2017  kamil ksh: Drop support for systems without memset(3)
 1.25 22-Jun-2017  kamil ksh: Drop usage of SVID header <memory.h>, it's legacy standard
 1.24 22-Jun-2017  kamil ksh: Drop support for systems without strcasecmp(3) and strncasecmp(3)
 1.23 22-Jun-2017  kamil ksh: Drop support for systems without strstr(3)
 1.22 22-Jun-2017  kamil ksh: Drop support for systems without <string.h>
 1.21 22-Jun-2017  kamil ksh: Drop support for systems without <stddef.h>

No functional change.
 1.20 22-Jun-2017  kamil ksh: Drop support for systems without <stdlib.h>

No functional change.
 1.19 22-Jun-2017  kamil ksh: Drop support for systems without <unistd.h>

This code wouldn't work for them anyway.

No functional change.
 1.18 22-Jun-2017  kamil ksh: Kill switch for CLOSEDIR_VOID (closedir(2) returns void)

No users in the code. No functional change.
 1.17 22-Jun-2017  kamil ksh: Assume that $CC handles const (ANSI C89)

No functional change.
 1.16 22-Jun-2017  kamil Drop support for $CC incapable to use void* / volatile* (pre ANSI C89)
 1.15 22-Jun-2017  kamil Drop Ultrix support from ksh(1), a DEC UNIX for VAX
 1.14 22-Jun-2017  kamil Reapply removal of code from 90ties for OS/2 Cygwin AIX HPUX SCOUnix

Added missing #endif terminator in emacs.c
 1.13 22-Jun-2017  kamil Temporarily revert previous.

emacs.* gets wrong code in generation
 1.12 22-Jun-2017  kamil Remove code for AIX, including hack for 3.2.5 (from 1997) - from ksh(1)

OK by <kre>
 1.11 22-Jun-2017  kamil Drop HP-UX support from ksh(1)

OK by <kre>
 1.10 22-Jun-2017  kamil Remove sco unix 3.2v4.1 support (from 1992) from ksh(1)

OK by <kre>
 1.9 22-Jun-2017  kamil Remove os2 support in ksh(1)

OK by <kre>
 1.8 19-Aug-2004  christos Handle RESCUEDIR
 1.7 07-Jul-2004  mycroft Cleanup of ksh memory handling from OpenBSD, via Stefan Krueger in PR 24962.
Should also fix:
PR 8153
PR 13301
PR 15256
PR 25084
 1.6 11-Nov-2002  jdolecek default to "$HOME/.kshrc" if ENV is not set
 1.5 04-Feb-2001  christos we do have dup2.
 1.4 12-Jan-1997  tls branches: 1.4.12;
RCS ID Police
 1.3 09-Oct-1996  jtc sync with 5.2.11 (now define POSIXLY_CORRECT)
 1.2 09-Oct-1996  jtc autoconf biffed a test --- our stat macros do work
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.3 09-Oct-1996  jtc import pdksh 5.2.11
 1.1.1.2 09-Oct-1996  jtc import pdksh 5.2.9
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.4.12.1 26-Feb-2002  he Pull up revision 1.5 (requested by dogcow):
Synchronize with current development version:
o fix problem with ``set -x'' causing a core dump; fixes PR#11361
o fix problem with ``set -e'' causing early exit; fixes PR#11542
o fix various misspellings and nested extern declarations
 1.53.6.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.53.6.1 21-Apr-2020  martin Sync with HEAD
 1.36 03-Oct-2024  rillig bin: fix lint warning "effectively discards 'const'"

For example: src/bin/ed/io.c(339): warning: call to 'strchr' effectively
discards 'const' from argument [346]

No binary change.
 1.35 03-Jun-2018  kamil branches: 1.35.2; 1.35.14;
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>
 1.34 01-Jul-2017  joerg branches: 1.34.4;
Kill enough K&R cruft to build with clang again.
 1.33 30-Jun-2017  kamil ksh: Eliminate dead function x_complete_word()
 1.32 30-Jun-2017  kamil ksh: Upgrade to C99 <stdbool.h>

This shell already used C99 functions.
 1.31 30-Jun-2017  kamil ksh: Drop support for systems without <sys/stat.h>

In future the order of includes will be normalized with KNF.

No functional change intended.
 1.30 22-Jun-2017  kamil Reapply removal of code from 90ties for OS/2 Cygwin AIX HPUX SCOUnix

Added missing #endif terminator in emacs.c
 1.29 22-Jun-2017  kamil Temporarily revert previous.

emacs.* gets wrong code in generation
 1.28 22-Jun-2017  kamil Remove sco unix 3.2v4.1 support (from 1992) from ksh(1)

OK by <kre>
 1.27 22-Jun-2017  kamil Remove os2 support in ksh(1)

OK by <kre>
 1.26 22-Jun-2017  kamil Remove ancient cygwin support in ksh(1)

OK by <kre>
 1.25 05-Jun-2010  sjg PR: 39604
Reviewed by:

add_glob:
Do not stop scanning if we see '$' as it does more harm than good.
For $HOME/tm we should return $HOME/tm*
 1.24 02-Apr-2010  plunky - int putbuf_func ARGS((const char *s, size_t len));
+ int (*putbuf_func) ARGS((const char *, size_t));

for pcc
- did not recognise pointer to function
- argument names shadowed other arguments
 1.23 24-Nov-2009  seanb Back out fix for PR 22846 as it has issues. See PR 22846 for details.
 1.22 25-Apr-2009  lukem Fix most of the -Wsign-compare issues.
 1.21 28-Jan-2007  cbiere branches: 1.21.20;
* Escape '?', '[', ']' like other meta characters.
* Partial fix for completion when '[' is in the way.
* Addresses PR bin/22846.
 1.20 14-May-2006  christos Don't free random memory; thanks gcc-4.
 1.19 27-Apr-2006  christos Avoid double free.
 1.18 24-Apr-2006  christos Coverity CID 2995: Fix memory leak.
 1.17 11-Sep-2005  christos Process window size changes after xread(). [change window size]
echo $COLUMS $LINES; will now print the new sizes immediately, instead
of after the second echo. From otto@OpenBSD
 1.16 26-Jun-2005  christos warns=3
- remove param names from function decls
- sprinkle const
- rename shadowed variables
- XXX: there is a lot of fishy __UNCONST going on.
 1.15 23-May-2005  rillig Fixed wrong use of the <ctype.h> functions by adding (unsigned char) casts.
 1.14 07-Jul-2004  mycroft Cleanup of ksh memory handling from OpenBSD, via Stefan Krueger in PR 24962.
Should also fix:
PR 8153
PR 13301
PR 15256
PR 25084
 1.13 11-Sep-2003  jmmv Add ':' to the list of characters to be escaped. Fixes completion problems
when the filename includes this character.
 1.12 26-Aug-2003  wiz From OpenBSD:
revision 1.18
date: 2003/08/22 18:17:10; author: fgsch; state: Exp; lines: +2 -2
in word location, fix forward scanning so it correctly account for any
escaped char and not only spaces.
for "foo (bar.a)" and "foo (bar a)", cd foo\ \(bar.<tab> will correctly
expand to foo\ \(bar.a\).
otto@ and pval@ ok.
 1.11 23-Jun-2003  agc Add NetBSD RCS Ids.
 1.10 07-May-2003  wiz Fix completion of files containing #. From Soren Jacobsen in PR 21486.
 1.9 06-May-2003  wiz Add ` (backquote) to quoted characters for completion. From fgsch@openbsd.
Okayed by christos.
 1.8 25-Sep-2002  provos change complete-list to first complete and then print a list,
also complete after '=' (dd), and ':' (ssh) and ` (backtick)
improvements from camield@openbsd.org. approved by perry and thorpej.
 1.7 25-Jan-2002  sjg Fix some aspects of globbing - in emacs mode at least.
echo ~<ESC><ESC> and $HOME<ESC><ESC> as well as ~/b<ESC><ESC> all
now expand correctly.
 1.6 02-Nov-1999  jdolecek branches: 1.6.6;
Implement (somewhat enhanced) idea stealed from bash:
when completing the filename (either in vi mode with vi-tabcomplete on,
or in emacs mode after double esc), escape any shell special characters
and chars from $IFS with backslash - very handy especially when
dealing with filenames containing spaces

The patch has been sent to maintainer, but I haven't got any reply yet even
after about a month :(
 1.5 20-Oct-1999  hubertf resolve conflicts
 1.4 04-Nov-1998  christos branches: 1.4.4;
char->unsigned char casts for is*()
 1.3 20-Jul-1997  christos Fix compiler warnings
Add WARNS=1
 1.2 12-Jan-1997  tls RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.3 20-Oct-1999  hubertf Import pdksh V5.2.14.
Includes lots of bugfixes.
 1.1.1.2 09-Oct-1996  jtc import pdksh 5.2.9
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.4.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.6.6.1 06-Feb-2002  he Pull up revision 1.7 (requested by sjg):
Fix some aspects of globbing, particularly in emacs mode.
 1.21.20.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.34.4.1 25-Jun-2018  pgoyette Sync with HEAD
 1.35.14.1 02-Aug-2025  perseant Sync with HEAD
 1.35.2.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.35.2.1 21-Apr-2020  martin Sync with HEAD
 1.5 01-Jul-2017  joerg branches: 1.5.6;
Kill enough K&R cruft to build with clang again.
 1.4 30-Jun-2017  kamil ksh: Upgrade to C99 <stdbool.h>

This shell already used C99 functions.
 1.3 02-Nov-1999  jdolecek Implement (somewhat enhanced) idea stealed from bash:
when completing the filename (either in vi mode with vi-tabcomplete on,
or in emacs mode after double esc), escape any shell special characters
and chars from $IFS with backslash - very handy especially when
dealing with filenames containing spaces

The patch has been sent to maintainer, but I haven't got any reply yet even
after about a month :(
 1.2 12-Jan-1997  tls branches: 1.2.6;
RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.2.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.5.6.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.5.6.1 21-Apr-2020  martin Sync with HEAD
 1.4 25-Oct-2008  apb branches: 1.4.62;
In shell scripts run during the build, add a SED variable, defaulting
to "sed". SED=${TOOL_SED:Q} should be passed in the environment to
override this.
 1.3 19-Oct-2008  apb In shell scripts invoked during a build, and in crunchgen, use ${AWK}
instead of plain "awk". The Makefiles that invoke these scripts
or programs will pass AWK=${HOST_AWK:Q}.
 1.2 12-Jan-1997  tls RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.4.62.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.4.62.1 21-Apr-2020  martin Sync with HEAD
 1.38 08-May-2018  kamil branches: 1.38.2;
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
 1.37 30-Jun-2017  kamil branches: 1.37.4;
ksh: Upgrade to C99 <stdbool.h>

This shell already used C99 functions.
 1.36 30-Jun-2017  kamil ksh: Drop support for systems without <sys/stat.h>

In future the order of includes will be normalized with KNF.

No functional change intended.
 1.35 22-Jun-2017  kamil Reapply removal of code from 90ties for OS/2 Cygwin AIX HPUX SCOUnix

Added missing #endif terminator in emacs.c
 1.34 22-Jun-2017  kamil Temporarily revert previous.

emacs.* gets wrong code in generation
 1.33 22-Jun-2017  kamil Remove os2 support in ksh(1)

OK by <kre>
 1.32 25-Apr-2009  lukem Fix most of the -Wsign-compare issues.
 1.31 13-May-2006  christos branches: 1.31.28;
Coverity CID 3365: Fix inverted logic!
 1.30 01-Apr-2006  christos Coverity CID 1829: Fix memory leak.
 1.29 26-Jun-2005  christos warns=3
- remove param names from function decls
- sprinkle const
- rename shadowed variables
- XXX: there is a lot of fishy __UNCONST going on.
 1.28 23-May-2005  rillig Fixed wrong use of the <ctype.h> functions by adding (unsigned char) casts.
 1.27 11-Feb-2005  simonb White space nit- don't put a space before/after increment/decrement
operators.
 1.26 28-Oct-2004  dsl Add (unsigned char) casts to tolower/toupper and iscntrl calls.
Fixes PR 27593
 1.25 07-Jul-2004  mycroft Truncate the prompt if too long. Fixes PR 24674, but not with the patch from
there.
 1.24 07-Jul-2004  mycroft Cleanup of ksh memory handling from OpenBSD, via Stefan Krueger in PR 24962.
Should also fix:
PR 8153
PR 13301
PR 15256
PR 25084
 1.23 26-Feb-2004  jdolecek rename variable 'history' to 'histlist' to avoid clash with libedit
history() when this program is crunched into /rescue tools

fixes PR bin/24556 by Kouichirou Hiratsuka
 1.22 28-Aug-2003  wiz In emacs mode, when expanding file names containing characters that should
get quoted with <ESC>*, _do_ quote them.
Patch from Federico Schwindt fgsch@openbsd -- thanks!
[rev 1.25 in OpenBSD version of this file]
 1.21 26-Aug-2003  wiz Push (into yank buffer) words only if force_push is TRUE;
set force_push to TRUE in x_del_bword and x_del_fword.
Fixes behaviour where <meta-y> would put the previously yanked word in the yank buffer another time.
 1.20 26-Aug-2003  wiz From OpenBSD:
revision 1.21
date: 2003/08/02 19:26:15; author: fgsch; state: Exp; lines: +4 -2
On ESC-y ESC-y (yank-pop), also check that there is something to
insert (some text has been killed before). from otto@, fix bug report 3384.

On yank-pop error (no yank before), reset the index to killstack so
another yank-pop does not mangle the prompt if nothing was yanked, and
to avoid replacing a text when it shouldn't
(yank <something> yank-pop yank-pop).
otto@ ok.
 1.19 26-Aug-2003  wiz Allow single letters to be yanked, not only words of two or more letters.
 1.18 26-Aug-2003  wiz Add 'return' statement (and whitespace) to sync with OpenBSD version
of the patch committed in v1.12.
 1.17 26-Aug-2003  wiz From OpenBSD:
revision 1.23
date: 2003/08/23 02:30:59; author: fgsch; state: Exp; lines: +2 -2
under emacs mode, fix the case when the globbed file and the longest
prefix lenghts are equal ("a .b" and "a ab" by instance).
found and tested by otto@.
 1.16 23-Jun-2003  agc Add NetBSD RCS Ids.
 1.15 10-Oct-2002  provos enable meta key in emacs mode for 7-bit locales. approved thorpej.
 1.14 27-Sep-2002  provos complete file names including a ~ correctly.
 1.13 25-Sep-2002  provos enable emacs mode, add tab key to do completion in emacs and vi mode.
from millert@openbsd.org. approved by perry and thorpej.
 1.12 25-Sep-2002  provos change complete-list to first complete and then print a list,
also complete after '=' (dd), and ':' (ssh) and ` (backtick)
improvements from camield@openbsd.org. approved by perry and thorpej.
 1.11 25-Jan-2002  sjg Fix some aspects of globbing - in emacs mode at least.
echo ~<ESC><ESC> and $HOME<ESC><ESC> as well as ~/b<ESC><ESC> all
now expand correctly.
 1.10 09-Nov-1999  jdolecek branches: 1.10.4;
remove debug printf accidentaly left in
 1.9 08-Nov-1999  jdolecek in emacs mode, escape the contents of completed word appropriately even
in CT_COMPLIST case ("complete and then list (if non-exact)" mode)

Noticed by Soren S. Jorvang <soren@wheel.dk>.
 1.8 02-Nov-1999  jdolecek Implement (somewhat enhanced) idea stealed from bash:
when completing the filename (either in vi mode with vi-tabcomplete on,
or in emacs mode after double esc), escape any shell special characters
and chars from $IFS with backslash - very handy especially when
dealing with filenames containing spaces

The patch has been sent to maintainer, but I haven't got any reply yet even
after about a month :(
 1.7 20-Oct-1999  hubertf make this build with -Werror
 1.6 20-Oct-1999  hubertf resolve conflicts
 1.5 04-Nov-1998  christos branches: 1.5.4;
char->unsigned char casts for is*()
 1.4 29-Mar-1998  mrg - change "register" variables into int's
- don't use char as an array index.
 1.3 20-Jul-1997  christos Fix compiler warnings
Add WARNS=1
 1.2 12-Jan-1997  tls RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.2 20-Oct-1999  hubertf Import pdksh V5.2.14.
Includes lots of bugfixes.
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.5.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.10.4.1 06-Feb-2002  he Pull up revision 1.11 (requested by sjg):
Fix some aspects of globbing, particularly in emacs mode.
 1.31.28.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.37.4.1 21-May-2018  pgoyette Sync with HEAD
 1.38.2.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.38.2.1 21-Apr-2020  martin Sync with HEAD
 1.27 03-Oct-2024  rillig bin: fix lint warning "effectively discards 'const'"

For example: src/bin/ed/io.c(339): warning: call to 'strchr' effectively
discards 'const' from argument [346]

No binary change.
 1.26 26-Sep-2019  mlelstv branches: 1.26.10;
Fix FALLTHROUGH comments.
 1.25 12-Jun-2018  kamil branches: 1.25.2;
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>
 1.24 03-Jun-2018  kamil 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>
 1.23 08-May-2018  kamil 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
 1.22 30-Jun-2017  kamil branches: 1.22.4;
ksh: Upgrade to C99 <stdbool.h>

This shell already used C99 functions.
 1.21 30-Jun-2017  kamil ksh: Drop support for systems without <sys/stat.h>

In future the order of includes will be normalized with KNF.

No functional change intended.
 1.20 30-Jun-2017  kamil ksh: Drop support for systems without handling shebang
 1.19 22-Jun-2017  kamil Reapply removal of code from 90ties for OS/2 Cygwin AIX HPUX SCOUnix

Added missing #endif terminator in emacs.c
 1.18 22-Jun-2017  kamil Temporarily revert previous.

emacs.* gets wrong code in generation
 1.17 22-Jun-2017  kamil Remove os2 support in ksh(1)

OK by <kre>
 1.16 04-Oct-2016  joerg Add explicit char cast to show that the value change is intended.
 1.15 18-Oct-2013  christos branches: 1.15.8;
Fixed unused warnings.
 1.14 21-Aug-2011  dholland branches: 1.14.2; 1.14.8;
Requires stdint.h.
 1.13 14-Aug-2011  christos kill gcc-4.5 hack.
 1.12 24-Nov-2009  seanb Back out fix for PR 22846 as it has issues. See PR 22846 for details.
 1.11 25-Apr-2009  lukem Fix most of the -Wsign-compare issues.
 1.10 28-Jan-2007  cbiere branches: 1.10.20;
Applied patch by Arto Huusko from PR bin/24753 to append a slash to "."
and ".." instead of skipping over them fixing completion for patterns
like ".*" and "..*".
 1.9 28-Jan-2007  cbiere * Escape '?', '[', ']' like other meta characters.
* Partial fix for completion when '[' is in the way.
* Addresses PR bin/22846.
 1.8 28-Jan-2007  cbiere Committed patch from PR bin/34755: Append a slash when expanding ~user
to user's home directory.
 1.7 13-May-2006  christos Coverity CID 3368: Fix memory leak.
 1.6 10-May-2006  mrg quell GCC 4.1 uninitialised variable warnings.

XXX: we should audit the tree for which old ones are no longer needed
after getting the older compilers out of the tree..
 1.5 07-Jul-2004  mycroft Cleanup of ksh memory handling from OpenBSD, via Stefan Krueger in PR 24962.
Should also fix:
PR 8153
PR 13301
PR 15256
PR 25084
 1.4 23-Jun-2003  agc Add NetBSD RCS Ids.
 1.3 20-Oct-1999  hubertf resolve conflicts
 1.2 12-Jan-1997  tls branches: 1.2.6;
RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.2 20-Oct-1999  hubertf Import pdksh V5.2.14.
Includes lots of bugfixes.
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.2.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.10.20.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.14.8.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.14.2.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.15.8.1 04-Nov-2016  pgoyette Sync with HEAD
 1.22.4.2 25-Jun-2018  pgoyette Sync with HEAD
 1.22.4.1 21-May-2018  pgoyette Sync with HEAD
 1.25.2.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.25.2.2 21-Apr-2020  martin Sync with HEAD
 1.25.2.1 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.26.10.1 02-Aug-2025  perseant Sync with HEAD
 1.28 03-Jun-2018  kamil branches: 1.28.2;
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>
 1.27 08-May-2018  kamil 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
 1.26 24-Jan-2018  kamil branches: 1.26.2;
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>
 1.25 24-Jan-2018  kamil 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>
 1.24 01-Jul-2017  joerg Kill enough K&R cruft to build with clang again.
 1.23 30-Jun-2017  kamil ksh: Upgrade to C99 <stdbool.h>

This shell already used C99 functions.
 1.22 30-Jun-2017  kamil ksh: Drop support for systems without <sys/stat.h>

In future the order of includes will be normalized with KNF.

No functional change intended.
 1.21 30-Jun-2017  kamil ksh: Drop support for systems without functional waitpid(2)
 1.20 30-Jun-2017  kamil ksh: Drop support for systems without handling shebang
 1.19 22-Jun-2017  kamil ksh: Drop support for ISC UNIX
 1.18 22-Jun-2017  kamil Reapply removal of code from 90ties for OS/2 Cygwin AIX HPUX SCOUnix

Added missing #endif terminator in emacs.c
 1.17 22-Jun-2017  kamil Temporarily revert previous.

emacs.* gets wrong code in generation
 1.16 22-Jun-2017  kamil Remove os2 support in ksh(1)

OK by <kre>
 1.15 25-Mar-2012  christos PR/6764: Charles M. Hannum: `trap 0' does not work in ksh subshells. When
subshells exit normally, use unwind(LEXIT) instead of unwind(LLEAVE) so that
traps get executed.
 1.14 16-Oct-2011  joerg branches: 1.14.2;
Don't use non-literal format strings.
 1.13 24-Apr-2006  christos Coverity CID 2994: Don't leak memory in the perm case.
 1.12 11-Sep-2005  christos The following should print \"; from OpenBSD
$ cat << EOF
\"
EOF
 1.11 26-Jun-2005  christos warns=3
- remove param names from function decls
- sprinkle const
- rename shadowed variables
- XXX: there is a lot of fishy __UNCONST going on.
 1.10 19-Apr-2005  rillig Fixed wrong use of the <ctype.h> functions by adding an explicit conversion
to unsigned char. Approved by christos.
 1.9 07-Jul-2004  mycroft Cleanup of ksh memory handling from OpenBSD, via Stefan Krueger in PR 24962.
Should also fix:
PR 8153
PR 13301
PR 15256
PR 25084
 1.8 23-Jun-2003  agc Add NetBSD RCS Ids.
 1.7 25-Sep-2002  provos change complete-list to first complete and then print a list,
also complete after '=' (dd), and ':' (ssh) and ` (backtick)
improvements from camield@openbsd.org. approved by perry and thorpej.
 1.6 29-Sep-2001  wiz Bring in pdksh-5.2.14-patches.2.
ChangeLog entry:
Mon Jan 29 10:12:31 NST 2001 Michael Rendell (michael@garfield.cs.mun.ca)
* c_exec.c(execute): added rv_prop flag to prevent if/while/for/...
in the presence of "set -e" from causing the shell to exit.
Example:
set -e
(false && echo hi)
[exits shell though it shouldn't]
ChangeLog and example provided by pdksh maintainer, Michael Rendell.
 1.5 20-Oct-1999  hubertf branches: 1.5.6;
resolve conflicts
 1.4 28-Jul-1998  mycroft branches: 1.4.4;
Delint.
 1.3 20-Jul-1997  christos Fix compiler warnings
Add WARNS=1
 1.2 12-Jan-1997  tls RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.4 20-Oct-1999  hubertf Import pdksh V5.2.14.
Includes lots of bugfixes.
 1.1.1.3 18-Dec-1996  jtc import pdksh 5.2.12
 1.1.1.2 09-Oct-1996  jtc import pdksh 5.2.9
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.4.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.5.6.1 26-Feb-2002  he Pull up revision 1.6 (requested by dogcow):
Synchronize with current development version:
o fix problem with ``set -x'' causing a core dump; fixes PR#11361
o fix problem with ``set -e'' causing early exit; fixes PR#11542
o fix various misspellings and nested extern declarations
 1.14.2.1 17-Apr-2012  yamt sync with head
 1.26.2.2 25-Jun-2018  pgoyette Sync with HEAD
 1.26.2.1 21-May-2018  pgoyette Sync with HEAD
 1.28.2.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.28.2.1 21-Apr-2020  martin Sync with HEAD
 1.7 08-May-2018  kamil branches: 1.7.2;
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
 1.6 30-Jun-2017  kamil branches: 1.6.4;
ksh: Eliminate dead code from expand.h
 1.5 18-Oct-2013  christos Fixed unused warnings.
 1.4 26-Jul-2001  wiz branches: 1.4.58; 1.4.64;
Various typos in comments (neccessary, sceme, choise, ...).
 1.3 20-Oct-1999  hubertf branches: 1.3.6;
resolve conflicts
 1.2 12-Jan-1997  tls branches: 1.2.6;
RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.2 20-Oct-1999  hubertf Import pdksh V5.2.14.
Includes lots of bugfixes.
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.2.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.3.6.1 26-Feb-2002  he Pull up revision 1.4 (requested by dogcow):
Synchronize with current development version:
o fix problem with ``set -x'' causing a core dump; fixes PR#11361
o fix problem with ``set -e'' causing early exit; fixes PR#11542
o fix various misspellings and nested extern declarations
 1.4.64.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.4.58.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.6.4.1 21-May-2018  pgoyette Sync with HEAD
 1.7.2.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.7.2.1 21-Apr-2020  martin Sync with HEAD
 1.13 03-Jul-2022  kre Fix core dump caused by
ksh -c '(i=10; echo $((++-+++i)))'
reported by Steffen Nurpmeso (not on a NetBSD list or PR).

Seems pointless to fix just one of the bugs in this thing, but this one was
easy enough (and stupid enough). (The "i=10" part is unimportant, as is the sub-shell).
 1.12 08-May-2018  kamil branches: 1.12.2;
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
 1.11 01-Jul-2017  joerg branches: 1.11.4;
Kill enough K&R cruft to build with clang again.
 1.10 30-Jun-2017  kamil ksh: Upgrade to C99 <stdbool.h>

This shell already used C99 functions.
 1.9 16-Oct-2011  joerg Don't use non-literal format strings.
 1.8 26-Jun-2005  christos warns=3
- remove param names from function decls
- sprinkle const
- rename shadowed variables
- XXX: there is a lot of fishy __UNCONST going on.
 1.7 19-Apr-2005  rillig Fixed wrong use of the <ctype.h> functions by adding an explicit conversion
to unsigned char. Approved by christos.
 1.6 07-Jul-2004  mycroft Cleanup of ksh memory handling from OpenBSD, via Stefan Krueger in PR 24962.
Should also fix:
PR 8153
PR 13301
PR 15256
PR 25084
 1.5 23-Jun-2003  agc Add NetBSD RCS Ids.
 1.4 20-Oct-1999  hubertf make this build with -Werror
 1.3 20-Oct-1999  hubertf resolve conflicts
 1.2 12-Jan-1997  tls branches: 1.2.6;
RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.2 20-Oct-1999  hubertf Import pdksh V5.2.14.
Includes lots of bugfixes.
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.2.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.11.4.1 21-May-2018  pgoyette Sync with HEAD
 1.12.2.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.12.2.1 21-Apr-2020  martin Sync with HEAD
 1.20 08-Sep-2024  rillig s/effect/affect/ in a few places
 1.19 08-May-2018  kamil branches: 1.19.2; 1.19.14;
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
 1.18 17-Jan-2018  maya branches: 1.18.2;
Use 0600 as the mode for histfile here too.

pointed out by John D. Baker in PR bin/52480
 1.17 30-Jun-2017  kamil ksh: Upgrade to C99 <stdbool.h>

This shell already used C99 functions.
 1.16 30-Jun-2017  kamil ksh: Drop support for systems without <sys/stat.h>

In future the order of includes will be normalized with KNF.

No functional change intended.
 1.15 22-Jun-2017  kamil Reapply removal of code from 90ties for OS/2 Cygwin AIX HPUX SCOUnix

Added missing #endif terminator in emacs.c
 1.14 22-Jun-2017  kamil Temporarily revert previous.

emacs.* gets wrong code in generation
 1.13 22-Jun-2017  kamil Remove os2 support in ksh(1)

OK by <kre>
 1.12 14-Jan-2017  maya branches: 1.12.6;
reorganize the code so we test if open fails at the open call.
this doesn't actually make a functional difference as ftruncate can
handle it, but it's a bit clearer and appeases static analyzers.

ok riastradh
 1.11 31-Aug-2011  plunky branches: 1.11.4; 1.11.10; 1.11.18; 1.11.22; 1.11.24; 1.11.26; 1.11.30;
NULL does not need a cast
 1.10 31-Oct-2009  dsl Avoid generating a corrupt history file if multiple ksh exit together.
Fixes PR/28912
 1.9 26-Jun-2005  christos warns=3
- remove param names from function decls
- sprinkle const
- rename shadowed variables
- XXX: there is a lot of fishy __UNCONST going on.
 1.8 16-Jul-2004  christos PR/26339: Pavel Cahyna: the 'fc' command can crash non-interactive ksh
Produce an error message instead.
 1.7 07-Jul-2004  mycroft Cleanup of ksh memory handling from OpenBSD, via Stefan Krueger in PR 24962.
Should also fix:
PR 8153
PR 13301
PR 15256
PR 25084
 1.6 26-Feb-2004  jdolecek rename variable 'history' to 'histlist' to avoid clash with libedit
history() when this program is crunched into /rescue tools

fixes PR bin/24556 by Kouichirou Hiratsuka
 1.5 23-Jun-2003  agc Add NetBSD RCS Ids.
 1.4 20-Oct-1999  hubertf resolve conflicts
 1.3 20-Jul-1997  christos branches: 1.3.6;
Fix compiler warnings
Add WARNS=1
 1.2 12-Jan-1997  tls RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.2 20-Oct-1999  hubertf Import pdksh V5.2.14.
Includes lots of bugfixes.
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.3.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.11.30.1 22-Jan-2018  snj Pull up following revision(s) (requested by maya in ticket #1554):
bin/ksh/history.c: revision 1.18 via patch
Use 0600 as the mode for histfile here too.
pointed out by John D. Baker in PR bin/52480
 1.11.26.1 20-Mar-2017  pgoyette Sync with HEAD
 1.11.24.1 22-Jan-2018  snj Pull up following revision(s) (requested by maya in ticket #1554):
bin/ksh/history.c: revision 1.18 via patch
Use 0600 as the mode for histfile here too.
pointed out by John D. Baker in PR bin/52480
 1.11.22.1 22-Jan-2018  snj Pull up following revision(s) (requested by maya in ticket #1554):
bin/ksh/history.c: revision 1.18 via patch
Use 0600 as the mode for histfile here too.
pointed out by John D. Baker in PR bin/52480
 1.11.18.1 13-Mar-2018  snj Pull up following revision(s) (requested by maya in ticket #1520):
bin/ksh/history.c: 1.18
Use 0600 as the mode for histfile here too.
pointed out by John D. Baker in PR bin/52480
 1.11.10.1 13-Mar-2018  snj Pull up following revision(s) (requested by maya in ticket #1520):
bin/ksh/history.c: 1.18
Use 0600 as the mode for histfile here too.
pointed out by John D. Baker in PR bin/52480
 1.11.4.1 13-Mar-2018  snj Pull up following revision(s) (requested by maya in ticket #1520):
bin/ksh/history.c: 1.18
Use 0600 as the mode for histfile here too.
pointed out by John D. Baker in PR bin/52480
 1.12.6.1 22-Jan-2018  martin Pull up following revision(s) (requested by maya in ticket #505):
bin/ksh/history.c: revision 1.18
Use 0600 as the mode for histfile here too.
pointed out by John D. Baker in PR bin/52480
 1.18.2.1 21-May-2018  pgoyette Sync with HEAD
 1.19.14.1 02-Aug-2025  perseant Sync with HEAD
 1.19.2.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.19.2.1 21-Apr-2020  martin Sync with HEAD
 1.18 08-May-2018  kamil branches: 1.18.2;
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
 1.17 30-Jun-2017  kamil branches: 1.17.4;
ksh: Upgrade to C99 <stdbool.h>

This shell already used C99 functions.
 1.16 30-Jun-2017  kamil ksh: Drop support for systems without <sys/stat.h>

In future the order of includes will be normalized with KNF.

No functional change intended.
 1.15 30-Jun-2017  kamil ksh: Assume ANSI C prototypes
 1.14 23-Jun-2017  kamil ksh: Use ANSI C varargs, drop support for older version <varargs.h>
 1.13 22-Jun-2017  kamil Drop Ultrix support from ksh(1), a DEC UNIX for VAX
 1.12 22-Jun-2017  kamil Reapply removal of code from 90ties for OS/2 Cygwin AIX HPUX SCOUnix

Added missing #endif terminator in emacs.c
 1.11 22-Jun-2017  kamil Temporarily revert previous.

emacs.* gets wrong code in generation
 1.10 22-Jun-2017  kamil Remove os2 support in ksh(1)

OK by <kre>
 1.9 26-Jun-2005  christos warns=3
- remove param names from function decls
- sprinkle const
- rename shadowed variables
- XXX: there is a lot of fishy __UNCONST going on.
 1.8 07-Jul-2004  mycroft Cleanup of ksh memory handling from OpenBSD, via Stefan Krueger in PR 24962.
Should also fix:
PR 8153
PR 13301
PR 15256
PR 25084
 1.7 23-Jun-2003  agc Add NetBSD RCS Ids.
 1.6 16-Sep-2001  wiz Spell 'occurred' with two 'r's.
 1.5 20-Oct-1999  hubertf branches: 1.5.6;
resolve conflicts
 1.4 04-Nov-1998  christos branches: 1.4.4;
char->unsigned char casts for is*()
 1.3 19-Aug-1998  thorpej Add some braces to make egcs happy.
 1.2 12-Jan-1997  tls RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.2 20-Oct-1999  hubertf Import pdksh V5.2.14.
Includes lots of bugfixes.
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.4.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.5.6.1 26-Feb-2002  he Pull up revision 1.6 (requested by dogcow):
Synchronize with current development version:
o fix problem with ``set -x'' causing a core dump; fixes PR#11361
o fix problem with ``set -e'' causing early exit; fixes PR#11542
o fix various misspellings and nested extern declarations
 1.17.4.1 21-May-2018  pgoyette Sync with HEAD
 1.18.2.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.18.2.1 21-Apr-2020  martin Sync with HEAD
 1.19 30-Jun-2017  kamil branches: 1.19.6;
ksh: Upgrade to C99 <stdbool.h>

This shell already used C99 functions.
 1.18 30-Jun-2017  kamil ksh: Drop support for systems without <sys/stat.h>

In future the order of includes will be normalized with KNF.

No functional change intended.
 1.17 30-Jun-2017  kamil 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.
 1.16 30-Jun-2017  kamil ksh: Drop support for systems without functional waitpid(2)
 1.15 30-Jun-2017  kamil ksh: Drop ksh_wait.h that reinvents <sys/wait.h> (POSIX header)

Switch jobs.c to <sys/wait.h>.

No functional change intended.
 1.14 23-Jun-2017  kamil ksh: Replace homegrown int_least32_t with the C99 version
 1.13 22-Jun-2017  kamil Reapply removal of code from 90ties for OS/2 Cygwin AIX HPUX SCOUnix

Added missing #endif terminator in emacs.c
 1.12 22-Jun-2017  kamil Temporarily revert previous.

emacs.* gets wrong code in generation
 1.11 22-Jun-2017  kamil Remove os2 support in ksh(1)

OK by <kre>
 1.10 16-Oct-2011  joerg Don't use non-literal format strings.
 1.9 25-Feb-2006  wiz Fix typos, reported by Alexey Dobriyan ("Gathered from Linux"),
forwarded by jmc@openbsd.
 1.8 07-Jul-2004  mycroft Cleanup of ksh memory handling from OpenBSD, via Stefan Krueger in PR 24962.
Should also fix:
PR 8153
PR 13301
PR 15256
PR 25084
 1.7 23-Jun-2003  agc Add NetBSD RCS Ids.
 1.6 02-Nov-2000  christos PR/11361:Matthias Buelow: Apply patch1 from pdksh site.
- set -x dumps core (shf.c);
- output of "jobs" command is filled with ^A characters (jobs.c);
- "typeset -r foo=bar" fails saying foo is readonly (var.c).
 1.5 20-Oct-1999  hubertf branches: 1.5.6;
resolve conflicts
 1.4 19-Aug-1998  thorpej branches: 1.4.4;
Add some braces to make egcs happy.
 1.3 07-Apr-1998  fair change some /dev/tty to paths.h _PATH_TTY
 1.2 12-Jan-1997  tls RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.4 20-Oct-1999  hubertf Import pdksh V5.2.14.
Includes lots of bugfixes.
 1.1.1.3 18-Dec-1996  jtc import pdksh 5.2.12
 1.1.1.2 09-Oct-1996  jtc import pdksh 5.2.9
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.4.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.5.6.1 26-Feb-2002  he Pull up revision 1.6 (requested by dogcow):
Synchronize with current development version:
o fix problem with ``set -x'' causing a core dump; fixes PR#11361
o fix problem with ``set -e'' causing early exit; fixes PR#11542
o fix various misspellings and nested extern declarations
 1.19.6.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.19.6.1 21-Apr-2020  martin Sync with HEAD
 1.3 20-Oct-1999  hubertf this is now generated automatically
 1.2 12-Jan-1997  tls branches: 1.2.6;
RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.3 18-Dec-1996  jtc import pdksh 5.2.12
 1.1.1.2 09-Oct-1996  jtc import pdksh 5.2.9
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.2.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.30 08-Sep-2024  rillig s/effect/affect/ in a few places
 1.29 21-Aug-2022  uwe branches: 1.29.4;
ksh(1): fix pasto in the description of the -l option.
 1.28 07-Aug-2022  andvar fix various typos in comments, documentation and messages.
mainly s/paramater/parameter/ and s/reduntant/redundant/.
 1.27 30-Oct-2021  andvar "no affect" -> "no effect" in man pages and comments.
 1.26 26-Aug-2018  sevan Add -l to SYNOPSIS
 1.25 30-Dec-2015  christos branches: 1.25.8; 1.25.14; 1.25.16;
We don't have RLIMIT_SWAP
 1.24 12-Apr-2015  jmcneill document ulimit -r
 1.23 18-Oct-2011  reed Fix typo.
From Snader_LB on IRC.
 1.22 24-Oct-2009  reed Show that -l is option for bind builtin.
(The -l is described later.)
 1.21 27-Oct-2008  apb In foo="`echo \"hi\"`", the backslashes should be removed by the outer
double quotes, not seen by the inner backquotes. Previously, ksh(1)
handled this correctly in non-posix mode but incorrectly in posix mode.

The previous comments in src/bin/ksh/lex.c quote parts of some version
of the POSIX specification. The version of POSIX being quoted is not
identified, but the wording is very similar to that in SUSv3
<http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html>.

It seems clear that the words "having escape characters removed"
mean, in that context, that the backslash before the double quote in
"...`...\"...`..." is removed as part of the handling of the outer
double quotes, so that the backslash is not present when the inner
backquotes are handled. The new code in this commit follows this
interpretation. The previous code followed a different interpretation
which is now believed to be incorrect.

Discussed in tech-userlevel. Closes PR 26493.
 1.20 30-Jul-2007  mjf branches: 1.20.14;
Fix some grammatical errors in man pages.
Patch supplied by Joerg Niendorf in PR misc/36707, thanks.
 1.19 24-Oct-2006  cbiere Applied patch from PR bin/34790 so that ~/.kshrc is mentioned.
 1.18 07-Jul-2004  mycroft Cleanup of ksh memory handling from OpenBSD, via Stefan Krueger in PR 24962.
Should also fix:
PR 8153
PR 13301
PR 15256
PR 25084
 1.17 17-Apr-2004  christos understand rlimit sbsize
 1.16 13-Feb-2004  wiz Uppercase CPU, plural is CPUs.
 1.15 01-May-2003  wiz Some grammar and punctuation fixes from jmc@openbsd.
 1.14 29-Mar-2003  perry reimplementation -> re-implementation
 1.13 22-Mar-2003  kristerw Fix some typos noted by Soren Jacobsen in PR 20793.
 1.12 05-Dec-2002  wiz Fix typo.
 1.11 12-Oct-2002  wiz Miscellaneous fixes by Martin Weber (ephaeton at that gmx net thingy).
 1.10 02-Oct-2002  wiz parameter, not paramter. By Adrian Mrva.
 1.9 26-Sep-2002  wiz Begin new sentences on new lines. From Robert Elz (kre at munnari oz au).
 1.8 25-Sep-2002  provos enable emacs mode, add tab key to do completion in emacs and vi mode.
from millert@openbsd.org. approved by perry and thorpej.
 1.7 20-Sep-2002  jschauma comment out the (no longer) builtin newgrp
 1.6 29-Jan-2002  wiz Fix bin/5205 (weird interaction between tbl and man macros).
 1.5 11-Nov-2001  jwise Fix a cross reference in man page. This should probably be mandoc-ified
if we are not going to use reachover makefiles for ksh (and given that
pdksh does not update very frequently (at all? any more? at the moment?),
it is probably reasonable not to).
 1.4 20-Aug-2001  wiz precede, not preceed.
 1.3 11-Jun-2001  wiz Fix various misspellings of compatible/compatibility.
 1.2 09-Apr-2001  wiz environ(7), not (5); add RCS Id; whitespace fixes.
 1.1 20-Oct-1999  hubertf branches: 1.1.1;
Initial revision
 1.1.1.1 20-Oct-1999  hubertf branches: 1.1.1.1.4; 1.1.1.1.6;
Import pdksh V5.2.14.
Includes lots of bugfixes.
 1.1.1.1.6.2 26-Feb-2002  he Pull up revisions 1.3-1.6 (requested by dogcow):
Synchronize with current development version:
o fix problem with ``set -x'' causing a core dump; fixes PR#11361
o fix problem with ``set -e'' causing early exit; fixes PR#11542
o fix various misspellings and nested extern declarations
 1.1.1.1.6.1 26-Apr-2001  he Pull up revision 1.2 (requested by wiz):
Correct environ(7) reference, add RCS Id, whitespace fixes.
 1.1.1.1.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.20.14.2 30-Jul-2007  mjf Fix some grammatical errors in man pages.
Patch supplied by Joerg Niendorf in PR misc/36707, thanks.
 1.20.14.1 30-Jul-2007  mjf file ksh.Man was added on branch matt-mips64 on 2007-07-30 18:01:43 +0000
 1.25.16.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.25.16.2 21-Apr-2020  martin Sync with HEAD
 1.25.16.1 10-Jun-2019  christos Sync with HEAD
 1.25.14.1 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.25.8.1 28-Aug-2018  martin Pull up following revision(s) (requested by sevan in ticket #992):

external/bsd/top/dist/top.1.in: revision 1.11
sbin/gpt/main.c: revision 1.12
sbin/amrctl/amrctl.c: revision 1.11
bin/df/df.c: revision 1.93
sbin/fsck_ext2fs/fsck_ext2fs.8: revision 1.21
sbin/fsck_ext2fs/main.c: revision 1.38
bin/ksh/ksh.Man: revision 1.26
bin/ln/ln.c: revision 1.40
bin/df/df.1: revision 1.48
bin/df/df.1: revision 1.49

Document the WCPU field.

Match SYNOPSIS with usage()

-G cannot be specified alongside -i or -P.
Heads up by <leot>

Add -l to SYNOPSIS

Update usage to include -w

Match sequence as per SYNOPSIS in manual

Remove reference to -c flag which was never implemented.

Remove references to -c flag which was never included.

Add the -T flag to usage()
 1.29.4.1 02-Aug-2025  perseant Sync with HEAD
 1.2 12-Jan-1997  tls branches: 1.2.112;
RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.2.112.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.2.112.1 21-Apr-2020  martin Sync with HEAD
 1.2 12-Jan-1997  tls branches: 1.2.112;
RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.2.112.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.2.112.1 21-Apr-2020  martin Sync with HEAD
 1.4 30-Jun-2017  kamil ksh: Drop support for systems without <sys/stat.h>

In future the order of includes will be normalized with KNF.

No functional change intended.
 1.3 30-Jun-2017  kamil ksh: Drop support for systems without POSIX lstat(2)
 1.2 12-Jan-1997  tls RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.2 09-Oct-1996  jtc import pdksh 5.2.9
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.3 30-Jun-2017  kamil ksh: Drop support for systems with broken <sys/time.h> / <time.h>
 1.2 12-Jan-1997  tls RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.2 09-Oct-1996  jtc import pdksh 5.2.9
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.3 30-Jun-2017  kamil 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.
 1.2 12-Jan-1997  tls RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.2 09-Oct-1996  jtc import pdksh 5.2.9
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.3 30-Jun-2017  kamil ksh: Drop ksh_wait.h that reinvents <sys/wait.h> (POSIX header)

Switch jobs.c to <sys/wait.h>.

No functional change intended.
 1.2 12-Jan-1997  tls RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.24 26-Sep-2019  mlelstv Fix FALLTHROUGH comments.
 1.23 08-May-2018  kamil branches: 1.23.2;
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
 1.22 24-Jan-2018  kamil branches: 1.22.2;
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>
 1.21 30-Jun-2017  kamil ksh: Upgrade to C99 <stdbool.h>

This shell already used C99 functions.
 1.20 30-Jun-2017  kamil ksh: Assume ANSI C prototypes
 1.19 23-Jun-2017  kamil ksh: Use ANSI C varargs, drop support for older version <varargs.h>
 1.18 22-Jun-2017  kamil Reapply removal of code from 90ties for OS/2 Cygwin AIX HPUX SCOUnix

Added missing #endif terminator in emacs.c
 1.17 22-Jun-2017  kamil Temporarily revert previous.

emacs.* gets wrong code in generation
 1.16 22-Jun-2017  kamil Remove os2 support in ksh(1)

OK by <kre>
 1.15 16-Oct-2011  joerg Don't use non-literal format strings.
 1.14 23-Jan-2011  hauke The previous commit removed a { }�block, and unintendedly introduced a
C99ism (inlined variable declaration), which hurts when pdksh is used
for bootstrapping pkgsrc. Move the two declarations to the beginning
of the block.
 1.13 27-Oct-2008  apb branches: 1.13.14;
In foo="`echo \"hi\"`", the backslashes should be removed by the outer
double quotes, not seen by the inner backquotes. Previously, ksh(1)
handled this correctly in non-posix mode but incorrectly in posix mode.

The previous comments in src/bin/ksh/lex.c quote parts of some version
of the POSIX specification. The version of POSIX being quoted is not
identified, but the wording is very similar to that in SUSv3
<http://www.opengroup.org/onlinepubs/009695399/utilities/xcu_chap02.html>.

It seems clear that the words "having escape characters removed"
mean, in that context, that the backslash before the double quote in
"...`...\"...`..." is removed as part of the handling of the outer
double quotes, so that the backslash is not present when the inner
backquotes are handled. The new code in this commit follows this
interpretation. The previous code followed a different interpretation
which is now believed to be incorrect.

Discussed in tech-userlevel. Closes PR 26493.
 1.12 11-Sep-2005  christos The following should print \"; from OpenBSD
$ cat << EOF
\"
EOF
 1.11 19-Apr-2005  rillig Fixed wrong use of the <ctype.h> functions by adding an explicit conversion
to unsigned char. Approved by christos.
 1.10 07-Jul-2004  mycroft Cleanup of ksh memory handling from OpenBSD, via Stefan Krueger in PR 24962.
Should also fix:
PR 8153
PR 13301
PR 15256
PR 25084
 1.9 23-Jun-2003  agc Add NetBSD RCS Ids.
 1.8 20-Jan-2003  simonb The Double-Semi-Colon Police.
 1.7 20-Oct-1999  hubertf make this build with -Werror
 1.6 20-Oct-1999  hubertf resolve conflicts
 1.5 04-Nov-1998  christos branches: 1.5.4;
char->unsigned char casts for is*()
 1.4 19-Aug-1998  thorpej Add some braces to make egcs happy.
 1.3 20-Jul-1997  christos Fix compiler warnings
Add WARNS=1
 1.2 12-Jan-1997  tls RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.5 20-Oct-1999  hubertf Import pdksh V5.2.14.
Includes lots of bugfixes.
 1.1.1.4 18-Dec-1996  jtc import pdksh 5.2.12
 1.1.1.3 09-Oct-1996  jtc import pdksh 5.2.11
 1.1.1.2 09-Oct-1996  jtc import pdksh 5.2.9
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.5.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.13.14.1 08-Feb-2011  bouyer Sync with HEAD
 1.22.2.1 21-May-2018  pgoyette Sync with HEAD
 1.23.2.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.23.2.2 21-Apr-2020  martin Sync with HEAD
 1.23.2.1 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.7 11-Sep-2005  christos branches: 1.7.86;
The following should print \"; from OpenBSD
$ cat << EOF
\"
EOF
 1.6 07-Jul-2004  mycroft Cleanup of ksh memory handling from OpenBSD, via Stefan Krueger in PR 24962.
Should also fix:
PR 8153
PR 13301
PR 15256
PR 25084
 1.5 26-Feb-2004  jdolecek rename variable 'history' to 'histlist' to avoid clash with libedit
history() when this program is crunched into /rescue tools

fixes PR bin/24556 by Kouichirou Hiratsuka
 1.4 16-Sep-2001  wiz Spell 'occurred' with two 'r's.
 1.3 20-Oct-1999  hubertf branches: 1.3.6;
resolve conflicts
 1.2 12-Jan-1997  tls branches: 1.2.6;
RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.4 20-Oct-1999  hubertf Import pdksh V5.2.14.
Includes lots of bugfixes.
 1.1.1.3 09-Oct-1996  jtc import pdksh 5.2.11
 1.1.1.2 09-Oct-1996  jtc import pdksh 5.2.9
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.2.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.3.6.1 26-Feb-2002  he Pull up revision 1.4 (requested by dogcow):
Synchronize with current development version:
o fix problem with ``set -x'' causing a core dump; fixes PR#11361
o fix problem with ``set -e'' causing early exit; fixes PR#11542
o fix various misspellings and nested extern declarations
 1.7.86.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.7.86.1 21-Apr-2020  martin Sync with HEAD
 1.9 08-May-2018  kamil branches: 1.9.2;
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
 1.8 30-Jun-2017  kamil branches: 1.8.4;
ksh: Upgrade to C99 <stdbool.h>

This shell already used C99 functions.
 1.7 30-Jun-2017  kamil ksh: Drop support for systems without <sys/stat.h>

In future the order of includes will be normalized with KNF.

No functional change intended.
 1.6 30-Jun-2017  kamil ksh: Drop support for systems with broken <sys/time.h> / <time.h>
 1.5 15-Jan-2006  jschauma Start 'You have mail in' with a capital Y.
 1.4 23-Jun-2003  agc Add NetBSD RCS Ids.
 1.3 20-Oct-1999  hubertf resolve conflicts
 1.2 12-Jan-1997  tls branches: 1.2.6;
RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.2 20-Oct-1999  hubertf Import pdksh V5.2.14.
Includes lots of bugfixes.
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.2.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.8.4.1 21-May-2018  pgoyette Sync with HEAD
 1.9.2.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.9.2.1 21-Apr-2020  martin Sync with HEAD
 1.23 08-May-2018  kamil branches: 1.23.2;
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
 1.22 30-Jun-2017  kamil branches: 1.22.4;
ksh: Upgrade to C99 <stdbool.h>

This shell already used C99 functions.
 1.21 30-Jun-2017  kamil ksh: Eliminate dead MEM_DEBUG sections, there is missing code for it

MEM_DEBUG used to contain malloc(3) debugging facilities.
 1.20 30-Jun-2017  kamil ksh: Drop support for systems without <sys/stat.h>

In future the order of includes will be normalized with KNF.

No functional change intended.
 1.19 30-Jun-2017  kamil ksh: Drop support for systems with broken <sys/time.h> / <time.h>
 1.18 22-Jun-2017  kamil Reapply removal of code from 90ties for OS/2 Cygwin AIX HPUX SCOUnix

Added missing #endif terminator in emacs.c
 1.17 22-Jun-2017  kamil Temporarily revert previous.

emacs.* gets wrong code in generation
 1.16 22-Jun-2017  kamil Remove os2 support in ksh(1)

OK by <kre>
 1.15 16-Oct-2011  joerg Don't use non-literal format strings.
 1.14 24-Jun-2007  christos PR/36546: Aleksey Cheusov: problems with 8-bit input in /bin/ksh, needs to
call setlocale()
 1.13 13-May-2006  christos branches: 1.13.4; 1.13.6;
Coverity CID 3369: Fix memory leak.
 1.12 26-Jun-2005  christos warns=3
- remove param names from function decls
- sprinkle const
- rename shadowed variables
- XXX: there is a lot of fishy __UNCONST going on.
 1.11 07-Jul-2004  mycroft Cleanup of ksh memory handling from OpenBSD, via Stefan Krueger in PR 24962.
Should also fix:
PR 8153
PR 13301
PR 15256
PR 25084
 1.10 23-Jun-2003  agc Add NetBSD RCS Ids.
 1.9 25-Sep-2002  provos enable emacs mode, add tab key to do completion in emacs and vi mode.
from millert@openbsd.org. approved by perry and thorpej.
 1.8 20-Sep-2002  jschauma We don't have 'newgrp' -> ifdef the builtin alias. This prevents
the shell from unexpectedly dying if a user enters the (nonexistent)
'newgrp' command. OK'd by perry.
As seen in OpenBSD.
 1.7 11-Jul-2002  wiz 'environment', not 'enviroment'
 1.6 20-Oct-1999  hubertf branches: 1.6.6; 1.6.8;
resolve conflicts
 1.5 19-Aug-1998  thorpej branches: 1.5.4;
Add some braces to make egcs happy.
 1.4 28-Jul-1998  mycroft Delint.
 1.3 20-Jul-1997  christos Fix compiler warnings
Add WARNS=1
 1.2 12-Jan-1997  tls RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.4 20-Oct-1999  hubertf Import pdksh V5.2.14.
Includes lots of bugfixes.
 1.1.1.3 18-Dec-1996  jtc import pdksh 5.2.12
 1.1.1.2 09-Oct-1996  jtc import pdksh 5.2.9
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.5.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.6.8.1 14-Oct-2002  lukem Pull up revision 1.8 (requested by jschauma in ticket #910):
We don't have 'newgrp' -> ifdef the builtin alias. This prevents
the shell from unexpectedly dying if a user enters the (nonexistent)
'newgrp' command. OK'd by perry.
As seen in OpenBSD.
 1.6.6.1 18-Oct-2002  itojun bin/ksh/main.c 1.8

Prevent ksh from dying if a user enters the (nonexistent) 'newgrp' command.
 1.13.6.1 03-Sep-2007  wrstuden Sync w/ NetBSD-4-RC_1
 1.13.4.1 01-Jul-2007  bouyer Pull up following revision(s) (requested by christos in ticket #749):
bin/ksh/main.c: revision 1.14
PR/36546: Aleksey Cheusov: problems with 8-bit input in /bin/ksh, needs to
call setlocale()
 1.22.4.1 21-May-2018  pgoyette Sync with HEAD
 1.23.2.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.23.2.1 21-Apr-2020  martin Sync with HEAD
 1.26 03-Oct-2024  rillig bin: fix lint warning "effectively discards 'const'"

For example: src/bin/ed/io.c(339): warning: call to 'strchr' effectively
discards 'const' from argument [346]

No binary change.
 1.25 29-Oct-2019  joerg branches: 1.25.10;
Avoid arithmetics on strings.
 1.24 08-May-2018  kamil branches: 1.24.2;
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
 1.23 30-Jun-2017  kamil branches: 1.23.4;
ksh: Upgrade to C99 <stdbool.h>

This shell already used C99 functions.
 1.22 22-Jun-2017  kamil ksh: Drop support for systems without <limits.h>
 1.21 22-Jun-2017  kamil Reapply removal of code from 90ties for OS/2 Cygwin AIX HPUX SCOUnix

Added missing #endif terminator in emacs.c
 1.20 22-Jun-2017  kamil Temporarily revert previous.

emacs.* gets wrong code in generation
 1.19 22-Jun-2017  kamil Drop HP-UX support from ksh(1)

OK by <kre>
 1.18 22-Jun-2017  kamil Remove os2 support in ksh(1)

OK by <kre>
 1.17 03-May-2017  christos PR/52210: David H. Gutteridge: revert var pattern handling.
 1.16 30-Apr-2017  christos Use backtracking for regular patterns, but not ksh-specific ones [*?!+@](...)
which still use recursion.
 1.15 16-Oct-2011  joerg branches: 1.15.34;
Don't use non-literal format strings.
 1.14 22-Oct-2009  seanb Wrong buffer len being passed to strlcpy(). Innocuous
here but...
 1.13 25-Apr-2009  lukem Fix most of the -Wsign-compare issues.
 1.12 26-Jun-2005  christos branches: 1.12.28;
warns=3
- remove param names from function decls
- sprinkle const
- rename shadowed variables
- XXX: there is a lot of fishy __UNCONST going on.
 1.11 19-Apr-2005  rillig Fixed wrong use of the <ctype.h> functions by adding an explicit conversion
to unsigned char. Approved by christos.
 1.10 11-Feb-2005  simonb White space nit- don't put a space before/after increment/decrement
operators.
 1.9 07-Jul-2004  mycroft Cleanup of ksh memory handling from OpenBSD, via Stefan Krueger in PR 24962.
Should also fix:
PR 8153
PR 13301
PR 15256
PR 25084
 1.8 23-Jun-2003  agc Add NetBSD RCS Ids.
 1.7 20-Jan-2003  simonb The Double-Semi-Colon Police.
 1.6 25-Sep-2002  provos change complete-list to first complete and then print a list,
also complete after '=' (dd), and ':' (ssh) and ` (backtick)
improvements from camield@openbsd.org. approved by perry and thorpej.
 1.5 04-Feb-2001  christos remove duplicate declarations, and fix nested externs.
 1.4 20-Oct-1999  hubertf branches: 1.4.6;
resolve conflicts
 1.3 09-Oct-1998  erh branches: 1.3.4;
getwd() -> getcwd(,MAXPATHLEN).
 1.2 12-Jan-1997  tls RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.2 20-Oct-1999  hubertf Import pdksh V5.2.14.
Includes lots of bugfixes.
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.3.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.4.6.1 26-Feb-2002  he Pull up revision 1.5 (requested by dogcow):
Synchronize with current development version:
o fix problem with ``set -x'' causing a core dump; fixes PR#11361
o fix problem with ``set -e'' causing early exit; fixes PR#11542
o fix various misspellings and nested extern declarations
 1.12.28.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.15.34.2 11-May-2017  pgoyette Sync with HEAD
 1.15.34.1 02-May-2017  pgoyette Sync with HEAD - tag prg-localcount2-base1
 1.23.4.1 21-May-2018  pgoyette Sync with HEAD
 1.24.2.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.24.2.2 21-Apr-2020  martin Sync with HEAD
 1.24.2.1 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.25.10.1 02-Aug-2025  perseant Sync with HEAD
 1.2 10-Oct-1996  jtc Removed missing.c beacuse NetBSD is not missing any of the functions
that it provides.
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.3 19-Oct-2008  apb branches: 1.3.62;
In shell scripts invoked during a build, and in crunchgen, use ${AWK}
instead of plain "awk". The Makefiles that invoke these scripts
or programs will pass AWK=${HOST_AWK:Q}.
 1.2 05-Jan-2004  jmmv Homogenize usage messages: make the 'usage' word all lowercase, as this seems
to be the most common practice in our tree.
 1.1 20-Oct-1999  hubertf branches: 1.1.1;
Initial revision
 1.1.1.1 20-Oct-1999  hubertf branches: 1.1.1.1.4;
Import pdksh V5.2.14.
Includes lots of bugfixes.
 1.1.1.1.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.3.62.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.3.62.1 21-Apr-2020  martin Sync with HEAD
 1.2 10-Oct-1996  jtc options.h is no longer used
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.14 31-Oct-2022  andvar fix various typos in comments and one output message.
 1.13 30-Jun-2017  kamil branches: 1.13.6;
ksh: Drop support for systems without <sys/stat.h>

In future the order of includes will be normalized with KNF.

No functional change intended.
 1.12 22-Jun-2017  kamil Reapply removal of code from 90ties for OS/2 Cygwin AIX HPUX SCOUnix

Added missing #endif terminator in emacs.c
 1.11 22-Jun-2017  kamil Temporarily revert previous.

emacs.* gets wrong code in generation
 1.10 22-Jun-2017  kamil Remove os2 support in ksh(1)

OK by <kre>
 1.9 22-Jun-2017  kamil Remove ancient cygwin support in ksh(1)

OK by <kre>
 1.8 25-Apr-2009  lukem Fix most of the -Wsign-compare issues.
 1.7 26-Jun-2005  christos branches: 1.7.28;
warns=3
- remove param names from function decls
- sprinkle const
- rename shadowed variables
- XXX: there is a lot of fishy __UNCONST going on.
 1.6 07-Jul-2004  mycroft Cleanup of ksh memory handling from OpenBSD, via Stefan Krueger in PR 24962.
Should also fix:
PR 8153
PR 13301
PR 15256
PR 25084
 1.5 23-Jun-2003  agc Add NetBSD RCS Ids.
 1.4 20-Oct-1999  hubertf resolve conflicts
 1.3 20-Oct-1997  lukem branches: 1.3.6;
remove $Log$ entries
 1.2 12-Jan-1997  tls RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.2 20-Oct-1999  hubertf Import pdksh V5.2.14.
Includes lots of bugfixes.
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.3.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.7.28.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.13.6.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.13.6.1 21-Apr-2020  martin Sync with HEAD
 1.13 03-Jun-2018  kamil branches: 1.13.2;
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>
 1.12 24-Jan-2018  kamil branches: 1.12.2;
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>
 1.11 24-Jan-2018  kamil 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>
 1.10 01-Jul-2017  joerg Kill enough K&R cruft to build with clang again.
 1.9 30-Jun-2017  kamil ksh: Upgrade to C99 <stdbool.h>

This shell already used C99 functions.
 1.8 30-Jun-2017  kamil ksh: Drop old hack fo SunOS 4.1.x (1990-1994)
 1.7 26-Jun-2005  christos warns=3
- remove param names from function decls
- sprinkle const
- rename shadowed variables
- XXX: there is a lot of fishy __UNCONST going on.
 1.6 07-Jul-2004  mycroft Cleanup of ksh memory handling from OpenBSD, via Stefan Krueger in PR 24962.
Should also fix:
PR 8153
PR 13301
PR 15256
PR 25084
 1.5 25-Sep-2002  provos change complete-list to first complete and then print a list,
also complete after '=' (dd), and ':' (ssh) and ` (backtick)
improvements from camield@openbsd.org. approved by perry and thorpej.
 1.4 20-Oct-1999  hubertf resolve conflicts
 1.3 26-Jul-1998  mycroft branches: 1.3.4;
Remove silly prototype.
 1.2 12-Jan-1997  tls RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.3 20-Oct-1999  hubertf Import pdksh V5.2.14.
Includes lots of bugfixes.
 1.1.1.2 18-Dec-1996  jtc import pdksh 5.2.12
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.3.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.12.2.1 25-Jun-2018  pgoyette Sync with HEAD
 1.13.2.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.13.2.1 21-Apr-2020  martin Sync with HEAD
 1.35 30-Jun-2017  kamil branches: 1.35.6;
ksh: Drop old hack for FreeBSD 1.1.5 and CLK_TCK
 1.34 30-Jun-2017  kamil ksh: Upgrade to C99 <stdbool.h>

This shell already used C99 functions.
 1.33 30-Jun-2017  kamil ksh: Eliminate dead MEM_DEBUG sections, there is missing code for it

MEM_DEBUG used to contain malloc(3) debugging facilities.
 1.32 30-Jun-2017  kamil ksh: Drop support for OSes without POSIX sigaction(2)
 1.31 30-Jun-2017  kamil ksh: Assume ANSI C prototypes
 1.30 30-Jun-2017  kamil ksh: Drop support for systems without functional waitpid(2)
 1.29 23-Jun-2017  kamil ksh: Replace homegrown int_least32_t with the C99 version
 1.28 23-Jun-2017  kamil ksh: Use ANSI C varargs, drop support for older version <varargs.h>
 1.27 23-Jun-2017  kamil ksh: Remove support for NeXT Operating System
 1.26 22-Jun-2017  kamil ksh: Drop support for systems without dup2(2)
 1.25 22-Jun-2017  kamil ksh: Drop support for ISC UNIX
 1.24 22-Jun-2017  kamil ksh: Drop support for systems without offsetof(3)
 1.23 22-Jun-2017  kamil ksh: Drop support for systems without <limits.h>
 1.22 22-Jun-2017  kamil ksh: Drop support for systems without <fcntl.h>
 1.21 22-Jun-2017  kamil ksh: Drop support for systems without memmove(3) and stop using bcopy(3)
 1.20 22-Jun-2017  kamil ksh: Drop support for systems without memset(3)
 1.19 22-Jun-2017  kamil ksh: Drop usage of SVID header <memory.h>, it's legacy standard
 1.18 22-Jun-2017  kamil ksh: Drop support for systems without strcasecmp(3) and strncasecmp(3)
 1.17 22-Jun-2017  kamil ksh: Drop support for systems without strstr(3)
 1.16 22-Jun-2017  kamil ksh: Drop support for systems without <string.h>
 1.15 22-Jun-2017  kamil ksh: Drop support for systems without <stddef.h>

No functional change.
 1.14 22-Jun-2017  kamil ksh: Drop support for systems without <stdlib.h>

No functional change.
 1.13 22-Jun-2017  kamil ksh: Drop support for systems without <unistd.h>

This code wouldn't work for them anyway.

No functional change.
 1.12 22-Jun-2017  kamil Reapply removal of code from 90ties for OS/2 Cygwin AIX HPUX SCOUnix

Added missing #endif terminator in emacs.c
 1.11 22-Jun-2017  kamil Temporarily revert previous.

emacs.* gets wrong code in generation
 1.10 22-Jun-2017  kamil Remove code for AIX, including hack for 3.2.5 (from 1997) - from ksh(1)

OK by <kre>
 1.9 22-Jun-2017  kamil Remove os2 support in ksh(1)

OK by <kre>
 1.8 22-Jun-2017  kamil Remove ancient cygwin support in ksh(1)

OK by <kre>
 1.7 26-Jun-2005  christos warns=3
- remove param names from function decls
- sprinkle const
- rename shadowed variables
- XXX: there is a lot of fishy __UNCONST going on.
 1.6 07-Jul-2004  mycroft Cleanup of ksh memory handling from OpenBSD, via Stefan Krueger in PR 24962.
Should also fix:
PR 8153
PR 13301
PR 15256
PR 25084
 1.5 11-Jul-2002  wiz 'environment', not 'enviroment'
 1.4 21-Jan-2000  mycroft Nuke `extern int errno;' in code we compile with -Wstrict-prototypes. We get
the correct definition from errno.h.
 1.3 20-Oct-1999  hubertf resolve conflicts
 1.2 12-Jan-1997  tls branches: 1.2.4; 1.2.6;
RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.3 20-Oct-1999  hubertf Import pdksh V5.2.14.
Includes lots of bugfixes.
 1.1.1.2 09-Oct-1996  jtc import pdksh 5.2.9
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.2.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.2.4.1 23-Jan-2000  he Pull up revision 1.4 (requested by mycroft):
Always do the errno indirection hack, so that non-threaded
libraries get the right errno value when linked with -lpthread.
This means "always include <errno.h> and never declare errno
yourself".
 1.35.6.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.35.6.1 21-Apr-2020  martin Sync with HEAD
 1.16 05-Feb-2024  andvar s/formated/formatted/ in comment and distrib sets descriptions.
 1.15 06-Nov-2021  msaitoh s/writting/writing/
 1.14 16-Sep-2021  christos It is either teaching gcc about aaah or adding FALLTHROUGH.
 1.13 30-Jun-2017  kamil branches: 1.13.6;
ksh: Drop support for systems without <sys/stat.h>

In future the order of includes will be normalized with KNF.

No functional change intended.
 1.12 30-Jun-2017  kamil ksh: Assume ANSI C prototypes
 1.11 23-Jun-2017  kamil ksh: Use ANSI C varargs, drop support for older version <varargs.h>
 1.10 22-Jun-2017  kamil Reapply removal of code from 90ties for OS/2 Cygwin AIX HPUX SCOUnix

Added missing #endif terminator in emacs.c
 1.9 22-Jun-2017  kamil Temporarily revert previous.

emacs.* gets wrong code in generation
 1.8 22-Jun-2017  kamil Remove os2 support in ksh(1)

OK by <kre>
 1.7 26-Jun-2005  christos warns=3
- remove param names from function decls
- sprinkle const
- rename shadowed variables
- XXX: there is a lot of fishy __UNCONST going on.
 1.6 07-Jul-2004  mycroft Cleanup of ksh memory handling from OpenBSD, via Stefan Krueger in PR 24962.
Should also fix:
PR 8153
PR 13301
PR 15256
PR 25084
 1.5 23-Jun-2003  agc Add NetBSD RCS Ids.
 1.4 02-Nov-2000  christos PR/11361:Matthias Buelow: Apply patch1 from pdksh site.
- set -x dumps core (shf.c);
- output of "jobs" command is filled with ^A characters (jobs.c);
- "typeset -r foo=bar" fails saying foo is readonly (var.c).
 1.3 20-Oct-1999  hubertf branches: 1.3.6;
resolve conflicts
 1.2 12-Jan-1997  tls branches: 1.2.6;
RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.2 20-Oct-1999  hubertf Import pdksh V5.2.14.
Includes lots of bugfixes.
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.2.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.3.6.1 26-Feb-2002  he Pull up revision 1.4 (requested by dogcow):
Synchronize with current development version:
o fix problem with ``set -x'' causing a core dump; fixes PR#11361
o fix problem with ``set -e'' causing early exit; fixes PR#11542
o fix various misspellings and nested extern declarations
 1.13.6.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.13.6.1 21-Apr-2020  martin Sync with HEAD
 1.3 20-Oct-1999  hubertf branches: 1.3.106;
resolve conflicts
 1.2 12-Jan-1997  tls branches: 1.2.6;
RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.2 20-Oct-1999  hubertf Import pdksh V5.2.14.
Includes lots of bugfixes.
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.2.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.3.106.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.3.106.1 21-Apr-2020  martin Sync with HEAD
 1.8 30-Jun-2017  kamil ksh: Drop support for OSes without POSIX sigaction(2)
 1.7 23-Jun-2017  kamil ksh: Drop the latest ifdef for BSD4.1 and eliminate dead code around it
 1.6 22-Jun-2017  kamil ksh: Drop fallback for BSD4.2 signal routines
 1.5 22-Jun-2017  kamil ksh: Remove fallback to BSD4.1 signal routines
 1.4 23-Jun-2003  agc Add NetBSD RCS Ids.
 1.3 25-May-2002  wiz __STDC__ is always defined on NetBSD.
 1.2 12-Jan-1997  tls RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.4 30-Jun-2017  kamil ksh: Drop support for OSes without POSIX sigaction(2)
 1.3 25-May-2002  wiz __STDC__ is always defined on NetBSD.
 1.2 12-Jan-1997  tls RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.2 12-Jan-1997  tls branches: 1.2.112;
RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.2.112.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.2.112.1 21-Apr-2020  martin Sync with HEAD
 1.13 23-Feb-2021  christos PR/56007: Greg A. Woods: ksh unable to execute ERR traps
(probably since 2016/03/17 - i.e. 8.x and 9.x)
 1.12 17-Mar-2016  christos branches: 1.12.8; 1.12.16; 1.12.18;
s/sed/${SED}/
 1.11 17-Mar-2016  christos put back the complex sed/awk since the code can't handle unsorted or repeated
entries (Rin Okuyama)
 1.10 16-Mar-2016  christos We don't need all this magic to build the signals lists. Do the work at
compile time.
 1.9 23-Jan-2011  hauke The Solaris 7 "/usr/{,xpg4/}bin/sort"s expect whitespace between the "-k"
option and its argument.
 1.8 25-Oct-2008  apb branches: 1.8.14;
In shell scripts run during the build, add a SED variable, defaulting
to "sed". SED=${TOOL_SED:Q} should be passed in the environment to
override this.
 1.7 19-Oct-2008  apb In shell scripts invoked during a build, and in crunchgen, use ${AWK}
instead of plain "awk". The Makefiles that invoke these scripts
or programs will pass AWK=${HOST_AWK:Q}.
 1.6 14-Nov-2006  christos fix the sort order too.
 1.5 14-Nov-2006  christos Fix signal list generation, from Jukka Salmi
 1.4 16-Oct-2006  christos use c99 initializers
 1.3 27-Jun-2006  christos PR/33834: Bucky Katz: Crossbuild on FC5:sort doesn't handle '+' field
specifications
 1.2 12-Jan-1997  tls RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.2 09-Oct-1996  jtc import pdksh 5.2.9
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.8.14.1 08-Feb-2011  bouyer Sync with HEAD
 1.12.18.1 23-Feb-2021  martin Pull up following revision(s) (requested by christos in ticket #1212):

bin/ksh/siglist.sh: revision 1.13

PR/56007: Greg A. Woods: ksh unable to execute ERR traps
(probably since 2016/03/17 - i.e. 8.x and 9.x)
 1.12.16.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.12.16.1 21-Apr-2020  martin Sync with HEAD
 1.12.8.1 23-Feb-2021  martin Pull up following revision(s) (requested by christos in ticket #1658):

bin/ksh/siglist.sh: revision 1.13

PR/56007: Greg A. Woods: ksh unable to execute ERR traps
(probably since 2016/03/17 - i.e. 8.x and 9.x)
 1.11 08-May-2018  kamil branches: 1.11.2;
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
 1.10 30-Jun-2017  kamil branches: 1.10.4;
ksh: Upgrade to C99 <stdbool.h>

This shell already used C99 functions.
 1.9 16-Oct-2006  christos use c99 initializers
 1.8 22-Mar-2006  christos Change previous to assert that the index is within bounds only when the
index actually used.
 1.7 18-Mar-2006  christos Coverity CID 1484: Static buffer overrun.
 1.6 07-Jul-2004  mycroft Cleanup of ksh memory handling from OpenBSD, via Stefan Krueger in PR 24962.
Should also fix:
PR 8153
PR 13301
PR 15256
PR 25084
 1.5 23-Jun-2003  agc Add NetBSD RCS Ids.
 1.4 20-Oct-1999  hubertf resolve conflicts
 1.3 09-Oct-1998  erh branches: 1.3.4;
Initialize the correct fields when creating an empty command. Fixes bug where a function with no body ( e.g. "testfun () ;" ) would cause ksh to segfault when it was called.
 1.2 12-Jan-1997  tls RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.5 20-Oct-1999  hubertf Import pdksh V5.2.14.
Includes lots of bugfixes.
 1.1.1.4 18-Dec-1996  jtc import pdksh 5.2.12
 1.1.1.3 09-Oct-1996  jtc import pdksh 5.2.11
 1.1.1.2 09-Oct-1996  jtc import pdksh 5.2.9
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.3.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.10.4.1 21-May-2018  pgoyette Sync with HEAD
 1.11.2.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.11.2.1 21-Apr-2020  martin Sync with HEAD
 1.8 03-Jun-2018  kamil branches: 1.8.2;
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>
 1.7 08-May-2018  kamil 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
 1.6 24-Jan-2018  kamil branches: 1.6.2;
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>
 1.5 24-Jan-2018  kamil 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>
 1.4 23-Jun-2003  agc Add NetBSD RCS Ids.
 1.3 19-Aug-1998  thorpej Add some braces to make egcs happy.
 1.2 12-Jan-1997  tls RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.6.2.2 25-Jun-2018  pgoyette Sync with HEAD
 1.6.2.1 21-May-2018  pgoyette Sync with HEAD
 1.8.2.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.8.2.1 21-Apr-2020  martin Sync with HEAD
 1.4 03-Jun-2018  kamil branches: 1.4.2;
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>
 1.3 20-Oct-1999  hubertf branches: 1.3.104;
resolve conflicts
 1.2 12-Jan-1997  tls branches: 1.2.6;
RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.3 20-Oct-1999  hubertf Import pdksh V5.2.14.
Includes lots of bugfixes.
 1.1.1.2 18-Dec-1996  jtc import pdksh 5.2.12
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.2.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.3.104.1 25-Jun-2018  pgoyette Sync with HEAD
 1.4.2.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.4.2.1 21-Apr-2020  martin Sync with HEAD
 1.14 08-May-2018  kamil branches: 1.14.2;
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
 1.13 30-Jun-2017  kamil branches: 1.13.4;
ksh: Upgrade to C99 <stdbool.h>

This shell already used C99 functions.
 1.12 23-Jun-2017  kamil ksh: Drop support for UNIX V7-style signal routines
 1.11 22-Jun-2017  kamil Reapply removal of code from 90ties for OS/2 Cygwin AIX HPUX SCOUnix

Added missing #endif terminator in emacs.c
 1.10 22-Jun-2017  kamil Temporarily revert previous.

emacs.* gets wrong code in generation
 1.9 22-Jun-2017  kamil Remove code for AIX, including hack for 3.2.5 (from 1997) - from ksh(1)

OK by <kre>
 1.8 16-Oct-2006  christos use c99 initializers
 1.7 13-May-2006  christos Coverity CID 3367, 3368: Avoid NULL deref.
 1.6 07-Jul-2004  mycroft Cleanup of ksh memory handling from OpenBSD, via Stefan Krueger in PR 24962.
Should also fix:
PR 8153
PR 13301
PR 15256
PR 25084
 1.5 23-Jun-2003  agc Add NetBSD RCS Ids.
 1.4 16-Sep-2001  wiz Spell 'occurred' with two 'r's.
 1.3 20-Oct-1999  hubertf branches: 1.3.6;
resolve conflicts
 1.2 12-Jan-1997  tls branches: 1.2.6;
RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.2 20-Oct-1999  hubertf Import pdksh V5.2.14.
Includes lots of bugfixes.
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.2.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.3.6.1 26-Feb-2002  he Pull up revision 1.4 (requested by dogcow):
Synchronize with current development version:
o fix problem with ``set -x'' causing a core dump; fixes PR#11361
o fix problem with ``set -e'' causing early exit; fixes PR#11542
o fix various misspellings and nested extern declarations
 1.13.4.1 21-May-2018  pgoyette Sync with HEAD
 1.14.2.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.14.2.1 21-Apr-2020  martin Sync with HEAD
 1.10 16-Sep-2021  christos It is either teaching gcc about aaah or adding FALLTHROUGH.
 1.9 08-May-2018  kamil branches: 1.9.2;
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
 1.8 30-Jun-2017  kamil branches: 1.8.4;
ksh: Assume ANSI C prototypes
 1.7 23-Jun-2017  kamil ksh: Use ANSI C varargs, drop support for older version <varargs.h>
 1.6 26-Jun-2005  christos warns=3
- remove param names from function decls
- sprinkle const
- rename shadowed variables
- XXX: there is a lot of fishy __UNCONST going on.
 1.5 07-Jul-2004  mycroft Cleanup of ksh memory handling from OpenBSD, via Stefan Krueger in PR 24962.
Should also fix:
PR 8153
PR 13301
PR 15256
PR 25084
 1.4 23-Jun-2003  agc Add NetBSD RCS Ids.
 1.3 20-Oct-1999  hubertf resolve conflicts
 1.2 12-Jan-1997  tls branches: 1.2.6;
RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.2 20-Oct-1999  hubertf Import pdksh V5.2.14.
Includes lots of bugfixes.
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.2.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.8.4.1 21-May-2018  pgoyette Sync with HEAD
 1.9.2.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.9.2.1 21-Apr-2020  martin Sync with HEAD
 1.7 22-Jun-2017  kamil branches: 1.7.6;
Reapply removal of code from 90ties for OS/2 Cygwin AIX HPUX SCOUnix

Added missing #endif terminator in emacs.c
 1.6 22-Jun-2017  kamil Temporarily revert previous.

emacs.* gets wrong code in generation
 1.5 22-Jun-2017  kamil Remove os2 support in ksh(1)

OK by <kre>
 1.4 07-Jul-2004  mycroft Cleanup of ksh memory handling from OpenBSD, via Stefan Krueger in PR 24962.
Should also fix:
PR 8153
PR 13301
PR 15256
PR 25084
 1.3 20-Oct-1999  hubertf resolve conflicts
 1.2 12-Jan-1997  tls branches: 1.2.6;
RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.2 20-Oct-1999  hubertf Import pdksh V5.2.14.
Includes lots of bugfixes.
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.2.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.7.6.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.7.6.1 21-Apr-2020  martin Sync with HEAD
 1.10 24-Jul-2021  andvar Fix all remaining typos, mainly in comments but also in few definitions and log messages, reported by me in PR kern/54889.
Also fixed some additional typos in comments, found on review of same files or typos.
 1.9 30-Jun-2017  kamil branches: 1.9.6;
ksh: Upgrade to C99 <stdbool.h>

This shell already used C99 functions.
 1.8 30-Jun-2017  kamil ksh: Drop support for systems without <sys/stat.h>

In future the order of includes will be normalized with KNF.

No functional change intended.
 1.7 23-Jun-2017  kamil ksh: Remove remnant hack for SCO UNIX in tty code
 1.6 23-Jun-2017  kamil ksh: Remove support for NeXT Operating System
 1.5 23-Jun-2017  kamil ksh: Drop BSD4.3 temporary hack in tty code
 1.4 23-Jun-2003  agc Add NetBSD RCS Ids.
 1.3 07-Apr-1998  fair change some /dev/tty to paths.h _PATH_TTY
 1.2 12-Jan-1997  tls RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.2 09-Oct-1996  jtc import pdksh 5.2.9
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.9.6.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.9.6.1 21-Apr-2020  martin Sync with HEAD
 1.2 12-Jan-1997  tls branches: 1.2.112;
RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.2 18-Dec-1996  jtc import pdksh 5.2.12
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.2.112.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.2.112.1 21-Apr-2020  martin Sync with HEAD
 1.26 08-Sep-2024  rillig s/effect/affect/ in a few places
 1.25 02-Jun-2023  andvar branches: 1.25.2;
follow the steps of Andrew Doran (ad) commit and fix more s/loose/lose/ typos.
also s/beyound/beyond/ and few others along the way, mainly in comments.
 1.24 08-May-2018  kamil branches: 1.24.2;
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
 1.23 24-Jan-2018  kamil branches: 1.23.2;
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>
 1.22 24-Jan-2018  kamil 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>
 1.21 01-Jul-2017  joerg Kill enough K&R cruft to build with clang again.
 1.20 30-Jun-2017  kamil ksh: Upgrade to C99 <stdbool.h>

This shell already used C99 functions.
 1.19 30-Jun-2017  kamil ksh: Drop support for systems without <sys/stat.h>

In future the order of includes will be normalized with KNF.

No functional change intended.
 1.18 30-Jun-2017  kamil ksh: Drop support for systems with broken <sys/time.h> / <time.h>
 1.17 16-Oct-2011  joerg Don't use non-literal format strings.
 1.16 16-Oct-2011  reed Typo in comment fix from Snader_LB via IRC.
 1.15 02-Aug-2009  dsl Support 0xnn for hexadecimal constants - as well as 16#nn.
While here, make '-' only valid once, and at the start of the number.
Fixes PR/40512
 1.14 29-Mar-2006  christos branches: 1.14.26; 1.14.32;
Don't leak memory if RJUST|LJUST is specified. This might fix a whole bunch
of Coverity issues, but we'll wait until the next run :-)
 1.13 19-Mar-2006  christos Coverity CID 1606: Plug memory leak.
 1.12 26-Jun-2005  christos warns=3
- remove param names from function decls
- sprinkle const
- rename shadowed variables
- XXX: there is a lot of fishy __UNCONST going on.
 1.11 23-May-2005  rillig Fixed wrong use of the <ctype.h> functions by adding (unsigned char) casts.
 1.10 28-Oct-2004  dsl Add (unsigned char) casts to tolower/toupper and iscntrl calls.
Fixes PR 27593
 1.9 07-Jul-2004  mycroft Cleanup of ksh memory handling from OpenBSD, via Stefan Krueger in PR 24962.
Should also fix:
PR 8153
PR 13301
PR 15256
PR 25084
 1.8 23-Jun-2003  agc Add NetBSD RCS Ids.
 1.7 02-Nov-2000  christos PR/11361:Matthias Buelow: Apply patch1 from pdksh site.
- set -x dumps core (shf.c);
- output of "jobs" command is filled with ^A characters (jobs.c);
- "typeset -r foo=bar" fails saying foo is readonly (var.c).
 1.6 20-Oct-1999  hubertf branches: 1.6.6;
resolve conflicts
 1.5 04-Nov-1998  christos branches: 1.5.4;
char->unsigned char casts for is*()
 1.4 19-Aug-1998  thorpej Add some braces to make egcs happy.
 1.3 20-Jul-1997  christos Fix compiler warnings
Add WARNS=1
 1.2 12-Jan-1997  tls RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.3 20-Oct-1999  hubertf Import pdksh V5.2.14.
Includes lots of bugfixes.
 1.1.1.2 09-Oct-1996  jtc import pdksh 5.2.9
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.5.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.6.6.1 26-Feb-2002  he Pull up revision 1.7 (requested by dogcow):
Synchronize with current development version:
o fix problem with ``set -x'' causing a core dump; fixes PR#11361
o fix problem with ``set -e'' causing early exit; fixes PR#11542
o fix various misspellings and nested extern declarations
 1.14.32.1 21-Apr-2010  matt sync to netbsd-5
 1.14.26.1 26-Sep-2009  snj Pull up following revision(s) (requested by tron in ticket #889):
bin/ksh/var.c: revision 1.15
Support 0xnn for hexadecimal constants - as well as 16#nn.
While here, make '-' only valid once, and at the start of the number.
Fixes PR/40512
 1.23.2.1 21-May-2018  pgoyette Sync with HEAD
 1.24.2.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.24.2.1 21-Apr-2020  martin Sync with HEAD
 1.25.2.1 02-Aug-2025  perseant Sync with HEAD
 1.5 26-Jun-2005  christos branches: 1.5.86;
warns=3
- remove param names from function decls
- sprinkle const
- rename shadowed variables
- XXX: there is a lot of fishy __UNCONST going on.
 1.4 23-Jun-2003  agc Add NetBSD RCS Ids.
 1.3 20-Oct-1999  hubertf resolve conflicts
 1.2 12-Jan-1997  tls branches: 1.2.6;
RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.5 20-Oct-1999  hubertf Import pdksh V5.2.14.
Includes lots of bugfixes.
 1.1.1.4 18-Dec-1996  jtc import pdksh 5.2.12
 1.1.1.3 09-Oct-1996  jtc import pdksh 5.2.11
 1.1.1.2 09-Oct-1996  jtc import pdksh 5.2.9
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.2.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.5.86.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.5.86.1 21-Apr-2020  martin Sync with HEAD
 1.21 16-Sep-2021  christos It is either teaching gcc about aaah or adding FALLTHROUGH.
 1.20 08-May-2018  kamil branches: 1.20.2;
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
 1.19 24-Jan-2018  kamil branches: 1.19.2;
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>
 1.18 30-Jun-2017  kamil ksh: Upgrade to C99 <stdbool.h>

This shell already used C99 functions.
 1.17 30-Jun-2017  kamil ksh: Drop support for systems without <sys/stat.h>

In future the order of includes will be normalized with KNF.

No functional change intended.
 1.16 22-Jun-2017  kamil Reapply removal of code from 90ties for OS/2 Cygwin AIX HPUX SCOUnix

Added missing #endif terminator in emacs.c
 1.15 22-Jun-2017  kamil Temporarily revert previous.

emacs.* gets wrong code in generation
 1.14 22-Jun-2017  kamil Remove os2 support in ksh(1)

OK by <kre>
 1.13 03-Feb-2016  christos PR/50747: David Binderman: check bounds before dereference.
While here add some continues before semicolons.
 1.12 22-Jun-2011  mrg add a missing part from rev 1.10 and s/newline/newlinex/. found by GCC 4.5.3.
 1.11 25-Apr-2009  lukem Fix most of the -Wsign-compare issues.
 1.10 26-Jun-2005  christos branches: 1.10.28;
warns=3
- remove param names from function decls
- sprinkle const
- rename shadowed variables
- XXX: there is a lot of fishy __UNCONST going on.
 1.9 28-Oct-2004  dsl Add (unsigned char) casts to tolower/toupper and iscntrl calls.
Fixes PR 27593
 1.8 07-Jul-2004  mycroft Cleanup of ksh memory handling from OpenBSD, via Stefan Krueger in PR 24962.
Should also fix:
PR 8153
PR 13301
PR 15256
PR 25084
 1.7 23-Jun-2003  agc Add NetBSD RCS Ids.
 1.6 20-Aug-2001  wiz "wierd" is weird.
 1.5 02-Nov-1999  jdolecek branches: 1.5.6;
Implement (somewhat enhanced) idea stealed from bash:
when completing the filename (either in vi mode with vi-tabcomplete on,
or in emacs mode after double esc), escape any shell special characters
and chars from $IFS with backslash - very handy especially when
dealing with filenames containing spaces

The patch has been sent to maintainer, but I haven't got any reply yet even
after about a month :(
 1.4 20-Oct-1999  hubertf resolve conflicts
 1.3 04-Nov-1998  christos branches: 1.3.4;
char->unsigned char casts for is*()
 1.2 12-Jan-1997  tls RCS ID Police
 1.1 21-Sep-1996  jtc branches: 1.1.1;
Initial revision
 1.1.1.3 20-Oct-1999  hubertf Import pdksh V5.2.14.
Includes lots of bugfixes.
 1.1.1.2 18-Dec-1996  jtc import pdksh 5.2.12
 1.1.1.1 21-Sep-1996  jtc import pdksh 5.2.8
 1.3.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.5.6.1 26-Feb-2002  he Pull up revision 1.6 (requested by dogcow):
Synchronize with current development version:
o fix problem with ``set -x'' causing a core dump; fixes PR#11361
o fix problem with ``set -e'' causing early exit; fixes PR#11542
o fix various misspellings and nested extern declarations
 1.10.28.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.19.2.1 21-May-2018  pgoyette Sync with HEAD
 1.20.2.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.20.2.1 21-Apr-2020  martin Sync with HEAD
 1.10 27-Sep-1999  jdolecek branches: 1.10.106;
Move symlink.7 to share/man/man7/, where it belongs
 1.9 20-Jul-1997  christos branches: 1.9.6;
Remove WARNS=1 from all the subdirectory Makefiles, and add it to Makefile.inc
now that all /bin has been cleaned.
 1.8 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.7 21-Mar-1995  cgd convert to new RCS id conventions.
 1.6 22-Dec-1994  cgd new way of specifying man pages.
 1.5 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.9.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.10.106.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.10.106.1 21-Apr-2020  martin Sync with HEAD
 1.28 20-Apr-2017  christos branches: 1.28.12;
- fix number in copyright
- comment out "simplified link" for now.
 1.27 20-Apr-2017  christos Replace ours with the FreeBSD version; it is more versatile and handles
errors better (does not remove files if it is going to fail when -f).
 1.26 10-Aug-2016  sevan branches: 1.26.2;
ln was there from v1.
Confirmed from the TUHS & cat-v.org hosted copies of man pages
 1.25 22-Mar-2012  wiz branches: 1.25.16;
Fix whitespace nits. Suggested by Bug Hunting.
 1.24 24-Oct-2011  yamt branches: 1.24.2;
note what "other ln implementations" is.
 1.23 27-Feb-2011  wiz Typographical improvement by David H. Gutteridge in PR 44645.
 1.22 29-Oct-2007  jnemeth branches: 1.22.24;
PR/37243 - Joerg Niendorf -- fixup botched addition of -i option
 1.21 07-Oct-2006  elad branches: 1.21.2; 1.21.4; 1.21.8;
PR/22799: Hiramatsu Yoshifumi: Add -i option to /bin/ln

Slightly modified patch applied, thanks!
 1.20 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.19 25-Feb-2003  wiz .Nm does not need a dummy argument ("") before punctuation or
for correct formatting of the SYNOPSIS any longer.
 1.18 28-Dec-2002  wiz Whitespace nits.
 1.17 26-Dec-2002  jrf Added verbose extension and updated manual pages. Changes approved by Klaus Klein.
 1.16 25-Sep-2002  wiz New policy: New sentences start on a new line.
Patches by Robert Elz <kre at munnari oz au>, with minimal changes by me.
 1.15 14-Oct-2000  bjh21 Document POSIX.2 conformance.
In the second form, the destination directory isn't optional.
Formatting cleanups.
 1.14 22-Mar-1999  garbled More and more .Os cleanups. .Os is defined in the tmac.doc-common file,
so we shouldn't override it with versions in the manpages. Many more to
come.
 1.13 20-Oct-1997  enami Fix .Nm usage.
 1.12 16-May-1997  mycroft Correct bogons and omissions in previous change.
Also, remove the `-F' option, which was undocumented and is no longer useful.
 1.11 16-May-1997  jtk add -h flag to suppress following of symbolic links
 1.10 25-Jul-1995  jtc Use "utility" instead of "command". Modern definitions of these terms
are distinct (See POSIX.2 glossary).

A utility is a executable, script or shell builtin; while a command
can be any of those things plus lists, pipelines, compound commands
(if, for, while) and shell function definitions.
 1.9 21-Mar-1995  cgd convert to new RCS id conventions.
 1.8 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.7 11-Jan-1994  jtc Fix spelling errors.
 1.6 19-Aug-1993  jtc Document the fact that ln now conforms to IEEE 1003.2-92.
 1.5 17-Aug-1993  jtc Make 1003.2-1992 compliant by implementing -f (force) option.
 1.4 01-Aug-1993  mycroft Add RCS indentifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.21.8.1 06-Nov-2007  matt sync with HEAD
 1.21.4.1 06-Jan-2008  wrstuden Catch up to netbsd-4.0 release.
 1.21.2.1 30-Oct-2007  liamjfoy Pull up following revision(s) (requested by jnemeth in ticket #960):
bin/ln/ln.1: revision 1.22
PR/37243 - Joerg Niendorf -- fixup botched addition of -i option
 1.22.24.1 05-Mar-2011  bouyer Sync with HEAD
 1.24.2.1 17-Apr-2012  yamt sync with head
 1.25.16.1 26-Apr-2017  pgoyette Sync with HEAD
 1.26.2.1 21-Apr-2017  bouyer Sync with HEAD
 1.28.12.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.28.12.1 21-Apr-2020  martin Sync with HEAD
 1.40 26-Aug-2018  sevan Update usage to include -w
Match sequence as per SYNOPSIS in manual
 1.39 22-Apr-2017  kre branches: 1.39.4; 1.39.10; 1.39.12;

When called as "link" (not currently installed that way) always simply
do a link(2) sys call, never use the internal linkit() routine, which
allows for a destination directory and installs the link inside (and more.)

This makes ln's "link" variant comply with its (currently commented out)
section if its manual page, and also makes it identical to /usr/sbin/link.
 1.38 21-Apr-2017  szptvlfn fix number in copyright(4->3)
 1.37 21-Apr-2017  christos mark usage dead, simplify strings in it.
 1.36 20-Apr-2017  christos Replace ours with the FreeBSD version; it is more versatile and handles
errors better (does not remove files if it is going to fail when -f).
 1.35 29-Aug-2011  joerg branches: 1.35.26; 1.35.30;
static + __dead
 1.34 20-Jul-2008  lukem Remove the \n and tabs from the __COPYRIGHT() strings.
 1.33 26-May-2008  christos PR/38755: murray armfield: /bin posix programs missing setprogname and/or
setlocale
 1.32 13-Oct-2006  wiz branches: 1.32.16; 1.32.18;
Add -i to usage.
 1.31 07-Oct-2006  elad PR/22799: Hiramatsu Yoshifumi: Add -i option to /bin/ln

Slightly modified patch applied, thanks!
 1.30 26-Jun-2005  christos sprinkle a little const, and now everything compiles with WARNS=3
 1.29 25-Jun-2004  wiz Add -v to usage. Closes PR 25862 by Kouichirou Hiratsuka.
 1.28 05-Jan-2004  jmmv Homogenize usage messages: make the 'usage' word all lowercase, as this seems
to be the most common practice in our tree.
 1.27 04-Jan-2004  jschauma remove unused code left from printescaped() backput as pointed out by Jeff
Ito in PR bin/23969 and PR bin/23970.
 1.26 14-Sep-2003  jschauma Following private discussion with kleink@ and hubertf@ and public discussion
on tech-userlevel@, back out printescaped() functionality.
kleink: ``We sell rope.''
 1.25 21-Aug-2003  jschauma Fix two more memory leaks found by Hubert Feyrer.
 1.24 13-Aug-2003  itojun use bounded string op
 1.23 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.22 04-Aug-2003  jschauma As discusses a little while back on tech-userlevel:

If stdout is a tty, use vis(3) to print any filenames to prevent garbage
from being printed if the filename contains control- or other non-printable
characters.

While here, sprinkle some EXIT_FAILURE and NOTREACHED where appropriate.
 1.21 27-Dec-2002  jrf The previious ln commit did not have the change for -v in the source. Here is the working code.
 1.20 30-Oct-2002  kleink Revert rev. 1.16, as per PR kern/17411.

While a hard link to a symbolic link is not ruled out by POSIX-2001,
the ln(1) utility (sans -s) is to perform equivalent to the link(2)
function on its operands, which includes the resolution of symbolic
links in source_file arguments.
 1.19 16-Sep-2001  wiz ANSIfy, KNF, by Petri Koistinen.
 1.18 19-Feb-2001  cgd convert to use getprogname()
 1.17 04-Feb-2001  christos fix nested extern.
 1.16 05-Sep-1999  hubertf Allow hardlinks to symlinks.

Reviewed by: Bill Studenmund, Klaus Klein
 1.15 28-Jul-1998  mycroft Delint.
 1.14 05-Nov-1997  cgd lint
 1.13 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.12 16-May-1997  mycroft Correct bogons and omissions in previous change.
Also, remove the `-F' option, which was undocumented and is no longer useful.
 1.11 16-May-1997  jtk add -h flag to suppress following of symbolic links
 1.10 21-Mar-1995  cgd convert to new RCS id conventions.
 1.9 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.8 08-Feb-1994  mycroft Fix core dump when source is a directory.
 1.7 27-Jan-1994  jtc Converted to use standard err()/warn() error messages functions.
 1.6 27-Jan-1994  jtc Pass through FlexeLint
 1.5 17-Aug-1993  jtc Make 1003.2-1992 compliant by implementing -f (force) option.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.32.18.2 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.32.18.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.32.16.1 04-Jun-2008  yamt sync with head
 1.35.30.1 21-Apr-2017  bouyer Sync with HEAD
 1.35.26.1 26-Apr-2017  pgoyette Sync with HEAD
 1.39.12.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.39.12.2 21-Apr-2020  martin Sync with HEAD
 1.39.12.1 10-Jun-2019  christos Sync with HEAD
 1.39.10.1 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.39.4.1 28-Aug-2018  martin Pull up following revision(s) (requested by sevan in ticket #992):

external/bsd/top/dist/top.1.in: revision 1.11
sbin/gpt/main.c: revision 1.12
sbin/amrctl/amrctl.c: revision 1.11
bin/df/df.c: revision 1.93
sbin/fsck_ext2fs/fsck_ext2fs.8: revision 1.21
sbin/fsck_ext2fs/main.c: revision 1.38
bin/ksh/ksh.Man: revision 1.26
bin/ln/ln.c: revision 1.40
bin/df/df.1: revision 1.48
bin/df/df.1: revision 1.49

Document the WCPU field.

Match SYNOPSIS with usage()

-G cannot be specified alongside -i or -P.
Heads up by <leot>

Add -l to SYNOPSIS

Update usage to include -w

Match sequence as per SYNOPSIS in manual

Remove reference to -c flag which was never implemented.

Remove references to -c flag which was never included.

Add the -T flag to usage()
 1.11 27-Sep-1999  jdolecek Move symlink.7 to share/man/man7/, where it belongs
 1.10 26-Sep-1999  jdolecek add lutimes(2) to list of syscalls not following symlinks

Noted by Christos Zoulas <christos@NetBSD.org>
 1.9 22-Mar-1999  garbled branches: 1.9.4;
More and more .Os cleanups. .Os is defined in the tmac.doc-common file,
so we shouldn't override it with versions in the manpages. Many more to
come.
 1.8 13-Dec-1998  fair Patch per PR#6166 and clean up some nroff bogons.
 1.7 06-Feb-1998  perry macroize BSD, NetBSD, FreeBSD and misc cleanup
 1.6 20-Oct-1997  enami Partial .Nm usage fix. XXX
 1.5 25-Mar-1997  mikel add xref to symlink(2)
 1.4 25-Apr-1996  mycroft Remove comment regarding 4.4 symlink behaviour.
 1.3 21-Mar-1995  cgd convert to new RCS id conventions.
 1.2 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.1 22-Sep-1994  mycroft branches: 1.1.1;
Initial revision
 1.1.1.1 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.9.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.15 17-May-2020  christos No ACL support for install media (SMALLPROG)
 1.14 14-Dec-2006  he branches: 1.14.78;
Complete the move of string_to_flags() and flags_to_string() from the
bin/ls sources to libutil:
o Bump libutil minor version number
o Fix uses to include <util.h> to pick up the function definitions
o Fix most uses of flags_to_string() to release the now-malloc()ed result
 1.13 03-Nov-2001  lukem change from WARNS?=1 to WARNS?=2 for bin/
 1.12 19-Oct-2001  lukem enable WARNS?= 2
 1.11 17-May-1999  lukem Rename ls.c::main() -> ls.c::ls_main(), and add a wrapper in main.c::main().
This allows other programs to link in against ls (e.g, ftpd), without having
to have hacks to deal with multiple main()s.

Yes, I know this is ugly. The clean alternative (move lots of this
into libutil, yada yada yada) isn't fun, and the effort probably isn't
justified...
 1.10 20-Jul-1997  christos Remove WARNS=1 from all the subdirectory Makefiles, and add it to Makefile.inc
now that all /bin has been cleaned.
 1.9 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.8 21-Mar-1995  cgd convert to new RCS id conventions.
 1.7 23-Sep-1994  mycroft Merge with 4.4-Lite version.
 1.6 28-Jan-1994  cgd don't need -lutil
 1.5 07-Aug-1993  mycroft New version from uunet, with -k option readded, and 4.4 ffs--specific stuff
in `#ifdef notyet'.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.14.78.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.14.78.1 21-Apr-2020  martin Sync with HEAD
 1.17 07-Aug-2003  agc branches: 1.17.98;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.16 29-Jul-2000  lukem convert to new KNF
 1.15 23-Jul-2000  mycroft ts_nsec -> tv_nsec, per POSIX.
 1.14 09-Oct-1998  enami - To make this file compile again on NetBSD, force to use
old (i.e. posix version of timespec) code if defined(__NetBSD__).
- Introduce some macro for readability.
 1.13 08-Oct-1998  wsanchez Global replace:
-#ifdef _POSIX_SOURCE
+#if defined(_POSIX_SOURCE) || defined(_POSIX_C_SOURCE) || defined (_XOPEN_SOURCE)
 1.12 08-Oct-1998  wsanchez Handle non-POSIX version of timespec.
 1.11 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.10 08-Jul-1996  mycroft Stylistic change of no consequence.
 1.9 08-Jul-1996  mycroft Compare nanosecond field in timestamps.
Always do name comparison if other sort field is equal.
 1.8 21-Mar-1995  cgd branches: 1.8.6;
convert to new RCS id conventions.
 1.7 23-Sep-1994  mycroft Merge with 4.4-Lite version.
 1.6 05-Dec-1993  mycroft Patches from Thomas Eberhardt <thomas@mathematik.uni-Bremen.de> to allow
sorting by size.
 1.5 07-Aug-1993  mycroft New version from uunet, with -k option readded, and 4.4 ffs--specific stuff
in `#ifdef notyet'.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.8.6.1 10-Dec-1996  mycroft From trunk:
Various changes to make the sorting deterministic (and correct).
 1.17.98.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.17.98.1 21-Apr-2020  martin Sync with HEAD
 1.17 29-Aug-2011  joerg branches: 1.17.44;
Move usage and mark it static+dead
 1.16 14-Dec-2006  he Complete the move of string_to_flags() and flags_to_string() from the
bin/ls sources to libutil:
o Bump libutil minor version number
o Fix uses to include <util.h> to pick up the function definitions
o Fix most uses of flags_to_string() to release the now-malloc()ed result
 1.15 14-Sep-2003  jschauma Implement '-b' option, which, following FreeBSD, Linux and (I think) Solaris
prints octal escapes for nongraphic characters.
 1.14 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.13 29-Jul-2000  lukem convert to new KNF
 1.12 22-Jun-2000  assar make sure to print file names consistenly in column mode
 1.11 17-Jun-2000  assar branches: 1.11.2;
Do not modify the file name when not printing non-printables, also do
it consistently with the file name and possible symbolic link target.

fixes bin/10385 and bin/10384
 1.10 17-May-1999  lukem branches: 1.10.6;
Rename ls.c::main() -> ls.c::ls_main(), and add a wrapper in main.c::main().
This allows other programs to link in against ls (e.g, ftpd), without having
to have hacks to deal with multiple main()s.

Yes, I know this is ugly. The clean alternative (move lots of this
into libutil, yada yada yada) isn't fun, and the effort probably isn't
justified...
 1.9 12-Feb-1999  kleink Implement 1003.2 -m option (stream output format).
 1.8 10-Oct-1998  mrg create a "stat_flags.h" to go with stat_flags.c
 1.7 18-Jan-1998  lukem * implement -x; sort columns across the page rather than down the page
* in printcol(), don't bother building an index to the linked list if
only printing one column with printscol()...
* implement -n; prevent [gu]id->name mapping in long listings.
inspired by openbsd, but does *not* imply -l by itself.
* fix sorting with -d (or mismatched types in general). (from openbsd).
* document -g. (from openbsd).
 1.6 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.5 21-Mar-1995  cgd convert to new RCS id conventions.
 1.4 23-Sep-1994  mycroft Merge with 4.4-Lite version.
 1.3 25-Jan-1994  cgd (1) fixup getbsize() calls
(2) convert to use err(), warn(), and warnx(), as appropriate.
 1.2 05-Dec-1993  mycroft Patches from Thomas Eberhardt <thomas@mathematik.uni-Bremen.de> to allow
sorting by size.
 1.1 07-Aug-1993  mycroft branches: 1.1.1;
New file.
Note: This version of ls has deprecated the `-g' option. This is for POSIX
compatibility, I think.
 1.1.1.1 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.10.6.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.11.2.1 26-Jun-2000  assar merge in fixes for printing of strange filenames in column mode from trunk
approved by releng-1-5
 1.17.44.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.17.44.1 21-Apr-2020  martin Sync with HEAD
 1.83 06-Nov-2024  jschauma Correct details around when '-s' prints 'total': this is controlled by
whether we are using multi-column output (-C, -x) or single column output
(-1, -m) with the rationale being that multi-column output is intended
for human consumption.

Based on input from kre@
 1.82 05-Nov-2024  jschauma Note that when '-s' is combined with '-1', the 'total' is _not_ printed.
 1.81 16-May-2020  christos branches: 1.81.8;
Add ACL support for FFS. From FreeBSD.
 1.80 03-Jul-2017  wiz branches: 1.80.6;
Remove workaround for ancient HTML generation code.
 1.79 10-Aug-2016  sevan ls was there from v1.
Confirmed from the TUHS & cat-v.org hosted copies of man pages
 1.78 18-Oct-2014  jschauma Remove the confusing reference to 'file sizes' in the short description of
the '-l' flag. As explained in the Long Format section, the total is of
the blocks used by the files, and only applies to directories.
 1.77 02-Apr-2014  wiz Wording improvement for previous.
From jmc@OpenBSD via Igor Sobrado.
 1.76 02-Apr-2014  wiz Apply diff from Igor Sobrado <isd@orion.ciencias.uniovi.es>:

We have written a diff to our ls(1) to recover the traditional behaviour
of -f implying -a. This change does not only accommodates POSIX.1
but also matches traditional UNIX.

OpenBSD commit message:

CVSROOT: /cvs
Module name: src
Changes by: sobrado@cvs.openbsd.org 2014/03/31 14:54:37

Modified files:
bin/ls : ls.1 ls.c

Log message:
restore the traditional behavior of -f implying -a; apparently Keith Bostic
forgot to restore it when the -f flag was put back on 2nd of September 1989,
after being removed on 16th of August as a consequence of issues getting it
working over NFS, so deviation from traditional UNIX behavior in all BSDs
looks like an historical accident; as a side effect, this change accommodates
behavior of this option to IEEE Std 1003.1-2008 (``POSIX.1'').

joint work with jmc@ (who found the inaccuracy in our implementation),
schwarze@ (who provided a detailed tracking of historical facts) and millert@

ok millert@, schwarze@
 1.75 21-Feb-2014  christos Add -X (don't cross mount points when recursing) from tls@
 1.74 20-Feb-2014  wiz Use .Nm for 'ls'.
 1.73 20-Feb-2014  christos Add -O (only leaf files) and -P (print full path), from tls@
 1.72 20-Nov-2012  abs PR/47167
ls.1:
- Sort options in `SYNOPSIS', the option list, and texts within
the man page;
- improve wording;
- improve macro usage;
- use more consistency regarding (locations for) information about
which options override which;
- cross-reference `-d' and `-R';
- simplify description of `-k', removing redundant and unneeded
information;
- sort entry type list (but leave `-a' and `-A' as they are, given
their meaning);
- correct / augment description of `BLOCKSIZE' environment variable
in `ENVIRONMENT' section;
- bump date.

ls.c:
- Sort options in `usage';
- augment comment about when to figure out block size.
 1.71 21-Oct-2012  wiz Use more markup.
 1.70 21-Oct-2012  jschauma clarify that BLOCKSIZE also influences the total in long output
 1.69 02-Apr-2011  mbalmer branches: 1.69.4; 1.69.10;
Wording fixes from Ryo HAYASAKA, thanks.
 1.68 15-Mar-2011  wiz Remove trailing whitespace. Add -M to usage.
 1.67 15-Mar-2011  erh PR#7540, add a -M option to ls which causes sizes (and number of blocks) to be
displayed with comma separators (or a locale specific separator).
 1.66 17-Dec-2010  njoly Fix cross-reference, dir(5) -> dirent(3).
 1.65 14-May-2010  joerg Reorder sections to canonical order. Use .Ex -std
 1.64 05-Apr-2010  joerg \\ -> \e
 1.63 25-Sep-2008  lukem Change '-n' so that it implies '-l' rather than requires '-l'.
Brings ls(1) closer to POSIX.1 2004.
Per discussion with Thomas Klausner and Igor Sobrado.
 1.62 22-Sep-2006  elad PR/33635: Anne Bennett: Document 'a' and 'A' flags in ls(1).
strmode(3) already documented these, so use the description from there.
 1.61 21-Aug-2006  christos from zafer: you can have either -k or -h but not both since it does not make
sense.
 1.60 08-Apr-2006  wiz branches: 1.60.2;
Fix date (no leading zero), fix xref: sticky(7) not (8).
 1.59 08-Apr-2006  jschauma Address PR 30374 as suggested in there:

"Modify the man page to match the behaviour of "ls -q",

(done by yamt@ previously)

...
modify both the behaviour and documentation for "ls -b" and "ls -B" to
make spaces readily apparent. This could be done by adding VIS_WHITE
to the flags passed to strvis(3) in the safe_print() function in
src/bin/ls/util.c."

'ls -b' now yields foo\sbar (whitespace) foo\tbar (tab) foo\rbar (CR).
'ls -B' now yields foo\040bar foo\011bar foo\015bar.
 1.58 08-Apr-2006  yamt s/non-graphic/non-printable/ to match with the code.
 1.57 26-Oct-2005  jschauma Rather than '-h' suppressing '-s' when using '-l', make it display
the blocks used in human readable form, as suggested by Alan Barrett
and Daniel Carosone.
 1.56 23-Oct-2005  jschauma document that -h makes -l suppress -s.
(Ie 'ls -lhs' == 'ls -lh')
 1.55 10-Oct-2005  wiz Bump date for previous.
 1.54 10-Oct-2005  jschauma Document that '-g' overrides -1, -C, -m, and -x, but is _always_
overridden by '-l', even if '-g' was specified last.
 1.53 13-Aug-2005  elad Document -L correctly. PR #30430.
 1.52 17-Jun-2005  hira Sort options (description).
 1.51 11-May-2005  pooka Clarify the description for -s to include a pointer to an explanation
on how BLOCKSIZE affects the parameter.
 1.50 09-Jan-2005  jschauma The '-l' option *always* prints a total, not only if output is to a
terminal.
 1.49 06-Aug-2004  jschauma Clarify meaning of '-c' flag by using similar wording as for '-u' flag.
 1.48 26-Dec-2003  wiz Crossreference -h and -k. Use Aq instead of \*[Lt]\*[Gt].
 1.47 26-Dec-2003  grant implement -h(umanize).

from David P. Reese Jr. in PR bin/23870.
 1.46 22-Sep-2003  wiz Bump date for previous; comma cleanup.
 1.45 22-Sep-2003  jschauma After last weeks addition of the '-b' flag, add '-B' and '-w' flags,
suggested by uwe@, inspired by FreeBSD. The three flags override
each other (and the '-q' flag) and behave as follows:

-B Force printing of non-printable characters in file names as
\xxx, where xxx is the numeric value of the character in octal.

-b As -B, but use C escape codes whenever possible.

-w Force raw printing of non-printable characters. This is the
default when output is not to a terminal.
 1.44 14-Sep-2003  wiz Bump date for previous.
 1.43 14-Sep-2003  jschauma Implement '-b' option, which, following FreeBSD, Linux and (I think) Solaris
prints octal escapes for nongraphic characters.
 1.42 02-Sep-2003  wiz Sort options (AaBb...).
 1.41 28-Aug-2003  wiz From FreeBSD bug report 54294, via Jonathan Gray and then jmc@openbsd:
-l option to ls(1) gives all permissions, not just owner and group.
 1.40 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.39 07-May-2003  grant make -g behave as it should according to SUSv3 (long listing without
owner).

ok'd by kleink@.
 1.38 25-Feb-2003  wiz .Nm does not need a dummy argument ("") before punctuation or
for correct formatting of the SYNOPSIS any longer.
 1.37 10-Feb-2003  grant some mdoc improvements.

ok'd by wiz.
 1.36 09-Nov-2002  enami Default .Ar arugment is sufficient.
 1.35 25-Sep-2002  wiz New policy: New sentences start on a new line.
Patches by Robert Elz <kre at munnari oz au>, with minimal changes by me.
 1.34 24-Jun-2002  kleink Note that -T is to be used with -l; synced with FreeBSD via Michal
Pasternak on current-users.
 1.33 29-Apr-2002  kleink branches: 1.33.2;
Add missing -p to synopsis; from Ryan Younce in PR misc/16555.
 1.32 08-Feb-2002  ross Generate <>& symbolically. I'm avoiding .../dist/... directories for now.
 1.31 03-Feb-2002  fair Add explanation of directory sizes, and a reference to dir(5),
per PR 14291.
 1.30 20-Dec-2001  wiz Use standard headers.
 1.29 20-Dec-2001  wiz Whitespace nits.
 1.28 08-Jul-2001  simonb Xref getbsize(3).
 1.27 04-Sep-2000  kleink For commands and utilities, use EXIT STATUS rather than RETURN VALUES as
appropriate (and documented in mdoc(7)).
 1.26 28-Aug-2000  hubertf Add 'RETURN VALUE' section header.
 1.25 28-May-2000  bjh21 branches: 1.25.2;
Add Ev tag to COLUMNS
 1.24 05-Jan-2000  mjl branches: 1.24.2;
Add description of FIFOs in long display (was commented out).
 1.23 17-Feb-1999  kleink Add support for the XCU5 -p option (-F restricted to directories).
 1.22 12-Feb-1999  kleink Implement 1003.2 -m option (stream output format).
 1.21 03-Jan-1999  lukem update date
 1.20 03-Jan-1999  lukem refer to chflags(1) for more info on file flags.
split up explanation of long format into a list
 1.19 01-Jun-1998  hubertf Document ls -F's output for sockets;
per PR 5520 by Matt Debergalis <deberg@big-veronica.ai.mit.edu>
 1.18 20-Mar-1998  fair correct HISTORY section of ls(1) to note that Ls(1) exited in 5th Edition UNIX from Bell Labs; add that version to At macro in mdoc, all per PR#4790
 1.17 18-Jan-1998  lukem * implement -x; sort columns across the page rather than down the page
* in printcol(), don't bother building an index to the linked list if
only printing one column with printscol()...
* implement -n; prevent [gu]id->name mapping in long listings.
inspired by openbsd, but does *not* imply -l by itself.
* fix sorting with -d (or mismatched types in general). (from openbsd).
* document -g. (from openbsd).
 1.16 20-Oct-1997  enami Fix .Nm usage.
 1.15 09-Sep-1997  jtc Changed order of STANDARDS and HISTORY subheads to conform with
specification in mdoc(7).
 1.14 05-Dec-1995  jtc ENVIRONMENTAL -> ENVIRONMENT
 1.13 26-Sep-1995  jtc Uncomment text that describes that a vertical bar is printed after each
pathname that is a FIFO when the -F option is used.
 1.12 25-Jul-1995  jtc Use "utility" instead of "command". Modern definitions of these terms
are distinct (See POSIX.2 glossary).

A utility is a executable, script or shell builtin; while a command
can be any of those things plus lists, pipelines, compound commands
(if, for, while) and shell function definitions.
 1.11 21-Mar-1995  cgd convert to new RCS id conventions.
 1.10 27-Dec-1994  mycroft Sync with CSRG.
 1.9 23-Sep-1994  mycroft Merge with 4.4-Lite version.
 1.8 05-Mar-1994  chopps added missing -A and -o to options summary list.
 1.7 11-Jan-1994  jtc Fix spelling errors.
 1.6 05-Dec-1993  mycroft Patches from Thomas Eberhardt <thomas@mathematik.uni-Bremen.de> to allow
sorting by size.
 1.5 07-Aug-1993  mycroft New version from uunet, with -k option readded, and 4.4 ffs--specific stuff
in `#ifdef notyet'.
 1.4 01-Aug-1993  mycroft Add RCS indentifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.3 07-Sep-1995  jtc imported from 44lite2
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.24.2.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.25.2.1 28-Aug-2000  hubertf Pull up to netbsd-1-5 branch, OK'd by thorpej:

Log Message:
> Add 'RETURN VALUE' section header.

Files & Revisionis:
> cvs rdiff -r1.19 -r1.20 basesrc/bin/cat/cat.1
> cvs rdiff -r1.12 -r1.13 basesrc/bin/chmod/chmod.1
> cvs rdiff -r1.14 -r1.15 basesrc/bin/cp/cp.1
> cvs rdiff -r1.8 -r1.9 basesrc/bin/dd/dd.1
> cvs rdiff -r1.9 -r1.10 basesrc/bin/echo/echo.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/expr/expr.1
> cvs rdiff -r1.25 -r1.26 basesrc/bin/ls/ls.1
> cvs rdiff -r1.10 -r1.11 basesrc/bin/mkdir/mkdir.1
> cvs rdiff -r1.23 -r1.24 basesrc/bin/mt/mt.1
> cvs rdiff -r1.12 -r1.13 basesrc/bin/mv/mv.1
> cvs rdiff -r1.16 -r1.17 basesrc/bin/pwd/pwd.1
> cvs rdiff -r1.9 -r1.10 basesrc/bin/rm/rm.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/rmdir/rmdir.1
> cvs rdiff -r1.35 -r1.36 basesrc/bin/sh/sh.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/sleep/sleep.1
> cvs rdiff -r1.20 -r1.21 basesrc/bin/stty/stty.1
 1.33.2.1 24-Jun-2002  lukem Pull up revision 1.34 (requested by kleink in ticket #360):
Note that -T is to be used with -l; synced with FreeBSD via Michal
Pasternak on current-users.
 1.60.2.1 27-Aug-2006  riz Pull up following revision(s) (requested by christos in ticket #53):
bin/df/df.c: revision 1.71
bin/mv/mv.c: revision 1.36
bin/ls/ls.1: revision 1.61
bin/ls/ls.c: revision 1.60
-h and -[mgk] are mutually exclusive. Only use the last specified. [from zafer]
Use one variable instead of 3 flags to hold the scale, so that we don't have
to reset each flag.
from zafer: you can have either -k or -h but not both since it does not make
sense.
- detect buffer overflow.
- if we have a trailing slash, don't add another one.
 1.69.10.3 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.69.10.2 25-Feb-2013  tls resync with head
 1.69.10.1 20-Nov-2012  tls Resync to 2012-11-19 00:00:00 UTC
 1.69.4.3 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.69.4.2 16-Jan-2013  yamt sync with (a bit old) head
 1.69.4.1 30-Oct-2012  yamt sync with head
 1.80.6.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.80.6.1 21-Apr-2020  martin Sync with HEAD
 1.81.8.1 02-Aug-2025  perseant Sync with HEAD
 1.79 11-Dec-2024  simonb Use the number of blocks used in the current directory instead of the
total file byte count when printing the initial "total" line with the
-h option combined with -s or -l options.
 1.78 02-Feb-2024  christos branches: 1.78.2;
PR/57892: Roberto Branco: Print full pathname in error messages
 1.77 07-Jul-2020  christos branches: 1.77.6;
Fix skipping of directories that begin with a '.' in -R mode.
It is not enough to avoid displaying the contents of the directory,
we need to set FTS_SKIP to avoid descending into any subdirs too.
Otherwise, if a ".foo" directory has a subdirectory "bar", ls will
descend into bar and display its contents. From Todd Miller
 1.76 06-Feb-2017  rin branches: 1.76.12;
white space
 1.75 30-May-2016  dholland branches: 1.75.2; 1.75.4;
PR 50997 David Binderman: fix format strings
 1.74 02-Apr-2014  wiz Apply diff from Igor Sobrado <isd@orion.ciencias.uniovi.es>:

We have written a diff to our ls(1) to recover the traditional behaviour
of -f implying -a. This change does not only accommodates POSIX.1
but also matches traditional UNIX.

OpenBSD commit message:

CVSROOT: /cvs
Module name: src
Changes by: sobrado@cvs.openbsd.org 2014/03/31 14:54:37

Modified files:
bin/ls : ls.1 ls.c

Log message:
restore the traditional behavior of -f implying -a; apparently Keith Bostic
forgot to restore it when the -f flag was put back on 2nd of September 1989,
after being removed on 16th of August as a consequence of issues getting it
working over NFS, so deviation from traditional UNIX behavior in all BSDs
looks like an historical accident; as a side effect, this change accommodates
behavior of this option to IEEE Std 1003.1-2008 (``POSIX.1'').

joint work with jmc@ (who found the inaccuracy in our implementation),
schwarze@ (who provided a detailed tracking of historical facts) and millert@

ok millert@, schwarze@
 1.73 22-Feb-2014  mlelstv add u option to getopt again.
 1.72 21-Feb-2014  christos Add -X (don't cross mount points when recursing) from tls@
 1.71 20-Feb-2014  christos Add -O (only leaf files) and -P (print full path), from tls@
 1.70 20-Nov-2012  abs PR/47167
ls.1:
- Sort options in `SYNOPSIS', the option list, and texts within
the man page;
- improve wording;
- improve macro usage;
- use more consistency regarding (locations for) information about
which options override which;
- cross-reference `-d' and `-R';
- simplify description of `-k', removing redundant and unneeded
information;
- sort entry type list (but leave `-a' and `-A' as they are, given
their meaning);
- correct / augment description of `BLOCKSIZE' environment variable
in `ENVIRONMENT' section;
- bump date.

ls.c:
- Sort options in `usage';
- augment comment about when to figure out block size.
 1.69 29-Aug-2011  joerg branches: 1.69.2; 1.69.8;
Move usage and mark it static+dead
 1.68 15-Mar-2011  erh PR#7540, add a -M option to ls which causes sizes (and number of blocks) to be
displayed with comma separators (or a locale specific separator).
 1.67 08-Jul-2010  rmind Add #include <sys/param.h> for howmany().
 1.66 14-Feb-2009  lukem fix -Wsign-compare issues
 1.65 25-Sep-2008  lukem branches: 1.65.6;
Change '-n' so that it implies '-l' rather than requires '-l'.
Brings ls(1) closer to POSIX.1 2004.
Per discussion with Thomas Klausner and Igor Sobrado.
 1.64 20-Jul-2008  lukem Remove the \n and tabs from the __COPYRIGHT() strings.
 1.63 14-Dec-2006  he branches: 1.63.12;
Complete the move of string_to_flags() and flags_to_string() from the
bin/ls sources to libutil:
o Bump libutil minor version number
o Fix uses to include <util.h> to pick up the function definitions
o Fix most uses of flags_to_string() to release the now-malloc()ed result
 1.62 14-Dec-2006  christos Pass lint. From Anon Ymous
 1.61 23-Sep-2006  elad PR/34499: mac at S dot culver dot net: ls allocates more memory than needed
Applied patch, thanks!
 1.60 21-Aug-2006  christos from zafer: you can have either -k or -h but not both since it does not make
sense.
 1.59 22-Mar-2006  christos branches: 1.59.2;
Coverity CID 1755: Add fts_close() in traverse.
 1.58 26-Oct-2005  jschauma Rather than '-h' suppressing '-s' when using '-l', make it display
the blocks used in human readable form, as suggested by Alan Barrett
and Daniel Carosone.
 1.57 14-Oct-2005  jschauma Honor the COLUMNS variable even when producing output not to a terminal,
as long as a specific format (that could use COLUMNS) was requested.
This makes

env COLUMNS=50 ls -C > file

not surprise the user who clearly wanted some specific output in the file.
 1.56 17-Jun-2005  hira - Add -h to usage.
- Use set/getprogname(3).
 1.55 26-Dec-2003  simonb Fix bug with previous change- always initialise d.s_block. Now "ls -1sh"
works.
XXX: ls -sh now always uses 4 chars for the size column width even if it
needs less than that.
 1.54 26-Dec-2003  simonb KNF - wrap long lines.
 1.53 26-Dec-2003  grant implement -h(umanize).

from David P. Reese Jr. in PR bin/23870.
 1.52 22-Sep-2003  jschauma After last weeks addition of the '-b' flag, add '-B' and '-w' flags,
suggested by uwe@, inspired by FreeBSD. The three flags override
each other (and the '-q' flag) and behave as follows:

-B Force printing of non-printable characters in file names as
\xxx, where xxx is the numeric value of the character in octal.

-b As -B, but use C escape codes whenever possible.

-w Force raw printing of non-printable characters. This is the
default when output is not to a terminal.
 1.51 14-Sep-2003  jschauma Implement '-b' option, which, following FreeBSD, Linux and (I think) Solaris
prints octal escapes for nongraphic characters.
 1.50 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.49 30-May-2003  simonb Pass a NULL to getbsize() for any arguments that aren't needed, and
remove the otherwise unused variables.
 1.48 11-May-2003  kleink If both -g and -l options are present, let -l take precedence over -g,
as requested by several users.

POSIX-2001 is unclear on conflicting option letters as far as the -g
option is concerned; a clarification request will be made.

Addresses PR bin/21519 by Geoff Wing.
 1.47 07-May-2003  simonb Add a FALLTHROUGH to keep lint happy.
 1.46 07-May-2003  grant make -g behave as it should according to SUSv3 (long listing without
owner).

ok'd by kleink@.
 1.45 27-Sep-2002  simonb Need to initialise the blocksize variable if the -i option is used
too. The code in display() could possibly be a bit smarter about this
requirement...

Fixes the problem in PR bin/18321 from David Laight and PR bin/18436
from FUKAUMI Naoki.
 1.44 14-Sep-2002  sommerfeld Use the output blocksize when computing how much room to leave for the
-s option, to suppress unnecessary leading spaces.
 1.43 29-Jul-2000  lukem convert to new KNF
 1.42 17-Jun-2000  assar Do not modify the file name when not printing non-printables, also do
it consistently with the file name and possible symbolic link target.

fixes bin/10385 and bin/10384
 1.41 06-Mar-2000  enami branches: 1.41.2;
- Fix a typo that when group_from_gid is failed (though it merely fails
on NetBSD), numeric gid string is used as `user'.
- Don't mix use %u and %d for numeric uid/gid.
- Share temporary buffer.
 1.40 09-Nov-1999  drochner Since our gcc doesn't warn about NULL format strings anymore, we can
fix the incorrect err(1, "%s", "") et al.
Closes PR bin/7592 by cgd.
 1.39 02-Nov-1999  tron Call setlocale(3) to use localization in "ls" as suggested by
Jens A Nilsson in PR bin/8730.
 1.38 17-May-1999  lukem branches: 1.38.2;
Rename ls.c::main() -> ls.c::ls_main(), and add a wrapper in main.c::main().
This allows other programs to link in against ls (e.g, ftpd), without having
to have hacks to deal with multiple main()s.

Yes, I know this is ugly. The clean alternative (move lots of this
into libutil, yada yada yada) isn't fun, and the effort probably isn't
justified...
 1.37 04-May-1999  simonb If an error is encountered, exit with non-zero status.

Based on FreeBSD fix (in 1996!), and fixes PR bin/7514 from
Jorgen Pehrson.
 1.36 17-Feb-1999  kleink branches: 1.36.2;
Add support for the XCU5 -p option (-F restricted to directories).
 1.35 12-Feb-1999  kleink Implement 1003.2 -m option (stream output format).
 1.34 04-Nov-1998  christos %q -> %ll
 1.33 04-Nov-1998  christos include termios.h for struct winsize
 1.32 13-Oct-1998  wsanchez Check for NULL value returned by user_from_uid and group_from_gid and DTRT,
rather than crash.
 1.31 19-Aug-1998  thorpej Add some braces to make egcs happy.
 1.30 28-Jul-1998  mycroft Delint.
 1.29 28-Jul-1998  mycroft Delint.
 1.28 27-Jul-1998  mycroft Slight code reduction.
 1.27 27-Jul-1998  mycroft __AUDIT__ cleanup.
 1.26 16-May-1998  lukem * blocks can be 64bit (not u_long), so calculate and display accordingly
* cleanup some other typedamage (int is as good as u_long)
* remove unused variables
 1.25 03-Feb-1998  mycroft Deal with GCC warning.
 1.24 18-Jan-1998  lukem * implement -x; sort columns across the page rather than down the page
* in printcol(), don't bother building an index to the linked list if
only printing one column with printscol()...
* implement -n; prevent [gu]id->name mapping in long listings.
inspired by openbsd, but does *not* imply -l by itself.
* fix sorting with -d (or mismatched types in general). (from openbsd).
* document -g. (from openbsd).
 1.23 17-Jan-1998  mycroft Automatically resize the major and minor number fields, and DTRT if the size
field is not the same size.
 1.22 30-Jul-1997  thorpej branches: 1.22.2;
Add some casts to printf args to keep the compiler happy on the Alpha.
 1.21 20-Jul-1997  christos include <pwd.h> and <grp.h> to bring prototypes into scope.
 1.20 20-Jul-1997  christos remove user_from_uid and group_from_gid prototypes.
 1.19 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.18 09-Jul-1996  mycroft Sort FTS_NS entries by name, just so the order is well-defined.
 1.17 08-Jul-1996  mycroft Partition FTS_NS entries from others, to avoid confusing qsort().
Don't bother to order the FTS_NS entries amongst themselves.
 1.16 14-Feb-1996  jtc branches: 1.16.4;
Changed so that COLUMNS environment variable will override the value
obtained via the TIOCGWINSIZ ioctl. This is required by POSIX.2, see
section 4.39.5.3.
 1.15 07-Sep-1995  jtc Sync with 4.4lite2
 1.14 21-Mar-1995  cgd convert to new RCS id conventions.
 1.13 27-Dec-1994  mycroft Sync with CSRG.
 1.12 23-Sep-1994  mycroft Merge with 4.4-Lite version.
 1.11 08-Apr-1994  jtc Changed traverse() and display() so that errors are reported when
they are first detected in display(), rather than waiting until later
in traverse().

The old code allowed "Can not read directory" error messages to be
left unreported unless the -R (recurse) option was used.
 1.10 04-Apr-1994  chopps "yet" on quad_t/off_t things. (not noticed on i386 becuase of endian I guess.)
 1.9 25-Jan-1994  cgd (1) fixup getbsize() calls
(2) convert to use err(), warn(), and warnx(), as appropriate.
 1.8 05-Dec-1993  mycroft Patches from Thomas Eberhardt <thomas@mathematik.uni-Bremen.de> to allow
sorting by size.
 1.7 07-Aug-1993  mycroft New version from uunet, with -k option readded, and 4.4 ffs--specific stuff
in `#ifdef notyet'.
 1.6 01-Aug-1993  mycroft Add RCS identifiers.
 1.5 26-May-1993  deraadt previous patch to ls.c wasn't quite correct. This patch from same author.
 1.4 24-May-1993  deraadt first environment variable was trounced in the case of
"ls -l". Patch from <bob@obiwan.uucp> Bob Willcox
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.3 07-Sep-1995  jtc imported from 44lite2
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.16.4.1 10-Dec-1996  mycroft From trunk:
Various changes to make the sorting deterministic (and correct).
 1.22.2.1 09-Feb-1998  mellon Pull up 1.23 (mycroft)
 1.36.2.1 09-Sep-2000  he Pull up revision 1.37 (requested by hubertf):
Exit with non-zero status if an error was encountered. Fixes
PR#7514.
 1.38.2.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.41.2.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.59.2.1 27-Aug-2006  riz Pull up following revision(s) (requested by christos in ticket #53):
bin/df/df.c: revision 1.71
bin/mv/mv.c: revision 1.36
bin/ls/ls.1: revision 1.61
bin/ls/ls.c: revision 1.60
-h and -[mgk] are mutually exclusive. Only use the last specified. [from zafer]
Use one variable instead of 3 flags to hold the scale, so that we don't have
to reset each flag.
from zafer: you can have either -k or -h but not both since it does not make
sense.
- detect buffer overflow.
- if we have a trailing slash, don't add another one.
 1.63.12.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.65.6.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.69.8.2 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.69.8.1 25-Feb-2013  tls resync with head
 1.69.2.2 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.69.2.1 16-Jan-2013  yamt sync with (a bit old) head
 1.75.4.1 21-Apr-2017  bouyer Sync with HEAD
 1.75.2.1 20-Mar-2017  pgoyette Sync with HEAD
 1.76.12.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.76.12.1 21-Apr-2020  martin Sync with HEAD
 1.77.6.1 20-Jul-2024  martin Pull up following revision(s) (requested by rin in ticket #743):

bin/ls/ls.c: revision 1.78

PR/57892: Roberto Branco: Print full pathname in error messages
 1.78.2.1 02-Aug-2025  perseant Sync with HEAD
 1.20 11-Dec-2024  simonb Use the number of blocks used in the current directory instead of the
total file byte count when printing the initial "total" line with the
-h option combined with -s or -l options.
 1.19 20-Feb-2014  christos branches: 1.19.26; 1.19.38;
Add -O (only leaf files) and -P (print full path), from tls@
 1.18 15-Mar-2011  erh branches: 1.18.4; 1.18.10;
PR#7540, add a -M option to ls which causes sizes (and number of blocks) to be
displayed with comma separators (or a locale specific separator).
 1.17 14-Feb-2009  lukem fix -Wsign-compare issues
 1.16 26-Dec-2003  grant branches: 1.16.40;
implement -h(umanize).

from David P. Reese Jr. in PR bin/23870.
 1.15 22-Sep-2003  jschauma After last weeks addition of the '-b' flag, add '-B' and '-w' flags,
suggested by uwe@, inspired by FreeBSD. The three flags override
each other (and the '-q' flag) and behave as follows:

-B Force printing of non-printable characters in file names as
\xxx, where xxx is the numeric value of the character in octal.

-b As -B, but use C escape codes whenever possible.

-w Force raw printing of non-printable characters. This is the
default when output is not to a terminal.
 1.14 14-Sep-2003  jschauma Implement '-b' option, which, following FreeBSD, Linux and (I think) Solaris
prints octal escapes for nongraphic characters.
 1.13 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.12 07-May-2003  grant make -g behave as it should according to SUSv3 (long listing without
owner).

ok'd by kleink@.
 1.11 17-Jun-2000  assar Do not modify the file name when not printing non-printables, also do
it consistently with the file name and possible symbolic link target.

fixes bin/10385 and bin/10384
 1.10 17-Feb-1999  kleink branches: 1.10.8;
Add support for the XCU5 -p option (-F restricted to directories).
 1.9 16-May-1998  lukem * blocks can be 64bit (not u_long), so calculate and display accordingly
* cleanup some other typedamage (int is as good as u_long)
* remove unused variables
 1.8 17-Jan-1998  mycroft Automatically resize the major and minor number fields, and DTRT if the size
field is not the same size.
 1.7 21-Mar-1995  cgd branches: 1.7.4;
convert to new RCS id conventions.
 1.6 23-Sep-1994  mycroft Merge with 4.4-Lite version.
 1.5 07-Aug-1993  mycroft New version from uunet, with -k option readded, and 4.4 ffs--specific stuff
in `#ifdef notyet'.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.7.4.3 09-Feb-1998  mellon Pull up 1.8 (mycroft)
 1.7.4.2 09-Feb-1998  mellon Undo accidental commit on branch
 1.7.4.1 17-Jan-1998  mycroft Automatically resize the major and minor number fields, and DTRT if the size
field is not the same size.
 1.10.8.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.16.40.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.18.10.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.18.4.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.19.38.1 02-Aug-2025  perseant Sync with HEAD
 1.19.26.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.19.26.1 21-Apr-2020  martin Sync with HEAD
 1.5 05-Sep-2016  sevan branches: 1.5.14;
Drop main() prototype.
 1.4 28-Apr-2008  martin Remove clause 3 and 4 from TNF licenses
 1.3 14-Dec-2006  christos branches: 1.3.10;
Pass lint. From Anon Ymous
 1.2 29-Jul-2000  lukem convert to new KNF
 1.1 17-May-1999  lukem Rename ls.c::main() -> ls.c::ls_main(), and add a wrapper in main.c::main().
This allows other programs to link in against ls (e.g, ftpd), without having
to have hacks to deal with multiple main()s.

Yes, I know this is ugly. The clean alternative (move lots of this
into libutil, yada yada yada) isn't fun, and the effort probably isn't
justified...
 1.3.10.1 18-May-2008  yamt sync with head.
 1.5.14.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.5.14.1 21-Apr-2020  martin Sync with HEAD
 1.59 11-Dec-2024  simonb Use the number of blocks used in the current directory instead of the
total file byte count when printing the initial "total" line with the
-h option combined with -s or -l options.
 1.58 06-Nov-2024  jschauma In multi-column output, print the 'total' line even if we ended up
using only a single column (e.g., COLUMNS is small or the filenames
are long).
 1.57 17-May-2020  christos branches: 1.57.8;
No ACL support for install media (SMALLPROG)
 1.56 16-May-2020  christos Add ACL support for FFS. From FreeBSD.
 1.55 10-May-2014  martin branches: 1.55.24;
PR bin/48798: fix format for ino_t.
Slightly modifed variant of the patch provided by Thomas Schmitt.
 1.54 22-Feb-2014  mlelstv branches: 1.54.2;
fix -w output
 1.53 20-Feb-2014  christos Add -O (only leaf files) and -P (print full path), from tls@
 1.52 02-May-2013  zafer remove whitespace from end of file.
introduced by erh in 1.49
 1.51 29-Jun-2012  yamt branches: 1.51.2;
handle realloc failure
 1.50 15-Mar-2011  christos branches: 1.50.4;
- use printf(3) "'" format to do thousands grouping instead of custom version.
- fix sign format inconsistencies.
 1.49 15-Mar-2011  erh PR#7540, add a -M option to ls which causes sizes (and number of blocks) to be
displayed with comma separators (or a locale specific separator).
 1.48 18-Aug-2010  enami Fix broken indentation. No functional change.
 1.47 02-Apr-2010  christos make it obvious to grep that we are checking ctime.
 1.46 01-Apr-2010  christos handle ctime returning NULL
 1.45 14-Feb-2009  lukem fix -Wsign-compare issues
 1.44 28-Dec-2008  christos branches: 1.44.2;
prepare for 64 bit dev_t
 1.43 02-Nov-2008  ahoka Implement a printtotal function to avoid code repetition.
 1.42 14-Dec-2006  christos Pass lint. From Anon Ymous
 1.41 31-Oct-2005  jschauma Somehow missed this in the last commit, where I only changed the commentary
to reflect the actual change (plus minor nits). So here's the actual change:

Rather than '-h' suppressing '-s' when using '-l', make it display
the blocks used in human readable form, as suggested by Alan Barrett
and Daniel Carosone.

(Missing bits spotted, as usual, by wiz@.)
 1.40 17-Nov-2004  mycroft Print some future timestamps with HH:MM rather than YYYY, so that we get
something reasonable if we're using a file server whose time is slightly
ahead.
 1.39 09-Aug-2004  jschauma If a files date is any time in the future (not six months or more),
use "date "+%b %e %Y"" format. This is what SUSv3 says.
Ok jdolecek, dsl
 1.38 26-Dec-2003  grant implement -h(umanize).

from David P. Reese Jr. in PR bin/23870.
 1.37 22-Sep-2003  jschauma After last weeks addition of the '-b' flag, add '-B' and '-w' flags,
suggested by uwe@, inspired by FreeBSD. The three flags override
each other (and the '-q' flag) and behave as follows:

-B Force printing of non-printable characters in file names as
\xxx, where xxx is the numeric value of the character in octal.

-b As -B, but use C escape codes whenever possible.

-w Force raw printing of non-printable characters. This is the
default when output is not to a terminal.
 1.36 14-Sep-2003  jschauma Implement '-b' option, which, following FreeBSD, Linux and (I think) Solaris
prints octal escapes for nongraphic characters.
 1.35 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.34 07-May-2003  grant make -g behave as it should according to SUSv3 (long listing without
owner).

ok'd by kleink@.
 1.33 09-Nov-2002  enami Cosmetic changes.
 1.32 01-Aug-2002  christos don't include utmp.h
 1.31 04-Feb-2001  christos fix nested extern.
 1.30 29-Jul-2000  lukem convert to new KNF
 1.29 22-Jun-2000  assar make sure to print file names consistenly in column mode
 1.28 17-Jun-2000  assar branches: 1.28.2;
Do not modify the file name when not printing non-printables, also do
it consistently with the file name and possible symbolic link target.

fixes bin/10385 and bin/10384
 1.27 09-Nov-1999  drochner branches: 1.27.2;
Since our gcc doesn't warn about NULL format strings anymore, we can
fix the incorrect err(1, "%s", "") et al.
Closes PR bin/7592 by cgd.
 1.26 17-Feb-1999  kleink branches: 1.26.6;
Add support for the XCU5 -p option (-F restricted to directories).
 1.25 12-Feb-1999  kleink Implement 1003.2 -m option (stream output format).
 1.24 04-Nov-1998  christos %q -> %ll
 1.23 04-Nov-1998  christos conservative casts for printfs.
 1.22 28-Jul-1998  mycroft Delint.
 1.21 16-May-1998  lukem * blocks can be 64bit (not u_long), so calculate and display accordingly
* cleanup some other typedamage (int is as good as u_long)
* remove unused variables
 1.20 21-Jan-1998  mycroft If a time stamp is 6 months or more in the future, display the year in the
short form. Also, cache the current time.
 1.19 18-Jan-1998  lukem * implement -x; sort columns across the page rather than down the page
* in printcol(), don't bother building an index to the linked list if
only printing one column with printscol()...
* implement -n; prevent [gu]id->name mapping in long listings.
inspired by openbsd, but does *not* imply -l by itself.
* fix sorting with -d (or mismatched types in general). (from openbsd).
* document -g. (from openbsd).
 1.18 17-Jan-1998  mycroft Automatically resize the major and minor number fields, and DTRT if the size
field is not the same size.
 1.17 30-Jul-1997  thorpej branches: 1.17.2;
Add some casts to printf args to keep the compiler happy on the Alpha.
 1.16 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.15 11-Dec-1996  thorpej Do multicolumn output in a way that's a little more likely to always
line up evenly. From D'Arcy J.M. Cain <darcy@druid.com>, PR #2965.
 1.14 07-Sep-1995  jtc Sync with 4.4lite2
 1.13 21-Mar-1995  cgd convert to new RCS id conventions.
 1.12 27-Dec-1994  mycroft Sync with CSRG.
 1.11 23-Sep-1994  mycroft Merge with 4.4-Lite version.
 1.10 04-Apr-1994  chopps "yet" on quad_t/off_t things. (not noticed on i386 becuase of endian I guess.)
 1.9 27-Mar-1994  cgd off_t exp.
 1.8 25-Jan-1994  cgd (1) fixup getbsize() calls
(2) convert to use err(), warn(), and warnx(), as appropriate.
 1.7 13-Jan-1994  jtc The working directory is changed back when fts_children() returns,
so their fts_accpath members no longer have the correct relative
path. We can solve this problem by looking at the parent's path
via p->fts_parent->fts_accpath.
 1.6 14-Oct-1993  jtc POSIX.2 says to print a pipe character after fifo names when using
the -F option.
 1.5 07-Aug-1993  mycroft New version from uunet, with -k option readded, and 4.4 ffs--specific stuff
in `#ifdef notyet'.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.3 07-Sep-1995  jtc imported from 44lite2
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.17.2.2 09-Feb-1998  mellon Pull up 1.18 (mycroft)
 1.17.2.1 29-Jan-1998  mellon Pull up 1.20 (mycroft)
 1.26.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.27.2.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.28.2.1 26-Jun-2000  assar merge in fixes for printing of strange filenames in column mode from trunk
approved by releng-1-5
 1.44.2.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.50.4.2 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.50.4.1 30-Oct-2012  yamt sync with head
 1.51.2.2 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.51.2.1 23-Jun-2013  tls resync from head
 1.54.2.1 10-Aug-2014  tls Rebase.
 1.55.24.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.55.24.1 21-Apr-2020  martin Sync with HEAD
 1.57.8.1 02-Aug-2025  perseant Sync with HEAD
 1.22 14-Dec-2006  he Complete the move of string_to_flags() and flags_to_string() from the
bin/ls sources to libutil:
o Bump libutil minor version number
o Fix uses to include <util.h> to pick up the function definitions
o Fix most uses of flags_to_string() to release the now-malloc()ed result
 1.21 14-Dec-2006  he The "#ifdef UNUSED_BLOCK" in stat_flags.c wasn't so unused after
all -- it was used by the mtree source. So, instead do an "#ifdef
MTREE", and define that one when building mtree.
 1.20 14-Dec-2006  christos Pass lint. From Anon Ymous
 1.19 25-May-2004  hannken Add ffs internal snapshots. Written by Marshall Kirk McKusick for FreeBSD.

- Not enabled by default. Needs kernel option FFS_SNAPSHOT.
- Change parameters of ffs_blkfree.
- Let the copy-on-write functions return an error so spec_strategy
may fail if the copy-on-write fails.
- Change genfs_*lock*() to use vp->v_vnlock instead of &vp->v_lock.
- Add flag B_METAONLY to VOP_BALLOC to return indirect block buffer.
- Add a function ffs_checkfreefile needed for snapshot creation.
- Add special handling of snapshot files:
Snapshots may not be opened for writing and the attributes are read-only.
Use the mtime as the time this snapshot was taken.
Deny mtime updates for snapshot files.
- Add function transferlockers to transfer any waiting processes from
one lock to another.
- Add vfsop VFS_SNAPSHOT to take a snapshot and make it accessible through
a vnode.
- Add snapshot support to ls, fsck_ffs and dump.

Welcome to 2.0F.

Approved by: Jason R. Thorpe <thorpej@netbsd.org>
 1.18 27-Oct-2003  lukem Overhaul how `build.sh tools' are used:

* Rename "config.h" to "nbtool_config.h" and
HAVE_CONFIG_H to HAVE_NBTOOL_CONFIG_H.
This makes in more obvious in the source when we're using
tools/compat/config.h versus "standard autoconf" config.h

* Consistently move the inclusion of nbtool_config.h to before
<sys/cdefs.h> so that the former can provide __RCSID() (et al),
and there's no need to protect those macros any more.

These changes should make it easier to "tool-ify" a program by adding:
#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
to the top of the source files (for the general case).
 1.17 13-Aug-2003  itojun use bounded string op
 1.16 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.15 16-Nov-2002  itojun we can't do {} while (0) for TEST() macro since it has "break" which
goes out of the switch statement. from shin-ichi kaneta.
 1.14 09-Nov-2002  enami Cosmetic changes.
 1.13 31-Jan-2002  tv Roll in fixes to permit cross-compiling from non-NetBSD hosts. This
round has been tested on Solaris/x86 and Linux hosts.

* Add host tools cap_mkdb, ctags, m4, uudecode.
* Protect __RCSID() and __COPYRIGHT() better.
* Reduce the number of places that need to include "config.h", to keep
sources closer to their "vanilla" versions.
* Add more compat #defines and autoconf-checked functions.
 1.12 29-Jan-2002  tv Add hooks to make mtree compilable from src/tools.
 1.11 18-Oct-2001  lukem fix strlcpy length in previous...
 1.10 18-Oct-2001  lukem make this WARNS=2 safe
 1.9 18-Oct-2001  lukem flags_to_strings() changes:

- fix bug where the 'clear' flag was set outside of main loop, so
"dump,uchg,arch" on a file with flags would clear all the bits
(because "dump" sets 'clear'). noted in FreeBSD PR 10071, and fix
obtained from FreeBSD.

- enforce right-most priority on arguments by clearing the appropriate
bit in the opposite set; if a string of "nouchg,uchg" is given,
chflags(8) would clear uchg because it applies the "set" bits before
clearing the "clear" bits. now, "uchg" sets UF_IMMUTABLE in setp
and also resets it in clrp, solving this problem

- constify second arg (the `default' arg)
 1.8 29-Jul-2000  lukem convert to new KNF
 1.7 03-Jan-1999  lukem * parse `nonodump' (synonym for `dump'; both are inverse of `nodump').
from Brian C. Grayson <bgrayson@marvin.ece.utexas.edu> in [bin/3932]
* don't depend on a fallthrough in one case; explicitly return like
all other cases.
* include "stat_flags.h" instead of "ls.h" and "extern.h"; other
programs use this file and don't need the other cruft.
 1.6 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.5 07-Sep-1995  jtc Sync with 4.4lite2
 1.4 21-Mar-1995  cgd convert to new RCS id conventions.
 1.3 27-Dec-1994  mycroft Sync with CSRG.
 1.2 23-Sep-1994  mycroft Merge with 4.4-Lite version.
 1.1 22-Sep-1994  mycroft branches: 1.1.1;
Initial revision
 1.1.1.2 07-Sep-1995  jtc imported from 44lite2
 1.1.1.1 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.5 14-Dec-2006  he Complete the move of string_to_flags() and flags_to_string() from the
bin/ls sources to libutil:
o Bump libutil minor version number
o Fix uses to include <util.h> to pick up the function definitions
o Fix most uses of flags_to_string() to release the now-malloc()ed result
 1.4 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.3 18-Oct-2001  lukem flags_to_strings() changes:

- fix bug where the 'clear' flag was set outside of main loop, so
"dump,uchg,arch" on a file with flags would clear all the bits
(because "dump" sets 'clear'). noted in FreeBSD PR 10071, and fix
obtained from FreeBSD.

- enforce right-most priority on arguments by clearing the appropriate
bit in the opposite set; if a string of "nouchg,uchg" is given,
chflags(8) would clear uchg because it applies the "set" bits before
clearing the "clear" bits. now, "uchg" sets UF_IMMUTABLE in setp
and also resets it in clrp, solving this problem

- constify second arg (the `default' arg)
 1.2 29-Jul-2000  lukem convert to new KNF
 1.1 10-Oct-1998  mrg create a "stat_flags.h" to go with stat_flags.c
 1.34 29-Aug-2011  joerg branches: 1.34.44;
Move usage and mark it static+dead
 1.33 15-Mar-2011  wiz Add -M to usage.
 1.32 06-Jun-2007  soda modify a comment in previous change
 1.31 05-Jun-2007  wiz Apply SODA Nuriyuki's patch for multibyte support.
Based on soda-revised2.patch (in private mail) with a change from
soda on tech-userlevel.

Ok christos@.
 1.30 14-Dec-2006  christos Pass lint. From Anon Ymous
 1.29 08-Apr-2006  jschauma Address PR 30374 as suggested in there:

"Modify the man page to match the behaviour of "ls -q",

(done by yamt@ previously)

...
modify both the behaviour and documentation for "ls -b" and "ls -B" to
make spaces readily apparent. This could be done by adding VIS_WHITE
to the flags passed to strvis(3) in the safe_print() function in
src/bin/ls/util.c."

'ls -b' now yields foo\sbar (whitespace) foo\tbar (tab) foo\rbar (CR).
'ls -B' now yields foo\040bar foo\011bar foo\015bar.
 1.28 17-Jun-2005  hira - Add -h to usage.
- Use set/getprogname(3).
 1.27 22-Sep-2003  jschauma After last weeks addition of the '-b' flag, add '-B' and '-w' flags,
suggested by uwe@, inspired by FreeBSD. The three flags override
each other (and the '-q' flag) and behave as follows:

-B Force printing of non-printable characters in file names as
\xxx, where xxx is the numeric value of the character in octal.

-b As -B, but use C escape codes whenever possible.

-w Force raw printing of non-printable characters. This is the
default when output is not to a terminal.
 1.26 14-Sep-2003  jschauma Implement '-b' option, which, following FreeBSD, Linux and (I think) Solaris
prints octal escapes for nongraphic characters.
 1.25 02-Sep-2003  wiz Sort options (AaBb...).
 1.24 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.23 09-Nov-2002  enami Cosmetic changes.
 1.22 20-Dec-2001  soren Oops, pasto.
 1.21 20-Dec-2001  soren Sync getopt() / man page with actual getopt options.
 1.20 29-Jul-2000  lukem convert to new KNF
 1.19 22-Jun-2000  assar make sure to print file names consistenly in column mode
 1.18 17-Jun-2000  assar branches: 1.18.2;
Do not modify the file name when not printing non-printables, also do
it consistently with the file name and possible symbolic link target.

fixes bin/10385 and bin/10384
 1.17 17-Feb-1999  kleink branches: 1.17.8;
Add support for the XCU5 -p option (-F restricted to directories).
 1.16 12-Feb-1999  kleink Implement 1003.2 -m option (stream output format).
 1.15 28-Jul-1998  mycroft Delint.
 1.14 18-Jan-1998  lukem * implement -x; sort columns across the page rather than down the page
* in printcol(), don't bother building an index to the linked list if
only printing one column with printscol()...
* implement -n; prevent [gu]id->name mapping in long listings.
inspired by openbsd, but does *not* imply -l by itself.
* fix sorting with -d (or mismatched types in general). (from openbsd).
* document -g. (from openbsd).
 1.13 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.12 07-Sep-1995  jtc Sync with 4.4lite2
 1.11 21-Mar-1995  cgd convert to new RCS id conventions.
 1.10 27-Dec-1994  mycroft Sync with CSRG.
 1.9 25-Oct-1994  mycroft Add `-o' option to usage string.
 1.8 23-Sep-1994  mycroft Merge with 4.4-Lite version.
 1.7 25-Jan-1994  cgd (1) fixup getbsize() calls
(2) convert to use err(), warn(), and warnx(), as appropriate.
 1.6 05-Dec-1993  mycroft Patches from Thomas Eberhardt <thomas@mathematik.uni-Bremen.de> to allow
sorting by size.
 1.5 07-Aug-1993  mycroft New version from uunet, with -k option readded, and 4.4 ffs--specific stuff
in `#ifdef notyet'.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.3 07-Sep-1995  jtc imported from 44lite2
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.17.8.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.18.2.1 26-Jun-2000  assar merge in fixes for printing of strange filenames in column mode from trunk
approved by releng-1-5
 1.34.44.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.34.44.1 21-Apr-2020  martin Sync with HEAD
 1.8 20-Jul-1997  christos branches: 1.8.112;
Remove WARNS=1 from all the subdirectory Makefiles, and add it to Makefile.inc
now that all /bin has been cleaned.
 1.7 20-Jul-1997  christos Fix compiler warnings
Add WARNS=1
 1.6 21-Mar-1995  cgd clean up slightly; id tags, alphabetization, prototypes, etc.
 1.5 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.8.112.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.8.112.1 21-Apr-2020  martin Sync with HEAD
 1.21 04-Jul-2017  wiz branches: 1.21.6;
Use Ex. Remove superfluous Pp.
 1.20 03-Jul-2017  wiz Remove workaround for ancient HTML generation code.
 1.19 10-Aug-2016  sevan Grammar
 1.18 10-Aug-2016  sevan Document the version mkdir first appeared.
 1.17 22-Mar-2012  wiz Fix whitespace nits. Suggested by Bug Hunting.
 1.16 07-Aug-2003  agc branches: 1.16.54;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.15 25-Sep-2002  wiz New policy: New sentences start on a new line.
Patches by Robert Elz <kre at munnari oz au>, with minimal changes by me.
 1.14 08-Feb-2002  ross Generate <>& symbolically. I'm avoiding .../dist/... directories for now.
 1.13 07-Oct-2000  abs xref mkdir(2)
 1.12 04-Sep-2000  kleink For commands and utilities, use EXIT STATUS rather than RETURN VALUES as
appropriate (and documented in mdoc(7)).
 1.11 28-Aug-2000  hubertf Add 'RETURN VALUE' section header.
 1.10 20-Oct-1997  enami branches: 1.10.12;
Fix .Nm usage.
 1.9 25-Jul-1995  jtc Use "utility" instead of "command". Modern definitions of these terms
are distinct (See POSIX.2 glossary).

A utility is a executable, script or shell builtin; while a command
can be any of those things plus lists, pipelines, compound commands
(if, for, while) and shell function definitions.
 1.8 25-Jun-1995  mycroft Don't complain if a directory already exists. From Greg Hudson, but
slightly edited.
 1.7 21-Mar-1995  cgd clean up slightly; id tags, alphabetization, prototypes, etc.
 1.6 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.5 01-Aug-1993  mycroft Add RCS indentifiers.
 1.4 20-Jul-1993  jtc Make Posix 1003.2 (D11.2) compliant by adding -m (mode) option.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.10.12.2 08-Oct-2000  abs Pullup basesrc/bin/mkdir/mkdir.1 1.13 - xref mkdir(2)
approved by jhawk
 1.10.12.1 28-Aug-2000  hubertf Pull up to netbsd-1-5 branch, OK'd by thorpej:

Log Message:
> Add 'RETURN VALUE' section header.

Files & Revisionis:
> cvs rdiff -r1.19 -r1.20 basesrc/bin/cat/cat.1
> cvs rdiff -r1.12 -r1.13 basesrc/bin/chmod/chmod.1
> cvs rdiff -r1.14 -r1.15 basesrc/bin/cp/cp.1
> cvs rdiff -r1.8 -r1.9 basesrc/bin/dd/dd.1
> cvs rdiff -r1.9 -r1.10 basesrc/bin/echo/echo.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/expr/expr.1
> cvs rdiff -r1.25 -r1.26 basesrc/bin/ls/ls.1
> cvs rdiff -r1.10 -r1.11 basesrc/bin/mkdir/mkdir.1
> cvs rdiff -r1.23 -r1.24 basesrc/bin/mt/mt.1
> cvs rdiff -r1.12 -r1.13 basesrc/bin/mv/mv.1
> cvs rdiff -r1.16 -r1.17 basesrc/bin/pwd/pwd.1
> cvs rdiff -r1.9 -r1.10 basesrc/bin/rm/rm.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/rmdir/rmdir.1
> cvs rdiff -r1.35 -r1.36 basesrc/bin/sh/sh.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/sleep/sleep.1
> cvs rdiff -r1.20 -r1.21 basesrc/bin/stty/stty.1
 1.16.54.1 17-Apr-2012  yamt sync with head
 1.21.6.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.21.6.1 21-Apr-2020  martin Sync with HEAD
 1.39 13-Sep-2021  kre PR bin/56398

The final component of both a/b/c and a/b/c/ is "c", so that's the one
to which the -m arg applies.
 1.38 29-Aug-2011  joerg branches: 1.38.36; 1.38.44; 1.38.46;
static + __dead
 1.37 20-Jul-2008  lukem Remove the \n and tabs from the __COPYRIGHT() strings.
 1.36 01-Oct-2005  christos branches: 1.36.20;
Fix setmode error handling.
 1.35 04-Jan-2004  jschauma remove unused code left from printescaped() backput as pointed out by Jeff
Ito in PR bin/23969 and PR bin/23970.
 1.34 14-Sep-2003  jschauma Following private discussion with kleink@ and hubertf@ and public discussion
on tech-userlevel@, back out printescaped() functionality.
kleink: ``We sell rope.''
 1.33 11-Sep-2003  christos Comment out unneeded code now that the kernel has been fixed to ignore
trailing slashes.
 1.32 13-Aug-2003  itojun use bounded string op
 1.31 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.30 04-Aug-2003  jschauma As discusses a little while back on tech-userlevel:

If stdout is a tty, use vis(3) to print any filenames to prevent garbage
from being printed if the filename contains control- or other non-printable
characters.

While here, sprinkle some EXIT_FAILURE and NOTREACHED where appropriate.
 1.29 10-Mar-2003  lukem Fix typo in mkpath()'s test before chmod().
Noted in [bin/20652] by Anthony Mallet.
 1.28 05-Mar-2003  lukem Rework -p support again;
- If mkdir() fails on a component (no matter what the error is),
stat() the result. If the stat() fails, print the error mkdir() generated.
Otherwise, if the result is not a directory, explicitly fail with ENOTDIR.
- Move the chmod() of last element to be done only if the mkdir() succeeded
(i.e, it wasn't an existing directory)

Fixes problem introduced in previous commit where "mkdir -p existing"
on a read-only file system would incorrectly return a non zero exit code,
whilst retaining the race-condition fix of the previous commit.

Thanks to simonb@ for sanity checking my fix versus the standards.
 1.27 24-Nov-2002  chs in mkpath(), don't try to stat() the to-be-created directory beforehand,
just try to create it and interpret any error appropriately.
this fixes a problem where multiple "mkdir -p" processes can race,
with the loser returning a spurious error.
 1.26 19-Feb-2002  enami Cosmetic changes.
 1.25 16-Sep-2001  wiz ANSIfy, KNF, use [gs]etprogname. By Petri Koistinen.
 1.24 10-Oct-2000  enami - The type of return value of setmode is a void * and getmode takes it,
rather than mode_t *.
- Free the storage allocated by setmode unless it is obvious that program
exits immediately.
 1.23 26-May-1999  kleink Change previous to use symbolic names for file modes. Also, clean up some
whitespace lossage and use EXIT_{SUCCESS,FAILURE}.
 1.22 25-May-1999  tron Only call chmod(2) if we create a directory with a mode which isn't
accepted by mkdir(2).
 1.21 13-Oct-1998  scw Insert a missing 'else' which caused mknod(2) to still be called
even if mkdir(2) failed. At best this caused two (different)
error messages to be printed. At worst, it changed the mode of
an existing file/directory.
 1.20 08-Oct-1998  wsanchez umask()/mkdir() system calls will not set the setuid bit; the kernel intentionally
discards the high bits, so "mkdir -m 1777 foo" won't set the sticky bit. So call
chmod() on the directory.
 1.19 28-Jul-1998  mycroft Delint.
 1.18 05-Nov-1997  cgd lint
 1.17 20-Jul-1997  christos Fix compiler warnings
Add WARNS=1
 1.16 09-Jan-1997  tls kill use of register
 1.15 12-Aug-1996  explorer Fix mkdir -p to not ignore terminal directory mode specified.
Closes pr bin/2686.
 1.14 25-Jun-1995  mycroft Style and format police.
 1.13 25-Jun-1995  mycroft Don't complain if a directory already exists. From Greg Hudson, but
slightly edited.
 1.12 21-Mar-1995  cgd clean up slightly; id tags, alphabetization, prototypes, etc.
 1.11 01-Mar-1995  jtc Revert to code before it was merged with 4.4lite.
Updated to conform to new NetBSD RCS ID conventions.
 1.10 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.9 29-Apr-1994  jtc Strip trailing slashes, to make "mkdir -p" work as specified by POSIX.2.
 1.8 28-Apr-1994  jtc simplify recursive directory create (mkdir -p) code
 1.7 31-Dec-1993  jtc Localization changes.
Updated to use standard error message routines.
 1.6 13-Oct-1993  jtc #include <unistd.h>, so getopt(), optarg, opterr, optind, and optopt
declarations can be removed from <stdlib.h>.
 1.5 01-Aug-1993  mycroft Add RCS identifiers.
 1.4 20-Jul-1993  jtc Make Posix 1003.2 (D11.2) compliant by adding -m (mode) option.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.36.20.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.38.46.1 23-Sep-2021  martin Pull up following revision(s) (requested by kre in ticket #1347):

bin/mkdir/mkdir.c: revision 1.39

PR bin/56398

The final component of both a/b/c and a/b/c/ is "c", so that's the one
to which the -m arg applies.
 1.38.44.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.38.44.1 21-Apr-2020  martin Sync with HEAD
 1.38.36.1 23-Sep-2021  martin Pull up following revision(s) (requested by kre in ticket #1697):

bin/mkdir/mkdir.c: revision 1.39

PR bin/56398

The final component of both a/b/c and a/b/c/ is "c", so that's the one
to which the -m arg applies.
 1.8 20-Jul-1997  christos branches: 1.8.112;
Remove WARNS=1 from all the subdirectory Makefiles, and add it to Makefile.inc
now that all /bin has been cleaned.
 1.7 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.6 09-Aug-1996  jtc converted to use -lrmt
 1.5 06-Mar-1996  scottr Since we're setuid, let's be paranoid.
 1.4 06-Mar-1996  scottr Forgot this bit, somehow.
 1.3 21-Mar-1995  cgd merge with lite
 1.2 01-Aug-1993  mycroft Add RCS identifiers.
 1.1 25-Mar-1993  cgd branches: 1.1.1;
added mt from net-2 sources, with added rcs header.
note that it's in /bin rather than /usr/bin. wouldn't want to be
screwed trying to restore a backup...
 1.1.1.1 21-Mar-1995  cgd from Lite's src/usr.bin/mt
 1.8.112.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.8.112.1 21-Apr-2020  martin Sync with HEAD
 1.38 01-Sep-2018  wiz Bump date for previous.
 1.37 01-Sep-2018  mlelstv Make tape cache ioctls available to userland command.
 1.36 19-Mar-2012  njoly branches: 1.36.32; 1.36.34;
Adjust for mandoc output.
 1.35 28-Apr-2011  wiz branches: 1.35.4;
Sort sections, punctuation nits.
 1.34 09-Mar-2008  dholland Grammar fix and clarification from PR 37727 by Randolf Richardson.
While here, do some of my own grammar and formatting fixes too.
Bump date.
 1.33 07-Aug-2003  agc branches: 1.33.6; 1.33.16; 1.33.18; 1.33.22; 1.33.26; 1.33.28;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.32 25-Feb-2003  wiz .Nm does not need a dummy argument ("") before punctuation or
for correct formatting of the SYNOPSIS any longer.
 1.31 04-Feb-2003  perry "Utilize" has exactly the same meaning as "use," but it is more
difficult to read and understand. Most manuals of English style
therefore say that you should use "use".
 1.30 25-Sep-2002  wiz New policy: New sentences start on a new line.
Patches by Robert Elz <kre at munnari oz au>, with minimal changes by me.
 1.29 13-Aug-2002  wiz Drop trailing whitespace.
 1.28 13-Aug-2002  sommerfeld Improve some of the description of optional drive features.
 1.27 20-Dec-2001  wiz Whitespace nits.
 1.26 03-Feb-2001  mason Clarify meaning of "mt asf". The man page extant says "Absolute space
to file count", which is incorrect. File numbering, according to
restore(8), starts at one, and thus "mt asf 1" neither moves to the
first file on the tape nor to file number one on the tape.
 1.25 04-Sep-2000  kleink For commands and utilities, use EXIT STATUS rather than RETURN VALUES as
appropriate (and documented in mdoc(7)).
 1.24 28-Aug-2000  hubertf Add 'RETURN VALUE' section header.
 1.23 02-Feb-2000  mjacob branches: 1.23.4;
Fix a buglet in the man page: eom spaces to the end of recorded media-
not the 'end of the media'.
 1.22 30-Sep-1999  soren Add list of common density codes.
The remote function was implemented in 1996.
 1.21 21-Jul-1999  tron branches: 1.21.2;
Add "setblk" as alias for "blocksize" and "setdensity" as alias for
"density". This makes life easier for people which use NetBSD and Linux.
 1.20 22-Mar-1999  garbled More and more .Os cleanups. .Os is defined in the tmac.doc-common file,
so we shouldn't override it with versions in the manpages. Many more to
come.
 1.19 14-Jan-1999  fair mtio.4 no longer missing
 1.18 30-Jul-1998  mjacob rev man page for eew
 1.17 27-Jul-1998  msaitoh Add missing ".El"
 1.16 04-Jul-1998  mjacob adjust document to reality
 1.15 03-Jul-1998  fair PR#5696 + nroff fixes and text clarity
 1.14 20-Oct-1997  enami Fix .Nm usage.
 1.13 04-Oct-1997  hannken Code cleanup. All commands are run from the table.
`compression' command added to man page.
Default tape in man page adjusted. Closes PR #4212.
 1.12 03-Oct-1997  mikel update location of scsi_tape.h and fix set[hl]pos descriptions, from
Dave Huang in PR bin/4202. some more misc. cleanup of my own.
 1.11 29-Sep-1997  mjacob Add support for enabling/disabling compression, as well as reporting and
seeking to logical and hardware block locations.
 1.10 01-Jul-1997  hannken Allow a count of zero for the following commands:
blocksize density eom erase offline rewind rewoffl status retension
This makes it possible to set the default density or to set variable
blocksize. See PR #3256.
 1.9 08-Aug-1996  jtc Add the asf command for absolute tape positioning.

Some cleanup of the remote tape interface, but a lot more is needed.
Ideally, we'd have a "rmt" library which provides a remote tape API
including open, read, write, close, and ioctl. This is useful not
only for mt, but also for programs like tar, cpio, pax, backup and
restore.
 1.8 21-May-1996  mrg add blocksize/density commands. other part of #921 (Olaf Seibert <rhialto@polder.ubc.kun.nl>)
 1.7 05-Mar-1996  scottr Add remote tape capabilities.
 1.6 22-Aug-1995  jtc Change default magtape from /dev/rmt12 to /dev/rst0; PR #1399
 1.5 25-Jul-1995  jtc Use "utility" instead of "command". Modern definitions of these terms
are distinct (See POSIX.2 glossary).

A utility is a executable, script or shell builtin; while a command
can be any of those things plus lists, pipelines, compound commands
(if, for, while) and shell function definitions.
 1.4 21-Mar-1995  cgd merge with lite
 1.3 16-Oct-1994  andrew Added mention of the retension, erase and eom options.
 1.2 01-Aug-1993  mycroft Add RCS indentifiers.
 1.1 25-Mar-1993  cgd branches: 1.1.1;
added mt from net-2 sources, with added rcs header.
note that it's in /bin rather than /usr/bin. wouldn't want to be
screwed trying to restore a backup...
 1.1.1.1 21-Mar-1995  cgd from Lite's src/usr.bin/mt
 1.21.2.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.23.4.1 28-Aug-2000  hubertf Pull up to netbsd-1-5 branch, OK'd by thorpej:

Log Message:
> Add 'RETURN VALUE' section header.

Files & Revisionis:
> cvs rdiff -r1.19 -r1.20 basesrc/bin/cat/cat.1
> cvs rdiff -r1.12 -r1.13 basesrc/bin/chmod/chmod.1
> cvs rdiff -r1.14 -r1.15 basesrc/bin/cp/cp.1
> cvs rdiff -r1.8 -r1.9 basesrc/bin/dd/dd.1
> cvs rdiff -r1.9 -r1.10 basesrc/bin/echo/echo.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/expr/expr.1
> cvs rdiff -r1.25 -r1.26 basesrc/bin/ls/ls.1
> cvs rdiff -r1.10 -r1.11 basesrc/bin/mkdir/mkdir.1
> cvs rdiff -r1.23 -r1.24 basesrc/bin/mt/mt.1
> cvs rdiff -r1.12 -r1.13 basesrc/bin/mv/mv.1
> cvs rdiff -r1.16 -r1.17 basesrc/bin/pwd/pwd.1
> cvs rdiff -r1.9 -r1.10 basesrc/bin/rm/rm.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/rmdir/rmdir.1
> cvs rdiff -r1.35 -r1.36 basesrc/bin/sh/sh.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/sleep/sleep.1
> cvs rdiff -r1.20 -r1.21 basesrc/bin/stty/stty.1
 1.33.28.1 24-Mar-2008  keiichi sync with head.
 1.33.26.1 19-Mar-2008  bouyer Pull up following revision(s) (requested by dholland in ticket #1100):
bin/mt/mt.1: revision 1.34
Grammar fix and clarification from PR 37727 by Randolf Richardson.
While here, do some of my own grammar and formatting fixes too.
Bump date.
 1.33.22.1 23-Mar-2008  matt sync with HEAD
 1.33.18.1 03-Jun-2008  skrll Sync with netbsd-4.
 1.33.16.1 19-Mar-2008  bouyer Pull up following revision(s) (requested by dholland in ticket #1100):
bin/mt/mt.1: revision 1.34
Grammar fix and clarification from PR 37727 by Randolf Richardson.
While here, do some of my own grammar and formatting fixes too.
Bump date.
 1.33.6.1 26-Mar-2008  bouyer Pull up following revision(s) (requested by dholland in ticket #1911):
bin/mt/mt.1: revision 1.34
Grammar fix and clarification from PR 37727 by Randolf Richardson.
While here, do some of my own grammar and formatting fixes too.
Bump date.
 1.35.4.1 17-Apr-2012  yamt sync with head
 1.36.34.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.36.34.2 21-Apr-2020  martin Sync with HEAD
 1.36.34.1 10-Jun-2019  christos Sync with HEAD
 1.36.32.1 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.49 24-Jan-2022  andvar s/begining/beginning/ in comments and messages.
 1.48 01-Sep-2018  mlelstv Make tape cache ioctls available to userland command.
 1.47 29-Aug-2011  joerg branches: 1.47.42; 1.47.44;
static + __dead
 1.46 20-Jul-2008  lukem Remove the \n and tabs from the __COPYRIGHT() strings.
 1.45 27-Jun-2008  dogcow %d doesn't work so well with the now-u_long type. (HI GMCGARRY!)
 1.44 27-Jun-2008  gmcgarry ioctl commands are unsigned long.
 1.43 16-Oct-2006  christos branches: 1.43.18;
use c99 initializers
 1.42 22-Mar-2006  christos Oops, the previous change was not entirely correct since we were checking
the wrong byte.
 1.41 21-Mar-2006  christos Coverity CID 879: Don't increment bits before checking it for NULL.
 1.40 17-Jun-2005  hira - Whitespace nit.
- Wrap long line.
 1.39 03-Feb-2005  christos Allow short command names as long as they are not ambiguous.
 1.38 03-Feb-2005  christos Use the length of the command for comparison not the length
of the command line argument. Allows "compression" to be used
for "compress" etc.
 1.37 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.36 16-Sep-2001  wiz ANSIfy, KNF, use [gs]etprogname. Patch by Petri Koistinen.
 1.35 13-Feb-2001  mason Equally trivial formatting tweak. Leaving the thing alone after this. :)
 1.34 13-Feb-2001  mason trivial formatting tweak
 1.33 13-Feb-2001  christos from Mason Loring Bliss: support "asf 0"
 1.32 07-Sep-1999  simonb Print the current file and block numbers now that at least one tape
driver returns usable values for these.
 1.31 21-Jul-1999  tron Add "setblk" as alias for "blocksize" and "setdensity" as alias for
"density". This makes life easier for people which use NetBSD and Linux.
 1.30 02-Mar-1999  christos Don't assume that defined(sun) means SunOS... Protect with __SVR4 against
Solaris.
 1.29 30-Jul-1998  mjacob add eew as MTEWARN command
 1.28 28-Jul-1998  mycroft Delint.
 1.27 27-Jul-1998  mycroft __AUDIT__ cleanup.
 1.26 04-Jul-1998  mjacob More for kern/1275- and fix a bug in here that's been there forever.
 1.25 21-Oct-1997  thorpej <rmt.h> does funky function remapping stuff. Include it after we've
pulled in the other headers.
 1.24 05-Oct-1997  veego branches: 1.24.2;
Remove a unused variable.
 1.23 04-Oct-1997  hannken Code cleanup. All commands are run from the table.
`compression' command added to man page.
Default tape in man page adjusted. Closes PR #4212.
 1.22 29-Sep-1997  mjacob Add support for enabling/disabling compression, as well as reporting and
seeking to logical and hardware block locations.
 1.21 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.20 01-Jul-1997  hannken Allow a count of zero for the following commands:
blocksize density eom erase offline rewind rewoffl status retension
This makes it possible to set the default density or to set variable
blocksize. See PR #3256.
 1.19 15-Apr-1997  lukem use _PATH_DEFTAPE from <paths.h> instead of DEFTAPE from <sys/mtio.h>
 1.18 09-Jan-1997  tls kill use of register
 1.17 09-Aug-1996  jtc converted to use -lrmt
 1.16 08-Aug-1996  jtc Add the asf command for absolute tape positioning.

Some cleanup of the remote tape interface, but a lot more is needed.
Ideally, we'd have a "rmt" library which provides a remote tape API
including open, read, write, close, and ioctl. This is useful not
only for mt, but also for programs like tar, cpio, pax, backup and
restore.
 1.15 26-May-1996  mrg "SCSI tape" -> "SCSI", from is
 1.14 21-May-1996  mrg branches: 1.14.2;
add blocksize/density commands. other part of #921 (Olaf Seibert <rhialto@polder.ubc.kun.nl>)
 1.13 21-May-1996  mrg add type 7 (scsi tape). part of pr#921 (Olaf Seibert <rhialto@polder.ubc.kun.nl>)
 1.12 28-Mar-1996  scottr - Open device with O_WRONLY only if the command actually writes to the
device, otherwise O_RDONLY. Thanks to Hitoshi Osada for noticing this.
Closes PRs 2202 and 2249.
- Style police.
 1.11 06-Mar-1996  scottr Picked up a little lint in the dryer.
 1.10 06-Mar-1996  scottr Since we're setuid, let's be paranoid.
 1.9 05-Mar-1996  scottr Add remote tape capabilities.
 1.8 28-Sep-1995  tls Sync with 4.4BSD-Lite2
 1.7 27-Aug-1995  pk Sync exit codes with man page (from Scott Burns).
 1.6 01-Jun-1995  ragge Removed some (unworking) #ifdef vax.
 1.5 21-Mar-1995  cgd merge with lite
 1.4 05-Apr-1994  mycroft Add `retension', `erase', `eom', and `nbsf' subcommands.
 1.3 30-Mar-1994  jtc misc cleanup:
use getopt to parse command line arguments.
use err(), errx(), etc. for error messages.
include appropriate header files for function prototypes.
return 0 on successfull exit instead of dropping off the end of main().
 1.2 01-Aug-1993  mycroft Add RCS identifiers.
 1.1 25-Mar-1993  cgd branches: 1.1.1;
added mt from net-2 sources, with added rcs header.
note that it's in /bin rather than /usr/bin. wouldn't want to be
screwed trying to restore a backup...
 1.1.1.2 28-Sep-1995  tls imported from 44lite2
 1.1.1.1 21-Mar-1995  cgd from Lite's src/usr.bin/mt
 1.14.2.1 27-May-1996  mrg grok scsi tapes
 1.24.2.1 21-Oct-1997  thorpej Pull up from trunk:

<rmt.h> does funky function remapping stuff. Include it after we've
pulled in the other headers.
 1.43.18.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.47.44.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.47.44.2 21-Apr-2020  martin Sync with HEAD
 1.47.44.1 10-Jun-2019  christos Sync with HEAD
 1.47.42.1 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.3 09-Aug-1996  jtc converted to use -lrmt
 1.2 08-Aug-1996  jtc Add the asf command for absolute tape positioning.

Some cleanup of the remote tape interface, but a lot more is needed.
Ideally, we'd have a "rmt" library which provides a remote tape API
including open, read, write, close, and ioctl. This is useful not
only for mt, but also for programs like tar, cpio, pax, backup and
restore.
 1.1 05-Mar-1996  scottr Add remote tape capabilities.
 1.4 09-Aug-1996  jtc converted to use -lrmt
 1.3 08-Aug-1996  jtc Add the asf command for absolute tape positioning.

Some cleanup of the remote tape interface, but a lot more is needed.
Ideally, we'd have a "rmt" library which provides a remote tape API
including open, read, write, close, and ioctl. This is useful not
only for mt, but also for programs like tar, cpio, pax, backup and
restore.
 1.2 06-Mar-1996  scottr branches: 1.2.4;
Since we're setuid, let's be paranoid.
 1.1 05-Mar-1996  scottr Add remote tape capabilities.
 1.2.4.1 29-Nov-1996  thorpej Don't attempt to alter TCP_MAXSEG. Since it can only be lowered, the
value of 1024 fails on networks such as ARCnet.

Fixes PR #2937, from Michael van Elst <mlelstv@serpens.swb.de>.
 1.2 09-Aug-1996  jtc converted to use -lrmt
 1.1 05-Mar-1996  scottr Add remote tape capabilities.
 1.9 30-Nov-2002  lukem branches: 1.9.98;
don't need -lutil here
 1.8 20-Jul-1997  christos Remove WARNS=1 from all the subdirectory Makefiles, and add it to Makefile.inc
now that all /bin has been cleaned.
 1.7 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.9.98.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.9.98.1 21-Apr-2020  martin Sync with HEAD
 1.32 24-Jun-2020  riastradh Note that -h is an extension to POSIX. Bump date.
 1.31 24-Jun-2020  riastradh New mv -h option.

`mv -h source target' just issues rename(source, target) without
discriminating on whether target resolves to a directory; this way
you can atomically replace a symlink to a directory.
 1.30 04-Jul-2017  wiz branches: 1.30.6;
Use Ex.
 1.29 03-Jul-2017  wiz Remove workaround for ancient HTML generation code.
 1.28 10-Aug-2016  sevan Forgot to bump date with previous commit
 1.27 10-Aug-2016  sevan Document the version mv first appeared.
 1.26 22-Mar-2012  wiz Fix whitespace nits. Suggested by Bug Hunting.
 1.25 11-Mar-2009  joerg branches: 1.25.6;
Remove redundant tabs
 1.24 07-Aug-2003  agc branches: 1.24.40;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.23 25-Feb-2003  wiz .Nm does not need a dummy argument ("") before punctuation or
for correct formatting of the SYNOPSIS any longer.
 1.22 19-Jan-2003  jdolecek insert a paragraph break between the option list and following text
 1.21 28-Dec-2002  wiz Restore word that got removed when -v was added.
 1.20 28-Dec-2002  wiz Whitespace nits.
 1.19 26-Dec-2002  jrf Added verbose extension and updated manual pages. Changes approved by Klaus Klein.
 1.18 08-Feb-2002  ross Generate <>& symbolically. I'm avoiding .../dist/... directories for now.
 1.17 20-Dec-2001  wiz Drop unnecessary .Pps.
 1.16 20-Dec-2001  wiz Whitespace nits.
 1.15 19-Jun-2001  wiz `existent', not `existant'
 1.14 04-Sep-2000  kleink For commands and utilities, use EXIT STATUS rather than RETURN VALUES as
appropriate (and documented in mdoc(7)).
 1.13 28-Aug-2000  hubertf Add 'RETURN VALUE' section header.
 1.12 02-Aug-1999  sommerfeld branches: 1.12.8;
Fix PR8073:
bogons in mv(1) man page.
don't bother prompting for mv -i if source doesn't exist
 1.11 20-Apr-1998  ross s/cp -pr/cp -PRp/ in the discussion of the cross-device cp(1) operation.
 1.10 20-Oct-1997  enami branches: 1.10.2;
Fix .Nm usage.
 1.9 27-Aug-1997  kleink Suppress stray space character; from maximum entropy in PR bin/4049.
 1.8 21-Mar-1995  cgd convert to new RCS id conventions.
 1.7 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.6 11-Jan-1994  jtc Fix spelling errors.
 1.5 22-Sep-1993  jtc Bring closer to POSIX.2 compliance. The latter of any -i or -f options
is the one that is actually effective.
 1.4 01-Aug-1993  mycroft Add RCS indentifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.10.2.1 08-May-1998  mycroft Pull up 1.11, per request of ross.
 1.12.8.1 28-Aug-2000  hubertf Pull up to netbsd-1-5 branch, OK'd by thorpej:

Log Message:
> Add 'RETURN VALUE' section header.

Files & Revisionis:
> cvs rdiff -r1.19 -r1.20 basesrc/bin/cat/cat.1
> cvs rdiff -r1.12 -r1.13 basesrc/bin/chmod/chmod.1
> cvs rdiff -r1.14 -r1.15 basesrc/bin/cp/cp.1
> cvs rdiff -r1.8 -r1.9 basesrc/bin/dd/dd.1
> cvs rdiff -r1.9 -r1.10 basesrc/bin/echo/echo.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/expr/expr.1
> cvs rdiff -r1.25 -r1.26 basesrc/bin/ls/ls.1
> cvs rdiff -r1.10 -r1.11 basesrc/bin/mkdir/mkdir.1
> cvs rdiff -r1.23 -r1.24 basesrc/bin/mt/mt.1
> cvs rdiff -r1.12 -r1.13 basesrc/bin/mv/mv.1
> cvs rdiff -r1.16 -r1.17 basesrc/bin/pwd/pwd.1
> cvs rdiff -r1.9 -r1.10 basesrc/bin/rm/rm.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/rmdir/rmdir.1
> cvs rdiff -r1.35 -r1.36 basesrc/bin/sh/sh.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/sleep/sleep.1
> cvs rdiff -r1.20 -r1.21 basesrc/bin/stty/stty.1
 1.24.40.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.25.6.1 17-Apr-2012  yamt sync with head
 1.30.6.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.30.6.1 21-Apr-2020  martin Sync with HEAD
 1.46 24-Jun-2020  riastradh New mv -h option.

`mv -h source target' just issues rename(source, target) without
discriminating on whether target resolves to a directory; this way
you can atomically replace a symlink to a directory.
 1.45 28-Feb-2016  mrg branches: 1.45.16;
add SIGINFO for mv(1), largely based upon the support in cp(1).
 1.44 02-Mar-2015  enami Don't truncate at microseconds while preserving timestamps.

One of motivation of this change is to make the behavior of test(1)
-nt/ot with preserved copy (like cp -p) closer to the NetBSD 6.
Of course whether full timestamps are kept or not depends also on
underlying file system.

The ifdef added in mv(1) since existing ifdefs was our local change
to compile it on solaris (though I couldn't test it):
http://mail-index.netbsd.org/tech-userlevel/2014/11/28/msg008831.html
 1.43 29-Aug-2011  joerg branches: 1.43.22; 1.43.24;
static + __dead
 1.42 03-Aug-2011  manu Make cp -p and mv preverve extended attributes, and complain if they cannot.

Also introduce library functions for copying extended attributes from one
file to another:
- extattr_copy_file, extattr_copy_fd, extattr_copy_link, with FreeBSD style,
where a namespace is to be supplied
- cpxattr, fcpxattr, lcpxattr, with Linux style, where all namespaces
accessible to the caller are copied, and the others are silently ignored.
 1.41 20-Jul-2008  lukem Remove the \n and tabs from the __COPYRIGHT() strings.
 1.40 25-May-2008  christos PR/38738: murray armfield: Fix type of blen, and reset it on error path. From
OpenBSD.
 1.39 16-Jan-2008  hubertf branches: 1.39.4; 1.39.6;
Code cleanup:
- Removed case '?' before default in switch
- Don't forget to close opened decriptors when malloc() fails
- Hold vfork() result in variable with pid_t type (instead of int)

Sent ages ago by Slava Semushin <php-coder@altlinux.ru> in private mail.
 1.38 15-Feb-2007  rillig branches: 1.38.4;
Allow files whose name starts with a dash be moved across filesystem
boundaries.

This fix was inspired by the recent -froot zero day exploit for Solaris.
 1.37 23-Sep-2006  elad PR/33396: Christian Biere: Global Warming may be caused by mv(1)
Applied patch, thanks!
 1.36 21-Aug-2006  christos - detect buffer overflow.
- if we have a trailing slash, don't add another one.
 1.35 03-Jun-2005  hubertf branches: 1.35.2;
the case of waitpid() returning an error was already tested, and errno
printed for that case. the tests for the return code don't have errno
set, so don't try to print it.

By Liam J. Foy <liamfoy sepulcrum.org>, posted to tech-userlevel@.
 1.34 04-Jan-2004  jschauma branches: 1.34.6;
remove unused code left from printescaped() backput as pointed out by Jeff
Ito in PR bin/23969 and PR bin/23970.
 1.33 14-Sep-2003  jschauma Following private discussion with kleink@ and hubertf@ and public discussion
on tech-userlevel@, back out printescaped() functionality.
kleink: ``We sell rope.''
 1.32 13-Aug-2003  itojun use bounded string op
 1.31 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.30 04-Aug-2003  jschauma As discusses a little while back on tech-userlevel:

If stdout is a tty, use vis(3) to print any filenames to prevent garbage
from being printed if the filename contains control- or other non-printable
characters.

While here, sprinkle some EXIT_FAILURE and NOTREACHED where appropriate.
 1.29 13-Jul-2003  itojun use bounded string op
 1.28 26-Dec-2002  jrf Added verbose extension and updated manual pages. Changes approved by Klaus Klein.
 1.27 16-Sep-2001  wiz ANSIfy, KNF, use [gs]etprogname. By Petri Koistinen, slightly modified by me.
 1.26 16-Jul-2000  darrenr don't emit a warning message if fchflags() returns EOPNOTSUPP
 1.25 15-Jul-2000  darrenr From FreeBSD:
To make inherit file flags when mv(1) moves file between directories
on different file systems.
 1.24 14-Dec-1999  jdolecek fix typo
 1.23 09-Nov-1999  drochner Since our gcc doesn't warn about NULL format strings anymore, we can
fix the incorrect err(1, "%s", "") et al.
Closes PR bin/7592 by cgd.
 1.22 16-Aug-1999  christos branches: 1.22.4;
make this compile on non bsd-4.4 systems
 1.21 02-Aug-1999  sommerfeld Fix PR8073:
bogons in mv(1) man page.
don't bother prompting for mv -i if source doesn't exist
 1.20 04-Nov-1998  christos use utimes, if we don't have futimes.
 1.19 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.18 28-Jul-1998  mycroft Delint.
 1.17 28-Jul-1998  mycroft Delint.
 1.16 19-Oct-1997  mycroft If we fail to set ownership, turn off SUID and SGID bits (like cp(1)).
Use futimes(2), and actually fill in the microsecond field.
 1.15 19-Oct-1997  mikel use lstat() instead of stat() when determining type of 'from' file;
from John F. Woods in PR bin/4288.
Also fixed a similar check of the 'to' file, so we don't try to
rmdir() symlinks to directories; and fixed typo in a comment.
 1.14 08-Oct-1997  kleink * Initialize native locale on startup.
* When using the form "mv src ... dir", strip off trailing slashes from
`src' arguments to provide 1003.2 semantics.
 1.13 07-Oct-1997  hubertf Be silend if owner can't be set on cross-device-moved.
Fixes PR bin/1223 by John F. Woods <jfw@jfwhome.funhouse.com>.
 1.12 20-Jul-1997  christos include <pwd.h> and <grp.h> to bring prototypes into scope.
 1.11 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.10 09-Jan-1997  tls kill use of register
 1.9 21-Mar-1995  cgd convert to new RCS id conventions.
 1.8 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.7 09-Nov-1993  jtc Check user's response against both 'Y' and 'y' --- this is really supposed
to be a locale specific regular expression. This change hard codes POSIX
locale behavior, and will be replaced by a locale independant equivalent
as soon as locales are fully implemented.
 1.6 22-Sep-1993  jtc Bring even closer to POSIX.2 compliance. Add comments documenting
the steps outlined in the standards. Steps 1-5 look good, there
may be some work outstanding in the final Step 6.
 1.5 22-Sep-1993  jtc Bring closer to POSIX.2 compliance. The latter of any -i or -f options
is the one that is actually effective.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.22.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.34.6.1 30-Jul-2005  tron Pull up revision 1.35 (requested by hubertf in ticket #619):
the case of waitpid() returning an error was already tested, and errno
printed for that case. the tests for the return code don't have errno
set, so don't try to print it.
By Liam J. Foy <liamfoy sepulcrum.org>, posted to tech-userlevel@.
 1.35.2.1 27-Aug-2006  riz Pull up following revision(s) (requested by christos in ticket #53):
bin/df/df.c: revision 1.71
bin/mv/mv.c: revision 1.36
bin/ls/ls.1: revision 1.61
bin/ls/ls.c: revision 1.60
-h and -[mgk] are mutually exclusive. Only use the last specified. [from zafer]
Use one variable instead of 3 flags to hold the scale, so that we don't have
to reset each flag.
from zafer: you can have either -k or -h but not both since it does not make
sense.
- detect buffer overflow.
- if we have a trailing slash, don't add another one.
 1.38.4.1 23-Mar-2008  matt sync with HEAD
 1.39.6.2 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.39.6.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.39.4.1 04-Jun-2008  yamt sync with head
 1.43.24.1 04-Mar-2016  martin Missing file in pullup #1096: Don't truncate at sub-microsecond while
preserving timestamps.
 1.43.22.2 06-Mar-2016  martin Pull up following revision(s) (requested by mrg in ticket #1129):
bin/mv/mv.c: revision 1.45
add SIGINFO for mv(1), largely based upon the support in cp(1).
 1.43.22.1 04-Mar-2016  martin Missing file in pullup #1096: Don't truncate at sub-microsecond while
preserving timestamps.
 1.45.16.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.45.16.1 21-Apr-2020  martin Sync with HEAD
 1.8 19-Aug-2004  christos branches: 1.8.92;
Deal with RESCUEDIR
 1.7 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.8.92.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.8.92.1 21-Apr-2020  martin Sync with HEAD
 1.42 03-Jun-2023  lukem bsd.own.mk: rename GCC_NO_* to CC_WNO_*

Rename compiler-warning-disable variables from
GCC_NO_warning
to
CC_WNO_warning
where warning is the full warning name as used by the compiler.

GCC_NO_IMPLICIT_FALLTHRU is CC_WNO_IMPLICIT_FALLTHROUGH

Using the convention CC_compilerflag, where compilerflag
is based on the full compiler flag name.
 1.41 13-Oct-2019  mrg introduce some common variables for use in GCC warning disables:

GCC_NO_FORMAT_TRUNCATION -Wno-format-truncation (GCC 7/8)
GCC_NO_STRINGOP_TRUNCATION -Wno-stringop-truncation (GCC 8)
GCC_NO_STRINGOP_OVERFLOW -Wno-stringop-overflow (GCC 8)
GCC_NO_CAST_FUNCTION_TYPE -Wno-cast-function-type (GCC 8)

use these to turn off warnings for most GCC-8 complaints. many
of these are false positives, most of the real bugs are already
commited, or are yet to come.


we plan to introduce versions of (some?) of these that use the
"-Wno-error=" form, which still displays the warnings but does
not make it an error, and all of the above will be re-considered
as either being "fix me" (warning still displayed) or "warning
is wrong."
 1.40 15-Aug-2019  uwe Install manual pages for tar and cpio only if ${MKBSDTAR} == "no"
PR bin/54468
 1.39 23-Apr-2010  joerg branches: 1.39.48; 1.39.50;
Add MKBSDTAR, defaulting to no. It switching cpio and tar to the
libarchive based frontends.
 1.38 03-Feb-2009  dbj do not require sys/mtio.h for a tools build of pax
 1.37 24-Feb-2008  joerg branches: 1.37.8; 1.37.10; 1.37.12;
Kill pre-4.4BSD compat code.
 1.36 16-Dec-2006  bouyer branches: 1.36.4; 1.36.8;
Don't depend on lubutil if we're building host tools. Fix tools build on
non-NetBSD hosts.
 1.35 14-Dec-2006  he Complete the move of string_to_flags() and flags_to_string() from the
bin/ls sources to libutil:
o Bump libutil minor version number
o Fix uses to include <util.h> to pick up the function definitions
o Fix most uses of flags_to_string() to release the now-malloc()ed result
 1.34 29-Jun-2005  christos Don't compile in cpio usage, if we are SMALL.
 1.33 26-Jun-2005  christos sprinkle a little const, and now everything compiles with WARNS=3
 1.32 01-May-2005  christos PR/29005: Jesse Off: nbpax requires sys/mtio.h which doesn't exist on Interix
I added an ifdef called HAVE_MTIO_H and fixed the code to use it. It is
always defined now, but you can change this for Interix.
 1.31 11-May-2004  christos Welcome to WARNS=3
 1.30 18-Jul-2003  lukem branches: 1.30.4;
doc2html works here now, so remove NOHTML=
(not that it worked here, because NOHTML= was provided *after* <bsd.own.mk>...)
 1.29 18-May-2003  lukem Now that <bsd.prog.mk> DTRT if HOSTPROG is defined (i.e, it is a no-op),
there's no need to special-case .include-ing it.
 1.28 09-May-2003  lukem #ifdef HOSTPROG then explicitly use {major,minor,makedev}_netbsd
macros from pack_dev.h instead of the native versions.
Fixes [toolchain/20051] by Valeriy E. Ushakov.
 1.27 30-Nov-2002  lukem don't need -lutil any more
 1.26 21-Oct-2002  lukem don't bother with cpio/tar links or compat symlinks for HOSTPROGs
 1.25 20-Oct-2002  thorpej Install compatibility symlinks for /usr/bin/tar (-> /bin/tar)
and /usr/bin/cpio (/bin/cpio). The pathname of tar(1) is hard-coded
into things like binary packages, and thus must be kept around
for backward compatibility.
 1.24 16-Oct-2002  christos Flip the pax switch
 1.23 14-Oct-2002  lukem fix previous: SMALLPROG w/o HOSTPROG still needs <bsd.prog.mk>. (hi christos)
 1.22 14-Oct-2002  christos don't support rmt for SMALLPROG
 1.21 12-Oct-2002  thorpej It doesn't help much to skip linking librmt if HOSTPROG but leave
all the librmt calls in the program. So, make librmt support conditional
on #ifdef SUPPORT_RMT, and define that when NOT building as a HOSTPROG.
 1.20 12-Oct-2002  christos merge OpenBSD changes:
- correct -C processing
- add ability to read filenames and flags from a file
- don't print dangerous escape sequences to the terminal
- use strlcpy/strncpy properly.
- handle tmpfile creation better.
- improve documentation of options.
- handle stdout/stderr list selection correctly.
- kill gzip when we get interrupted.
- simplify gzip setup.
- add more flags to programs.

additional changes:
- librmt processing.
- set POSIXLY_CORRECT in options parsing.
- prevent more string overruns.
- support -T

we don't turn the switch on to replace tar and cpio yet.
 1.19 19-Aug-2002  lukem Use ${NETBSDSRCDIR}/some/path instead of ${.CURDIR}/../../some/path (etc).
 1.18 02-Feb-2002  lukem make smaller if SMALLPROG is defined, not CRUNCHEDPROG; it's feasible
that a crunchgen(3)ed version of pax wants -USMALL ...
 1.17 02-Feb-2002  lukem - don't compile in support for -M (mtree specfile input) or -N dbdir
(alternate directory for id info) if SMALL is defined
- enable -DSMALL and remove unused objects if CRUNCHEDPROG is defined
- minor cleanup in next_file() (preparation for future work)
 1.16 29-Jan-2002  tv Make almost all tools compile and run properly on non-NetBSD hosts. (In
particular, most tools now run correctly on Solaris 7.)
 1.15 25-Jan-2002  martin Do not .include <bsd.prog.mk> if building as a host tool.
 1.14 24-Jan-2002  lukem Add '-N dbdir', to specify that user & group lookups (via pwcache(3)) are to
be done from dbdir/master.passwd and dbdir/group, using simple text file
parsing routines from ../../usr.sbin/mtree/getid.c.
This does not affect the -G and -U options, which always use the
system databases.
 1.13 28-Oct-2001  lukem s/u_longlong_t/unsigned long long/ to shut up the parc port
 1.12 25-Oct-2001  lukem Implement -M flag: During a write or copy operation, treat the list of
files on standard input as an mtree(8) `specfile' specification, and
write or copy only those items in the specfile.

If the file exists in the underlying file system, its permissions and
modification time will be used unless specifically overridden by the
specfile. An error will be raised if the type of entry in the specfile
conflicts with that of an existing file.

Otherwise, it is necessary to specify at least the following parameters
in the specfile: type, mode, gname or gid, and uname or uid, device
(in the case of block or character devices), and link (in the case of
symbolic links). If time isn't provided, the current time will be used.
 1.11 25-Oct-2001  lukem - ansi KNF (just remove all the #ifndef __STDC__ prototype junk,
leaving the ansi stuff)
- use longlong_t instead of quad_t (etc), and rename *uqd*() -> *ull*()
- clean up the NET2_STAT stuff similar to ftpd; provide #defines and
macros which select which cast to use, etc
- clean up the NET2_FTS and NET2_REGEX #define use
 1.10 26-Sep-2000  phil Don't generate html yet. doc2html needs a fix.
 1.9 28-Jul-1998  mycroft Use the uid/gid caching moved into libc. Fixed a memory leak!
 1.8 06-Mar-1998  mrg - implement -C and -l for pax-as-tar, as inspried by GNU tar.
- minor cleanups to the options parsing code.
- add a cpio frontend.

note: a few GNU/svr4 cpio options are not supported yet (#ifdef
notyet), however all x/open ones are.
 1.7 10-Oct-1997  christos CFLAGS->CPPFLAGS
 1.6 20-Jul-1997  christos Remove WARNS=1 from all the subdirectory Makefiles, and add it to Makefile.inc
now that all /bin has been cleaned.
 1.5 20-Jul-1997  christos - Rename local warn so tty_warn that we can include <err.h> since this program
uses err() and errx().
- Fix printf format strings.
- Added WARNS=1; note
- Added missing prototypes.
- Split overloaded trail function into trail() and subtrail().
 1.4 21-Mar-1995  cgd convert to new RCS id conventions.
 1.3 14-Jun-1994  jtc Whoever wrote the tar command line argument parsing code didn't realize
that the order of arguments is different if the command flags are specified
without a `-'. I've integrated getoldopt.c from John Gilmore's pdtar which
handles argument parsing correctly.
 1.2 13-Jun-1994  jtc Add RCS ID's
 1.1 13-Jun-1994  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 13-Jun-1994  jtc From 4.4 lite
 1.30.4.1 23-Jul-2005  snj Apply patch (requested by riz in ticket #5512):
Sync pax with HEAD of 2005-07-22.
 1.36.8.1 24-Mar-2008  keiichi sync with head.
 1.36.4.1 23-Mar-2008  matt sync with HEAD
 1.37.12.3 21-Apr-2010  matt sync to netbsd-5
 1.37.12.2 18-Dec-2009  matt Make this compile on snow leopard.
 1.37.12.1 15-Dec-2009  matt Allow tools to build on OSX 10.6 (Snow Leopard).
 1.37.10.1 03-Oct-2009  snj Pull up following revision(s) (requested by apb in ticket #1020):
bin/pax/Makefile: revision 1.38
bin/pax/ar_io.c: revision 1.49
tools/compat/configure: revision 1.69
tools/compat/configure.ac: revision 1.69
do not require sys/mtio.h for a tools build of pax
 1.37.8.1 03-Oct-2009  snj Pull up following revision(s) (requested by apb in ticket #1020):
bin/pax/Makefile: revision 1.38
bin/pax/ar_io.c: revision 1.49
tools/compat/configure: revision 1.69
tools/compat/configure.ac: revision 1.69
do not require sys/mtio.h for a tools build of pax
 1.39.50.1 13-Nov-2019  martin Pull up following revision(s) (requested by nakayama in ticket #421):

bin/pax/Makefile: revision 1.40

Install manual pages for tar and cpio only if ${MKBSDTAR} == "no"
PR bin/54468
 1.39.48.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.39.48.2 21-Apr-2020  martin Sync with HEAD
 1.39.48.1 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.60 20-Jul-2023  lukem pax: fix -Wformat for host tool
 1.59 04-Feb-2019  mrg - bump buffer sizes to avoid potential truncation issues
 1.58 02-Oct-2017  joerg branches: 1.58.4;
Include time.h for time(2). Sort.
 1.57 31-May-2016  dholland Don't use the length return from snprintf to write out the result
buffer. If snprintf truncated the output, the length returned will be
greater and we'll write trash. Just call strlen instead. (And since
what we're doing is writing progress messages to the user, checking
carefully for truncation isn't really worthwhile either.)

Spotted when attending to PR 50998 from David Binderman; the issue
there (computation of an unused value) popped up because one of the
prints was already calling strlen.
 1.56 09-Mar-2015  sevan From OpenBSD r1.49
Fix comment grammar
 1.55 08-Aug-2014  joerg Fix RCS ID.
 1.54 20-Mar-2012  matt branches: 1.54.2; 1.54.10;
Use C89 function definitions
 1.53 31-Aug-2011  plunky branches: 1.53.2;
NULL does not need a cast
 1.52 31-Aug-2010  enami Backout previous; it was wrong fix.
 1.51 31-Aug-2010  enami Fix build as tools.
 1.50 31-Aug-2010  enami - Raise an error rather than silently creating broken archive
if user don't specify --force-local but opened file is actually
a local file.
- Make cpio to accept -F option as described in manpage.
- Make pax to set forcelocal flag if requested to do so.
- Add missing break statement.
 1.49 03-Feb-2009  dbj do not require sys/mtio.h for a tools build of pax
 1.48 23-Apr-2007  christos branches: 1.48.18; 1.48.20; 1.48.22;
PR/36194: Greg A. Woods: add a '-V' flag to pax for verbose summary without
listing (and other minor fixes)
 1.47 11-Feb-2006  dsl Change all the 'return(x)' to 'return c'.
Makes some other searches I want to do a bit easier.
 1.46 01-May-2005  christos PR/29005: Jesse Off: nbpax requires sys/mtio.h which doesn't exist on Interix
I added an ifdef called HAVE_MTIO_H and fixed the code to use it. It is
always defined now, but you can change this for Interix.
 1.45 24-Apr-2005  christos PR/18759: FUKAUMI Naoki: pax/tar dot-dot handling broken
PR/18840: Frederick Bruckman: Fix for PR/18663 incomplete pax symlink handling

This patch makes ``--insecure'' do something. Now if ``--insecure''
is not set (the default) we do a realpath(3) in all the pathnames
that we are trying to create and if either realpath fails, or the
path is outside our working directory, we print a warning and die.
This maybe too strict and might fail on valid archives that create
symlinks and directories in the wrong order.
 1.44 02-Aug-2004  yamt indent fix for the previous (ar_io.c rev.1.43)
 1.43 18-Jul-2004  yamt for copy mode, show more meaningful infomation on SIGINFO.
 1.42 21-Jun-2004  christos don't keep going if -C failed to chdir(); from OpenBSD
 1.41 20-Jun-2004  jmc Completely rework how tools/compat is done. Purge all uses/references to
_NETBSD_SOURCE as this makes cross building from older/newer versions of
NetBSD harder, not easier (and also makes the resulting tools 'different')

Wrap all required code with the inclusion of nbtool_config.h, attempt to
only use POSIX code in all places (or when reasonable test w. configure and
provide definitions: ala u_int, etc).

Reviewed by lukem. Tested on FreeBSD 4.9, Redhat Linux ES3, NetBSD 1.6.2 x86
NetBSD current (x86 and amd64) and Solaris 9.

Fixes PR's: PR#17762 PR#25944
 1.40 11-May-2004  christos Welcome to WARNS=3
 1.39 27-Oct-2003  lukem branches: 1.39.2;
Overhaul how `build.sh tools' are used:

* Rename "config.h" to "nbtool_config.h" and
HAVE_CONFIG_H to HAVE_NBTOOL_CONFIG_H.
This makes in more obvious in the source when we're using
tools/compat/config.h versus "standard autoconf" config.h

* Consistently move the inclusion of nbtool_config.h to before
<sys/cdefs.h> so that the former can provide __RCSID() (et al),
and there's no need to protect those macros any more.

These changes should make it easier to "tool-ify" a program by adding:
#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
to the top of the source files (for the general case).
 1.38 13-Oct-2003  agc Move Keith Muller's code from a 4-clause to a 3-clause licence by removing
the advertising clause. Diffs provided in PR 22397 by Joel Baker, confirmed
to the board by Keith Muller.
 1.37 18-Sep-2003  grant wait for compression program to finish before exiting.

fix races condition resulting in a zero length archive after pax
exits.

patch from OpenBSD, provided by Joerg Sonnenberger in PR bin/22740.
 1.36 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.35 31-Mar-2003  christos Detect and report end of archive properly ala' gnu tar.
 1.34 31-Mar-2003  christos RP/8227: Peter Seeback: Emulate old gnu tar better.
 1.33 25-Feb-2003  wiz Add support for '-j' to create (and extract, but -z already did that)
bzip2 compressed tar files, for GNU tar compatibility.
Patch from Soren Jacobsen in PR 19467, okayed by christos.
Sync usage with options while here.
 1.32 09-Feb-2003  grant cosmetic error condition improvements.
- sprinkle some fflush() to print filenames correctly.
- print a \n to stdout to fix formatting.
- English, consistently use "Cannot" in error messages.

some whitespace cleanup.
 1.31 02-Feb-2003  wiz Fix various spelling, grammar, and punctuation problems in comments, from Sergey Svishchev in PR 20164.
 1.30 12-Dec-2002  christos fix horrible side effect introduced by changing the append mode into archive.
The append mode already did that, so we always ended up overwriting the archive.
 1.29 10-Dec-2002  christos PR/19339: Martin Weber: Tar fails to append on empty files and exits with
0. Revert previous change for PR/18689. We always want to exit with
an error if we could not determine the archive format. Instead,
treat empty files specially. On list/extract we turn into no/op.
On append, we turn into archive.
 1.28 05-Dec-2002  grant don't print archive summary twice.
 1.27 16-Oct-2002  christos Support gnu long filename extensions by default for tar on create, list, and
extract. We now generate GNU tar archives by default ("ustar ^@" instead of
"ustar^@00"). GNU extensions can be disabled with --strict.

XXX: long symlinks untested.
 1.26 13-Oct-2002  christos If the archive refers to stdin, the archive's name is null. Check for
null when we see if it refers to a remote file. From Eric Gillespie, thanks!
 1.25 12-Oct-2002  thorpej Only include <rmt.h> if SUPPORT_RMT.
 1.24 12-Oct-2002  thorpej It doesn't help much to skip linking librmt if HOSTPROG but leave
all the librmt calls in the program. So, make librmt support conditional
on #ifdef SUPPORT_RMT, and define that when NOT building as a HOSTPROG.
 1.23 12-Oct-2002  christos merge OpenBSD changes:
- correct -C processing
- add ability to read filenames and flags from a file
- don't print dangerous escape sequences to the terminal
- use strlcpy/strncpy properly.
- handle tmpfile creation better.
- improve documentation of options.
- handle stdout/stderr list selection correctly.
- kill gzip when we get interrupted.
- simplify gzip setup.
- add more flags to programs.

additional changes:
- librmt processing.
- set POSIXLY_CORRECT in options parsing.
- prevent more string overruns.
- support -T

we don't turn the switch on to replace tar and cpio yet.
 1.22 31-Jan-2002  tv branches: 1.22.2;
Protect __RCSID and __COPYRIGHT from being invoked if not defined.
 1.21 29-Jan-2002  tv Make almost all tools compile and run properly on non-NetBSD hosts. (In
particular, most tools now run correctly on Solaris 7.)
 1.20 25-Oct-2001  lukem - ansi KNF (just remove all the #ifndef __STDC__ prototype junk,
leaving the ansi stuff)
- use longlong_t instead of quad_t (etc), and rename *uqd*() -> *ull*()
- clean up the NET2_STAT stuff similar to ftpd; provide #defines and
macros which select which cast to use, etc
- clean up the NET2_FTS and NET2_REGEX #define use
 1.19 16-Sep-2001  wiz Spell 'occurred' with two 'r's.
 1.18 30-Mar-2000  thorpej Add a -O option: force one volume. This causes pax to not prompt for
a new volume upon premature end-of-volume (e.g. broken pipe). This is
especially useful in automated environments where error recovery cannot
be performed by a human.
 1.17 17-Feb-2000  itohy Cleanup and fix typos.
Partially from FreeBSD.
 1.16 17-Feb-2000  itohy Use lseek(2) on devices if not tape drives.
This makes listing on raw disk device much faster.
Reviewed and discussed in tech-kern and tech-userlevel lists.
 1.15 17-Feb-2000  itohy Fix SIGINFO botch (PR #8868).
Continue partial write(2) on signals (xwrite()).
Partial read(2) at a few places are also continued (xread()).

Add {read,write}_with_restart() hooks for porting on systems
which don't restart interrupted read()/write() calls.

Reviewed and discussed in tech-kern and tech-userlevel lists.
 1.14 22-Oct-1999  is Implement positional -C argument to tar.
 1.13 03-Mar-1999  christos branches: 1.13.4;
Add SIGINFO support.
 1.12 04-Nov-1998  christos Fix problem compiling when NET2_STAT is defined.
 1.11 04-Nov-1998  christos make printf formats conservative
 1.10 10-Aug-1998  tv Transparently handle old-style GNU tar archives that may have garbage in
the trailing block and do not have an end-of-archive marker. (Does this
handling based on whether the ustar id is "ustar" or "ustar ".)
 1.9 27-Jul-1998  mycroft __AUDIT__ cleanup.
 1.8 06-Mar-1998  mrg - implement -C and -l for pax-as-tar, as inspried by GNU tar.
- minor cleanups to the options parsing code.
- add a cpio frontend.

note: a few GNU/svr4 cpio options are not supported yet (#ifdef
notyet), however all x/open ones are.
 1.7 20-Jul-1997  christos - Rename local warn so tty_warn that we can include <err.h> since this program
uses err() and errx().
- Fix printf format strings.
- Added WARNS=1; note
- Added missing prototypes.
- Split overloaded trail function into trail() and subtrail().
 1.6 11-Jan-1997  tls kill 'register'
 1.5 26-Mar-1996  mrg impliment -z (gzip) in pax and tar, and -Z (compress) in tar.
 1.4 21-Mar-1995  cgd convert to new RCS id conventions.
 1.3 14-Jun-1994  mycroft Fix up RCS ids.
 1.2 13-Jun-1994  jtc Add RCS ID's
 1.1 13-Jun-1994  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 13-Jun-1994  jtc From 4.4 lite
 1.13.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.22.2.2 16-Jun-2004  jmc fix botched pullup from ticket #1021 and make sure this compiles on
non-NetBSD hosts again. Also fixes PR#24481
 1.22.2.1 07-Apr-2004  jmc Pullup rev 1.23-1.39 (requested by rafal in ticket #1021)

Pullup pax to current version on trunk. Includes many fixes.
 1.39.2.1 22-Jun-2004  tron branches: 1.39.2.1.2;
Pull up revision 1.41 (requested by jmc in ticket #527):
Completely rework how tools/compat is done. Purge all uses/references to
_NETBSD_SOURCE as this makes cross building from older/newer versions of
NetBSD harder, not easier (and also makes the resulting tools 'different')
Wrap all required code with the inclusion of nbtool_config.h, attempt to
only use POSIX code in all places (or when reasonable test w. configure and
provide definitions: ala u_int, etc).
Reviewed by lukem. Tested on FreeBSD 4.9, Redhat Linux ES3, NetBSD 1.6.2 x86
NetBSD current (x86 and amd64) and Solaris 9.
Fixes PR's: PR#17762 PR#25944
 1.39.2.1.2.1 23-Jul-2005  snj Apply patch (requested by riz in ticket #5512):
Sync pax with HEAD of 2005-07-22.
 1.48.22.2 18-Dec-2009  matt Make this compile on snow leopard.
 1.48.22.1 15-Dec-2009  matt Allow tools to build on OSX 10.6 (Snow Leopard).
 1.48.20.1 03-Oct-2009  snj Pull up following revision(s) (requested by apb in ticket #1020):
bin/pax/Makefile: revision 1.38
bin/pax/ar_io.c: revision 1.49
tools/compat/configure: revision 1.69
tools/compat/configure.ac: revision 1.69
do not require sys/mtio.h for a tools build of pax
 1.48.18.1 03-Oct-2009  snj Pull up following revision(s) (requested by apb in ticket #1020):
bin/pax/Makefile: revision 1.38
bin/pax/ar_io.c: revision 1.49
tools/compat/configure: revision 1.69
tools/compat/configure.ac: revision 1.69
do not require sys/mtio.h for a tools build of pax
 1.53.2.1 17-Apr-2012  yamt sync with head
 1.54.10.1 10-Aug-2014  tls Rebase.
 1.54.2.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.58.4.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.58.4.2 21-Apr-2020  martin Sync with HEAD
 1.58.4.1 10-Jun-2019  christos Sync with HEAD
 1.59 05-Aug-2024  riastradh pax(1): Nix trailing whitespace.

No functional change intended.
 1.58 28-May-2023  lukem branches: 1.58.2;
pax: don't overwrite destination if -r -w copy fails

Add more error handling to pax -r -w so that any failure
during the copy to the temporary file (including a failed flush)
prevents any existing destination file from being replaced
with the partial (including possibly empty) temporary file.
The partial temporary file is removed. pax still exists non-zero.

Thanks to Michael van Elst (mlelstv@) for the analysis
of the problem in the PR.

Should fix PR misc/33753.
 1.57 05-Dec-2021  msaitoh branches: 1.57.2;
s/exisit/exist/ in comment.
 1.56 31-Aug-2011  plunky branches: 1.56.44; 1.56.46;
NULL does not need a cast
 1.55 14-Feb-2009  lukem fix -Wsign-compare issues
 1.54 04-May-2007  christos branches: 1.54.20;
PR/36275: john at iastate dot edu: tar -s modifies symlink targets
- This is explained in a comment in pat_rep.c inside mod_name(). I did not
want to change the default behavior, so I added another modifier "s" which
when set, the pattern will not modify the symlink destination.
- While here I fixed another bug that was introduced before by the fix in
PR/35257 where the renaming was happening twice since we called rep_name
twice.
- Finally if we are renaming hard of soft-link targets print the renames for
those too.
 1.53 23-Apr-2007  christos PR/36194: Greg A. Woods: add a '-V' flag to pax for verbose summary without
listing (and other minor fixes)
 1.52 08-Mar-2007  rillig EROFS is not a critical error in a "mkdir -p".
 1.51 14-Dec-2006  christos PR/35257: Andreas Gustafsson: tar(1) opens excluded files
Apply the file modification function in the input files so that we can
de-select matched patterns.
 1.50 07-Oct-2006  elad PR/34265: dieter roelants: pax truncates archive when appending fails.

Patch applied, thanks!
 1.49 11-Feb-2006  dsl Ensure we exit with an error if we don't actually do anything.
There are a lot of tty_warn(0,...) and syswarn(0,...) which probably ought
to be tty_warn/syswarn(1,...) to force an error exit. However some are
used in interactive parts (eg opening a continuation archive) where there
is a separate retry loop.
So we just pass a failure code out to main() - how quaint!
This should now cause the NetBSD build to fail when gzip tries to write
to a non-existant directory.
(I suspect there are still many errors that don't get reported correctly.)
 1.48 11-Feb-2006  dsl Change all the 'return(x)' to 'return c'.
Makes some other searches I want to do a bit easier.
 1.47 31-Jan-2006  christos Solaris mkdir on an automounted component returns ENOSYS. Go figure.
Good choices whould have been EROFS, EEXIST, EACCES, EISDIR...
 1.46 06-Dec-2005  christos If we get EACCES from mkdir, try to stat() it, and if it is ok, we keep going.
Fixes problem with IRIX.
 1.45 01-Nov-2005  christos PR/31923: Sergey Svishchev: pax-as-tar ignores -k, overwrites existing files
Fix from Onno van der Linden
 1.44 17-Sep-2005  christos mkdir(2) can return EISDIR for /
 1.43 16-Sep-2005  christos Another missing mkdir() race pointed out by chuq. Factor out the mkdir code.
 1.42 22-May-2005  christos Don't even declare fdochroot() if we are in tool mode.
 1.41 14-May-2005  christos Allow writing symlinks that point outside our tree, but not traversing
them to write other files.
 1.40 06-May-2005  jmc Wrap call to fchroot in CONFIG tests so this doesn't blow up on cross-tools
 1.39 05-May-2005  christos PR/30132: Juan RP: tar --chroot refuses to extract files.
fchroot() changes the effective path, so we need to call updatepath().
 1.38 24-Apr-2005  christos If we cannot resolve a path, that probably means that we have not created
directories for that path yet. So, do the check for each component recursively
and succeed if none of the components fall outside our current working
directory.
 1.37 24-Apr-2005  christos PR/18759: FUKAUMI Naoki: pax/tar dot-dot handling broken
PR/18840: Frederick Bruckman: Fix for PR/18663 incomplete pax symlink handling

This patch makes ``--insecure'' do something. Now if ``--insecure''
is not set (the default) we do a realpath(3) in all the pathnames
that we are trying to create and if either realpath fails, or the
path is outside our working directory, we print a warning and die.
This maybe too strict and might fail on valid archives that create
symlinks and directories in the wrong order.
 1.36 24-Apr-2005  christos For clarity instead of using -1 and -2 use the negative of the archive type.
 1.35 23-Jan-2005  jmc Apply user supplied patterns first before applying actions for -A. This way pax
behavior WRT to patterns lines up with the example in the documentation
and how other implementations do it as well since -A is a non-standard
option/behavior. Fixes items noted in PR#23776
 1.34 22-Oct-2004  jmc For HOSTTOOL builds, just exclude the chroot support as it's not needed and
avoids having to platform check for fchroot(). Fixes PR#27336
 1.33 17-Oct-2004  dsl Add an option --chroot to tar. Causes it to chroot(".") before doing
an extract. With -h this will cause existing absolute symlinks to be treated
as relative to the current directory.
Helps sysinst handle existing symlinks in the target system.
Remove 'L' from the usage (got spilt into 'h' and 'H' many moons ago)
Add 'S' to usage, and put into correct place in options list.
 1.32 10-Oct-2004  christos PR/27213: Greg A. Woods: pax doesn't honour SIGPIPE when listing
But always exit, not just on SIGPIPE.
 1.31 22-Aug-2004  tron Propery handle "cpio" archives where the last hardlink includes the
data of a file. This fixes PR bin/26514.
 1.30 16-Apr-2004  christos remove misplaced semi-colons.
 1.29 27-Oct-2003  lukem branches: 1.29.2;
Overhaul how `build.sh tools' are used:

* Rename "config.h" to "nbtool_config.h" and
HAVE_CONFIG_H to HAVE_NBTOOL_CONFIG_H.
This makes in more obvious in the source when we're using
tools/compat/config.h versus "standard autoconf" config.h

* Consistently move the inclusion of nbtool_config.h to before
<sys/cdefs.h> so that the former can provide __RCSID() (et al),
and there's no need to protect those macros any more.

These changes should make it easier to "tool-ify" a program by adding:
#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
to the top of the source files (for the general case).
 1.28 13-Oct-2003  agc Move Keith Muller's code from a 4-clause to a 3-clause licence by removing
the advertising clause. Diffs provided in PR 22397 by Joel Baker, confirmed
to the board by Keith Muller.
 1.27 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.26 08-Jul-2003  simonb Add support for gnutar's -O "extract to stdout" option.
 1.25 23-Jun-2003  grant consistently use "cannot" instead of "can not".
 1.24 31-Mar-2003  christos RP/8227: Peter Seeback: Emulate old gnu tar better.
 1.23 09-Feb-2003  grant cosmetic error condition improvements.
- sprinkle some fflush() to print filenames correctly.
- print a \n to stdout to fix formatting.
- English, consistently use "Cannot" in error messages.

some whitespace cleanup.
 1.22 02-Feb-2003  wiz Fix various spelling, grammar, and punctuation problems in comments, from Sergey Svishchev in PR 20164.
 1.21 10-Dec-2002  christos PR/19339: Martin Weber: Tar fails to append on empty files and exits with
0. Revert previous change for PR/18689. We always want to exit with
an error if we could not determine the archive format. Instead,
treat empty files specially. On list/extract we turn into no/op.
On append, we turn into archive.
 1.20 18-Oct-2002  itojun exit with 0 if the input is empty of not a tar file. matches GNU tar behavior.
solves PR 18689.
 1.19 17-Oct-2002  christos Add proper longlink support. Previously we handled longname support, and
the longlink support was completely wrong.
 1.18 16-Oct-2002  christos Support gnu long filename extensions by default for tar on create, list, and
extract. We now generate GNU tar archives by default ("ustar ^@" instead of
"ustar^@00"). GNU extensions can be disabled with --strict.

XXX: long symlinks untested.
 1.17 12-Oct-2002  christos merge OpenBSD changes:
- correct -C processing
- add ability to read filenames and flags from a file
- don't print dangerous escape sequences to the terminal
- use strlcpy/strncpy properly.
- handle tmpfile creation better.
- improve documentation of options.
- handle stdout/stderr list selection correctly.
- kill gzip when we get interrupted.
- simplify gzip setup.
- add more flags to programs.

additional changes:
- librmt processing.
- set POSIXLY_CORRECT in options parsing.
- prevent more string overruns.
- support -T

we don't turn the switch on to replace tar and cpio yet.
 1.16 31-Jan-2002  tv branches: 1.16.2;
Protect __RCSID and __COPYRIGHT from being invoked if not defined.
 1.15 25-Oct-2001  lukem - ansi KNF (just remove all the #ifndef __STDC__ prototype junk,
leaving the ansi stuff)
- use longlong_t instead of quad_t (etc), and rename *uqd*() -> *ull*()
- clean up the NET2_STAT stuff similar to ftpd; provide #defines and
macros which select which cast to use, etc
- clean up the NET2_FTS and NET2_REGEX #define use
 1.14 17-Feb-2000  itohy Cleanup and fix typos.
Partially from FreeBSD.
 1.13 22-Oct-1999  mrg support ././@LongLink extraction, as created by GNU tar.
 1.12 24-Aug-1999  tron branches: 1.12.2;
Strip leading slashes from filenames by default, add new "A" option to
disable this behaviour. Patches supplied by Peter Seebach in PR bin/8233.
 1.11 03-Mar-1999  christos Add SIGINFO support.
 1.10 10-Aug-1998  tv Transparently handle old-style GNU tar archives that may have garbage in
the trailing block and do not have an end-of-archive marker. (Does this
handling based on whether the ustar id is "ustar" or "ustar ".)
 1.9 28-Jul-1998  mycroft Use the uid/gid caching moved into libc. Fixed a memory leak!
 1.8 01-Apr-1998  kleink Need <time.h> for time() prototype.
 1.7 20-Jul-1997  christos - Rename local warn so tty_warn that we can include <err.h> since this program
uses err() and errx().
- Fix printf format strings.
- Added WARNS=1; note
- Added missing prototypes.
- Split overloaded trail function into trail() and subtrail().
 1.6 11-Jan-1997  tls kill 'register'
 1.5 21-Mar-1995  cgd convert to new RCS id conventions.
 1.4 23-Sep-1994  mycroft Eliminate uses of some obsolete functions.
 1.3 14-Jun-1994  mycroft Fix up RCS ids.
 1.2 13-Jun-1994  jtc Add RCS ID's
 1.1 13-Jun-1994  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 13-Jun-1994  jtc From 4.4 lite
 1.12.2.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.16.2.3 25-Aug-2004  he Pull up revision 1.30 (requested by cube in ticket #1732):
Remove misplaced semi-colons.
 1.16.2.2 16-Jun-2004  jmc fix botched pullup from ticket #1021 and make sure this compiles on
non-NetBSD hosts again. Also fixes PR#24481
 1.16.2.1 07-Apr-2004  jmc Pullup rev 1.17-1.29 (requested by rafal in ticket #1021)

Pullup pax to current version on trunk. Includes many fixes.
 1.29.2.2 25-Aug-2004  jmc branches: 1.29.2.2.2;
Pullup rev 1.31 (requested by tron in ticket #784)

Propery handle "cpio" archives where the last hardlink includes the
data of a file. PR#26514
 1.29.2.1 21-Apr-2004  jmc Pullup rev 1.30 (requested by christos in ticket #170)

Fixes for multiple pax pr's. PR#25200. PR#25207.
 1.29.2.2.2.1 23-Jul-2005  snj Apply patch (requested by riz in ticket #5512):
Sync pax with HEAD of 2005-07-22.
 1.54.20.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.56.46.1 07-Aug-2024  martin Pull up following revision(s) (requested by riastradh in ticket #1863):

bin/pax/buf_subs.c: revision 1.31
tests/bin/pax/t_pax.sh: revision 1.3
tests/bin/pax/t_pax.sh: revision 1.4
tests/bin/pax/t_pax.sh: revision 1.5
tests/bin/pax/t_pax.sh: revision 1.6
tests/bin/pax/t_pax.sh: revision 1.7
bin/pax/extern.h: revision 1.61
bin/pax/pax.c: revision 1.50
bin/pax/pax.c: revision 1.51
bin/pax/file_subs.c: revision 1.65
bin/pax/ftree.c: revision 1.43
bin/pax/ar_subs.c: revision 1.58

pax: exit 0 if stdin filelist is used and empty

If copying a list of files from stdin, exit zero instead of non-zero
if there are no files supplied.

AFAICT, POSIX doesn't require a non-zero an error in this situation,
since there are no files to not match.

Fix from PR bin/41736 by Lloyd Parkes.

pax: don't overwrite destination if -r -w copy fails

Add more error handling to pax -r -w so that any failure
during the copy to the temporary file (including a failed flush)
prevents any existing destination file from being replaced
with the partial (including possibly empty) temporary file.

The partial temporary file is removed. pax still exists non-zero.
Thanks to Michael van Elst (mlelstv@) for the analysis
of the problem in the PR.
Should fix PR misc/33753.

tests/bin/pax: Add test for pax list in an ungettable cwd.
The list operation has no need to touch the file system, so it should
have no need for open(".") or getcwd() to succeed.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
(The PR was filed about tar(1), which is now bsdtar by default, but
the issue applies to pax(1) too and would continue to apply to tar(1)
if you set MKBSDTAR=no.)
pax(1): Don't require open(".") or getcwd to work for list operation.

PR 44498: tar(1) unnecessarily demands that getcwd() work
tests/bin/pax: Test pax(1) succeeds with empty file list on stdin.

PR bin/41736: pax reports an error when copying zero files
tests/bin/pax: Add some more cwd-related test cases.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
pax(1): Only require getcwd if we're reading without --insecure.

No other paths use the result of getcwd.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
 1.56.44.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.56.44.1 21-Apr-2020  martin Sync with HEAD
 1.57.2.1 07-Aug-2024  martin Pull up following revision(s) (requested by riastradh in ticket #776):

bin/pax/buf_subs.c: revision 1.31
tests/bin/pax/t_pax.sh: revision 1.3
tests/bin/pax/t_pax.sh: revision 1.4
tests/bin/pax/t_pax.sh: revision 1.5
tests/bin/pax/t_pax.sh: revision 1.6
tests/bin/pax/t_pax.sh: revision 1.7
bin/pax/extern.h: revision 1.61
bin/pax/pax.c: revision 1.50
bin/pax/pax.c: revision 1.51
bin/pax/file_subs.c: revision 1.65
bin/pax/ftree.c: revision 1.43
bin/pax/ar_subs.c: revision 1.58

pax: exit 0 if stdin filelist is used and empty

If copying a list of files from stdin, exit zero instead of non-zero
if there are no files supplied.

AFAICT, POSIX doesn't require a non-zero an error in this situation,
since there are no files to not match.

Fix from PR bin/41736 by Lloyd Parkes.

pax: don't overwrite destination if -r -w copy fails

Add more error handling to pax -r -w so that any failure
during the copy to the temporary file (including a failed flush)
prevents any existing destination file from being replaced
with the partial (including possibly empty) temporary file.

The partial temporary file is removed. pax still exists non-zero.
Thanks to Michael van Elst (mlelstv@) for the analysis
of the problem in the PR.
Should fix PR misc/33753.

tests/bin/pax: Add test for pax list in an ungettable cwd.
The list operation has no need to touch the file system, so it should
have no need for open(".") or getcwd() to succeed.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
(The PR was filed about tar(1), which is now bsdtar by default, but
the issue applies to pax(1) too and would continue to apply to tar(1)
if you set MKBSDTAR=no.)
pax(1): Don't require open(".") or getcwd to work for list operation.

PR 44498: tar(1) unnecessarily demands that getcwd() work
tests/bin/pax: Test pax(1) succeeds with empty file list on stdin.

PR bin/41736: pax reports an error when copying zero files
tests/bin/pax: Add some more cwd-related test cases.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
pax(1): Only require getcwd if we're reading without --insecure.

No other paths use the result of getcwd.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
 1.58.2.1 02-Aug-2025  perseant Sync with HEAD
 1.32 08-Sep-2024  rillig fix a/an grammar in obvious cases
 1.31 28-May-2023  lukem branches: 1.31.2;
pax: don't overwrite destination if -r -w copy fails

Add more error handling to pax -r -w so that any failure
during the copy to the temporary file (including a failed flush)
prevents any existing destination file from being replaced
with the partial (including possibly empty) temporary file.
The partial temporary file is removed. pax still exists non-zero.

Thanks to Michael van Elst (mlelstv@) for the analysis
of the problem in the PR.

Should fix PR misc/33753.
 1.30 28-May-2022  andvar branches: 1.30.2;
fix various typos, mainly in comments.
 1.29 19-Mar-2018  msaitoh branches: 1.29.2; 1.29.4;
s/copy copy/copy/ in comment.
 1.28 29-Apr-2007  msaitoh branches: 1.28.76;
fix typos
 1.27 11-Feb-2006  dsl Change all the 'return(x)' to 'return c'.
Makes some other searches I want to do a bit easier.
 1.26 24-Apr-2005  christos PR/20228: Simon Burge: pax has problems reading a particular cpio archive
The problem here is that the archive is too short (< 512 bytes). The
buffer routines, try to read at least 512 bytes, even when we try to determine
what format file we have, which is wrong.
 1.25 22-Sep-2004  christos fd can be -1 or -2 for the gnu long name/link hack. So check for >= 0
instead of -1 if we need to flush.
 1.24 15-Jun-2004  christos do the special symlink treatment with all negative fd's.
 1.23 27-Oct-2003  lukem branches: 1.23.2;
Overhaul how `build.sh tools' are used:

* Rename "config.h" to "nbtool_config.h" and
HAVE_CONFIG_H to HAVE_NBTOOL_CONFIG_H.
This makes in more obvious in the source when we're using
tools/compat/config.h versus "standard autoconf" config.h

* Consistently move the inclusion of nbtool_config.h to before
<sys/cdefs.h> so that the former can provide __RCSID() (et al),
and there's no need to protect those macros any more.

These changes should make it easier to "tool-ify" a program by adding:
#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
to the top of the source files (for the general case).
 1.22 13-Oct-2003  agc Move Keith Muller's code from a 4-clause to a 3-clause licence by removing
the advertising clause. Diffs provided in PR 22397 by Joel Baker, confirmed
to the board by Keith Muller.
 1.21 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.20 21-Feb-2003  lukem When writing (-w) or copying (-rw), and using -M (stdin file list is
mtree(8) specfile), compare the "post write/copy" mtime against the
actual "pre write/copy" mtime instead of the mtime from the specfile.

This prevents warnings such as:
pax: File ./foo was modified during copy to archive
if the file's mtime in the file system is different to what's in the specfile.
(The resultant file will still get the specfile mtime ...)
 1.19 09-Feb-2003  grant cosmetic error condition improvements.
- sprinkle some fflush() to print filenames correctly.
- print a \n to stdout to fix formatting.
- English, consistently use "Cannot" in error messages.

some whitespace cleanup.
 1.18 02-Feb-2003  wiz Fix various spelling, grammar, and punctuation problems in comments, from Sergey Svishchev in PR 20164.
 1.17 31-Jan-2002  tv branches: 1.17.2;
Protect __RCSID and __COPYRIGHT from being invoked if not defined.
 1.16 25-Oct-2001  lukem - ansi KNF (just remove all the #ifndef __STDC__ prototype junk,
leaving the ansi stuff)
- use longlong_t instead of quad_t (etc), and rename *uqd*() -> *ull*()
- clean up the NET2_STAT stuff similar to ftpd; provide #defines and
macros which select which cast to use, etc
- clean up the NET2_FTS and NET2_REGEX #define use
 1.15 07-Oct-2001  wiz Fix typo.
 1.14 16-Sep-2001  wiz Spell 'occurred' with two 'r's.
 1.13 17-Feb-2000  itohy Cleanup and fix typos.
Partially from FreeBSD.
 1.12 17-Feb-2000  itohy Fix SIGINFO botch (PR #8868).
Continue partial write(2) on signals (xwrite()).
Partial read(2) at a few places are also continued (xread()).

Add {read,write}_with_restart() hooks for porting on systems
which don't restart interrupted read()/write() calls.

Reviewed and discussed in tech-kern and tech-userlevel lists.
 1.11 22-Oct-1999  mrg support ././@LongLink extraction, as created by GNU tar.
 1.10 22-Oct-1999  mrg remove some old cruft.
 1.9 03-Feb-1998  mycroft branches: 1.9.4;
Deal with GCC warnings.
 1.8 25-Jul-1997  scottr #if __GNUC__, work around bogus initialization warning that occurs on
at least the m68k-based ports.
 1.7 20-Jul-1997  christos - Rename local warn so tty_warn that we can include <err.h> since this program
uses err() and errx().
- Fix printf format strings.
- Added WARNS=1; note
- Added missing prototypes.
- Split overloaded trail function into trail() and subtrail().
 1.6 11-Jan-1997  tls kill 'register'
 1.5 21-Mar-1995  cgd convert to new RCS id conventions.
 1.4 23-Sep-1994  mycroft Eliminate uses of some obsolete functions.
 1.3 14-Jun-1994  mycroft Fix up RCS ids.
 1.2 13-Jun-1994  jtc Add RCS ID's
 1.1 13-Jun-1994  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 13-Jun-1994  jtc From 4.4 lite
 1.9.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.17.2.3 25-Aug-2004  he Pull up revision 1.24 (requested by cube in ticket #1733):
Do the special symlink treatment with all negative fd's.
 1.17.2.2 16-Jun-2004  jmc fix botched pullup from ticket #1021 and make sure this compiles on
non-NetBSD hosts again. Also fixes PR#24481
 1.17.2.1 07-Apr-2004  jmc Pullup rev 1.18-1.23 (requested by rafal in ticket #1021)

Pullup pax to current version on trunk. Includes many fixes.
 1.23.2.2 12-Nov-2004  jmc branches: 1.23.2.2.2;
Pullup rev 1.25 (requested by christos in ticket #922)

fd can be -1 or -2 for the gnu long name/link hack. So check for >= 0
instead of -1 if we need to flush.
 1.23.2.1 18-Jun-2004  tron Pull up revision 1.24 (requested by dillo in ticket #507):
do the special symlink treatment with all negative fd's.
 1.23.2.2.2.1 23-Jul-2005  snj Apply patch (requested by riz in ticket #5512):
Sync pax with HEAD of 2005-07-22.
 1.28.76.1 22-Mar-2018  pgoyette Synch with HEAD, resolve conflicts
 1.29.4.1 07-Aug-2024  martin Pull up following revision(s) (requested by riastradh in ticket #1863):

bin/pax/buf_subs.c: revision 1.31
tests/bin/pax/t_pax.sh: revision 1.3
tests/bin/pax/t_pax.sh: revision 1.4
tests/bin/pax/t_pax.sh: revision 1.5
tests/bin/pax/t_pax.sh: revision 1.6
tests/bin/pax/t_pax.sh: revision 1.7
bin/pax/extern.h: revision 1.61
bin/pax/pax.c: revision 1.50
bin/pax/pax.c: revision 1.51
bin/pax/file_subs.c: revision 1.65
bin/pax/ftree.c: revision 1.43
bin/pax/ar_subs.c: revision 1.58

pax: exit 0 if stdin filelist is used and empty

If copying a list of files from stdin, exit zero instead of non-zero
if there are no files supplied.

AFAICT, POSIX doesn't require a non-zero an error in this situation,
since there are no files to not match.

Fix from PR bin/41736 by Lloyd Parkes.

pax: don't overwrite destination if -r -w copy fails

Add more error handling to pax -r -w so that any failure
during the copy to the temporary file (including a failed flush)
prevents any existing destination file from being replaced
with the partial (including possibly empty) temporary file.

The partial temporary file is removed. pax still exists non-zero.
Thanks to Michael van Elst (mlelstv@) for the analysis
of the problem in the PR.
Should fix PR misc/33753.

tests/bin/pax: Add test for pax list in an ungettable cwd.
The list operation has no need to touch the file system, so it should
have no need for open(".") or getcwd() to succeed.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
(The PR was filed about tar(1), which is now bsdtar by default, but
the issue applies to pax(1) too and would continue to apply to tar(1)
if you set MKBSDTAR=no.)
pax(1): Don't require open(".") or getcwd to work for list operation.

PR 44498: tar(1) unnecessarily demands that getcwd() work
tests/bin/pax: Test pax(1) succeeds with empty file list on stdin.

PR bin/41736: pax reports an error when copying zero files
tests/bin/pax: Add some more cwd-related test cases.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
pax(1): Only require getcwd if we're reading without --insecure.

No other paths use the result of getcwd.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
 1.29.2.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.29.2.1 21-Apr-2020  martin Sync with HEAD
 1.30.2.1 07-Aug-2024  martin Pull up following revision(s) (requested by riastradh in ticket #776):

bin/pax/buf_subs.c: revision 1.31
tests/bin/pax/t_pax.sh: revision 1.3
tests/bin/pax/t_pax.sh: revision 1.4
tests/bin/pax/t_pax.sh: revision 1.5
tests/bin/pax/t_pax.sh: revision 1.6
tests/bin/pax/t_pax.sh: revision 1.7
bin/pax/extern.h: revision 1.61
bin/pax/pax.c: revision 1.50
bin/pax/pax.c: revision 1.51
bin/pax/file_subs.c: revision 1.65
bin/pax/ftree.c: revision 1.43
bin/pax/ar_subs.c: revision 1.58

pax: exit 0 if stdin filelist is used and empty

If copying a list of files from stdin, exit zero instead of non-zero
if there are no files supplied.

AFAICT, POSIX doesn't require a non-zero an error in this situation,
since there are no files to not match.

Fix from PR bin/41736 by Lloyd Parkes.

pax: don't overwrite destination if -r -w copy fails

Add more error handling to pax -r -w so that any failure
during the copy to the temporary file (including a failed flush)
prevents any existing destination file from being replaced
with the partial (including possibly empty) temporary file.

The partial temporary file is removed. pax still exists non-zero.
Thanks to Michael van Elst (mlelstv@) for the analysis
of the problem in the PR.
Should fix PR misc/33753.

tests/bin/pax: Add test for pax list in an ungettable cwd.
The list operation has no need to touch the file system, so it should
have no need for open(".") or getcwd() to succeed.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
(The PR was filed about tar(1), which is now bsdtar by default, but
the issue applies to pax(1) too and would continue to apply to tar(1)
if you set MKBSDTAR=no.)
pax(1): Don't require open(".") or getcwd to work for list operation.

PR 44498: tar(1) unnecessarily demands that getcwd() work
tests/bin/pax: Test pax(1) succeeds with empty file list on stdin.

PR bin/41736: pax reports an error when copying zero files
tests/bin/pax: Add some more cwd-related test cases.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
pax(1): Only require getcwd if we're reading without --insecure.

No other paths use the result of getcwd.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
 1.31.2.1 02-Aug-2025  perseant Sync with HEAD
 1.10 28-Jul-1998  mycroft Use the uid/gid caching moved into libc. Fixed a memory leak!
 1.9 27-Jul-1998  mycroft Slight code reduction.
 1.8 27-Jul-1998  mycroft __AUDIT__ cleanup.
 1.7 26-Jul-1998  mycroft const poisoning.
 1.6 20-Jul-1997  christos branches: 1.6.2;
- Rename local warn so tty_warn that we can include <err.h> since this program
uses err() and errx().
- Fix printf format strings.
- Added WARNS=1; note
- Added missing prototypes.
- Split overloaded trail function into trail() and subtrail().
 1.5 11-Jan-1997  tls kill 'register'
 1.4 21-Mar-1995  cgd convert to new RCS id conventions.
 1.3 14-Jun-1994  mycroft Fix up RCS ids.
 1.2 13-Jun-1994  jtc Add RCS ID's
 1.1 13-Jun-1994  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 13-Jun-1994  jtc From 4.4 lite
 1.6.2.1 20-Jan-1999  cgd patch to fix a memory leak (due to never actually adding items to the
hash table). The change corresponds with a change in -current, but the
file is now in libc (replacing a different version that was already in
libc). (mycroft)
 1.4 28-Jul-1998  mycroft Use the uid/gid caching moved into libc. Fixed a memory leak!
 1.3 21-Mar-1995  cgd convert to new RCS id conventions.
 1.2 13-Jun-1994  jtc Add RCS ID's
 1.1 13-Jun-1994  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 13-Jun-1994  jtc From 4.4 lite
 1.15 03-Jul-2017  wiz branches: 1.15.6;
Remove workaround for ancient HTML generation code.
 1.14 19-Dec-2015  wiz Use An in AUTHORS section.
 1.13 19-Jun-2011  wiz Use Fl Fl for long options.
 1.12 19-Jun-2011  christos document --xz
 1.11 02-Dec-2007  wiz branches: 1.11.26;
Minimally sort options; use Dq; new sentence, new line.
 1.10 23-Apr-2007  christos branches: 1.10.4;
bump date.
 1.9 11-Sep-2005  wiz Use better section header. From YOMURA Masanori in private mail.
 1.8 15-Sep-2004  hubertf Change copyright to 2-clause BSD-copyright.
Changed with explicit permission of Thorsten Lockert (tholo sigmasoft com)
 1.7 13-Feb-2004  snj branches: 1.7.2; 1.7.4;
s/nonmathing/nonmatching/
 1.6 10-Apr-2003  grant some mdoc fixes.
 1.5 25-Feb-2003  wiz New sentence, new line.
 1.4 16-Oct-2002  christos add commas between options. Suggested by wiz.
 1.3 16-Oct-2002  christos Document the long options.
 1.2 12-Oct-2002  wiz Miscellaneous fixes.
 1.1 12-Oct-2002  christos man pages from OpenBSD.
TODO: add long option documentation.
 1.7.4.2 07-Apr-2004  jmc Pullup rev 0-1.7 (requested by rafal in ticket #1021)

Pullup pax to current version on trunk. Includes many fixes.
 1.7.4.1 13-Feb-2004  jmc file cpio.1 was added on branch netbsd-1-6 on 2004-04-07 06:57:20 +0000
 1.7.2.1 16-Sep-2004  jmc Pullup rev 1.8 (requested by hubertf in ticket #850)

Change copyright to 2-clause BSD-copyright.
 1.10.4.1 09-Jan-2008  matt sync with HEAD
 1.11.26.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.15.6.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.15.6.1 21-Apr-2020  martin Sync with HEAD
 1.22 09-Aug-2012  christos branches: 1.22.34;
PR/46786: Simon Burge: After conversion to 64 bit time_t, tar/pax/cpio
erroneously think that negative time_t's never fit in 32 bits. Rework
conversion code to always use uintmax_t, and detect negative values.
XXX[1]: Perhaps we should do the same (use a signed conversion) for all
fields not just for time_t
XXX[2]: pullup for 6
 1.21 26-Mar-2011  martin branches: 1.21.4;
Fix bogus paranthesis, From Henning Peternse in PR bin/44773.
 1.20 14-Feb-2009  lukem fix -Wsign-compare issues
 1.19 11-Feb-2006  dsl branches: 1.19.28;
Change all the 'return(x)' to 'return c'.
Makes some other searches I want to do a bit easier.
 1.18 04-Feb-2005  matt Don't write hardlinked file contents in sv4cpio mode.
 1.17 27-Oct-2003  lukem branches: 1.17.4;
Overhaul how `build.sh tools' are used:

* Rename "config.h" to "nbtool_config.h" and
HAVE_CONFIG_H to HAVE_NBTOOL_CONFIG_H.
This makes in more obvious in the source when we're using
tools/compat/config.h versus "standard autoconf" config.h

* Consistently move the inclusion of nbtool_config.h to before
<sys/cdefs.h> so that the former can provide __RCSID() (et al),
and there's no need to protect those macros any more.

These changes should make it easier to "tool-ify" a program by adding:
#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
to the top of the source files (for the general case).
 1.16 13-Oct-2003  agc Move Keith Muller's code from a 4-clause to a 3-clause licence by removing
the advertising clause. Diffs provided in PR 22397 by Joel Baker, confirmed
to the board by Keith Muller.
 1.15 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.14 02-Feb-2003  wiz Fix various spelling, grammar, and punctuation problems in comments, from Sergey Svishchev in PR 20164.
 1.13 11-Feb-2002  wiz branches: 1.13.2;
achive is not a good word.
 1.12 31-Jan-2002  tv Protect __RCSID and __COPYRIGHT from being invoked if not defined.
 1.11 25-Oct-2001  lukem - ansi KNF (just remove all the #ifndef __STDC__ prototype junk,
leaving the ansi stuff)
- use longlong_t instead of quad_t (etc), and rename *uqd*() -> *ull*()
- clean up the NET2_STAT stuff similar to ftpd; provide #defines and
macros which select which cast to use, etc
- clean up the NET2_FTS and NET2_REGEX #define use
 1.10 04-Jan-2001  lukem use more standard %ll_ in favour of %q_
 1.9 17-Feb-2000  itohy Cleanup and fix typos.
Partially from FreeBSD.
 1.8 28-Feb-1998  mrg rename variable to "cpio_swp_head"
 1.7 20-Jul-1997  christos - Rename local warn so tty_warn that we can include <err.h> since this program
uses err() and errx().
- Fix printf format strings.
- Added WARNS=1; note
- Added missing prototypes.
- Split overloaded trail function into trail() and subtrail().
 1.6 11-Jan-1997  tls kill 'register'
 1.5 21-Mar-1995  cgd convert to new RCS id conventions.
 1.4 23-Sep-1994  mycroft Eliminate uses of some obsolete functions.
 1.3 14-Jun-1994  mycroft Fix up RCS ids.
 1.2 13-Jun-1994  jtc Add RCS ID's
 1.1 13-Jun-1994  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 13-Jun-1994  jtc From 4.4 lite
 1.13.2.2 16-Jun-2004  jmc fix botched pullup from ticket #1021 and make sure this compiles on
non-NetBSD hosts again. Also fixes PR#24481
 1.13.2.1 07-Apr-2004  jmc Pullup rev 1.14-1.17 (requested by rafal in ticket #1021)

Pullup pax to current version on trunk. Includes many fixes.
 1.17.4.1 06-May-2005  riz Pull up revision 1.18 (requested by matt in ticket #1164):
Don't write hardlinked file contents in sv4cpio mode.
 1.19.28.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.21.4.1 30-Oct-2012  yamt sync with head
 1.22.34.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.22.34.1 21-Apr-2020  martin Sync with HEAD
 1.6 13-Oct-2003  agc branches: 1.6.98;
Move Keith Muller's code from a 4-clause to a 3-clause licence by removing
the advertising clause. Diffs provided in PR 22397 by Joel Baker, confirmed
to the board by Keith Muller.
 1.5 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.4 17-Feb-2000  itohy branches: 1.4.6;
Cleanup and fix typos.
Partially from FreeBSD.
 1.3 21-Mar-1995  cgd convert to new RCS id conventions.
 1.2 13-Jun-1994  jtc Add RCS ID's
 1.1 13-Jun-1994  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 13-Jun-1994  jtc From 4.4 lite
 1.4.6.1 07-Apr-2004  jmc Pullup rev 1.5-1.6 (requested by rafal in ticket #1021)

Pullup pax to current version on trunk. Includes many fixes.
 1.6.98.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.6.98.1 21-Apr-2020  martin Sync with HEAD
 1.3 30-May-2016  dholland branches: 1.3.16;
Remove undefined behavior in buf(); use buf() as intended in intarg().
While here also add includes to fix the build. Retires PR 50999 from
David Binderman.
 1.2 28-Apr-2008  martin Remove clause 3 and 4 from TNF licenses
 1.1 16-Jun-2004  christos branches: 1.1.4; 1.1.26;
A small standalone program to dump ustar headers, used for debugging.
 1.1.26.1 18-May-2008  yamt sync with head.
 1.1.4.1 23-Jul-2005  snj Apply patch (requested by riz in ticket #5512):
Sync pax with HEAD of 2005-07-22.
 1.3.16.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.3.16.1 21-Apr-2020  martin Sync with HEAD
 1.61 28-May-2023  lukem pax: don't overwrite destination if -r -w copy fails

Add more error handling to pax -r -w so that any failure
during the copy to the temporary file (including a failed flush)
prevents any existing destination file from being replaced
with the partial (including possibly empty) temporary file.
The partial temporary file is removed. pax still exists non-zero.

Thanks to Michael van Elst (mlelstv@) for the analysis
of the problem in the PR.

Should fix PR misc/33753.
 1.60 03-Apr-2020  joerg branches: 1.60.6;
Move definition of chdname and do_chroot to options.c
 1.59 09-Aug-2012  christos branches: 1.59.26; 1.59.34; 1.59.36;
PR/46786: Simon Burge: After conversion to 64 bit time_t, tar/pax/cpio
erroneously think that negative time_t's never fit in 32 bits. Rework
conversion code to always use uintmax_t, and detect negative values.
XXX[1]: Perhaps we should do the same (use a signed conversion) for all
fields not just for time_t
XXX[2]: pullup for 6
 1.58 29-Aug-2011  joerg branches: 1.58.2;
static + __dead
 1.57 18-Jun-2011  christos add --xz
 1.56 07-Apr-2009  perry branches: 1.56.4;
Fixes from christos for pr-41167

XXX -X is still broken, but that wasn't part of this PR.
 1.55 24-Feb-2008  joerg branches: 1.55.8; 1.55.10;
Kill pre-4.4BSD compat code.
 1.54 10-Jan-2008  tls branches: 1.54.2;
Teach set_ftime about symbolic links, because it has to know: on some
Linux systems, when we build as a tool we think we have lutimes but it
doesn't work on some filesystems at runtime. A bit ugly but effective
and without use of AC_TRY_RUN in the tool build. Tidier (than mine)
set_ftime reorganization from christos.
 1.53 04-May-2007  christos branches: 1.53.4;
PR/36275: john at iastate dot edu: tar -s modifies symlink targets
- This is explained in a comment in pat_rep.c inside mod_name(). I did not
want to change the default behavior, so I added another modifier "s" which
when set, the pattern will not modify the symlink destination.
- While here I fixed another bug that was introduced before by the fix in
PR/35257 where the renaming was happening twice since we called rep_name
twice.
- Finally if we are renaming hard of soft-link targets print the renames for
those too.
 1.52 23-Apr-2007  christos PR/36194: Greg A. Woods: add a '-V' flag to pax for verbose summary without
listing (and other minor fixes)
 1.51 16-Apr-2006  christos PR/22995: Sergey Svishchev: If we are cd'ing around and we have directories
with relative paths, convert the directories to absolute paths, so that
restoring timestamps does not fail.
 1.50 11-Feb-2006  dsl Ensure we exit with an error if we don't actually do anything.
There are a lot of tty_warn(0,...) and syswarn(0,...) which probably ought
to be tty_warn/syswarn(1,...) to force an error exit. However some are
used in interactive parts (eg opening a continuation archive) where there
is a separate retry loop.
So we just pass a failure code out to main() - how quaint!
This should now cause the NetBSD build to fail when gzip tries to write
to a non-existant directory.
(I suspect there are still many errors that don't get reported correctly.)
 1.49 16-Sep-2005  christos Another missing mkdir() race pointed out by chuq. Factor out the mkdir code.
 1.48 24-Apr-2005  christos PR/18759: FUKAUMI Naoki: pax/tar dot-dot handling broken
PR/18840: Frederick Bruckman: Fix for PR/18663 incomplete pax symlink handling

This patch makes ``--insecure'' do something. Now if ``--insecure''
is not set (the default) we do a realpath(3) in all the pathnames
that we are trying to create and if either realpath fails, or the
path is outside our working directory, we print a warning and die.
This maybe too strict and might fail on valid archives that create
symlinks and directories in the wrong order.
 1.47 22-Oct-2004  jmc For HOSTTOOL builds, just exclude the chroot support as it's not needed and
avoids having to platform check for fchroot(). Fixes PR#27336
 1.46 17-Oct-2004  dsl Add an option --chroot to tar. Causes it to chroot(".") before doing
an extract. With -h this will cause existing absolute symlinks to be treated
as relative to the current directory.
Helps sysinst handle existing symlinks in the target system.
Remove 'L' from the usage (got spilt into 'h' and 'H' many moons ago)
Add 'S' to usage, and put into correct place in options list.
 1.45 22-Sep-2004  christos Keep track of the length of the gnu long name/link hack, so that
we can append to it. Modify the code so that file_write is re-entrant,
even in the gnu long name/link hack.

The old code assumed that the buffer already contained the necessary
blocks to satisfy the read file request that contained the long
filename. This is not always the case, specially when we are dealing
with pipes which do shorter reads, thus having more probability
that a long file name will fall across a buffer boundary.

To reproduce, create a tar archive with a lot of gnu-long-names
(pkgsrc/devel/libsigc++2 is a good example), do a tar -tf to get
a list of filenames, compress it and do a tar -tzf to get another
list of the filenames. Notice that the two lists differ.
 1.44 22-Aug-2004  tron Propery handle "cpio" archives where the last hardlink includes the
data of a file. This fixes PR bin/26514.
 1.43 19-Jun-2004  christos add -0 argument to handle nul separated file lists.
 1.42 11-May-2004  christos Welcome to WARNS=3
 1.41 13-Feb-2004  matt branches: 1.41.2;
When pax catches a signal and cleans up, make sure to remove any temporary
file that was being extracted to. This will prevent pax from leaving
droppings when you hit ^C.
 1.40 13-Oct-2003  agc Move Keith Muller's code from a 4-clause to a 3-clause licence by removing
the advertising clause. Diffs provided in PR 22397 by Joel Baker, confirmed
to the board by Keith Muller.
 1.39 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.38 08-Jul-2003  simonb Add support for gnutar's -O "extract to stdout" option.
 1.37 25-Feb-2003  wiz Add support for '-j' to create (and extract, but -z already did that)
bzip2 compressed tar files, for GNU tar compatibility.
Patch from Soren Jacobsen in PR 19467, okayed by christos.
Sync usage with options while here.
 1.36 08-Dec-2002  mrg add --exclude support.

XXX: --exclude & --exclude-from are still not 100% compatible, but since
rafal fixed --exclude-from's most annoying bug, they mostly work now.
 1.35 17-Oct-2002  christos Add proper longlink support. Previously we handled longname support, and
the longlink support was completely wrong.
 1.34 16-Oct-2002  christos Support gnu long filename extensions by default for tar on create, list, and
extract. We now generate GNU tar archives by default ("ustar ^@" instead of
"ustar^@00"). GNU extensions can be disabled with --strict.

XXX: long symlinks untested.
 1.33 15-Oct-2002  christos PR/18663: Jeremy Reed: pax/tar/cpio allows ".." in names.
We now disallow it by default on both archive creation and extraction.
Add --insecure option to override.
 1.32 13-Oct-2002  mrg make this build on LP64
 1.31 12-Oct-2002  christos merge OpenBSD changes:
- correct -C processing
- add ability to read filenames and flags from a file
- don't print dangerous escape sequences to the terminal
- use strlcpy/strncpy properly.
- handle tmpfile creation better.
- improve documentation of options.
- handle stdout/stderr list selection correctly.
- kill gzip when we get interrupted.
- simplify gzip setup.
- add more flags to programs.

additional changes:
- librmt processing.
- set POSIXLY_CORRECT in options parsing.
- prevent more string overruns.
- support -T

we don't turn the switch on to replace tar and cpio yet.
 1.30 31-Jan-2002  tv branches: 1.30.2;
Roll in fixes to permit cross-compiling from non-NetBSD hosts. This
round has been tested on Solaris/x86 and Linux hosts.

* Add host tools cap_mkdb, ctags, m4, uudecode.
* Protect __RCSID() and __COPYRIGHT() better.
* Reduce the number of places that need to include "config.h", to keep
sources closer to their "vanilla" versions.
* Add more compat #defines and autoconf-checked functions.
 1.29 29-Jan-2002  tv Make almost all tools compile and run properly on non-NetBSD hosts. (In
particular, most tools now run correctly on Solaris 7.)
 1.28 28-Oct-2001  lukem s/u_longlong_t/unsigned long long/ to shut up the parc port
 1.27 25-Oct-2001  lukem Implement -M flag: During a write or copy operation, treat the list of
files on standard input as an mtree(8) `specfile' specification, and
write or copy only those items in the specfile.

If the file exists in the underlying file system, its permissions and
modification time will be used unless specifically overridden by the
specfile. An error will be raised if the type of entry in the specfile
conflicts with that of an existing file.

Otherwise, it is necessary to specify at least the following parameters
in the specfile: type, mode, gname or gid, and uname or uid, device
(in the case of block or character devices), and link (in the case of
symbolic links). If time isn't provided, the current time will be used.
 1.26 25-Oct-2001  lukem - ansi KNF (just remove all the #ifndef __STDC__ prototype junk,
leaving the ansi stuff)
- use longlong_t instead of quad_t (etc), and rename *uqd*() -> *ull*()
- clean up the NET2_STAT stuff similar to ftpd; provide #defines and
macros which select which cast to use, etc
- clean up the NET2_FTS and NET2_REGEX #define use
 1.25 22-Oct-2000  kleink LC_TIME holds a locale name, which isn't a strftime() format string.
 1.24 04-Jul-2000  thorpej Add GNU tar-style long options for pax's tar front-end. Still many
GNU options not implemented, but there is an #if 0'd out canonical
list.
 1.23 30-Mar-2000  thorpej branches: 1.23.4;
Add a -O option: force one volume. This causes pax to not prompt for
a new volume upon premature end-of-volume (e.g. broken pipe). This is
especially useful in automated environments where error recovery cannot
be performed by a human.
 1.22 17-Feb-2000  itohy Cleanup and fix typos.
Partially from FreeBSD.
 1.21 17-Feb-2000  itohy Fix SIGINFO botch (PR #8868).
Continue partial write(2) on signals (xwrite()).
Partial read(2) at a few places are also continued (xread()).

Add {read,write}_with_restart() hooks for porting on systems
which don't restart interrupted read()/write() calls.

Reviewed and discussed in tech-kern and tech-userlevel lists.
 1.20 01-Nov-1999  mrg add a new -p subflag: 'f' to preserve 4.4BSD fileflags. this only
really works for `pax -rw' but that's where it is most useful.
 1.19 22-Oct-1999  is Implement positional -C argument to tar.
 1.18 22-Oct-1999  mrg support ././@LongLink extraction, as created by GNU tar.
 1.17 24-Aug-1999  tron branches: 1.17.2;
Strip leading slashes from filenames by default, add new "A" option to
disable this behaviour. Patches supplied by Peter Seebach in PR bin/8233.
 1.16 03-Mar-1999  christos Add SIGINFO support.
 1.15 07-Feb-1999  tron Fix opt_add() prototype.
 1.14 20-Jan-1999  mrg implement the gnutar -X flag in terms of pax -s. fix some bugs in pax -s handling while i am here...
 1.13 10-Aug-1998  tv Transparently handle old-style GNU tar archives that may have garbage in
the trailing block and do not have an end-of-archive marker. (Does this
handling based on whether the ustar id is "ustar" or "ustar ".)
 1.12 28-Jul-1998  mycroft Use the uid/gid caching moved into libc. Fixed a memory leak!
 1.11 27-Jul-1998  mycroft __AUDIT__ cleanup.
 1.10 26-Jul-1998  mycroft const poisoning.
 1.9 06-Mar-1998  mrg - implement -C and -l for pax-as-tar, as inspried by GNU tar.
- minor cleanups to the options parsing code.
- add a cpio frontend.

note: a few GNU/svr4 cpio options are not supported yet (#ifdef
notyet), however all x/open ones are.
 1.8 28-Feb-1998  mrg rename variable to "cpio_swp_head"
 1.7 14-Sep-1997  lukem * cleanup manpage
* getopt returns -1 not EOF
* deprecate register
 1.6 20-Jul-1997  christos - Rename local warn so tty_warn that we can include <err.h> since this program
uses err() and errx().
- Fix printf format strings.
- Added WARNS=1; note
- Added missing prototypes.
- Split overloaded trail function into trail() and subtrail().
 1.5 26-Mar-1996  mrg impliment -z (gzip) in pax and tar, and -Z (compress) in tar.
 1.4 21-Mar-1995  cgd convert to new RCS id conventions.
 1.3 04-Dec-1994  cgd from James Jegers <jimj@miller.cs.uwm.edu>: quiet -Wall, and squelch
some of the worst style errors.
 1.2 13-Jun-1994  jtc Add RCS ID's
 1.1 13-Jun-1994  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 13-Jun-1994  jtc From 4.4 lite
 1.17.2.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.23.4.1 25-Oct-2000  tv Pullup 1.25 [kleink]:
Don't abuse LC_TIME for strftime(3) format strings.
 1.30.2.1 07-Apr-2004  jmc Pullup rev 1.31-1.41 (requested by rafal in ticket #1021)

Pullup pax to current version on trunk. Includes many fixes.
 1.41.2.2 12-Nov-2004  jmc branches: 1.41.2.2.2;
Pullup rev 1.45 (requested by christos in ticket #921)

Keep track of the length of the gnu long name/link hack, so that
we can append to it. Modify the code so that file_write is re-entrant,
even in the gnu long name/link hack.
 1.41.2.1 25-Aug-2004  jmc Pullup rev 1.44 (requested by tron in ticket #784)

Propery handle "cpio" archives where the last hardlink includes the
data of a file. PR#26514
 1.41.2.2.2.1 23-Jul-2005  snj Apply patch (requested by riz in ticket #5512):
Sync pax with HEAD of 2005-07-22.
 1.53.4.1 23-Mar-2008  matt sync with HEAD
 1.54.2.1 24-Mar-2008  keiichi sync with head.
 1.55.10.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.55.8.1 13-Apr-2009  snj Pull up following revision(s) (requested by christo in ticket #678):
bin/pax/extern.h: revision 1.56
bin/pax/file_subs.c: revision 1.62
bin/pax/options.c: revision 1.104
bin/pax/pat_rep.c: revision 1.29
bin/pax/pax.h: revision 1.30
Fixes from christos for pr-41167
XXX -X is still broken, but that wasn't part of this PR.
 1.56.4.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.58.2.1 30-Oct-2012  yamt sync with head
 1.59.36.2 07-Aug-2024  martin Pull up following revision(s) (requested by riastradh in ticket #1863):

bin/pax/buf_subs.c: revision 1.31
tests/bin/pax/t_pax.sh: revision 1.3
tests/bin/pax/t_pax.sh: revision 1.4
tests/bin/pax/t_pax.sh: revision 1.5
tests/bin/pax/t_pax.sh: revision 1.6
tests/bin/pax/t_pax.sh: revision 1.7
bin/pax/extern.h: revision 1.61
bin/pax/pax.c: revision 1.50
bin/pax/pax.c: revision 1.51
bin/pax/file_subs.c: revision 1.65
bin/pax/ftree.c: revision 1.43
bin/pax/ar_subs.c: revision 1.58

pax: exit 0 if stdin filelist is used and empty

If copying a list of files from stdin, exit zero instead of non-zero
if there are no files supplied.

AFAICT, POSIX doesn't require a non-zero an error in this situation,
since there are no files to not match.

Fix from PR bin/41736 by Lloyd Parkes.

pax: don't overwrite destination if -r -w copy fails

Add more error handling to pax -r -w so that any failure
during the copy to the temporary file (including a failed flush)
prevents any existing destination file from being replaced
with the partial (including possibly empty) temporary file.

The partial temporary file is removed. pax still exists non-zero.
Thanks to Michael van Elst (mlelstv@) for the analysis
of the problem in the PR.
Should fix PR misc/33753.

tests/bin/pax: Add test for pax list in an ungettable cwd.
The list operation has no need to touch the file system, so it should
have no need for open(".") or getcwd() to succeed.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
(The PR was filed about tar(1), which is now bsdtar by default, but
the issue applies to pax(1) too and would continue to apply to tar(1)
if you set MKBSDTAR=no.)
pax(1): Don't require open(".") or getcwd to work for list operation.

PR 44498: tar(1) unnecessarily demands that getcwd() work
tests/bin/pax: Test pax(1) succeeds with empty file list on stdin.

PR bin/41736: pax reports an error when copying zero files
tests/bin/pax: Add some more cwd-related test cases.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
pax(1): Only require getcwd if we're reading without --insecure.

No other paths use the result of getcwd.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
 1.59.36.1 30-Apr-2021  martin Pull up following revision(s) (requested by mrg in ticket #1263):

bin/pax/options.c: revision 1.119
usr.bin/config/scan.l: revision 1.32
usr.bin/make/main.c: revision 1.274
bin/pax/extern.h: revision 1.60
usr.bin/config/defs.h: revision 1.106
usr.bin/make/make.h: revision 1.105
sbin/newfs_udf/udf_create.c: revision 1.27 (patch)
sbin/newfs_udf/newfs_udf.h: revision 1.7 (patch)
usr.bin/config/main.c: revision 1.100

Avoid depending on common symbols.
 1.59.34.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.59.34.2 21-Apr-2020  martin Sync with HEAD
 1.59.34.1 08-Apr-2020  martin Merge changes from current as of 20200406
 1.59.26.1 07-May-2021  martin Pull up following revision(s) (requested by mrg in ticket #1678):

bin/pax/options.c: revision 1.119
usr.bin/config/scan.l: revision 1.32
usr.bin/make/main.c: revision 1.274
bin/pax/extern.h: revision 1.60
usr.bin/config/defs.h: revision 1.106
usr.bin/make/make.h: revision 1.105
sbin/newfs_udf/udf_create.c: revision 1.27 (patch)
sbin/newfs_udf/newfs_udf.h: revision 1.7 (patch)
usr.bin/config/main.c: revision 1.100
usr.sbin/installboot/Makefile: revisions 1.53,1.54 (patch)
usr.sbin/installboot/installboot.h: revision 1.41 (patch)
usr.sbin/installboot/machines.c: revisisons 1.41,1.42 (patch)

Avoid depending on common symbols.
 1.60.6.1 07-Aug-2024  martin Pull up following revision(s) (requested by riastradh in ticket #776):

bin/pax/buf_subs.c: revision 1.31
tests/bin/pax/t_pax.sh: revision 1.3
tests/bin/pax/t_pax.sh: revision 1.4
tests/bin/pax/t_pax.sh: revision 1.5
tests/bin/pax/t_pax.sh: revision 1.6
tests/bin/pax/t_pax.sh: revision 1.7
bin/pax/extern.h: revision 1.61
bin/pax/pax.c: revision 1.50
bin/pax/pax.c: revision 1.51
bin/pax/file_subs.c: revision 1.65
bin/pax/ftree.c: revision 1.43
bin/pax/ar_subs.c: revision 1.58

pax: exit 0 if stdin filelist is used and empty

If copying a list of files from stdin, exit zero instead of non-zero
if there are no files supplied.

AFAICT, POSIX doesn't require a non-zero an error in this situation,
since there are no files to not match.

Fix from PR bin/41736 by Lloyd Parkes.

pax: don't overwrite destination if -r -w copy fails

Add more error handling to pax -r -w so that any failure
during the copy to the temporary file (including a failed flush)
prevents any existing destination file from being replaced
with the partial (including possibly empty) temporary file.

The partial temporary file is removed. pax still exists non-zero.
Thanks to Michael van Elst (mlelstv@) for the analysis
of the problem in the PR.
Should fix PR misc/33753.

tests/bin/pax: Add test for pax list in an ungettable cwd.
The list operation has no need to touch the file system, so it should
have no need for open(".") or getcwd() to succeed.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
(The PR was filed about tar(1), which is now bsdtar by default, but
the issue applies to pax(1) too and would continue to apply to tar(1)
if you set MKBSDTAR=no.)
pax(1): Don't require open(".") or getcwd to work for list operation.

PR 44498: tar(1) unnecessarily demands that getcwd() work
tests/bin/pax: Test pax(1) succeeds with empty file list on stdin.

PR bin/41736: pax reports an error when copying zero files
tests/bin/pax: Add some more cwd-related test cases.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
pax(1): Only require getcwd if we're reading without --insecure.

No other paths use the result of getcwd.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
 1.66 05-Aug-2024  riastradh pax(1): Nix trailing whitespace.

No functional change intended.
 1.65 28-May-2023  lukem branches: 1.65.2;
pax: don't overwrite destination if -r -w copy fails

Add more error handling to pax -r -w so that any failure
during the copy to the temporary file (including a failed flush)
prevents any existing destination file from being replaced
with the partial (including possibly empty) temporary file.
The partial temporary file is removed. pax still exists non-zero.

Thanks to Michael van Elst (mlelstv@) for the analysis
of the problem in the PR.

Should fix PR misc/33753.
 1.64 20-Mar-2019  gutteridge branches: 1.64.2; 1.64.10;
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).
 1.63 29-Jul-2013  christos branches: 1.63.26;
PR/48095: NAKAJIMA Yoshihiro: remove casts from time_t to long
 1.62 07-Apr-2009  perry branches: 1.62.6; 1.62.12;
Fixes from christos for pr-41167

XXX -X is still broken, but that wasn't part of this PR.
 1.61 10-Jan-2008  tls branches: 1.61.10; 1.61.12;
Teach set_ftime about symbolic links, because it has to know: on some
Linux systems, when we build as a tool we think we have lutimes but it
doesn't work on some filesystems at runtime. A bit ugly but effective
and without use of AC_TRY_RUN in the tool build. Tidier (than mine)
set_ftime reorganization from christos.
 1.60 29-Apr-2007  msaitoh branches: 1.60.4;
fix typos
 1.59 11-Feb-2006  dsl Change all the 'return(x)' to 'return c'.
Makes some other searches I want to do a bit easier.
 1.58 04-Jan-2006  yamt obey umask as it used to be before the "mkstemp and then rename" change.
 1.57 18-Sep-2005  christos Check for another race reported by chuq.
 1.56 16-Sep-2005  christos Another missing mkdir() race pointed out by chuq. Factor out the mkdir code.
 1.55 13-Sep-2005  christos use stat and not lstat to detect if we have a directory. refactor the mkpath()
code.
 1.54 13-Sep-2005  christos when creating directories, check if the failure occured because someone
else created the directory before we did to avoid races. From chuq.
 1.53 24-Apr-2005  christos For clarity instead of using -1 and -2 use the negative of the archive type.
 1.52 22-Sep-2004  christos Keep track of the length of the gnu long name/link hack, so that
we can append to it. Modify the code so that file_write is re-entrant,
even in the gnu long name/link hack.

The old code assumed that the buffer already contained the necessary
blocks to satisfy the read file request that contained the long
filename. This is not always the case, specially when we are dealing
with pipes which do shorter reads, thus having more probability
that a long file name will fall across a buffer boundary.

To reproduce, create a tar archive with a lot of gnu-long-names
(pkgsrc/devel/libsigc++2 is a good example), do a tar -tf to get
a list of filenames, compress it and do a tar -tzf to get another
list of the filenames. Notice that the two lists differ.
 1.51 22-Aug-2004  tron Propery handle "cpio" archives where the last hardlink includes the
data of a file. This fixes PR bin/26514.
 1.50 02-Jul-2004  enami Salvage the `create parent directories and try again' path which is
removed by the previous commit.
 1.49 26-Jun-2004  grant break out of the loop and avoid the infinite loop if attempts to
create, unlink and create all fail.
 1.48 26-Jun-2004  grant don't attempt to remove the current directory.
 1.47 20-Jun-2004  jmc Completely rework how tools/compat is done. Purge all uses/references to
_NETBSD_SOURCE as this makes cross building from older/newer versions of
NetBSD harder, not easier (and also makes the resulting tools 'different')

Wrap all required code with the inclusion of nbtool_config.h, attempt to
only use POSIX code in all places (or when reasonable test w. configure and
provide definitions: ala u_int, etc).

Reviewed by lukem. Tested on FreeBSD 4.9, Redhat Linux ES3, NetBSD 1.6.2 x86
NetBSD current (x86 and amd64) and Solaris 9.

Fixes PR's: PR#17762 PR#25944
 1.46 03-May-2004  christos Don't exit with 1 on broken archives that mark directories as files for
the benefit of pkgsrc. I would have prefered that people fixed their
archivers instead.
 1.45 30-Apr-2004  matt Set the chflags *after* the rename, not before.
 1.44 27-Apr-2004  christos PR/25350: Alan Barrett: Treat the sticky bit specially as non-superusers
cannot set it on non-directories (elvis-2.2_0 extraction failed)
 1.43 25-Apr-2004  christos PR/25188: Thomas Klausner: pax-as-tar problem with trailing slashes
The problem is with the program that generates the tar file:
-rwxrwxrwx 1 root wheel 0 Feb 8 16:46 faad2/aacDECdrop/
It creates directory nodes without the 'd' bit set, so that pax thinks
they are files and does the temporary name and dance with them. Added
code to detect this condition, warn about it, and work around it.
 1.42 20-Apr-2004  christos PR/25207: Hubert Feyrer: tar cores on certain archive contents
Don't forget to cleanup xtmp_name when we fail to create the temporary file.
 1.41 13-Feb-2004  matt branches: 1.41.2;
When pax catches a signal and cleans up, make sure to remove any temporary
file that was being extracted to. This will prevent pax from leaving
droppings when you hit ^C.
 1.40 13-Feb-2004  matt Nuke extra mkstemp. Use name, not tmp_name with chk_path.
 1.39 13-Feb-2004  matt Back out last change. Since the name is the entire path, prefixing it
with a '.' will not have the desired effect.
 1.38 13-Feb-2004  matt When creating the temporary file, prefix the name with a '.'
 1.37 12-Feb-2004  matt When restoring files from an archive, initially restore to a temp-file and
once it has been instantiated correctly, rename it to desired name. This
prevents the problem of partially created files being accessed before they
are complete. If said file is a shared library, that can cause ramdon core
dumps.
 1.36 27-Oct-2003  lukem Overhaul how `build.sh tools' are used:

* Rename "config.h" to "nbtool_config.h" and
HAVE_CONFIG_H to HAVE_NBTOOL_CONFIG_H.
This makes in more obvious in the source when we're using
tools/compat/config.h versus "standard autoconf" config.h

* Consistently move the inclusion of nbtool_config.h to before
<sys/cdefs.h> so that the former can provide __RCSID() (et al),
and there's no need to protect those macros any more.

These changes should make it easier to "tool-ify" a program by adding:
#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
to the top of the source files (for the general case).
 1.35 13-Oct-2003  agc Move Keith Muller's code from a 4-clause to a 3-clause licence by removing
the advertising clause. Diffs provided in PR 22397 by Joel Baker, confirmed
to the board by Keith Muller.
 1.34 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.33 23-Jun-2003  grant no need for HAVE_LCHOWN and HAVE_LCHMOD anymore.
 1.32 10-Feb-2003  grant add missing braces.

from HIRATSUKA Kouichirou in PR bin/20276.
 1.31 09-Feb-2003  grant cosmetic error condition improvements.
- sprinkle some fflush() to print filenames correctly.
- print a \n to stdout to fix formatting.
- English, consistently use "Cannot" in error messages.

some whitespace cleanup.
 1.30 02-Feb-2003  wiz Fix various spelling, grammar, and punctuation problems in comments, from Sergey Svishchev in PR 20164.
 1.29 20-Jan-2003  simonb The Double-Semi-Colon Police.
 1.28 06-Jan-2003  wiz writable, not writeable.
 1.27 19-Dec-2002  grant Fix oops in last, chown() failure should be fatal.

Noted by Charles Hannum.
 1.26 19-Dec-2002  grant Don't attempt to chown files if euid != 0, and make chown() non-fatal
if it does fail.

This addresses my PR bin/18912 (running pkg_info as non-root user on a
binary package).
 1.25 17-Oct-2002  christos Add proper longlink support. Previously we handled longname support, and
the longlink support was completely wrong.
 1.24 12-Oct-2002  christos merge OpenBSD changes:
- correct -C processing
- add ability to read filenames and flags from a file
- don't print dangerous escape sequences to the terminal
- use strlcpy/strncpy properly.
- handle tmpfile creation better.
- improve documentation of options.
- handle stdout/stderr list selection correctly.
- kill gzip when we get interrupted.
- simplify gzip setup.
- add more flags to programs.

additional changes:
- librmt processing.
- set POSIXLY_CORRECT in options parsing.
- prevent more string overruns.
- support -T

we don't turn the switch on to replace tar and cpio yet.
 1.23 11-Feb-2002  wiz branches: 1.23.2;
"doesn't" should have an 's'.
 1.22 31-Jan-2002  tv Protect __RCSID and __COPYRIGHT from being invoked if not defined.
 1.21 29-Jan-2002  tv Make almost all tools compile and run properly on non-NetBSD hosts. (In
particular, most tools now run correctly on Solaris 7.)
 1.20 25-Oct-2001  lukem - ansi KNF (just remove all the #ifndef __STDC__ prototype junk,
leaving the ansi stuff)
- use longlong_t instead of quad_t (etc), and rename *uqd*() -> *ull*()
- clean up the NET2_STAT stuff similar to ftpd; provide #defines and
macros which select which cast to use, etc
- clean up the NET2_FTS and NET2_REGEX #define use
 1.19 17-Jun-2000  mrg minor KNF nits.
 1.18 13-Mar-2000  soren branches: 1.18.2;
Fix doubled 'the' in comment.
 1.17 17-Feb-2000  itohy Cleanup and fix typos.
Partially from FreeBSD.
 1.16 17-Feb-2000  itohy Fix SIGINFO botch (PR #8868).
Continue partial write(2) on signals (xwrite()).
Partial read(2) at a few places are also continued (xread()).

Add {read,write}_with_restart() hooks for porting on systems
which don't restart interrupted read()/write() calls.

Reviewed and discussed in tech-kern and tech-userlevel lists.
 1.15 07-Nov-1999  mycroft branches: 1.15.2;
Completely disable all the chflags(2) support for now. With this enabled, I
can't even get a build to finish over NFS. Not acceptable.
 1.14 07-Nov-1999  mycroft Don't try to set file flags for a symlink.
 1.13 01-Nov-1999  mrg add a new -p subflag: 'f' to preserve 4.4BSD fileflags. this only
really works for `pax -rw' but that's where it is most useful.
 1.12 22-Oct-1999  mrg support ././@LongLink extraction, as created by GNU tar.
 1.11 22-Oct-1999  mrg remove some old cruft.
 1.10 28-Feb-1998  mrg branches: 1.10.4;
fix typo in a comment
 1.9 03-Feb-1998  mycroft Deal with GCC warnings.
 1.8 19-Oct-1997  mycroft When copying time stamps, copy the microseconds, too. Also nuke silly
static structure.
 1.7 08-Oct-1997  enami Restore owner/group/mode/atime/mtime of symbolic links.
 1.6 20-Jul-1997  christos - Rename local warn so tty_warn that we can include <err.h> since this program
uses err() and errx().
- Fix printf format strings.
- Added WARNS=1; note
- Added missing prototypes.
- Split overloaded trail function into trail() and subtrail().
 1.5 11-Jan-1997  tls kill 'register'
 1.4 21-Mar-1995  cgd convert to new RCS id conventions.
 1.3 14-Jun-1994  mycroft Fix up RCS ids.
 1.2 13-Jun-1994  jtc Add RCS ID's
 1.1 13-Jun-1994  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 13-Jun-1994  jtc From 4.4 lite
 1.10.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.15.2.2 07-Nov-1999  mycroft Completely disable all the chflags(2) support for now. With this enabled, I
can't even get a build to finish over NFS. Not acceptable.
 1.15.2.1 07-Nov-1999  mycroft file file_subs.c was added on branch comdex-fall-1999 on 1999-11-07 15:48:25 +0000
 1.18.2.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.23.2.3 25-Aug-2004  he Pull up revisions 1.42-1.46,1.48-1.50 (requested by cube in ticket #1736):
Fix handling of certain archives, fixing core dump (PR#25207).
Fix problem with trailing slashes (PR#15188).
Treat sticky bit specially for non-superuser (PR#25350).
Set chflags after rename, not before.
Don't exit with 1 on broken archives that mark directories as files.
Don't attempt to remove current directory.
Break out of loop and avoid infinite loop in certain error situations.
Salvage the `create parent directories and try again' patch.
 1.23.2.2 16-Jun-2004  jmc fix botched pullup from ticket #1021 and make sure this compiles on
non-NetBSD hosts again. Also fixes PR#24481
 1.23.2.1 07-Apr-2004  jmc Pullup rev 1.24-1.41 (requested by rafal in ticket #1021)

Pullup pax to current version on trunk. Includes many fixes.
 1.41.2.8 12-Nov-2004  jmc branches: 1.41.2.8.2;
Pullup rev 1.52 (requested by christos in ticket #921)

Keep track of the length of the gnu long name/link hack, so that
we can append to it. Modify the code so that file_write is re-entrant,
even in the gnu long name/link hack.
 1.41.2.7 25-Aug-2004  jmc Pullup rev 1.51 (requested by tron in ticket #784)

Propery handle "cpio" archives where the last hardlink includes the
data of a file. PR#26514
 1.41.2.6 12-Aug-2004  jmc Pullup rev 1.48-1.50 (requested by cube in ticket #754)

Fixes for pax breaking on some archives
 1.41.2.5 22-Jun-2004  tron Pull up revision 1.47 (requested by jmc in ticket #527):
Completely rework how tools/compat is done. Purge all uses/references to
_NETBSD_SOURCE as this makes cross building from older/newer versions of
NetBSD harder, not easier (and also makes the resulting tools 'different')
Wrap all required code with the inclusion of nbtool_config.h, attempt to
only use POSIX code in all places (or when reasonable test w. configure and
provide definitions: ala u_int, etc).
Reviewed by lukem. Tested on FreeBSD 4.9, Redhat Linux ES3, NetBSD 1.6.2 x86
NetBSD current (x86 and amd64) and Solaris 9.
Fixes PR's: PR#17762 PR#25944
 1.41.2.4 06-May-2004  jmc Pullup rev 1.45-1.46 (requested by snj in ticket #252)

Set the chflags *after* the rename, not before.
Don't exit with 1 on broken archives that mark directories as files for
the benefit of pkgsrc.
 1.41.2.3 28-Apr-2004  grant Pull up revision 1.44 (requested by snj in ticket #217):

PR/25350: Alan Barrett: Treat the sticky bit specially as non-superusers
cannot set it on non-directories (elvis-2.2_0 extraction failed)
 1.41.2.2 28-Apr-2004  grant Pull up revision 1.43 (requested by snj in ticket #217):

PR/25188: Thomas Klausner: pax-as-tar problem with trailing slashes
The problem is with the program that generates the tar file:
-rwxrwxrwx 1 root wheel 0 Feb 8 16:46 faad2/aacDECdrop/
It creates directory nodes without the 'd' bit set, so that pax thinks
they are files and does the temporary name and dance with them. Added
code to detect this condition, warn about it, and work around it.
 1.41.2.1 21-Apr-2004  jmc Pullup rev 1.42 (requested by christos in ticket #170)

Fixes for multiple pax pr's. PR#25200. PR#25207.
 1.41.2.8.2.1 23-Jul-2005  snj Apply patch (requested by riz in ticket #5512):
Sync pax with HEAD of 2005-07-22.
 1.60.4.1 23-Mar-2008  matt sync with HEAD
 1.61.12.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.61.10.1 13-Apr-2009  snj Pull up following revision(s) (requested by christo in ticket #678):
bin/pax/extern.h: revision 1.56
bin/pax/file_subs.c: revision 1.62
bin/pax/options.c: revision 1.104
bin/pax/pat_rep.c: revision 1.29
bin/pax/pax.h: revision 1.30
Fixes from christos for pr-41167
XXX -X is still broken, but that wasn't part of this PR.
 1.62.12.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.62.6.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.63.26.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.63.26.2 21-Apr-2020  martin Sync with HEAD
 1.63.26.1 10-Jun-2019  christos Sync with HEAD
 1.64.10.1 07-Aug-2024  martin Pull up following revision(s) (requested by riastradh in ticket #776):

bin/pax/buf_subs.c: revision 1.31
tests/bin/pax/t_pax.sh: revision 1.3
tests/bin/pax/t_pax.sh: revision 1.4
tests/bin/pax/t_pax.sh: revision 1.5
tests/bin/pax/t_pax.sh: revision 1.6
tests/bin/pax/t_pax.sh: revision 1.7
bin/pax/extern.h: revision 1.61
bin/pax/pax.c: revision 1.50
bin/pax/pax.c: revision 1.51
bin/pax/file_subs.c: revision 1.65
bin/pax/ftree.c: revision 1.43
bin/pax/ar_subs.c: revision 1.58

pax: exit 0 if stdin filelist is used and empty

If copying a list of files from stdin, exit zero instead of non-zero
if there are no files supplied.

AFAICT, POSIX doesn't require a non-zero an error in this situation,
since there are no files to not match.

Fix from PR bin/41736 by Lloyd Parkes.

pax: don't overwrite destination if -r -w copy fails

Add more error handling to pax -r -w so that any failure
during the copy to the temporary file (including a failed flush)
prevents any existing destination file from being replaced
with the partial (including possibly empty) temporary file.

The partial temporary file is removed. pax still exists non-zero.
Thanks to Michael van Elst (mlelstv@) for the analysis
of the problem in the PR.
Should fix PR misc/33753.

tests/bin/pax: Add test for pax list in an ungettable cwd.
The list operation has no need to touch the file system, so it should
have no need for open(".") or getcwd() to succeed.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
(The PR was filed about tar(1), which is now bsdtar by default, but
the issue applies to pax(1) too and would continue to apply to tar(1)
if you set MKBSDTAR=no.)
pax(1): Don't require open(".") or getcwd to work for list operation.

PR 44498: tar(1) unnecessarily demands that getcwd() work
tests/bin/pax: Test pax(1) succeeds with empty file list on stdin.

PR bin/41736: pax reports an error when copying zero files
tests/bin/pax: Add some more cwd-related test cases.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
pax(1): Only require getcwd if we're reading without --insecure.

No other paths use the result of getcwd.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
 1.64.2.1 07-Aug-2024  martin Pull up following revision(s) (requested by riastradh in ticket #1863):

bin/pax/buf_subs.c: revision 1.31
tests/bin/pax/t_pax.sh: revision 1.3
tests/bin/pax/t_pax.sh: revision 1.4
tests/bin/pax/t_pax.sh: revision 1.5
tests/bin/pax/t_pax.sh: revision 1.6
tests/bin/pax/t_pax.sh: revision 1.7
bin/pax/extern.h: revision 1.61
bin/pax/pax.c: revision 1.50
bin/pax/pax.c: revision 1.51
bin/pax/file_subs.c: revision 1.65
bin/pax/ftree.c: revision 1.43
bin/pax/ar_subs.c: revision 1.58

pax: exit 0 if stdin filelist is used and empty

If copying a list of files from stdin, exit zero instead of non-zero
if there are no files supplied.

AFAICT, POSIX doesn't require a non-zero an error in this situation,
since there are no files to not match.

Fix from PR bin/41736 by Lloyd Parkes.

pax: don't overwrite destination if -r -w copy fails

Add more error handling to pax -r -w so that any failure
during the copy to the temporary file (including a failed flush)
prevents any existing destination file from being replaced
with the partial (including possibly empty) temporary file.

The partial temporary file is removed. pax still exists non-zero.
Thanks to Michael van Elst (mlelstv@) for the analysis
of the problem in the PR.
Should fix PR misc/33753.

tests/bin/pax: Add test for pax list in an ungettable cwd.
The list operation has no need to touch the file system, so it should
have no need for open(".") or getcwd() to succeed.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
(The PR was filed about tar(1), which is now bsdtar by default, but
the issue applies to pax(1) too and would continue to apply to tar(1)
if you set MKBSDTAR=no.)
pax(1): Don't require open(".") or getcwd to work for list operation.

PR 44498: tar(1) unnecessarily demands that getcwd() work
tests/bin/pax: Test pax(1) succeeds with empty file list on stdin.

PR bin/41736: pax reports an error when copying zero files
tests/bin/pax: Add some more cwd-related test cases.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
pax(1): Only require getcwd if we're reading without --insecure.

No other paths use the result of getcwd.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
 1.65.2.1 02-Aug-2025  perseant Sync with HEAD
 1.46 08-Sep-2024  rillig s/effect/affect/ in a few places
 1.45 08-Sep-2024  rillig fix a/an grammar in obvious cases
 1.44 05-Aug-2024  riastradh pax(1): Nix trailing whitespace.

No functional change intended.
 1.43 28-May-2023  lukem branches: 1.43.2;
pax: exit 0 if stdin filelist is used and empty

If copying a list of files from stdin, exit zero instead of non-zero
if there are no files supplied.

AFAICT, POSIX doesn't require a non-zero an error in this situation,
since there are no files to not match.

Fix from PR bin/41736 by Lloyd Parkes.
 1.42 27-Sep-2012  christos branches: 1.42.32; 1.42.34; 1.42.42;
deal properly with empty lines in spec file
 1.41 20-Mar-2012  matt branches: 1.41.2;
Use C89 function definitions
 1.40 14-Feb-2009  lukem branches: 1.40.6; 1.40.8;
fix -Wsign-compare issues
 1.39 28-Apr-2008  martin branches: 1.39.6; 1.39.8;
Remove clause 3 and 4 from TNF licenses
 1.38 24-Feb-2008  joerg branches: 1.38.2;
Kill pre-4.4BSD compat code.
 1.37 18-Feb-2008  simonb branches: 1.37.2;
Set "ftent" to NULL in in ftree_arg() after we call fts_close(), so that
it isn't used later on in ftree_sel() after it's been free'd.

OK christos@ (and for the interesting test case).
 1.36 10-Jan-2008  tls Teach set_ftime about symbolic links, because it has to know: on some
Linux systems, when we build as a tool we think we have lutimes but it
doesn't work on some filesystems at runtime. A bit ugly but effective
and without use of AC_TRY_RUN in the tool build. Tidier (than mine)
set_ftime reorganization from christos.
 1.35 29-Apr-2007  msaitoh branches: 1.35.4;
fix typos
 1.34 11-Feb-2006  dsl branches: 1.34.4;
Change all the 'return(x)' to 'return c'.
Makes some other searches I want to do a bit easier.
 1.33 24-Sep-2005  dsl Don't leave arcn->org_name pointing to ftent->fts_path, if we being cpio
then ftent is freed just below. Take a copy of the name and point org_name
at the copy.
Should fix PR/30627 (the fix in the PR will break pax and tar!)
 1.32 02-Jul-2004  christos remove stray debugging left from last commit.
 1.31 20-Jun-2004  jmc Completely rework how tools/compat is done. Purge all uses/references to
_NETBSD_SOURCE as this makes cross building from older/newer versions of
NetBSD harder, not easier (and also makes the resulting tools 'different')

Wrap all required code with the inclusion of nbtool_config.h, attempt to
only use POSIX code in all places (or when reasonable test w. configure and
provide definitions: ala u_int, etc).

Reviewed by lukem. Tested on FreeBSD 4.9, Redhat Linux ES3, NetBSD 1.6.2 x86
NetBSD current (x86 and amd64) and Solaris 9.

Fixes PR's: PR#17762 PR#25944
 1.30 19-Jun-2004  christos add -0 argument to handle nul separated file lists.
 1.29 27-Oct-2003  lukem branches: 1.29.2;
Overhaul how `build.sh tools' are used:

* Rename "config.h" to "nbtool_config.h" and
HAVE_CONFIG_H to HAVE_NBTOOL_CONFIG_H.
This makes in more obvious in the source when we're using
tools/compat/config.h versus "standard autoconf" config.h

* Consistently move the inclusion of nbtool_config.h to before
<sys/cdefs.h> so that the former can provide __RCSID() (et al),
and there's no need to protect those macros any more.

These changes should make it easier to "tool-ify" a program by adding:
#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
to the top of the source files (for the general case).
 1.28 13-Oct-2003  agc Move Keith Muller's code from a 4-clause to a 3-clause licence by removing
the advertising clause. Diffs provided in PR 22397 by Joel Baker, confirmed
to the board by Keith Muller.
 1.27 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.26 23-Jun-2003  christos spelling
 1.25 09-May-2003  itojun use sizeof for safety
 1.24 21-Apr-2003  christos PR/21253: Manuel Bouyer: cpio should never descend on a directory; even on
the first one.
 1.23 19-Oct-2002  provos use readlink with bufsize - 1; approved thorpej.
 1.22 12-Oct-2002  christos merge OpenBSD changes:
- correct -C processing
- add ability to read filenames and flags from a file
- don't print dangerous escape sequences to the terminal
- use strlcpy/strncpy properly.
- handle tmpfile creation better.
- improve documentation of options.
- handle stdout/stderr list selection correctly.
- kill gzip when we get interrupted.
- simplify gzip setup.
- add more flags to programs.

additional changes:
- librmt processing.
- set POSIXLY_CORRECT in options parsing.
- prevent more string overruns.
- support -T

we don't turn the switch on to replace tar and cpio yet.
 1.21 28-Jun-2002  lukem Change how "cd .." is handled in next_file() with -M, to ensure that curdir[]
isn't addressed with a negative offset when back at the top of the tree.
This caused pax -M on sparc64 to generate corrupt tar files.
Problem found by Tim Goodwin <tjg@star.le.ac.uk> in [bin/17412].
 1.20 20-Apr-2002  lukem branches: 1.20.2;
- Directories marked `optional' won't be stored, even though their
contents will be.
- Document that `optional' faked-up entries won't be created.
 1.19 12-Apr-2002  lukem If an entry is in the specfile but not in the underlying file system, and it's
marked "optional", don't add it.
 1.18 02-Feb-2002  lukem - don't compile in support for -M (mtree specfile input) or -N dbdir
(alternate directory for id info) if SMALL is defined
- enable -DSMALL and remove unused objects if CRUNCHEDPROG is defined
- minor cleanup in next_file() (preparation for future work)
 1.17 31-Jan-2002  tv Roll in fixes to permit cross-compiling from non-NetBSD hosts. This
round has been tested on Solaris/x86 and Linux hosts.

* Add host tools cap_mkdb, ctags, m4, uudecode.
* Protect __RCSID() and __COPYRIGHT() better.
* Reduce the number of places that need to include "config.h", to keep
sources closer to their "vanilla" versions.
* Add more compat #defines and autoconf-checked functions.
 1.16 29-Jan-2002  tv Make almost all tools compile and run properly on non-NetBSD hosts. (In
particular, most tools now run correctly on Solaris 7.)
 1.15 26-Oct-2001  lukem improve previous; use %lu and (u_long) to print size_t
 1.14 26-Oct-2001  lukem Cast size_t's to (int) when printing as %d; appeases the sparc port.
Found by Rob Windsor in [bin/14362].
 1.13 25-Oct-2001  lukem minor improvements in wording and flag testing
 1.12 25-Oct-2001  lukem Implement -M flag: During a write or copy operation, treat the list of
files on standard input as an mtree(8) `specfile' specification, and
write or copy only those items in the specfile.

If the file exists in the underlying file system, its permissions and
modification time will be used unless specifically overridden by the
specfile. An error will be raised if the type of entry in the specfile
conflicts with that of an existing file.

Otherwise, it is necessary to specify at least the following parameters
in the specfile: type, mode, gname or gid, and uname or uid, device
(in the case of block or character devices), and link (in the case of
symbolic links). If time isn't provided, the current time will be used.
 1.11 25-Oct-2001  lukem - ansi KNF (just remove all the #ifndef __STDC__ prototype junk,
leaving the ansi stuff)
- use longlong_t instead of quad_t (etc), and rename *uqd*() -> *ull*()
- clean up the NET2_STAT stuff similar to ftpd; provide #defines and
macros which select which cast to use, etc
- clean up the NET2_FTS and NET2_REGEX #define use
 1.10 17-Feb-2000  itohy Cleanup and fix typos.
Partially from FreeBSD.
 1.9 22-Oct-1999  is Implement positional -C argument to tar.
 1.8 28-Jul-1998  mycroft branches: 1.8.4;
Use the uid/gid caching moved into libc. Fixed a memory leak!
 1.7 06-Mar-1998  mrg - implement -C and -l for pax-as-tar, as inspried by GNU tar.
- minor cleanups to the options parsing code.
- add a cpio frontend.

note: a few GNU/svr4 cpio options are not supported yet (#ifdef
notyet), however all x/open ones are.
 1.6 20-Jul-1997  christos - Rename local warn so tty_warn that we can include <err.h> since this program
uses err() and errx().
- Fix printf format strings.
- Added WARNS=1; note
- Added missing prototypes.
- Split overloaded trail function into trail() and subtrail().
 1.5 11-Jan-1997  tls kill 'register'
 1.4 21-Mar-1995  cgd convert to new RCS id conventions.
 1.3 14-Jun-1994  mycroft Fix up RCS ids.
 1.2 13-Jun-1994  jtc Add RCS ID's
 1.1 13-Jun-1994  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 13-Jun-1994  jtc From 4.4 lite
 1.8.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.20.2.3 16-Jun-2004  jmc fix botched pullup from ticket #1021 and make sure this compiles on
non-NetBSD hosts again. Also fixes PR#24481
 1.20.2.2 07-Apr-2004  jmc Pullup rev 1.22-1.29 (requested by rafal in ticket #1021)

Pullup pax to current version on trunk. Includes many fixes.
 1.20.2.1 28-Jun-2002  lukem Pull up revision 1.21 (requested by lukem in ticket #388):
Change how "cd .." is handled in next_file() with -M, to ensure that curdir[]
isn't addressed with a negative offset when back at the top of the tree.
This caused pax -M on sparc64 to generate corrupt tar files.
Problem found by Tim Goodwin <tjg@star.le.ac.uk> in [bin/17412].
 1.29.2.1 22-Jun-2004  tron branches: 1.29.2.1.2;
Pull up revision 1.31 (requested by jmc in ticket #527):
Completely rework how tools/compat is done. Purge all uses/references to
_NETBSD_SOURCE as this makes cross building from older/newer versions of
NetBSD harder, not easier (and also makes the resulting tools 'different')
Wrap all required code with the inclusion of nbtool_config.h, attempt to
only use POSIX code in all places (or when reasonable test w. configure and
provide definitions: ala u_int, etc).
Reviewed by lukem. Tested on FreeBSD 4.9, Redhat Linux ES3, NetBSD 1.6.2 x86
NetBSD current (x86 and amd64) and Solaris 9.
Fixes PR's: PR#17762 PR#25944
 1.29.2.1.2.1 23-Jul-2005  snj Apply patch (requested by riz in ticket #5512):
Sync pax with HEAD of 2005-07-22.
 1.34.4.1 23-Oct-2012  riz Pull up following revision(s) (requested by msaitoh in ticket #1459):
bin/pax/ftree.c: revision 1.42
deal properly with empty lines in spec file
 1.35.4.1 23-Mar-2008  matt sync with HEAD
 1.37.2.1 24-Mar-2008  keiichi sync with head.
 1.38.2.1 18-May-2008  yamt sync with head.
 1.39.8.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.39.6.1 17-Oct-2012  bouyer Pull up following revision(s) (requested by msaitoh in ticket #1799):
bin/pax/ftree.c: revision 1.42
deal properly with empty lines in spec file
 1.40.8.1 17-Oct-2012  riz Pull up following revision(s) (requested by msaitoh in ticket #586):
bin/pax/ftree.c: revision 1.42
deal properly with empty lines in spec file
 1.40.6.2 30-Oct-2012  yamt sync with head
 1.40.6.1 17-Apr-2012  yamt sync with head
 1.41.2.1 20-Nov-2012  tls Resync to 2012-11-19 00:00:00 UTC
 1.42.42.1 07-Aug-2024  martin Pull up following revision(s) (requested by riastradh in ticket #776):

bin/pax/buf_subs.c: revision 1.31
tests/bin/pax/t_pax.sh: revision 1.3
tests/bin/pax/t_pax.sh: revision 1.4
tests/bin/pax/t_pax.sh: revision 1.5
tests/bin/pax/t_pax.sh: revision 1.6
tests/bin/pax/t_pax.sh: revision 1.7
bin/pax/extern.h: revision 1.61
bin/pax/pax.c: revision 1.50
bin/pax/pax.c: revision 1.51
bin/pax/file_subs.c: revision 1.65
bin/pax/ftree.c: revision 1.43
bin/pax/ar_subs.c: revision 1.58

pax: exit 0 if stdin filelist is used and empty

If copying a list of files from stdin, exit zero instead of non-zero
if there are no files supplied.

AFAICT, POSIX doesn't require a non-zero an error in this situation,
since there are no files to not match.

Fix from PR bin/41736 by Lloyd Parkes.

pax: don't overwrite destination if -r -w copy fails

Add more error handling to pax -r -w so that any failure
during the copy to the temporary file (including a failed flush)
prevents any existing destination file from being replaced
with the partial (including possibly empty) temporary file.

The partial temporary file is removed. pax still exists non-zero.
Thanks to Michael van Elst (mlelstv@) for the analysis
of the problem in the PR.
Should fix PR misc/33753.

tests/bin/pax: Add test for pax list in an ungettable cwd.
The list operation has no need to touch the file system, so it should
have no need for open(".") or getcwd() to succeed.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
(The PR was filed about tar(1), which is now bsdtar by default, but
the issue applies to pax(1) too and would continue to apply to tar(1)
if you set MKBSDTAR=no.)
pax(1): Don't require open(".") or getcwd to work for list operation.

PR 44498: tar(1) unnecessarily demands that getcwd() work
tests/bin/pax: Test pax(1) succeeds with empty file list on stdin.

PR bin/41736: pax reports an error when copying zero files
tests/bin/pax: Add some more cwd-related test cases.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
pax(1): Only require getcwd if we're reading without --insecure.

No other paths use the result of getcwd.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
 1.42.34.1 07-Aug-2024  martin Pull up following revision(s) (requested by riastradh in ticket #1863):

bin/pax/buf_subs.c: revision 1.31
tests/bin/pax/t_pax.sh: revision 1.3
tests/bin/pax/t_pax.sh: revision 1.4
tests/bin/pax/t_pax.sh: revision 1.5
tests/bin/pax/t_pax.sh: revision 1.6
tests/bin/pax/t_pax.sh: revision 1.7
bin/pax/extern.h: revision 1.61
bin/pax/pax.c: revision 1.50
bin/pax/pax.c: revision 1.51
bin/pax/file_subs.c: revision 1.65
bin/pax/ftree.c: revision 1.43
bin/pax/ar_subs.c: revision 1.58

pax: exit 0 if stdin filelist is used and empty

If copying a list of files from stdin, exit zero instead of non-zero
if there are no files supplied.

AFAICT, POSIX doesn't require a non-zero an error in this situation,
since there are no files to not match.

Fix from PR bin/41736 by Lloyd Parkes.

pax: don't overwrite destination if -r -w copy fails

Add more error handling to pax -r -w so that any failure
during the copy to the temporary file (including a failed flush)
prevents any existing destination file from being replaced
with the partial (including possibly empty) temporary file.

The partial temporary file is removed. pax still exists non-zero.
Thanks to Michael van Elst (mlelstv@) for the analysis
of the problem in the PR.
Should fix PR misc/33753.

tests/bin/pax: Add test for pax list in an ungettable cwd.
The list operation has no need to touch the file system, so it should
have no need for open(".") or getcwd() to succeed.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
(The PR was filed about tar(1), which is now bsdtar by default, but
the issue applies to pax(1) too and would continue to apply to tar(1)
if you set MKBSDTAR=no.)
pax(1): Don't require open(".") or getcwd to work for list operation.

PR 44498: tar(1) unnecessarily demands that getcwd() work
tests/bin/pax: Test pax(1) succeeds with empty file list on stdin.

PR bin/41736: pax reports an error when copying zero files
tests/bin/pax: Add some more cwd-related test cases.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
pax(1): Only require getcwd if we're reading without --insecure.

No other paths use the result of getcwd.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
 1.42.32.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.42.32.1 21-Apr-2020  martin Sync with HEAD
 1.43.2.1 02-Aug-2025  perseant Sync with HEAD
 1.5 13-Oct-2003  agc branches: 1.5.98;
Move Keith Muller's code from a 4-clause to a 3-clause licence by removing
the advertising clause. Diffs provided in PR 22397 by Joel Baker, confirmed
to the board by Keith Muller.
 1.4 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.3 21-Mar-1995  cgd branches: 1.3.18;
convert to new RCS id conventions.
 1.2 13-Jun-1994  jtc Add RCS ID's
 1.1 13-Jun-1994  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 13-Jun-1994  jtc From 4.4 lite
 1.3.18.1 07-Apr-2004  jmc Pullup rev 1.4-1.5 (requested by rafal in ticket #1021)

Pullup pax to current version on trunk. Includes many fixes.
 1.5.98.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.5.98.1 21-Apr-2020  martin Sync with HEAD
 1.37 30-Nov-2018  christos Add base-256 decoding support (Micha Gorny)
 1.36 09-Aug-2012  christos branches: 1.36.32; 1.36.34;
PR/46786: Simon Burge: After conversion to 64 bit time_t, tar/pax/cpio
erroneously think that negative time_t's never fit in 32 bits. Rework
conversion code to always use uintmax_t, and detect negative values.
XXX[1]: Perhaps we should do the same (use a signed conversion) for all
fields not just for time_t
XXX[2]: pullup for 6
 1.35 14-Aug-2011  christos branches: 1.35.2;
eliminate dup code and non literal format strings
 1.34 24-Feb-2008  joerg Kill pre-4.4BSD compat code.
 1.33 11-Feb-2006  dsl branches: 1.33.10; 1.33.16;
Change all the 'return(x)' to 'return c'.
Makes some other searches I want to do a bit easier.
 1.32 27-Oct-2003  lukem Overhaul how `build.sh tools' are used:

* Rename "config.h" to "nbtool_config.h" and
HAVE_CONFIG_H to HAVE_NBTOOL_CONFIG_H.
This makes in more obvious in the source when we're using
tools/compat/config.h versus "standard autoconf" config.h

* Consistently move the inclusion of nbtool_config.h to before
<sys/cdefs.h> so that the former can provide __RCSID() (et al),
and there's no need to protect those macros any more.

These changes should make it easier to "tool-ify" a program by adding:
#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
to the top of the source files (for the general case).
 1.31 13-Oct-2003  agc Move Keith Muller's code from a 4-clause to a 3-clause licence by removing
the advertising clause. Diffs provided in PR 22397 by Joel Baker, confirmed
to the board by Keith Muller.
 1.30 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.29 24-Jun-2003  grant backout previous; it was a missing prototype elsewhere.

noted by mrg.
 1.28 24-Jun-2003  grant add cast to fix an integer -> pointer warning.
 1.27 19-Dec-2002  kleink Change verbose output for symbolic links from "%s => %s" to "%s -> %s",
which matches ls(1) (per POSIX), as well as GNU tar output.
 1.26 13-Oct-2002  mrg make this build on LP64
 1.25 12-Oct-2002  christos merge OpenBSD changes:
- correct -C processing
- add ability to read filenames and flags from a file
- don't print dangerous escape sequences to the terminal
- use strlcpy/strncpy properly.
- handle tmpfile creation better.
- improve documentation of options.
- handle stdout/stderr list selection correctly.
- kill gzip when we get interrupted.
- simplify gzip setup.
- add more flags to programs.

additional changes:
- librmt processing.
- set POSIXLY_CORRECT in options parsing.
- prevent more string overruns.
- support -T

we don't turn the switch on to replace tar and cpio yet.
 1.24 01-Aug-2002  wiz Remove unneeded utmp.h include.
 1.23 31-Jan-2002  tv branches: 1.23.2;
Protect __RCSID and __COPYRIGHT from being invoked if not defined.
 1.22 28-Oct-2001  lukem s/u_longlong_t/unsigned long long/ to shut up the parc port
 1.21 25-Oct-2001  lukem - ansi KNF (just remove all the #ifndef __STDC__ prototype junk,
leaving the ansi stuff)
- use longlong_t instead of quad_t (etc), and rename *uqd*() -> *ull*()
- clean up the NET2_STAT stuff similar to ftpd; provide #defines and
macros which select which cast to use, etc
- clean up the NET2_FTS and NET2_REGEX #define use
 1.20 04-Jan-2001  lukem use more standard %ll_ in favour of %q_
 1.19 22-Oct-2000  kleink LC_TIME holds a locale name, which isn't a strftime() format string.
 1.18 17-Jun-2000  mrg branches: 1.18.2;
minor KNF nits.
 1.17 17-Feb-2000  itohy branches: 1.17.2;
Cleanup and fix typos.
Partially from FreeBSD.
 1.16 22-Oct-1999  mrg remove some old cruft.
 1.15 24-Aug-1999  tron branches: 1.15.2;
Strip leading slashes from filenames by default, add new "A" option to
disable this behaviour. Patches supplied by Peter Seebach in PR bin/8233.
 1.14 04-Nov-1998  christos make printf formats conservative
 1.13 28-Jul-1998  mycroft Use the uid/gid caching moved into libc. Fixed a memory leak!
 1.12 27-Jul-1998  mycroft __AUDIT__ cleanup.
 1.11 26-Jul-1998  mycroft const poisoning.
 1.10 01-Apr-1998  kleink Need <time.h> for strftime(), localtime() and time() prototypes.
 1.9 03-Feb-1998  mycroft Deal with GCC warnings.
 1.8 30-Jul-1997  thorpej Cast off_t's to (long long) for printing with %q*, for the Alpha.
 1.7 20-Jul-1997  christos - Rename local warn so tty_warn that we can include <err.h> since this program
uses err() and errx().
- Fix printf format strings.
- Added WARNS=1; note
- Added missing prototypes.
- Split overloaded trail function into trail() and subtrail().
 1.6 11-Jan-1997  tls kill 'register'
 1.5 21-Mar-1995  cgd convert to new RCS id conventions.
 1.4 23-Sep-1994  mycroft Eliminate uses of some obsolete functions.
 1.3 14-Jun-1994  mycroft Fix up RCS ids.
 1.2 13-Jun-1994  jtc Add RCS ID's
 1.1 13-Jun-1994  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 13-Jun-1994  jtc From 4.4 lite
 1.15.2.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.17.2.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.18.2.1 25-Oct-2000  tv Pullup 1.19 [kleink]:
Don't abuse LC_TIME for strftime(3) format strings.
 1.23.2.4 25-Aug-2004  he Apply patch (requested by cube in ticket #1738):
Minimize difference with -current and netbsd-2-0 version.
 1.23.2.3 16-Jun-2004  jmc fix botched pullup from ticket #1021 and make sure this compiles on
non-NetBSD hosts again. Also fixes PR#24481
 1.23.2.2 07-Apr-2004  jmc Pullup rev 1.24-1.26,1.28-1.32 (requested by rafal in ticket #1021)

Pullup pax to current version on trunk. Includes many fixes.
 1.23.2.1 05-Jan-2003  jmc Pull up revisions 1.26-1.27 (via hand patch and requested by kleink in ticket
#1040)
Change verbose output for symbolic links from "%s => %s" to
"%s -> %s", which matches ls(1) (per POSIX), as well as GNU
tar output.
 1.33.16.1 24-Mar-2008  keiichi sync with head.
 1.33.10.1 23-Mar-2008  matt sync with HEAD
 1.35.2.1 30-Oct-2012  yamt sync with head
 1.36.34.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.36.34.2 21-Apr-2020  martin Sync with HEAD
 1.36.34.1 10-Jun-2019  christos Sync with HEAD
 1.36.32.1 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.24 03-Oct-2024  rillig bin: fix lint warning "effectively discards 'const'"

For example: src/bin/ed/io.c(339): warning: call to 'strchr' effectively
discards 'const' from argument [346]

No binary change.
 1.23 09-Aug-2012  christos branches: 1.23.34; 1.23.46;
add missing include files
 1.22 11-Feb-2006  dsl branches: 1.22.42;
Change all the 'return(x)' to 'return c'.
Makes some other searches I want to do a bit easier.
 1.21 05-Jun-2005  chs appease gcc -Wuninitialized on hp700.
 1.20 20-Jun-2004  jmc Completely rework how tools/compat is done. Purge all uses/references to
_NETBSD_SOURCE as this makes cross building from older/newer versions of
NetBSD harder, not easier (and also makes the resulting tools 'different')

Wrap all required code with the inclusion of nbtool_config.h, attempt to
only use POSIX code in all places (or when reasonable test w. configure and
provide definitions: ala u_int, etc).

Reviewed by lukem. Tested on FreeBSD 4.9, Redhat Linux ES3, NetBSD 1.6.2 x86
NetBSD current (x86 and amd64) and Solaris 9.

Fixes PR's: PR#17762 PR#25944
 1.19 27-Oct-2003  lukem branches: 1.19.2;
Overhaul how `build.sh tools' are used:

* Rename "config.h" to "nbtool_config.h" and
HAVE_CONFIG_H to HAVE_NBTOOL_CONFIG_H.
This makes in more obvious in the source when we're using
tools/compat/config.h versus "standard autoconf" config.h

* Consistently move the inclusion of nbtool_config.h to before
<sys/cdefs.h> so that the former can provide __RCSID() (et al),
and there's no need to protect those macros any more.

These changes should make it easier to "tool-ify" a program by adding:
#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
to the top of the source files (for the general case).
 1.18 24-Jun-2003  christos Revert previous change, and fix the -T problem differently: When the options
of the second argument are exhausted, call the appropriate getopt() routine
to process the rest of the arguments instead of finishing option processing.
Fixes:
tar cf - -T foo
 1.17 23-Jun-2003  christos Simplify the way we deal with old argument style. Old way was broken when
tar cf - -T dir
 1.16 13-Oct-2002  mrg add a "+" to the options string rather than setting POSIXLY_CORRECT
 1.15 12-Oct-2002  christos merge OpenBSD changes:
- correct -C processing
- add ability to read filenames and flags from a file
- don't print dangerous escape sequences to the terminal
- use strlcpy/strncpy properly.
- handle tmpfile creation better.
- improve documentation of options.
- handle stdout/stderr list selection correctly.
- kill gzip when we get interrupted.
- simplify gzip setup.
- add more flags to programs.

additional changes:
- librmt processing.
- set POSIXLY_CORRECT in options parsing.
- prevent more string overruns.
- support -T

we don't turn the switch on to replace tar and cpio yet.
 1.14 31-Jan-2002  tv branches: 1.14.2;
Roll in fixes to permit cross-compiling from non-NetBSD hosts. This
round has been tested on Solaris/x86 and Linux hosts.

* Add host tools cap_mkdb, ctags, m4, uudecode.
* Protect __RCSID() and __COPYRIGHT() better.
* Reduce the number of places that need to include "config.h", to keep
sources closer to their "vanilla" versions.
* Add more compat #defines and autoconf-checked functions.
 1.13 29-Jan-2002  tv Make almost all tools compile and run properly on non-NetBSD hosts. (In
particular, most tools now run correctly on Solaris 7.)
 1.12 03-Nov-2001  lukem fix WARNS=2
 1.11 25-Oct-2001  lukem - ansi KNF (just remove all the #ifndef __STDC__ prototype junk,
leaving the ansi stuff)
- use longlong_t instead of quad_t (etc), and rename *uqd*() -> *ull*()
- clean up the NET2_STAT stuff similar to ftpd; provide #defines and
macros which select which cast to use, etc
- clean up the NET2_FTS and NET2_REGEX #define use
 1.10 04-Jul-2000  thorpej Add GNU tar-style long options for pax's tar front-end. Still many
GNU options not implemented, but there is an #if 0'd out canonical
list.
 1.9 14-Apr-2000  simonb Don't declare 'extern opt*' getopt variables.
 1.8 17-Feb-2000  itohy Cleanup and fix typos.
Partially from FreeBSD.
 1.7 01-Nov-1997  lukem getoldopt is expect to return -1 (not EOF), so "make it so"
 1.6 14-Sep-1997  lukem * cleanup manpage
* getopt returns -1 not EOF
* deprecate register
 1.5 20-Jul-1997  christos - Rename local warn so tty_warn that we can include <err.h> since this program
uses err() and errx().
- Fix printf format strings.
- Added WARNS=1; note
- Added missing prototypes.
- Split overloaded trail function into trail() and subtrail().
 1.4 17-May-1996  jtc Fixed typo in comment
 1.3 21-Mar-1995  cgd convert to new RCS id conventions.
 1.2 04-Dec-1994  cgd from James Jegers <jimj@miller.cs.uwm.edu>: quiet -Wall, and squelch
some of the worst style errors.
 1.1 14-Jun-1994  jtc Whoever wrote the tar command line argument parsing code didn't realize
that the order of arguments is different if the command flags are specified
without a `-'. I've integrated getoldopt.c from John Gilmore's pdtar which
handles argument parsing correctly.
 1.14.2.2 16-Jun-2004  jmc fix botched pullup from ticket #1021 and make sure this compiles on
non-NetBSD hosts again. Also fixes PR#24481
 1.14.2.1 07-Apr-2004  jmc Pullup rev 1.15-1.19 (requested by rafal in ticket #1021)

Pullup pax to current version on trunk. Includes many fixes.
 1.19.2.1 22-Jun-2004  tron branches: 1.19.2.1.2;
Pull up revision 1.20 (requested by jmc in ticket #527):
Completely rework how tools/compat is done. Purge all uses/references to
_NETBSD_SOURCE as this makes cross building from older/newer versions of
NetBSD harder, not easier (and also makes the resulting tools 'different')
Wrap all required code with the inclusion of nbtool_config.h, attempt to
only use POSIX code in all places (or when reasonable test w. configure and
provide definitions: ala u_int, etc).
Reviewed by lukem. Tested on FreeBSD 4.9, Redhat Linux ES3, NetBSD 1.6.2 x86
NetBSD current (x86 and amd64) and Solaris 9.
Fixes PR's: PR#17762 PR#25944
 1.19.2.1.2.1 23-Jul-2005  snj Apply patch (requested by riz in ticket #5512):
Sync pax with HEAD of 2005-07-22.
 1.22.42.1 30-Oct-2012  yamt sync with head
 1.23.46.1 02-Aug-2025  perseant Sync with HEAD
 1.23.34.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.23.34.1 21-Apr-2020  martin Sync with HEAD
 1.122 04-May-2025  rillig pax: remove unreachable 'break' and 'return' statements
 1.121 05-Aug-2024  riastradh pax(1): Nix trailing whitespace.

No functional change intended.
 1.120 20-Jul-2023  lukem branches: 1.120.2;
pax: need <strings.h> for ffs()

Fixes warning as host tool on NetBSD 9.99
 1.119 03-Apr-2020  joerg branches: 1.119.6;
Move definition of chdname and do_chroot to options.c
 1.118 19-Dec-2015  christos branches: 1.118.8; 1.118.16; 1.118.18;
Add the timestamp option to the pax front end.
 1.117 19-Dec-2015  christos PR/50119: Thomas Klausner: Add --timestamp option to tar.
 1.116 11-Apr-2015  christos Add -J/--xz to specifically decompress xz compressed files. We don't really
need this because -z autodetects the compression format; this is for syntax
compatibility with other tar implementations. From Joachim Henke
 1.115 14-Nov-2013  christos add --gnu for pax.
 1.114 09-Aug-2012  christos branches: 1.114.2;
add missing include files
 1.113 20-Mar-2012  matt Use C89 function definitions
 1.112 31-Aug-2011  plunky branches: 1.112.2;
NULL does not need a cast
 1.111 29-Aug-2011  joerg static + __dead
 1.110 18-Jun-2011  christos add --xz
 1.109 31-Aug-2010  enami branches: 1.109.4;
- Raise an error rather than silently creating broken archive
if user don't specify --force-local but opened file is actually
a local file.
- Make cpio to accept -F option as described in manpage.
- Make pax to set forcelocal flag if requested to do so.
- Add missing break statement.
 1.108 25-Aug-2010  sjg Do not throw a usage message for -i followed by -t
since 'cpio -itv' is a valid command.
 1.107 19-Jun-2010  christos add --use-compress-program to pax, requested by mrg.
 1.106 14-Dec-2009  dholland Don't use NOGLOB_MTCH for pax, only for tar. Fixes fix for PR 41167 and
closes PR 42301. Since the 41167 fix was pulled up to -5, this needs to
be too.
 1.105 13-Jul-2009  roy Rename internal getline() function to get_line() so it does
conflict with the soon to be added getline(3) libc function.
 1.104 07-Apr-2009  perry Fixes from christos for pr-41167

XXX -X is still broken, but that wasn't part of this PR.
 1.103 14-Feb-2009  lukem fix -Wsign-compare issues
 1.102 23-Nov-2008  dholland branches: 1.102.2;
Don't pass strings from argv[] to free(). Fixes PR 39992.
 1.101 26-Oct-2007  hira branches: 1.101.12;
- Add -0 and -V to pax_usage().
- Add -k to tar_usage().
 1.100 23-Apr-2007  christos branches: 1.100.4;
PR/36194: Greg A. Woods: add a '-V' flag to pax for verbose summary without
listing (and other minor fixes)
 1.99 16-Jan-2007  cbiere Fix for PR bin/35432:
* The flags "crtux" are mutually exclusive for tar.
* The flags "ipt" and "oA" are mutually exclusive for cpio.
 1.98 16-Oct-2006  christos branches: 1.98.2; 1.98.6;
add missing inititializer
 1.97 16-Apr-2006  christos PR/22995: Sergey Svishchev: If we are cd'ing around and we have directories
with relative paths, convert the directories to absolute paths, so that
restoring timestamps does not fail.
 1.96 14-Apr-2006  christos Don't free str; it is used in pat_add. Noted by: Kouichirou Hiratsuka
 1.95 19-Mar-2006  christos Coverity CID 2734, 2731: Use after free; resource leak
 1.94 18-Mar-2006  christos Coverity CID 345: Add missing free's.
 1.93 11-Feb-2006  dsl Change all the 'return(x)' to 'return c'.
Makes some other searches I want to do a bit easier.
 1.92 16-Sep-2005  christos Another missing mkdir() race pointed out by chuq. Factor out the mkdir code.
 1.91 13-Sep-2005  christos use stat and not lstat to detect if we have a directory. refactor the mkpath()
code.
 1.90 13-Sep-2005  christos when creating directories, check if the failure occured because someone
else created the directory before we did to avoid races. From chuq.
 1.89 29-Jun-2005  christos Don't compile in cpio usage, if we are SMALL.
 1.88 01-Jun-2005  lukem appease gcc -Wuninitialized
 1.87 15-May-2005  christos be more lenient on the comparison to argv[0] to determine our personality.
 1.86 07-May-2005  christos PR/30167: J.T. Conklin: NetBSD tar does not support GNU tar --no-recursion flag
 1.85 01-May-2005  christos PR/29005: Jesse Off: nbpax requires sys/mtio.h which doesn't exist on Interix
I added an ifdef called HAVE_MTIO_H and fixed the code to use it. It is
always defined now, but you can change this for Interix.
 1.84 24-Apr-2005  christos Pax longopts were not empty element terminated.
 1.83 10-Feb-2005  jmc Fix from PR#29290. Properly terminate the cpio_longopts struct so an unknown
option doesn't run off the end and core dump
 1.82 26-Oct-2004  tron Fix broken cpio(1) option handling:
- "cpio -i -t" should list the contents of a file, not extract it.
- Don't extract a file when only option "-d" is given.
Patch supplied by Paul Ripke in PR bin/26513.
 1.81 22-Oct-2004  jmc For HOSTTOOL builds, just exclude the chroot support as it's not needed and
avoids having to platform check for fchroot(). Fixes PR#27336
 1.80 17-Oct-2004  dsl Add an option --chroot to tar. Causes it to chroot(".") before doing
an extract. With -h this will cause existing absolute symlinks to be treated
as relative to the current directory.
Helps sysinst handle existing symlinks in the target system.
Remove 'L' from the usage (got spilt into 'h' and 'H' many moons ago)
Add 'S' to usage, and put into correct place in options list.
 1.79 10-Oct-2004  christos PR/27212: Greg A. Woods: Accept "-C <dirname>" inside filelists in addition
to "-C\n<dirname>".
But we are not making it the default output option as the patch suggests.
 1.78 26-Sep-2004  christos PR/19490: Julio Merino: Teach tar about --sparse, -S option.
Do it for cpio too.
 1.77 22-Sep-2004  christos update the comment that documents the fields initialized in the array below.
 1.76 20-Jun-2004  jmc Completely rework how tools/compat is done. Purge all uses/references to
_NETBSD_SOURCE as this makes cross building from older/newer versions of
NetBSD harder, not easier (and also makes the resulting tools 'different')

Wrap all required code with the inclusion of nbtool_config.h, attempt to
only use POSIX code in all places (or when reasonable test w. configure and
provide definitions: ala u_int, etc).

Reviewed by lukem. Tested on FreeBSD 4.9, Redhat Linux ES3, NetBSD 1.6.2 x86
NetBSD current (x86 and amd64) and Solaris 9.

Fixes PR's: PR#17762 PR#25944
 1.75 19-Jun-2004  christos add -0 argument to handle nul separated file lists.
 1.74 11-May-2004  christos Welcome to WARNS=3
 1.73 20-Feb-2004  uebayasi branches: 1.73.2;
Tar(1)'s -s option needs an argument. Correct option string (s -> s:).

Reported and tested by Tomoaki Imamura.
 1.72 06-Feb-2004  christos recognize -k [--keep-old-files] short option. From hubertf.
 1.71 05-Jan-2004  jmmv Homogenize usage messages: make the 'usage' word all lowercase, as this seems
to be the most common practice in our tree.
 1.70 05-Jan-2004  jmmv Rewrite the loop in printflg(): if passing -Z to pax with syntax errors
(like executing "pax -Z" by itself), this caused a shr of 32 bits, which is
undefined behavior (C99) if the variable is 32 bits wide, too. Also solves
a problem where the flgch array could be indexed out of bounds.

Thanks to uwe@ and lha@ for their suggestions... I just found the bug :p
 1.69 27-Oct-2003  lukem Overhaul how `build.sh tools' are used:

* Rename "config.h" to "nbtool_config.h" and
HAVE_CONFIG_H to HAVE_NBTOOL_CONFIG_H.
This makes in more obvious in the source when we're using
tools/compat/config.h versus "standard autoconf" config.h

* Consistently move the inclusion of nbtool_config.h to before
<sys/cdefs.h> so that the former can provide __RCSID() (et al),
and there's no need to protect those macros any more.

These changes should make it easier to "tool-ify" a program by adding:
#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
to the top of the source files (for the general case).
 1.68 21-Oct-2003  fvdl Don't pass an integer as NULL.
 1.67 13-Oct-2003  agc Move Keith Muller's code from a 4-clause to a 3-clause licence by removing
the advertising clause. Diffs provided in PR 22397 by Joel Baker, confirmed
to the board by Keith Muller.
 1.66 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.65 08-Jul-2003  simonb Add support for gnutar's -O "extract to stdout" option.
 1.64 08-Apr-2003  christos PR/21064: Perry Metzger: Re-add -k to tar (--keep-old-files)
 1.63 31-Mar-2003  christos PR/20495: Soren Jacobsen: Add -j [bzip2] compression to tar.
 1.62 25-Feb-2003  wiz Add support for '-j' to create (and extract, but -z already did that)
bzip2 compressed tar files, for GNU tar compatibility.
Patch from Soren Jacobsen in PR 19467, okayed by christos.
Sync usage with options while here.
 1.61 02-Feb-2003  wiz Fix various spelling, grammar, and punctuation problems in comments, from Sergey Svishchev in PR 20164.
 1.60 19-Jan-2003  wiz tar(1) has no -L any longer.
 1.59 15-Jan-2003  kristerw Correct which _usage function to use for some pax/cpio error cases.
 1.58 09-Jan-2003  christos PR/19757: Andreas Wrede: make tar's -h behave like pax's -L
 1.57 08-Dec-2002  mrg add --exclude support.

XXX: --exclude & --exclude-from are still not 100% compatible, but since
rafal fixed --exclude-from's most annoying bug, they mostly work now.
 1.56 27-Nov-2002  grant void functions should not return a value.
 1.55 18-Oct-2002  christos Handle -C\ndir\n in a file containing a file list. From Eric Gillespie.
 1.54 17-Oct-2002  christos Fix DEFOP botch. DEFOP == LIST so tar -tvf stopped working. (hi soren)
 1.53 16-Oct-2002  soren Tweak tar_usage().
 1.52 16-Oct-2002  soren Have tar require a specific action as documented.
 1.51 16-Oct-2002  soren Sync cpio_usage() with cpio.1 as with pax and tar.
 1.50 16-Oct-2002  christos we need to have -force-local on pax.
 1.49 16-Oct-2002  christos turn gnu tar off by pax and cpio front ends.
 1.48 16-Oct-2002  christos Support gnu long filename extensions by default for tar on create, list, and
extract. We now generate GNU tar archives by default ("ustar ^@" instead of
"ustar^@00"). GNU extensions can be disabled with --strict.

XXX: long symlinks untested.
 1.47 15-Oct-2002  christos PR/18663: Jeremy Reed: pax/tar/cpio allows ".." in names.
We now disallow it by default on both archive creation and extraction.
Add --insecure option to override.
 1.46 15-Oct-2002  christos implement fast read. nothing to it really, it was already there as -q option.
 1.45 15-Oct-2002  christos Add all the gnu cpio long options.
 1.44 14-Oct-2002  soren Sync tar_usage() with tar.1.
 1.43 13-Oct-2002  christos -T expects an argument. From Eric Gillespie, thanks.
 1.42 13-Oct-2002  mrg fix "pax -rw" processing. for some reason, it was now written to process
"pax -rwvpe a b" as "pax -rwvpe b a", and "pax -rwvpe a b c" as
"pax -rwvpe b c a". only the part of revision 1.40 that broken this has
been reverted.
 1.41 13-Oct-2002  mrg add a "+" to the options string rather than setting POSIXLY_CORRECT
 1.40 12-Oct-2002  christos merge OpenBSD changes:
- correct -C processing
- add ability to read filenames and flags from a file
- don't print dangerous escape sequences to the terminal
- use strlcpy/strncpy properly.
- handle tmpfile creation better.
- improve documentation of options.
- handle stdout/stderr list selection correctly.
- kill gzip when we get interrupted.
- simplify gzip setup.
- add more flags to programs.

additional changes:
- librmt processing.
- set POSIXLY_CORRECT in options parsing.
- prevent more string overruns.
- support -T

we don't turn the switch on to replace tar and cpio yet.
 1.39 02-Feb-2002  lukem branches: 1.39.2;
- don't compile in support for -M (mtree specfile input) or -N dbdir
(alternate directory for id info) if SMALL is defined
- enable -DSMALL and remove unused objects if CRUNCHEDPROG is defined
- minor cleanup in next_file() (preparation for future work)
 1.38 31-Jan-2002  tv Roll in fixes to permit cross-compiling from non-NetBSD hosts. This
round has been tested on Solaris/x86 and Linux hosts.

* Add host tools cap_mkdb, ctags, m4, uudecode.
* Protect __RCSID() and __COPYRIGHT() better.
* Reduce the number of places that need to include "config.h", to keep
sources closer to their "vanilla" versions.
* Add more compat #defines and autoconf-checked functions.
 1.37 29-Jan-2002  tv Make almost all tools compile and run properly on non-NetBSD hosts. (In
particular, most tools now run correctly on Solaris 7.)
 1.36 26-Jan-2002  lukem provide meaningful warning if -N is called with a dodgy dbdir
 1.35 24-Jan-2002  lukem Add '-N dbdir', to specify that user & group lookups (via pwcache(3)) are to
be done from dbdir/master.passwd and dbdir/group, using simple text file
parsing routines from ../../usr.sbin/mtree/getid.c.
This does not affect the -G and -U options, which always use the
system databases.
 1.34 25-Oct-2001  lukem Implement -M flag: During a write or copy operation, treat the list of
files on standard input as an mtree(8) `specfile' specification, and
write or copy only those items in the specfile.

If the file exists in the underlying file system, its permissions and
modification time will be used unless specifically overridden by the
specfile. An error will be raised if the type of entry in the specfile
conflicts with that of an existing file.

Otherwise, it is necessary to specify at least the following parameters
in the specfile: type, mode, gname or gid, and uname or uid, device
(in the case of block or character devices), and link (in the case of
symbolic links). If time isn't provided, the current time will be used.
 1.33 25-Oct-2001  lukem - ansi KNF (just remove all the #ifndef __STDC__ prototype junk,
leaving the ansi stuff)
- use longlong_t instead of quad_t (etc), and rename *uqd*() -> *ull*()
- clean up the NET2_STAT stuff similar to ftpd; provide #defines and
macros which select which cast to use, etc
- clean up the NET2_FTS and NET2_REGEX #define use
 1.32 22-Jul-2001  wiz seperate -> separate
 1.31 04-Jul-2000  thorpej Implement --atime-preserve.
 1.30 04-Jul-2000  thorpej Implement --use-compress-program.
 1.29 04-Jul-2000  thorpej Add GNU tar-style long options for pax's tar front-end. Still many
GNU options not implemented, but there is an #if 0'd out canonical
list.
 1.28 14-Apr-2000  simonb Don't declare 'extern opt*' getopt variables.
 1.27 30-Mar-2000  thorpej Add a -O option: force one volume. This causes pax to not prompt for
a new volume upon premature end-of-volume (e.g. broken pipe). This is
especially useful in automated environments where error recovery cannot
be performed by a human.
 1.26 17-Feb-2000  itohy Cleanup and fix typos.
Partially from FreeBSD.
 1.25 07-Nov-1999  mycroft branches: 1.25.2;
Disable -pf for now, too.
 1.24 01-Nov-1999  mrg add a new -p subflag: 'f' to preserve 4.4BSD fileflags. this only
really works for `pax -rw' but that's where it is most useful.
 1.23 22-Oct-1999  is Implement positional -C argument to tar.
 1.22 24-Aug-1999  tron branches: 1.22.2;
Fix "tar" mode usage string.
 1.21 24-Aug-1999  tron Recognize GnuTAR's "h" (instead of "H") and "P" options.
 1.20 24-Aug-1999  tron Strip leading slashes from filenames by default, add new "A" option to
disable this behaviour. Patches supplied by Peter Seebach in PR bin/8233.
 1.19 07-Mar-1999  mycroft Add -z option to usage message.
 1.18 07-Feb-1999  tv Fix opt_add to take const char * so that it can be passed a constant
string legally (it strdup()s the argument). How pax-as-tar `-o' ever
worked without a coredump is beyond me...

Also modify pax-as-tar `-o' to do three things, which depend on the
create/extract mode:

- write V7 format archives (which, though part of GNU tar, actually
goes along with the following point--after all, old pax-as-tar created
V7 archives by default);
- write archives with "write_opt=nodir", as pax already did, and as
specified by 4.2BSD;
- extract archives with owner/group set to invoking user, as specified
by SUS.
 1.17 02-Feb-1999  tv Lost 5 lines in the version that was committed. Re-add them.
 1.16 02-Feb-1999  tv Uh, when called as "tar" we should still default to "ustar" output, not
V7 tar format. Also change meaning of pax-as-tar option "-o" to mean
"use V7 output format" (same as GNU tar's -o).
 1.15 20-Jan-1999  mrg implement the gnutar -X flag in terms of pax -s. fix some bugs in pax -s handling while i am here...
 1.14 28-Jul-1998  mycroft Use the uid/gid caching moved into libc. Fixed a memory leak!
 1.13 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.12 28-Jul-1998  mycroft Delint.
 1.11 09-Jun-1998  mrg oops, fix broken cpio options code. tar options code should be made like pax and cpio code.
 1.10 06-Mar-1998  mrg - implement -C and -l for pax-as-tar, as inspried by GNU tar.
- minor cleanups to the options parsing code.
- add a cpio frontend.

note: a few GNU/svr4 cpio options are not supported yet (#ifdef
notyet), however all x/open ones are.
 1.9 14-Sep-1997  lukem * cleanup manpage
* getopt returns -1 not EOF
* deprecate register
 1.8 20-Jul-1997  christos - Rename local warn so tty_warn that we can include <err.h> since this program
uses err() and errx().
- Fix printf format strings.
- Added WARNS=1; note
- Added missing prototypes.
- Split overloaded trail function into trail() and subtrail().
 1.7 11-Jan-1997  tls kill 'register'
 1.6 26-Mar-1996  mrg impliment -z (gzip) in pax and tar, and -Z (compress) in tar.
 1.5 21-Mar-1995  cgd convert to new RCS id conventions.
 1.4 14-Jun-1994  jtc Whoever wrote the tar command line argument parsing code didn't realize
that the order of arguments is different if the command flags are specified
without a `-'. I've integrated getoldopt.c from John Gilmore's pdtar which
handles argument parsing correctly.
 1.3 14-Jun-1994  mycroft Fix up RCS ids.
 1.2 13-Jun-1994  jtc Add RCS ID's
 1.1 13-Jun-1994  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 13-Jun-1994  jtc From 4.4 lite
 1.22.2.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.25.2.2 07-Nov-1999  mycroft Disable -pf for now, too.
 1.25.2.1 07-Nov-1999  mycroft file options.c was added on branch comdex-fall-1999 on 1999-11-07 15:57:32 +0000
 1.39.2.2 16-Jun-2004  jmc fix botched pullup from ticket #1021 and make sure this compiles on
non-NetBSD hosts again. Also fixes PR#24481
 1.39.2.1 07-Apr-2004  jmc Pullup rev 1.40-1.73 (requested by rafal in ticket #1021)

Pullup pax to current version on trunk. Includes many fixes.
 1.73.2.1 22-Jun-2004  tron branches: 1.73.2.1.2;
Pull up revision 1.76 (requested by jmc in ticket #527):
Completely rework how tools/compat is done. Purge all uses/references to
_NETBSD_SOURCE as this makes cross building from older/newer versions of
NetBSD harder, not easier (and also makes the resulting tools 'different')
Wrap all required code with the inclusion of nbtool_config.h, attempt to
only use POSIX code in all places (or when reasonable test w. configure and
provide definitions: ala u_int, etc).
Reviewed by lukem. Tested on FreeBSD 4.9, Redhat Linux ES3, NetBSD 1.6.2 x86
NetBSD current (x86 and amd64) and Solaris 9.
Fixes PR's: PR#17762 PR#25944
 1.73.2.1.2.3 23-Jul-2005  snj Apply patch (requested by riz in ticket #5512):
Sync pax with HEAD of 2005-07-22.
 1.73.2.1.2.2 29-May-2005  riz Pull up revision 1.83 (requested by kleink in ticket #1554):
Fix from PR#29290. Properly terminate the cpio_longopts struct so an unknown
option doesn't run off the end and core dump
 1.73.2.1.2.1 30-Jan-2005  he Pull up revision 1.82 (requested by tron in ticket #952):
Fix broken cpio(1) option handling:
o "cpio -i -t" should list the contents of a file, not
extract it.
o Don't extract a file when only option "-d" is given.
Fixes PR#26513.
 1.98.6.1 25-Aug-2009  snj Pull up following revision(s) (requested by plunky in ticket #1354):
bin/pax/options.c: revision 1.105 via patch
usr.bin/ctags/C.c: revision 1.19
usr.bin/ctags/ctags.h: revision 1.9
usr.bin/ctags/fortran.c: revision 1.11
usr.bin/ctags/lisp.c: revision 1.11
usr.bin/ctags/print.c: revision 1.10
usr.bin/ctags/yacc.c: revision 1.12
usr.bin/gencat/gencat.c: revision 1.30 via patch
Rename internal getline() function to get_line() so it does not
conflict with the soon to be added getline(3) libc function.
 1.98.2.1 25-Aug-2009  snj Pull up following revision(s) (requested by plunky in ticket #1354):
bin/pax/options.c: revision 1.105 via patch
usr.bin/ctags/C.c: revision 1.19
usr.bin/ctags/ctags.h: revision 1.9
usr.bin/ctags/fortran.c: revision 1.11
usr.bin/ctags/lisp.c: revision 1.11
usr.bin/ctags/print.c: revision 1.10
usr.bin/ctags/yacc.c: revision 1.12
usr.bin/gencat/gencat.c: revision 1.30 via patch
Rename internal getline() function to get_line() so it does
conflict with the soon to be added getline(3) libc function.
 1.100.4.1 06-Nov-2007  matt sync with HEAD
 1.101.12.4 30-Jan-2010  snj Pull up following revision(s) (requested by dholland in ticket #1272):
bin/pax/options.c: revision 1.106
Don't use NOGLOB_MTCH for pax, only for tar. Fixes fix for PR 41167 and
closes PR 42301. Since the 41167 fix was pulled up to -5, this needs to
be too.
 1.101.12.3 14-Aug-2009  snj Pull up following revision(s) (requested by roy in ticket #885):
bin/pax/options.c: revision 1.105
dist/nawk/maketab.c: revision 1.12
dist/nawk/proctab.c: revision 1.11
dist/nawk/proto.h: revision 1.9
dist/nawk/run.c: revision 1.29
usr.bin/ctags/C.c: revision 1.19
usr.bin/ctags/ctags.h: revision 1.9
usr.bin/ctags/fortran.c: revision 1.11
usr.bin/ctags/lisp.c: revision 1.11
usr.bin/ctags/print.c: revision 1.10
usr.bin/ctags/yacc.c: revision 1.12
usr.bin/gencat/gencat.c: revision 1.30
Rename internal getline() function to get_line() so it does
conflict with the soon to be added getline(3) libc function.
 1.101.12.2 13-Apr-2009  snj branches: 1.101.12.2.2; 1.101.12.2.4;
Pull up following revision(s) (requested by christo in ticket #678):
bin/pax/extern.h: revision 1.56
bin/pax/file_subs.c: revision 1.62
bin/pax/options.c: revision 1.104
bin/pax/pat_rep.c: revision 1.29
bin/pax/pax.h: revision 1.30
Fixes from christos for pr-41167
XXX -X is still broken, but that wasn't part of this PR.
 1.101.12.1 23-Nov-2008  riz Pull up following revision(s) (requested by dholland in ticket #114):
bin/pax/options.c: revision 1.102
Don't pass strings from argv[] to free(). Fixes PR 39992.
 1.101.12.2.4.1 21-Apr-2010  matt sync to netbsd-5
 1.101.12.2.2.1 14-Aug-2009  snj Pull up following revision(s) (requested by roy in ticket #885):
bin/pax/options.c: revision 1.105
dist/nawk/maketab.c: revision 1.12
dist/nawk/proctab.c: revision 1.11
dist/nawk/proto.h: revision 1.9
dist/nawk/run.c: revision 1.29
usr.bin/ctags/C.c: revision 1.19
usr.bin/ctags/ctags.h: revision 1.9
usr.bin/ctags/fortran.c: revision 1.11
usr.bin/ctags/lisp.c: revision 1.11
usr.bin/ctags/print.c: revision 1.10
usr.bin/ctags/yacc.c: revision 1.12
usr.bin/gencat/gencat.c: revision 1.30
Rename internal getline() function to get_line() so it does
conflict with the soon to be added getline(3) libc function.
 1.102.2.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.109.4.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.112.2.3 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.112.2.2 30-Oct-2012  yamt sync with head
 1.112.2.1 17-Apr-2012  yamt sync with head
 1.114.2.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.118.18.2 19-Oct-2025  martin Pull up following revision(s) (requested by mrg in ticket #1973):

tools/compat/configure.ac: revision 1.111
tools/compat/compat_defs.h: revision 1.126
tools/compat/compat_defs.h: revision 1.127
usr.bin/nbperf/nbperf.c: revision 1.7
bin/pax/options.c: revision 1.120
tools/compat/nbtool_config.h.in: revision 1.59 (patch)
tools/compat/Makefile: revision 1.95 (patch)
tools/compat/configure.ac: revision 1.110 (patch)
tools/compat/configure (patch for regen from above)

Don't use arc4random for the tools build on NetBSD either.
compat_defs.h explicitly forces _POSIX_SOURCE and this hides the
prototype in stdlib.h.
pax: need <strings.h> for ffs()

Fixes warning as host tool on NetBSD 9.99

Add more functions needed by mandoc

add vsyslog decl check
 1.118.18.1 30-Apr-2021  martin Pull up following revision(s) (requested by mrg in ticket #1263):

bin/pax/options.c: revision 1.119
usr.bin/config/scan.l: revision 1.32
usr.bin/make/main.c: revision 1.274
bin/pax/extern.h: revision 1.60
usr.bin/config/defs.h: revision 1.106
usr.bin/make/make.h: revision 1.105
sbin/newfs_udf/udf_create.c: revision 1.27 (patch)
sbin/newfs_udf/newfs_udf.h: revision 1.7 (patch)
usr.bin/config/main.c: revision 1.100

Avoid depending on common symbols.
 1.118.16.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.118.16.2 21-Apr-2020  martin Sync with HEAD
 1.118.16.1 08-Apr-2020  martin Merge changes from current as of 20200406
 1.118.8.1 07-May-2021  martin Pull up following revision(s) (requested by mrg in ticket #1678):

bin/pax/options.c: revision 1.119
usr.bin/config/scan.l: revision 1.32
usr.bin/make/main.c: revision 1.274
bin/pax/extern.h: revision 1.60
usr.bin/config/defs.h: revision 1.106
usr.bin/make/make.h: revision 1.105
sbin/newfs_udf/udf_create.c: revision 1.27 (patch)
sbin/newfs_udf/newfs_udf.h: revision 1.7 (patch)
usr.bin/config/main.c: revision 1.100
usr.sbin/installboot/Makefile: revisions 1.53,1.54 (patch)
usr.sbin/installboot/installboot.h: revision 1.41 (patch)
usr.sbin/installboot/machines.c: revisisons 1.41,1.42 (patch)

Avoid depending on common symbols.
 1.119.6.1 19-Oct-2025  martin Pull up following revision(s) (requested by mrg in ticket #1174):

tools/compat/configure.ac: revision 1.111 (patch)
tools/compat/compat_defs.h: revision 1.126 (patch)
tools/compat/compat_defs.h: revision 1.127 (patch)
bin/pax/options.c: revision 1.120
tools/compat/nbtool_config.h.in: revision 1.59 (patch)
tools/compat/Makefile: revision 1.95 (patch)
tools/compat/configure.ac: revision 1.110 (patch)

pax: need <strings.h> for ffs()

Fixes warning as host tool on NetBSD 9.99

Add more functions needed by mandoc

add vsyslog decl check
 1.120.2.1 02-Aug-2025  perseant Sync with HEAD
 1.11 23-Apr-2007  christos branches: 1.11.78;
PR/36194: Greg A. Woods: add a '-V' flag to pax for verbose summary without
listing (and other minor fixes)
 1.10 11-May-2004  christos Welcome to WARNS=3
 1.9 13-Oct-2003  agc branches: 1.9.4;
Move Keith Muller's code from a 4-clause to a 3-clause licence by removing
the advertising clause. Diffs provided in PR 22397 by Joel Baker, confirmed
to the board by Keith Muller.
 1.8 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.7 25-Oct-2001  lukem branches: 1.7.2;
Implement -M flag: During a write or copy operation, treat the list of
files on standard input as an mtree(8) `specfile' specification, and
write or copy only those items in the specfile.

If the file exists in the underlying file system, its permissions and
modification time will be used unless specifically overridden by the
specfile. An error will be raised if the type of entry in the specfile
conflicts with that of an existing file.

Otherwise, it is necessary to specify at least the following parameters
in the specfile: type, mode, gname or gid, and uname or uid, device
(in the case of block or character devices), and link (in the case of
symbolic links). If time isn't provided, the current time will be used.
 1.6 01-Nov-1999  mrg whoops; reverse the logic of the previous to make it correct. noted by mycroft.
 1.5 01-Nov-1999  mrg add CAF testing to extraction, archiving and listing (where it is used)
 1.4 24-Aug-1999  tron branches: 1.4.2;
Strip leading slashes from filenames by default, add new "A" option to
disable this behaviour. Patches supplied by Peter Seebach in PR bin/8233.
 1.3 21-Mar-1995  cgd convert to new RCS id conventions.
 1.2 13-Jun-1994  jtc Add RCS ID's
 1.1 13-Jun-1994  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 13-Jun-1994  jtc From 4.4 lite
 1.4.2.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.7.2.1 07-Apr-2004  jmc Pullup rev 1.8-1.9 (requested by rafal in ticket #1021)

Pullup pax to current version on trunk. Includes many fixes.
 1.9.4.1 23-Jul-2005  snj Apply patch (requested by riz in ticket #5512):
Sync pax with HEAD of 2005-07-22.
 1.11.78.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.11.78.1 21-Apr-2020  martin Sync with HEAD
 1.32 05-Aug-2024  riastradh pax(1): Nix trailing whitespace.

No functional change intended.
 1.31 20-Mar-2019  gutteridge branches: 1.31.12;
pax: minor adjustments to comments in pat_rep.c

Amend several comments to match present reality (the functionality was
added back in 2007).
 1.30 13-Jun-2018  christos branches: 1.30.2;
PR/53362: Thomas Barabosch: Use strlcpy() to prevent theoretical buffer
overflow.
 1.29 07-Apr-2009  perry branches: 1.29.46;
Fixes from christos for pr-41167

XXX -X is still broken, but that wasn't part of this PR.
 1.28 24-Feb-2008  joerg branches: 1.28.8; 1.28.10;
Kill pre-4.4BSD compat code.
 1.27 04-May-2007  christos branches: 1.27.4; 1.27.8;
PR/36275: john at iastate dot edu: tar -s modifies symlink targets
- This is explained in a comment in pat_rep.c inside mod_name(). I did not
want to change the default behavior, so I added another modifier "s" which
when set, the pattern will not modify the symlink destination.
- While here I fixed another bug that was introduced before by the fix in
PR/35257 where the renaming was happening twice since we called rep_name
twice.
- Finally if we are renaming hard of soft-link targets print the renames for
those too.
 1.26 29-Apr-2007  msaitoh fix typos
 1.25 24-Apr-2006  snj It's "its."
 1.24 11-Feb-2006  dsl Change all the 'return(x)' to 'return c'.
Makes some other searches I want to do a bit easier.
 1.23 23-Jan-2005  jmc Apply user supplied patterns first before applying actions for -A. This way pax
behavior WRT to patterns lines up with the example in the documentation
and how other implementations do it as well since -A is a non-standard
option/behavior. Fixes items noted in PR#23776
 1.22 21-Jan-2005  dsl Check for *ptr == 0, not ptr == NULL twice when validating -s xxxxx in tar mode.
Fixes PR bin/29040
Also actually report an error to the tty if NET2_REGEX is defined.
 1.21 27-Oct-2003  lukem branches: 1.21.4;
Overhaul how `build.sh tools' are used:

* Rename "config.h" to "nbtool_config.h" and
HAVE_CONFIG_H to HAVE_NBTOOL_CONFIG_H.
This makes in more obvious in the source when we're using
tools/compat/config.h versus "standard autoconf" config.h

* Consistently move the inclusion of nbtool_config.h to before
<sys/cdefs.h> so that the former can provide __RCSID() (et al),
and there's no need to protect those macros any more.

These changes should make it easier to "tool-ify" a program by adding:
#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
to the top of the source files (for the general case).
 1.20 13-Oct-2003  agc Move Keith Muller's code from a 4-clause to a 3-clause licence by removing
the advertising clause. Diffs provided in PR 22397 by Joel Baker, confirmed
to the board by Keith Muller.
 1.19 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.18 02-Feb-2003  wiz Fix various spelling, grammar, and punctuation problems in comments, from Sergey Svishchev in PR 20164.
 1.17 29-Nov-2002  rafal Fix lossage with the GNU-tar -X compatibility code: the replacement pattern
code did not properly deal with the fact that the replacement patterns built
by the -X code were on the stack, happily saving pointers into last weeks'
stack into the REPLACE structures.

Now, the replacement string is strdup'ed so it doesn't matter where the
original expression came from or it's lifetime.
 1.16 23-Oct-2002  christos ignore files that contain "..", not symlink sources.
 1.15 15-Oct-2002  christos PR/18663: Jeremy Reed: pax/tar/cpio allows ".." in names.
We now disallow it by default on both archive creation and extraction.
Add --insecure option to override.
 1.14 12-Oct-2002  christos merge OpenBSD changes:
- correct -C processing
- add ability to read filenames and flags from a file
- don't print dangerous escape sequences to the terminal
- use strlcpy/strncpy properly.
- handle tmpfile creation better.
- improve documentation of options.
- handle stdout/stderr list selection correctly.
- kill gzip when we get interrupted.
- simplify gzip setup.
- add more flags to programs.

additional changes:
- librmt processing.
- set POSIXLY_CORRECT in options parsing.
- prevent more string overruns.
- support -T

we don't turn the switch on to replace tar and cpio yet.
 1.13 31-Jan-2002  tv branches: 1.13.2;
Protect __RCSID and __COPYRIGHT from being invoked if not defined.
 1.12 25-Oct-2001  lukem - ansi KNF (just remove all the #ifndef __STDC__ prototype junk,
leaving the ansi stuff)
- use longlong_t instead of quad_t (etc), and rename *uqd*() -> *ull*()
- clean up the NET2_STAT stuff similar to ftpd; provide #defines and
macros which select which cast to use, etc
- clean up the NET2_FTS and NET2_REGEX #define use
 1.11 17-Feb-2000  itohy Cleanup and fix typos.
Partially from FreeBSD.
 1.10 22-Oct-1999  is Implement positional -C argument to tar.
 1.9 24-Mar-1999  pk branches: 1.9.4;
When substituting backreferences source from the pattern, not the replacement
strings.
 1.8 20-Jan-1999  mrg implement the gnutar -X flag in terms of pax -s. fix some bugs in pax -s handling while i am here...
 1.7 20-Jul-1997  christos - Rename local warn so tty_warn that we can include <err.h> since this program
uses err() and errx().
- Fix printf format strings.
- Added WARNS=1; note
- Added missing prototypes.
- Split overloaded trail function into trail() and subtrail().
 1.6 29-Mar-1997  mycroft Adjust the input string by the right amount if the pattern wasn't at the
beginning.
 1.5 11-Jan-1997  tls kill 'register'
 1.4 21-Mar-1995  cgd convert to new RCS id conventions.
 1.3 14-Jun-1994  mycroft Fix up RCS ids.
 1.2 13-Jun-1994  jtc Add RCS ID's
 1.1 13-Jun-1994  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 13-Jun-1994  jtc From 4.4 lite
 1.9.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.13.2.2 16-Jun-2004  jmc fix botched pullup from ticket #1021 and make sure this compiles on
non-NetBSD hosts again. Also fixes PR#24481
 1.13.2.1 07-Apr-2004  jmc Pullup rev 1.14-1.21 (requested by rafal in ticket #1021)

Pullup pax to current version on trunk. Includes many fixes.
 1.21.4.1 23-Jul-2005  snj Apply patch (requested by riz in ticket #5512):
Sync pax with HEAD of 2005-07-22.
 1.27.8.1 24-Mar-2008  keiichi sync with head.
 1.27.4.1 23-Mar-2008  matt sync with HEAD
 1.28.10.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.28.8.1 13-Apr-2009  snj Pull up following revision(s) (requested by christo in ticket #678):
bin/pax/extern.h: revision 1.56
bin/pax/file_subs.c: revision 1.62
bin/pax/options.c: revision 1.104
bin/pax/pat_rep.c: revision 1.29
bin/pax/pax.h: revision 1.30
Fixes from christos for pr-41167
XXX -X is still broken, but that wasn't part of this PR.
 1.29.46.1 25-Jun-2018  pgoyette Sync with HEAD
 1.30.2.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.30.2.2 21-Apr-2020  martin Sync with HEAD
 1.30.2.1 10-Jun-2019  christos Sync with HEAD
 1.31.12.1 02-Aug-2025  perseant Sync with HEAD
 1.7 24-Feb-2008  joerg branches: 1.7.66;
Kill pre-4.4BSD compat code.
 1.6 04-May-2007  christos branches: 1.6.4; 1.6.8;
PR/36275: john at iastate dot edu: tar -s modifies symlink targets
- This is explained in a comment in pat_rep.c inside mod_name(). I did not
want to change the default behavior, so I added another modifier "s" which
when set, the pattern will not modify the symlink destination.
- While here I fixed another bug that was introduced before by the fix in
PR/35257 where the renaming was happening twice since we called rep_name
twice.
- Finally if we are renaming hard of soft-link targets print the renames for
those too.
 1.5 13-Oct-2003  agc Move Keith Muller's code from a 4-clause to a 3-clause licence by removing
the advertising clause. Diffs provided in PR 22397 by Joel Baker, confirmed
to the board by Keith Muller.
 1.4 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.3 21-Mar-1995  cgd branches: 1.3.18;
convert to new RCS id conventions.
 1.2 13-Jun-1994  jtc Add RCS ID's
 1.1 13-Jun-1994  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 13-Jun-1994  jtc From 4.4 lite
 1.3.18.1 07-Apr-2004  jmc Pullup rev 1.4-1.5 (requested by rafal in ticket #1021)

Pullup pax to current version on trunk. Includes many fixes.
 1.6.8.1 24-Mar-2008  keiichi sync with head.
 1.6.4.1 23-Mar-2008  matt sync with HEAD
 1.7.66.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.7.66.1 21-Apr-2020  martin Sync with HEAD
 1.72 06-Nov-2021  gutteridge pax.1: minor grammar fixes
 1.71 19-Mar-2019  gutteridge 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.)
 1.70 19-Mar-2019  gutteridge 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.
 1.69 03-Jul-2017  wiz branches: 1.69.6;
Remove workaround for ancient HTML generation code.
 1.68 12-Aug-2016  sevan Document the version pax first appeared.
Bump date.
 1.67 19-Dec-2015  wiz Remove trailing whitespace.
 1.66 19-Dec-2015  christos Add the timestamp option to the pax front end.
 1.65 04-Nov-2015  wiz Use An in AUTHORS section.
 1.64 03-Nov-2015  pgoyette By my count, [[[[[cc]yy]mm]dd]hh]mm[.ss] has seven fields, not six!
 1.63 14-Nov-2013  wiz Stop hissing.
 1.62 14-Nov-2013  christos add --gnu for pax.
 1.61 19-Jun-2011  wiz branches: 1.61.2; 1.61.8;
Use Fl Fl for long options.
 1.60 19-Jun-2011  christos document --xz
 1.59 19-Jun-2010  wiz branches: 1.59.4;
Use Fl Fl for long options. Remove unnecessary Bk/Ek.
 1.58 19-Jun-2010  christos add --use-compress-program to pax, requested by mrg.
 1.57 05-Apr-2010  joerg \\ -> \e
 1.56 06-Sep-2009  christos too many dots. noted by anon-ymous
 1.55 02-Dec-2007  wiz Minimally sort options; use Dq.
 1.54 26-Oct-2007  hira Remove extra space from -T option in SYNOPSIS.
 1.53 23-Apr-2007  christos branches: 1.53.4;
bump date.
 1.52 23-Apr-2007  christos PR/36194: Greg A. Woods: add a '-V' flag to pax for verbose summary without
listing (and other minor fixes)
 1.51 22-Aug-2006  christos PR/34255: dieter roelants: Remove mention of -p f.
 1.50 11-Sep-2005  wiz Use better section header. Sort sections. From YOMURA Masanori in private mail.
 1.49 19-Jun-2004  christos add -0 argument to handle nul separated file lists.
 1.48 04-May-2004  grant - remove an extraneous word
- it's "backward compatibility" not "backwards compatibility"
 1.47 02-Apr-2004  keihan Revert last, revision 1.46.
 1.46 02-Apr-2004  keihan ".../newdir" --> "../newdir"
 1.45 13-Oct-2003  agc branches: 1.45.2;
Move Keith Muller's code from a 4-clause to a 3-clause licence by removing
the advertising clause. Diffs provided in PR 22397 by Joel Baker, confirmed
to the board by Keith Muller.
 1.44 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.43 19-Jul-2003  mrg note -z is an extention.
 1.42 01-May-2003  wiz Some grammar and punctuation fixes from jmc@openbsd.
 1.41 16-Apr-2003  wiz Bump date for last.
 1.40 31-Mar-2003  christos PR/20495: Soren Jacobsen: Add -j [bzip2] compression to tar.
 1.39 25-Feb-2003  wiz .Nm does not need a dummy argument ("") before punctuation or
for correct formatting of the SYNOPSIS any longer.
 1.38 28-Dec-2002  wiz Whitespace nits.
 1.37 19-Dec-2002  kleink Catch up with gen_subs.c rev. 1.27: the verbose output for symbolic links
is plain ls -l.
 1.36 16-Oct-2002  christos grr, I put these in the wrong spot.
 1.35 16-Oct-2002  christos Document the long options.
 1.34 25-Sep-2002  wiz New policy: New sentences start on a new line.
Patches by Robert Elz <kre at munnari oz au>, with minimal changes by me.
 1.33 21-Apr-2002  wiz branches: 1.33.2;
End sentence with a dot.
 1.32 20-Apr-2002  lukem - Directories marked `optional' won't be stored, even though their
contents will be.
- Document that `optional' faked-up entries won't be created.
 1.31 08-Feb-2002  ross Generate <>& symbolically. I'm avoiding .../dist/... directories for now.
 1.30 24-Jan-2002  lukem Add '-N dbdir', to specify that user & group lookups (via pwcache(3)) are to
be done from dbdir/master.passwd and dbdir/group, using simple text file
parsing routines from ../../usr.sbin/mtree/getid.c.
This does not affect the -G and -U options, which always use the
system databases.
 1.29 08-Nov-2001  hubertf Add a second example for copying directory trees, this time as root
(using -pe, which also preserves ownership).

Proof-read by Perry.
 1.28 07-Nov-2001  hubertf * Fix example on how to copy a tree.
Noted in PR 14486 by Hiramatsu Yoshifumi <hiramatu@boreas.dti.ne.jp>
* In the same example, add flags to preserve permissions (-pp)
 1.27 28-Oct-2001  wiz Whitespace nits, sort sections, fix Dv usage (doesn't like macro argument).
 1.26 25-Oct-2001  lukem Implement -M flag: During a write or copy operation, treat the list of
files on standard input as an mtree(8) `specfile' specification, and
write or copy only those items in the specfile.

If the file exists in the underlying file system, its permissions and
modification time will be used unless specifically overridden by the
specfile. An error will be raised if the type of entry in the specfile
conflicts with that of an existing file.

Otherwise, it is necessary to specify at least the following parameters
in the specfile: type, mode, gname or gid, and uname or uid, device
(in the case of block or character devices), and link (in the case of
symbolic links). If time isn't provided, the current time will be used.
 1.25 04-Sep-2000  kleink Elaborate a bit on -t.
 1.24 21-Aug-2000  tv Work around a groff bug by splitting a long .Cm into two parts.
 1.23 30-Mar-2000  thorpej Add a -O option: force one volume. This causes pax to not prompt for
a new volume upon premature end-of-volume (e.g. broken pipe). This is
especially useful in automated environments where error recovery cannot
be performed by a human.
 1.22 07-Nov-1999  mycroft branches: 1.22.2;
Disable -pf for now, too.
 1.21 01-Nov-1999  mrg add a new -p subflag: 'f' to preserve 4.4BSD fileflags. this only
really works for `pax -rw' but that's where it is most useful.
 1.20 26-Sep-1999  jdolecek add SEE ALSO section, xref tar(1), cpio(1), symlink(7)
 1.19 26-Sep-1999  enami Don't emphasys punctuations.
 1.18 24-Aug-1999  tron branches: 1.18.2;
Strip leading slashes from filenames by default, add new "A" option to
disable this behaviour. Patches supplied by Peter Seebach in PR bin/8233.
 1.17 22-Mar-1999  garbled More and more .Os cleanups. .Os is defined in the tmac.doc-common file,
so we shouldn't override it with versions in the manpages. Many more to
come.
 1.16 07-Mar-1999  mycroft Do the previous is a slightly different way, to avoid .Po/.Pc.
 1.15 07-Mar-1999  mycroft Clean up embedded angle brackets for mdoc2html.
 1.14 07-Mar-1999  mycroft Finally get all of the highlighting correct in the -T argument.
 1.13 07-Mar-1999  mycroft Clean up SYNOPSIS formatting.
 1.12 07-Mar-1999  mycroft Fix formatting errors.
 1.11 07-Mar-1999  mycroft Use .Ns a bit.
 1.10 07-Mar-1999  mycroft Improve the formatting of the SYNOPSIS section.
 1.9 02-Mar-1998  mrg document pax -z. only 2 years late...
 1.8 06-Feb-1998  perry macroize BSD, NetBSD, FreeBSD and misc cleanup
 1.7 21-Jan-1998  mycroft Allow a century to be specified in time stamps.
 1.6 20-Oct-1997  enami branches: 1.6.2;
Fix .Nm usage.
 1.5 14-Sep-1997  lukem * cleanup manpage
* getopt returns -1 not EOF
* deprecate register
 1.4 18-Dec-1996  pk Change misleading example to read `pax -v -f filename' which doesn't
extract files (Robert Dick; PR#3032)
 1.3 21-Mar-1995  cgd convert to new RCS id conventions.
 1.2 13-Jun-1994  jtc Add RCS ID's
 1.1 13-Jun-1994  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 13-Jun-1994  jtc From 4.4 lite
 1.6.2.1 29-Jan-1998  mellon Pull up 1.7 (mycroft)
 1.18.2.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.22.2.2 07-Nov-1999  mycroft Disable -pf for now, too.
 1.22.2.1 07-Nov-1999  mycroft file pax.1 was added on branch comdex-fall-1999 on 1999-11-07 15:57:32 +0000
 1.33.2.3 25-Aug-2004  he Pull up revision 1.48 (requested by cube in ticket #1735):
Remove extraneous word, and fix a grammar error.
 1.33.2.2 07-Apr-2004  jmc Pullup rev 1.34-1.36,1.38-1.47 (requested by rafal in ticket #1021)

Pullup pax to current version on trunk. Includes many fixes.
 1.33.2.1 05-Jan-2003  jmc Pull up revisions 1.36-1.37 (requested by kleink in ticket #1040)
Change verbose output for symbolic links from "%s => %s" to
"%s -> %s", which matches ls(1) (per POSIX), as well as GNU
tar output.
 1.45.2.1 09-May-2004  jdc branches: 1.45.2.1.2;
Pull up revision 1.48 (requested by grant in ticket #263)

- remove an extraneous word
- it's "backward compatibility" not "backwards compatibility"
 1.45.2.1.2.1 23-Jul-2005  snj Apply patch (requested by riz in ticket #5512):
Sync pax with HEAD of 2005-07-22.
 1.53.4.2 09-Jan-2008  matt sync with HEAD
 1.53.4.1 06-Nov-2007  matt sync with HEAD
 1.59.4.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.61.8.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.61.2.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.69.6.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.69.6.2 21-Apr-2020  martin Sync with HEAD
 1.69.6.1 10-Jun-2019  christos Sync with HEAD
 1.52 05-Aug-2024  riastradh pax(1): Nix trailing whitespace.

No functional change intended.
 1.51 05-Aug-2024  riastradh pax(1): Only require getcwd if we're reading without --insecure.

No other paths use the result of getcwd.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
 1.50 05-Aug-2024  riastradh pax(1): Don't require open(".") or getcwd to work for list operation.

PR 44498: tar(1) unnecessarily demands that getcwd() work
 1.49 24-Apr-2019  cheusov branches: 1.49.2; 1.49.10; 1.49.12;
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.
 1.48 02-Oct-2017  joerg branches: 1.48.4;
Include time.h for time(2). Sort.
 1.47 29-Aug-2011  joerg static + __dead
 1.46 18-Jun-2011  christos add --xz
 1.45 20-Jul-2008  lukem branches: 1.45.18;
Remove the \n and tabs from the __COPYRIGHT() strings.
 1.44 05-Oct-2007  lukem branches: 1.44.8;
Convert to using raise_default_signal(3).
 1.43 03-Jul-2007  lukem branches: 1.43.4;
In sig_cleanup(), attempt to reset the signal handler to the default
and raise the appropriate signal, so the correct wait status is
returned to the parent.
 1.42 29-Apr-2007  msaitoh fix typos
 1.41 23-Apr-2007  christos PR/36194: Greg A. Woods: add a '-V' flag to pax for verbose summary without
listing (and other minor fixes)
 1.40 11-Feb-2006  dsl Ensure we exit with an error if we don't actually do anything.
There are a lot of tty_warn(0,...) and syswarn(0,...) which probably ought
to be tty_warn/syswarn(1,...) to force an error exit. However some are
used in interactive parts (eg opening a continuation archive) where there
is a separate retry loop.
So we just pass a failure code out to main() - how quaint!
This should now cause the NetBSD build to fail when gzip tries to write
to a non-existant directory.
(I suspect there are still many errors that don't get reported correctly.)
 1.39 11-Feb-2006  dsl Change all the 'return(x)' to 'return c'.
Makes some other searches I want to do a bit easier.
 1.38 11-Feb-2006  dsl Actually exit with error if we cannot open("."), make it more obvious that
we exit with error on signals.
 1.37 24-Apr-2005  christos PR/18759: FUKAUMI Naoki: pax/tar dot-dot handling broken
PR/18840: Frederick Bruckman: Fix for PR/18663 incomplete pax symlink handling

This patch makes ``--insecure'' do something. Now if ``--insecure''
is not set (the default) we do a realpath(3) in all the pathnames
that we are trying to create and if either realpath fails, or the
path is outside our working directory, we print a warning and die.
This maybe too strict and might fail on valid archives that create
symlinks and directories in the wrong order.
 1.36 10-Oct-2004  christos PR/27208: Greg A. Woods: pax must call options() before using syswarn()
or tty_warn()
 1.35 20-Jun-2004  jmc Completely rework how tools/compat is done. Purge all uses/references to
_NETBSD_SOURCE as this makes cross building from older/newer versions of
NetBSD harder, not easier (and also makes the resulting tools 'different')

Wrap all required code with the inclusion of nbtool_config.h, attempt to
only use POSIX code in all places (or when reasonable test w. configure and
provide definitions: ala u_int, etc).

Reviewed by lukem. Tested on FreeBSD 4.9, Redhat Linux ES3, NetBSD 1.6.2 x86
NetBSD current (x86 and amd64) and Solaris 9.

Fixes PR's: PR#17762 PR#25944
 1.34 11-May-2004  christos Welcome to WARNS=3
 1.33 13-Feb-2004  matt branches: 1.33.2;
When pax catches a signal and cleans up, make sure to remove any temporary
file that was being extracted to. This will prevent pax from leaving
droppings when you hit ^C.
 1.32 13-Feb-2004  wiz Uppercase CPU, plural is CPUs.
 1.31 27-Oct-2003  lukem Overhaul how `build.sh tools' are used:

* Rename "config.h" to "nbtool_config.h" and
HAVE_CONFIG_H to HAVE_NBTOOL_CONFIG_H.
This makes in more obvious in the source when we're using
tools/compat/config.h versus "standard autoconf" config.h

* Consistently move the inclusion of nbtool_config.h to before
<sys/cdefs.h> so that the former can provide __RCSID() (et al),
and there's no need to protect those macros any more.

These changes should make it easier to "tool-ify" a program by adding:
#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
to the top of the source files (for the general case).
 1.30 13-Oct-2003  agc Move Keith Muller's code from a 4-clause to a 3-clause licence by removing
the advertising clause. Diffs provided in PR 22397 by Joel Baker, confirmed
to the board by Keith Muller.
 1.29 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.28 08-Jul-2003  simonb Add support for gnutar's -O "extract to stdout" option.
 1.27 23-Jun-2003  grant consistently use "cannot" instead of "can not".
 1.26 03-Mar-2003  grant call setprogname()
 1.25 25-Feb-2003  wiz Add support for '-j' to create (and extract, but -z already did that)
bzip2 compressed tar files, for GNU tar compatibility.
Patch from Soren Jacobsen in PR 19467, okayed by christos.
Sync usage with options while here.
 1.24 12-Dec-2002  christos fix horrible side effect introduced by changing the append mode into archive.
The append mode already did that, so we always ended up overwriting the archive.
 1.23 10-Dec-2002  christos PR/19339: Martin Weber: Tar fails to append on empty files and exits with
0. Revert previous change for PR/18689. We always want to exit with
an error if we could not determine the archive format. Instead,
treat empty files specially. On list/extract we turn into no/op.
On append, we turn into archive.
 1.22 20-Oct-2002  christos PR/18733: Jed Davis: stderr is not constant under linux and this is a host
tool.
 1.21 17-Oct-2002  christos Fix DEFOP botch. DEFOP == LIST so tar -tvf stopped working. (hi soren)
 1.20 15-Oct-2002  christos PR/18663: Jeremy Reed: pax/tar/cpio allows ".." in names.
We now disallow it by default on both archive creation and extraction.
Add --insecure option to override.
 1.19 13-Oct-2002  mrg include <string.h> for mem* and str*
 1.18 12-Oct-2002  christos merge OpenBSD changes:
- correct -C processing
- add ability to read filenames and flags from a file
- don't print dangerous escape sequences to the terminal
- use strlcpy/strncpy properly.
- handle tmpfile creation better.
- improve documentation of options.
- handle stdout/stderr list selection correctly.
- kill gzip when we get interrupted.
- simplify gzip setup.
- add more flags to programs.

additional changes:
- librmt processing.
- set POSIXLY_CORRECT in options parsing.
- prevent more string overruns.
- support -T

we don't turn the switch on to replace tar and cpio yet.
 1.17 31-Jan-2002  tv branches: 1.17.2;
Protect __RCSID and __COPYRIGHT from being invoked if not defined.
 1.16 29-Jan-2002  tv Make almost all tools compile and run properly on non-NetBSD hosts. (In
particular, most tools now run correctly on Solaris 7.)
 1.15 25-Oct-2001  lukem Implement -M flag: During a write or copy operation, treat the list of
files on standard input as an mtree(8) `specfile' specification, and
write or copy only those items in the specfile.

If the file exists in the underlying file system, its permissions and
modification time will be used unless specifically overridden by the
specfile. An error will be raised if the type of entry in the specfile
conflicts with that of an existing file.

Otherwise, it is necessary to specify at least the following parameters
in the specfile: type, mode, gname or gid, and uname or uid, device
(in the case of block or character devices), and link (in the case of
symbolic links). If time isn't provided, the current time will be used.
 1.14 25-Oct-2001  lukem - ansi KNF (just remove all the #ifndef __STDC__ prototype junk,
leaving the ansi stuff)
- use longlong_t instead of quad_t (etc), and rename *uqd*() -> *ull*()
- clean up the NET2_STAT stuff similar to ftpd; provide #defines and
macros which select which cast to use, etc
- clean up the NET2_FTS and NET2_REGEX #define use
 1.13 22-Oct-2000  kleink LC_TIME holds a locale name, which isn't a strftime() format string.
 1.12 17-Feb-2000  itohy branches: 1.12.4;
Cleanup and fix typos.
Partially from FreeBSD.
 1.11 01-Nov-1999  mrg add a new -p subflag: 'f' to preserve 4.4BSD fileflags. this only
really works for `pax -rw' but that's where it is most useful.
 1.10 24-Aug-1999  tron branches: 1.10.2;
Strip leading slashes from filenames by default, add new "A" option to
disable this behaviour. Patches supplied by Peter Seebach in PR bin/8233.
 1.9 03-Mar-1999  christos Add SIGINFO support.
 1.8 28-Jul-1998  mycroft Delint.
 1.7 27-Jul-1998  mycroft __AUDIT__ cleanup.
 1.6 20-Jul-1997  christos - Rename local warn so tty_warn that we can include <err.h> since this program
uses err() and errx().
- Fix printf format strings.
- Added WARNS=1; note
- Added missing prototypes.
- Split overloaded trail function into trail() and subtrail().
 1.5 26-Mar-1996  mrg impliment -z (gzip) in pax and tar, and -Z (compress) in tar.
 1.4 21-Mar-1995  cgd convert to new RCS id conventions.
 1.3 14-Jun-1994  mycroft Fix up RCS ids.
 1.2 13-Jun-1994  jtc Add RCS ID's
 1.1 13-Jun-1994  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 13-Jun-1994  jtc From 4.4 lite
 1.10.2.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.12.4.1 25-Oct-2000  tv Pullup 1.13 [kleink]:
Don't abuse LC_TIME for strftime(3) format strings.
 1.17.2.2 16-Jun-2004  jmc fix botched pullup from ticket #1021 and make sure this compiles on
non-NetBSD hosts again. Also fixes PR#24481
 1.17.2.1 07-Apr-2004  jmc Pullup rev 1.18-1.33 (requested by rafal in ticket #1021)

Pullup pax to current version on trunk. Includes many fixes.
 1.33.2.1 22-Jun-2004  tron branches: 1.33.2.1.2;
Pull up revision 1.35 (requested by jmc in ticket #527):
Completely rework how tools/compat is done. Purge all uses/references to
_NETBSD_SOURCE as this makes cross building from older/newer versions of
NetBSD harder, not easier (and also makes the resulting tools 'different')
Wrap all required code with the inclusion of nbtool_config.h, attempt to
only use POSIX code in all places (or when reasonable test w. configure and
provide definitions: ala u_int, etc).
Reviewed by lukem. Tested on FreeBSD 4.9, Redhat Linux ES3, NetBSD 1.6.2 x86
NetBSD current (x86 and amd64) and Solaris 9.
Fixes PR's: PR#17762 PR#25944
 1.33.2.1.2.1 23-Jul-2005  snj Apply patch (requested by riz in ticket #5512):
Sync pax with HEAD of 2005-07-22.
 1.43.4.1 06-Nov-2007  matt sync with HEAD
 1.44.8.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.45.18.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.48.4.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.48.4.2 21-Apr-2020  martin Sync with HEAD
 1.48.4.1 10-Jun-2019  christos Sync with HEAD
 1.49.12.1 02-Aug-2025  perseant Sync with HEAD
 1.49.10.1 07-Aug-2024  martin Pull up following revision(s) (requested by riastradh in ticket #776):

bin/pax/buf_subs.c: revision 1.31
tests/bin/pax/t_pax.sh: revision 1.3
tests/bin/pax/t_pax.sh: revision 1.4
tests/bin/pax/t_pax.sh: revision 1.5
tests/bin/pax/t_pax.sh: revision 1.6
tests/bin/pax/t_pax.sh: revision 1.7
bin/pax/extern.h: revision 1.61
bin/pax/pax.c: revision 1.50
bin/pax/pax.c: revision 1.51
bin/pax/file_subs.c: revision 1.65
bin/pax/ftree.c: revision 1.43
bin/pax/ar_subs.c: revision 1.58

pax: exit 0 if stdin filelist is used and empty

If copying a list of files from stdin, exit zero instead of non-zero
if there are no files supplied.

AFAICT, POSIX doesn't require a non-zero an error in this situation,
since there are no files to not match.

Fix from PR bin/41736 by Lloyd Parkes.

pax: don't overwrite destination if -r -w copy fails

Add more error handling to pax -r -w so that any failure
during the copy to the temporary file (including a failed flush)
prevents any existing destination file from being replaced
with the partial (including possibly empty) temporary file.

The partial temporary file is removed. pax still exists non-zero.
Thanks to Michael van Elst (mlelstv@) for the analysis
of the problem in the PR.
Should fix PR misc/33753.

tests/bin/pax: Add test for pax list in an ungettable cwd.
The list operation has no need to touch the file system, so it should
have no need for open(".") or getcwd() to succeed.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
(The PR was filed about tar(1), which is now bsdtar by default, but
the issue applies to pax(1) too and would continue to apply to tar(1)
if you set MKBSDTAR=no.)
pax(1): Don't require open(".") or getcwd to work for list operation.

PR 44498: tar(1) unnecessarily demands that getcwd() work
tests/bin/pax: Test pax(1) succeeds with empty file list on stdin.

PR bin/41736: pax reports an error when copying zero files
tests/bin/pax: Add some more cwd-related test cases.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
pax(1): Only require getcwd if we're reading without --insecure.

No other paths use the result of getcwd.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
 1.49.2.1 07-Aug-2024  martin Pull up following revision(s) (requested by riastradh in ticket #1863):

bin/pax/buf_subs.c: revision 1.31
tests/bin/pax/t_pax.sh: revision 1.3
tests/bin/pax/t_pax.sh: revision 1.4
tests/bin/pax/t_pax.sh: revision 1.5
tests/bin/pax/t_pax.sh: revision 1.6
tests/bin/pax/t_pax.sh: revision 1.7
bin/pax/extern.h: revision 1.61
bin/pax/pax.c: revision 1.50
bin/pax/pax.c: revision 1.51
bin/pax/file_subs.c: revision 1.65
bin/pax/ftree.c: revision 1.43
bin/pax/ar_subs.c: revision 1.58

pax: exit 0 if stdin filelist is used and empty

If copying a list of files from stdin, exit zero instead of non-zero
if there are no files supplied.

AFAICT, POSIX doesn't require a non-zero an error in this situation,
since there are no files to not match.

Fix from PR bin/41736 by Lloyd Parkes.

pax: don't overwrite destination if -r -w copy fails

Add more error handling to pax -r -w so that any failure
during the copy to the temporary file (including a failed flush)
prevents any existing destination file from being replaced
with the partial (including possibly empty) temporary file.

The partial temporary file is removed. pax still exists non-zero.
Thanks to Michael van Elst (mlelstv@) for the analysis
of the problem in the PR.
Should fix PR misc/33753.

tests/bin/pax: Add test for pax list in an ungettable cwd.
The list operation has no need to touch the file system, so it should
have no need for open(".") or getcwd() to succeed.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
(The PR was filed about tar(1), which is now bsdtar by default, but
the issue applies to pax(1) too and would continue to apply to tar(1)
if you set MKBSDTAR=no.)
pax(1): Don't require open(".") or getcwd to work for list operation.

PR 44498: tar(1) unnecessarily demands that getcwd() work
tests/bin/pax: Test pax(1) succeeds with empty file list on stdin.

PR bin/41736: pax reports an error when copying zero files
tests/bin/pax: Add some more cwd-related test cases.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
pax(1): Only require getcwd if we're reading without --insecure.

No other paths use the result of getcwd.

PR bin/44498: tar(1) unnecessarily demands that getcwd() work
 1.31 09-Aug-2012  christos branches: 1.31.34;
PR/46786: Simon Burge: After conversion to 64 bit time_t, tar/pax/cpio
erroneously think that negative time_t's never fit in 32 bits. Rework
conversion code to always use uintmax_t, and detect negative values.
XXX[1]: Perhaps we should do the same (use a signed conversion) for all
fields not just for time_t
XXX[2]: pullup for 6
 1.30 07-Apr-2009  perry branches: 1.30.6;
Fixes from christos for pr-41167

XXX -X is still broken, but that wasn't part of this PR.
 1.29 14-Feb-2009  lukem fix -Wsign-compare issues
 1.28 24-Feb-2008  joerg branches: 1.28.8; 1.28.10;
Kill pre-4.4BSD compat code.
 1.27 02-Dec-2007  wiz branches: 1.27.4;
Fix typo in comment.
 1.26 29-Apr-2007  msaitoh branches: 1.26.4;
fix typos
 1.25 24-Sep-2005  dsl Don't leave arcn->org_name pointing to ftent->fts_path, if we being cpio
then ftent is freed just below. Take a copy of the name and point org_name
at the copy.
Should fix PR/30627 (the fix in the PR will break pax and tar!)
 1.24 11-May-2004  christos Welcome to WARNS=3
 1.23 12-Feb-2004  matt branches: 1.23.4;
When restoring files from an archive, initially restore to a temp-file and
once it has been instantiated correctly, rename it to desired name. This
prevents the problem of partially created files being accessed before they
are complete. If said file is a shared library, that can cause ramdon core
dumps.
 1.22 27-Oct-2003  lukem Overhaul how `build.sh tools' are used:

* Rename "config.h" to "nbtool_config.h" and
HAVE_CONFIG_H to HAVE_NBTOOL_CONFIG_H.
This makes in more obvious in the source when we're using
tools/compat/config.h versus "standard autoconf" config.h

* Consistently move the inclusion of nbtool_config.h to before
<sys/cdefs.h> so that the former can provide __RCSID() (et al),
and there's no need to protect those macros any more.

These changes should make it easier to "tool-ify" a program by adding:
#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
to the top of the source files (for the general case).
 1.21 13-Oct-2003  agc Move Keith Muller's code from a 4-clause to a 3-clause licence by removing
the advertising clause. Diffs provided in PR 22397 by Joel Baker, confirmed
to the board by Keith Muller.
 1.20 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.19 23-Jun-2003  grant no need for HAVE_LCHOWN and HAVE_LCHMOD anymore.
 1.18 09-May-2003  lukem #ifdef HOSTPROG then explicitly use {major,minor,makedev}_netbsd
macros from pack_dev.h instead of the native versions.
Fixes [toolchain/20051] by Valeriy E. Ushakov.
 1.17 17-Oct-2002  christos Fix DEFOP botch. DEFOP == LIST so tar -tvf stopped working. (hi soren)
 1.16 13-Oct-2002  mrg make this build on LP64
 1.15 12-Oct-2002  thorpej It doesn't help much to skip linking librmt if HOSTPROG but leave
all the librmt calls in the program. So, make librmt support conditional
on #ifdef SUPPORT_RMT, and define that when NOT building as a HOSTPROG.
 1.14 12-Oct-2002  christos merge OpenBSD changes:
- correct -C processing
- add ability to read filenames and flags from a file
- don't print dangerous escape sequences to the terminal
- use strlcpy/strncpy properly.
- handle tmpfile creation better.
- improve documentation of options.
- handle stdout/stderr list selection correctly.
- kill gzip when we get interrupted.
- simplify gzip setup.
- add more flags to programs.

additional changes:
- librmt processing.
- set POSIXLY_CORRECT in options parsing.
- prevent more string overruns.
- support -T

we don't turn the switch on to replace tar and cpio yet.
 1.13 31-Jan-2002  tv branches: 1.13.2;
Roll in fixes to permit cross-compiling from non-NetBSD hosts. This
round has been tested on Solaris/x86 and Linux hosts.

* Add host tools cap_mkdb, ctags, m4, uudecode.
* Protect __RCSID() and __COPYRIGHT() better.
* Reduce the number of places that need to include "config.h", to keep
sources closer to their "vanilla" versions.
* Add more compat #defines and autoconf-checked functions.
 1.12 28-Oct-2001  lukem s/u_longlong_t/unsigned long long/ to shut up the parc port
 1.11 25-Oct-2001  lukem - ansi KNF (just remove all the #ifndef __STDC__ prototype junk,
leaving the ansi stuff)
- use longlong_t instead of quad_t (etc), and rename *uqd*() -> *ull*()
- clean up the NET2_STAT stuff similar to ftpd; provide #defines and
macros which select which cast to use, etc
- clean up the NET2_FTS and NET2_REGEX #define use
 1.10 04-Sep-2001  wiz Fix typo (specifc instead of specific).
 1.9 17-Feb-2000  itohy Cleanup and fix typos.
Partially from FreeBSD.
 1.8 22-Oct-1999  is Implement positional -C argument to tar.
 1.7 22-Oct-1999  mrg support ././@LongLink extraction, as created by GNU tar.
 1.6 03-Jul-1999  kleink branches: 1.6.2;
Honor the contents of $TMPDIR for the creation of temporary files, and use
_PATH_TMP instead of hardcoding /tmp if not set in the environment; fixes
PR bin/7796 from Chris Demetriou.
 1.5 26-Mar-1998  mycroft branches: 1.5.2;
Use major(), minor() and makedev(), not a hacked up local equivalent, which
only supported 8-bit major and minor numbers.
 1.4 20-Jul-1997  christos - Rename local warn so tty_warn that we can include <err.h> since this program
uses err() and errx().
- Fix printf format strings.
- Added WARNS=1; note
- Added missing prototypes.
- Split overloaded trail function into trail() and subtrail().
 1.3 21-Mar-1995  cgd convert to new RCS id conventions.
 1.2 13-Jun-1994  jtc Add RCS ID's
 1.1 13-Jun-1994  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 13-Jun-1994  jtc From 4.4 lite
 1.5.2.1 07-Jul-1999  perry pullup 1.5->1.6 (kleink)
 1.6.2.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.13.2.2 16-Jun-2004  jmc fix botched pullup from ticket #1021 and make sure this compiles on
non-NetBSD hosts again. Also fixes PR#24481
 1.13.2.1 07-Apr-2004  jmc Pullup rev 1.14-1.23 (requested by rafal in ticket #1021)

Pullup pax to current version on trunk. Includes many fixes.
 1.23.4.1 23-Jul-2005  snj Apply patch (requested by riz in ticket #5512):
Sync pax with HEAD of 2005-07-22.
 1.26.4.2 23-Mar-2008  matt sync with HEAD
 1.26.4.1 09-Jan-2008  matt sync with HEAD
 1.27.4.1 24-Mar-2008  keiichi sync with head.
 1.28.10.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.28.8.1 13-Apr-2009  snj Pull up following revision(s) (requested by christo in ticket #678):
bin/pax/extern.h: revision 1.56
bin/pax/file_subs.c: revision 1.62
bin/pax/options.c: revision 1.104
bin/pax/pat_rep.c: revision 1.29
bin/pax/pax.h: revision 1.30
Fixes from christos for pr-41167
XXX -X is still broken, but that wasn't part of this PR.
 1.30.6.1 30-Oct-2012  yamt sync with head
 1.31.34.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.31.34.1 21-Apr-2020  martin Sync with HEAD
 1.24 31-Aug-2011  plunky branches: 1.24.44;
NULL does not need a cast
 1.23 21-Dec-2009  christos Remove unnecessary casts.
 1.22 21-Dec-2009  wiz Free pt in another error case.
Found by Henning Petersen using cppcheck, in PR 42488.
 1.21 29-Apr-2007  msaitoh fix typos
 1.20 11-Feb-2006  dsl Change all the 'return(x)' to 'return c'.
Makes some other searches I want to do a bit easier.
 1.19 27-Oct-2003  lukem Overhaul how `build.sh tools' are used:

* Rename "config.h" to "nbtool_config.h" and
HAVE_CONFIG_H to HAVE_NBTOOL_CONFIG_H.
This makes in more obvious in the source when we're using
tools/compat/config.h versus "standard autoconf" config.h

* Consistently move the inclusion of nbtool_config.h to before
<sys/cdefs.h> so that the former can provide __RCSID() (et al),
and there's no need to protect those macros any more.

These changes should make it easier to "tool-ify" a program by adding:
#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
to the top of the source files (for the general case).
 1.18 13-Oct-2003  agc Move Keith Muller's code from a 4-clause to a 3-clause licence by removing
the advertising clause. Diffs provided in PR 22397 by Joel Baker, confirmed
to the board by Keith Muller.
 1.17 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.16 02-Feb-2003  wiz Fix various spelling, grammar, and punctuation problems in comments, from Sergey Svishchev in PR 20164.
 1.15 31-Jan-2002  tv branches: 1.15.2;
Protect __RCSID and __COPYRIGHT from being invoked if not defined.
 1.14 04-Dec-2001  wiz Replace some misuses of "then" with "than".
 1.13 25-Oct-2001  lukem - ansi KNF (just remove all the #ifndef __STDC__ prototype junk,
leaving the ansi stuff)
- use longlong_t instead of quad_t (etc), and rename *uqd*() -> *ull*()
- clean up the NET2_STAT stuff similar to ftpd; provide #defines and
macros which select which cast to use, etc
- clean up the NET2_FTS and NET2_REGEX #define use
 1.12 17-Feb-2000  itohy Cleanup and fix typos.
Partially from FreeBSD.
 1.11 04-Nov-1998  christos char -> unsigned char cast.
 1.10 27-Jul-1998  mycroft __AUDIT__ cleanup.
 1.9 01-Apr-1998  kleink Need <time.h> for time(), localtime() and mktime() prototypes.
 1.8 21-Jan-1998  mycroft Allow a century to be specified in time stamps.
 1.7 20-Jul-1997  christos branches: 1.7.2;
- Rename local warn so tty_warn that we can include <err.h> since this program
uses err() and errx().
- Fix printf format strings.
- Added WARNS=1; note
- Added missing prototypes.
- Split overloaded trail function into trail() and subtrail().
 1.6 11-Jan-1997  tls kill 'register'
 1.5 21-Mar-1995  cgd convert to new RCS id conventions.
 1.4 23-Sep-1994  mycroft Eliminate uses of some obsolete functions.
 1.3 14-Jun-1994  mycroft Fix up RCS ids.
 1.2 13-Jun-1994  jtc Add RCS ID's
 1.1 13-Jun-1994  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 13-Jun-1994  jtc From 4.4 lite
 1.7.2.1 29-Jan-1998  mellon Pull up 1.8 (mycroft)
 1.15.2.2 16-Jun-2004  jmc fix botched pullup from ticket #1021 and make sure this compiles on
non-NetBSD hosts again. Also fixes PR#24481
 1.15.2.1 07-Apr-2004  jmc Pullup rev 1.16-1.19 (requested by rafal in ticket #1021)

Pullup pax to current version on trunk. Includes many fixes.
 1.24.44.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.24.44.1 21-Apr-2020  martin Sync with HEAD
 1.6 13-Oct-2003  agc branches: 1.6.98;
Move Keith Muller's code from a 4-clause to a 3-clause licence by removing
the advertising clause. Diffs provided in PR 22397 by Joel Baker, confirmed
to the board by Keith Muller.
 1.5 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.4 21-Jan-1998  mycroft branches: 1.4.12;
Allow a century to be specified in time stamps.
 1.3 21-Mar-1995  cgd branches: 1.3.4;
convert to new RCS id conventions.
 1.2 13-Jun-1994  jtc Add RCS ID's
 1.1 13-Jun-1994  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 13-Jun-1994  jtc From 4.4 lite
 1.3.4.1 29-Jan-1998  mellon Pull up 1.4 (mycroft)
 1.4.12.1 07-Apr-2004  jmc Pullup rev 1.5-1.6 (requested by rafal in ticket #1021)

Pullup pax to current version on trunk. Includes many fixes.
 1.6.98.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.6.98.1 21-Apr-2020  martin Sync with HEAD
 1.31 18-Oct-2013  christos branches: 1.31.26;
Fixed unused warnings.
 1.30 10-Jan-2008  tls branches: 1.30.26; 1.30.32;
Teach set_ftime about symbolic links, because it has to know: on some
Linux systems, when we build as a tool we think we have lutimes but it
doesn't work on some filesystems at runtime. A bit ugly but effective
and without use of AC_TRY_RUN in the tool build. Tidier (than mine)
set_ftime reorganization from christos.
 1.29 29-Apr-2007  msaitoh branches: 1.29.4;
fix typos
 1.28 24-Nov-2006  christos fix spelling of accidentally; from Zapher
 1.27 24-Apr-2006  snj It's "its."
 1.26 16-Apr-2006  christos PR/22995: Sergey Svishchev: If we are cd'ing around and we have directories
with relative paths, convert the directories to absolute paths, so that
restoring timestamps does not fail.
 1.25 11-Feb-2006  dsl Change all the 'return(x)' to 'return c'.
Makes some other searches I want to do a bit easier.
 1.24 16-Sep-2005  christos spelling fixes from Joerg Sonnenberger
 1.23 27-Oct-2003  lukem Overhaul how `build.sh tools' are used:

* Rename "config.h" to "nbtool_config.h" and
HAVE_CONFIG_H to HAVE_NBTOOL_CONFIG_H.
This makes in more obvious in the source when we're using
tools/compat/config.h versus "standard autoconf" config.h

* Consistently move the inclusion of nbtool_config.h to before
<sys/cdefs.h> so that the former can provide __RCSID() (et al),
and there's no need to protect those macros any more.

These changes should make it easier to "tool-ify" a program by adding:
#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
to the top of the source files (for the general case).
 1.22 13-Oct-2003  agc Move Keith Muller's code from a 4-clause to a 3-clause licence by removing
the advertising clause. Diffs provided in PR 22397 by Joel Baker, confirmed
to the board by Keith Muller.
 1.21 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.20 02-Feb-2003  wiz Fix various spelling, grammar, and punctuation problems in comments, from Sergey Svishchev in PR 20164.
 1.19 06-Jan-2003  wiz writable, not writeable.
 1.18 12-Oct-2002  christos merge OpenBSD changes:
- correct -C processing
- add ability to read filenames and flags from a file
- don't print dangerous escape sequences to the terminal
- use strlcpy/strncpy properly.
- handle tmpfile creation better.
- improve documentation of options.
- handle stdout/stderr list selection correctly.
- kill gzip when we get interrupted.
- simplify gzip setup.
- add more flags to programs.

additional changes:
- librmt processing.
- set POSIXLY_CORRECT in options parsing.
- prevent more string overruns.
- support -T

we don't turn the switch on to replace tar and cpio yet.
 1.17 31-Jan-2002  tv branches: 1.17.2;
Protect __RCSID and __COPYRIGHT from being invoked if not defined.
 1.16 29-Jan-2002  tv Make almost all tools compile and run properly on non-NetBSD hosts. (In
particular, most tools now run correctly on Solaris 7.)
 1.15 25-Oct-2001  lukem - ansi KNF (just remove all the #ifndef __STDC__ prototype junk,
leaving the ansi stuff)
- use longlong_t instead of quad_t (etc), and rename *uqd*() -> *ull*()
- clean up the NET2_STAT stuff similar to ftpd; provide #defines and
macros which select which cast to use, etc
- clean up the NET2_FTS and NET2_REGEX #define use
 1.14 24-Oct-2001  lukem fix typo
 1.13 21-Mar-2000  thorpej Use an in-core stack for directory permissions rather than a temporary
file. The temporary file code can be reenabled by building with
-DDIRS_USE_FILE.

Fixes a problem where sysinst would fail to unpack the base set because
the temporary file wouldn't fit in the ramdisk.
 1.12 17-Feb-2000  itohy Cleanup and fix typos.
Partially from FreeBSD.
 1.11 17-Feb-2000  itohy Fix SIGINFO botch (PR #8868).
Continue partial write(2) on signals (xwrite()).
Partial read(2) at a few places are also continued (xread()).

Add {read,write}_with_restart() hooks for porting on systems
which don't restart interrupted read()/write() calls.

Reviewed and discussed in tech-kern and tech-userlevel lists.
 1.10 01-Nov-1999  mrg add a new -p subflag: 'f' to preserve 4.4BSD fileflags. this only
really works for `pax -rw' but that's where it is most useful.
 1.9 03-Jul-1999  kleink branches: 1.9.2;
Honor the contents of $TMPDIR for the creation of temporary files, and use
_PATH_TMP instead of hardcoding /tmp if not set in the environment; fixes
PR bin/7796 from Chris Demetriou.
 1.8 12-Feb-1999  kleink branches: 1.8.2;
<sys/fcntl.h> -> <fcntl.h>
 1.7 20-Jul-1997  christos - Rename local warn so tty_warn that we can include <err.h> since this program
uses err() and errx().
- Fix printf format strings.
- Added WARNS=1; note
- Added missing prototypes.
- Split overloaded trail function into trail() and subtrail().
 1.6 22-Mar-1997  lukem use mkstemp() instead of tempnam()
 1.5 11-Jan-1997  tls kill 'register'
 1.4 21-Mar-1995  cgd convert to new RCS id conventions.
 1.3 14-Jun-1994  mycroft Fix up RCS ids.
 1.2 13-Jun-1994  jtc Add RCS ID's
 1.1 13-Jun-1994  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 13-Jun-1994  jtc From 4.4 lite
 1.8.2.1 07-Jul-1999  perry pullup 1.8->1.9 (kleink)
 1.9.2.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.17.2.2 16-Jun-2004  jmc fix botched pullup from ticket #1021 and make sure this compiles on
non-NetBSD hosts again. Also fixes PR#24481
 1.17.2.1 07-Apr-2004  jmc Pullup rev 1.18-1.23 (requested by rafal in ticket #1021)

Pullup pax to current version on trunk. Includes many fixes.
 1.29.4.1 23-Mar-2008  matt sync with HEAD
 1.30.32.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.30.26.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.31.26.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.31.26.1 21-Apr-2020  martin Sync with HEAD
 1.10 29-Apr-2007  msaitoh branches: 1.10.78;
fix typos
 1.9 16-Sep-2005  christos spelling fixes from Joerg Sonnenberger
 1.8 13-Oct-2003  agc Move Keith Muller's code from a 4-clause to a 3-clause licence by removing
the advertising clause. Diffs provided in PR 22397 by Joel Baker, confirmed
to the board by Keith Muller.
 1.7 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.6 21-Mar-2000  thorpej branches: 1.6.6;
Use an in-core stack for directory permissions rather than a temporary
file. The temporary file code can be reenabled by building with
-DDIRS_USE_FILE.

Fixes a problem where sysinst would fail to unpack the base set because
the temporary file wouldn't fit in the ramdisk.
 1.5 17-Feb-2000  itohy Cleanup and fix typos.
Partially from FreeBSD.
 1.4 01-Nov-1999  mrg add a new -p subflag: 'f' to preserve 4.4BSD fileflags. this only
really works for `pax -rw' but that's where it is most useful.
 1.3 21-Mar-1995  cgd branches: 1.3.10;
convert to new RCS id conventions.
 1.2 13-Jun-1994  jtc Add RCS ID's
 1.1 13-Jun-1994  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 13-Jun-1994  jtc From 4.4 lite
 1.3.10.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.6.6.1 07-Apr-2004  jmc Pullup rev 1.7-1.8 (requested by rafal in ticket #1021)

Pullup pax to current version on trunk. Includes many fixes.
 1.10.78.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.10.78.1 21-Apr-2020  martin Sync with HEAD
 1.39 19-Mar-2019  wiz Whitespace nits.
 1.38 19-Mar-2019  gutteridge 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.)
 1.37 03-Jul-2017  wiz branches: 1.37.6;
Remove workaround for ancient HTML generation code.
 1.36 19-Dec-2015  christos PR/50119: Thomas Klausner: Add --timestamp option to tar.
 1.35 11-Apr-2015  wiz Use An in AUTHORS section.
 1.34 11-Apr-2015  christos Add -J/--xz to specifically decompress xz compressed files. We don't really
need this because -z autodetects the compression format; this is for syntax
compatibility with other tar implementations. From Joachim Henke
 1.33 22-Mar-2012  wiz Fix whitespace nits. Suggested by Bug Hunting.
 1.32 19-Jun-2011  wiz branches: 1.32.2;
Use Fl Fl for long options.
 1.31 19-Jun-2011  christos document --xz
 1.30 05-Apr-2010  joerg branches: 1.30.4;
\\ -> \e
 1.29 23-Mar-2010  dholland bump date for previous
 1.28 23-Mar-2010  dholland The -X option of tar names a file containing exception globs, not
exception filenames, same as in gtar. PR 41168 from Perry.
 1.27 23-Nov-2008  dholland "compatibility", not "compatability".
 1.26 02-Dec-2007  wiz branches: 1.26.12;
Minimally sort options; use Dq; new sentence, new line.
 1.25 04-May-2007  christos branches: 1.25.4;
PR/36275: john at iastate dot edu: tar -s modifies symlink targets
- This is explained in a comment in pat_rep.c inside mod_name(). I did not
want to change the default behavior, so I added another modifier "s" which
when set, the pattern will not modify the symlink destination.
- While here I fixed another bug that was introduced before by the fix in
PR/35257 where the renaming was happening twice since we called rep_name
twice.
- Finally if we are renaming hard of soft-link targets print the renames for
those too.
 1.24 23-Apr-2007  christos bump date.
 1.23 23-Apr-2007  christos PR/36194: Greg A. Woods: add a '-V' flag to pax for verbose summary without
listing (and other minor fixes)
 1.22 24-May-2005  wiz branches: 1.22.4;
Bump date for -no-recursion.
 1.21 07-May-2005  christos PR/30167: J.T. Conklin: NetBSD tar does not support GNU tar --no-recursion flag
 1.20 20-Oct-2004  wiz English improvement (remove "be").
 1.19 17-Oct-2004  dsl Update date - before wiz complains
 1.18 17-Oct-2004  dsl Add an option --chroot to tar. Causes it to chroot(".") before doing
an extract. With -h this will cause existing absolute symlinks to be treated
as relative to the current directory.
Helps sysinst handle existing symlinks in the target system.
Remove 'L' from the usage (got spilt into 'h' and 'H' many moons ago)
Add 'S' to usage, and put into correct place in options list.
 1.17 27-Sep-2004  wiz Bump date for -S support.
 1.16 26-Sep-2004  christos PR/19490: Julio Merino: Teach tar about --sparse, -S option.
Do it for cpio too.
 1.15 15-Sep-2004  hubertf Change copyright to 2-clause BSD-copyright.
Changed with explicit permission of Thorsten Lockert (tholo sigmasoft com)
 1.14 04-May-2004  grant --unlink is the default, the argument is ignored and only accepted for
compatibility with other tar(1) implementations.
 1.13 09-Jul-2003  wiz branches: 1.13.2; 1.13.4;
Bump date for -O change.
 1.12 08-Jul-2003  simonb Add support for gnutar's -O "extract to stdout" option.
 1.11 10-Apr-2003  grant some mdoc fixes.
 1.10 08-Apr-2003  christos PR/21064: Perry Metzger: Re-add -k to tar (--keep-old-files)
 1.9 04-Apr-2003  wiz Remove sentence describing pax's -X option. From Jim Bernard in PR 21009.
 1.8 25-Feb-2003  wiz Add support for '-j' to create (and extract, but -z already did that)
bzip2 compressed tar files, for GNU tar compatibility.
Patch from Soren Jacobsen in PR 19467, okayed by christos.
Sync usage with options while here.
 1.7 19-Jan-2003  wiz tar(1) has no -L any longer.
 1.6 06-Nov-2002  wiz Drop a trailing space.
 1.5 06-Nov-2002  enami Let the roff to process the end of sentence.
 1.4 16-Oct-2002  christos add commas between options. Suggested by wiz.
 1.3 16-Oct-2002  christos Document the long options.
 1.2 12-Oct-2002  wiz Miscellaneous fixes.
 1.1 12-Oct-2002  christos man pages from OpenBSD.
TODO: add long option documentation.
 1.13.4.3 25-Aug-2004  he Pull up revision 1.14 (requested by cube in ticket #1734):
Document that ``--unlink'' is the default, and that it is
therefore ignored and only accepted for compatibility with
other tar(1) implememtations.
 1.13.4.2 07-Apr-2004  jmc Pullup rev 0-1.13 (requested by rafal in ticket #1021)

Pullup pax to current version on trunk. Includes many fixes.
 1.13.4.1 09-Jul-2003  jmc file tar.1 was added on branch netbsd-1-6 on 2004-04-07 06:58:52 +0000
 1.13.2.2 16-Sep-2004  jmc branches: 1.13.2.2.2;
Pullup rev 1.15 (requested by hubertf in ticket #850)

Change copyright to 2-clause BSD-copyright.
 1.13.2.1 09-May-2004  jdc Pull up revision 1.14 (requested by grant in ticket #263)

--unlink is the default, the argument is ignored and only accepted for
compatibility with other tar(1) implementations.
 1.13.2.2.2.1 23-Jul-2005  snj Apply patch (requested by riz in ticket #5512):
Sync pax with HEAD of 2005-07-22.
 1.22.4.1 28-Mar-2010  snj Pull up following revision(s) (requested by dholland in ticket #1391):
bin/pax/tar.1: revision 1.28, 1.29 via patch
The -X option of tar names a file containing exception globs, not
exception filenames, same as in gtar. PR 41168 from Perry.
--
bump date for previous
 1.25.4.1 09-Jan-2008  matt sync with HEAD
 1.26.12.2 28-Mar-2010  snj Pull up following revision(s) (requested by dholland in ticket #1354):
bin/pax/tar.1: revision 1.28, 1.29
The -X option of tar names a file containing exception globs, not
exception filenames, same as in gtar. PR 41168 from Perry.
--
bump date for previous
 1.26.12.1 23-Nov-2008  riz branches: 1.26.12.1.4;
Pull up following revision(s) (requested by dholland in ticket #113):
bin/pax/tar.1: revision 1.27
"compatibility", not "compatability".
 1.26.12.1.4.1 21-Apr-2010  matt sync to netbsd-5
 1.30.4.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.32.2.1 17-Apr-2012  yamt sync with head
 1.37.6.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.37.6.2 21-Apr-2020  martin Sync with HEAD
 1.37.6.1 10-Jun-2019  christos Sync with HEAD
 1.76 05-Aug-2024  riastradh pax(1): Nix trailing whitespace.

No functional change intended.
 1.75 20-Mar-2019  gutteridge branches: 1.75.12;
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).
 1.74 30-Nov-2018  christos Add base-256 decoding support (Micha Gorny)
 1.73 19-Dec-2015  christos branches: 1.73.14; 1.73.16;
PR/50119: Thomas Klausner: Add --timestamp option to tar.
 1.72 16-Jun-2015  christos remove \n from error messages
 1.71 24-Jan-2013  christos Recognize extended attribute headers and warn about them since we are not
currently handling them.
 1.70 09-Aug-2012  christos branches: 1.70.2;
PR/46786: Simon Burge: After conversion to 64 bit time_t, tar/pax/cpio
erroneously think that negative time_t's never fit in 32 bits. Rework
conversion code to always use uintmax_t, and detect negative values.
XXX[1]: Perhaps we should do the same (use a signed conversion) for all
fields not just for time_t
XXX[2]: pullup for 6
 1.69 20-Mar-2012  matt Use C89 function definitions
 1.68 03-Nov-2011  christos Accept -X -
 1.67 14-Feb-2009  lukem branches: 1.67.6;
fix -Wsign-compare issues
 1.66 24-Feb-2008  joerg branches: 1.66.10;
Kill pre-4.4BSD compat code.
 1.65 23-Apr-2007  christos branches: 1.65.4; 1.65.8;
PR/36194: Greg A. Woods: add a '-V' flag to pax for verbose summary without
listing (and other minor fixes)
 1.64 18-Mar-2006  christos Coverity CID 343: Rework code so that we don't write after the end of the
array. The end effect is the same though.
 1.63 11-Feb-2006  dsl Change all the 'return(x)' to 'return c'.
Makes some other searches I want to do a bit easier.
 1.62 24-Apr-2005  christos PR/20071: Perry Metzger: --extract handling is broken. This patch fixes
Perry's example.
 1.61 20-Feb-2005  christos branches: 1.61.2;
The quagga-0.98.2 tar archive is busted. It was created with an old tar
program, and then someone appended to it a ustar record. Detect this and
warn, but don't treat it as an error.
 1.60 17-Oct-2004  dsl Improve error reporting for invalid tar archives.
 1.59 26-Sep-2004  christos In GNU-tar mode, don't split filenames to prefix+name because GNU tar does
not honor it.
 1.58 22-Sep-2004  christos Keep track of the length of the gnu long name/link hack, so that
we can append to it. Modify the code so that file_write is re-entrant,
even in the gnu long name/link hack.

The old code assumed that the buffer already contained the necessary
blocks to satisfy the read file request that contained the long
filename. This is not always the case, specially when we are dealing
with pipes which do shorter reads, thus having more probability
that a long file name will fall across a buffer boundary.

To reproduce, create a tar archive with a lot of gnu-long-names
(pkgsrc/devel/libsigc++2 is a good example), do a tar -tf to get
a list of filenames, compress it and do a tar -tzf to get another
list of the filenames. Notice that the two lists differ.
 1.57 14-Jul-2004  christos - Grr! unterminated buffer string in exclude destination buffer
- fix isfoo() ctype macros to take unsigned char.
 1.56 16-Jun-2004  christos PR/25934: Dieter Baron: hard links with long names still fail.
As mentioned in the previous commit, the switch statement in the longlink()
needed simplification and it was a bit incorrect. Only depend on the passed
type to determine what kind of gnu longlink to produce. Don't try to deduce
it from the archive file type.
 1.55 15-Jun-2004  christos PR/25934: Dieter Baron: tar and hardlinks with long filenames don't work.
- always put the @LongLink tag on the name, not the long-link name.
- pass in what type of long name record we want to create; one for long-name
or long-link name.
XXX: We should get rid of the switch too.
 1.54 15-Jun-2004  christos Restructure the code to make it more readable. No functional changes.
 1.53 11-May-2004  christos Welcome to WARNS=3
 1.52 25-Apr-2004  christos PR/25299: grant beattie: 1.5.3/i386 kernel set archives confuse -current pax
PR/25310: Juan RP: tar can't unpack MS-DOS gzip files correctly
 1.51 20-Apr-2004  christos PR/25207: Hubert Feyrer: tar cores on certain archive contents
- On ustar archives with a specified prefix directory, arcn->nlen was not
computed properly.
- While I am here cast some snprintf's() to void.
 1.50 16-Apr-2004  christos PR/25200: Alan Barrett: bug in pax append mode. This problem was introduced
while trying to fix the tar append mode. Restore the correct 2 null blocks
at the end of file, and disable the old bug compatible mode for gnutar.
 1.49 12-Apr-2004  mrg fix bugs in the previous. use snprintf(). fixes the problem reported
in PR#25131.
 1.48 01-Apr-2004  mrg redo GNU tar --exclude emulation to actually work the same. unfortunately,
because alternation (|) isn't available in "pax -s" RE's, we have to pass
four (yes 4!) different patterns:
.*\/<pattern>$
.*\/<pattern>\/.*
^<pattern>$
^<pattern>\/.*
instead of the more elegant
(^|.*\/)<pattern>($|\/.*)

fixes a problem reported by simonb.
 1.47 30-Jan-2004  christos branches: 1.47.2;
While the comment is not correct anymore, this fixes the problem of tar -rf
where tar would skip backwards too much, corrupting the current tar archive
on append.
 1.46 24-Nov-2003  matt Don't emit any warnings when tar is trying to figure out if the file is
really tar format. valid CPIO files should not trigger a warning from tar.c
 1.45 27-Oct-2003  lukem Overhaul how `build.sh tools' are used:

* Rename "config.h" to "nbtool_config.h" and
HAVE_CONFIG_H to HAVE_NBTOOL_CONFIG_H.
This makes in more obvious in the source when we're using
tools/compat/config.h versus "standard autoconf" config.h

* Consistently move the inclusion of nbtool_config.h to before
<sys/cdefs.h> so that the former can provide __RCSID() (et al),
and there's no need to protect those macros any more.

These changes should make it easier to "tool-ify" a program by adding:
#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
to the top of the source files (for the general case).
 1.44 13-Oct-2003  agc Move Keith Muller's code from a 4-clause to a 3-clause licence by removing
the advertising clause. Diffs provided in PR 22397 by Joel Baker, confirmed
to the board by Keith Muller.
 1.43 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.42 24-Jun-2003  grant backout previous; it was a missing prototype elsewhere.

noted by mrg.
 1.41 24-Jun-2003  grant add cast to fix an integer -> pointer warning.
 1.40 23-Jun-2003  grant consistently use "cannot" instead of "can not".
 1.39 20-Apr-2003  christos PR/6195: Matt Watson: Pax fails for paths of exactly 100 characters.
make sure that we don't strlcpy more than the source buffer.
 1.38 31-Mar-2003  christos PR/18959: Xavier HUMBERT: Don't pay attention to sockets in gnu-tar mode.
 1.37 31-Mar-2003  christos RP/8227: Peter Seeback: Emulate old gnu tar better.
 1.36 09-Jan-2003  christos remove debugging printf's I accidentally committed.
 1.35 09-Jan-2003  christos print a bit more info about checksum mismatches.
 1.34 08-Dec-2002  mrg oops, remove a debug statement from the previous.
 1.33 08-Dec-2002  mrg add --exclude support.

XXX: --exclude & --exclude-from are still not 100% compatible, but since
rafal fixed --exclude-from's most annoying bug, they mostly work now.
 1.32 27-Oct-2002  christos PR/18831: Andreas Wrede: Don't exit with an error when we are reading a gnu
tar archive.
 1.31 18-Oct-2002  christos Only warn the user once about gnu extensions. From Eric Gillespie.
 1.30 17-Oct-2002  christos Factor out some more code, and don't expand names in ././@LongLink records.
 1.29 17-Oct-2002  christos Add proper longlink support. Previously we handled longname support, and
the longlink support was completely wrong.
 1.28 16-Oct-2002  christos turn gnu tar off by pax and cpio front ends.
 1.27 16-Oct-2002  christos Warn if we are trying to read a gnu tar archive, if --strict is specified.
 1.26 16-Oct-2002  christos Support gnu long filename extensions by default for tar on create, list, and
extract. We now generate GNU tar archives by default ("ustar ^@" instead of
"ustar^@00"). GNU extensions can be disabled with --strict.

XXX: long symlinks untested.
 1.25 13-Oct-2002  christos Fix error in counting the archive's name length. From Eric Gillespie, thanks!
 1.24 13-Oct-2002  mrg make this build on LP64
 1.23 12-Oct-2002  christos merge OpenBSD changes:
- correct -C processing
- add ability to read filenames and flags from a file
- don't print dangerous escape sequences to the terminal
- use strlcpy/strncpy properly.
- handle tmpfile creation better.
- improve documentation of options.
- handle stdout/stderr list selection correctly.
- kill gzip when we get interrupted.
- simplify gzip setup.
- add more flags to programs.

additional changes:
- librmt processing.
- set POSIXLY_CORRECT in options parsing.
- prevent more string overruns.
- support -T

we don't turn the switch on to replace tar and cpio yet.
 1.22 11-Oct-2002  christos str{n,l}cpy fixes and buffer limit fixes from OpenBSD
 1.21 31-Jan-2002  tv branches: 1.21.2;
Protect __RCSID and __COPYRIGHT from being invoked if not defined.
 1.20 28-Oct-2001  lukem s/u_longlong_t/unsigned long long/ to shut up the parc port
 1.19 25-Oct-2001  lukem - ansi KNF (just remove all the #ifndef __STDC__ prototype junk,
leaving the ansi stuff)
- use longlong_t instead of quad_t (etc), and rename *uqd*() -> *ull*()
- clean up the NET2_STAT stuff similar to ftpd; provide #defines and
macros which select which cast to use, etc
- clean up the NET2_FTS and NET2_REGEX #define use
 1.18 17-Feb-2000  itohy Cleanup and fix typos.
Partially from FreeBSD.
 1.17 19-Jan-2000  mycroft Fix a glitch with handling files larger than 2GB.
 1.16 22-Oct-1999  mrg support ././@LongLink extraction, as created by GNU tar.
 1.15 18-Aug-1999  kleink branches: 1.15.2;
Add the ability to handle estranged V7 tar archives that use USTAR-style
directories; addresses PR bin/8220.
 1.14 21-Jan-1999  mycroft branches: 1.14.2;
Make this build on the Alpha.
 1.13 20-Jan-1999  mrg implement the gnutar -X flag in terms of pax -s. fix some bugs in pax -s handling while i am here...
 1.12 15-Oct-1998  mycroft Reconstruct a split file name correctly.
 1.11 10-Aug-1998  tv Transparently handle old-style GNU tar archives that may have garbage in
the trailing block and do not have an end-of-archive marker. (Does this
handling based on whether the ustar id is "ustar" or "ustar ".)
 1.10 28-Jul-1998  mycroft Use the uid/gid caching moved into libc. Fixed a memory leak!
 1.9 20-Jul-1997  christos branches: 1.9.2;
- Rename local warn so tty_warn that we can include <err.h> since this program
uses err() and errx().
- Fix printf format strings.
- Added WARNS=1; note
- Added missing prototypes.
- Split overloaded trail function into trail() and subtrail().
 1.8 25-Jun-1997  kleink Fix OBOB: when reassembling a file name from the header block's `prefix'
and `name' fields, don't overwrite the glueing '/' between them.
 1.7 16-May-1997  kleink Use '0' to front-pad octal strings, not ' '.
 1.6 11-Jan-1997  tls kill 'register'
 1.5 21-Mar-1995  cgd convert to new RCS id conventions.
 1.4 23-Sep-1994  mycroft Eliminate uses of some obsolete functions.
 1.3 14-Jun-1994  mycroft Fix up RCS ids.
 1.2 13-Jun-1994  jtc Add RCS ID's
 1.1 13-Jun-1994  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 13-Jun-1994  jtc From 4.4 lite
 1.9.2.1 15-Oct-1998  cgd pull up rev 1.12 from trunk (mycroft)
 1.14.2.2 21-Jan-2000  he Pull up revision 1.17 (requested by mycroft):
Fix bugs handling files larger than 2GB.
 1.14.2.1 20-Aug-1999  cgd pull up rev 1.15 from trunk (kleink)
 1.15.2.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.21.2.3 25-Aug-2004  he Pull up revisions 1.49-1.52,1.54-1.57 (via patch, requested by cube in ticket #1737):
Fix bug in --exclude emulation, and use snprintf. (PR#25131)
Fix bug in pax append mode. (PR#25200)
Fix handling of certain archives, fixing core dump. (PR#25207)
Fix problems with 1.5.3/i386 kernel sets (PR#25299) and
MS-DOS gzip files (PR#25310).
Slight code restructuring.
Fix handling of tar with hardlinks to long file names
(PR#25934, PR#25934).
Fix unterminated buffer string in exaclude destination buffer.
Fix isfoo() ctype macros to take unsigned char.
 1.21.2.2 16-Jun-2004  jmc fix botched pullup from ticket #1021 and make sure this compiles on
non-NetBSD hosts again. Also fixes PR#24481
 1.21.2.1 07-Apr-2004  jmc Pullup rev 1.22-1.48 (requested by rafal in ticket #1021)

Pullup pax to current version on trunk. Includes many fixes.
 1.47.2.8 12-Nov-2004  jmc branches: 1.47.2.8.2;
Pullup rev 1.58 (requested by christos in ticket #921)

Keep track of the length of the gnu long name/link hack, so that
we can append to it. Modify the code so that file_write is re-entrant,
even in the gnu long name/link hack.
 1.47.2.7 12-Nov-2004  jmc Pullup rev 1.59 (requested by christos in ticket #920)

In GNU-tar mode, dontt split filenames to prefix+name because GNU tar does
not honor it.
 1.47.2.6 15-Jul-2004  tron Pull up revision 1.57 (requested by hubertf in ticket #654):
- Grr! unterminated buffer string in exclude destination buffer
- fix isfoo() ctype macros to take unsigned char.
 1.47.2.5 18-Jun-2004  tron Pull up revision 1.54-1.56 via patch (requested by dillo in ticket #507):
Restructure the code to make it more readable. No functional changes.
PR/25934: Dieter Baron: tar and hardlinks with long filenames don't work.
- always put the @LongLink tag on the name, not the long-link name.
- pass in what type of long name record we want to create; one for long-name
or long-link name.
XXX: We should get rid of the switch too.
PR/25934: Dieter Baron: hard links with long names still fail.
As mentioned in the previous commit, the switch statement in the longlink()
needed simplification and it was a bit incorrect. Only depend on the passed
type to determine what kind of gnu longlink to produce. Don't try to deduce
it from the archive file type.
 1.47.2.4 28-Apr-2004  grant Pull up revision 1.52 (requested by snj in ticket #217):

PR/25299: grant beattie: 1.5.3/i386 kernel set archives confuse -current pax
PR/25310: Juan RP: tar can't unpack MS-DOS gzip files correctly
 1.47.2.3 21-Apr-2004  jmc Pullup rev 1.50-1.51 (requested by christos in ticket #170)

Fixes for multiple pax pr's. PR#25200. PR#25207.
 1.47.2.2 19-Apr-2004  jmc Pullup rev 1.49 (requested by mrg in ticket #145)

Fix bugs in the previous. use snprintf(). PR#25131
 1.47.2.1 19-Apr-2004  jmc Pullup rev 1.48 (requested by mrg in ticket #155)

Redo GNU tar --exclude emulation to actually work the same.
 1.47.2.8.2.1 23-Jul-2005  snj Apply patch (requested by riz in ticket #5512):
Sync pax with HEAD of 2005-07-22.
 1.61.2.1 09-Nov-2005  tron Pull up following revision(s) (requested by christos in ticket #946):
bin/pax/tar.c: revision 1.62
PR/20071: Perry Metzger: --extract handling is broken. This patch fixes
Perry's example.
 1.65.8.1 24-Mar-2008  keiichi sync with head.
 1.65.4.1 23-Mar-2008  matt sync with HEAD
 1.66.10.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.67.6.4 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.67.6.3 30-Oct-2012  yamt sync with head
 1.67.6.2 17-Apr-2012  yamt sync with head
 1.67.6.1 10-Nov-2011  yamt sync with head
 1.70.2.1 25-Feb-2013  tls resync with head
 1.73.16.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.73.16.2 21-Apr-2020  martin Sync with HEAD
 1.73.16.1 10-Jun-2019  christos Sync with HEAD
 1.73.14.1 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.75.12.1 02-Aug-2025  perseant Sync with HEAD
 1.10 24-Jan-2013  christos branches: 1.10.32;
Recognize extended attribute headers and warn about them since we are not
currently handling them.
 1.9 11-May-2004  christos branches: 1.9.48; 1.9.54;
Welcome to WARNS=3
 1.8 13-Oct-2003  agc branches: 1.8.4;
Move Keith Muller's code from a 4-clause to a 3-clause licence by removing
the advertising clause. Diffs provided in PR 22397 by Joel Baker, confirmed
to the board by Keith Muller.
 1.7 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.6 12-Oct-2002  christos merge OpenBSD changes:
- correct -C processing
- add ability to read filenames and flags from a file
- don't print dangerous escape sequences to the terminal
- use strlcpy/strncpy properly.
- handle tmpfile creation better.
- improve documentation of options.
- handle stdout/stderr list selection correctly.
- kill gzip when we get interrupted.
- simplify gzip setup.
- add more flags to programs.

additional changes:
- librmt processing.
- set POSIXLY_CORRECT in options parsing.
- prevent more string overruns.
- support -T

we don't turn the switch on to replace tar and cpio yet.
 1.5 17-Feb-2000  itohy branches: 1.5.6;
Cleanup and fix typos.
Partially from FreeBSD.
 1.4 22-Oct-1999  mrg support ././@LongLink extraction, as created by GNU tar.
 1.3 21-Mar-1995  cgd branches: 1.3.10;
convert to new RCS id conventions.
 1.2 13-Jun-1994  jtc Add RCS ID's
 1.1 13-Jun-1994  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 13-Jun-1994  jtc From 4.4 lite
 1.3.10.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.5.6.1 07-Apr-2004  jmc Pullup rev 1.6-1.8 (requested by rafal in ticket #1021)

Pullup pax to current version on trunk. Includes many fixes.
 1.8.4.1 23-Jul-2005  snj Apply patch (requested by riz in ticket #5512):
Sync pax with HEAD of 2005-07-22.
 1.9.54.1 25-Feb-2013  tls resync with head
 1.9.48.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.10.32.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.10.32.1 21-Apr-2020  martin Sync with HEAD
 1.19 23-Apr-2007  christos branches: 1.19.78;
PR/36194: Greg A. Woods: add a '-V' flag to pax for verbose summary without
listing (and other minor fixes)
 1.18 11-Feb-2006  dsl Change all the 'return(x)' to 'return c'.
Makes some other searches I want to do a bit easier.
 1.17 10-Oct-2004  christos PR/27209: Greg A. Woods: typo in comment in pax/tty_subs.c
 1.16 27-Oct-2003  lukem branches: 1.16.4;
Overhaul how `build.sh tools' are used:

* Rename "config.h" to "nbtool_config.h" and
HAVE_CONFIG_H to HAVE_NBTOOL_CONFIG_H.
This makes in more obvious in the source when we're using
tools/compat/config.h versus "standard autoconf" config.h

* Consistently move the inclusion of nbtool_config.h to before
<sys/cdefs.h> so that the former can provide __RCSID() (et al),
and there's no need to protect those macros any more.

These changes should make it easier to "tool-ify" a program by adding:
#if HAVE_NBTOOL_CONFIG_H
#include "nbtool_config.h"
#endif
to the top of the source files (for the general case).
 1.15 13-Oct-2003  agc Move Keith Muller's code from a 4-clause to a 3-clause licence by removing
the advertising clause. Diffs provided in PR 22397 by Joel Baker, confirmed
to the board by Keith Muller.
 1.14 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.13 04-Mar-2003  christos No other program prints errors inside angle brackets!
 1.12 09-Feb-2003  grant cosmetic error condition improvements.
- sprinkle some fflush() to print filenames correctly.
- print a \n to stdout to fix formatting.
- English, consistently use "Cannot" in error messages.

some whitespace cleanup.
 1.11 31-Jan-2002  tv branches: 1.11.2;
Protect __RCSID and __COPYRIGHT from being invoked if not defined.
 1.10 25-Oct-2001  lukem - ansi KNF (just remove all the #ifndef __STDC__ prototype junk,
leaving the ansi stuff)
- use longlong_t instead of quad_t (etc), and rename *uqd*() -> *ull*()
- clean up the NET2_STAT stuff similar to ftpd; provide #defines and
macros which select which cast to use, etc
- clean up the NET2_FTS and NET2_REGEX #define use
 1.9 24-Sep-2001  wiz va_{start,end} audit:
Make sure that each va_start has one and only one matching va_end,
especially in error cases.
If the va_list is used multiple times, do multiple va_starts/va_ends.
If a function gets va_list as argument, don't let it use va_end (since
it's the callers responsibility).

Improved by comments from enami and christos -- thanks!

Heimdal/krb4/KAME changes already fed back, rest to follow.

Inspired by, but not not based on, OpenBSD.
 1.8 17-Feb-2000  itohy Cleanup and fix typos.
Partially from FreeBSD.
 1.7 20-Jul-1997  christos - Rename local warn so tty_warn that we can include <err.h> since this program
uses err() and errx().
- Fix printf format strings.
- Added WARNS=1; note
- Added missing prototypes.
- Split overloaded trail function into trail() and subtrail().
 1.6 11-Jan-1997  tls kill 'register'
 1.5 21-Mar-1995  cgd convert to new RCS id conventions.
 1.4 11-Oct-1994  jtc use strerror() instead of sys_errlist[]
 1.3 14-Jun-1994  mycroft Fix up RCS ids.
 1.2 13-Jun-1994  jtc Add RCS ID's
 1.1 13-Jun-1994  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 13-Jun-1994  jtc From 4.4 lite
 1.11.2.2 16-Jun-2004  jmc fix botched pullup from ticket #1021 and make sure this compiles on
non-NetBSD hosts again. Also fixes PR#24481
 1.11.2.1 07-Apr-2004  jmc Pullup rev 1.12-1.16 (requested by rafal in ticket #1021)

Pullup pax to current version on trunk. Includes many fixes.
 1.16.4.1 23-Jul-2005  snj Apply patch (requested by riz in ticket #5512):
Sync pax with HEAD of 2005-07-22.
 1.19.78.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.19.78.1 21-Apr-2020  martin Sync with HEAD
 1.31 14-Sep-2021  christos Provide symbolic printing of some keywords by capitalizing them.
 1.30 06-Apr-2021  simonb We only need -Wno-format-y2k for print.c .
 1.29 14-Aug-2011  christos branches: 1.29.44;
Document non-literal formats
 1.28 19-Jun-2002  jdolecek g/c procfs fallback code, it's redundant now that we use sysctl to get
process info
 1.27 26-May-2000  simonb branches: 1.27.6;
Use new sysctl/kvm interfaces. This will stop the "proc size mismatch"
errors when internal kernel structures change size. Also remove
the sgid bit - all live kernel data is accessed through the sysctl
interface.
 1.26 12-Sep-1999  chs the PMAP_NEW option is gone as the new interface is no longer optional.
 1.25 26-Mar-1999  bgrayson Added experimental fallback /proc-based lookup. Warnings are printed when the
fallback method is used, as the results could be untrustworthy if an
intruder is present. It is highly likely that NetBSD-1.5 will have
an improved kvm interface for reading process information, at which
point this code can be garbage-collected. Also added a word to the
man page -x option description while I was here.
 1.24 24-Mar-1999  mrg completely remove Mach VM support. all that is left is the all the
header files as UVM still uses (most of) these.
 1.23 25-Aug-1998  tv Add CWARNFLAGS+=-Wno-format-y2k.
 1.22 25-Aug-1998  ross Kill the CFLAGS+=-Wno-format-y2k which will go, for now, into bsd.sys.mk
It wasn't working anyway, although that could have been fixed by moving it
down below the .include <bsd.prog.mk> line.

It's not completely clear that this usage of %y is in fact y2k-safe, it
seems safer for now to have a single XXX option in bsd.sys.mk, and we need
to rev this anyway for -Wno-uninitialized
 1.21 21-Aug-1998  tv This needs strftime()'s %y; use -Wno-format-y2k.
 1.20 18-Feb-1998  perry add -DPMAP_NEW if PMAP_NEW is on in mk.conf
 1.19 09-Feb-1998  mrg add -DUVM to CPPFLAGS if defined(UVM)
 1.18 20-Jul-1997  christos Remove WARNS=1 from all the subdirectory Makefiles, and add it to Makefile.inc
now that all /bin has been cleaned.
 1.17 20-Jul-1997  christos Fix compiler warnings
Add WARNS=1
 1.16 26-Oct-1995  jtc LIBMATH -> LIBM (PR# 1688)
 1.15 21-May-1995  mycroft Split out some helper functions again.
 1.14 18-May-1995  mycroft Create and strvis(3) the argument and environment lists only as needed.
Do not keep extra copies around.
 1.13 21-Mar-1995  cgd convert to new RCS id conventions.
 1.12 03-Aug-1994  deraadt do not need -I/sys
 1.11 03-Jul-1994  deraadt devname is in libc now
 1.10 17-May-1994  pk Restore "${DESTDIR}" in CFLAGS.
 1.9 12-May-1994  mycroft Need devname.c.
 1.8 09-May-1994  cgd clean up import
 1.7 28-Jan-1994  cgd needs -lkvm, not -lutil
 1.6 14-Aug-1993  mycroft Fix library dependencies.
 1.5 01-Aug-1993  mycroft Add RCS identifiers.
 1.4 17-Jun-1993  mycroft Don't need -I/sys. (Why was it here?)
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 09-May-1994  cgd new ps, to match libkvm
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.27.6.1 23-Apr-2002  nathanw Add support for handling multi-LWP processes by selecting one LWP as
"representative" of the process.
Also add a -s option that prints a line for each LWP.
While I'm here, remove the /proc fallback code.
 1.29.44.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.29.44.1 21-Apr-2020  martin Sync with HEAD
 1.7 03-Jul-1994  mycroft Clean up deleted files.
 1.6 09-May-1994  cgd clean up import
 1.5 01-Aug-1993  mycroft Add RCS identifiers.
 1.4 21-Apr-1993  proven Changed hash_open to dbopen.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 09-May-1994  cgd new ps, to match libkvm
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.39 09-Dec-2017  kamil branches: 1.39.4;
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>
 1.38 02-Dec-2016  christos provide a tree like display with -d, from FreeBSD
 1.37 28-Nov-2016  rin Calculate CPU usage (pcpu) once per process if it is required. This change
significantly improves performance for slow machines when output is sorted
by pcpu.

ok martin
 1.36 28-Nov-2016  rin for donlist{,_sysctl}:
- obtain log_ccpu = log(ccpu) rather than ccpu itself
- use common default values and warn users appropriately when errors occur
ok martin
 1.35 20-Apr-2014  dholland branches: 1.35.6;
Use an enum type for PRINTMODE vs. WIDTHMODE. Compiler output diffs have
been checked.
 1.34 15-Jan-2014  mlelstv branches: 1.34.2;
Add an LTIME column that prints lwp cputime.
 1.33 31-May-2010  rmind branches: 1.33.6; 1.33.12;
Fix ps(1) and top(1) to show reasonable CPU numbers i.e. cpu_index() provided
by the kernel, instead of CPU order number, which is generally random.
 1.32 10-Feb-2008  christos Add a CPUID keyword; idea and some of the code from Anon Ymous
 1.31 01-Sep-2007  yamt add a keyword "lname", to print l_name.
 1.30 29-Oct-2006  christos branches: 1.30.8;
Add support for printing the emulation of the process.
 1.29 02-Oct-2006  apb * When all columns are given null customised headers, the blank header
line is not printed at all. This is specified in P1003.1-2004
(SUSv3), and is useful.

* Customised headers may contain embedded space, commas and equals
signs. To specify multiple customised headers, use multiple -o or -O
options. This is specified (for "-o", not for "-O") in P1003.1-2004
(SUSv3), and is useful.

* When a column is given a null (blank) customised header, it keeps its
default minimum width. This is specified in P1003.1-2004 (SUSv3), and
seems harmless.

* Fix a bug that made it impossible to print the same keyword multiple
times, with different customised headers each time. (Previously, the
last customised header was used for all instances of the keyword.)

* Make the behaviour of "-O" more useful. The first -O option adds
the default keywords only if there have not yet been any formatting
options, and multiple -O options now insert their keywords in adjacent
positions. Now {ps -j -O %cpu} is like {ps -j} with one extra column;
Previously, it would have had all the columns implied by "-j", plus
all the default columns, plus the extra column specified by "-O".

* Convert from home-grown linked lists to SIMPLEQ lists.

Discussed in tech-userlevel.
 1.28 26-Jun-2005  christos sprinkle a little const, and now everything compiles with WARNS=3
 1.27 27-Mar-2004  simonb Add support for the POSIX.2 "etime" keyword - shows the elapsed time
since the process was started. Fix a couple of style nits as well.

Fix for part of PR standards/11224.
 1.26 27-Mar-2004  simonb KNF, ANSFify, de-__P, unC++ify, white space nits.
 1.25 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.24 06-Mar-2003  dsl - add keywords for utime, stime and ctime (output sss.ssssss if small)
- fiddle with keyword table - mainly to keep width under 80 cols
- add -k keylist so sort output
- don't use logs to work out field width, get -MAXINT right
- user defined headers can be 1 char (actually they can be zero length)
so ?? in tty column isn't guaranteed to be wider than header
- remove some long long casts (compiles on sparc64 still)
- use kvm_openfiles(NULL, NULL, NULL, KVN_NO_FILES, ..) unless core file
specified,
(approved by christos)
 1.23 01-Mar-2003  atatat Add p_svuid and p_svgid to kinfo_proc2. Populate them in the kernel
and in libkvm. Then teach ps how to show them to you.

Also, teach ps how to show the names for all the uids, the rest of the
group numbers, and the "group access list".
 1.22 18-Jan-2003  thorpej Merge the nathanw_sa branch.
 1.21 19-Jun-2002  jdolecek g/c procfs fallback code, it's redundant now that we use sysctl to get
process info
 1.20 14-Jul-2001  matt branches: 1.20.2;
Retrieve maxslp & uspace from kernel instead of using defines. Needed for
shared arm code.
 1.19 07-Jun-2000  simonb Calculate field widths on the fly so that all columns line up nicely
and make more effective use of screen real estate when some columns
(eg USER and VSZ) didn't need the full default width.
 1.18 02-Jun-2000  simonb G/C evar() - now that everything is in the one structure it was exactly
the same as pvar().
 1.17 26-May-2000  simonb branches: 1.17.2;
Use new sysctl/kvm interfaces. This will stop the "proc size mismatch"
errors when internal kernel structures change size. Also remove
the sgid bit - all live kernel data is accessed through the sysctl
interface.
 1.16 03-Dec-1999  simonb Since we have an "extern.h", put all external variable declarations in
that file and not sprinkled around various .c files.
 1.15 03-Dec-1999  simonb "ps -e" only shows the environment for the processes owned by the
current user id or all process if run by root.

Fixes PR security/5967 from Todd Vierling.
 1.14 15-Oct-1999  jdolecek When using the procfs for extracting process information, extract
also process start time, process arguments and session leadership status.

The procfs fallback is also used when kvm_openfiles() completely fails
(e.g. when /dev/mem is not readable).

Solves PR 7772, though the final implementation is different.
 1.13 26-Mar-1999  bgrayson branches: 1.13.2; 1.13.4;
Added experimental fallback /proc-based lookup. Warnings are printed when the
fallback method is used, as the results could be untrustworthy if an
intruder is present. It is highly likely that NetBSD-1.5 will have
an improved kvm interface for reading process information, at which
point this code can be garbage-collected. Also added a word to the
man page -x option description while I was here.
 1.12 06-Feb-1998  mrg remove old OLD BSD VM code.
 1.11 02-Oct-1996  ws Fix p_nice vs. NZERO code.
Remove some unnecessary casts.
Remove an unused time format field.
 1.10 21-May-1995  mycroft Split out some helper functions again.
 1.9 18-May-1995  mycroft Create and strvis(3) the argument and environment lists only as needed.
Do not keep extra copies around.
 1.8 21-Mar-1995  cgd convert to new RCS id conventions.
 1.7 03-Jul-1994  deraadt devname is in libc now
 1.6 16-Jun-1994  pk Declare devname().
 1.5 09-May-1994  cgd clean up import
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 09-May-1994  cgd new ps, to match libkvm
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.13.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.13.2.1 08-Nov-1999  cgd pull up rev 1.14 from trunk (requested by jdolecek):
When using the procfs for extracting process information, extract process
start time, arguments, and session leadership status. Also, fall back to
procfs when kvm_openfiles() completely fails (e.g. when /dev/mem is not
readable). Fixes PR#7772.
 1.17.2.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.20.2.3 24-Apr-2002  nathanw Note the presense of multiple LWPs or scheduler activations under "state".
Add a "lstate" that prints LWP-specific state and display it instead
of "state" under ps -s.
 1.20.2.2 23-Apr-2002  nathanw Add support for handling multi-LWP processes by selecting one LWP as
"representative" of the process.
Also add a -s option that prints a line for each LWP.
While I'm here, remove the /proc fallback code.
 1.20.2.1 14-Jul-2001  nathanw file extern.h was added on branch nathanw_sa on 2002-04-23 20:41:12 +0000
 1.30.8.2 23-Mar-2008  matt sync with HEAD
 1.30.8.1 06-Nov-2007  matt sync with HEAD
 1.33.12.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.33.6.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.34.2.1 10-Aug-2014  tls Rebase.
 1.35.6.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.39.4.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.39.4.1 21-Apr-2020  martin Sync with HEAD
 1.21 12-Dec-2007  lukem branches: 1.21.70;
use __RCSID()
 1.20 27-Mar-2004  simonb branches: 1.20.22;
Wrap some long lines.
Consisently use (void) in front of printf(...);
A few more white-space nits.
 1.19 27-Mar-2004  simonb KNF, ANSFify, de-__P, unC++ify, white space nits.
 1.18 14-Feb-2002  enami branches: 1.18.2;
Just use return value of strvis instead of calling strlen.
 1.17 14-Feb-2002  enami Don't define variable as static unnecessarily.
 1.16 23-Mar-2001  enami Always initialize an auto variable.
 1.15 20-Mar-2001  itojun do not update maxlen if realloc fails.
 1.14 03-Dec-1999  simonb Since we have an "extern.h", put all external variable declarations in
that file and not sprinkled around various .c files.
 1.13 03-Feb-1998  perry branches: 1.13.6;
add <sys/time.h> to fix compiler error
 1.12 09-Jan-1998  perry RCS Id Police.
 1.11 20-Jul-1997  christos Fix compiler warnings
Add WARNS=1
 1.10 25-May-1995  mycroft Simplify this just a little.
 1.9 21-May-1995  mycroft Split out some helper functions again.
 1.8 18-May-1995  mycroft Create and strvis(3) the argument and environment lists only as needed.
Do not keep extra copies around.
 1.7 18-May-1995  mycroft Do the right thing for an empty environment.
 1.6 18-May-1995  mycroft Avoid an extra (gratuitous) pass through strvis(3).
 1.5 21-Mar-1995  cgd convert to new RCS id conventions.
 1.4 08-Jan-1995  mycroft Better fix for the previous.
 1.3 08-Jan-1995  mycroft Fix oversight in previous change, when argument list is sort of empty.
 1.2 18-May-1994  cgd kill extra space, save my sanity
 1.1 09-May-1994  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 09-Jan-1997  tls Import from 4.4BSD-Lite2
 1.1.1.1 09-May-1994  cgd new ps, to match libkvm
 1.13.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.18.2.2 14-Feb-2002  enami Just use return value of strvis instead of calling strlen.
 1.18.2.1 14-Feb-2002  enami file fmt.c was added on branch nathanw_sa on 2002-02-14 06:57:20 +0000
 1.20.22.1 09-Jan-2008  matt sync with HEAD
 1.21.70.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.21.70.1 21-Apr-2020  martin Sync with HEAD
 1.60 15-Sep-2021  christos Don't assign v to newvar, so we can still access the original length.
 1.59 14-Sep-2021  christos use emalloc and friends, add ktrace flag printing.
 1.58 14-Sep-2021  christos Provide symbolic printing of some keywords by capitalizing them.
 1.57 06-Aug-2019  kamil Restore maxrss, idrss, isrss, ixrss printing in ps(1)

The RSS related statistics are now back in the NetBSD kernel.

These values were disabled since day0 until today.

libkvm(3) users will still receive inappropriate values as RSS statistics
are updated upon sysctl(3) call.

Patch submitted by <Krzysztof Lasocki>
 1.56 11-Apr-2018  christos branches: 1.56.2; 1.56.4;
use EXIT_FAILURE instead of 1
 1.55 09-Dec-2017  kamil branches: 1.55.2;
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>
 1.54 15-Jan-2014  mlelstv Add an LTIME column that prints lwp cputime.
 1.53 21-Oct-2009  rmind branches: 1.53.6; 1.53.12;
Remove uarea swap-out functionality:

- Addresses the issue described in PR/38828.
- Some simplification in threading and sleepq subsystems.
- Eliminates pmap_collect() and, as a side note, allows pmap optimisations.
- Eliminates XS_CTL_DATA_ONSTACK in scsipi code.
- Avoids few scans on LWP list and thus potentially long holds of proc_lock.
- Cuts ~1.5k lines of code. Reduces amd64 kernel size by ~4k.
- Removes __SWAP_BROKEN cases.

Tested on x86, mips, acorn32 (thanks <mpumford>) and partly tested on
acorn26 (thanks to <bjh21>).

Discussed on <tech-kern>, reviewed by <ad>.
 1.52 10-Feb-2008  christos Add a CPUID keyword; idea and some of the code from Anon Ymous
 1.51 01-Sep-2007  yamt add a keyword "lname", to print l_name.
 1.50 29-Oct-2006  christos branches: 1.50.8;
Add support for printing the emulation of the process.
 1.49 16-Oct-2006  christos c99 initializers
 1.48 02-Oct-2006  apb Set the width in the correct data structure. (Fix error in previous.)
 1.47 02-Oct-2006  apb * When all columns are given null customised headers, the blank header
line is not printed at all. This is specified in P1003.1-2004
(SUSv3), and is useful.

* Customised headers may contain embedded space, commas and equals
signs. To specify multiple customised headers, use multiple -o or -O
options. This is specified (for "-o", not for "-O") in P1003.1-2004
(SUSv3), and is useful.

* When a column is given a null (blank) customised header, it keeps its
default minimum width. This is specified in P1003.1-2004 (SUSv3), and
seems harmless.

* Fix a bug that made it impossible to print the same keyword multiple
times, with different customised headers each time. (Previously, the
last customised header was used for all instances of the keyword.)

* Make the behaviour of "-O" more useful. The first -O option adds
the default keywords only if there have not yet been any formatting
options, and multiple -O options now insert their keywords in adjacent
positions. Now {ps -j -O %cpu} is like {ps -j} with one extra column;
Previously, it would have had all the columns implied by "-j", plus
all the default columns, plus the extra column specified by "-O".

* Convert from home-grown linked lists to SIMPLEQ lists.

Discussed in tech-userlevel.
 1.46 18-Mar-2006  christos Coverity CID 1559: Fix memory leak.
 1.45 05-Nov-2005  chs add a keyword for the uarea address. very useful with crashdumps.
 1.44 26-Jun-2005  christos sprinkle a little const, and now everything compiles with WARNS=3
 1.43 16-Nov-2004  christos branches: 1.43.2;
print TTY instead of TT, one question mark instead of two and always the
full tty name.
 1.42 23-Aug-2004  nathanw Add a "laddr" keyword, analagous to "paddr". Useful for finding
struct pcb's.
 1.41 28-Jul-2004  simonb For the "lid" keyword, change ULONG to INT32 since it's a 32bit type.
From Nicolas Joly in PR bin/26452.
 1.40 27-Mar-2004  simonb branches: 1.40.2;
Add support for the POSIX.2 "args" and "comm" keywords. "args" is just
an alias for "command", and "comm" shows just argv[0] and not the whole
argument list.

Fix for part of PR standards/11224.
 1.39 27-Mar-2004  simonb Add support for the POSIX.2 "etime" keyword - shows the elapsed time
since the process was started. Fix a couple of style nits as well.

Fix for part of PR standards/11224.
 1.38 27-Mar-2004  simonb Wrap some long lines.
Consisently use (void) in front of printf(...);
A few more white-space nits.
 1.37 27-Mar-2004  simonb Change the header for the "tty" keyword "TT" instead of "TTY".

Fixed POSIX.2 conformance problem mentioned in standards/11225 from
Ben Harris.
 1.36 16-Dec-2003  mjl Correct typo in page header for tpgid.
 1.35 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.34 08-Mar-2003  dsl Support -ktt and -ktty, quote "tdev" in comment so table can be sorted.
 1.33 08-Mar-2003  christos XXX: cast tdev to INT32 instead of UINT32 so that that sorting works as
before [NODEV entries come first]
 1.32 06-Mar-2003  dsl - add keywords for utime, stime and ctime (output sss.ssssss if small)
- fiddle with keyword table - mainly to keep width under 80 cols
- add -k keylist so sort output
- don't use logs to work out field width, get -MAXINT right
- user defined headers can be 1 char (actually they can be zero length)
so ?? in tty column isn't guaranteed to be wider than header
- remove some long long casts (compiles on sparc64 still)
- use kvm_openfiles(NULL, NULL, NULL, KVN_NO_FILES, ..) unless core file
specified,
(approved by christos)
 1.31 03-Mar-2003  dsl Seems better to display p_sv{u,g}id when asked for it...
 1.30 01-Mar-2003  atatat Add p_svuid and p_svgid to kinfo_proc2. Populate them in the kernel
and in libkvm. Then teach ps how to show them to you.

Also, teach ps how to show the names for all the uids, the rest of the
group numbers, and the "group access list".
 1.29 18-Jan-2003  thorpej Merge the nathanw_sa branch.
 1.28 17-Oct-2002  itojun correct use of format string (signedness, %ldd). PR 18679
 1.27 24-Apr-2002  nathanw branches: 1.27.2;
Since kinfo_proc2 has many values that are 64-bit, ps needs to know
that they're 64-bit, and grab them out of memory appropriately. Otherwise,
big-endian systems get the wrong end of the 64-bit value and lose.

Keywords affected: inblk, majflt, minflt, msgrcv, msgsnd, nivcsw, nsigs,
nswap, nvcsw, and oublk.
 1.26 08-Jan-2001  itojun branches: 1.26.2;
address PR 11653. checked on alpha.
XXX supports 32bit and 64bit pointers only
 1.25 07-Jun-2000  simonb branches: 1.25.2;
Calculate field widths on the fly so that all columns line up nicely
and make more effective use of screen real estate when some columns
(eg USER and VSZ) didn't need the full default width.
 1.24 02-Jun-2000  simonb G/C evar() - now that everything is in the one structure it was exactly
the same as pvar().
 1.23 26-May-2000  simonb branches: 1.23.2;
Use new sysctl/kvm interfaces. This will stop the "proc size mismatch"
errors when internal kernel structures change size. Also remove
the sgid bit - all live kernel data is accessed through the sysctl
interface.
 1.22 11-May-2000  mjl Fix problem in option processing, would segfault when parsing "-O,".
Closes PR/10096 by Yuji Yamano using the patch provided.
 1.21 03-Dec-1999  simonb Since we have an "extern.h", put all external variable declarations in
that file and not sprinkled around various .c files.
 1.20 09-Nov-1999  drochner Since our gcc doesn't warn about NULL format strings anymore, we can
fix the incorrect err(1, "%s", "") et al.
Closes PR bin/7592 by cgd.
 1.19 03-May-1999  mrg branches: 1.19.4;
limit session to 6 chars. from Geoff Wing <gcw@pobox.com> and fixes his PR#7506
 1.18 16-Apr-1999  christos Add support for printing wide sigset_t properly.
 1.17 17-Feb-1998  thorpej Add a "sid" keyword to print the session ID.
 1.16 06-Feb-1998  mrg remove old OLD BSD VM code.
 1.15 20-Jul-1997  christos Fix compiler warnings
Add WARNS=1
 1.14 02-Oct-1996  ws Fix p_nice vs. NZERO code.
Remove some unnecessary casts.
Remove an unused time format field.
 1.13 30-May-1996  cgd in findvar(), return after warning about unknown keyword so we don't
deref. a null pointer.
 1.12 29-Sep-1995  cgd branches: 1.12.6;
correct the sizes specified for various variables.
 1.11 15-Aug-1995  cgd Kenneth Stailey pointed out that his previous fix was incorrect. RE and SL
should be bounded at 127, not 99.
 1.10 14-Aug-1995  cgd as suggested in PR 1347, by Kenneth Stailey, make "RE" and "SL" fields
show up as two digits, max, displaying "99" if > 99. allow any field with
an unsigned value to be specified as printing that way (though right now
it's only used for RE and SL, which are both unsigned longs). This fix
is substantially different than that suggested in the PR.
 1.9 08-May-1995  cgd add a 'holdcnt' keyword and document it.
 1.8 21-Mar-1995  cgd convert to new RCS id conventions.
 1.7 09-May-1994  cgd clean up import
 1.6 05-May-1994  cgd struct element foo
 1.5 05-May-1994  cgd update for changed flags
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 09-May-1994  cgd new ps, to match libkvm
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.12.6.1 30-May-1996  cgd pull up from trunk:
in findvar(), return after warning about unknown keyword so we don't
deref. a null pointer.
 1.19.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.23.2.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.25.2.1 18-Oct-2002  itojun bin/ps 1.28 via patch

Correct printf format string.
 1.26.2.4 12-Nov-2002  nathanw Catch up to -current.
 1.26.2.3 24-Apr-2002  nathanw Parallel change to mainline: handle 64-bit values properly.
 1.26.2.2 24-Apr-2002  nathanw Note the presense of multiple LWPs or scheduler activations under "state".
Add a "lstate" that prints LWP-specific state and display it instead
of "state" under ps -s.
 1.26.2.1 23-Apr-2002  nathanw Add support for handling multi-LWP processes by selecting one LWP as
"representative" of the process.
Also add a -s option that prints a line for each LWP.
While I'm here, remove the /proc fallback code.
 1.27.2.1 21-Oct-2002  lukem Pull up revision 1.28 (requested by itojun in ticket #922):
correct use of format string (signedness, %ldd). PR 18679
 1.40.2.1 28-Jul-2004  tron Pull up revision 1.41 (requested by simonb in ticket #723):
For the "lid" keyword, change ULONG to INT32 since it's a 32bit type.
From Nicolas Joly in PR bin/26452.
 1.43.2.1 06-Nov-2005  tron Pull up following revision(s) (requested by chs in ticket #936):
bin/ps/keyword.c: revision 1.45
add a keyword for the uarea address. very useful with crashdumps.
 1.50.8.2 23-Mar-2008  matt sync with HEAD
 1.50.8.1 06-Nov-2007  matt sync with HEAD
 1.53.12.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.53.6.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.55.2.1 16-Apr-2018  pgoyette Sync with HEAD, resolve some conflicts
 1.56.4.1 07-Aug-2019  martin Pull up following revision(s) (requested by kamil in ticket #24):

bin/ps/ps.1: revision 1.110
bin/ps/keyword.c: revision 1.57

Restore maxrss, idrss, isrss, ixrss printing in ps(1)

The RSS related statistics are now back in the NetBSD kernel.
These values were disabled since day0 until today.
libkvm(3) users will still receive inappropriate values as RSS statistics=
are updated upon sysctl(3) call.

Patch submitted by <Krzysztof Lasocki>
 1.56.2.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.56.2.2 21-Apr-2020  martin Sync with HEAD
 1.56.2.1 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.28 26-Aug-2020  simonb Don't rely on the USPACE kernel define as a fallback if the vm.uspace
sysctl fails. We've got bigger problems if the sysctl fail anyway.
 1.27 28-Nov-2016  rin branches: 1.27.14;
for donlist{,_sysctl}:
- obtain log_ccpu = log(ccpu) rather than ccpu itself
- use common default values and warn users appropriately when errors occur
ok martin
 1.26 28-Apr-2008  martin branches: 1.26.12; 1.26.46;
Remove clause 3 and 4 from TNF licenses
 1.25 16-Oct-2006  christos branches: 1.25.16;
c99 initializers
 1.24 22-Aug-2004  dsl Use HW_PHYSMEM64 to avoid problem with > 2GB memory (and >4GB as well)
Fixes PR/23717 and PR/24092
 1.23 27-Mar-2004  simonb #define<TAB>
 1.22 27-Mar-2004  simonb KNF, ANSFify, de-__P, unC++ify, white space nits.
 1.21 25-Feb-2004  jrf donlist_sysctl() was overwriting maxslp with vm.uspace. Changed
it to use uspace. See PR bin/23713, approved by christos@netbsd.org.
Thanks to joff@stchome.com for the submission.
 1.20 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.19 18-Jan-2003  thorpej Merge the nathanw_sa branch.
 1.18 14-Jul-2001  matt branches: 1.18.2;
Retrieve maxslp & uspace from kernel instead of using defines. Needed for
shared arm code.
 1.17 08-Jun-2000  simonb Fix handling of "ps U<user>" where <user> ended it 't' - now if the
first argument doesn't start with a '-' and ends in 't', the 't' only
gets converted to a 'T' (for listing processes on the current tty)
if it doesn't contain any other command line option characters that
take an argument. Problem noticed by ITOH Yasufumi on tech-userlevel.

If the 'start' format was the last column, the width wouldn't be set
and started() output a zero length string instead of a string the right
length - fix header width calculations so the the last column is handled
the same all others. This only showed up for 'start' because started()
knew that the column header is the same width and the field itself...

Update copyrights.
 1.16 26-May-2000  simonb branches: 1.16.2;
Use new sysctl/kvm interfaces. This will stop the "proc size mismatch"
errors when internal kernel structures change size. Also remove
the sgid bit - all live kernel data is accessed through the sysctl
interface.
 1.15 03-Dec-1999  simonb Since we have an "extern.h", put all external variable declarations in
that file and not sprinkled around various .c files.
 1.14 27-Mar-1998  is branches: 1.14.6;
Use the physmem variable to get at the physical memory size (instead of the
avail_end - avail_start difference which is only correct on machines with
contiguous memory).
 1.13 06-Feb-1998  mrg remove old OLD BSD VM code.
 1.12 20-Jul-1997  christos Fix compiler warnings
Add WARNS=1
 1.11 21-Mar-1995  cgd convert to new RCS id conventions.
 1.10 04-Dec-1994  cgd from James Jegers <jimj@miller.cs.uwm.edu>: quiet -Wall, and squelch
some of the worst style errors.
 1.9 25-Nov-1994  mycroft Format police.
 1.8 25-Nov-1994  deraadt print %MEM correctly
 1.7 09-May-1994  cgd clean up import
 1.6 05-May-1994  cgd update for changed flags
 1.5 01-Aug-1993  mycroft Add RCS identifiers.
 1.4 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.3 22-Mar-1993  cgd added rcs ids to all files
 1.2 21-Mar-1993  cgd after 0.2.2 "stable" patches applied
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 09-May-1994  cgd new ps, to match libkvm
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.14.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.16.2.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.18.2.2 23-Apr-2002  nathanw Add support for handling multi-LWP processes by selecting one LWP as
"representative" of the process.
Also add a -s option that prints a line for each LWP.
While I'm here, remove the /proc fallback code.
 1.18.2.1 14-Jul-2001  nathanw file nlist.c was added on branch nathanw_sa on 2002-04-23 20:41:12 +0000
 1.25.16.1 18-May-2008  yamt sync with head.
 1.26.46.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.26.12.1 27-Dec-2011  matt Use UPAGES in preferences to USPACE.
 1.27.14.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.27.14.1 21-Apr-2020  martin Sync with HEAD
 1.138 26-Jan-2022  andvar remove double t from targeted, add missing r to arbitrary
And fix few more typos along the way in comments and man pages.
 1.137 14-Sep-2021  christos use emalloc and friends, add ktrace flag printing.
 1.136 14-Sep-2021  christos Provide symbolic printing of some keywords by capitalizing them.
 1.135 17-Apr-2021  maya Remove SCCS workaround. No binary change.
 1.134 06-Apr-2021  christos - dedup code
- add a safe_strftime() to handle error cases
 1.133 06-Apr-2021  simonb Fix the column width calculation for the lstart column if an empty
column header is specified.

Fixes bug pointed out by Ted Spradley in
https://mail-index.netbsd.org/netbsd-users/2021/04/05/msg026808.html .
 1.132 19-Jun-2019  kamil branches: 1.132.2;
Add a fallback definition of LSDEAD in ps(1)

The symbol is no longer available in headers.

Requested by <mrg>
 1.131 18-Jun-2019  kamil Make LSDEAD usage conditional

LSDEAD is not used since NetBSD-5.0 and will be gone.

The same conditional usage is already in ps.c in the same program.
 1.130 19-Sep-2018  maxv Don't display l_wchan, either there is something in l_wmesg and we display
it, or there's nothing and we print "-".
 1.129 11-Apr-2018  christos branches: 1.129.2;
use EXIT_FAILURE instead of 1
 1.128 09-Dec-2017  kamil branches: 1.128.2;
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>
 1.127 12-Dec-2016  christos branches: 1.127.6;
Handle functions that use the offset from either kinfo_proc2/kinfo_lwp properly.
 1.126 02-Dec-2016  christos provide a tree like display with -d, from FreeBSD
 1.125 28-Nov-2016  rin Calculate CPU usage (pcpu) once per process if it is required. This change
significantly improves performance for slow machines when output is sorted
by pcpu.

ok martin
 1.124 28-Nov-2016  rin for donlist{,_sysctl}:
- obtain log_ccpu = log(ccpu) rather than ccpu itself
- use common default values and warn users appropriately when errors occur
ok martin
 1.123 15-Nov-2014  joerg branches: 1.123.2;
Use l_wmesg if the string is not empty. Don't bother checking l_name for
nullness.
 1.122 20-Apr-2014  dholland branches: 1.122.2;
Use an enum type for PRINTMODE vs. WIDTHMODE. Compiler output diffs have
been checked.
 1.121 15-Jan-2014  mlelstv branches: 1.121.2;
Add an LTIME column that prints lwp cputime.
 1.120 20-Mar-2012  matt branches: 1.120.2;
Use C89 function definitions
 1.119 13-Feb-2012  wiz Remove unused variable.
From cppcheck via Henning Petersen in PR 46002.
 1.118 13-Jun-2011  dholland branches: 1.118.2;
When converting from pages to kilobytes, cast the return value of
getpagesize() to size_t. For some reason getpagesize() is defined to
return int, and several of the page counts we get come back from the
kernel as int32_t; in LP64 without the cast the byte count will be
computed in a 32-bit value and for large processes will overflow and
become negative... and then remain negative when divided by 1024 to
convert to kilobytes.

Fixes a problem I hit the other day where I saw negative RSS, which
turns out also to be PR 40642.

Note: other logic in here will break down when we first get >2TB
processes... and int32 page counts will break on >8TB processes. But
hopefully we won't see any of that for a few years yet.
 1.117 22-Jan-2011  christos branches: 1.117.2;
fix previous.
 1.116 22-Jan-2011  christos Make printing of lwp flags similar to the process one, identifying (O)nproc
(K)ernel threads and scheduler (a)ctivations.
 1.115 28-Jul-2010  njoly branches: 1.115.2;
Do not check pcpu value against 100.0 but rather 99.95, to properly
deal with value rounding. From apb@.
 1.114 27-Jul-2010  njoly Make %cpu drop the decimal part when reaching 100%, to stay in the 5
expected columns.
 1.113 31-May-2010  rmind Fix ps(1) and top(1) to show reasonable CPU numbers i.e. cpu_index() provided
by the kernel, instead of CPU order number, which is generally random.
 1.112 21-Oct-2009  rmind Remove uarea swap-out functionality:

- Addresses the issue described in PR/38828.
- Some simplification in threading and sleepq subsystems.
- Eliminates pmap_collect() and, as a side note, allows pmap optimisations.
- Eliminates XS_CTL_DATA_ONSTACK in scsipi code.
- Avoids few scans on LWP list and thus potentially long holds of proc_lock.
- Cuts ~1.5k lines of code. Reduces amd64 kernel size by ~4k.
- Removes __SWAP_BROKEN cases.

Tested on x86, mips, acorn32 (thanks <mpumford>) and partly tested on
acorn26 (thanks to <bjh21>).

Discussed on <tech-kern>, reviewed by <ad>.
 1.111 29-Mar-2009  mrg - add new RLIMIT_AS (aka RLIMIT_VMEM) resource that limits the total
address space available to processes. this limit exists in most other
modern unix variants, and like most of them, our defaults are unlimited.
remove the old mmap / rlimit.datasize hack.

- adds the VMCMD_STACK flag to all the stack-creation vmcmd callers.
it is currently unused, but was added a few years ago.

- add a pair of new process size values to kinfo_proc2{}. one is the
total size of the process memory map, and the other is the total size
adjusted for unused stack space (since most processes have a lot of
this...)

- patch sh, and csh to notice RLIMIT_AS. (in some cases, the alias
RLIMIT_VMEM was already present and used if availble.)

- patch ps, top and systat to notice the new k_vm_vsize member of
kinfo_proc2{}.

- update irix, svr4, svr4_32, linux and osf1 emulations to support
this information. (freebsd could be done, but that it's best left
as part of the full-update of compat/freebsd.)


this addresses PR 7897. it also gives correct memory usage values,
which have never been entirely correct (since mmap), and have been
very incorrect since jemalloc() was enabled.

tested on i386 and sparc64, build tested on several other platforms.

thanks to many folks for feedback and testing but most espcially
chuq and yamt for critical suggestions that lead to this patch not
having a special ugliness i wasn't happy with anyway :-)
 1.110 14-Feb-2009  lukem fix -Wsign-compare issues
 1.109 03-Feb-2009  drochner branches: 1.109.2;
make sure the table stays aligned even if no groups are to be displayed
with the "groupnames" option
 1.108 03-Feb-2009  drochner make sure the table stays aligned even if no groups are to be displayed
with the "groups" option
 1.107 28-Dec-2008  christos fix dev_t 64 bit issues.
 1.106 19-Oct-2008  christos branches: 1.106.2;
Use 'O' for LSONPROC like solaris instead of bundling LSIDL, LSRUN, LSONPROC
to 'R'
 1.105 28-Apr-2008  martin Remove clause 3 and 4 from TNF licenses
 1.104 10-Feb-2008  christos branches: 1.104.4;
Add a CPUID keyword; idea and some of the code from Anon Ymous
 1.103 31-Dec-2007  ad Remove systrace. Ok core@.
 1.102 06-Nov-2007  ad Kill another use of PZERO.
 1.101 01-Sep-2007  yamt add a keyword "lname", to print l_name.
 1.100 28-Jul-2007  dsl branches: 1.100.4; 1.100.6;
Don't call P_ZOMBIE(struct kinfo_proc2), it's p_stat field contains
lwp states, not process states!
Since the times are filled with zeros for zombie processes the check
before assuming teh process time is zero is moot - delete it.
Fix the other chact to test the p_realstat.
Fixer PR kern/36295
 1.99 17-Feb-2007  pavel Revert the changes done after newlock2 merge, except for the removal of
the now unused LSDEAD state. The current kernel headers are now
source-compatible with pre-newlock2 kernel headers again.

Approved by Andrew Doran.
 1.98 10-Feb-2007  ad Further changes to use compat process flags mapped by sysctl. To be
revisited soon.
 1.97 09-Feb-2007  ad Sync with kernel changes introduced by merging the newlock2 branch.
 1.96 29-Oct-2006  christos branches: 1.96.2; 1.96.4;
Add support for printing the emulation of the process.
 1.95 02-Oct-2006  apb * When all columns are given null customised headers, the blank header
line is not printed at all. This is specified in P1003.1-2004
(SUSv3), and is useful.

* Customised headers may contain embedded space, commas and equals
signs. To specify multiple customised headers, use multiple -o or -O
options. This is specified (for "-o", not for "-O") in P1003.1-2004
(SUSv3), and is useful.

* When a column is given a null (blank) customised header, it keeps its
default minimum width. This is specified in P1003.1-2004 (SUSv3), and
seems harmless.

* Fix a bug that made it impossible to print the same keyword multiple
times, with different customised headers each time. (Previously, the
last customised header was used for all instances of the keyword.)

* Make the behaviour of "-O" more useful. The first -O option adds
the default keywords only if there have not yet been any formatting
options, and multiple -O options now insert their keywords in adjacent
positions. Now {ps -j -O %cpu} is like {ps -j} with one extra column;
Previously, it would have had all the columns implied by "-j", plus
all the default columns, plus the extra column specified by "-O".

* Convert from home-grown linked lists to SIMPLEQ lists.

Discussed in tech-userlevel.
 1.94 27-Jun-2005  christos Add some more const so that this compiles again.
 1.93 26-Jun-2005  christos sprinkle a little const, and now everything compiles with WARNS=3
 1.92 07-Jun-2005  he Appease -Wuninitialized by initializing val and uval to 0.
Marked with XXXGCC for hpcarm (found while building for it).

Reviewed (in a slightly different form) by lukem.
 1.91 16-Nov-2004  christos print TTY instead of TT, one question mark instead of two and always the
full tty name.
 1.90 23-Apr-2004  simonb s/the the/the/ (only in sources that aren't regularly imported from
elsewhere).
 1.89 27-Mar-2004  simonb Gah, fix uninitialised variable with the "etime" change. _Never_ be
happy with something and then edit a single line before committing
without retesting...
 1.88 27-Mar-2004  simonb Add support for the POSIX.2 "args" and "comm" keywords. "args" is just
an alias for "command", and "comm" shows just argv[0] and not the whole
argument list.

Fix for part of PR standards/11224.
 1.87 27-Mar-2004  simonb Add support for the POSIX.2 "etime" keyword - shows the elapsed time
since the process was started. Fix a couple of style nits as well.

Fix for part of PR standards/11224.
 1.86 27-Mar-2004  simonb #define<TAB>
 1.85 27-Mar-2004  simonb Use some constants from <tzfile.h>.
 1.84 27-Mar-2004  simonb Wrap some long lines.
Consisently use (void) in front of printf(...);
A few more white-space nits.
 1.83 27-Mar-2004  simonb Fix grammar in a comment.
 1.82 27-Mar-2004  simonb KNF, ANSFify, de-__P, unC++ify, white space nits.
 1.81 24-Feb-2004  wiz Spell interrupt with two rs. From Peter Postma.
 1.80 11-Jan-2004  jdolecek g/c SDEAD
 1.79 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.78 06-Mar-2003  dsl - add keywords for utime, stime and ctime (output sss.ssssss if small)
- fiddle with keyword table - mainly to keep width under 80 cols
- add -k keylist so sort output
- don't use logs to work out field width, get -MAXINT right
- user defined headers can be 1 char (actually they can be zero length)
so ?? in tty column isn't guaranteed to be wider than header
- remove some long long casts (compiles on sparc64 still)
- use kvm_openfiles(NULL, NULL, NULL, KVN_NO_FILES, ..) unless core file
specified,
(approved by christos)
 1.77 01-Mar-2003  atatat Add p_svuid and p_svgid to kinfo_proc2. Populate them in the kernel
and in libkvm. Then teach ps how to show them to you.

Also, teach ps how to show the names for all the uids, the rest of the
group numbers, and the "group access list".
 1.76 18-Jan-2003  thorpej Merge the nathanw_sa branch.
 1.75 06-Jan-2003  wiz interrupt with two rs.
 1.74 19-Jun-2002  jdolecek g/c procfs fallback code, it's redundant now that we use sysctl to get
process info
 1.73 17-Jun-2002  christos Document some missing flags and add systrace(1) 'x' flag.
 1.72 24-Apr-2002  nathanw Since kinfo_proc2 has many values that are 64-bit, ps needs to know
that they're 64-bit, and grab them out of memory appropriately. Otherwise,
big-endian systems get the wrong end of the 64-bit value and lose.

Keywords affected: inblk, majflt, minflt, msgrcv, msgsnd, nivcsw, nsigs,
nswap, nvcsw, and oublk.
 1.71 21-Feb-2002  martin branches: 1.71.2;
Use int32_t for temporary variables (instead of long), that is the same
type as we get the data passed from the kernel. This avoids (missing)
sign extension bugs on LP64 systems and partly takes care of PR 15677.

We now print this values as negative seconds - still wrong, but that
probably is due to the simple way this values are acumulated in the
scheduler, causing negative times when ntpd steps time backwards.
 1.70 21-Jan-2002  jdolecek started(): don't assume that the field width is correct and recompute
it as needed

This fixes bogus output for cases like `ps -ao start=xx`, as reported by
Martin Macok of Undeground.cz on current-users@netbsd.org.
 1.69 24-Aug-2001  lukem fix comment: "[]" are [square] brackets, "<>" are angle brackets
 1.68 24-Aug-2001  enami Print commands within angled brackets only if they are system processes.
Print other commands, like cc, cpp and as, within parentheres.
 1.67 07-Aug-2001  christos Print system processes/kernel threads as [kprocname] as top(1) does.
 1.66 14-Jul-2001  matt Retrieve maxslp & uspace from kernel instead of using defines. Needed for
shared arm code.
 1.65 15-Jan-2001  christos clarify things a bit more (from hubertf)
 1.64 15-Jan-2001  christos Really explain what parenthesized names mean and why we parenthesize names!
 1.63 15-Jan-2001  hubertf Document the meaning of commands shown in parantheses.

Patches submitted by Jeremy C. Reed <reed@reedmedia.net> in PR 11930.
 1.62 09-Jan-2001  itojun KPTR/KPTR24 are using u_int64_t. commented by minoura
 1.61 08-Jan-2001  itojun redo previous (simplify KPTR)
 1.60 08-Jan-2001  itojun backout previous; did not compile on non-alpha
 1.59 08-Jan-2001  itojun simplify KPTR processing.
 1.58 08-Jan-2001  itojun address PR 11653. checked on alpha.
XXX supports 32bit and 64bit pointers only
 1.57 23-Oct-2000  simonb Add a K flag to the state information - K is for kernel thread or system
process (ie, P_SYSTEM set in p_flag). The L flag (locked in core) is
only used for non-P_SYSTEM processes.
 1.56 29-Jun-2000  mrg remove include of <vm/vm.h>
 1.55 08-Jun-2000  simonb branches: 1.55.2;
Fix handling of "ps U<user>" where <user> ended it 't' - now if the
first argument doesn't start with a '-' and ends in 't', the 't' only
gets converted to a 'T' (for listing processes on the current tty)
if it doesn't contain any other command line option characters that
take an argument. Problem noticed by ITOH Yasufumi on tech-userlevel.

If the 'start' format was the last column, the width wouldn't be set
and started() output a zero length string instead of a string the right
length - fix header width calculations so the the last column is handled
the same all others. This only showed up for 'start' because started()
knew that the column header is the same width and the field itself...

Update copyrights.
 1.54 08-Jun-2000  simonb Fix back-to-front assignment in tname() which meant that the column
width would never autosize past the header ("TT") width. Problem
noticed by Geoff Wing.
 1.53 07-Jun-2000  simonb Calculate field widths on the fly so that all columns line up nicely
and make more effective use of screen real estate when some columns
(eg USER and VSZ) didn't need the full default width.
 1.52 02-Jun-2000  simonb G/C evar() - now that everything is in the one structure it was exactly
the same as pvar().
 1.51 26-May-2000  simonb branches: 1.51.2;
Use new sysctl/kvm interfaces. This will stop the "proc size mismatch"
errors when internal kernel structures change size. Also remove
the sgid bit - all live kernel data is accessed through the sysctl
interface.
 1.50 26-May-2000  thorpej Handle SONPROC state.
 1.49 05-Dec-1999  fredb Fix CL* purge fallout.
 1.48 03-Dec-1999  simonb Since we have an "extern.h", put all external variable declarations in
that file and not sprinkled around various .c files.
 1.47 03-Dec-1999  simonb "ps -e" only shows the environment for the processes owned by the
current user id or all process if run by root.

Fixes PR security/5967 from Todd Vierling.
 1.46 15-Oct-1999  jdolecek command(): don't start using procfs_getargv() instead of kvm_getargv()
just because the latter returned a NULL - that may normally happen,
when the process is, for example, a kernel thread

procfs_getargv(): if the name is brackened in '(' and ')', just
return NULL immediately - the code in command() will DTRT
and the output will be same for procfs- and kvm- based lookup
in all cases
 1.45 15-Oct-1999  jdolecek When using the procfs for extracting process information, extract
also process start time, process arguments and session leadership status.

The procfs fallback is also used when kvm_openfiles() completely fails
(e.g. when /dev/mem is not readable).

Solves PR 7772, though the final implementation is different.
 1.44 11-Oct-1999  mrg skip "dty" just like we skip "tty", otherwise we just get "dt" in ps output for all dty's
 1.43 23-Jul-1999  veego branches: 1.43.2;
According to the previous commit the new process state should be SDEAD and
not SDYING, so s/SDYING/SDEAD/.
 1.42 22-Jul-1999  thorpej Update for SDEAD.
 1.41 03-May-1999  mrg limit session to 6 chars. from Geoff Wing <gcw@pobox.com> and fixes his PR#7506
 1.40 16-Apr-1999  christos Add support for printing wide sigset_t properly.
 1.39 28-Jul-1998  mycroft branches: 1.39.2;
Look for the formats used by setproctitle(3) and login shells, and avoiding
outputting the redundant `(comm)'.
 1.38 27-Jul-1998  mycroft __AUDIT__ cleanup.
 1.37 01-Apr-1998  kleink Need <time.h> for localtime(), time() and strftime() prototypes.
 1.36 06-Feb-1998  mrg remove old OLD BSD VM code.
 1.35 05-Feb-1998  gwr Begone KERNBASE! (Just print kernel pointers as-is.)
 1.34 03-Aug-1997  mikel use %c not %C for lstart; from tetsuya@secom-sis.co.jp via OpenBSD
 1.33 20-Jul-1997  christos Fix compiler warnings
Add WARNS=1
 1.32 19-Mar-1997  mycroft DTRT if e_wmesg is not NUL-terminated.
 1.31 19-Mar-1997  mycroft DTRT if e_login is not NUL-terminated.
 1.30 02-Oct-1996  ws Fix p_nice vs. NZERO code.
Remove some unnecessary casts.
Remove an unused time format field.
 1.29 15-Aug-1996  explorer return 0.0 cpu usage for zombies. closes pr bin/2693. Perhaps this should
be fixed in exit, so p->p_pctcpu is cleared when the process exits.
 1.28 13-Jul-1996  explorer include <pwd.h> for one function (user_from_uid) and correct one
%*d -> %*ld in a format string. (yes, trying to find the problem with
ps wwwwwwwww1 and core dump, and might as well clean up while here)
 1.27 29-Sep-1995  cgd add code to convert from INT32 and UINT32 to native types.
 1.26 15-Aug-1995  cgd Kenneth Stailey pointed out that his previous fix was incorrect. RE and SL
should be bounded at 127, not 99.
 1.25 14-Aug-1995  cgd as suggested in PR 1347, by Kenneth Stailey, make "RE" and "SL" fields
show up as two digits, max, displaying "99" if > 99. allow any field with
an unsigned value to be specified as printing that way (though right now
it's only used for RE and SL, which are both unsigned longs). This fix
is substantially different than that suggested in the PR.
 1.24 07-Jun-1995  cgd typeof(timeval.ts_sec) != time_t
 1.23 25-May-1995  mycroft If there is another field after the command, fill in the command field with
blanks.
 1.22 21-May-1995  mycroft Split out some helper functions again.
 1.21 18-May-1995  mycroft Create and strvis(3) the argument and environment lists only as needed.
Do not keep extra copies around.
 1.20 18-May-1995  mycroft Avoid an extra (gratuitous) pass through strvis(3).
 1.19 08-May-1995  cgd add the ability to understand and print INT and UINT-sized variables.
 1.18 21-Mar-1995  cgd convert to new RCS id conventions.
 1.17 23-Dec-1994  cgd be careful with addresses.
 1.16 04-Dec-1994  cgd from James Jegers <jimj@miller.cs.uwm.edu>: quiet -Wall, and squelch
some of the worst style errors.
 1.15 03-Nov-1994  deraadt kill UPAGES
 1.14 15-Sep-1994  deraadt NBPG -> getpagesize()
 1.13 16-Jun-1994  pk No more P_NOSWAP/P_PHYSIO.
 1.12 09-May-1994  cgd clean up import
 1.11 05-May-1994  cgd struct element foo
 1.10 05-May-1994  cgd update for changed flags
 1.9 14-Aug-1993  mycroft Fix test for zombie process.
 1.8 01-Aug-1993  mycroft Add RCS identifiers.
 1.7 18-Jun-1993  cgd use strvis on the arguments and environment, so we don't end up in
sed and/or xterm-title-changing hell.
 1.6 02-Jun-1993  cgd make ps do something more sensible with zombie processes
 1.5 01-Jun-1993  cgd fix more-or-less-uninitialized variable...
 1.4 01-Jun-1993  cgd add support and documentation for the 'e' option, to print out
the process environment.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 09-May-1994  cgd new ps, to match libkvm
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.39.2.2 08-Nov-1999  cgd pull up revs 1.45-1.46 from trunk (requested by jdolecek):
When using the procfs for extracting process information, extract process
start time, arguments, and session leadership status. Also, fall back to
procfs when kvm_openfiles() completely fails (e.g. when /dev/mem is not
readable). Fixes PR#7772.
 1.39.2.1 12-Oct-1999  he Pull up revision 1.44 (requested by mrg):
Make various utilities skip "dty" as well as "tty" on terminals,
so that the actually used tty is visiable instead of just 'dt'.
 1.43.2.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.51.2.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.55.2.1 27-Jan-2001  jhawk Pull up revisions 1.63-1.65 (requested by hubertf):
Clarify meaning of COMMANDs printed in parentheses. Addresses PR11930.
 1.71.2.7 17-Jan-2003  thorpej Sync with HEAD.
 1.71.2.6 12-Nov-2002  nathanw Catch up to -current.
 1.71.2.5 26-Jul-2002  nathanw Print state letter 'U' for suspended LWPs.
Make sure to print '+' (controlling terminal) character last.
 1.71.2.4 24-Apr-2002  nathanw Parallel change to mainline: handle 64-bit values properly.
 1.71.2.3 24-Apr-2002  nathanw Note the presense of multiple LWPs or scheduler activations under "state".
Add a "lstate" that prints LWP-specific state and display it instead
of "state" under ps -s.
 1.71.2.2 23-Apr-2002  nathanw Add support for handling multi-LWP processes by selecting one LWP as
"representative" of the process.
Also add a -s option that prints a line for each LWP.
While I'm here, remove the /proc fallback code.
 1.71.2.1 21-Feb-2002  nathanw file print.c was added on branch nathanw_sa on 2002-04-23 20:41:12 +0000
 1.96.4.1 03-Sep-2007  wrstuden Sync w/ NetBSD-4-RC_1
 1.96.2.1 30-Jul-2007  liamjfoy Pull up following revision(s) (requested by dsl in ticket #798):
bin/ps/print.c: revision 1.100
Don't call P_ZOMBIE(struct kinfo_proc2), it's p_stat field contains
lwp states, not process states!
Since the times are filled with zeros for zombie processes the check
before assuming teh process time is zero is moot - delete it.
Fix the other chact to test the p_realstat.
Fixer PR kern/36295
 1.100.6.2 28-Jul-2007  dsl Don't call P_ZOMBIE(struct kinfo_proc2), it's p_stat field contains
lwp states, not process states!
Since the times are filled with zeros for zombie processes the check
before assuming teh process time is zero is moot - delete it.
Fix the other chact to test the p_realstat.
Fixer PR kern/36295
 1.100.6.1 28-Jul-2007  dsl file print.c was added on branch matt-mips64 on 2007-07-28 17:05:51 +0000
 1.100.4.3 23-Mar-2008  matt sync with HEAD
 1.100.4.2 09-Jan-2008  matt sync with HEAD
 1.100.4.1 06-Nov-2007  matt sync with HEAD
 1.104.4.1 18-May-2008  yamt sync with head.
 1.106.2.1 01-Apr-2009  snj Pull up following revision(s) (requested by mrg in ticket #622):
bin/csh/csh.1: revision 1.46
bin/csh/func.c: revision 1.37
bin/ps/print.c: revision 1.111
bin/ps/ps.c: revision 1.74
bin/sh/miscbltin.c: revision 1.38
bin/sh/sh.1: revision 1.92 via patch
external/bsd/top/dist/machine/m_netbsd.c: revision 1.7
lib/libkvm/kvm_proc.c: revision 1.82
sys/arch/mips/mips/cpu_exec.c: revision 1.55
sys/compat/darwin/darwin_exec.c: revision 1.57
sys/compat/ibcs2/ibcs2_exec.c: revision 1.73
sys/compat/irix/irix_resource.c: revision 1.15
sys/compat/linux/arch/amd64/linux_exec_machdep.c: revision 1.16
sys/compat/linux/arch/i386/linux_exec_machdep.c: revision 1.12
sys/compat/linux/common/linux_limit.h: revision 1.5
sys/compat/osf1/osf1_resource.c: revision 1.14
sys/compat/svr4/svr4_resource.c: revision 1.18
sys/compat/svr4_32/svr4_32_resource.c: revision 1.17
sys/kern/exec_subr.c: revision 1.62
sys/kern/init_sysctl.c: revision 1.160
sys/kern/kern_exec.c: revision 1.288
sys/kern/kern_resource.c: revision 1.151
sys/sys/param.h: patch
sys/sys/resource.h: revision 1.31
sys/sys/sysctl.h: revision 1.184
sys/uvm/uvm_extern.h: revision 1.153
sys/uvm/uvm_glue.c: revision 1.136
sys/uvm/uvm_mmap.c: revision 1.128
usr.bin/systat/ps.c: revision 1.32
- - add new RLIMIT_AS (aka RLIMIT_VMEM) resource that limits the total
address space available to processes. this limit exists in most other
modern unix variants, and like most of them, our defaults are unlimited.
remove the old mmap / rlimit.datasize hack.
- - adds the VMCMD_STACK flag to all the stack-creation vmcmd callers.
it is currently unused, but was added a few years ago.
- - add a pair of new process size values to kinfo_proc2{}. one is the
total size of the process memory map, and the other is the total size
adjusted for unused stack space (since most processes have a lot of
this...)
- - patch sh, and csh to notice RLIMIT_AS. (in some cases, the alias
RLIMIT_VMEM was already present and used if availble.)
- - patch ps, top and systat to notice the new k_vm_vsize member of
kinfo_proc2{}.
- - update irix, svr4, svr4_32, linux and osf1 emulations to support
this information. (freebsd could be done, but that it's best left
as part of the full-update of compat/freebsd.)
this addresses PR 7897. it also gives correct memory usage values,
which have never been entirely correct (since mmap), and have been
very incorrect since jemalloc() was enabled.
tested on i386 and sparc64, build tested on several other platforms.
thanks to many folks for feedback and testing but most espcially
chuq and yamt for critical suggestions that lead to this patch not
having a special ugliness i wasn't happy with anyway :-)
 1.109.2.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.115.2.1 08-Feb-2011  bouyer Sync with HEAD
 1.117.2.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.118.2.2 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.118.2.1 17-Apr-2012  yamt sync with head
 1.120.2.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.121.2.1 10-Aug-2014  tls Rebase.
 1.122.2.1 14-May-2015  snj Pull up following revision(s) (requested by joerg in ticket #758):
bin/ksh/Makefile: revision 1.31
bin/ps/print.c: revision 1.123
external/bsd/iscsi/dist/src/lib/parameters.c: revision 1.4
external/bsd/iscsi/dist/src/lib/target.c: revision 1.9
external/gpl2/lvm2/dist/lib/commands/toolcontext.c: revision 1.8
external/gpl2/lvm2/dist/lib/format1/import-export.c: revision 1.2
gnu/dist/texinfo/makeinfo/xml.c: revision 1.2
sbin/bioctl/bioctl.c: revision 1.16
usr.sbin/isdn/isdnd/msghdl.c: revision 1.12
Drop pointer checks that are always true.
--
Comment out impossible code.
--
Remove tautologic check.
--
Skip vendor if the string is empty.
Use l_wmesg if the string is not empty. Don't bother checking l_name for
nullness.
--
Array can't be null.
--
Show display if it is not empty.
--
Make cast warnings for clang non-fatal.
 1.123.2.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.127.6.1 06-Apr-2021  martin Pull up following revision(s) (requested by simonb in ticket #1669):

bin/ps/print.c: revision 1.133

Fix the column width calculation for the lstart column if an empty
column header is specified.

Fixes bug pointed out by Ted Spradley in
https://mail-index.netbsd.org/netbsd-users/2021/04/05/msg026808.html .
 1.128.2.2 30-Sep-2018  pgoyette Ssync with HEAD
 1.128.2.1 16-Apr-2018  pgoyette Sync with HEAD, resolve some conflicts
 1.129.2.4 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.129.2.3 21-Apr-2020  martin Sync with HEAD
 1.129.2.2 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.129.2.1 10-Jun-2019  christos Sync with HEAD
 1.132.2.1 06-Apr-2021  martin Pull up following revision(s) (requested by simonb in ticket #1244):

bin/ps/print.c: revision 1.133

Fix the column width calculation for the lstart column if an empty
column header is specified.

Fixes bug pointed out by Ted Spradley in
https://mail-index.netbsd.org/netbsd-users/2021/04/05/msg026808.html .
 1.10 19-Jun-2002  jdolecek g/c procfs fallback code, it's redundant now that we use sysctl to get
process info
 1.9 26-May-2000  simonb branches: 1.9.6;
Use new sysctl/kvm interfaces. This will stop the "proc size mismatch"
errors when internal kernel structures change size. Also remove
the sgid bit - all live kernel data is accessed through the sysctl
interface.
 1.8 07-Apr-2000  jdolecek Ignore also "self" link when searching for proc info.
Patch sent by NAKAJIMA Yoshihiro in bin/9819
 1.7 15-Oct-1999  jdolecek command(): don't start using procfs_getargv() instead of kvm_getargv()
just because the latter returned a NULL - that may normally happen,
when the process is, for example, a kernel thread

procfs_getargv(): if the name is brackened in '(' and ')', just
return NULL immediately - the code in command() will DTRT
and the output will be same for procfs- and kvm- based lookup
in all cases
 1.6 15-Oct-1999  jdolecek When using the procfs for extracting process information, extract
also process start time, process arguments and session leadership status.

The procfs fallback is also used when kvm_openfiles() completely fails
(e.g. when /dev/mem is not readable).

Solves PR 7772, though the final implementation is different.
 1.5 09-May-1999  thorpej branches: 1.5.2;
KNF.
 1.4 28-Mar-1999  bgrayson branches: 1.4.2;
- Be really paranoid. Any time we open a file that is in /proc, run
fstatfs() on the open file to verify that no intruder has mounted
something on portions of /proc. This will catch, for instance,
"mount_null /tmp /proc/1378". We already do 5 syscalls per
process, so one more won't hurt :) ... and safety is better than
performance when ps is otherwise broken.
- Also added a few close()'s at early returns, to avoid chewing up fd's.
 1.3 27-Mar-1999  bgrayson Minor fixes:
1. Use statfs() to verify that /proc is a procfs.
2. Add -K option to disable /proc-based method.
3. Make warnings less verbose, but still accurate.
 1.2 27-Mar-1999  tron Add missing prototype so that this actually compiles.
 1.1 26-Mar-1999  bgrayson Added experimental fallback /proc-based lookup. Warnings are printed when the
fallback method is used, as the results could be untrustworthy if an
intruder is present. It is highly likely that NetBSD-1.5 will have
an improved kvm interface for reading process information, at which
point this code can be garbage-collected. Also added a word to the
man page -x option description while I was here.
 1.4.2.1 08-Nov-1999  cgd pull up revs 1.5-1.7 from trunk (requested by jdolecek):
When using the procfs for extracting process information, extract process
start time, arguments, and session leadership status. Also, fall back to
procfs when kvm_openfiles() completely fails (e.g. when /dev/mem is not
readable). Fixes PR#7772.
 1.5.2.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.9.6.1 23-Apr-2002  nathanw Add support for handling multi-LWP processes by selecting one LWP as
"representative" of the process.
Also add a -s option that prints a line for each LWP.
While I'm here, remove the /proc fallback code.
 1.115 06-Nov-2021  msaitoh s/maxiumum/maximum/
 1.114 14-Sep-2021  christos reflect reality on flags.
 1.113 14-Sep-2021  christos Provide symbolic printing of some keywords by capitalizing them.
 1.112 04-Jun-2021  wiz Sort options in synopsis.
 1.111 04-Jun-2021  cjep PR standards/11223

Add -G to take a single group argument heading towards POSIX.2 compliance.
Patch from jperkin and reviewed by simonb.
 1.110 06-Aug-2019  kamil branches: 1.110.6;
Restore maxrss, idrss, isrss, ixrss printing in ps(1)

The RSS related statistics are now back in the NetBSD kernel.

These values were disabled since day0 until today.

libkvm(3) users will still receive inappropriate values as RSS statistics
are updated upon sysctl(3) call.

Patch submitted by <Krzysztof Lasocki>
 1.109 28-Aug-2017  wiz branches: 1.109.4; 1.109.6;
Bump date for previous. Remove/replace bogus Tn uses.
 1.108 28-Aug-2017  kamil 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>
 1.107 03-Jul-2017  wiz Remove workaround for ancient HTML generation code.
 1.106 02-Dec-2016  christos branches: 1.106.6;
provide a tree like display with -d, from FreeBSD
 1.105 10-Aug-2016  sevan Earliest reference in the TUHS archive is v3
Unfortunately only the source code for the compiler is present for that
release. The v2 archive is missing the manuals & only contains a some of the
commands.
Switch to v3 and reference manual category eight, as per r1.32 of
src/bin/ps/ps.1 from OpenBSD.
Heads up from Ingo Schwarze.
 1.104 10-Aug-2016  sevan Document the version ps first appeared.
 1.103 15-Jan-2014  wiz branches: 1.103.8;
Bump date for previous.
 1.102 15-Jan-2014  mlelstv Add an LTIME column that prints lwp cputime.
 1.101 03-Jun-2012  joerg branches: 1.101.2;
Switch device database to cdb(5). Rework ttyname(3) and ttyname_r(3) to
depend on new devname_r(3) as heart. Add /dev/pts magic directly to
devname(3). While it can lead to returning non-existing paths, the
behavior is more consistent that way. Drop caching layer in devname(3),
it doesn't buy anything for the common case of having access to the
database. Teach devname(3) proper fallback behavior of scanning /dev.
Create both old-style and new-style database for now in /etc/rc.d/sysdb.
 1.100 15-Apr-2012  wiz Increase width of table column so it formats more nicely.
 1.99 15-Apr-2012  wiz - choose better argument name for `-U';
- add argument names for options in man page;
- improve wording, punctuation, capitalization.

From Bug Hunting in email.
 1.98 15-Mar-2012  wiz Remove trailing whitespace.
 1.97 15-Mar-2012  christos Fix obsolete example (that referred to letters in process state that are
not set anymore), and clarify their meaning.
 1.96 23-Feb-2012  dholland The 'lstart' column uses strftime %c, not %C. Noted by mrg.
 1.95 27-Jan-2012  dholland branches: 1.95.2;
Minor English improvements for -w, partly from Snader_LB.
 1.94 13-Feb-2011  dholland branches: 1.94.4;
fix typo, from Randolf Richardson in PR 44559
 1.93 14-May-2010  joerg branches: 1.93.2;
Don't break lines in .It, there be dragons in groff.
 1.92 22-Mar-2010  joerg Use .In instead of .Aq Pa for header files
 1.91 21-Oct-2009  wiz Bump date for uarea swap-out removal.
 1.90 21-Oct-2009  rmind Remove uarea swap-out functionality:

- Addresses the issue described in PR/38828.
- Some simplification in threading and sleepq subsystems.
- Eliminates pmap_collect() and, as a side note, allows pmap optimisations.
- Eliminates XS_CTL_DATA_ONSTACK in scsipi code.
- Avoids few scans on LWP list and thus potentially long holds of proc_lock.
- Cuts ~1.5k lines of code. Reduces amd64 kernel size by ~4k.
- Removes __SWAP_BROKEN cases.

Tested on x86, mips, acorn32 (thanks <mpumford>) and partly tested on
acorn26 (thanks to <bjh21>).

Discussed on <tech-kern>, reviewed by <ad>.
 1.89 03-Aug-2009  wiz Bump date for previous.
 1.88 03-Aug-2009  dsl Remove reference to P_SYSTRACE.
Fixes PR/41426
(leave defn in sys/sysctl.h since the bit shouldn't be reused!)
 1.87 13-Mar-2009  joerg Fix number of columns in .Bl. Escape %C.
 1.86 19-Oct-2008  wiz branches: 1.86.4;
Improve wording.
 1.85 19-Oct-2008  christos Use 'O' for LSONPROC like solaris instead of bundling LSIDL, LSRUN, LSONPROC
to 'R'
 1.84 26-Sep-2008  wiz Sort options.
Remove systrace reference.
 1.83 26-Sep-2008  apb Add "ps -A" option. This displays information about all processes,
like BSD "ps -ax" or SYSV "ps -ef". It's specified in SUSv3.

Also make some minor clarifications to the man page.
 1.82 10-Feb-2008  christos Add a CPUID keyword; idea and some of the code from Anon Ymous
 1.81 31-Dec-2007  ad Remove systrace. Ok core@.
 1.80 09-Sep-2007  apb Document "emul" keyword. Fixes PR 36935.
Also sort list of keywords, and bump date.
 1.79 01-Sep-2007  yamt add a keyword "lname", to print l_name.
 1.78 25-Jun-2007  yamt branches: 1.78.4;
remove descriptions of some states. ('A', 'S', '>')
- they are wrong. (madvise is not vadvise)
- they don't exist anymore.
 1.77 09-Oct-2006  wiz Remove trailing whitespace.
Remove a partially duplicated sentence.
 1.76 07-Oct-2006  apb Make it clear that multiple sort keys may be specified.
Closes PR 34271.
 1.75 03-Oct-2006  wiz Avoid .No if easily possible.
 1.74 02-Oct-2006  apb Add some markup missed in previous.
 1.73 02-Oct-2006  apb Document changes to -o and -O options, and improve markup.
 1.72 11-Nov-2005  chs add "uaddr" and improve text for "laddr".
 1.71 29-Dec-2004  skrll branches: 1.71.2;
Add the missing suspended state 'U'.

Bump date.
 1.70 23-Aug-2004  wiz Bump date for laddr.
 1.69 23-Aug-2004  nathanw Add a "laddr" keyword, analagous to "paddr". Useful for finding
struct pcb's.
 1.68 23-Apr-2004  simonb s/the the/the/ (only in sources that aren't regularly imported from
elsewhere).
 1.67 27-Mar-2004  simonb Add support for the POSIX.2 "args" and "comm" keywords. "args" is just
an alias for "command", and "comm" shows just argv[0] and not the whole
argument list.

Fix for part of PR standards/11224.
 1.66 27-Mar-2004  simonb Add support for the POSIX.2 "etime" keyword - shows the elapsed time
since the process was started. Fix a couple of style nits as well.

Fix for part of PR standards/11224.
 1.65 13-Feb-2004  wiz Uppercase CPU where appropriate.
 1.64 16-Oct-2003  wiz Add Xr for dev_mkdb(8), per PR 23168. Increase width in FILES section while here.
 1.63 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.62 16-Apr-2003  wiz More mdoc.
 1.61 17-Mar-2003  wiz Update date for recent changes.
 1.60 06-Mar-2003  dsl - add keywords for utime, stime and ctime (output sss.ssssss if small)
- fiddle with keyword table - mainly to keep width under 80 cols
- add -k keylist so sort output
- don't use logs to work out field width, get -MAXINT right
- user defined headers can be 1 char (actually they can be zero length)
so ?? in tty column isn't guaranteed to be wider than header
- remove some long long casts (compiles on sparc64 still)
- use kvm_openfiles(NULL, NULL, NULL, KVN_NO_FILES, ..) unless core file
specified,
(approved by christos)
 1.59 01-Mar-2003  atatat Add p_svuid and p_svgid to kinfo_proc2. Populate them in the kernel
and in libkvm. Then teach ps how to show them to you.

Also, teach ps how to show the names for all the uids, the rest of the
group numbers, and the "group access list".
 1.58 25-Feb-2003  wiz .Nm does not need a dummy argument ("") before punctuation or
for correct formatting of the SYNOPSIS any longer.
 1.57 18-Jan-2003  thorpej Merge the nathanw_sa branch.
 1.56 01-Oct-2002  wiz Use more mdoc.
 1.55 25-Sep-2002  wiz New policy: New sentences start on a new line.
Patches by Robert Elz <kre at munnari oz au>, with minimal changes by me.
 1.54 19-Jun-2002  jdolecek g/c procfs fallback code, it's redundant now that we use sysctl to get
process info
 1.53 17-Jun-2002  wiz Sort SEE ALSO.
 1.52 17-Jun-2002  christos Document some missing flags and add systrace(1) 'x' flag.
 1.51 24-Apr-2002  wiz "a, b, c, and d", not "a, b, c and d".
 1.50 04-Mar-2002  wiz branches: 1.50.2;
Drop trailing space.
 1.49 04-Mar-2002  ad Xref pkill/pgrep.
 1.48 08-Feb-2002  ross Generate <>& symbolically. I'm avoiding .../dist/... directories for now.
 1.47 20-Dec-2001  wiz Properly quote punctuation.
 1.46 20-Dec-2001  wiz Whitespace nits.
 1.45 20-Aug-2001  wiz precede, not preceed.
 1.44 07-Aug-2001  christos Print system processes/kernel threads as [kprocname] as top(1) does.
 1.43 04-Jul-2001  sommerfeld correct the description of paddr
 1.42 15-Jan-2001  christos clarify things a bit more (from hubertf)
 1.41 15-Jan-2001  christos Really explain what parenthesized names mean and why we parenthesize names!
 1.40 15-Jan-2001  hubertf Document the meaning of commands shown in parantheses.

Patches submitted by Jeremy C. Reed <reed@reedmedia.net> in PR 11930.
 1.39 23-Oct-2000  simonb Add a K flag to the state information - K is for kernel thread or system
process (ie, P_SYSTEM set in p_flag). The L flag (locked in core) is
only used for non-P_SYSTEM processes.
 1.38 28-Aug-2000  nathanw Update description of process flag 0x20000 to match reality:
P_NOCLDWAIT, not P_SSTEP.
 1.37 24-Jun-2000  simonb Remove the 'poip' (pageouts in progress) keyword from the manpage.
From PR bin/8843 by Brian Stark.
 1.36 15-Apr-2000  simonb branches: 1.36.4;
Use tty "?" to select processes with no controlling tty and tty "-" to
select processes with a revoked controlling tty.

Idea from SunOS/Solaris, suggested by Alexis Rosen.
 1.35 10-Apr-2000  chs make -M imply -K. the current system is not likely to be
a reasonable approximation of a crash dump.
 1.34 22-Feb-2000  dbj name database is kvm.db not kvm_netbsd.db
 1.33 04-Dec-1999  hubertf Document -U switch (displays processes belonging to given user/uid).
Reported in PR 8943 by WL <wliao@dura.spc.uchicago.edu>
 1.32 03-Dec-1999  simonb "ps -e" only shows the environment for the processes owned by the
current user id or all process if run by root.

Fixes PR security/5967 from Todd Vierling.
 1.31 27-Mar-1999  bgrayson branches: 1.31.6;
Minor fixes:
1. Use statfs() to verify that /proc is a procfs.
2. Add -K option to disable /proc-based method.
3. Make warnings less verbose, but still accurate.
 1.30 26-Mar-1999  bgrayson Added experimental fallback /proc-based lookup. Warnings are printed when the
fallback method is used, as the results could be untrustworthy if an
intruder is present. It is highly likely that NetBSD-1.5 will have
an improved kvm interface for reading process information, at which
point this code can be garbage-collected. Also added a word to the
man page -x option description while I was here.
 1.29 22-Mar-1999  garbled More and more .Os cleanups. .Os is defined in the tmac.doc-common file,
so we shouldn't override it with versions in the manpages. Many more to
come.
 1.28 17-Mar-1999  msaitoh ps's information is sorted by controlling terminal and (among processes with
the same controlling terminal) by PID.
 1.27 07-Mar-1999  mycroft Clean up SYNOPSIS formatting.
 1.26 07-Mar-1999  mycroft Fix formatting errors.
 1.25 02-Jan-1999  tron The system namelist database is called "/var/db/kvm.db" not
"/var/db/kvm_netbsd.db". Fixes PR bin/6712 by Brian Grayson.
 1.24 28-Apr-1998  fair change references to vadvise to madvise; remove reference to lisp(1)
 1.23 24-Mar-1998  bouyer Sync with reality: the uprocp keyword doesn't exist.
 1.22 06-Feb-1998  mrg remove old OLD BSD VM code.
 1.21 11-Oct-1997  enami branches: 1.21.2;
Use .Nm "" instead of .Nm ps in SYNOPSIS.
 1.20 10-Oct-1997  lukem all but the first .Nm in SYNOPSIS need to be spelt out. [bin/4256]
 1.19 14-Sep-1997  lukem getopt returns -1 not EOF
cleanup manpage
 1.18 27-Aug-1997  mikel default swap device is /dev/drum, not nonexistent /dev/swap; from
<entropy@tardis.bernstein.com> in PR bin/4042
 1.17 23-Jun-1997  pk Refer to execve(2), add section to `vfork' ref. (almost per PR#3627).
 1.16 21-Mar-1996  jtc fix damage caused by sccs (long ago)
 1.15 08-May-1995  cgd add a 'holdcnt' keyword and document it.
 1.14 07-May-1995  pk Sync process flags with sys/proc.h
 1.13 21-Mar-1995  cgd convert to new RCS id conventions.
 1.12 16-Sep-1994  mycroft Integrate `-c' option, from Arne Juul.
 1.11 06-Aug-1994  jtc vmunix -> netbsd (PR #375)
 1.10 09-May-1994  cgd branches: 1.10.2;
clean up import
 1.9 11-Jan-1994  jtc Fix spelling errors.
 1.8 07-Oct-1993  cgd fix from Thomas Eberhardt <thomas@mathematik.uni-Bremen.de>:
don't try to display the resident text size; we can't do that anymore
 1.7 01-Aug-1993  mycroft Add RCS indentifiers.
 1.6 13-Jun-1993  mycroft Fix up FILES sections.
 1.5 13-Jun-1993  mycroft Update file names.
 1.4 01-Jun-1993  cgd add support and documentation for the 'e' option, to print out
the process environment.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 09-May-1994  cgd new ps, to match libkvm
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.10.2.1 06-Aug-1994  mycroft update from trunk
 1.21.2.1 05-May-1998  mycroft Pull up 1.23, per request of bouyer. Also do 1.24.
 1.31.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.36.4.2 27-Jan-2001  jhawk Pull up revisions 1.40-1.42 (requested by hubertf):
Clarify meaning of COMMANDs printed in parentheses. Addresses PR11930.
 1.36.4.1 25-Jun-2000  simonb Pull up rev 1.37:

Remove the 'poip' (pageouts in progress) keyword from the manpage.
From PR bin/8843 by Brian Stark.
 1.50.2.5 12-Nov-2002  nathanw Catch up to -current.
 1.50.2.4 24-Apr-2002  nathanw Note the presense of multiple LWPs or scheduler activations under "state".
Add a "lstate" that prints LWP-specific state and display it instead
of "state" under ps -s.
 1.50.2.3 23-Apr-2002  wrstuden Add -s to synopsis.
 1.50.2.2 23-Apr-2002  nathanw Add support for handling multi-LWP processes by selecting one LWP as
"representative" of the process.
Also add a -s option that prints a line for each LWP.
While I'm here, remove the /proc fallback code.
 1.50.2.1 04-Mar-2002  nathanw file ps.1 was added on branch nathanw_sa on 2002-04-23 20:41:13 +0000
 1.71.2.1 12-Nov-2005  tron Pull up following revision(s) (requested by chs in ticket #953):
bin/ps/ps.1: revision 1.72
add "uaddr" and improve text for "laddr".
 1.78.4.3 23-Mar-2008  matt sync with HEAD
 1.78.4.2 09-Jan-2008  matt sync with HEAD
 1.78.4.1 06-Nov-2007  matt sync with HEAD
 1.86.4.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.93.2.1 17-Feb-2011  bouyer Sync with HEAD
 1.94.4.3 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.94.4.2 30-Oct-2012  yamt sync with head
 1.94.4.1 17-Apr-2012  yamt sync with head
 1.95.2.1 23-Jun-2012  riz Pull up following revision(s) (requested by joerg in ticket #328):
lib/libc/gen/devname.c: revision 1.22
distrib/sparc64/instfs/dot.profile: revision 1.7
distrib/i386/cdroms/install.sh: revision 1.3
tests/lib/libcurses/director/director.c: revision 1.10
lib/libc/include/reentrant.h: revision 1.15
distrib/amd64/cdroms/install.sh: revision 1.3
tests/include/t_paths.c: revision 1.12
share/man/man7/hier.7: revision 1.101
etc/rc.d/sysdb: revision 1.24
include/stdlib.h: revision 1.99
tests/lib/libcurses/Makefile: revision 1.3
distrib/sparc64/cdroms/installcd/install.sh: revision 1.3
distrib/i386/cdroms/Makefile.cdrom: revision 1.27
lib/libc/gen/ttyname.c: revision 1.25
usr.bin/tic/Makefile: revision 1.2
tools/compat/Makefile: revision 1.57
lib/libterminfo/compile.c: revision 1.7
lib/libc/gen/ttyname.c: revision 1.26
lib/libterminfo/compile.c: revision 1.8
tools/compat/Makefile: revision 1.59
tools/compat/cdbw.h: revision 1.1
distrib/i386/installimage/install.sh: revision 1.2
lib/libc/include/namespace.h: revision 1.154
usr.bin/tic/tic.c: revision 1.13
usr.bin/tic/tic.c: revision 1.14
usr.bin/tic/tic.c: revision 1.15
distrib/sets/lists/base/mi: revision 1.996
usr.bin/tic/tic.c: revision 1.16
usr.sbin/dev_mkdb/dev_mkdb.8: revision 1.13
usr.bin/tic/tic.c: revision 1.17
usr.bin/tic/tic.c: revision 1.18
tools/compat/cdbr.h: revision 1.1
distrib/sparc64/instfs/list: revision 1.25
usr.bin/tic/tic.c: revision 1.19
lib/libc/cdb/cdbw.c: revision 1.4
lib/libc/cdb/cdbw.3: revision 1.4
lib/libc/cdb/cdbw.3: revision 1.5
share/terminfo/Makefile: revision 1.3
usr.sbin/dev_mkdb/dev_mkdb.c: revision 1.29
usr.sbin/dev_mkdb/Makefile: revision 1.8
lib/libc/gen/ttyname.3: revision 1.23
lib/libc/gen/ttyname.3: revision 1.24
lib/libc/cdb/cdbr.c: revision 1.3
include/cdbw.h: revision 1.2
distrib/sparc64/cdroms/installcd/Makefile: revision 1.15
lib/libc/gen/devname.3: revision 1.12
tools/compat/compat_defs.h: revision 1.85
tools/compat/compat_defs.h: revision 1.86
lib/libterminfo/term_private.h: revision 1.10
lib/libterminfo/term.c: revision 1.14
usr.bin/tic/tic.1: revision 1.10
lib/libterminfo/term.c: revision 1.15
lib/libterminfo/Makefile.hash: revision 1.5
lib/libterminfo/term.c: revision 1.16
distrib/amd64/installimage/install.sh: revision 1.2
share/man/man5/rc.conf.5: revision 1.152
distrib/sets/lists/tests/mi: revision 1.472
distrib/amd64/cdroms/Makefile.cdrom: revision 1.7
include/paths.h: revision 1.41
bin/ps/ps.1: revision 1.101
lib/libterminfo/terminfo.5.in: revision 1.16
usr.bin/tic/tic.c: revision 1.20
lib/libterminfo/terminfo.5.in: revision 1.17
Prefer __printflike.
Use queue(3)
Replace linear lookup with hash table, reducing runtime by 60%.
Use e* from util.h.
One more strdup -> estrdup.
Maintain nalias and nterm on the fly.
Add void for ttyslot prototype.
Use memcmp in place of strncmp, since length is known to no longer than
either of the arguments.
Fix a number of memory leaks. Keep final loop of the cleanup in tic(1)
under #ifdef __VALGRIND__ though.
Add __predict_true / __predict_false definitions.
Add a special seeder for cdbw_output that gives stable results.
Hook up cdbw(3) for the tool build.
Prototype for cdbw_stable_seeder.
Provide a usable thr_once for !_REENTRANT
Switch device database to cdb(5). Rework ttyname(3) and ttyname_r(3) to
depend on new devname_r(3) as heart. Add /dev/pts magic directly to
devname(3). While it can lead to returning non-existing paths, the
behavior is more consistent that way. Drop caching layer in devname(3),
it doesn't buy anything for the common case of having access to the
database. Teach devname(3) proper fallback behavior of scanning /dev.
Create both old-style and new-style database for now in /etc/rc.d/sysdb.
Switch terminfo(3) to cdb(5).
Fix typos.
Make Alan Barrett happy and add ().
Add cdbr(3) support to tools/compat.
Bump date for previous.
Relax consistency to make it possible to match valid entries.
Prefix devname's buffer with /dev/ as ttyname is supposed to return the
full path. Reported by Dave Huang.
 1.101.2.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.103.8.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.106.6.1 12-Apr-2018  martin Pull up following revision(s) (requested by kamil in ticket #713):

sys/modules/procfs/Makefile: revision 1.4
sys/miscfs/procfs/procfs_vfsops.c: revision 1.98
bin/ps/ps.1: revision 1.108
sys/compat/linux/arch/i386/linux_ptrace.c: revision 1.32
sys/miscfs/procfs/procfs_vnops.c: revision 1.198
sys/kern/sys_ptrace_common.c: revision 1.23
sys/kern/sys_ptrace_common.c: revision 1.24
sbin/mount_procfs/mount_procfs.8: revision 1.36
sys/kern/sys_ptrace_common.c: revision 1.25
sys/kern/sys_ptrace.c: revision 1.5
sys/compat/linux/arch/powerpc/linux_ptrace.c: revision 1.30
sys/sys/proc.h: revision 1.342
sys/kern/sys_ptrace_common.c: revision 1.26
sys/miscfs/procfs/procfs_ctl.c: file removal
sys/kern/sys_ptrace_common.c: revision 1.27
sys/miscfs/procfs/procfs_subr.c: revision 1.109
sys/kern/sys_ptrace_common.c: revision 1.28
sys/secmodel/extensions/secmodel_extensions.c: revision 1.8
sys/kern/sys_ptrace_common.c: revision 1.29
sys/sys/ptrace.h: revision 1.62
sys/compat/netbsd32/netbsd32_signal.c: revision 1.45
share/man/man9/kauth.9: revision 1.109
sys/miscfs/procfs/files.procfs: revision 1.12
sys/compat/netbsd32/netbsd32.h: revision 1.115
sys/miscfs/procfs/procfs.h: revision 1.72
sys/compat/netbsd32/netbsd32_ptrace.c: revision 1.5
sys/kern/kern_sig.c: revision 1.337
sys/sys/kauth.h: revision 1.75
sys/sys/sysctl.h: revision 1.224
sys/kern/sys_ptrace_common.c: revision 1.30
sys/kern/sys_ptrace_common.c: revision 1.31
sys/kern/sys_ptrace_common.c: revision 1.32
sys/kern/sys_ptrace_common.c: revision 1.33
sys/compat/linux/arch/arm/linux_ptrace.c: revision 1.20
sys/kern/sys_ptrace_common.c: revision 1.34
sys/kern/sys_ptrace_common.c: revision 1.36
sys/kern/kern_proc.c: revision 1.207
sys/kern/kern_exit.c: revision 1.269
doc/TODO.ptrace: revision 1.29

Make {s,g}et{db,fp,}regs work again for PK_32 processes
XXX: pullup-8

add disgusting magic to handle compat_netbsd32 as a module.

use process_*reg32 instead of struct *reg32.

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>

untangle the mess:
- factor out common code
- break each ptrace subcall to its own sub-function
.. more to come ...
- reduce ifdef ugliness by moving it up top.
- factor out PT_IO and make PT_{READ,WRITE}_{I,D} use it
- factor out PT_DUMPCORE
- factor out sendsig code
.. more to come ...

handle siginfo requests for ptrace32

ptrace: Partially undo PT_{READ,WRITE}_{I,D} and unbreak these commands

The refactored code did not work and was generating EFAULT.

Sponsored by <The NetBSD Foundation>

Merge the code back; the problem was that since we are reading/writing
to a kernel address for PT_{READ,WRITE}_{I,D} we need the kernel vmspace.
provide separate read and write functions to accomodate register functions
that need a size argument.

don't ignore error from copyout_piod

Use the proper process (the tracee) to get information about lwps and
registers and the tracer for vmspace.

Add new sysctl(3) entry: security.models.extensions.user_set_dbregs

Model this new sysctl(3) entry after "user_set_cpu_affinity" in the same
level of sysctl(3) switches.

Allow to read unconditionally Debug Registers (no change here). This is
convenient as even if a user of a debugger does not use hardware assisted
watchpoints/breakpoints, a debugger can still prompt these values to store
in an internal cache with context of registers. Reading them should have
no security concerns.

Add a paranoid MI switch that prohibits by default setting these registers
by a regular user (non-superuser). Make this switch disabled by default.
There are enough reserved bits out there to allow using them
unconditionally on hardened hosts.

Features shipped with Debug Registers are optional features in debuggers.
There is no reduction in elementary functionality.

Reviewed by <christos>

Sponsored by <The NetBSD Foundation>
 1.109.6.1 07-Aug-2019  martin Pull up following revision(s) (requested by kamil in ticket #24):

bin/ps/ps.1: revision 1.110
bin/ps/keyword.c: revision 1.57

Restore maxrss, idrss, isrss, ixrss printing in ps(1)

The RSS related statistics are now back in the NetBSD kernel.
These values were disabled since day0 until today.
libkvm(3) users will still receive inappropriate values as RSS statistics=
are updated upon sysctl(3) call.

Patch submitted by <Krzysztof Lasocki>
 1.109.4.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.109.4.2 21-Apr-2020  martin Sync with HEAD
 1.109.4.1 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.110.6.1 06-Jun-2021  cjep sync with head
 1.98 13-Jul-2025  rillig ps: remove unreachable break statement after return statement

No binary change.
 1.97 14-Sep-2021  christos branches: 1.97.4;
use emalloc and friends, add ktrace flag printing.
 1.96 04-Jun-2021  christos use parsenum like everywhere else in the code.
 1.95 04-Jun-2021  wiz Sort options in usage.
 1.94 04-Jun-2021  cjep PR standards/11223

Add -G to take a single group argument heading towards POSIX.2 compliance.
Patch from jperkin and reviewed by simonb.
 1.93 15-Sep-2019  kamil branches: 1.93.6;
ps(1): Guard freeing the memory of pinfo with __NO_LEAKS.

No more leaks are detected by LSan/NetBSD as of the LLVM snapshot
(clang10svn) from 2019-09-15.
 1.92 11-Sep-2019  kamil Plug memory leak in ps(1)

pinfo is allocated in setpinfo() with calloc(3).

Free it when no longer used, just before the program termination.

Detected with LSan.
 1.91 11-Apr-2018  christos branches: 1.91.2;
kvm_geterr() already contains errno, use errx.
 1.90 11-Apr-2018  christos use EXIT_FAILURE instead of 1
 1.89 12-Jan-2018  kamil branches: 1.89.2;
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>
 1.88 26-Dec-2016  rin branches: 1.88.6;
simplify logic; there must be no CPU usage when p_swtime is zero
 1.87 02-Dec-2016  christos provide a tree like display with -d, from FreeBSD
 1.86 28-Nov-2016  rin Calculate CPU usage (pcpu) once per process if it is required. This change
significantly improves performance for slow machines when output is sorted
by pcpu.

ok martin
 1.85 28-Nov-2016  rin KNF
ok martin
 1.84 05-Sep-2016  sevan Drop main() prototype.
 1.83 16-Jun-2015  christos branches: 1.83.2;
remove \n from error messages
 1.82 28-Jun-2014  dholland Don't print junk errnos. Avoids:
% ps -tfoo
ps: /dev/ttyfoo: Undefined error: 0
Exit 1
 1.81 20-Apr-2014  dholland Don't bother using variables whose value is never changed from the
initialization value.
 1.80 19-Feb-2014  dsl branches: 1.80.2;
Remove some pointless inclusions os sys/user.h
 1.79 15-Jan-2014  mlelstv Make ps -s use LTIME instead of TIME in the default output format.
 1.78 07-May-2012  joerg branches: 1.78.2;
Push logic to convert a ttyname to a device number into its own
function. Improve dealing with ptyfs by explicitly handling missing
pts/%d entries, if the kernel supports the pts device (PR 40813).
 1.77 15-Apr-2012  wiz Choose better argument name for `-U' (sync with man page).
From Bug Hunting in email.
 1.76 29-Aug-2011  joerg branches: 1.76.2; 1.76.4;
Use __dead
 1.75 31-May-2010  rmind Fix ps(1) and top(1) to show reasonable CPU numbers i.e. cpu_index() provided
by the kernel, instead of CPU order number, which is generally random.
 1.74 29-Mar-2009  mrg - add new RLIMIT_AS (aka RLIMIT_VMEM) resource that limits the total
address space available to processes. this limit exists in most other
modern unix variants, and like most of them, our defaults are unlimited.
remove the old mmap / rlimit.datasize hack.

- adds the VMCMD_STACK flag to all the stack-creation vmcmd callers.
it is currently unused, but was added a few years ago.

- add a pair of new process size values to kinfo_proc2{}. one is the
total size of the process memory map, and the other is the total size
adjusted for unused stack space (since most processes have a lot of
this...)

- patch sh, and csh to notice RLIMIT_AS. (in some cases, the alias
RLIMIT_VMEM was already present and used if availble.)

- patch ps, top and systat to notice the new k_vm_vsize member of
kinfo_proc2{}.

- update irix, svr4, svr4_32, linux and osf1 emulations to support
this information. (freebsd could be done, but that it's best left
as part of the full-update of compat/freebsd.)


this addresses PR 7897. it also gives correct memory usage values,
which have never been entirely correct (since mmap), and have been
very incorrect since jemalloc() was enabled.

tested on i386 and sparc64, build tested on several other platforms.

thanks to many folks for feedback and testing but most espcially
chuq and yamt for critical suggestions that lead to this patch not
having a special ugliness i wasn't happy with anyway :-)
 1.73 14-Feb-2009  lukem fix -Wsign-compare issues
 1.72 28-Dec-2008  christos branches: 1.72.2;
fix dev_t 64 bit issues.
 1.71 26-Sep-2008  wiz branches: 1.71.4;
Sync usage with man page.
 1.70 26-Sep-2008  apb Add "ps -A" option. This displays information about all processes,
like BSD "ps -ax" or SYSV "ps -ef". It's specified in SUSv3.

Also make some minor clarifications to the man page.
 1.69 20-Jul-2008  lukem Remove the \n and tabs from the __COPYRIGHT() strings.
 1.68 26-May-2008  christos PR/38755: murray armfield: /bin posix programs missing setprogname and/or
setlocale
 1.67 28-Apr-2008  martin branches: 1.67.2;
Remove clause 3 and 4 from TNF licenses
 1.66 10-Mar-2008  dholland branches: 1.66.2;
ps -t? needs to imply -x. Otherwise the non-x logic filters all the results
out. Thanks to der Mouse for prompting me to fix it...
 1.65 10-Feb-2008  christos branches: 1.65.2;
Add a CPUID keyword; idea and some of the code from Anon Ymous
 1.64 24-Oct-2007  yamt make -s alter the default format rather than doing parsefmt.
it allows users to use -o to replace it later.
 1.63 17-Feb-2007  pavel branches: 1.63.4;
Revert the changes done after newlock2 merge, except for the removal of
the now unused LSDEAD state. The current kernel headers are now
source-compatible with pre-newlock2 kernel headers again.

Approved by Andrew Doran.
 1.62 09-Feb-2007  ad Sync with kernel changes introduced by merging the newlock2 branch.
 1.61 02-Oct-2006  apb * When all columns are given null customised headers, the blank header
line is not printed at all. This is specified in P1003.1-2004
(SUSv3), and is useful.

* Customised headers may contain embedded space, commas and equals
signs. To specify multiple customised headers, use multiple -o or -O
options. This is specified (for "-o", not for "-O") in P1003.1-2004
(SUSv3), and is useful.

* When a column is given a null (blank) customised header, it keeps its
default minimum width. This is specified in P1003.1-2004 (SUSv3), and
seems harmless.

* Fix a bug that made it impossible to print the same keyword multiple
times, with different customised headers each time. (Previously, the
last customised header was used for all instances of the keyword.)

* Make the behaviour of "-O" more useful. The first -O option adds
the default keywords only if there have not yet been any formatting
options, and multiple -O options now insert their keywords in adjacent
positions. Now {ps -j -O %cpu} is like {ps -j} with one extra column;
Previously, it would have had all the columns implied by "-j", plus
all the default columns, plus the extra column specified by "-O".

* Convert from home-grown linked lists to SIMPLEQ lists.

Discussed in tech-userlevel.
 1.60 02-Sep-2006  christos factor out the code that does numeric parsing into a function and check for
errors properly.
 1.59 26-Jun-2005  christos sprinkle a little const, and now everything compiles with WARNS=3
 1.58 01-Jun-2005  lukem appease gcc -Wuninitialized
 1.57 11-Feb-2005  christos allow -t ttyp0 -t p0 -t /dev/ttyp0 -t /dev/pts/0
 1.56 29-Nov-2004  atatat Make ps understand "-tpts/4" by assuming that ptyfs is mounted on
/dev/pts and that the '/' immediately preceding the digits does not
need a 'p' in front of it (ick, kludge_oldps_options()).
 1.55 29-Oct-2004  dsl Add (unsigned char) cast to ctype function
 1.54 27-Mar-2004  simonb #define<TAB>
 1.53 27-Mar-2004  simonb Wrap some long lines.
Consisently use (void) in front of printf(...);
A few more white-space nits.
 1.52 27-Mar-2004  simonb KNF, ANSFify, de-__P, unC++ify, white space nits.
 1.51 11-Jan-2004  jdolecek g/c SDEAD
 1.50 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.49 06-Mar-2003  dsl - add keywords for utime, stime and ctime (output sss.ssssss if small)
- fiddle with keyword table - mainly to keep width under 80 cols)
- add -k keylist so sort output
- don't use logs to work out field width, get -MAXINT right
- user defined headers can be 1 char (actually they can be zero length)
so ?? in tty column isn't guaranteed to be wider than header
- use PRId64 to remove some long long casts
- use kvm_openfiles(NULL, NULL, NULL, KVN_NO_FILES, ..) unless core file
specified
(approved by christos)
 1.48 18-Jan-2003  thorpej Merge the nathanw_sa branch.
 1.47 19-Jun-2002  jdolecek g/c procfs fallback code, it's redundant now that we use sysctl to get
process info
 1.46 20-Dec-2001  soren branches: 1.46.2;
Sync getopt() / man page with actual getopt options.
 1.45 03-Nov-2001  lukem fix minor WARNS=2
 1.44 30-Jul-2001  wiz Remove duplicate `if'. Noted by Geoff C. Wing in bin/13551.
 1.43 20-Jul-2001  matt Add a small optimizetion in savevars(). break out of the loop once needcomm
is set since setting it multiple times only wastes cpu time.
 1.42 14-Jun-2001  cyber If there were no processes to list, it meant that our criteria failed
and didnt match anything. Dont make our caller think we succeeded.
 1.41 16-Jun-2000  simonb Don't check namelist when choosing to use the sysctl interface.
 1.40 08-Jun-2000  simonb Fix handling of "ps U<user>" where <user> ended it 't' - now if the
first argument doesn't start with a '-' and ends in 't', the 't' only
gets converted to a 'T' (for listing processes on the current tty)
if it doesn't contain any other command line option characters that
take an argument. Problem noticed by ITOH Yasufumi on tech-userlevel.

If the 'start' format was the last column, the width wouldn't be set
and started() output a zero length string instead of a string the right
length - fix header width calculations so the the last column is handled
the same all others. This only showed up for 'start' because started()
knew that the column header is the same width and the field itself...

Update copyrights.
 1.39 07-Jun-2000  simonb Calculate field widths on the fly so that all columns line up nicely
and make more effective use of screen real estate when some columns
(eg USER and VSZ) didn't need the full default width.
 1.38 26-May-2000  simonb branches: 1.38.2;
Use new sysctl/kvm interfaces. This will stop the "proc size mismatch"
errors when internal kernel structures change size. Also remove
the sgid bit - all live kernel data is accessed through the sysctl
interface.
 1.37 29-Apr-2000  abs If ps cannot get the terminal size from STDOUT, it tried STDERR and STDIN.
What this does to the POLS when a script that is parsing ps output just happens
to run in a narrow terminal cannot be described in polite company.
If STDOUT is not a tty, leave width at 80.
 1.36 15-Apr-2000  simonb Use tty "?" to select processes with no controlling tty and tty "-" to
select processes with a revoked controlling tty.

Idea from SunOS/Solaris, suggested by Alexis Rosen.
 1.35 10-Apr-2000  chs make -M imply -K. the current system is not likely to be
a reasonable approximation of a crash dump.
 1.34 04-Dec-1999  hubertf Document -U switch (displays processes belonging to given user/uid).
Reported in PR 8943 by WL <wliao@dura.spc.uchicago.edu>
 1.33 03-Dec-1999  simonb "ps -e" only shows the environment for the processes owned by the
current user id or all process if run by root.

Fixes PR security/5967 from Todd Vierling.
 1.32 09-Nov-1999  drochner Since our gcc doesn't warn about NULL format strings anymore, we can
fix the incorrect err(1, "%s", "") et al.
Closes PR bin/7592 by cgd.
 1.31 15-Oct-1999  jdolecek command(): don't start using procfs_getargv() instead of kvm_getargv()
just because the latter returned a NULL - that may normally happen,
when the process is, for example, a kernel thread

procfs_getargv(): if the name is brackened in '(' and ')', just
return NULL immediately - the code in command() will DTRT
and the output will be same for procfs- and kvm- based lookup
in all cases
 1.30 15-Oct-1999  jdolecek getkinfo_kvm(): fix bug in previous - handled size_t i as if it would be signed
saveuser(): don't check kd - if we get here, kd is never NULL
 1.29 15-Oct-1999  jdolecek When using the procfs for extracting process information, extract
also process start time, process arguments and session leadership status.

The procfs fallback is also used when kvm_openfiles() completely fails
(e.g. when /dev/mem is not readable).

Solves PR 7772, though the final implementation is different.
 1.28 27-Mar-1999  bgrayson branches: 1.28.2; 1.28.4;
Minor fixes:
1. Use statfs() to verify that /proc is a procfs.
2. Add -K option to disable /proc-based method.
3. Make warnings less verbose, but still accurate.
 1.27 26-Mar-1999  bgrayson Added experimental fallback /proc-based lookup. Warnings are printed when the
fallback method is used, as the results could be untrustworthy if an
intruder is present. It is highly likely that NetBSD-1.5 will have
an improved kvm interface for reading process information, at which
point this code can be garbage-collected. Also added a word to the
man page -x option description while I was here.
 1.26 04-Jan-1999  kim Fixed segfaulting for "ps -T". There is no optarg with -T, so instructions
under label "tty:" need to use ttname instead.
 1.25 28-Jul-1998  mycroft Add a -U option to select by user/uid.
 1.24 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.23 28-Jul-1998  mycroft Delint.
 1.22 27-Jul-1998  mycroft __AUDIT__ cleanup.
 1.21 06-Jul-1998  mrg - change setgid kmem programs (that lend themselves to this) so setegid(getgid())
and the top, and then set the effective gid back to kmem around the call to
kvm_openfiles(). this reduces the time group kmem is available.
- for those above that also allow this, setgid(getgid()) after the call to
kvm_openfiles() to fully revoke priviledges.
- some KNF
- use err(3) over fprintf(3) in some places
 1.20 14-Sep-1997  lukem getopt returns -1 not EOF
cleanup manpage
 1.19 20-Jul-1997  christos Fix compiler warnings
Add WARNS=1
 1.18 21-Apr-1997  mrg note that strcpy is safe.
 1.17 28-Feb-1997  pk Message buffer passed to kvm_openfiles() should _POSIX2_LINE_MAX long; PR#3266
 1.16 27-Sep-1996  thorpej Fix SEGV when kludging old-style options.
From Greg Stark <gsstark@mit.edu>, PR #2550.
 1.15 18-May-1995  mycroft Create and strvis(3) the argument and environment lists only as needed.
Do not keep extra copies around.
 1.14 18-May-1995  mycroft Gather the argument and environment lists when we actually want to print them.
 1.13 21-Mar-1995  cgd convert to new RCS id conventions.
 1.12 16-Sep-1994  mycroft Integrate `-c' option, from Arne Juul.
 1.11 09-May-1994  cgd clean up import
 1.10 05-May-1994  cgd update for changed flags
 1.9 07-Oct-1993  cgd fix from Thomas Eberhardt <thomas@mathematik.uni-Bremen.de>:
don't try to display the resident text size; we can't do that anymore
 1.8 01-Aug-1993  mycroft Add RCS identifiers.
 1.7 19-Jul-1993  cgd branches: 1.7.2;
from Brian Somers <brian@awfulhak.demon.co.uk>:
The kvm entry isn't updated for command unless 'time' is given,
therefore
ps -o command
outputs blank lines [or, in NetBSD, seg faults]
ps -o time,command
works.
 1.6 11-Jul-1993  cgd fix alien 'w' handling. you really don't want to know...
 1.5 01-Jun-1993  cgd add support and documentation for the 'e' option, to print out
the process environment.
 1.4 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.3 22-Mar-1993  cgd added rcs ids to all files
 1.2 21-Mar-1993  cgd after 0.2.2 "stable" patches applied
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 09-May-1994  cgd new ps, to match libkvm
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.7.2.2 19-Jul-1993  cgd from Brian Somers <brian@awfulhak.demon.co.uk>:
The kvm entry isn't updated for command unless 'time' is given,
therefore
ps -o command
outputs blank lines [or, in NetBSD, seg faults]
ps -o time,command
works.
 1.7.2.1 19-Jul-1993  cgd file ps.c was added on branch netbsd-0-9 on 1993-07-19 11:02:11 +0000
 1.28.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.28.2.2 22-May-2000  he Pull up revision 1.37 (requested by abs):
Only check the terminal size via STDOUT, not STDERR or STDIN.
 1.28.2.1 08-Nov-1999  cgd pull up revs 1.29-1.31 from trunk (requested by jdolecek):
When using the procfs for extracting process information, extract process
start time, arguments, and session leadership status. Also, fall back to
procfs when kvm_openfiles() completely fails (e.g. when /dev/mem is not
readable). Fixes PR#7772.
 1.38.2.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.46.2.11 12-Nov-2002  nathanw Catch up to -current.
 1.46.2.10 24-Oct-2002  nathanw Small typo fix.

Treat SSTOP and SACTIVE the same way in pick_representative_lwp(), and
include LSSTOP in the state hierarchy.
 1.46.2.9 26-Jul-2002  nathanw Consider LWPs in LSSUSPENDED as a worst-case for a representative LWP.
 1.46.2.8 06-Jun-2002  nathanw Use the real process state when picking a representative LWP, not the
old-style state that was faked up for us by sysctl.
 1.46.2.7 02-May-2002  nathanw Don't puke on zero-LWP processes (zombies).
 1.46.2.6 24-Apr-2002  nathanw Note the presense of multiple LWPs or scheduler activations under "state".
Add a "lstate" that prints LWP-specific state and display it instead
of "state" under ps -s.
 1.46.2.5 23-Apr-2002  wrstuden Add -s to synopsis.
 1.46.2.4 23-Apr-2002  nathanw Don't need to free the kinfo_lwp pointer; libkvm does that.
 1.46.2.3 23-Apr-2002  nathanw Don't shadow global symbol "sleep".
 1.46.2.2 23-Apr-2002  nathanw Add support for handling multi-LWP processes by selecting one LWP as
"representative" of the process.
Also add a -s option that prints a line for each LWP.
While I'm here, remove the /proc fallback code.
 1.46.2.1 20-Dec-2001  nathanw file ps.c was added on branch nathanw_sa on 2002-04-23 20:41:14 +0000
 1.63.4.2 23-Mar-2008  matt sync with HEAD
 1.63.4.1 06-Nov-2007  matt sync with HEAD
 1.65.2.1 24-Mar-2008  keiichi sync with head.
 1.66.2.2 04-Jun-2008  yamt sync with head
 1.66.2.1 18-May-2008  yamt sync with head.
 1.67.2.2 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.67.2.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.71.4.1 01-Apr-2009  snj Pull up following revision(s) (requested by mrg in ticket #622):
bin/csh/csh.1: revision 1.46
bin/csh/func.c: revision 1.37
bin/ps/print.c: revision 1.111
bin/ps/ps.c: revision 1.74
bin/sh/miscbltin.c: revision 1.38
bin/sh/sh.1: revision 1.92 via patch
external/bsd/top/dist/machine/m_netbsd.c: revision 1.7
lib/libkvm/kvm_proc.c: revision 1.82
sys/arch/mips/mips/cpu_exec.c: revision 1.55
sys/compat/darwin/darwin_exec.c: revision 1.57
sys/compat/ibcs2/ibcs2_exec.c: revision 1.73
sys/compat/irix/irix_resource.c: revision 1.15
sys/compat/linux/arch/amd64/linux_exec_machdep.c: revision 1.16
sys/compat/linux/arch/i386/linux_exec_machdep.c: revision 1.12
sys/compat/linux/common/linux_limit.h: revision 1.5
sys/compat/osf1/osf1_resource.c: revision 1.14
sys/compat/svr4/svr4_resource.c: revision 1.18
sys/compat/svr4_32/svr4_32_resource.c: revision 1.17
sys/kern/exec_subr.c: revision 1.62
sys/kern/init_sysctl.c: revision 1.160
sys/kern/kern_exec.c: revision 1.288
sys/kern/kern_resource.c: revision 1.151
sys/sys/param.h: patch
sys/sys/resource.h: revision 1.31
sys/sys/sysctl.h: revision 1.184
sys/uvm/uvm_extern.h: revision 1.153
sys/uvm/uvm_glue.c: revision 1.136
sys/uvm/uvm_mmap.c: revision 1.128
usr.bin/systat/ps.c: revision 1.32
- - add new RLIMIT_AS (aka RLIMIT_VMEM) resource that limits the total
address space available to processes. this limit exists in most other
modern unix variants, and like most of them, our defaults are unlimited.
remove the old mmap / rlimit.datasize hack.
- - adds the VMCMD_STACK flag to all the stack-creation vmcmd callers.
it is currently unused, but was added a few years ago.
- - add a pair of new process size values to kinfo_proc2{}. one is the
total size of the process memory map, and the other is the total size
adjusted for unused stack space (since most processes have a lot of
this...)
- - patch sh, and csh to notice RLIMIT_AS. (in some cases, the alias
RLIMIT_VMEM was already present and used if availble.)
- - patch ps, top and systat to notice the new k_vm_vsize member of
kinfo_proc2{}.
- - update irix, svr4, svr4_32, linux and osf1 emulations to support
this information. (freebsd could be done, but that it's best left
as part of the full-update of compat/freebsd.)
this addresses PR 7897. it also gives correct memory usage values,
which have never been entirely correct (since mmap), and have been
very incorrect since jemalloc() was enabled.
tested on i386 and sparc64, build tested on several other platforms.
thanks to many folks for feedback and testing but most espcially
chuq and yamt for critical suggestions that lead to this patch not
having a special ugliness i wasn't happy with anyway :-)
 1.72.2.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.76.4.1 24-Jun-2012  riz Pull up following revision(s) (requested by dholland in ticket #355):
bin/ps/ps.c: revision 1.78
Push logic to convert a ttyname to a device number into its own
function. Improve dealing with ptyfs by explicitly handling missing
pts/%d entries, if the kernel supports the pts device (PR 40813).
 1.76.2.3 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.76.2.2 23-May-2012  yamt sync with head.
 1.76.2.1 17-Apr-2012  yamt sync with head
 1.78.2.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.80.2.1 10-Aug-2014  tls Rebase.
 1.83.2.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.88.6.1 03-Feb-2018  snj Pull up following revision(s) (requested by kamil in ticket #515):
bin/ps/ps.c: revision 1.89
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>
 1.89.2.1 16-Apr-2018  pgoyette Sync with HEAD, resolve some conflicts
 1.91.2.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.91.2.2 21-Apr-2020  martin Sync with HEAD
 1.91.2.1 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.93.6.1 06-Jun-2021  cjep sync with head
 1.97.4.1 02-Aug-2025  perseant Sync with HEAD
 1.32 14-Sep-2021  christos use emalloc and friends, add ktrace flag printing.
 1.31 14-Sep-2021  christos Provide symbolic printing of some keywords by capitalizing them.
 1.30 10-Sep-2021  rillig bin: remove unnecessary lint comment CONSTCOND

Since 2021-01-31, lint no longer warns about 'do ... while (0)'.

No functional change.
 1.29 02-Dec-2016  christos branches: 1.29.14;
provide a tree like display with -d, from FreeBSD
 1.28 28-Nov-2016  rin Calculate CPU usage (pcpu) once per process if it is required. This change
significantly improves performance for slow machines when output is sorted
by pcpu.

ok martin
 1.27 20-Apr-2014  dholland branches: 1.27.6;
Use an enum type for PRINTMODE vs. WIDTHMODE. Compiler output diffs have
been checked.
 1.26 02-Oct-2006  apb branches: 1.26.40; 1.26.46; 1.26.60;
* When all columns are given null customised headers, the blank header
line is not printed at all. This is specified in P1003.1-2004
(SUSv3), and is useful.

* Customised headers may contain embedded space, commas and equals
signs. To specify multiple customised headers, use multiple -o or -O
options. This is specified (for "-o", not for "-O") in P1003.1-2004
(SUSv3), and is useful.

* When a column is given a null (blank) customised header, it keeps its
default minimum width. This is specified in P1003.1-2004 (SUSv3), and
seems harmless.

* Fix a bug that made it impossible to print the same keyword multiple
times, with different customised headers each time. (Previously, the
last customised header was used for all instances of the keyword.)

* Make the behaviour of "-O" more useful. The first -O option adds
the default keywords only if there have not yet been any formatting
options, and multiple -O options now insert their keywords in adjacent
positions. Now {ps -j -O %cpu} is like {ps -j} with one extra column;
Previously, it would have had all the columns implied by "-j", plus
all the default columns, plus the extra column specified by "-O".

* Convert from home-grown linked lists to SIMPLEQ lists.

Discussed in tech-userlevel.
 1.25 26-Jun-2005  christos sprinkle a little const, and now everything compiles with WARNS=3
 1.24 27-Mar-2004  simonb Add support for the POSIX.2 "args" and "comm" keywords. "args" is just
an alias for "command", and "comm" shows just argv[0] and not the whole
argument list.

Fix for part of PR standards/11224.
 1.23 27-Mar-2004  simonb #define<TAB>
 1.22 27-Mar-2004  simonb KNF, ANSFify, de-__P, unC++ify, white space nits.
 1.21 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.20 06-Mar-2003  dsl - add keywords for utime, stime and ctime (output sss.ssssss if small)
- fiddle with keyword table - mainly to keep width under 80 cols
- add -k keylist so sort output
- don't use logs to work out field width, get -MAXINT right
- user defined headers can be 1 char (actually they can be zero length)
so ?? in tty column isn't guaranteed to be wider than header
- remove some long long casts (compiles on sparc64 still)
- use kvm_openfiles(NULL, NULL, NULL, KVN_NO_FILES, ..) unless core file
specified,
(approved by christos)
 1.19 18-Jan-2003  thorpej Merge the nathanw_sa branch.
 1.18 24-Apr-2002  nathanw Since kinfo_proc2 has many values that are 64-bit, ps needs to know
that they're 64-bit, and grab them out of memory appropriately. Otherwise,
big-endian systems get the wrong end of the 64-bit value and lose.

Keywords affected: inblk, majflt, minflt, msgrcv, msgsnd, nivcsw, nsigs,
nswap, nvcsw, and oublk.
 1.17 07-Jun-2000  simonb branches: 1.17.4;
Calculate field widths on the fly so that all columns line up nicely
and make more effective use of screen real estate when some columns
(eg USER and VSZ) didn't need the full default width.
 1.16 26-May-2000  simonb branches: 1.16.2;
Use new sysctl/kvm interfaces. This will stop the "proc size mismatch"
errors when internal kernel structures change size. Also remove
the sgid bit - all live kernel data is accessed through the sysctl
interface.
 1.15 03-May-1999  mrg oops, fix merge botch in previous..
 1.14 03-May-1999  mrg limit session to 6 chars. from Geoff Wing <gcw@pobox.com> and fixes his PR#7506
 1.13 16-Apr-1999  christos Add support for printing wide sigset_t properly.
 1.12 02-Oct-1996  ws Fix p_nice vs. NZERO code.
Remove some unnecessary casts.
Remove an unused time format field.
 1.11 29-Sep-1995  cgd add INT32 and UINT32 variable type specifiers. some of the variables
that are printed are of fixed size.
 1.10 15-Aug-1995  cgd Kenneth Stailey pointed out that his previous fix was incorrect. RE and SL
should be bounded at 127, not 99.
 1.9 14-Aug-1995  cgd as suggested in PR 1347, by Kenneth Stailey, make "RE" and "SL" fields
show up as two digits, max, displaying "99" if > 99. allow any field with
an unsigned value to be specified as printing that way (though right now
it's only used for RE and SL, which are both unsigned longs). This fix
is substantially different than that suggested in the PR.
 1.8 18-May-1995  mycroft Create and strvis(3) the argument and environment lists only as needed.
Do not keep extra copies around.
 1.7 08-May-1995  cgd add the ability to understand and print INT and UINT-sized variables.
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 09-May-1994  cgd clean up import
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 09-May-1994  cgd new ps, to match libkvm
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.16.2.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.17.4.2 24-Apr-2002  nathanw Parallel change to mainline: handle 64-bit values properly.
 1.17.4.1 23-Apr-2002  nathanw Add support for handling multi-LWP processes by selecting one LWP as
"representative" of the process.
Also add a -s option that prints a line for each LWP.
While I'm here, remove the /proc fallback code.
 1.26.60.1 10-Aug-2014  tls Rebase.
 1.26.46.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.26.40.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.27.6.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.29.14.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.29.14.1 21-Apr-2020  martin Sync with HEAD
 1.8 20-Jul-1997  christos branches: 1.8.112;
Remove WARNS=1 from all the subdirectory Makefiles, and add it to Makefile.inc
now that all /bin has been cleaned.
 1.7 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.8.112.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.8.112.1 21-Apr-2020  martin Sync with HEAD
 1.27 04-Jul-2017  wiz branches: 1.27.6;
Use Ex.
 1.26 03-Jul-2017  wiz Remove workaround for ancient HTML generation code.
 1.25 12-Aug-2016  sevan Document the version pwd first appeared.
Bump date.
 1.24 30-Oct-2003  wiz file system, not filesystem; e.g. is an abbreviation; use FAT instead of msdos
when describing the file system.
 1.23 30-Oct-2003  dsl Make /bin/pwd almost conform to IEEE 1003.1
- Make 'pwd -L' fall back to 'pwd -P' if PWD is incorrect.
- Ignore PWD if it contains "/./" or "/../".
- Garbage collect some redundant code.
It is still non-conformant because posix mandates that the default
be 'pwd -L' (aka ksh), not 'pwd -P' (historic practise everywhere else).
Changing the default will break too much...
 1.22 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.21 02-Oct-2002  wiz evironment -> environment.
 1.20 25-Sep-2002  wiz New policy: New sentences start on a new line.
Patches by Robert Elz <kre at munnari oz au>, with minimal changes by me.
 1.19 08-Feb-2002  ross Generate <>& symbolically. I'm avoiding .../dist/... directories for now.
 1.18 04-Sep-2000  kleink For commands and utilities, use EXIT STATUS rather than RETURN VALUES as
appropriate (and documented in mdoc(7)).
 1.17 28-Aug-2000  hubertf Add 'RETURN VALUE' section header.
 1.16 17-Nov-1999  kleink branches: 1.16.4;
Note that the options are extensions.
 1.15 31-May-1999  kleink branches: 1.15.4;
Make it clearer that -L does not necessarily do the job, and, following
conventions, move exit status information into its own section.
 1.14 22-Mar-1999  garbled More and more .Os cleanups. .Os is defined in the tmac.doc-common file,
so we shouldn't override it with versions in the manpages. Many more to
come.
 1.13 03-Nov-1998  wsanchez Add -L flag.
 1.12 14-Sep-1997  lukem - getopt returns -1 not EOF
- use .Nm correctly in man page
 1.11 03-Mar-1997  cjs PR 3284, from enami tsugutomo <enami@ba2.so-net.or.jp>.
Remove space before punctuation.
 1.10 07-Sep-1995  jtc Sync with 4.4lite2
 1.9 25-Jul-1995  jtc Use "utility" instead of "command". Modern definitions of these terms
are distinct (See POSIX.2 glossary).

A utility is a executable, script or shell builtin; while a command
can be any of those things plus lists, pipelines, compound commands
(if, for, while) and shell function definitions.
 1.8 21-Mar-1995  cgd convert to new RCS id conventions.
 1.7 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.6 28-Aug-1993  jtc Document the fact that the pwd utility is POSIX.2 compliant.
 1.5 01-Aug-1993  mycroft Add RCS indentifiers.
 1.4 22-Jul-1993  jtc Update to "new" -mandoc macros.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.3 07-Sep-1995  jtc imported from 44lite2
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.15.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.16.4.1 28-Aug-2000  hubertf Pull up to netbsd-1-5 branch, OK'd by thorpej:

Log Message:
> Add 'RETURN VALUE' section header.

Files & Revisionis:
> cvs rdiff -r1.19 -r1.20 basesrc/bin/cat/cat.1
> cvs rdiff -r1.12 -r1.13 basesrc/bin/chmod/chmod.1
> cvs rdiff -r1.14 -r1.15 basesrc/bin/cp/cp.1
> cvs rdiff -r1.8 -r1.9 basesrc/bin/dd/dd.1
> cvs rdiff -r1.9 -r1.10 basesrc/bin/echo/echo.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/expr/expr.1
> cvs rdiff -r1.25 -r1.26 basesrc/bin/ls/ls.1
> cvs rdiff -r1.10 -r1.11 basesrc/bin/mkdir/mkdir.1
> cvs rdiff -r1.23 -r1.24 basesrc/bin/mt/mt.1
> cvs rdiff -r1.12 -r1.13 basesrc/bin/mv/mv.1
> cvs rdiff -r1.16 -r1.17 basesrc/bin/pwd/pwd.1
> cvs rdiff -r1.9 -r1.10 basesrc/bin/rm/rm.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/rmdir/rmdir.1
> cvs rdiff -r1.35 -r1.36 basesrc/bin/sh/sh.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/sleep/sleep.1
> cvs rdiff -r1.20 -r1.21 basesrc/bin/stty/stty.1
 1.27.6.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.27.6.1 21-Apr-2020  martin Sync with HEAD
 1.23 16-Nov-2021  kre Make pwd (both /bin/pwd and the /bin/sh built-in version) check for
write errors on stdout, and indicate an error if that happens.
 1.22 29-Aug-2011  joerg branches: 1.22.44;
Use __dead
 1.21 20-Jul-2008  lukem Remove the \n and tabs from the __COPYRIGHT() strings.
 1.20 26-May-2008  christos PR/38755: murray armfield: /bin posix programs missing setprogname and/or
setlocale
 1.19 30-Oct-2003  dsl branches: 1.19.30; 1.19.32;
Make /bin/pwd almost conform to IEEE 1003.1
- Make 'pwd -L' fall back to 'pwd -P' if PWD is incorrect.
- Ignore PWD if it contains "/./" or "/../".
- Garbage collect some redundant code.
It is still non-conformant because posix mandates that the default
be 'pwd -L' (aka ksh), not 'pwd -P' (historic practise everywhere else).
Changing the default will break too much...
 1.18 14-Sep-2003  jschauma Following private discussion with kleink@ and hubertf@ and public discussion
on tech-userlevel@, back out printescaped() functionality.
kleink: ``We sell rope.''
 1.17 13-Aug-2003  itojun use bounded string op
 1.16 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.15 04-Aug-2003  jschauma As discusses a little while back on tech-userlevel:

If stdout is a tty, use vis(3) to print any filenames to prevent garbage
from being printed if the filename contains control- or other non-printable
characters.

While here, sprinkle some EXIT_FAILURE and NOTREACHED where appropriate.
 1.14 16-Sep-2001  wiz ANSIfy, KNF, use [gs]etprogname. By Petri Koistinen.
 1.13 09-Nov-1999  drochner Since our gcc doesn't warn about NULL format strings anymore, we can
fix the incorrect err(1, "%s", "") et al.
Closes PR bin/7592 by cgd.
 1.12 31-May-1999  kleink branches: 1.12.4;
* Restructure getcwd_logical() to have a reasonable error description even if
$PWD is unset in the environment and update usage() string; addresses
PR bin/7504.
* Fix whitespace lossage from rev. 1.11.
* Minor other cleanups, including EXIT_{SUCCESS,FAILURE}.
 1.11 03-Nov-1998  wsanchez Add -L flag.
 1.10 28-Jul-1998  mycroft Delint.
 1.9 14-Sep-1997  lukem - getopt returns -1 not EOF
- use .Nm correctly in man page
 1.8 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.7 21-Mar-1995  cgd convert to new RCS id conventions.
 1.6 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.5 10-Sep-1993  jtc Include appropriate header files to bring prototypes into scope.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.12.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.19.32.2 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.19.32.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.19.30.1 04-Jun-2008  yamt sync with head
 1.22.44.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.22.44.1 21-Apr-2020  martin Sync with HEAD
 1.11 28-May-2007  tls branches: 1.11.78;
Add new Makefile knob, USE_FORT, which extends USE_SSP by turning on the
FORTIFY_SOURCE feature of libssp, thus checking the size of arguments to
various string and memory copy and set functions (as well as a few system
calls and other miscellany) where known at function entry. RedHat has
evidently built all "core system packages" with this option for some time.

This option should be used at the top of Makefiles (or Makefile.inc where
this is used for subdirectories) but after any setting of LIB.

This is only useful for userland code, and cannot be used in libc or in
any code which includes the libc internals, because it overrides certain
libc functions with macros. Some effort has been made to make USE_FORT=yes
work correctly for a full-system build by having the bsd.sys.mk logic
disable the feature where it should not be used (libc, libssp iteself,
the kernel) but no attempt has been made to build the entire system with
USE_FORT and doing so will doubtless expose numerous bugs and misfeatures.

Adjust the system build so that all programs and libraries that are setuid,
directly handle network data (including serial comm data), perform
authentication, or appear likely to have (or have a history of having)
data-driven bugs (e.g. file(1)) are built with USE_FORT=yes by default,
with the exception of libc, which cannot use USE_FORT and thus uses
only USE_SSP by default. Tested on i386 with no ill results; USE_FORT=no
per-directory or in a system build will disable if desired.
 1.10 26-Jun-2005  christos sprinkle a little const, and now everything compiles with WARNS=3
 1.9 16-Oct-2004  christos Add WARNS=3
 1.8 16-Oct-2004  dan teach rcmd about getport move in rsh directory
 1.7 05-Sep-2004  manu Kerberos support is broken in rcommands, thus making -K -k and -x options
unavailable. Remove theses options from the man pages (it won't be very
hard to bring them back if we fix kerberos one day)
 1.6 19-Aug-2002  lukem branches: 1.6.2;
Use ${NETBSDSRCDIR}/some/path instead of ${.CURDIR}/../../some/path (etc).
 1.5 10-Oct-1997  christos CFLAGS->CPPFLAGS
 1.4 20-Jul-1997  christos Remove WARNS=1 from all the subdirectory Makefiles, and add it to Makefile.inc
now that all /bin has been cleaned.
 1.3 20-Jul-1997  christos Add WARNS=1
 1.2 26-May-1997  mrg merge conflicts.
 1.1 16-Feb-1997  mrg branches: 1.1.1; 1.1.2;
file Makefile was initially added on branch mrg-rcmd.
 1.1.2.1 16-Feb-1997  mrg new rcmd(1) command, mostly cloned from rsh(1). for use with the
new rcmd(3) suite.
 1.1.1.1 26-May-1997  mrg rcmd backend.
 1.6.2.1 12-Nov-2004  he Pull up revision 1.8 (requested by martin to fix ticket #928):
Build with the new factored-out getport file in the rsh directory.
 1.11.78.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.11.78.1 21-Apr-2020  martin Sync with HEAD
 1.22 03-Jul-2017  wiz branches: 1.22.6;
Remove workaround for ancient HTML generation code.
 1.21 31-May-2011  wiz Sort options in SYNOPSIS, sort option descriptions, remove some extra
space, bump date for previous.
 1.20 31-May-2011  christos PR/45008: Martin Neitzel: document all rcmd(1) options. -46p were missing,
and -d was not in the synopsis.
 1.19 30-May-2008  mrg convert the rest of my licenses to 2-clause, extracting myself out
from a group as necessary.

bozohttpd remains, but it will get fixed next time i update it.
 1.18 05-Sep-2004  wiz branches: 1.18.24; 1.18.26;
Bump date for removal of Kerberos option descriptions.
Remove dots at end of SEE ALSO.
 1.17 05-Sep-2004  manu Kerberos support is broken in rcommands, thus making -K -k and -x options
unavailable. Remove theses options from the man pages (it won't be very
hard to bring them back if we fix kerberos one day)
 1.16 13-Oct-2003  agc Move Matt Green's code from a 4-clause to a 3-clause licence by removing
the advertising clause. Diffs provided in PR 22396 by Joel Baker, the changes
were confirmed to the board by Matt Green.
 1.15 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.14 26-May-2003  lukem * Document $RCMD_CMD in environ(7).
* Cross-reference rcmd(1), rcmd(3), and environ(7) as appropriate.

Should fix [bin/21670] from Geoff Wing.
 1.13 25-Sep-2002  wiz New policy: New sentences start on a new line.
Patches by Robert Elz <kre at munnari oz au>, with minimal changes by me.
 1.12 05-Mar-2002  wiz Drop duplicate .Pp.
 1.11 08-Feb-2002  ross Generate <>& symbolically. I'm avoiding .../dist/... directories for now.
 1.10 15-Jan-2002  wiz We don't want trailing dots in .Nd.
 1.9 20-Dec-2001  wiz Punctuation nit, sort SEE ALSO.
 1.8 03-Apr-2001  wiz Xref kerberos 8, not 3.
XXX: trailing xrefs to krb_sendauth(3) and krb_realmofhost(3) -- remove them?
 1.7 22-Mar-1999  garbled branches: 1.7.10;
More and more .Os cleanups. .Os is defined in the tmac.doc-common file,
so we shouldn't override it with versions in the manpages. Many more to
come.
 1.6 14-Jan-1998  scottr Grammar police, plus a bit of general cleanup of the BUGS section.
 1.5 20-Oct-1997  enami Fix .Nm usage.
 1.4 09-Jun-1997  mrg expand HISTORY section a bit.
 1.3 08-Jun-1997  jeremy Fix typo. [PR/3722]
 1.2 26-May-1997  mrg merge conflicts.
 1.1 16-Feb-1997  mrg branches: 1.1.1; 1.1.2;
file rcmd.1 was initially added on branch mrg-rcmd.
 1.1.2.1 16-Feb-1997  mrg new rcmd(1) command, mostly cloned from rsh(1). for use with the
new rcmd(3) suite.
 1.1.1.1 26-May-1997  mrg rcmd backend.
 1.7.10.1 04-Apr-2001  he Pull up revision 1.8 (requested by wiz):
Xref kerberos 8, not 3.
XXX: trailing xrefs to krb_sendauth(3) and krb_realmofhost(3) -- remove them?
 1.18.26.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.18.24.1 04-Jun-2008  yamt sync with head
 1.22.6.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.22.6.1 21-Apr-2020  martin Sync with HEAD
 1.25 14-Feb-2009  lukem branches: 1.25.48;
Default to WARNS=4 (except for rcp & ksh)
 1.24 20-Mar-2006  christos branches: 1.24.28;
Goodbye KerberosIV
 1.23 05-Sep-2004  manu Kerberos support is broken in rcommands, thus making -K -k and -x options
unavailable. Remove theses options from the man pages (it won't be very
hard to bring them back if we fix kerberos one day)
 1.22 23-Jul-2003  itojun split MKKERBEROS4 from MKKERBEROS. based on work by lha at stacken.kth.se
(build confirmed with both MKKERBEROS4=yes and MKKERBEROS4=no)
 1.21 19-Aug-2002  lukem Use ${NETBSDSRCDIR}/some/path instead of ${.CURDIR}/../../some/path (etc).
 1.20 22-Mar-2002  thorpej Split the notion of building Hesiod, Kerberos, S/key, and YP
infrastructure and using that infrastructure in programs.

* MKHESIOD, MKKERBEROS, MKSKEY, and MKYP control building
of the infratsructure (libraries, support programs, etc.)

* USE_HESIOD, USE_KERBEROS, USE_SKEY, and USE_YP control
building of support for using the corresponding API
in various libraries/programs that can use it.

As discussed on tech-toolchain.
 1.19 12-Dec-2001  tv MKfoo=no -> NOfoo=
 1.18 23-Jun-2000  thorpej Add MK... variables to enable/disable various aspects of building
crypto support into the system. See share/mk/bsd.README for more
a full description.
 1.17 20-Jun-2000  thorpej branches: 1.17.2;
Merge a bunch of things from crypto-us and crypto-intl into basesrc,
adding support for Heimdal/KTH Kerberos where easy to do so. Eliminate
bsd.crypto.mk.

There is still a bunch more work to do, but crypto is now more-or-less
fully merged into the base NetBSD distribution.
 1.16 20-Jul-1999  mrg branches: 1.16.6;
optionally include CRYPTOPATH Makefile.frag files.
 1.15 12-Jul-1999  thorpej Use bsd.crypto.mk.
 1.14 18-Feb-1999  scottr Remove the crypto-related bits until such time as we have a fully-
integrated source tree. Export-controlled versions of these are now
built during the domestic build process.
 1.13 17-Feb-1999  scottr Check the rlogin source for headers ifdef KERBEROS
 1.12 10-Oct-1997  christos CFLAGS->CPPFLAGS
 1.11 20-Jul-1997  christos Remove WARNS=1 from all the subdirectory Makefiles, and add it to Makefile.inc
now that all /bin has been cleaned.
 1.10 20-Jul-1997  christos Fix compiler warnings
Add WARNS=1
 1.9 27-May-1997  mrg remove the setuid bit.
 1.8 08-May-1997  gwr Back out the .PATH.c changes. The .depend problem (and others)
will be fixed using the new .NOPATH make feature instead.
 1.7 06-May-1997  gwr Use .PATH.c: ...
 1.6 23-Mar-1995  mycroft branches: 1.6.4;
Rearrange Kerberos stuff.
 1.5 21-Mar-1995  cgd actually do the import. rcp appears to have been previously imported
but not merged(?!?!?).
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.4 07-Sep-1995  jtc imported from 44lite2
 1.1.1.3 21-Mar-1995  cgd bin/rcp from Lite
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.6.4.1 17-Feb-1997  mrg this program is not setuid in the new-rcmd-world-order
 1.16.6.1 22-Jun-2000  minoura Sync w/ netbsd-1-5-base.
 1.17.2.2 24-Jun-2000  thorpej Update from trunk:
Add MK... variables to enable/disable various aspects of building
crypto support into the system. See share/mk/bsd.README for more
a full description.
 1.17.2.1 20-Jun-2000  thorpej file Makefile was added on branch netbsd-1-5 on 2000-06-24 06:59:32 +0000
 1.24.28.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.25.48.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.25.48.1 21-Apr-2020  martin Sync with HEAD
 1.6 11-Mar-2005  ginsbach branches: 1.6.92;
Add mechanism for escaping IPv6 address strings, since they already
contain colons. Idea from Solaris rcp(1).
 1.5 17-Feb-2005  xtraeme Kill __P(), use ANSI function declarations.
 1.4 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.3 28-May-1997  mrg oops, forgot to commit these: garbage collect setuid stuff.
 1.2 21-Mar-1995  cgd actually do the import. rcp appears to have been previously imported
but not merged(?!?!?).
 1.1 22-Sep-1994  mycroft branches: 1.1.1;
Initial revision
 1.1.1.1 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.6.92.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.6.92.1 21-Apr-2020  martin Sync with HEAD
 1.7 19-Aug-2004  christos branches: 1.7.92;
Handle RESCUEDIR
 1.6 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.5 21-Mar-1995  cgd actually do the import. rcp appears to have been previously imported
but not merged(?!?!?).
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.7.92.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.7.92.1 21-Apr-2020  martin Sync with HEAD
 1.22 22-Mar-2012  wiz branches: 1.22.34;
Fix whitespace nits. Suggested by Bug Hunting.
 1.21 11-Mar-2005  wiz branches: 1.21.48;
Use more macros (Sq/Dq).
 1.20 11-Mar-2005  ginsbach Add mechanism for escaping IPv6 address strings, since they already
contain colons. Idea from Solaris rcp(1).
 1.19 09-Mar-2005  ginsbach - Support -4 and -6. Approved by christos.
 1.18 05-Sep-2004  wiz Bump date for removal of Kerberos option descriptions.
Remove dots at end of SEE ALSO.
 1.17 05-Sep-2004  manu Kerberos support is broken in rcommands, thus making -K -k and -x options
unavailable. Remove theses options from the man pages (it won't be very
hard to bring them back if we fix kerberos one day)
 1.16 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.15 26-May-2003  lukem * Document $RCMD_CMD in environ(7).
* Cross-reference rcmd(1), rcmd(3), and environ(7) as appropriate.

Should fix [bin/21670] from Geoff Wing.
 1.14 25-Feb-2003  wiz .Nm does not need a dummy argument ("") before punctuation or
for correct formatting of the SYNOPSIS any longer.
 1.13 25-Sep-2002  wiz New policy: New sentences start on a new line.
Patches by Robert Elz <kre at munnari oz au>, with minimal changes by me.
 1.12 20-Dec-2001  wiz Sort SEE ALSO.
 1.11 22-Mar-1999  garbled More and more .Os cleanups. .Os is defined in the tmac.doc-common file,
so we shouldn't override it with versions in the manpages. Many more to
come.
 1.10 26-Nov-1997  tv Add xrefs to new hosts.equiv(5)/rhosts(5) man page.
 1.9 11-Oct-1997  enami branches: 1.9.2;
- Use ``.Nm ""'' instead of ``.Nm rcp'' in SYNOPSIS.
- Use ``.Nm "" .'' instead of ``.Nm rcp .''.
 1.8 10-Oct-1997  lukem all but the first .Nm in SYNOPSIS need to be spelt out. [bin/4256]
 1.7 14-Sep-1997  lukem - cleanup use of .Nm in man page
- getopt returns -1 not EOF
 1.6 25-Jul-1995  jtc Use "utility" instead of "command". Modern definitions of these terms
are distinct (See POSIX.2 glossary).

A utility is a executable, script or shell builtin; while a command
can be any of those things plus lists, pipelines, compound commands
(if, for, while) and shell function definitions.
 1.5 21-Mar-1995  cgd actually do the import. rcp appears to have been previously imported
but not merged(?!?!?).
 1.4 01-Aug-1993  mycroft Add RCS indentifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.9.2.1 27-Nov-1997  mellon Pull rev 1.10 up from trunk (tv)
 1.21.48.1 17-Apr-2012  yamt sync with head
 1.22.34.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.22.34.1 21-Apr-2020  martin Sync with HEAD
 1.53 01-Aug-2023  mrg convert explicit length check before unchecked snprintf() with just a
overflow checked snprintf(). for res_debug.c and res_query.c, convert
from sprintf() to snprintf().

tested scp and rcp fail properly with too-long paths.
tested getaddrinfo fails as expected for too-long domains.
tested dig and ping for similar (res_debug.c/res_query.c).
created a temporary fs with quotas to test edquota with a long EDITOR.
did not test ypserv directly, but it's the same pattern.

avoids GCC 12 snprintf() warnings, and reduces total code size.
 1.52 18-Jul-2022  rin Fix bug revealed by SIGINFO support; Do not treat short read(2)/write(2)
as error (*). This occurs typically when signal is received.

(*) For older version, we already deal with short read(2) from remote
host in sink(). But for other cases, i.e., write(2) to local file in
sink(), read(2)/write(2) in source(), error was raised.

This version of rcp(1) can successfully send/receive files with older
version, even if short read(2)/write(2) occurs by SIGINFO.

Also, when real error occurs, give up immediately instead of continue to
send/receive wrong data.

Clean up the mess a little bit as well...
 1.51 26-Jun-2022  rin Add SIGINFO support. Mostly stolen from scan_ffs(8).
 1.50 06-May-2020  aymeric In sink(), upon error, avoid multiple replies to the source as this
would lead to a desynchronization of the protocol and further files or
directories to be ignored or corrupted.

Reported by Daniel Goujot, Georges-Axel Jaloyan, Ryan Lahfa, and David Naccache.
 1.49 07-May-2012  chs branches: 1.49.12; 1.49.14; 1.49.22; 1.49.26; 1.49.34; 1.49.36;
allocate a separate buffer in each call to sink().
reusing the buffer across recursive calls just doesn't work right.
 1.48 31-Aug-2009  dholland branches: 1.48.6; 1.48.8;
Fix up seriously borked mallocing of a static buffer, which seems to
have been this way since at least 4.4. This will still dump core if
malloc fails on the first trip through, instead of on any malloc
failure, but should otherwise behave much more reasonably.
 1.47 20-Jul-2008  lukem Remove the \n and tabs from the __COPYRIGHT() strings.
 1.46 26-May-2008  christos PR/38755: murray armfield: /bin posix programs missing setprogname and/or
setlocale
 1.45 24-Feb-2008  dholland branches: 1.45.2; 1.45.4;
Print time_t as long long, not long.
 1.44 15-Dec-2006  christos branches: 1.44.4; 1.44.8;
pass lint; from Anon Ymous
 1.43 15-Dec-2006  christos remove two (void)&foo; which are ot needed. From Anon Ymous
 1.42 20-Mar-2006  christos Goodbye KerberosIV
 1.41 18-Mar-2006  ginsbach Fix Coverity CID 1863, 1864; resource leak, bp not freed when !okname()
before function return and/or bp overwritten by malloc().
 1.40 26-Jun-2005  christos sprinkle a little const, and now everything compiles with WARNS=3
 1.39 11-Mar-2005  ginsbach Add mechanism for escaping IPv6 address strings, since they already
contain colons. Idea from Solaris rcp(1).
 1.38 09-Mar-2005  wiz Add -4 and -6 to usage.
 1.37 09-Mar-2005  ginsbach - Support -4 and -6. Approved by christos.
 1.36 17-Feb-2005  xtraeme Kill __P(), use ANSI function declarations.
 1.35 02-Apr-2004  martin Remove duplicate include, fixes PR bin/25018 from Jeff Ito.
 1.34 28-Mar-2004  jdolecek IPv6 support, based on src/usr.bin/rsh/rsh.c rev. 1.13
fixes PR bin/24940 by Manuel Bouyer
 1.33 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.32 23-Jul-2003  itojun include des.h, not kerberosIV/des.h
 1.31 16-Apr-2003  itojun misuse of strncpy. PR 21201 from Christian Biere.
 1.30 22-Nov-2002  ragge It's a very bad habit to store file sizes in int's, so change it to off_t
instead. Obviously noone has tried to rcp files larger than 2GB.
 1.29 25-May-2002  wiz __STDC__ is always defined on NetBSD.
 1.28 24-Sep-2001  wiz branches: 1.28.2;
va_{start,end} audit:
Make sure that each va_start has one and only one matching va_end,
especially in error cases.
If the va_list is used multiple times, do multiple va_starts/va_ends.
If a function gets va_list as argument, don't let it use va_end (since
it's the callers responsibility).

Improved by comments from enami and christos -- thanks!

Heimdal/krb4/KAME changes already fed back, rest to follow.

Inspired by, but not not based on, OpenBSD.
 1.27 04-Jan-2001  lukem use more standard %ll_ in favour of %q_
 1.26 09-Nov-1999  drochner Since our gcc doesn't warn about NULL format strings anymore, we can
fix the incorrect err(1, "%s", "") et al.
Closes PR bin/7592 by cgd.
 1.25 17-Feb-1999  scottr branches: 1.25.6;
Make this compile ifdef KERBEROS.
 1.24 05-Nov-1998  christos s/MODEMASK/RCPMODEMASK/
From Darrin Jewell.
 1.23 04-Nov-1998  christos char -> unsigned char
work around missing futimes.
 1.22 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.21 28-Jul-1998  mycroft Delint.
 1.20 03-Feb-1998  mycroft Deal with GCC warnings.
 1.19 21-Oct-1997  christos PR/4284: Tatoku Ogaito: rcmd(3) get be called with getpwent(3) results as
arguments, leading to memory corruption.
 1.18 19-Oct-1997  mycroft Use futimes(2).
Send microseconds over the wire, rather than just 0.
 1.17 14-Sep-1997  lukem - cleanup use of .Nm in man page
- getopt returns -1 not EOF
 1.16 30-Jul-1997  thorpej Cast some printf arguments to keep the compiler happy on the Alpha.
 1.15 20-Jul-1997  christos Fix compiler warnings
Add WARNS=1
 1.14 07-Jun-1997  jeremy Fix incorrect use of name vs. pwd->pw_name and wrong logic in strdup() test.
[PR/3718]
 1.13 05-Jun-1997  mrg fix PR#3710, reported by Tatoku Ogaito <tacha@trap.fukui-med.ac.jp>.
- don't pass pw->pw_name into functions; make a copy.
 1.12 27-May-1997  mrg use rcmd(); garbage collect junk.
 1.11 26-May-1997  mrg use orcmd() for now.
 1.10 01-Feb-1996  jtc Rename struct timespec fields to conform to POSIX.1b
 1.9 21-Mar-1995  cgd actually do the import. rcp appears to have been previously imported
but not merged(?!?!?).
 1.8 30-Jan-1995  mycroft Use S_IS*().
 1.7 30-Jan-1995  mycroft Use S_IS*().
 1.6 04-Dec-1994  cgd from James Jegers <jimj@miller.cs.uwm.edu>: quiet -Wall, and squelch
some of the worst style errors.
 1.5 27-Mar-1994  cgd more off_t
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.25.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.28.2.1 22-Nov-2002  tron Pull up revision 1.30 (requested by ragge in ticket #1008):
It's a very bad habit to store file sizes in int's, so change it to off_t
instead. Obviously noone has tried to rcp files larger than 2GB.
 1.44.8.1 24-Mar-2008  keiichi sync with head.
 1.44.4.1 23-Mar-2008  matt sync with HEAD
 1.45.4.2 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.45.4.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.45.2.1 04-Jun-2008  yamt sync with head
 1.48.8.1 09-May-2012  riz Pull up following revision(s) (requested by chs in ticket #230):
bin/rcp/rcp.c: revision 1.49
allocate a separate buffer in each call to sink().
reusing the buffer across recursive calls just doesn't work right.
 1.48.6.1 23-May-2012  yamt sync with head.
 1.49.36.1 07-May-2020  martin Pull up following revision(s) (requested by aymeric in ticket #890):

bin/rcp/rcp.c: revision 1.50

In sink(), upon error, avoid multiple replies to the source as this
would lead to a desynchronization of the protocol and further files or
directories to be ignored or corrupted.

Reported by Daniel Goujot, Georges-Axel Jaloyan, Ryan Lahfa, and David Naccache.
 1.49.34.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.49.34.1 21-Apr-2020  martin Sync with HEAD
 1.49.26.1 07-May-2020  martin Pull up following revision(s) (requested by aymeric in ticket #1546):

bin/rcp/rcp.c: revision 1.50

In sink(), upon error, avoid multiple replies to the source as this
would lead to a desynchronization of the protocol and further files or
directories to be ignored or corrupted.

Reported by Daniel Goujot, Georges-Axel Jaloyan, Ryan Lahfa, and David Naccache.
 1.49.22.1 07-May-2020  sborrill Pull up the following revisions(s) (requested by aymeric in ticket #1731):
bin/rcp/rcp.c: revision 1.50

In sink(), upon error, avoid multiple replies to the source as this
would lead to a desynchronization of the protocol and further files or
directories to be ignored or corrupted.
 1.49.14.1 07-May-2020  sborrill Pull up the following revisions(s) (requested by aymeric in ticket #1731):
bin/rcp/rcp.c: revision 1.50

In sink(), upon error, avoid multiple replies to the source as this
would lead to a desynchronization of the protocol and further files or
directories to be ignored or corrupted.
 1.49.12.1 07-May-2020  sborrill Pull up the following revisions(s) (requested by aymeric in ticket #1731):
bin/rcp/rcp.c: revision 1.50

In sink(), upon error, avoid multiple replies to the source as this
would lead to a desynchronization of the protocol and further files or
directories to be ignored or corrupted.
 1.11 15-Dec-2006  christos branches: 1.11.78;
pass lint; from Anon Ymous
 1.10 15-Dec-2006  christos fix an unused variable warning. From Anon Ymous
 1.9 11-Mar-2005  ginsbach Add mechanism for escaping IPv6 address strings, since they already
contain colons. Idea from Solaris rcp(1).
 1.8 17-Feb-2005  xtraeme Kill __P(), use ANSI function declarations.
 1.7 19-Sep-2003  itojun realloc pedant
 1.6 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.5 28-Jul-1998  mycroft Delint.
 1.4 20-Jul-1997  christos Fix compiler warnings
Add WARNS=1
 1.3 28-May-1997  mrg oops, forgot to commit these: garbage collect setuid stuff.
 1.2 21-Mar-1995  cgd actually do the import. rcp appears to have been previously imported
but not merged(?!?!?).
 1.1 22-Sep-1994  mycroft branches: 1.1.1;
Initial revision
 1.1.1.1 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.11.78.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.11.78.1 21-Apr-2020  martin Sync with HEAD
 1.9 20-Jul-1997  christos branches: 1.9.112;
Remove WARNS=1 from all the subdirectory Makefiles, and add it to Makefile.inc
now that all /bin has been cleaned.
 1.8 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.7 21-Mar-1995  cgd convert to new RCS id conventions.
 1.6 20-Sep-1994  mycroft Merge with 4.4-Lite version.
 1.5 28-Jan-1994  cgd don't need -lutil
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 19-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.9.112.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.9.112.1 21-Apr-2020  martin Sync with HEAD
 1.29 03-Jul-2017  wiz branches: 1.29.6;
Remove workaround for ancient HTML generation code.
 1.28 12-Aug-2016  sevan Document the version rm first appeared.
Bump date.
 1.27 26-Apr-2013  wiz Sort sections.
 1.26 26-Apr-2013  christos PR/47770: Eitan Adler: add x flag to rm to avoid cross mount points
 1.25 13-Oct-2012  njoly Remove a few unneeded Pp macros.
 1.24 02-Sep-2006  wiz branches: 1.24.40; 1.24.46;
New sentence, new line. Bump date for previous.
 1.23 25-Aug-2006  liamjfoy Document new -P behaviour
 1.22 24-Mar-2004  wiz Sort sections, bump date for previous, and add a comma after e.g..
 1.21 24-Mar-2004  fair Add an EXAMPLES section to answer PR 22255
 1.20 11-Jan-2004  wiz Bump date for previous; |fmt; remove trailing white space.
New sentence, new line.
 1.19 11-Jan-2004  tls UFS->FFS
 1.18 11-Jan-2004  tls Change behaviour of -P option to conform generally to DoD 5220.22-M
standard. This change inspired by Apple's "Secure Empty Trash" functionality
in MacOS 10.3. However, it is important to understand that this change
does not -- and can not -- actually achieve conformance to the current
revision of the standard. To quote the manual page:

The -P option attempts to conform to U.S. DoD 5220-22.M, "National Indus-
trial Security Program Operating Manual" ("NISPOM") as updated by Change
2 and the July 23, 2003 "Clearing & Sanitization Matrix". However,
unlike earlier revisions of NISPOM, the 2003 matrix imposes requirements
which make it clear that the standard does not and can not apply to the
erasure of individual files, in particular requirements relating to spare
sector management for an entire magnetic disk. Because these
requirements are not met, the -P option does not conform to the standard.

This also makes the -P option a *lot* more expensive than it used to be.
It used to overwrite with 0xff, overwrite with 0x00, overwrite with 0xff,
with an fsync after each write. Now it overwrites with a random character,
overwrites with 0xff, overwrites with 0x00, reads to validate the 0x00
overwrite, then overwrites with random data -- calling sync() after every
operation in an attempt to force seeks that will clear the data from the
cache of disks that lie about whether data has been committed to the
platters. Also, the file's opened with O_SYNC|O_RSYNC to cause metadata
updates on every read/write, which should cause still more seeks.

This is better than it used to be, but it's by no means adequate if you
have data you really don't want read by an adversary who can pull the
disk apart.
 1.17 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.16 12-Feb-2003  wiz New sentence, new line. I'm having a slight deja-vu...
 1.15 12-Feb-2003  jrf Added the -v flag.
 1.14 02-May-2002  enami A default .Ar arugment is sufficient.
 1.13 08-Feb-2002  ross Generate <>& symbolically. I'm avoiding .../dist/... directories for now.
 1.12 20-Dec-2001  wiz Whitespace nits.
 1.11 04-Sep-2000  kleink For commands and utilities, use EXIT STATUS rather than RETURN VALUES as
appropriate (and documented in mdoc(7)).
 1.10 28-Aug-2000  hubertf Add 'RETURN VALUE' section header.
 1.9 20-Oct-1997  enami branches: 1.9.12;
Fix .Nm usage.
 1.8 25-Jul-1995  jtc Use "utility" instead of "command". Modern definitions of these terms
are distinct (See POSIX.2 glossary).

A utility is a executable, script or shell builtin; while a command
can be any of those things plus lists, pipelines, compound commands
(if, for, while) and shell function definitions.
 1.7 21-Mar-1995  cgd convert to new RCS id conventions.
 1.6 28-Dec-1994  mycroft Mostly sync with CSRG.
 1.5 20-Sep-1994  mycroft Merge with 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS indentifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.3 07-Sep-1995  jtc imported from 44lite2
 1.1.1.2 19-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.9.12.1 28-Aug-2000  hubertf Pull up to netbsd-1-5 branch, OK'd by thorpej:

Log Message:
> Add 'RETURN VALUE' section header.

Files & Revisionis:
> cvs rdiff -r1.19 -r1.20 basesrc/bin/cat/cat.1
> cvs rdiff -r1.12 -r1.13 basesrc/bin/chmod/chmod.1
> cvs rdiff -r1.14 -r1.15 basesrc/bin/cp/cp.1
> cvs rdiff -r1.8 -r1.9 basesrc/bin/dd/dd.1
> cvs rdiff -r1.9 -r1.10 basesrc/bin/echo/echo.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/expr/expr.1
> cvs rdiff -r1.25 -r1.26 basesrc/bin/ls/ls.1
> cvs rdiff -r1.10 -r1.11 basesrc/bin/mkdir/mkdir.1
> cvs rdiff -r1.23 -r1.24 basesrc/bin/mt/mt.1
> cvs rdiff -r1.12 -r1.13 basesrc/bin/mv/mv.1
> cvs rdiff -r1.16 -r1.17 basesrc/bin/pwd/pwd.1
> cvs rdiff -r1.9 -r1.10 basesrc/bin/rm/rm.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/rmdir/rmdir.1
> cvs rdiff -r1.35 -r1.36 basesrc/bin/sh/sh.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/sleep/sleep.1
> cvs rdiff -r1.20 -r1.21 basesrc/bin/stty/stty.1
 1.24.46.2 23-Jun-2013  tls resync from head
 1.24.46.1 20-Nov-2012  tls Resync to 2012-11-19 00:00:00 UTC
 1.24.40.2 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.24.40.1 30-Oct-2012  yamt sync with head
 1.29.6.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.29.6.1 21-Apr-2020  martin Sync with HEAD
 1.55 12-May-2025  kim Remove unnecessary initialization to zero of static int option flags

This results in uniform handling of all of them, instead of some being
explicitly initialized and some not. Reduces confusion in the reader.
 1.54 10-Sep-2021  rillig branches: 1.54.4;
bin: remove unnecessary lint comment CONSTCOND

Since 2021-01-31, lint no longer warns about 'do ... while (0)'.

No functional change.
 1.53 26-Apr-2013  christos branches: 1.53.30;
PR/47770: Eitan Adler: add x flag to rm to avoid cross mount points
 1.52 13-Jun-2012  dholland branches: 1.52.2;
Rectify race condition in rm -P processing by checking that the file
we opened is the one we expected to get. Also use O_NOFOLLOW to help
avoid even opening devices, which sometimes produce side effects.

Reported by Radoslaw A. Zarzynski.
 1.51 21-Jan-2012  christos branches: 1.51.2;
Siginfo support from Daniel Loffgren
 1.50 29-Aug-2011  joerg branches: 1.50.2;
static + __dead
 1.49 14-Feb-2009  lukem fix -Wsign-compare issues
 1.48 20-Jul-2008  lukem branches: 1.48.4; 1.48.6;
Remove the \n and tabs from the __COPYRIGHT() strings.
 1.47 26-May-2008  christos PR/38754: murray armfield: Rm prints diagnostic messages when invoked with -f
and no other arguments, contrary to POSIX. Also cosmetic fixes.
 1.46 24-Jun-2007  christos branches: 1.46.10; 1.46.12;
PR/36535: Greg A. Woods: rm(1) should present user:group the same way as
chown(8) et al
 1.45 25-Aug-2006  liamjfoy branches: 1.45.2;
Only remove the file if the file can be successfully
overwritten. Old code would remove the file regardless of
whether the overwrite was successful or not.

New behaviour:

carpy: {12} touch moo ; chmod 444 moo
carpy: {13} ls
moo
carpy: {14} rm -P moo
rm: moo: -P was specified but file could not be overwritten
carpy: {15} ls
moo
carpy: {16}

ok: christos@, joerg@ & tech-userlevel@
 1.44 14-Aug-2006  tls Fix sentence fragment that somehow got into the long comment about rm -P.
 1.43 17-Mar-2006  peter Call fts_close() at the end of rm_tree().

Fixes Coverity CID 1670.
 1.42 11-Jan-2004  wiz Fix typo.
 1.41 11-Jan-2004  tls Adjust comment to indicate what this code is actually useful for, and that
it's not really "cheap".
 1.40 11-Jan-2004  tls Change behaviour of -P option to conform generally to DoD 5220.22-M
standard. This change inspired by Apple's "Secure Empty Trash" functionality
in MacOS 10.3. However, it is important to understand that this change
does not -- and can not -- actually achieve conformance to the current
revision of the standard. To quote the manual page:

The -P option attempts to conform to U.S. DoD 5220-22.M, "National Indus-
trial Security Program Operating Manual" ("NISPOM") as updated by Change
2 and the July 23, 2003 "Clearing & Sanitization Matrix". However,
unlike earlier revisions of NISPOM, the 2003 matrix imposes requirements
which make it clear that the standard does not and can not apply to the
erasure of individual files, in particular requirements relating to spare
sector management for an entire magnetic disk. Because these
requirements are not met, the -P option does not conform to the standard.

This also makes the -P option a *lot* more expensive than it used to be.
It used to overwrite with 0xff, overwrite with 0x00, overwrite with 0xff,
with an fsync after each write. Now it overwrites with a random character,
overwrites with 0xff, overwrites with 0x00, reads to validate the 0x00
overwrite, then overwrites with random data -- calling sync() after every
operation in an attempt to force seeks that will clear the data from the
cache of disks that lie about whether data has been committed to the
platters. Also, the file's opened with O_SYNC|O_RSYNC to cause metadata
updates on every read/write, which should cause still more seeks.

This is better than it used to be, but it's by no means adequate if you
have data you really don't want read by an adversary who can pull the
disk apart.
 1.39 04-Jan-2004  jschauma remove unused code left from printescaped() backput as pointed out by Jeff
Ito in PR bin/23969 and PR bin/23970.
 1.38 14-Sep-2003  jschauma Following private discussion with kleink@ and hubertf@ and public discussion
on tech-userlevel@, back out printescaped() functionality.
kleink: ``We sell rope.''
 1.37 13-Aug-2003  itojun use bounded string op
 1.36 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.35 04-Aug-2003  jschauma As discusses a little while back on tech-userlevel:

If stdout is a tty, use vis(3) to print any filenames to prevent garbage
from being printed if the filename contains control- or other non-printable
characters.

While here, sprinkle some EXIT_FAILURE and NOTREACHED where appropriate.
 1.34 01-Mar-2003  enami Remove space at the end of line.
 1.33 13-Feb-2003  jrf Note: Original change approved by Enami.
Accidentally left eval = 1 when I was adding the extension which caused the rm command to return a 1 when it successfully removed a directory. Thanks to tron for catching it.
 1.32 12-Feb-2003  jrf Added the -v flag.
 1.31 05-Nov-2002  enami - Remove the last __P.
- Don't put space between function name and () op.
 1.30 02-May-2002  enami - Use four space to indent continuation line.
- Put a space after the C language keyword ``switch''.
- Put an empty line if a function doesn't have local variable.
- Use do { } while (/*CONSTCOND*/ 0) instead of { } to protect a multi
statement macro
 1.29 20-Dec-2001  soren Sync getopt() / man page with actual getopt options.
 1.28 22-Nov-2001  jmc Patch from mycroft to fix case of rm -rf nonexistant on a r/o fs would still
report errors. Checking the fts_errno for ENOENT before attempting removal
fixes this.
 1.27 16-Sep-2001  wiz ANSIfy, KNF, [gs]etprogname, by Petri Koistinen.
 1.26 09-Nov-1999  drochner branches: 1.26.4;
Since our gcc doesn't warn about NULL format strings anymore, we can
fix the incorrect err(1, "%s", "") et al.
Closes PR bin/7592 by cgd.
 1.25 30-Jun-1999  is branches: 1.25.4;
Don't ask for permission to unlink a file if the access error is ETXTBSY.
This needs vfs_syscalls.c 1.138 to really work.
Fixes pr 4134 by Johan Danielsson.
 1.24 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.23 28-Jul-1998  mycroft Delint.
 1.22 28-Jul-1998  mycroft Delint.
 1.21 21-Dec-1997  kleink Per 1003.2, no warnings shall be generated for attempts to remove non-existent
files in case the `-f' option has been specified:
Extend the definition of `non-existent' to include ENAMETOOLONG and ENOTDIR
cases, since file names failing with these errors can safely assumed to be
non-existent. Fixes PR bin/2993.
 1.20 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.19 07-Sep-1995  jtc Sync with 4.4lite2
 1.18 21-Mar-1995  cgd convert to new RCS id conventions.
 1.17 28-Dec-1994  mycroft Mostly sync with CSRG.
 1.16 02-Nov-1994  jtc Strip trailing slashes of operands in checkdot().

POSIX.2 requires that if "." or ".." are specified as the basename
portion of an operand, a diagnostic message be written to standard
error, etc. We strip the slashes because POSIX.2 defines basename
as the final portion of a pathname after trailing slashes have been
removed.

This also makes rm "perform actions equivalent to" the POSIX.1
rmdir() and unlink() functions when removing directories and files,
even when they do not follow POSIX.1's pathname resolution semantics
(which require trailing slashes be ignored).
 1.15 20-Sep-1994  mycroft Merge with 4.4-Lite version.
 1.14 16-Mar-1994  jtc Fix bug where two messages were printed when a directory could not
be removed due to some error.
 1.13 28-Jan-1994  jtc The -f flag suppresses error messages and changes to the exit value if
a file specified on the command line does not exist. This is the _only_
case where error messages/exit value are effected by the -f flag.
 1.12 29-Nov-1993  jtc Change order of conditional so that a possible null pointer is not
dereferenced. This case would only occur when stdin was not a tty.
Thanks to Dan Muntz for discovering this bug.
 1.11 16-Nov-1993  jtc Some minor fixes:
If all the arguments have a "." or ".." basename, the exit value should be
modified whether or not the -f flag was specified.
Don't exit if a file can not be read or there is another error (FTS_DNR or
FTS_ERR), there are probably other files that we can process successfully.
 1.10 16-Nov-1993  jtc Check user's response against both 'Y' and 'y' --- this is really supposed
to be a locale specific regular expression. This change hard codes POSIX
locale behavior, and will be replaced by a locale independant equivalent
as soon as locales are fully implemented.
 1.9 25-Oct-1993  jtc Don't ask about each directory twice when using -r and -i flags.
Exit value is not always zero when using -f flag, only if all files were
removed or non-existant.
 1.8 25-Oct-1993  jtc Changed conditional from "iflag && check(...)" to "!fflag && check(...)".
Removed the !fflag from conditionals in check() ... the check function
is never executed if !fflag.
 1.7 25-Oct-1993  jtc Changed so that rm without -r or -R is 1003.2 compliant --- I haven't
had time to look at the recursive code, but it probably has the same
types of problems.
Added code to set the default locale, so it will work correctly when
our locale code is more than just stubs.
Added prototypes, etc. to make gcc -Wall happier.
 1.6 06-Aug-1993  deraadt change for new fts library
 1.5 01-Aug-1993  mycroft Add RCS identifiers.
 1.4 10-Apr-1993  mycroft Be silent if -f specified.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.3 07-Sep-1995  jtc imported from 44lite2
 1.1.1.2 19-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.25.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.26.4.1 06-Mar-2002  he Pull up revision 1.28 (requested by lukem):
Fix a problem where ``rm -rf foo'' exits with a non-zero exit status
on a read-only file system even if foo doesn't exist.
 1.45.2.1 15-Jun-2012  sborrill Pull up the following revisions(s) (requested by dholland in ticket #1451):
bin/rm/rm.c: revision 1.52

Rectify race condition in rm -P processing by checking that the file
we opened is the one we expected to get. Also use O_NOFOLLOW to help
avoid even opening devices, which sometimes produce side effects.
 1.46.12.2 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.46.12.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.46.10.1 04-Jun-2008  yamt sync with head
 1.48.6.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.48.4.1 15-Jun-2012  sborrill Pull up the following revisions(s) (requested by dholland in ticket #1775):
bin/rm/rm.c: revision 1.52

Rectify race condition in rm -P processing by checking that the file
we opened is the one we expected to get. Also use O_NOFOLLOW to help
avoid even opening devices, which sometimes produce side effects.
 1.50.2.3 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.50.2.2 30-Oct-2012  yamt sync with head
 1.50.2.1 17-Apr-2012  yamt sync with head
 1.51.2.1 15-Jun-2012  sborrill Pull up the following revisions(s) (requested by dholland in ticket #346):
bin/rm/rm.c: revision 1.52

Rectify race condition in rm -P processing by checking that the file
we opened is the one we expected to get. Also use O_NOFOLLOW to help
avoid even opening devices, which sometimes produce side effects.
 1.52.2.1 23-Jun-2013  tls resync from head
 1.53.30.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.53.30.1 21-Apr-2020  martin Sync with HEAD
 1.54.4.1 02-Aug-2025  perseant Sync with HEAD
 1.8 20-Jul-1997  christos branches: 1.8.112;
Remove WARNS=1 from all the subdirectory Makefiles, and add it to Makefile.inc
now that all /bin has been cleaned.
 1.7 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.8.112.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.8.112.1 21-Apr-2020  martin Sync with HEAD
 1.18 12-May-2025  kim Add -v option to rmdir(1)
 1.17 03-Jul-2017  wiz branches: 1.17.6; 1.17.16; 1.17.18;
Remove workaround for ancient HTML generation code.
 1.16 12-Aug-2016  sevan Document the version rmdir first appeared.
Bump date.
 1.15 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.14 08-Feb-2002  ross Generate <>& symbolically. I'm avoiding .../dist/... directories for now.
 1.13 04-Sep-2000  kleink For commands and utilities, use EXIT STATUS rather than RETURN VALUES as
appropriate (and documented in mdoc(7)).
 1.12 28-Aug-2000  hubertf Add 'RETURN VALUE' section header.
 1.11 20-Oct-1997  enami branches: 1.11.12;
Fix .Nm usage.
 1.10 25-Jul-1995  jtc Use "utility" instead of "command". Modern definitions of these terms
are distinct (See POSIX.2 glossary).

A utility is a executable, script or shell builtin; while a command
can be any of those things plus lists, pipelines, compound commands
(if, for, while) and shell function definitions.
 1.9 21-Mar-1995  cgd convert to new RCS id conventions.
 1.8 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.7 10-Sep-1993  jtc Note that rmdir(1) is POSIX.2 compliant.
Include appropriate header files to bring prototypes into scope.
Use new error/warning display functions.
 1.6 01-Aug-1993  mycroft Add RCS indentifiers.
 1.5 22-Jul-1993  jtc Restore RCS Header.
 1.4 22-Jul-1993  jtc Make POSIX 1003.2 (D11.2) compliant.
Update manpage to use new -mandoc macros.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.11.12.1 28-Aug-2000  hubertf Pull up to netbsd-1-5 branch, OK'd by thorpej:

Log Message:
> Add 'RETURN VALUE' section header.

Files & Revisionis:
> cvs rdiff -r1.19 -r1.20 basesrc/bin/cat/cat.1
> cvs rdiff -r1.12 -r1.13 basesrc/bin/chmod/chmod.1
> cvs rdiff -r1.14 -r1.15 basesrc/bin/cp/cp.1
> cvs rdiff -r1.8 -r1.9 basesrc/bin/dd/dd.1
> cvs rdiff -r1.9 -r1.10 basesrc/bin/echo/echo.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/expr/expr.1
> cvs rdiff -r1.25 -r1.26 basesrc/bin/ls/ls.1
> cvs rdiff -r1.10 -r1.11 basesrc/bin/mkdir/mkdir.1
> cvs rdiff -r1.23 -r1.24 basesrc/bin/mt/mt.1
> cvs rdiff -r1.12 -r1.13 basesrc/bin/mv/mv.1
> cvs rdiff -r1.16 -r1.17 basesrc/bin/pwd/pwd.1
> cvs rdiff -r1.9 -r1.10 basesrc/bin/rm/rm.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/rmdir/rmdir.1
> cvs rdiff -r1.35 -r1.36 basesrc/bin/sh/sh.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/sleep/sleep.1
> cvs rdiff -r1.20 -r1.21 basesrc/bin/stty/stty.1
 1.17.18.1 02-Aug-2025  perseant Sync with HEAD
 1.17.16.1 15-May-2025  martin Pull up following revision(s) (requested by kim in ticket #1117):

bin/rmdir/rmdir.c: revision 1.28
bin/rmdir/rmdir.1: revision 1.18

Add -v option to rmdir(1)
 1.17.6.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.17.6.1 21-Apr-2020  martin Sync with HEAD
 1.28 12-May-2025  kim Add -v option to rmdir(1)
 1.27 10-Aug-2017  ginsbach branches: 1.27.4; 1.27.14; 1.27.16;
PR/48182: Fix rmdir -p handling of top-level (root) directory.
XXX: pullup-8
XXX: pullup-7
XXX: pullup-6
 1.26 29-Aug-2011  joerg branches: 1.26.4; 1.26.22; 1.26.36;
static + __dead
 1.25 20-Jul-2008  lukem Remove the \n and tabs from the __COPYRIGHT() strings.
 1.24 26-May-2008  christos PR/38755: murray armfield: /bin posix programs missing setprogname and/or
setlocale
 1.23 29-Sep-2003  dsl branches: 1.23.30; 1.23.32;
Fix rmdir -p x/y/z//
Remove redundant code.
 1.22 14-Sep-2003  jschauma Following private discussion with kleink@ and hubertf@ and public discussion
on tech-userlevel@, back out printescaped() functionality.
kleink: ``We sell rope.''
 1.21 11-Sep-2003  christos Comment out unneeded code now that the kernel has been fixed to ignore
trailing slashes.
 1.20 13-Aug-2003  itojun use bounded string op
 1.19 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.18 04-Aug-2003  jschauma As discusses a little while back on tech-userlevel:

If stdout is a tty, use vis(3) to print any filenames to prevent garbage
from being printed if the filename contains control- or other non-printable
characters.

While here, sprinkle some EXIT_FAILURE and NOTREACHED where appropriate.
 1.17 16-Sep-2001  wiz ANSIfy, KNF, [gs]etprogname. By Petri Koistinen.
 1.16 28-Jul-1998  mycroft Delint.
 1.15 05-Nov-1997  cgd lint
 1.14 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.13 21-Mar-1995  cgd convert to new RCS id conventions.
 1.12 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.11 28-Apr-1994  jtc Skip trailing slashes, so "rmdir foo/" works even on systems without
a POSIX.1 compliant rmdir(2) syscall.
 1.10 31-Dec-1993  jtc localization.
 1.9 10-Sep-1993  jtc Fixed bug caused by premature checkin.
 1.8 10-Sep-1993  jtc Simplified (by removing unn
 1.7 10-Sep-1993  jtc Note that rmdir(1) is POSIX.2 compliant.
Include appropriate header files to bring prototypes into scope.
Use new error/warning display functions.
 1.6 01-Aug-1993  mycroft Add RCS identifiers.
 1.5 22-Jul-1993  jtc Restore RCS Header.
 1.4 22-Jul-1993  jtc Make POSIX 1003.2 (D11.2) compliant.
Update manpage to use new -mandoc macros.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.23.32.2 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.23.32.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.23.30.1 04-Jun-2008  yamt sync with head
 1.26.36.1 14-Aug-2017  snj Pull up following revision(s) (requested by ginsbach in ticket #208):
bin/rmdir/rmdir.c: revision 1.27
PR/48182: Fix rmdir -p handling of top-level (root) directory.
 1.26.22.1 19-Aug-2017  snj Pull up following revision(s) (requested by ginsbach in ticket #1488):
bin/rmdir/rmdir.c: revision 1.27
PR/48182: Fix rmdir -p handling of top-level (root) directory.
 1.26.4.1 19-Aug-2017  snj Pull up following revision(s) (requested by ginsbach in ticket #1490):
bin/rmdir/rmdir.c: revision 1.27
PR/48182: Fix rmdir -p handling of top-level (root) directory.
 1.27.16.1 02-Aug-2025  perseant Sync with HEAD
 1.27.14.1 15-May-2025  martin Pull up following revision(s) (requested by kim in ticket #1117):

bin/rmdir/rmdir.c: revision 1.28
bin/rmdir/rmdir.1: revision 1.18

Add -v option to rmdir(1)
 1.27.4.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.27.4.1 21-Apr-2020  martin Sync with HEAD
 1.1 16-May-2020  christos Add ACL support for FFS. From FreeBSD.
 1.1 16-May-2020  christos Add ACL support for FFS. From FreeBSD.
 1.1 16-May-2020  christos Add ACL support for FFS. From FreeBSD.
 1.1 16-May-2020  christos Add ACL support for FFS. From FreeBSD.
 1.1 16-May-2020  christos Add ACL support for FFS. From FreeBSD.
 1.2 18-Jun-2020  wiz Sort option descriptions. Sort options in SYNOPSIS. Fix xrefs.
Comment out acl(9) which we don't have.
 1.1 16-May-2020  christos Add ACL support for FFS. From FreeBSD.
 1.3 18-Jun-2020  wiz Sync usage with manpage.
 1.2 22-May-2020  joerg Mark usage as dead
 1.1 16-May-2020  christos Add ACL support for FFS. From FreeBSD.
 1.1 16-May-2020  christos Add ACL support for FFS. From FreeBSD.
 1.1 16-May-2020  christos Add ACL support for FFS. From FreeBSD.
 1.124 14-Oct-2024  kre Reject nul characters in shell input.

At the request of Thomas Klausner (wiz@) copy an idea from OpenBSD,
and have the shell simply reject any (sh) input containing a \0 (nul)
character. Previously nul characters were simply ignored (removed
from the input before it was examined in any other way).

Note this affects data read by the shell itself only, and has no
impact on other utilities, including those that are built into
the shell (so 'read' can still use -d '' to process output using
\0 as the record separator).

While I have tested that this works, there are so many places
where a nul might appear, that I cannot possibly test them all
(or even imagine all the possible places), so if this change
causes any problems, let me know (if from a script, send me the
script).

To undo this change, simply comment out (or delete) the line
CPPFLAGS+= -DREJECT_NULS
from the Makefile, and build again. Eventually if this causes
no problems, that option (conditional compilation) will probably
just go away, and this change would be permanent.

While the conditional compilation (on or off) persists, the
NETBSD_SHELL variable value will contain the word REJECT_NULLS
so you can easily verify if you are running a shell with this
included or not.

No pullups planned.
 1.123 19-Oct-2023  mrg branches: 1.123.2;
convert gcc12 -O1 into -Wno-error=clobbered.

parser.c wants all the optimisation, and this is very likely a
false positive.
 1.122 14-Oct-2023  mrg the parser.c longjmp vs gcc12 issue affects a few ports,
make the workaround global.
 1.121 14-Aug-2023  mrg use -O1 on sh3, GCC 12 and parser.c.

this triggers clobbered vs. longjmp/setjmp warnings with -Os that sh3 uses.
 1.120 10-Oct-2021  rillig sh: remove no-op 'continue' from do-while-0 loop

With Clang, the only change to the binary are the line number changes
from __LINE__, GCC generates a bit different code.

No functional change.
 1.119 09-Oct-2021  rillig bin: in builds with MKLINT=yes, run lint on all programs
 1.118 09-Oct-2021  rillig sh: ignore lint error about 'continue' in 'do while' loop

exec.c(575): error: continue in 'do ... while (0)' loop [323]
jobs.c(203): error: continue in 'do ... while (0)' loop [323]

It is certainly a rarely used feature, I saw it the first time today and
had to look up its meaning in the C standard. But after that, I don't
see why a 'continue' statement in a 'do while' loop should be an error.
Maybe a warning since up to now I thought that 'continue' would jump
back to the top of the loop, while it really jumps to the bottom of the
loop body, for all 3 kinds of loops.
 1.117 15-Aug-2021  christos Add -I to find filecomplete.h
 1.116 26-May-2021  christos Use the date tool
 1.115 28-Oct-2018  kre branches: 1.115.6;

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!)
 1.114 10-Jun-2018  christos branches: 1.114.2;
use SUBDIR.roff suggested by uwe@
 1.113 30-Jun-2017  kre branches: 1.113.4;

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.))
 1.112 08-Jun-2017  kre I am an idiot... revert the previous unintended commit.
 1.111 08-Jun-2017  kre Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
 1.110 29-May-2017  kre branches: 1.110.2;

More DEBUG mode changes. As usual, read the source if you care.
 1.109 28-May-2017  kre Redo mkoptions.sh .. much better this way, now fully automated
option sorting (no longer required option.list to be manually
sorted by long option name) and properly handles conditional
options. Cleaner output format as well.

This allows option.list to be reordered to group related options
together ... also added more comments to it.
 1.108 28-May-2017  kre Arrange for set -o and $- output to be sorted, rather than more
or less random (and becoming worse as more options are added.)
Since the data is known at compile time, sort at compile time,
rather than at run time.
 1.107 15-May-2017  kre Drop the lex library - hasn't been needed since the airthmetic
upgrade a while ago (this should make no difference to anything
other than a minor - very minor - build time speedup, ld is
smart enough to relaise that nothing from the lex library was
needed, and the executable contains no reference to it, even
befor ethis change.)
 1.106 14-May-2017  kre Fix a minor omission in last...
 1.105 14-May-2017  kre Make ${NETBSD_SHELL} value include (a human recognisable form of)
MKREPRO_TIMESTAMP (as an additional word in the value, with a "BUILD:" prefix)
if it is set during the build. (Trailing 00 pairs in the time are removed).

While here, throw in some extra words that list the compilation
options used which alter sh behaviour (mostly by removing stuff.)
Usually that will only be noticed in a SMALL shell compiled for
install media, or similar - none of the others (not that there
are many) are ever changed from the default in a normal build
(default settings are just omitted.) This also allows scripts
to tell if they are running in a DEBUG shell, which can sometimes
make debugging easier.
 1.104 20-Mar-2017  kre branches: 1.104.2;


Finish support for all required $(( )) (shell arithmetic) operators,
closing PR bin/50958

That meant adding the assignment operators ('=', and all of the +=, *= ...)
Currently, ++, --, and ',' are not implemented (none of those are required
by posix) but support for them (most likely ',' first) might be added later.

To do this, I removed the yacc/lex arithmetic parser completely, and
replaced it with a hand written recursive descent parser, that I obtained
from FreeBSD, who earlier had obtained it from dash (Herbert Xu).

While doing the import, I cleaned up the sources (changed some file names
to avoid requiring a clean build, or signifigant surgery to the obj
directories if "build.sh -u" was to be used - "build.sh -u" should work
fine as it is now) removed some dashisms, applied some KNF, ...
 1.103 16-Mar-2017  kre Have "make clean" remove sh.html1 and adapt it to clean trace files
the way they have been generated the past 20 years or so...
 1.102 27-Feb-2016  christos branches: 1.102.2; 1.102.4;
Improve debugging, from kre (I hooked it to the build).
 1.101 10-May-2015  joerg Include printf by default even for SMALL builds. It is used e.g. by
dhcpcd and as such required by most ramdisk images. Allow turning it off
again by TINYPROG.
 1.100 05-Jul-2014  dholland branches: 1.100.2;
remove .if make(install)
 1.99 02-Dec-2012  apb branches: 1.99.8;
Adjust everything under src (but outside src/tools) to use
the TOOLDIR version of libnbcompat, associated include files,
and associated defs.mk file, instead of the version from the
.OBJDIR of src/tools/compat. This should fix PR 47188.
 1.98 23-Aug-2011  christos branches: 1.98.2; 1.98.8;
document another non-literal format string
 1.97 14-Aug-2011  christos Document non-literal formats
 1.96 12-Jul-2011  joerg Move the savehandler assignment before setjmp() to avoid triggering
warnings about use before initialization with clang.
 1.95 26-May-2011  joerg Default to -Wno-sign-compare -Wno-pointer-sign for clang.
Push -Wno-array-bounds down to the cases that depend on it.
Selectively disable warnings for 3rd party software or non-trivial
issues to be reviewed later to get clang -Werror to build most of the
tree.
 1.94 30-Aug-2010  christos increase commented out debugging.
 1.93 22-Aug-2010  perry add bsd.subdir.mk
 1.92 22-Aug-2010  perry build the tutoral
 1.91 06-Feb-2010  he When using -lcurses, you also need -lterminfo.
This fixes the build for sun2, and also builds with LDSTATIC=-static,
since archive libraries don't record inter-library dependencies.
 1.90 03-Feb-2010  roy Userland now builds and uses terminfo instead of termcap.

OK: core@, jdc@
 1.89 14-Dec-2009  christos use .ORDER for rules that create multiple files.
 1.88 11-Dec-2009  uebayasi Rewrite file generation rules using ${GENCMD}. No functional changes intended.
 1.87 07-Dec-2009  uebayasi Define dependencies of arith.[ch] on arith.y.
 1.86 29-Oct-2009  christos use flex options instead of #defines
 1.85 20-Apr-2009  drochner define YY_NO_INPUT where appropriate, from Kurt J. Lidl per PR misc/41160
 1.84 14-Feb-2009  lukem Default to WARNS=4 (except for rcp & ksh)
 1.83 25-Oct-2008  apb branches: 1.83.4;
Pass SED=${TOOL_SED:Q} in the environment to scripts run during the build.
 1.82 19-Oct-2008  apb Use ${TOOL_AWK} instead of ${AWK} or plain "awk" in make commands.
Pass AWK=${TOOL_AWK:Q} to shell scripts that use awk.
 1.81 25-Mar-2007  apb WARNS=4
 1.80 26-Jun-2005  christos sprinkle a little const, and now everything compiles with WARNS=3
 1.79 30-Oct-2004  christos Pass WARNS=3
 1.78 06-Jun-2004  christos don't include the printf builtin if we are SMALL; saves 10K.
 1.77 17-Jan-2004  dsl Put a syntax.c under CVS instead of building if with the mksyntax program.
Kill mksyntax.c - no longer possible to get the 'wrong sort of chars'.
/bin/sh now has no helper binaries.
syntax.c uses C99 initialisers, run time initialisation could be used
for systems where the compiler doesn't support them.
I've used some #defines to help make this possible - but writing the code
starts making it rather messy.
 1.76 17-Jan-2004  dsl Put syntax.h under CVS instead of having it generated by mksyntax.
Use CHAR_MIN (from limits.h) to determine whether target char are signed
or unsigned - the syntax tables will not be indexed properly.
Rip out all the stuff from mksyntax.c that wrote syntax.h.
syntax.c can stiff be generated incorrectly...
 1.75 17-Jan-2004  dsl Replace mkinit.c with mkinit.sh
Build mksyntax directly from mksyntax.c so that the -DTARGET_CHAR=xxx
is applied when it is build.
OTOH mksyntax is broken as it tries to determine properties of the
target system by running code on the build system.
 1.74 16-Jan-2004  dsl Replace the C program mknodes.c with a shell script mknodes.sh
(mkinit and mksyntax may also die soon...)
 1.73 16-Nov-2003  lukem Improve how various "simple" host tools are built and invoked.
 1.72 21-Oct-2003  lukem Rework how MAKEVERBOSE operates:

* Don't bother prefixing commands with a line of ${_MKCMD}\
and instead rely upon "make -s". This is less intrusive on
all the Makefiles than the former. Idea from David Laight.

* Rename the variables use to print messages. The scheme now is:
_MKMSG_FOO Run _MKMSG 'foo'
_MKTARGET_FOO Run _MKMSG_FOO ${.TARGET}
From discussion with Alistair Crooks.
 1.71 19-Oct-2003  lukem rework to use the newer _MKMSGCREATE (et al) macros
 1.70 19-Oct-2003  lukem improve MAKEVERBOSE message for HOST_LINKed tools
 1.69 19-Oct-2003  lukem Support MAKEVERBOSE (XXX: mksyntax is noisy when MAKEVERBOSE=0).
Use DPSRCS appropriately.
 1.68 18-Oct-2003  lukem Link the host tools against tools/compat -lnbcompat since someone decided to
use strlcpy() and snprintf() in the host tools...

Should fix part of [toolchain/22504], and build problems on other
platforms that don't have strlcpy() or snprintf()...
 1.67 14-Sep-2003  jmmv Add 'trace' to CLEANFILES, generated when debug is enabled.
 1.66 15-May-2003  dsl Fix problems with parallel makes.
 1.65 08-May-2003  christos Use ${HOST_SH}
 1.64 10-Mar-2003  lukem If building as a CRUNCHEDPROG, use "lex -L" and "yacc -l" to suppress #line
generation. This may solve [bin/20637].
 1.63 22-Jan-2003  dsl Support command -p, -v and -V as posix
Stop temporary PATH assigments messing up hash table
Fix sh -c -e "echo $0 $*" -a x (as posix)
(agreed by christos)
 1.62 09-Jan-2003  christos allow this to be compiled with unsigned-chars.
 1.61 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.60 31-Oct-2002  wrstuden Use signed char to replace int8_t, not just char. Should fix macppc
cross-build problems introduced in rev 1.59.

Pointed out by Valeriy E. Ushakov uwe at ptc dot spbu dot ru on
tech-toolchain.
 1.59 15-Sep-2002  thorpej Use "unsigned char" and "char" instead of "u_int8_t" and "int8_t"
for TARGET_CHAR when building mksyntax. This isn't perfect, but
it lets the host tool work on non-BSD systems without completely
redoing how sh is built.
 1.58 19-Aug-2002  lukem Use ${NETBSDSRCDIR}/some/path instead of ${.CURDIR}/../../some/path (etc).
 1.57 15-May-2002  bjh21 branches: 1.57.2;
The printf builtin has been "temporarily" disabled for eight years. Remove
its directory from .PATH to avoid confusion.
 1.56 12-Feb-2002  ross back this directory up a day, systems won't even boot (rc.subr splodes)

suggested back-to-the-drawing-board test: $ echo "${PWD:-notlikely}"
 1.55 11-Feb-2002  christos Add a commented out -DDEBUG in CPPFLAGS.
 1.54 07-Feb-2002  lukem .ifdef SMALLPROG, -DSMALL
 1.53 03-Nov-2001  lukem change from WARNS?=1 to WARNS?=2 for bin/
 1.52 18-Apr-2001  bjh21 Use ${MACHINE_CPU} == "arm" to test for ARMishness.
 1.51 29-Dec-2000  bjh21 arm26 has unsigned chars too.
 1.50 09-Apr-2000  christos PR/9847: Geoff C. Wing: Make test(1) a builtin.
 1.49 09-Jul-1999  christos compile with WARNS = 2
 1.48 05-Feb-1999  tron branches: 1.48.2;
Remove "arith.h" while cleaning.
 1.47 05-Feb-1999  christos PR/5577: Craig M. Chase: sh does not build with PARALLEL set.
- Added YHEADER in Makefile, removed arith.h and adjusted the sources.
 1.46 08-Oct-1998  ross The recent cross-compile changes broke the build of x_sh. Properly use
${.IMPSRC} instead of the broken ${.CURDIR}/thing.c.
 1.45 12-Sep-1998  wrstuden We don't generate .o's anymore for helper programs, so don't CLEANFILES them.
 1.44 12-Sep-1998  wrstuden Patch to make sh cross-compile right. mksyntax reports unsigned char for
powerpc, and signed for m68k & i386.
 1.43 19-Apr-1998  cgd move OBJS depenency on built headers to after include of bsd.prog.mk, so
that things are built in the right order on 'make cleandir && make' (i.e.
so that the headers are properly built).
 1.42 13-Apr-1998  lukem * remove CFLAGS+=-w for powerpc (added in 1.26):
- it was in the wrong place
- makefiles shouldn't override CFLAGS; only CPPFLAGS and COPTS
- christos fixed unsigned char stuff in 1.33 which should
remove the need for -w anyway
* move .include <bsd.prog.mk> to EOF
 1.41 09-Apr-1998  tv .y.c <sys.mk> rule fixes. Don't create a y.tab.h file unless asked for,
and use smarter creation of the header file.
 1.40 30-Mar-1998  veego Another try to make this work again. Lets hope that the we don't need a 4th
fix for it.
 1.39 30-Mar-1998  mrg make this work again when . is not in $PATH
 1.38 29-Mar-1998  christos Pr/5221: Jason Thorpe: Simplify sh Makefile, so that builts from other
directories work.
 1.37 21-Jan-1998  christos BSD4_4 is a standard symbol in <sys/param.h>; make sure that files
that need this defined, include <sys/param.h> and don't define it in
the Makefile. Add a comment to that effect.
 1.36 20-Jan-1998  pk Define BSD4_4: quad_t's in struct rlimit;
 1.35 10-Oct-1997  christos branches: 1.35.2;
CFLAGS->CPPFLAGS
 1.34 20-Jul-1997  christos Remove WARNS=1 from all the subdirectory Makefiles, and add it to Makefile.inc
now that all /bin has been cleaned.
 1.33 04-Jul-1997  christos Make the syntax tables work on machines where characters are unsigned.
 1.32 04-Jul-1997  christos Don't roll own rules to build helper programs.
Add WARNS
 1.31 16-Jun-1997  lukem add auto-generated token.h to dependancy for ${OBJS}.
not everyone runs "make depend" ...
 1.30 15-May-1997  veego Add mkinit.o mknodes.o mksyntax.o to CLEANFILES
 1.29 09-May-1997  mycroft Eliminate bogus redefinitions of standard targets.
 1.28 08-May-1997  gwr Back out the .PATH.c changes. The .depend problem (and others)
will be fixed using the new .NOPATH make feature instead.
 1.27 06-May-1997  gwr Use .PATH.c: ...
 1.26 17-Apr-1997  thorpej - XXX Inhibit warnings on PowerPC for now - there are lots of
"char is unsigned" problems here.
- Add rules for mkinit, mknodes, and mksyntax that use the HOST_*
facilities, for cross-compiling.
 1.25 22-Oct-1996  cgd add builtins.h to the list of (built) headers depended on by ${OBJS}, so
make from 'cleandir' works.
 1.24 16-Oct-1996  christos PR/2808: Depend and clean fixes from FreeBSD
 1.23 18-Feb-1996  mycroft branches: 1.23.4;
Fix problems with the way init.o is built:
* Prevent gratuitous rebuilds when nothing has changed.
* Make sure it's rebuilt if a .h file is updated.
From Mike Long, PR 1454.
 1.22 22-Oct-1995  christos Parallel make fixes.
 1.21 10-Jun-1995  mycroft Add a DPADD.
 1.20 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.19 21-Mar-1995  cgd convert to new RCS id conventions.
 1.18 04-Nov-1994  jtc ulimit builtin (PR #388)

This public domain code, originally by Doug Gwyn, Doug Kingston, Eric
Gisin, and Michael Rendell was ripped from pdksh 5.0.8 and hacked for
use with ash.
 1.17 24-Jun-1994  jtc Renamed builtins to builtins.def to eliminate make's (incorrect) circular
dependancy between the builtins data file and builtins.c.

The bug only occured when there was no obj directory, and is a result of
NetBSD's better (compared to 4.4lite's) default make rules.

Fixes bug #301.
 1.16 11-Jun-1994  mycroft Add RCS ids.
 1.15 02-Jun-1994  pk Exclude `mkinit' from argument list to `mkinit'; this especially causes
trouble when compiling with `-g'.
 1.14 14-May-1994  cgd add back in support for building w/o obj dir. also, add NO_HISTORY
define, which (if you invoke mkbuiltins properly) gets you a sh w/o
history of command line editing (for floppy sh).
 1.13 12-May-1994  jtc Added DPADD.
 1.12 12-May-1994  jtc Use libc's sys_siglist[] instead of building and using our own array of
signal names. (from charles)
 1.11 11-May-1994  jtc temporarily disable printf builtin
 1.10 11-May-1994  jtc sync with 4.4lite
 1.9 28-Dec-1993  davidb Changed to use make's new .OBJDIR
 1.8 14-Nov-1993  cgd from Thomas Eberhardt <thomas@mathematik.uni-Bremen.de>:
Some reordering and modifications in the Makefiles for sh, dump,
restore to get dependicies right. The README in /usr/share/mk
states that it's a Bad Thing(tm) to add something after
.include <bsd.prog.mk>, but this seems the only way to get the
dependencies right.
 1.7 09-Aug-1993  mycroft Tweak for cross-compiling.
 1.6 06-Aug-1993  mycroft Use sys_signame[].
 1.5 02-Aug-1993  mycroft Add RCS identifiers, remove some completely useless RCS logs and patchkit
headers, and a few other insignificant changes.
 1.4 04-May-1993  mycroft Make sure init.c is touched so it doesn't get rebuilt if we
make again. (Okay, so I did reproduce it.)
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.23.4.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.35.2.1 08-May-1998  mycroft Sync with trunk, per request of christos.
 1.48.2.1 03-Jun-2000  he Pull up revision 1.50 (requested by he):
Enable test as a shell builtin. Fixes PR#9847.
 1.57.2.2 06-Dec-2002  he Pull up revision 1.60 (requested by thorpej in ticket #1027):
Use ``signed char'' instead of ``char'' to replace ``int8_t''.
Should fix cross-building problem introudced by the pull-up
in ticket #874.
 1.57.2.1 01-Dec-2002  he Pull up revision 1.59 (requested by thorpej in ticket #819):
Use ``unsigned char'' and ``char'' instead of ``u_int8_t''
and ``int8_t'' for TARGET_CHAR when building mksyntax.
This lets the host tool work on non-BSD systems without
causing too many changes to how sh is built.
 1.83.4.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.98.8.2 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.98.8.1 25-Feb-2013  tls resync with head
 1.98.2.1 16-Jan-2013  yamt sync with (a bit old) head
 1.99.8.1 10-Aug-2014  tls Rebase.
 1.100.2.1 04-Jun-2015  msaitoh Pull up following revision(s) (requested by martin in ticket #821):
bin/sh/builtins.def: revision 1.23
bin/sh/Makefile: revision 1.101
Include printf by default even for SMALL builds. It is used e.g. by
dhcpcd and as such required by most ramdisk images. Allow turning it off
again by TINYPROG.
 1.102.4.1 21-Apr-2017  bouyer Sync with HEAD
 1.102.2.2 26-Apr-2017  pgoyette Sync with HEAD
 1.102.2.1 20-Mar-2017  pgoyette Sync with HEAD
 1.104.2.1 19-May-2017  pgoyette Resolve conflicts from previous merge (all resulting from $NetBSD
keywork expansion)
 1.110.2.1 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.113.4.2 26-Nov-2018  pgoyette Sync with HEAD, resolve a couple of conflicts
 1.113.4.1 25-Jun-2018  pgoyette Sync with HEAD
 1.114.2.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.114.2.2 21-Apr-2020  martin Sync with HEAD
 1.114.2.1 10-Jun-2019  christos Sync with HEAD
 1.115.6.1 31-May-2021  cjep sync with head
 1.123.2.1 02-Aug-2025  perseant Sync with HEAD
 1.11 25-Oct-2016  abhinav branches: 1.11.14;
Fix grammar in couple of sentences.
 1.10 15-Nov-2008  snj branches: 1.10.32;
Some spelling fixes from VaX#n8 in PR bin/23812.
 1.9 24-Apr-2006  snj It's "its."
 1.8 16-Oct-1996  christos PR/2808: tokens.def -> tokens.h
 1.7 11-May-1995  christos branches: 1.7.6;
Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 11-Jun-1994  mycroft Add RCS ids.
 1.4 11-May-1994  jtc sync with 4.4lite
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.7.6.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.10.32.1 04-Nov-2016  pgoyette Sync with HEAD
 1.11.14.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.11.14.1 21-Apr-2020  martin Sync with HEAD
 1.24 12-May-2025  kre Fix a (probably) minor alias command bug

If run as
alias ''
the implementation could potentially reference
uninit'd memory, potentially even leading to a
SEGV, though in testing that doesn't happen, and
I doubt ever could.

Note this only occurs because of some ancient
code commented as "funny ksh stuff" with the
seemingly exotic purpose of allowing '=' to be
an alias.

Prevent even the possibility of the bug, and if
nothing else, make the error message that now
occurs more explicitly indicate the issue.

Note that this code (& this bug) has been in sh since
rev 1.1 of this file (ie: forever in NetBSD, and
before it - original ash had no aliases, so it wasn't
in that, but it has been in CSRG sh since aliases were
first added to that (sccs rev 1.1), in May 1992) and
has never been observed or reported as a problem, that
I'm aware of anyway, so I am not planning any pullups of
this fix. Found by code reading alone.
 1.23 11-May-2025  rillig sh: fix a lint warning

No binary change.
 1.22 24-Feb-2023  kre branches: 1.22.2;

Allow (but do not require) the magic '--' option terminator in
the builtin 'alias' command. This allows portability (not that
anyone should really care with aliases) for scripts from other
shells in which the alias command has options, and the -- is
required to allow the first alias name to begin with a '-'.

That is, for us, alias -x='echo x' works fine, always has,
and still does. But other shells treat that as an attempt
to use the -x option (and maybe -= etc), and require
alias -- -x='echo x'. For us that variant used to complain
about the alias -- not existing (as an arg with no '=' is
treated as a request to extract the value of the alias).

Posix also generally requires all standard commands (or
which "alias" is one, unfortunately) to support '--' even
if they have no options, for precisely this reason.
 1.21 09-Feb-2019  kre 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.
 1.20 03-Dec-2018  kre 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.
 1.19 02-Dec-2018  kre 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.)
 1.18 01-Dec-2018  kre NFC. KNF. return (x) -> return x
 1.17 07-Oct-2018  rillig When listing aliases, sort them alphabetically.
 1.16 24-Jul-2017  kre branches: 1.16.2; 1.16.4;
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).
 1.15 18-Jun-2014  christos branches: 1.15.16;
Adjust to the new alias text libedit API.
 1.14 18-Jun-2011  christos branches: 1.14.8; 1.14.20;
PR/45069: Henning Petersen: Use prototypes from builtins.h .
 1.13 29-Oct-2010  stacktic branches: 1.13.4;
Initialized flag field in struct alias (Fixed PR/43281)
 1.12 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.11 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.10 20-May-1998  christos Fix dangling else warning.
 1.9 04-Jul-1997  christos Fix compiler warnings.
 1.8 11-Jan-1997  tls kill 'register'
 1.7 16-Oct-1996  christos PR/2808: Remove trailing whitespace (from FreeBSD)
 1.6 11-May-1995  christos branches: 1.6.6;
Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.5 21-Mar-1995  cgd convert to new RCS id conventions.
 1.4 04-Dec-1994  cgd from James Jegers <jimj@miller.cs.uwm.edu>: quiet -Wall, and squelch
some of the worst style errors.
 1.3 23-Sep-1994  mycroft Eliminate uses of some obsolete functions.
 1.2 11-Jun-1994  mycroft Add RCS ids.
 1.1 11-May-1994  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 11-May-1994  jtc 44lite code
 1.6.6.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.13.4.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.14.20.1 10-Aug-2014  tls Rebase.
 1.14.8.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.15.16.1 07-Dec-2018  martin Pull up following revision(s) (requested by kre in ticket #1126):

bin/sh/alias.c: revision 1.19

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.)
 1.16.4.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.16.4.2 21-Apr-2020  martin Sync with HEAD
 1.16.4.1 10-Jun-2019  christos Sync with HEAD
 1.16.2.2 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.16.2.1 20-Oct-2018  pgoyette Sync with head
 1.22.2.1 02-Aug-2025  perseant Sync with HEAD
 1.9 03-Dec-2018  kre 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.
 1.8 18-Jun-2014  christos branches: 1.8.22; 1.8.24;
Adjust to the new alias text libedit API.
 1.7 18-Jun-2011  christos branches: 1.7.8; 1.7.20;
PR/45069: Henning Petersen: Use prototypes from builtins.h .
 1.6 07-Aug-2003  agc branches: 1.6.52;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.5 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.4 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.3 21-Mar-1995  cgd convert to new RCS id conventions.
 1.2 11-Jun-1994  mycroft Add RCS ids.
 1.1 11-May-1994  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 11-May-1994  jtc 44lite code
 1.6.52.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.7.20.1 10-Aug-2014  tls Rebase.
 1.7.8.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.8.24.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.8.24.2 21-Apr-2020  martin Sync with HEAD
 1.8.24.1 10-Jun-2019  christos Sync with HEAD
 1.8.22.1 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.3 05-Feb-1999  christos PR/5577: Craig M. Chase: sh does not build with PARALLEL set.
- Added YHEADER in Makefile, removed arith.h and adjusted the sources.
 1.2 04-Jul-1997  christos Fix compiler warnings.
 1.1 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.26 20-Mar-2017  kre Finish support for all required $(( )) (shell arithmetic) operators,
closing PR bin/50958

That meant adding the assignment operators ('=', and all of the +=, *= ...)
Currently, ++, --, and ',' are not implemented (none of those are required
by posix) but support for them (most likely ',' first) might be added later.

To do this, I removed the yacc/lex arithmetic parser completely, and
replaced it with a hand written recursive descent parser, that I obtained
from FreeBSD, who earlier had obtained it from dash (Herbert Xu).

While doing the import, I cleaned up the sources (changed some file names
to avoid requiring a clean build, or signifigant surgery to the obj
directories if "build.sh -u" was to be used - "build.sh -u" should work
fine as it is now) removed some dashisms, applied some KNF, ...
 1.25 12-May-2016  kre branches: 1.25.2; 1.25.4;

Fix the implementation of the ?: $(( )) operator. It is right associative...

ok christos@
 1.24 16-Mar-2016  christos PR/50960: The || and && operators in $(( )) should always have a 0 or 1
result, never anything different. (from kre)
 1.23 16-Mar-2016  christos PR/50958: (partial fix) - support ?: expressions in arith expansions
(from kre)
 1.22 20-Mar-2012  matt Use C89 function definitions
 1.21 29-Aug-2011  joerg branches: 1.21.2;
Mark yyerror as static and __dead.
 1.20 18-Jun-2011  christos PR/45069: Henning Petersen: Use prototypes from builtins.h .
 1.19 13-Nov-2009  gson branches: 1.19.4;
Removed return statement in parser action that caused a memory leak with
the new yacc, making "sh MAKEDEV -MM init" consume 27 MB of virtual memory
with the result that NetBSD could no longer be installed on a 32 MB system.
 1.18 25-Mar-2007  apb Make /bin/sh use intmax_t (instead of int) for arithmetic in $((...)).
 1.17 17-Sep-2003  jmmv branches: 1.17.16;
Drop equal signs from rule definitions; they cause problems with "bison -y".
 1.16 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.15 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.14 04-Feb-2001  christos remove redundant declaration of yyparse, and move code to the bottom of
the file.
 1.13 09-Jul-1999  christos compile with WARNS = 2
 1.12 05-Feb-1999  christos PR/5577: Craig M. Chase: sh does not build with PARALLEL set.
- Added YHEADER in Makefile, removed arith.h and adjusted the sources.
 1.11 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.10 18-May-1998  christos Need stdlib.h
 1.9 31-Jan-1998  christos Fix printf format.
 1.8 04-Jul-1997  christos branches: 1.8.2;
Fix compiler warnings.
 1.7 16-Oct-1996  christos PR/2808: Remove trailing whitespace (from FreeBSD)
 1.6 11-May-1995  christos branches: 1.6.6;
Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.5 21-Mar-1995  cgd convert to new RCS id conventions.
 1.4 23-Dec-1994  cgd eliminate two wrong-sized variables which were unused.
 1.3 15-Jul-1994  jtc Don't dump core when evaluating X % 0.
Simplify lexer by using %prec feature in yacc grammar.
Implement most of the operators required by POSIX.2.
 1.2 11-Jun-1994  mycroft Add RCS ids.
 1.1 11-May-1994  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 11-May-1994  jtc 44lite code
 1.6.6.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.8.2.1 08-May-1998  mycroft Sync with trunk, per request of christos.
 1.17.16.1 16-Apr-2007  bouyer Pull up following revision(s) (requested by apb in ticket #570):
bin/sh/expand.c: revision 1.78
bin/sh/arith.y: revision 1.18
bin/sh/expand.h: revision 1.17
regress/bin/sh/expand.sh: revision 1.4
bin/sh/sh.1: revision 1.86
bin/sh/arith_lex.l: revision 1.14
Make /bin/sh use intmax_t (instead of int) for arithmetic in $((...)).
 1.19.4.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.21.2.1 17-Apr-2012  yamt sync with head
 1.25.4.1 21-Apr-2017  bouyer Sync with HEAD
 1.25.2.1 26-Apr-2017  pgoyette Sync with HEAD
 1.19 20-Mar-2017  kre Finish support for all required $(( )) (shell arithmetic) operators,
closing PR bin/50958

That meant adding the assignment operators ('=', and all of the +=, *= ...)
Currently, ++, --, and ',' are not implemented (none of those are required
by posix) but support for them (most likely ',' first) might be added later.

To do this, I removed the yacc/lex arithmetic parser completely, and
replaced it with a hand written recursive descent parser, that I obtained
from FreeBSD, who earlier had obtained it from dash (Herbert Xu).

While doing the import, I cleaned up the sources (changed some file names
to avoid requiring a clean build, or signifigant surgery to the obj
directories if "build.sh -u" was to be used - "build.sh -u" should work
fine as it is now) removed some dashisms, applied some KNF, ...
 1.18 16-Mar-2016  christos branches: 1.18.2; 1.18.4;
PR/50958: (partial fix) - support ?: expressions in arith expansions
(from kre)
 1.17 16-Mar-2016  christos PR bin/50959 - allow consistent use of 0X hex constants (not just 0x)
(from kre)
 1.16 20-Mar-2012  matt Use C89 function definitions
 1.15 29-Oct-2009  christos branches: 1.15.6;
use flex options instead of #defines
 1.14 25-Mar-2007  apb Make /bin/sh use intmax_t (instead of int) for arithmetic in $((...)).
 1.13 21-Mar-2005  dsl branches: 1.13.4;
A rather better fix for treating $((x)) as equivalent to $(($x)) provided
that $x has a numeric value - which is what posix/sus needs.
 1.12 07-Aug-2003  agc branches: 1.12.6;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.11 06-May-2003  dsl Recognise octal and hexadecimal constants in expressions.
- as required by SUSv3
 1.10 05-Feb-1999  christos branches: 1.10.14;
PR/5577: Craig M. Chase: sh does not build with PARALLEL set.
- Added YHEADER in Makefile, removed arith.h and adjusted the sources.
 1.9 18-May-1998  christos Portability fixes for a non flex based lexical analyzer.
 1.8 29-Mar-1998  mrg - change "extern" variables into int's
- remove extern'd variables not actually referenced
- don't use char as an array index
 1.7 04-Jul-1997  christos branches: 1.7.2;
Fix compiler warnings.
 1.6 16-Oct-1996  christos PR/2808: Remove trailing whitespace (from FreeBSD)
 1.5 11-May-1995  christos branches: 1.5.6;
Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.4 21-Mar-1995  cgd convert to new RCS id conventions.
 1.3 15-Jul-1994  jtc Don't dump core when evaluating X % 0.
Simplify lexer by using %prec feature in yacc grammar.
Implement most of the operators required by POSIX.2.
 1.2 11-Jun-1994  mycroft Add RCS ids.
 1.1 11-May-1994  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 11-May-1994  jtc 44lite code
 1.5.6.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.7.2.1 08-May-1998  mycroft Sync with trunk, per request of christos.
 1.10.14.1 06-Apr-2004  jmc Pullup rev 1.11 (requested by simonb in ticket #1650)

Recognise octal and hexadecimal constants in expressions.
 1.12.6.1 07-Apr-2005  tron Pull up revision 1.13 (requested by dsl in ticket #120):
A rather better fix for treating $((x)) as equivalent to $(($x)) provided
that $x has a numeric value - which is what posix/sus needs.
 1.13.4.1 16-Apr-2007  bouyer Pull up following revision(s) (requested by apb in ticket #570):
bin/sh/expand.c: revision 1.78
bin/sh/arith.y: revision 1.18
bin/sh/expand.h: revision 1.17
regress/bin/sh/expand.sh: revision 1.4
bin/sh/sh.1: revision 1.86
bin/sh/arith_lex.l: revision 1.14
Make /bin/sh use intmax_t (instead of int) for arithmetic in $((...)).
 1.15.6.1 17-Apr-2012  yamt sync with head
 1.18.4.1 21-Apr-2017  bouyer Sync with HEAD
 1.18.2.1 26-Apr-2017  pgoyette Sync with HEAD
 1.7 17-Dec-2017  kre branches: 1.7.4;
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).
 1.6 24-Jul-2017  kre Add support for ++ and -- (pre & post) and ',' to arithmetic.
 1.5 07-Jun-2017  kre A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)

This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)

The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.

This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).

I would not call the results of this perfect yet, but it is close.
 1.4 29-May-2017  kre branches: 1.4.2;

Add DEBUG tracing to arithmetic $(( )) parsing & evaluation.
NFC for non-DEBUG shells.
 1.3 20-Mar-2017  kre branches: 1.3.2; 1.3.4;

That will teach me to commit from the version I've actually
been testing, rather than the "other version" ...
 1.2 20-Mar-2017  kre Alternative way of writing (part of) the arithmetic lexical analyzer
- committed separately to make it easier to swap back if desired.

This version avoids open coding is_name() and isdigit() - measurements
show it is perhaps fractionally faster (though the difference is so small
as to probably be statistically insignifigant - if I were a statician
I might understand that) and just a little larger than the previous version.
 1.1 20-Mar-2017  kre Finish support for all required $(( )) (shell arithmetic) operators,
closing PR bin/50958

That meant adding the assignment operators ('=', and all of the +=, *= ...)
Currently, ++, --, and ',' are not implemented (none of those are required
by posix) but support for them (most likely ',' first) might be added later.

To do this, I removed the yacc/lex arithmetic parser completely, and
replaced it with a hand written recursive descent parser, that I obtained
from FreeBSD, who earlier had obtained it from dash (Herbert Xu).

While doing the import, I cleaned up the sources (changed some file names
to avoid requiring a clean build, or signifigant surgery to the obj
directories if "build.sh -u" was to be used - "build.sh -u" should work
fine as it is now) removed some dashisms, applied some KNF, ...
 1.3.4.2 26-Apr-2017  pgoyette Sync with HEAD
 1.3.4.1 20-Mar-2017  pgoyette file arith_token.c was added on branch pgoyette-localcount on 2017-04-26 02:52:13 +0000
 1.3.2.2 21-Apr-2017  bouyer Sync with HEAD
 1.3.2.1 20-Mar-2017  bouyer file arith_token.c was added on branch bouyer-socketcan on 2017-04-21 16:50:42 +0000
 1.4.2.1 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.7.4.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.7.4.1 21-Apr-2020  martin Sync with HEAD
 1.3 24-Jul-2017  kre branches: 1.3.4;
Add support for ++ and -- (pre & post) and ',' to arithmetic.
 1.2 07-Jun-2017  kre A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)

This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)

The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.

This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).

I would not call the results of this perfect yet, but it is close.
 1.1 20-Mar-2017  kre branches: 1.1.2; 1.1.4; 1.1.8;


Finish support for all required $(( )) (shell arithmetic) operators,
closing PR bin/50958

That meant adding the assignment operators ('=', and all of the +=, *= ...)
Currently, ++, --, and ',' are not implemented (none of those are required
by posix) but support for them (most likely ',' first) might be added later.

To do this, I removed the yacc/lex arithmetic parser completely, and
replaced it with a hand written recursive descent parser, that I obtained
from FreeBSD, who earlier had obtained it from dash (Herbert Xu).

While doing the import, I cleaned up the sources (changed some file names
to avoid requiring a clean build, or signifigant surgery to the obj
directories if "build.sh -u" was to be used - "build.sh -u" should work
fine as it is now) removed some dashisms, applied some KNF, ...
 1.1.8.1 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.1.4.2 26-Apr-2017  pgoyette Sync with HEAD
 1.1.4.1 20-Mar-2017  pgoyette file arith_tokens.h was added on branch pgoyette-localcount on 2017-04-26 02:52:13 +0000
 1.1.2.2 21-Apr-2017  bouyer Sync with HEAD
 1.1.2.1 20-Mar-2017  bouyer file arith_tokens.h was added on branch bouyer-socketcan on 2017-04-21 16:50:42 +0000
 1.3.4.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.3.4.1 21-Apr-2020  martin Sync with HEAD
 1.5 21-Apr-2018  kre branches: 1.5.2;

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).
 1.4 24-Jul-2017  kre branches: 1.4.2;
Add support for ++ and -- (pre & post) and ',' to arithmetic.
 1.3 07-Jun-2017  kre A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)

This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)

The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.

This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).

I would not call the results of this perfect yet, but it is close.
 1.2 29-May-2017  kre branches: 1.2.2;

Add DEBUG tracing to arithmetic $(( )) parsing & evaluation.
NFC for non-DEBUG shells.
 1.1 20-Mar-2017  kre branches: 1.1.2; 1.1.4;


Finish support for all required $(( )) (shell arithmetic) operators,
closing PR bin/50958

That meant adding the assignment operators ('=', and all of the +=, *= ...)
Currently, ++, --, and ',' are not implemented (none of those are required
by posix) but support for them (most likely ',' first) might be added later.

To do this, I removed the yacc/lex arithmetic parser completely, and
replaced it with a hand written recursive descent parser, that I obtained
from FreeBSD, who earlier had obtained it from dash (Herbert Xu).

While doing the import, I cleaned up the sources (changed some file names
to avoid requiring a clean build, or signifigant surgery to the obj
directories if "build.sh -u" was to be used - "build.sh -u" should work
fine as it is now) removed some dashisms, applied some KNF, ...
 1.1.4.2 26-Apr-2017  pgoyette Sync with HEAD
 1.1.4.1 20-Mar-2017  pgoyette file arithmetic.c was added on branch pgoyette-localcount on 2017-04-26 02:52:13 +0000
 1.1.2.2 21-Apr-2017  bouyer Sync with HEAD
 1.1.2.1 20-Mar-2017  bouyer file arithmetic.c was added on branch bouyer-socketcan on 2017-04-21 16:50:42 +0000
 1.2.2.1 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.4.2.1 22-Apr-2018  pgoyette Sync with HEAD
 1.5.2.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.5.2.1 21-Apr-2020  martin Sync with HEAD
 1.2 07-Jun-2017  kre branches: 1.2.6;
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)

This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)

The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.

This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).

I would not call the results of this perfect yet, but it is close.
 1.1 20-Mar-2017  kre branches: 1.1.2; 1.1.4; 1.1.8;


Finish support for all required $(( )) (shell arithmetic) operators,
closing PR bin/50958

That meant adding the assignment operators ('=', and all of the +=, *= ...)
Currently, ++, --, and ',' are not implemented (none of those are required
by posix) but support for them (most likely ',' first) might be added later.

To do this, I removed the yacc/lex arithmetic parser completely, and
replaced it with a hand written recursive descent parser, that I obtained
from FreeBSD, who earlier had obtained it from dash (Herbert Xu).

While doing the import, I cleaned up the sources (changed some file names
to avoid requiring a clean build, or signifigant surgery to the obj
directories if "build.sh -u" was to be used - "build.sh -u" should work
fine as it is now) removed some dashisms, applied some KNF, ...
 1.1.8.1 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.1.4.2 26-Apr-2017  pgoyette Sync with HEAD
 1.1.4.1 20-Mar-2017  pgoyette file arithmetic.h was added on branch pgoyette-localcount on 2017-04-26 02:52:13 +0000
 1.1.2.2 21-Apr-2017  bouyer Sync with HEAD
 1.1.2.1 20-Mar-2017  bouyer file arithmetic.h was added on branch bouyer-socketcan on 2017-04-21 16:50:42 +0000
 1.2.6.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.2.6.1 21-Apr-2020  martin Sync with HEAD
 1.3 26-Jan-1994  mycroft Clean up deleted files.
 1.2 02-Aug-1993  mycroft Makefile
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.10 24-Jun-1994  mycroft Clean up deleted files.
 1.9 11-Jun-1994  mycroft Add RCS ids.
 1.8 14-May-1994  cgd add back in support for building w/o obj dir. also, add NO_HISTORY
define, which (if you invoke mkbuiltins properly) gets you a sh w/o
history of command line editing (for floppy sh).
 1.7 11-May-1994  jtc reintegrate NetBSD's false builtin
 1.6 11-May-1994  jtc temporarily disable printf builtin
 1.5 11-May-1994  jtc sync with 4.4lite
 1.4 07-Jul-1993  jtc IEEE 1003.2 (D11.2.2.3) requires that the system's true and false be accessed
instead of searching $PATH. The best way to satisfy this requirement is to
make them builtins.

True was allready builtin, this patch adds false.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.29 09-Oct-2024  kre PR bin/58687 -- implement suspend as a builtin in sh

Requested by uwe@ in PR bin/58687 without objections from
anyone except me, here is an implementation of a suspend
builtin command for /bin/sh

The sh.1 man page is updated, naturally, to describe it.

This new builtin does not exist in SMALL shells -- as used
on (some) boot media, etc.

If this turns out not to be useful, it can easily be removed.
 1.28 09-Oct-2024  kre Housekeeping.

Improve some comments, and sort the list of builtins, to make it easier
to find what is there and what isn't (since some have multiple different
command names, sort by the internal function name). Still leave the
commented out, could be implemented, but haven't ones, almost to last,
and the almost never indluded debug command absolute last.

NFCI. (No changes should be visible externally).
 1.27 10-Nov-2021  kre branches: 1.27.4;

Ensure that all of the POSIX standard utilities are correctly
identified with the -u flag (that is, I hope I identified all
the ones that were missing it).

This change is a no-op (NFC) as the -u flag does nothing.

Still, just in case we find a use for it one day, and just as a
matter of general principle, we should get this correct.
 1.26 14-Feb-2019  kre 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.
 1.25 15-May-2017  kre branches: 1.25.10;

DEBUG mode shell update (changes nothing for shells which are not
compiled for DEBUG.)

Add debug builtin command, and corresponding -D command line option.
As usual, for DEBUG related stuff, read the source for info, that's
all there is about this.

This completes the infrastructure changes for the updated DEBUG TRACE
mechanism, so now converting the rest of the shell's internal tracing
can happen as desired - piecemeal.
 1.24 02-Feb-2017  christos branches: 1.24.2;
Add fdflags builtin. Discussed with Chet and he has implemented it for
bash too.
 1.23 10-May-2015  joerg branches: 1.23.2; 1.23.4;
Include printf by default even for SMALL builds. It is used e.g. by
dhcpcd and as such required by most ramdisk images. Allow turning it off
again by TINYPROG.
 1.22 31-Dec-2012  dsl branches: 1.22.10;
Add support for '%n' being a shorthand for 'fg %n'.
 1.21 13-Jul-2004  seb branches: 1.21.48; 1.21.54;
Add new builtin `wordexp' to support wordexp(3).

From FreeBSD.
Provided in PR lib/26123.
Approved by kleink@.
 1.20 06-Jun-2004  christos don't include the printf builtin if we are SMALL; saves 10K.
 1.19 22-Aug-2003  itojun remove \r (agc, are you using MS-DOS?)
 1.18 22-Aug-2003  agc Move 4 more files from UCB 4 clause to 3 clause licence.

Thanks to Julio Merino for pointing them out.
 1.17 04-May-2003  gmcgarry Add new builtin 'inputrc' which allows keybindings to be redefined
for the current shell. From Arne H Juul in PR#10097.
 1.16 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.15 09-Apr-2000  christos PR/9847: Geoff C. Wing: Make test(1) a builtin.
 1.14 14-Mar-1997  christos branches: 1.14.4;
NO_HISTORY->SMALL
 1.13 06-Feb-1997  christos add type builtin.
 1.12 11-May-1995  christos branches: 1.12.6;
Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.11 21-Mar-1995  cgd convert to new RCS id conventions.
 1.10 04-Nov-1994  jtc ulimit builtin (PR #388)

This public domain code, originally by Doug Gwyn, Doug Kingston, Eric
Gisin, and Michael Rendell was ripped from pdksh 5.0.8 and hacked for
use with ash.
 1.9 11-Jun-1994  mycroft Add RCS ids.
 1.8 14-May-1994  cgd add back in support for building w/o obj dir. also, add NO_HISTORY
define, which (if you invoke mkbuiltins properly) gets you a sh w/o
history of command line editing (for floppy sh).
 1.7 11-May-1994  jtc reintegrate NetBSD's false builtin
 1.6 11-May-1994  jtc temporarily disable printf builtin
 1.5 11-May-1994  jtc sync with 4.4lite
 1.4 07-Jul-1993  jtc IEEE 1003.2 (D11.2.2.3) requires that the system's true and false be accessed
instead of searching $PATH. The best way to satisfy this requirement is to
make them builtins.

True was allready builtin, this patch adds false.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.12.6.1 04-Mar-1997  mycroft Pull up latest sh(1). Fixes yet more bugs.
 1.14.4.1 03-Jun-2000  he Pull up revision 1.15 (requested by he):
Enable test as a shell builtin. Fixes PR#9847.
 1.21.54.1 25-Feb-2013  tls resync with head
 1.21.48.1 23-Jan-2013  yamt sync with head
 1.22.10.1 04-Jun-2015  msaitoh Pull up following revision(s) (requested by martin in ticket #821):
bin/sh/builtins.def: revision 1.23
bin/sh/Makefile: revision 1.101
Include printf by default even for SMALL builds. It is used e.g. by
dhcpcd and as such required by most ramdisk images. Allow turning it off
again by TINYPROG.
 1.23.4.1 21-Apr-2017  bouyer Sync with HEAD
 1.23.2.1 20-Mar-2017  pgoyette Sync with HEAD
 1.24.2.1 19-May-2017  pgoyette Resolve conflicts from previous merge (all resulting from $NetBSD
keywork expansion)
 1.25.10.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.25.10.2 21-Apr-2020  martin Sync with HEAD
 1.25.10.1 10-Jun-2019  christos Sync with HEAD
 1.27.4.1 02-Aug-2025  perseant Sync with HEAD
 1.56 18-Aug-2025  kre PR bin/59602 PWD update failure message from cd fix.

Only complain about being unable to determine the new
directory name after a cd when -e is given (test for
that was forgotten).
 1.55 12-Aug-2025  kre Improve error messages from cd builtin

Have cd indicate (particularly for when CDPATH is in use,
but also in other cases) which path name was attempted
and failed, and why it failed. For CDPATH uses, when
(if) the cd eventually fails, prefer the first entry in
which the requested destination path is located (when
the chdir() to it fails), or if there is none of those,
the first lookup which failed to determine if the path
exists there or not (ie: not ENOENT).

If the all attempts return ENOENT simply report the arg
given (after "cd -" and "cd a b" processing), as no
one CDPATH entry is more appropriate than any other.

Inspired by PR standards/59565 but not directly related.
 1.54 12-Aug-2025  kre PR standards/59565 - cd "" is now an error, not == cd .

POSIX 2024 requires that cd "" generate an error message,
and fail.

Until now shells (including this one) have typically equated
it to "cd ." as in the distant past, a lookup of "" as a path
name would return the current directory (aka ".").

From now on, we generate an error, as specified.
(For this commit, the error message is kind of pathetic,
but it is at least there - that will be fixed in a
subsequent commit.)
 1.53 31-Jan-2022  kre branches: 1.53.6;

Add some comments explaining accesses to the environment via
getenv()/setenv()/unsetenv() which manipulate the envornoment
the shell was passed at entry.

These are a little odd in sh as that environment is copied into
the shell's internal variable data struct at shell startup, and
normally never accessed after that - in builtin commands (test.
printf, ...) getenv() is #defined to become an internal sh lookup
function instead, so even those never use the startup environment).

NFCI
 1.52 16-Nov-2021  kre Make pwd (both /bin/pwd and the /bin/sh built-in version) check for
write errors on stdout, and indicate an error if that happens.
 1.51 31-Oct-2021  kre PR bin/45390 - fix for folly four

In the pwd builtin, verify that curdir names '.' before
simply printing it. Never alter PWD or OLDPWD in the
pwd command.

Also while here, implement the (new: coming in POSIX, but has existed
for a while in several other shells) -e option to cd (with -e, cd -P
will exit(1) if the chdir() succeeds, but PWD cannot be discovered).

cd now prints the directory name used (if different from that given,
or cdprint is on) if interactive or (the new bit)in posix mode.

Some additional/changed comments added, and a DEBUG mode trace call
that was accidentally put inside an #if 0 block moved to where it
can do some good.

XXX pullup -9
 1.50 05-Jul-2017  kre branches: 1.50.6; 1.50.8;

DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
 1.49 17-Jun-2017  kre Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
 1.48 04-Jun-2017  kre Make cd (really) do cd -P, and not just claim that is what it is doing
while doing a half-hearted, broken, partial, version of cd -L instead.
The latter (as the manual says) is not supported, what's more, it is an
abomination, and should never be supported (anywhere.)

Fix the doc so that the pretense that we notice when a path given crosses
a symlink (and turns on printing of the destination directory) is claimed
no more (that used to be true until late Dec 2016, but was changed). Now
the print happens if -o cdprint is set, or if an entry from CDPATH that is
not "" or "." is used (or if the "cd dest repl" cd cmd variant is used.)

Fix CDPATH processing: avoid the magic '%' processing that is used for
PATH and MAILPATH from corrupting CDPATH. The % magic (both variants)
remains undocumented.

Also, don't double the '/' if an entry in PATH or CDPATH ends in '/'
(as in CDPATH=":/usr/src/"). A "cd usr.bin" used to do
chdir("/usr/src//usr.bin"). No more. This is almost invisible,
and relatively harmless, either way....

Also fix a bug where if a plausible destination directory in CDPATH
was located, but the chdir() failed (eg: permission denied) and then
a later "." or "" CDPATH entry succeeded, "print" mode was turned on.
That is:
cd /tmp; mkdir bin
mkdir -p P/bin; chmod 0 P/bin
CDPATH=/tmp/P:
cd bin
would cd to /tmp/bin (correctly) but print it (incorrectly).

Also when in "cd dest replace" mode, if the result of the replacement
generates '-' as the path named, as in:
cd $PWD -
then simply change to '-' (or attempt to, with CDPATH search), rather
than having this being equivalent to "cd -")

Because of these changes, the pwd command (and $PWD) essentially
always acts as pwd -P, even when called as pwd -L (which is still
the default.) That is, even more than it did before.

Also fixed a (kind of minor) mem management error (CDPATH related)
"whosoever shall padvance must stunalloc before repeating" (and the
same for MAILPATH).
 1.47 26-Dec-2016  christos branches: 1.47.6;
Don't trash the logical $PWD if a component is a symlink; no other shell
does this.
 1.46 02-May-2016  christos branches: 1.46.2;
Fix handing of user file descriptors outside the 0..9 range.
Also, move (most of) the shell's internal use fd's to much
higher values (depending upon what ulimit -n allows) so they
are less likely to clash with user supplied fd numbers. A future
patch will (hopefully) avoid this problem completely by dynamically
moving the shell's internal fds around as needed. (From kre@)
 1.45 04-Jan-2016  christos Don't leak redirected rescriptors to exec'ed processes. This is what ksh
does, but bash does not. For example:

$ cat test1
#!/bin/sh
exec 6> out
echo "test" >&6
sh ./test2
exec 6>&-
$ cat test2
echo "test2" >&6
$ ./test1
./test2: 6: Bad file descriptor

This fixes by side effect the problem of the rc system leaking file descriptors
7 and 8 to all starting daemons:

$ fstat -p 1359
USER CMD PID FD MOUNT INUM MODE SZ|DV R/W
root powerd 1359 wd / 2 drwxr-xr-x 512 r
root powerd 1359 0 / 63029 crw-rw-rw- null rw
root powerd 1359 1 / 63029 crw-rw-rw- null rw
root powerd 1359 2 / 63029 crw-rw-rw- null rw
root powerd 1359 3* kqueue pending 0
root powerd 1359 4 / 64463 crw-r----- power r
root powerd 1359 7 flags 0x80034<ISTTY,MPSAFE,LOCKSWORK,CLEAN>
root powerd 1359 8 flags 0x80034<ISTTY,MPSAFE,LOCKSWORK,CLEAN>
root powerd 1359 9* pipe 0xfffffe815d7bfdc0 -> 0x0 w

Note fd=7,8 pointing to the revoked pty from the parent rc process.
 1.44 31-Aug-2011  plunky NULL does not need a cast
 1.43 18-Jun-2011  christos PR/45069: Henning Petersen: Use prototypes from builtins.h .
 1.42 10-Jun-2011  uebayasi Support $OLDPWD. (christos@ will update the manual.)

Reviewd By: christos
 1.41 17-Feb-2011  pooka branches: 1.41.2;
Tell copyfd if the caller wants the exact tofd to just fd >= tofd.
Fixes "echo foo > /rump/bar" in a rump hijacked shell.

reviewed by christos
 1.40 01-Jan-2010  dholland branches: 1.40.2;
Make the cd builtin accept and ignore -P, which is a kshism that has been
allowed to leak into POSIX and selects the behavior cd already implements.
Closes PR bin/42557 and also relevant to PR pkg/42168.

I suppose this should probably be pulled up to both -4 and -5...
 1.39 04-May-2006  simonb branches: 1.39.4; 1.39.26; 1.39.32;
Make the change for bin/31548 to set $PWD on shell startup non-fatal if
the current directory doesn't exist.
 1.38 26-Nov-2005  dsl Don't change the name of the directory we are going to 'cd' to just because
we decide to remove a leading "./" when deciding whether to print the name.
Stops 'cd .//tmp' ending up the same as 'cd /tmp'.
Fixes PR/30735
 1.37 10-Oct-2005  christos PR/31548: Alexander Rigbo: $PWD not set when shell starts up, before pwd
or cd get executed.
 1.36 20-Aug-2005  dsl Don't apply CDPATH if the the first component of the target is "." or "..".
Fixes PR/30973 and applies the principle of least surprise.
Update documentation to match (including date).
(matches behaviour of pdksh - if not it's documentation)
 1.35 15-Jul-2005  christos Only do certain actions when we are trully interactive, not just when we
are connected to a tty:
1. enable editor
2. print directory on directory change.
3. print job messages
 1.34 14-Nov-2003  dsl Posix requires that 'pwd -P' reset the shells saved cwd value - so a
subsequent 'pwd -L' will report the same value.
Update man page to be a closer match to reality.
 1.33 30-Oct-2003  dsl Validate the arguments to 'pwd'. Treat -L and -P as per pwd(1).
Note that 'pwd -L' and 'pwd -P' almost always report the same answer as
the shell detects when 'cd' follows a symlink and discards its cached 'pwd'
 1.32 25-Aug-2003  jmmv Move an assignment that was using a variable out of scope to fix a build
problem on !NetBSD.
 1.31 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.30 22-Jan-2003  dsl Support command -p, -v and -V as posix
Stop temporary PATH assigments messing up hash table
Fix sh -c -e "echo $0 $*" -a x (as posix)
(agreed by christos)
 1.29 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.28 14-Nov-2001  he Plug a memory leak when setting PWD -- don't mark the variable with
VTEXTFIXED because the value is dynamically allocated. Fix from
Tor Egge, FreeBSD PR#31533.
 1.27 09-Jul-1999  christos compile with WARNS = 2
 1.26 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.25 21-May-1998  ross Fix a set of bugs where a cd of a relative pathname or a cd through a
symbolic link would not set PWD.
 1.24 17-Feb-1998  christos PR/5001: Tom Yu: cd ./ does not work.
 1.23 04-Jul-1997  christos branches: 1.23.2;
PR/3827: Wolfgang Rupprecht: cd ./. core dumps
Warning cleanups
 1.22 11-Apr-1997  christos __svr4__ -> __SVR4
 1.21 07-Mar-1997  christos Fix PR/3289: upon initialization check if $PWD contains the right directory
and use it. We also maintain $PWD now, like the rest of the
shells.
 1.20 11-Jan-1997  tls kill 'register'
 1.19 22-Dec-1996  cjs Re-introduce some code from 1.14 (with modifications) to make the
shell's idea of the current working directory match reality when
symlinks (or anything else we can't stat) are traversed with the
cd command. However, this does not print out the cwd after cd
traverses symlinks, as it used to, though the capability is still
there should one want to use it.
 1.18 16-Oct-1996  christos PR/2808: Handle cd "". This is for systems where "" != "."
 1.17 18-Sep-1996  thorpej If getcwd() fails, dang it, I want to know _why_.
 1.16 25-Jun-1996  christos Include header files to get function prototypes; don't use home grown ones.
 1.15 01-Mar-1996  jtc branches: 1.15.4;
Changed so that cd builtin doesn't do wierd stuff when cd'ing through
symlinks. From Chris Demetriou <cgd@NetBSD.ORG>. Fixes PR #1776.

Changed so that INTOFF/INTON are paired in getpwd(). From Matthias
Pfaller <leo@marco.de>. Fixes PR #2130.
 1.14 19-Nov-1995  christos Fix PR/1760, where 'cd -' before any other command could cause a reference
to an uninitialized pointer. Use getcwd() to get the current working directory,
instead of forking /bin/pwd [per Scott's suggestion]
 1.13 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.12 21-Mar-1995  cgd convert to new RCS id conventions.
 1.11 30-Jan-1995  mycroft Use S_IS*().
 1.10 05-Dec-1994  cgd clean up further. more patches from Jim Jegers
 1.9 04-Dec-1994  cgd from James Jegers <jimj@miller.cs.uwm.edu>: quiet -Wall, and squelch
some of the worst style errors.
 1.8 11-Jun-1994  mycroft Add RCS ids.
 1.7 12-May-1994  jtc Include appropriate header files to bring function prototypes into scope.
 1.6 11-May-1994  jtc sync with 4.4lite
 1.5 01-Aug-1993  mycroft Add RCS identifiers.
 1.4 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.3 22-Mar-1993  cgd added rcs ids to all files
 1.2 21-Mar-1993  cgd after 0.2.2 "stable" patches applied
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.15.4.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.23.2.1 08-May-1998  mycroft Sync with trunk, per request of christos.
 1.39.32.1 21-Apr-2010  matt sync to netbsd-5
 1.39.26.1 30-Jan-2010  snj Pull up following revision(s) (requested by dholland in ticket #1274):
bin/sh/cd.c: revision 1.40
bin/sh/sh.1: revision 1.95
Make the cd builtin accept and ignore -P, which is a kshism that has been
allowed to leak into POSIX and selects the behavior cd already implements.
Closes PR bin/42557 and also relevant to PR pkg/42168.
I suppose this should probably be pulled up to both -4 and -5...
 1.39.4.1 27-Jan-2010  bouyer Pull up following revision(s) (requested by dholland in ticket #1380):
bin/sh/cd.c: revision 1.40
bin/sh/sh.1: revision 1.95
bin/sh/sh.1: revision 1.97
Make the cd builtin accept and ignore -P, which is a kshism that has been
allowed to leak into POSIX and selects the behavior cd already implements.
Closes PR bin/42557 and also relevant to PR pkg/42168.
I suppose this should probably be pulled up to both -4 and -5...
Bump date for cd -P support.
 1.40.2.1 05-Mar-2011  bouyer Sync with HEAD
 1.41.2.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.46.2.1 07-Jan-2017  pgoyette Sync with HEAD. (Note that most of these changes are simply $NetBSD$
tag issues.)
 1.47.6.2 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.47.6.1 05-Jun-2017  snj Pull up following revision(s) (requested by kre in ticket #5):
bin/sh/cd.c: revision 1.48
bin/sh/eval.c: revision 1.141
bin/sh/exec.c: revision 1.48
bin/sh/exec.h: revision 1.25
bin/sh/mail.c: revisions 1.17, 1.18
bin/sh/sh.1: revision 1.147
Make cd (really) do cd -P, and not just claim that is what it is doing
while doing a half-hearted, broken, partial, version of cd -L instead.
The latter (as the manual says) is not supported, what's more, it is an
abomination, and should never be supported (anywhere.)
Fix the doc so that the pretense that we notice when a path given crosses
a symlink (and turns on printing of the destination directory) is claimed
no more (that used to be true until late Dec 2016, but was changed). Now
the print happens if -o cdprint is set, or if an entry from CDPATH that is
not "" or "." is used (or if the "cd dest repl" cd cmd variant is used.)
Fix CDPATH processing: avoid the magic '%' processing that is used for
PATH and MAILPATH from corrupting CDPATH. The % magic (both variants)
remains undocumented.
Also, don't double the '/' if an entry in PATH or CDPATH ends in '/'
(as in CDPATH=":/usr/src/"). A "cd usr.bin" used to do
chdir("/usr/src//usr.bin"). No more. This is almost invisible,
and relatively harmless, either way....
Also fix a bug where if a plausible destination directory in CDPATH
was located, but the chdir() failed (eg: permission denied) and then
a later "." or "" CDPATH entry succeeded, "print" mode was turned on.
That is:
cd /tmp; mkdir bin
mkdir -p P/bin; chmod 0 P/bin
CDPATH=/tmp/P:
cd bin
would cd to /tmp/bin (correctly) but print it (incorrectly).
Also when in "cd dest replace" mode, if the result of the replacement
generates '-' as the path named, as in:
cd $PWD -
then simply change to '-' (or attempt to, with CDPATH search), rather
than having this being equivalent to "cd -")
Because of these changes, the pwd command (and $PWD) essentially
always acts as pwd -P, even when called as pwd -L (which is still
the default.) That is, even more than it did before.
Also fixed a (kind of minor) mem management error (CDPATH related)
"whosoever shall padvance must stunalloc before repeating" (and the
same for MAILPATH).
--
If we are going to keep the MAILPATH % hack, then at least do something
rational. Since it isn't documented, what "rational" is is up for
discussion, but what it did before was not it (it was nonsense...).
 1.50.8.1 06-Nov-2021  martin Pull up following revision(s) (requested by kre in ticket #1372):

bin/sh/sh.1: revision 1.236 (patch)
bin/sh/cd.c: revision 1.51

PR bin/45390 - fix for folly four

In the pwd builtin, verify that curdir names '.' before
simply printing it. Never alter PWD or OLDPWD in the
pwd command.

Also while here, implement the (new: coming in POSIX, but has existed
for a while in several other shells) -e option to cd (with -e, cd -P
will exit(1) if the chdir() succeeds, but PWD cannot be discovered).
cd now prints the directory name used (if different from that given,
or cdprint is on) if interactive or (the new bit)in posix mode.

Some additional/changed comments added, and a DEBUG mode trace call
that was accidentally put inside an #if 0 block moved to where it
can do some good.

XXX pullup -9

PR bin/45390

Be explicit about what happens to PWD after a successful cd command.
Also be very clear that "cd" and "cd -P" are the same thing, and
the only cd variant implemented.
Also, when it is appropriate to print the new directory after a cd
command, note that it happens if interactive (as it always has here)
and also if the posix option is set (for POSIX compat, where "interactive"
is irrelevant). Mention that "cd -" is a case where the new directory
is printed (along with paths relative to a non-empty CDPATH entry,
and where the "cd old new" (string replacement in curdir) is used.

While here document the new -e option to cd.

XXX pullup -9
 1.50.6.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.50.6.1 21-Apr-2020  martin Sync with HEAD
 1.53.6.1 02-Sep-2025  martin Pull up following revision(s) (requested by kre in ticket #24):

bin/sh/cd.c: revision 1.54
bin/sh/cd.c: revision 1.55
bin/sh/cd.c: revision 1.56

PR standards/59565 - cd "" is now an error, not == cd .

POSIX 2024 requires that cd "" generate an error message,
and fail.

Until now shells (including this one) have typically equated
it to "cd ." as in the distant past, a lookup of "" as a path
name would return the current directory (aka ".").

From now on, we generate an error, as specified.
(For this commit, the error message is kind of pathetic,
but it is at least there - that will be fixed in a
subsequent commit.)


Improve error messages from cd builtin

Have cd indicate (particularly for when CDPATH is in use,
but also in other cases) which path name was attempted
and failed, and why it failed. For CDPATH uses, when
(if) the cd eventually fails, prefer the first entry in
which the requested destination path is located (when
the chdir() to it fails), or if there is none of those,
the first lookup which failed to determine if the path
exists there or not (ie: not ENOENT).

If the all attempts return ENOENT simply report the arg
given (after "cd -" and "cd a b" processing), as no
one CDPATH entry is more appropriate than any other.

Inspired by PR standards/59565 but not directly related.


PR bin/59602 PWD update failure message from cd fix.
Only complain about being unable to determine the new
directory name after a cd when -e is given (test for
that was forgotten).
 1.6 18-Jun-2011  christos branches: 1.6.44;
PR/45069: Henning Petersen: Use prototypes from builtins.h .
 1.5 10-Oct-2005  christos branches: 1.5.40;
PR/31548: Alexander Rigbo: $PWD not set when shell starts up, before pwd
or cd get executed.
 1.4 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.3 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.2 04-Jul-1997  christos Fix compiler warnings.
 1.1 19-Nov-1995  christos Fix PR/1760, where 'cd -' before any other command could cause a reference
to an uninitialized pointer. Use getcwd() to get the current working directory,
instead of forking /bin/pwd [per Scott's suggestion]
 1.5.40.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.6.44.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.6.44.1 21-Apr-2020  martin Sync with HEAD
 1.5 11-May-1994  mycroft Clean up deleted files.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.7 15-Jan-1995  mycroft Clean up deleted files.
 1.6 11-Jun-1994  mycroft Add RCS ids.
 1.5 11-May-1994  jtc sync with 4.4lite
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.7 15-Jan-1995  mycroft Clean up deleted files.
 1.6 11-Jun-1994  mycroft Add RCS ids.
 1.5 11-May-1994  jtc sync with 4.4lite
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.45 19-Mar-2023  kre Do a better job handling EACCES errors from exec() calls. If the
EACCES is from the namei(), treat it just like ENOENT or ENOTDIR
(and if that is the final error, the exit status from a failed exec
will be 127). If the EACCES is from the exec() itself, that indicates
the file to be run exists, but has no 'x' permission. That's a
meaningful error (as distinct from just "yet another PATH element
search failure").

While here, return the first meaingful error we encountered while
searching PATH, rather than the last (and ENOENT if there are none
of those).

This change results in some failed command executions returning status
127 now, where they returned 126 before - which better reflects the
intent of those values (127 is simply "not found" whereas 126 is "found
but couldn't be executed").

We still do nothing to distinguish errors encountered looking up the
command name give, with errors encountered (by the kernel) attempting to
run an interpreter needed for the exec to succeed (#! line path, or
/libexec/ld.elf_so and similar - or anything else of a similar nature).
 1.44 10-Nov-2021  kre DEBUG mode changes only. NFC (NC) for any normally compiled shell.

Mostly adding DEBUG mode tracing (when appropriate verbose tracing
is enabled generally) whenever a shell (including sushell) process
exits, so shells that the tracing should indicate why ehslls that
vanish did that.

Note for future investigators: if the relevant tracing is enabled,
and a (sub-)shell still simply seems to have vanished without trace,
the likely cause is that it was killed by a signal - and of those,
the most common that occurs is SIGPIPE.
 1.43 04-Feb-2019  kre 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).
 1.42 21-Jan-2019  kre 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".
 1.41 24-Jul-2017  kre branches: 1.41.2; 1.41.4;
PR bin/52348

Avoid a reference after free (detected by asan) - harmless here, but
easy to fix.
 1.40 05-Jul-2017  kre DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
 1.39 01-Jun-2016  kre branches: 1.39.8;

PR bin/47065 PR bin/39466

When the shell exits after an error (when that is the right thing for
it to do) ensure that it never does exit(0).
 1.38 15-Mar-2012  joerg Add __printflike attribution to use vprintf and friends with an argument
as format string.
 1.37 16-Oct-2008  dholland branches: 1.37.18;
Use "extern" properly for referencing globals defined in other modules.
Now builds cleanly with -warn-common.
 1.36 15-Feb-2008  matt Fix inconsistent definitions
 1.35 18-Dec-2007  dogcow This should have been all on one line anyway - and hey, it fixes my build
issue.
 1.34 15-Dec-2007  perry convert __attribute__s to applicable cdefs.h macros
 1.33 04-Oct-2006  christos branches: 1.33.8;
Prefix "Syntax Error" with the program name, if the command name is not
available. At least this way we get an idea of what program gives us the
message.
 1.32 02-Jan-2006  garbled Add error printing for ENAMETOOLONG. Partially addresses PR bin/13850 .
The actual bug mentioned in that PR no longer exists, but it does cause:
~~~~~: error 63
Which this fix cleans up.
 1.31 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.30 22-Jan-2003  dsl Support command -p, -v and -V as posix
Stop temporary PATH assigments messing up hash table
Fix sh -c -e "echo $0 $*" -a x (as posix)
(agreed by christos)
 1.29 16-Jan-2003  kleink Rename `sigset' locals to avoid symbol shadowing warning.
 1.28 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.27 27-Sep-2002  mycroft Clean up INTOFF/INTON usage a little -- none of fork{shell,parent,child}()
screw with them now, only their callers.
 1.26 27-Sep-2002  christos no SYSV for eagain, enable EMFILE
 1.25 25-May-2002  wiz __STDC__ is always defined on NetBSD, so remove #ifdef __STDC__ (and
unnecessary #else cases).
 1.24 15-May-2002  christos implement noclobber. From Ben Harris, with minor tweaks from me. Two
unimplemented comments to go. Go Ben!
 1.23 03-Jul-2000  matt include <stdlib.h>, <string.h>, or whatever as appropriate to shut up
gcc 2.96
 1.22 09-Jul-1999  christos compile with WARNS = 2
 1.21 05-Apr-1999  mycroft If we exit due to a SIGINT, do it with raise() so that the wait status is
correct in the parent.
 1.20 28-Jul-1998  mycroft branches: 1.20.2;
Be more retentive about use of NOTREACHED and noreturn.
 1.19 28-Jul-1998  mycroft Delint.
 1.18 31-Jan-1998  christos Include our local includes after the system's ones.
 1.17 04-Jul-1997  christos branches: 1.17.2;
Fix compiler warnings.
 1.16 11-Apr-1997  christos Use #ifdef __STDC__ instead of #if __STDC__
 1.15 16-Oct-1996  christos PR/287: Shell does not exit with 126/127 when permission denied/
command not found. Add extra exception type and generalize
error handling routines to take that exception type. Use
a global variable exerrno to keep the last exec error.
 1.14 11-May-1995  christos branches: 1.14.6;
Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.13 23-Mar-1995  mycroft Fix typo in previous.
 1.12 21-Mar-1995  mycroft Use sigprocmask(), not sigsetmask().
 1.11 21-Mar-1995  cgd convert to new RCS id conventions.
 1.10 05-Dec-1994  cgd clean up further. more patches from Jim Jegers
 1.9 04-Dec-1994  cgd from James Jegers <jimj@miller.cs.uwm.edu>: quiet -Wall, and squelch
some of the worst style errors.
 1.8 11-Jun-1994  mycroft Add RCS ids.
 1.7 12-May-1994  jtc Include appropriate header files to bring function prototypes into scope.
 1.6 11-May-1994  jtc sync with 4.4lite
 1.5 05-Sep-1993  mycroft From Vincent Broman:
error.c: Add , in initializer list if ENOLINK defined.
output.c: Gratuitously remove extra ;.
mknodes.c: main() should return a value.
Not from Vincent Broman:
mknodes.c: main() should return an error condition when approriate, too.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.14.6.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.17.2.1 08-May-1998  mycroft Sync with trunk, per request of christos.
 1.20.2.1 19-Apr-1999  mycroft Pull up rev 1.21:
If we exit due to a SIGINT, do it with raise() so that the wait status is
correct in the parent.
 1.33.8.2 23-Mar-2008  matt sync with HEAD
 1.33.8.1 09-Jan-2008  matt sync with HEAD
 1.37.18.1 17-Apr-2012  yamt sync with head
 1.39.8.1 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.41.4.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.41.4.2 21-Apr-2020  martin Sync with HEAD
 1.41.4.1 10-Jun-2019  christos Sync with HEAD
 1.41.2.1 26-Jan-2019  pgoyette Sync with HEAD
 1.25 21-Mar-2023  hannken Use "sigjmp_buf loc" after switch to sigsetjmp()/siglongjmp().

Fixes errors and aborts on sparc at least.
 1.24 19-Mar-2023  kre Switch from using _setjmp()/_longjmp() (on BSD systems which aren't SVR4)
(and setjmp()/longjmp() elsewhere) to using sigsetjmp()/siglongjmp()
everywhere.

NFCI.
 1.23 19-Mar-2023  kre Change a few #defines from octal to hex (pdp11 days are long gone).
Improve the layout of those definitions at the same time.

NFC.
 1.22 04-Feb-2019  kre 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).
 1.21 19-Aug-2018  kre 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)
 1.20 22-Jul-2018  kre 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.
 1.19 15-Mar-2012  joerg branches: 1.19.32; 1.19.34;
Add __printflike attribution to use vprintf and friends with an argument
as format string.
 1.18 23-Aug-2011  christos branches: 1.18.2;
add more gcc printf format attributes
 1.17 29-Mar-2008  apb If SHELL_BUILTIN is defined (as will be the case when building external
builtins such as the printf command), then hide a few declarations.

This allows the shell to build again, fixing a problem with
"error" being declared as a function here, and as a variable in
.../usr.bin/printf/printf.c.
 1.16 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.15 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.14 04-Feb-2001  christos remove redundant declarations and nexted externs.
 1.13 09-Jul-1999  christos compile with WARNS = 2
 1.12 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.11 15-Jul-1997  christos Don't re-define setjmp/longjmp in the svr4 case
 1.10 04-Jul-1997  christos Fix compiler warnings.
 1.9 16-Oct-1996  christos PR/287: Shell does not exit with 126/127 when permission denied/
command not found. Add extra exception type and generalize
error handling routines to take that exception type. Use
a global variable exerrno to keep the last exec error.
 1.8 11-May-1995  christos branches: 1.8.6;
Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.7 21-Mar-1995  cgd convert to new RCS id conventions.
 1.6 11-Jun-1994  mycroft Add RCS ids.
 1.5 11-May-1994  jtc sync with 4.4lite
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.8.6.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.18.2.1 17-Apr-2012  yamt sync with head
 1.19.34.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.19.34.2 21-Apr-2020  martin Sync with HEAD
 1.19.34.1 10-Jun-2019  christos Sync with HEAD
 1.19.32.2 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.19.32.1 28-Jul-2018  pgoyette Sync with HEAD
 1.197 11-Nov-2024  kre This commit is intended to be what was intended to happen in the
commit of Sun Nov 10 01:22:24 UTC 2024, see:

http://mail-index.netbsd.org/source-changes/2024/11/10/msg154310.html

The commit message for that applies to this one (wholly). I believe that
the problem with that version which caused it to be reverted has been found
and fixed in this version (a necessary change was made as part of one of
the fixes, but the side-effect implications of that were missed -- bad bad me.)

In addition, I found some more issues with setting close-on-exec on other
command lines

With:
func 3>whatever

fd 3 (anything > 2) got close on exec set. That makes no difference
to the function itself (nothing gets exec'd therefore nothing gets closed)
but does to any exec that might happen running a command within the function.

I believe that if this is done (just as if "func" was a regular command,
and not a function) such open fds should be passed through to anything
they exec - unless the function (or other command) takes care to close the
fd passed to it, or explicitly turn on close-on exec.

I expect this usage to be quite rare, and not make much practical difference.

The same applies do builtin commands, but is even less relevant there, eg:

printf 3>whatever

would have set close-on-exec on fd 3 for printf. This is generally
completely immaterial, as printf - and most other built-in commands -
neither uses any fd other than (some of) 0 1 & 2, nor do they exec anything.

That is, except for the "exec" built-in which was the focus of the original
fix (mentioned above) and which should remain fixed here, and for the "."
command.

Because of that last one (".") close-on-exec should not be set on built-in
commands (any of them) for redirections on the command line. This will
almost never make a difference - any such redirections last only as long
as the built-in command lasts (same with functions) and so will generally
never care about the state of close-on-exec, and I have never seen a use
of the "." command with any redirections other than stderr (which is unaffected
here, fd's <= 2 never get close-on-exec set). That's probably why no-one
ever noticed.

There are still "fd issues" when running a (non #!) shell script, that
are hard to fix, which we should probably handle the way most other shells
have, by simply abandoning the optimisation of not exec'ing a whole new
shell (#! scripts do that exec) and just doing it that way. Issues solved!
One day.
 1.196 10-Nov-2024  kre Revert the recent change until I can work out how things are broken.
 1.195 10-Nov-2024  kre exec builtin command redirection fixes

Several changes, all related to the exec special built in command,
or to close on exec, one way or another. (Except a few white space
and comment additions, KNF, etc)

1. The bug found by Edgar Fuß reported in:
http://mail-index.netbsd.org/tech-userlevel/2024/11/05/msg014588.html
has been fixed, now "exec N>whatever" will set close-on-exec for fd N
(as do ksh versions, and allowed by POSIX, though other shells do not)
which has happened now for many years. But "exec cmd N>whatever"
(which looks like the same command when redirections are processed)
which was setting close-on-exec on N, now no longer does, so fd N
can be passed to cmd as an open fd.

For anyone who cares, the big block of change just after "case CMDBUILTIN:"
in evalcommand() in eval.c is the fix for this (one line replaced by
about 90 ... though most of that is comments or #if 0'd example code
for later). It is a bit ugly, and will get worse if our exec command
ever gets any options, as others have, but it does work.

2. when the exec builtin utility is used to alter the shell's redirections
it is now "all or nothing". Previously the redirections were executed
left to right. If one failed, no more were attempted, but the earlier
ones remained. This makes no practical difference to a non-interactive
shell, as a redirection error causes that shell to exit, but it makes
a difference to interactive shells. Now if a redirection fails, any
earlier ones which had been performed are undone. Note however that
side-effects of redirections (like creating, or truncating, files in
the filesystem, cannot be reversed - just the shell's file descriptors
returned to how they were before the error).

Similarly usage errors on exec now exist .. our exec takes no options
(but does handle "--" as POSIX says it must - has done for ages).
Until now, that was the only magic piece of exec, running
exec -a name somecommand
(which several other shells support) would attempt to exec the "-a"
command, and most likely fail, causing immediate exit from the shell.
Now that is a usage error - a non-interactive shell still exits, as
exec is a special builtin, and any error from a special builtin causes
a non-interactive shell to exit. But now, an interactive shell will
no longer exit (and any redirections that were on the command will be
undone, the same as for a redirection error).

3. When a "close on exec" file descriptor is temporarily saved, so the
same fd can be redirected for another command (only built-in commands
and functions matter, redirects for file system commands happen after
a fork() and at that stage if anything goes wrong, the child simply
exits - but for non-forking commands, doing something like printf >file
required the previous stdout to be saved elsewhere, "file" opened to
be the new stdout, then when printf is finished, the old stdout moved
back. Anyway, if the fd being moved had close on exec set, then
when it was moved back, the close on exec was lost. That is now fixed.

4. The fdflags command no longer allows setting close on exec on stdin,
stdout, or stderr - POSIX requires that those 3 fd's always be open
(to something) when any normal command is invoked. With close-on-exec
set on one of these, that is impossible, so simply refuse it (when
"exec N>file" sets close on exec, it only does it for N>2).

Minor changes (should be invisible)

a. The shell now keeps track of the highest fd number it sees doing
normal operations (there are a few internal pipe() calls that aren't
monitored and a couple of others, but in general the shell will now
know the highest fd it ever saw allocated to it). This is mostly
for debugging.

b. calls to fcntl() passing an int as the "arg" are now all properly
cast to the void * that the fcntl kernel is expecting to receive.
I suspect that makes no actual difference to anything, but ...
 1.194 21-Oct-2024  kre Add function names to relevant error messages.

When a shell detected error occurs while executing a function,
include the name of the function currently being executed in
the error message.
 1.193 03-Aug-2024  kre Change the "string" argument to evalstring() and setinputstring()
from being "char *" to being "const char *".

This is needed for a forthcoming change which needs to pass a const char *
to evalstring (and through it to setinputstring) and be assured that
nothing will alter the characters in the string supplied.

This is (aside from the additional compile time protection provided)
a no-op change, all evalstring() does with its string is pass it to
setinputstring() and all that does with it is determine its length
(strlen() which expects a const char *) and assign the string pointer
to parsenextc which is already a const char * - there never has been
any reason for these two functions to not include the "const" in
the arg declaration -- except that when originally written (early
1990's) I suspect "const" either didn't exist at all, or wasn't
supported by relevant compilers.

NFCI. Most probably (though I didn't check) no binary change at all.
 1.192 15-Jun-2024  kre branches: 1.192.2;
POSIX.1-2024 requires that when an async (background) job is started
at the top level (ie: not in any kind of subshell environment) of an
interactive shell, that the shell print the job number assigned, and
the process id of the lead (or only) process in the job, in the form:

[JN] pid

Make that happen. (Other shells have been doing this for ages).
 1.191 25-Dec-2023  kre PR bin/57773

Fix a bug reported by Jarle Fredrik Greipsland in PR bin/57773,
where a substring expansion where the substring to be removed from
a variable expansion is itself a var expansion where the value
contains one (or more) of sh's CTLxxx chars - the pattern had
CTLESC inserted, the string to be matched against did not. Fail.
We fix that by always inserting CTLESC in var assign expansions.
See the PR for all the gory details.

Thanks for the PR.

XXX pullup to everything.
 1.190 24-Jun-2023  msaitoh Fix typo in a debug message.
 1.189 07-Apr-2023  kre The great shell trailing whitespace cleanup of 2023...
Inspired by private e-mail comments from mouse@

NFCI.
 1.188 05-Jan-2022  kre branches: 1.188.2;

Use a volative local shadow of a field in an (on-stack) non-volatile struct
that is to be referenced after a return from setjmp() via longjmp().

This doesn't ever seem to have caused a problem, but I think using
volative vars is required here.

For reasons I never bothered to discover, even though this change
certainly requires a store into stack memory which wasn't required
before, earlier measurements showed the shell getting (slightly) smaller
with this change in place.

NFCI
 1.187 05-Dec-2021  msaitoh s/commmand/command/ in comment.
 1.186 22-Nov-2021  kre PR bin/53550

Here we go again... One more time to redo how here docs are
processed (it has been a few years since the last time!)

This is actually a relatively minor change, mostly to timimg
(to just when things happen). Now here docs are expanded at the
same time the "filename" word in a redirect is expanded, rather than
later when the heredoc was being sent to its process. This actually
makes things more consistent - but does break one of the ATF tests
which was testing that we were (effectively) internally inconsistent
in this area.

Not all shells agree on the context in which redirection expansions
should happen, some make any side effects visible to the parent shell
(the majority do) others do the redirection expansions in a subshell
so any side effcts are lost. We used to have a foot in each camp,
with the majority for everything but here docs, and the minority for
here docs. Now we're all the way with LBJ ... (or something like that).
 1.185 16-Nov-2021  kre Detect write errors to stdout, and exit(1) from some built-in
commands which (primarily) are used just to generate output
(or with a particular option combination do so).
 1.184 16-Nov-2021  kre Fix value of ${LINENO} in "for" commands.

This affects (as best I can tell) only uses of ${LINENO} in PS4
when -x is enabled (and perhaps only when the list contains no
expansions). "for" like "case" (which was already handled) is
special in that it generates trace output before actually executing
any kind of simple command.
 1.183 10-Nov-2021  kre DEBUG mode changes only. NFC (NC) for any normally compiled shell.

Mostly adding DEBUG mode tracing (when appropriate verbose tracing
is enabled generally) whenever a shell (including sushell) process
exits, so shells that the tracing should indicate why ehslls that
vanish did that.

Note for future investigators: if the relevant tracing is enabled,
and a (sub-)shell still simply seems to have vanished without trace,
the likely cause is that it was killed by a signal - and of those,
the most common that occurs is SIGPIPE.
 1.182 04-Apr-2021  kre Related to PR bin/48875

Correct an issue found by Oguz <oguzismailuysal@gmail.com> and reported
in e-mail (on the bug-bash list initially!) with the code changed to deal
with PR bin/48875

With:

sh -c 'echo start at $SECONDS;
(sleep 3 & (sleep 1& wait) );
echo end at $SECONDS'

The shell should say "start at 0\nend at 1\n", but instead (before
this fix, in -9 and HEAD, but not -8) does "start at 0\nend at 3\n"
(Not in -8 as the 48875 changes were never pulled up)>

There was an old problem, fixed years ago, which cause the same symptom,
related to the way the jobs table was cleared (or not) in subshells, and
it seemed like that might have resurfaced.

But not so, the issue here is the sub-shell elimination, which was part
of the 48875 "fix" (not really, it wasn't really a bug, just sub-optimal
and unexpected behaviour).

What the shell actually has been running in this case is:

sh -c 'echo start at $SECONDS;
(sleep 3 & sleep 1& wait );
echo end at $SECONDS'

as the inner subshell was deemed unnecessary - all its parent would
do is wait for its exit status, and then exit with that status - we
may as well simply replace the current sub-shell with the new one,
let it do its thing, and we're done...

But not here, the running "sleep 3" will remain a child of that merged
sub-shell, and the "wait" will thus wait for it, along with the sleep 1
which is all it should be seeing.

For now, fix this by not eliminating a sub-shell if there are existing
unwaited upon children in the current one. It might be possible to
simply disregard the old child for the purposes of wait (and "jobs", etc,
all cmds which look at the jobs table) but the bookkeeping required to
make that work reliably is likely to take some time to get correct...

Along with this fix comes a fix to DEBUG mode shells, which, in situations
like this, could dump core in the debug code if the relevant tracing was
enabled, and add a new trace for when the jobs table is cleared (which was
added predating the discovery of the actual cause of this issue, but seems
worth keeping.) Neither of these changes have any effect on shells
compiled normally.

XXX pullup -9
 1.181 20-Aug-2020  kre Be less conservative about when we do clear_traps() when we have
traps_invalid (that is, when we actually nuke the parent shell's
caught traps in a subshell). This allows more reasonable use of
"trap -p" (and similar) in subshells than existed before (and in
particular, that command can be in a function now - there can also
be several related commands like
traps=$(trap -p INT; trap -p QUIT; trap -p HUP)
A side effect of all of this is that
(eval "$(trap -p)"; ...)
now allows copying caught traps into a subshell environment, if desired.

Also att the ksh93 variant (the one not picked by POSIX as it isn't
generally as useful) of "trap -p" (but call it "trap -P" which extracts
just the trap action for named signals (giving more than one is usually
undesirable). This allows
eval "$(trap -P INT)"
to run the action for SIGINT traps, without needing to attempt to parse
the "trap -p" output.
 1.180 14-May-2020  msaitoh Remove extra semicolon.
 1.179 23-Apr-2020  kre Stop forcing the -e option off in the subshell createds for a command
substitution. This was inherited in the big "-e" fixup patch set (rev 1.50)
of Jan 2000, which came from dash. dash no longer acts this way.
 1.178 04-Feb-2020  kre After bug report 262 (from 2010)
https://austingroupbugs.net/view.php?id=252
the Austin Group decided to require processing of "--" by the "."
and "exec" commands to solve a problem where some shells did
option processing for those commands (permitted) and others did
not (also permitted) which left no safe way to process a file
with a name beginning with "-".

This has finally made its way into what will be the next version of
the POSIX standard.

Since this shell did no option processing at all for those commands,
we need to update. This is that update.

The sole effect is that a "--" 'option' (to "." or "exec") is ignored.
This means that if you want to use "--" as the arg to one of those
commands, it needs to be given twice ". -- --". Apart from that there
should be no difference at all (though the "--" can now be used in other
situations, where we did not require it before, and still do not).
 1.177 21-Dec-2019  kre Use fork() rather than vfork() when forking to run a background
process with redirects. If we use vfork() and a redirect hangs
(eg: opening a fifo) which the parent was intended to unhang,
then the parent never gets to continue to unhang the child.

eg: mkfifo f; cat <f &; echo foo>f

The parent should not be waiting for a background process, even
just for its exec() to complete. if there are no redirects there
is (should be) nothing left that might be done that will cause any
noticeable delay, so vfork() should be safe in all other cases.
 1.176 09-Dec-2019  kre PR bin/54743

Having traps set should not enforce a fork for the next command,
whatever that command happens to be, only for commands which would
normally fork if they weren't the last command expected to be
executed (ie: builtins and functions shouldn't be exexuted in a
sub-shell merely because a trap is set).

As it was (for example)
trap 'whatever' SIGANY; wait $anypid
was guaranteed to fail the wait, as the subshell it was executed
in could not have any children.

XXX pullup -9
 1.175 04-May-2019  kre branches: 1.175.2;
When a return occurs in the test part of a loop statement (while/until)
(inside a function or dot script) the exit status of that return
statement should become the exit status of the function (or dot
script) - we were ignoring it,

That is
fn() { while return 7; do return 9; done; return 11; }
should exit with status 7. It was exiting 0.

This is apparently another old ash bug that has been fixed
everywhere else in the past.

Issue pointed out by Martijn Dekker, (fairly obvious) fix borrowed
from FreeBSD, due for return sometime next century.
 1.174 09-Feb-2019  kre DEBUG mode build changes - add extra trace output.

NFC for any normal shell build.
 1.173 09-Feb-2019  kre Delete extern decl for trap[] - hasn't been needed for a while now.
 1.172 09-Feb-2019  kre 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.
 1.171 04-Feb-2019  kre 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).
 1.170 21-Jan-2019  kre 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.
 1.169 09-Jan-2019  kre 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).
 1.168 03-Dec-2018  kre 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).
 1.167 03-Dec-2018  kre 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()
 1.166 03-Dec-2018  kre 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.
 1.165 30-Nov-2018  kre 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
 1.164 26-Nov-2018  kre 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.
 1.163 23-Nov-2018  kre 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
 1.162 09-Oct-2018  kre 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)]
 1.161 25-Aug-2018  kre 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.
 1.160 22-Aug-2018  kre 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.
 1.159 19-Aug-2018  kre 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)
 1.158 19-Aug-2018  kre 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
 1.157 14-Aug-2018  kre 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
 1.156 25-Jul-2018  kre 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
 1.155 22-Jun-2018  kre branches: 1.155.2;
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).
 1.154 17-Jun-2018  kre NFC: correct typo in a comment.
 1.153 19-Nov-2017  kre branches: 1.153.2;
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.
 1.152 29-Sep-2017  kre DEBUG only changes (non-debug, ie: normal, shell unaffected)
Add a little extra info in a few of the trace messages.
 1.151 30-Jun-2017  kre Include redirections in trace output from "set -x"
 1.150 19-Jun-2017  kre Another fix from FreeBSD (this one from April 2009).

When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.

That is:
sh -c 'false

'
echo $?
should produce 1, not 0.
 1.149 19-Jun-2017  kre Fix from FreeBSD (applied there in July 2008...)

Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
 1.148 17-Jun-2017  kre NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
 1.147 17-Jun-2017  kre Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).

Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)

From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
 1.146 08-Jun-2017  kre Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
 1.145 08-Jun-2017  kre I am an idiot... revert the previous unintended commit.
 1.144 08-Jun-2017  kre Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
 1.143 07-Jun-2017  kre A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)

This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)

The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.

This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).

I would not call the results of this perfect yet, but it is close.
 1.142 07-Jun-2017  kre An initial attempt at implementing LINENO to meet the specs.

Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.

Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)

This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.

POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)

This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.

This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).

Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
 1.141 04-Jun-2017  kre Make cd (really) do cd -P, and not just claim that is what it is doing
while doing a half-hearted, broken, partial, version of cd -L instead.
The latter (as the manual says) is not supported, what's more, it is an
abomination, and should never be supported (anywhere.)

Fix the doc so that the pretense that we notice when a path given crosses
a symlink (and turns on printing of the destination directory) is claimed
no more (that used to be true until late Dec 2016, but was changed). Now
the print happens if -o cdprint is set, or if an entry from CDPATH that is
not "" or "." is used (or if the "cd dest repl" cd cmd variant is used.)

Fix CDPATH processing: avoid the magic '%' processing that is used for
PATH and MAILPATH from corrupting CDPATH. The % magic (both variants)
remains undocumented.

Also, don't double the '/' if an entry in PATH or CDPATH ends in '/'
(as in CDPATH=":/usr/src/"). A "cd usr.bin" used to do
chdir("/usr/src//usr.bin"). No more. This is almost invisible,
and relatively harmless, either way....

Also fix a bug where if a plausible destination directory in CDPATH
was located, but the chdir() failed (eg: permission denied) and then
a later "." or "" CDPATH entry succeeded, "print" mode was turned on.
That is:
cd /tmp; mkdir bin
mkdir -p P/bin; chmod 0 P/bin
CDPATH=/tmp/P:
cd bin
would cd to /tmp/bin (correctly) but print it (incorrectly).

Also when in "cd dest replace" mode, if the result of the replacement
generates '-' as the path named, as in:
cd $PWD -
then simply change to '-' (or attempt to, with CDPATH search), rather
than having this being equivalent to "cd -")

Because of these changes, the pwd command (and $PWD) essentially
always acts as pwd -P, even when called as pwd -L (which is still
the default.) That is, even more than it did before.

Also fixed a (kind of minor) mem management error (CDPATH related)
"whosoever shall padvance must stunalloc before repeating" (and the
same for MAILPATH).
 1.140 13-May-2017  kre branches: 1.140.2;

The beginnings of the great shell DEBUG (tracing) upgrade of 2017...

First, be aware that the DEBUG spoken of here has nothing whatever to
do with MKDEBUG=true type builds of NetBSD. The only way to get a
DEBUG shell is to build it yourself manually.

That said, for non-DEBUG shells, this change makes only one slight
(trivial really) difference, which should affect nothing.

Previously some code was defined like ...

function(args)
{
#ifdef DEBUG
/* function code goes here */
#endif
}

and called like ...

#ifdef DEBUG
function(params);
#endif

resulting in several empty functions that are never called being
defined in non-DEBUG shells. Those are now gone. If you can detect
the difference any way other than using "nm" or similar, I'd be very
surprised...

For DEBUG shells, this introduces a whole new TRACE() setup to use
to assist in debugging the shell.

I have had this locally (uncommitted) for over a year... it helps.

By itself this change is almost useless, nothing really changes, but
it provides the framework to allow other TRACE() calls to be updated
over time. This is why I had not committed this earlier, my previous
version required a flag day, with all the shell's internal tracing
being updated a once - which I had done, but that shell version has
bit-rotted so badly now it is almost useless...

Future updates will add the mechanism to allow the new stuff to actually
be used in a productive way, and following that, over time, gradual
conversion of all the shell tracing to the updated form (as required,
or when I am bored...)

The one useful change that we do get now is that the fd that the shell
uses for tracing (which was usually 3, but not any more) is now protected
from user/script interference, like all the other shell inernal fds.

There is no doc (nor will there be) on any of this, if you are not reading
the source code it is useless to you, if you are, you know how it works.
 1.139 09-May-2017  kre If we are going to permit
! ! pipeline
(And for now the other places where ! is permitted)
we should at least generate the logically correct exit
status:
! ! (exit 5); echo $?
should print 1, not 5. ksh and bosh do it this way - and it makes sense.
bash and the FreeBSD sh echo "5" (as did we until now.)
dash, zsh, yash all enforce the standard syntax, and prohibit this.
 1.138 09-May-2017  kre Remove a now unnecessary (ater the changes in 1.136) clearing of EV_EXIT.
(NFC, but should save a byte or two of code space.)
 1.137 09-May-2017  kre NFC: whitespace (indentation).
 1.136 09-May-2017  kre Fix some bogus usage of EV_EXIT in evaltree(). Fix (somewhat) inspired
by FreeBSD sh (though different, for other reasons) - but the bug discovered
while searching for why a (nonsense) attempted test of the forthcoming
code to handle "! ! pipeline" properly wasn't working... (it was how I was
testing it that was broken, but until I achieved enlightenment, I was bug
hunting, and found this...)

Most likely the bugs here wouldn't have affected any real code (no bug
reports anyway), but ...
 1.135 07-May-2017  kre POSIX says that the arg to break or continue is to be a positive integer
(by which they mean > 0). We were checking for negative numbers, but
not for 0. More by chance of the implementation than any specific design
(I suspect) "break 0" was being treated the same as "break" or "break 1".
Since 3 ways to achieve the same thing is overkill, let's do what posix
wants and forbid "break 0" and "continue 0".
 1.134 04-May-2017  kre Implement the ';&' (used instead of ';;') case statement list terminator
which causes fall through the to command list of the following pattern
(wuthout evaluating that pattern). This has been approved for inclusion
in the next major version of the POSIX standard (Issue 8), and is
implemented by most other shells.

Now all form a circle and together attempt to summon the great wizd
in the hopes that his magic spells can transform the poor attempt
at documenting this feature into something rational...
 1.133 03-May-2017  kre So sayeth posix (of the special builtin "eval"):
If there are no arguments, or only null arguments,
eval shall return a zero exit status;

Make it so. Now:
false; eval; echo $?
produces 0 instead of 1.
 1.132 03-May-2017  kre Correct a dsl comment relating to setting $_ - I thought I had done
this ages ago, but apparently not...
 1.131 22-Apr-2017  kre branches: 1.131.2;

When -x is set, show assignments to the loop variable in a for loop.
 1.130 02-Feb-2017  christos Who Ride Wit Us?
 1.129 10-Jan-2017  christos branches: 1.129.2;
add missing <sys/stat.h>
 1.128 01-Jun-2016  kre branches: 1.128.2;

PR bin/43639

Redo earlier fix to only prohibit sourcing directories and block special files.
char specials (/dev/tty, /dev/null, ... incl /dev/rwd0a) and fifos are OK.

Posix actually requires that we find only readable files - that is not yet
implemented (doing it sanely, without opening the file twice, is going to
take some more modifications to code elsewhere).
 1.127 13-May-2016  kre More fallout from the fix for PR bin/48875 - this one found just by
code reading, rather than any actual real use case failing.

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

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

what should be output is

hello
hello
hello

foo= @a @b @c

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

hello
@b
hello
@c
hello

foo= @a

This fixes that. I think (hope) this is the last of these fixes...
 1.126 10-May-2016  kre PR bin/48875 - minor correction (well, not so minor) - commands in loops
must be assumed to have something following, even if the loop itself doesn't,
so redirected fd's around func calls need to be saved. Should fix etcupdate
 1.125 09-May-2016  kre PR bin/48875 - avoid holding (replaced) file descriptors open when running a
command in the current shell (so they can be restored for the next command)
in cases where it is obvious that there is not going to be a following
command to use them. This fixes the problem reported in the PR (though
there are still plenty of situations where a FD could be closed but isn't,
we do not do full fd flow eveluation to determine whether a fd will be
used or not).

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

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

OK christos@
 1.122 03-May-2016  kre Fix things so that STATIC can me made static (-DSTATIC=static)
and have the shell still compile, link, and run...

ok christos@
 1.121 03-May-2016  kre PR bin/43639 - check that a file being read by the '.' command
is a regular file, even when it is given as a full pathname.
 1.120 02-May-2016  christos Fix handing of user file descriptors outside the 0..9 range.
Also, move (most of) the shell's internal use fd's to much
higher values (depending upon what ulimit -n allows) so they
are less likely to clash with user supplied fd numbers. A future
patch will (hopefully) avoid this problem completely by dynamically
moving the shell's internal fds around as needed. (From kre@)
 1.119 16-Mar-2016  christos Keep redirs for subshells.
 1.118 13-Mar-2016  christos We want this to work too:
$ cat sep1
#!/bin/sh
{ ./sep2; } 3>out

$ cat sep2
#!/bin/sh
echo sep2 >&3

$ ./sep1
 1.117 12-Mar-2016  christos Don't close-on-exec redirections created explicitly for the command being
ran; i.e. we want this to work:
$ cat succ1
#!/bin/sh
./succ2 6>out

$ cat succ2
#!/bin/sh
echo succ2 >&6

$ ./succ1

And this to fail:
$ cat fail1
#!/bin/sh
exec 6> out
echo "fail1" >&6
./fail2
exec 6>&-

$ cat fail2
#!obj.amd64/sh
echo "fail2" >&6

$ ./fail1
./fail2: 6: Bad file descriptor

XXX: Do we want a -k (keep flag on exec to make redirections not close-on-exec?
 1.116 12-Mar-2016  christos Improve quoting in the output from sh -x - use less unnecessary
quotes ('_' and '.' do not need quoting) and never quote the '=' in
an assignment (or it would not be one.) From kre, with some refactoring
to be blamed to me.
 1.115 29-Feb-2016  christos Complete implementation of the noexec option (-n) including
disabling noexec, if the shell is interactive, each time that
a new command is about to be read. Also correct the -I
(ignoreeof) option so that it only applies to interactive shells,
as required by posix. (from kre)
 1.114 27-Feb-2016  christos Improve debugging, from kre (I hooked it to the build).
 1.113 24-Feb-2016  christos PR/46327: David Mandelberg: Fix exit codes of background jobs (from kre)
 1.112 22-Feb-2016  christos PR/43255: Make -n apply to the -c string so sh -n -c 'commands' works
as it should. Also, other places where the shell parses strings of
commands are also now controlled by -n (traps, eval, ...) (from kre)
 1.111 04-Jan-2016  christos Don't leak redirected rescriptors to exec'ed processes. This is what ksh
does, but bash does not. For example:

$ cat test1
#!/bin/sh
exec 6> out
echo "test" >&6
sh ./test2
exec 6>&-
$ cat test2
echo "test2" >&6
$ ./test1
./test2: 6: Bad file descriptor

This fixes by side effect the problem of the rc system leaking file descriptors
7 and 8 to all starting daemons:

$ fstat -p 1359
USER CMD PID FD MOUNT INUM MODE SZ|DV R/W
root powerd 1359 wd / 2 drwxr-xr-x 512 r
root powerd 1359 0 / 63029 crw-rw-rw- null rw
root powerd 1359 1 / 63029 crw-rw-rw- null rw
root powerd 1359 2 / 63029 crw-rw-rw- null rw
root powerd 1359 3* kqueue pending 0
root powerd 1359 4 / 64463 crw-r----- power r
root powerd 1359 7 flags 0x80034<ISTTY,MPSAFE,LOCKSWORK,CLEAN>
root powerd 1359 8 flags 0x80034<ISTTY,MPSAFE,LOCKSWORK,CLEAN>
root powerd 1359 9* pipe 0xfffffe815d7bfdc0 -> 0x0 w

Note fd=7,8 pointing to the revoked pty from the parent rc process.
 1.110 02-Jan-2015  christos Define an undocumented -F option to only use fork instead of vfork for
debugging purposes.
 1.109 31-May-2014  christos PR/48843: Jarmo Jaakkola: dot commands mess up scope nesting tracking

Evaluation of commands goes completely haywire if a file containing
a break/continue/return command outside its "intended" scope is sourced
using a dot command inside its "intended" scope. The main symptom is
not exiting from the sourced file when supposed to, leading to evaluation
of commands that were not supposed to be evaluated. A secondary symptom
is that these extra commands are not evaluated correctly, as some of them
are skipped. Some examples are listed in the How-To-Repeat section.

According to the POSIX standard, this is how it should work:
dot:
The shell shall execute commands from the file in the current
environment.
break:
The break utility shall exit from the smallest enclosing for, while,
or until loop, [...]
continue:
The continue utility shall return to the top of the smallest
enclosing for, while, or until loop, [...]
return:
The return utility shall cause the shell to stop executing
the current function or dot script. If the shell is not currently
executing a function or dot script, the results are unspecified.

It is clear that return should return from a sourced file, which
it does not do. Whether break and continue should work from the sourced
file might be debatable. Because the dot command says "in the current
environment", I'd say yes. In any case, it should not fail in weird
ways like it does now!

The problems occur with return (a) and break/continue (b) because:
1) dotcmd() does not record the function nesting level prior to
sourcing the file nor does it touch the loopnest variable,
leading to either
2 a) returncmd() being unable to detect that it should not set
evalskip to SKIPFUNC but SKIPFILE, or
b) breakcmd() setting evalskip to SKIPCONT or SKIPBREAK,
leading to
3) cmdloop() not detecting that it should skip the rest of
the file, due to only checking for SKIPFILE.
The result is that cmdloop() keeps executing lines from the file
whilst evalskip is set, which is the main symptom. Because
evalskip is checked in multiple places in eval.c, the secondary
symptom appears.
>How-To-Repeat:
Run the following script:

printf "break\necho break1; echo break2" >break
printf "continue\necho continue1; echo continue2" >continue
printf "return\necho return1; echo return2" >return

while true; do . ./break; done

for i in 1 2; do . ./continue; done

func() {
. ./return
}
func

No output should be produced, but instead this is the result:
break1
continue1
continue1
return1

The main symptom is evident from the unexpected output and the secondary
one from the fact that there are no lines with '2' in them.
>Fix:
Here is patch to src/bin/sh to fix the above problems. It keeps
track of the function nesting level at the beginning of a dot command
to enable the return command to work properly.

I also changed the undefined-by-standard functionality of the return
command when it's not in a dot command or function from (indirectly)
exiting the shell to being silently ignored. This was done because
the previous way has at least one bug: the shell exits without asking
for confirmation when there are stopped jobs.

Because I read the standard to mean that break and continue should have
an effect outside the sourced file, that's how I implemented it. For what
it's worth, this also seems to be what bash does. Also laziness, because
this way required no changes to loopnesting tracking. If this is not
wanted, it might make sense to move the nesting tracking to the inputfile
stack.

The patch also does some clean-up to reduce the amount of global
variables by moving the dotcmd() and the find_dot_file() functions from
main.c to eval.c and making in_function() a proper function.
 1.108 26-Jan-2014  christos branches: 1.108.2;
explain why forks fail
 1.107 27-Jun-2013  yamt fix descriptor leaks. PR/47805

this fix was taken from FreeBSD SVN rev 199953 (Jilles Tjoelker)
------------------------------------------------------------------------
r199953 | jilles | 2009-11-30 07:33:59 +0900 (Mon, 30 Nov 2009) | 16 lines

Fix some cases where file descriptors from redirections leak to programs.

- Redirecting fds that were not open before kept two copies of the
redirected file.
sh -c '{ :; } 7>/dev/null; fstat -p $$; true'
(both fd 7 and 10 remained open)
- File descriptors used to restore things after redirection were not
set close-on-exec, instead they were explicitly closed before executing
a program normally and before executing a shell procedure. The latter
must remain but the former is replaced by close-on-exec.
sh -c 'exec 7</; { exec fstat -p $$; } 7>/dev/null; true'
(fd 10 remained open)

The examples above are simpler than the testsuite because I do not want to
use fstat or procstat in the testsuite.
 1.106 02-Mar-2013  christos PR/47608: Robert Elz: ``var=value func-call'' does not export var in the
function (+FIX)
 1.105 02-Jan-2013  dsl include limits.h for CHAR_MIN
 1.104 14-Jun-2012  joerg branches: 1.104.2;
Make sure temp_path is always initialised, even if mklocal fails.
Make sure to restore localvars, even if possibly leaking memory.
Discussed with christos@
 1.103 14-Nov-2011  christos PR/45613: Aleksey Cheusov: /bin/sh: 'set -e' + 'if eval false' problem
Fixed from: http://www.freebsd.org/cgi/query-pr.cgi?pr=134881&cat=
 1.102 31-Aug-2011  plunky branches: 1.102.2;
NULL does not need a cast
 1.101 17-Feb-2011  pooka Tell copyfd if the caller wants the exact tofd to just fd >= tofd.
Fixes "echo foo > /rump/bar" in a rump hijacked shell.

reviewed by christos
 1.100 03-Jun-2010  christos branches: 1.100.2;
need errno for the debug build.
 1.99 03-Jun-2010  christos set -e is supposed to work inside eval; skip EV_TESTED.
 1.98 07-Oct-2009  christos only for when trap if we are going to exit.
 1.97 06-Oct-2009  christos fix regression exit1: Don't exec the last command in a subshell if it has
trap[0] (trap EXIT) set. Fork instead to give the shell a chance to execute
the trap when it is done.
 1.96 19-Jan-2009  christos Revert previous commit that fixes PR/36079 (shell misses exit trap), because
the fix causes $! to point to the wrong process in pipelines, which is worse.
 1.95 21-Dec-2008  christos PR/36079: M. Levinson: Disable the optimization of not forking for the last
command in a subshell, otherwise we miss the exit trap.
 1.94 31-Oct-2008  christos show better quoting output for sh -x, from Aleksey Cheusov
 1.93 26-May-2008  tron Revert revisions 1.91 and 1.92. The POSIX spec about the correct behaviour
is contradictory at best. And these changes seem to cause more problems
that they are worth.
 1.92 24-May-2008  tron Fix two more cases of bad handling of "set -e":
- false && false
- false || false
 1.91 24-May-2008  tron Fix another problem with "set -e": "! true" should terminate the shell.
 1.90 24-May-2008  tron Port revision 1.44 of "src/bin/sh/eval.c" from FreeBSD to fix PR bin/38584.
Reviewed by Michael van Elst.
 1.89 15-Feb-2008  matt branches: 1.89.4; 1.89.6;
Fix inconsistent definitions
 1.88 16-Oct-2006  christos branches: 1.88.2; 1.88.4; 1.88.8;
sprinkle volatile.
 1.87 13-May-2006  christos Coverity CID 3384: Don't close -1.
 1.86 18-Apr-2006  christos PR/33281: Martin J. Laubach: Prevent core-dump on "echo abc | { }". bash
prints and error and ksh prints nothing. We go the ksh way.
 1.85 17-Mar-2006  christos Coverity CID 2479: Clarify confusion about uninitialized variable in the
presence of setjmp/vfork.
 1.84 23-Jun-2005  christos Revert part of the previous commit. We cannot fix the problem by not waiting.
The problem is that the subshell code is not doing redirections properly.
 1.83 22-Jun-2005  christos Don't wait for a background job in a subshell when we are set to EV_EXIT.
While I am here, call forkshell() explicitly FORK_FOO flags instead of
depending in FORK_FG == 0 and FORK_BG == 1.
 1.82 01-Jun-2005  lukem Mark temp_path volatile so that it won't get clobbered after longjmp.
(Also appeases gcc -Wuninitialized.)
 1.81 02-Mar-2005  dsl branches: 1.81.2;
Fix printing of invalid commandname after certain types of errors on builtins.
Fixes bug bin/29410 in head.
All of /bin/sh needs pulling up into 2.0
 1.80 30-Oct-2004  christos Pass WARNS=3
 1.79 30-Jun-2004  mycroft Make "set -e" once again provide the behavior documented in the man page,
which was unnecessarily changed in revision 1.50 while fixing other bugs.
That is, exit the shell if the last command in a || or && compound statement
is not short-circuited, and exits with a false status. I.e., the following
will cause the shell to exit:

set -e
false || false

While this is not the prescribed behavior in SUSv3, it is what our man page
documents, and it is what all of the following implementations do:

NetBSD /bin/ksh (pdksh)
bash
zsh
Solaris 9 /bin/sh
Solaris 9 /usr/xpg4/bin/sh
Solaris 9 /usr/bin/ksh
Tru64 /bin/sh
HP/UX 11 /bin/sh

The "standard" seems to be wrong in this instance.
 1.78 26-Jun-2004  dsl Correctly apply IFS to unquoted text in ${x-text}.
Fixes PR/26058 and the 'for i in ${x-a b c}; do ...' and ${x-'a b' c}.
I can't find a PR for the latter problem.
Regression test goind in shortly.
 1.77 26-Jun-2004  dsl No functional changes (intended).
Rename some variables, add some comments, and restructure a little.
In preparation for fixing "set ${x-a b c}" and friends.
 1.76 30-Apr-2004  dsl Ensure that fd 0, 1 and 2 are not used for the local end of pipelines.
Fixes PR bin/25395
 1.75 14-Nov-2003  dsl Add '\n' to "fork failed" trace messages.
 1.74 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.73 13-Jul-2003  itojun use bounded string op
 1.72 23-Jan-2003  agc Make this build on platforms where size_t != int, i.e. sparc, arm, ppc, ...
 1.71 23-Jan-2003  rafal Make this build again.
 1.70 22-Jan-2003  dsl Support command -p, -v and -V as posix
Stop temporary PATH assigments messing up hash table
Fix sh -c -e "echo $0 $*" -a x (as posix)
(agreed by christos)
 1.69 25-Nov-2002  agc Include <stdio.h> to get the prototype for sprintf(3) - macppc needs this.
 1.68 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.67 23-Oct-2002  christos From David Laight
> The wrong process is aborting when variable assignment fails
> in the vfork path. So the following command fails to execute
> the second echo (shown here with the correct output).
>
> $ (readonly r; r= /bin/echo a; echo b)
> r: is read only
> b
>
> fix: defer the mklocal() to the child shell.
 1.66 23-Oct-2002  christos Fix interrupt problam from David Laight

$ /fred # non existant command
$ ^C # stops working

He says:
Ok the extra INTOFF is the one in exverror().
In almost all cases this doesn't matter because the longjmp()s
all end up in main() and the FORCEINTON call sorts it out
for the next command.
(There are a significant number of INTON/OFF mismatches through
the error paths...)

In any case the above failure can be 'fixed' by changing 2 (I think
they are both needed) INTON calls to FORCEINTON within evalcommand.
The following patch seems to work:

We should really look in the code and fix the INTON->INTOFF pairs.
 1.65 28-Sep-2002  christos Revert previous change. No need to save rootshell. It is only affecting
the non-vfork case. Having said that, it would be nice if pipelines of
simple commands were vforked too. Right now they are not.
Explain that setpgid() might fail because we are doing it both in the
parent and the child case, because we don't know which one will come
first.
Suspending a pipeline prints %1 Suspended n times where n is the number
of processes, but that was there before. It is easy to fix, but I'll
leave the code alone for now.
 1.64 27-Sep-2002  christos Deal with rootshell not being maintained correctly in the vfork() case.
Propagate isroot, throughout the eval process and maintain it properly.
Fixes sleep 10 | cat^C not exiting because sleep and cat ended up in
their own process groups, because wasroot was always true in the children.
 1.63 27-Sep-2002  mycroft Clean up INTOFF/INTON usage a little -- none of fork{shell,parent,child}()
screw with them now, only their callers.
 1.62 27-Sep-2002  christos Put back charles' fixes from -r1.60
 1.61 27-Sep-2002  christos VFork()ing shell: From elric@netbsd.org:
Plus my changes:
- walking process group fix in foregrounding a job.
- reset of process group in parent shell if interrupted before the wait.
- move INTON lower in the dowait so that the job structure is
consistent.
- error check all setpgid(), tcsetpgrp() calls.
- eliminate unneeded strpgid() call.
- check that we don't belong in the process group before we try to
set it.
 1.60 27-Sep-2002  mycroft In evalpipe(), move the INTOFF after the waitforjob(), to prevent possible
race conditions -- now we always synchronously wait for the job to finish.
In evalcommand(), add the same INTOFF/INTON locking as evalpipe(), to prevent
leaving internal state inconsistent, and also to insure that we synchronously
wait for the job.
 1.59 15-May-2002  christos implement noclobber. From Ben Harris, with minor tweaks from me. Two
unimplemented comments to go. Go Ben!
 1.58 14-Feb-2002  christos branches: 1.58.2;
PR/11542: Back-out previous change that caused
set -e
for x in a; do
BAR="foo"
false && echo true
echo mumble
done

not to echo mumble...
 1.57 04-Feb-2001  christos remove redundant declarations and nexted externs.
 1.56 22-May-2000  elric branches: 1.56.4;
Back out previous vfork changes.
 1.55 17-May-2000  elric When vforking ensure that the environment passed to exec is built before
vforking as a set of local variables which can be popped by the parent.

Addresses bin/10124.
 1.54 15-May-2000  elric INTON and FORCEINTON modify global variables, and so should not be
executed while we are vforked.
 1.53 13-May-2000  elric Added includes for waitpid, sys/types.h and sys/wait.h.
 1.52 13-May-2000  elric Now we use vfork(2) instead of fork(2) when we can.
 1.51 09-Feb-2000  christos Fix problem where commands that caused exitstatus != 0 inside loops did
not cause the shell to exit when -e was set.
 1.50 27-Jan-2000  christos Fix bin/9184, bin/9194, bin/9265, bin/9266
Exitcode and negation problems (From Martin Husemann)
 1.49 13-Oct-1999  mrg back out previous; it causes /etc/rc to break on my alpha and other lossage as reported in PR#8614
 1.48 10-Oct-1999  pk Backtrack `exitstatus' to make the shell really ignore the status
of `tested commands' as in this example:

set -e
true; false && echo "not reached"
 1.47 09-Jul-1999  christos branches: 1.47.2;
compile with WARNS = 2
 1.46 26-Jun-1999  christos PR/7814: Matthias Scheler: shell does not fork for builtins in backquotes,
leading to unexpected behaviour. Disable the no-fork optimization for now.
We need to revisit this and keep enough state around to recover from such
changes.
 1.45 04-Feb-1999  christos branches: 1.45.2;
PR/4966: Joel Reicher: Implement <> redirections which are documented in
the man page.
 1.44 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.43 28-Jul-1998  mycroft Delint.
 1.42 05-Feb-1998  christos Re-enabled EXP_RECORD
 1.41 04-Feb-1998  mikel back out last change until christos fixes EXP_RECORD; PR 4932
 1.40 31-Jan-1998  christos PR/4851: Benjamin Lorenz: In the "for <var> in <args>" construct <args>
was not marked as a region to be handled by ifsbreakup. Add EXP_RECORD
to indicate that the argument string needs to be recorded.
 1.39 26-Aug-1997  thorpej branches: 1.39.2;
Avoid a segv in bltinlookup() reported by Ronald Khoo <ronald@demon.net>
in PR #3929, fix submitted by hiroy@NETCOM.COM (Hiroyuki Ito).
 1.38 20-Jul-1997  christos PR/3888: Chris Demetriou: type command-with-slash prints
$PATH[0]/command-with-slash...
 1.37 15-Jul-1997  christos PR/3866: bayer@informatik.uni-leipzig.de: core dump using xon script.
cmdenviron is pointing to varlist.list; varlist gets reset everytime
you enter evalcommand, but cmdenviron does not. The wonders of global
variables...
 1.36 04-Jul-1997  christos Fix compiler warnings.
 1.35 14-Mar-1997  christos NO_HISTORY->SMALL
 1.34 11-Jan-1997  tls kill 'register'
 1.33 09-Nov-1996  christos remove a debugging printf that was left from the last POSIX error code fixes.
 1.32 06-Nov-1996  christos Fix miscellaneous getopts problems:
- the 3 argument version of getopts would not reset properly
- OPTARG did not get cleared after a non argument option was found
- OPTIND was not set properly after a non argument option.
 1.31 16-Oct-1996  christos PR/287: Exit with 127/126 when command is not found/permission denied.
PR/2808: don't bomb out on "set -e; false && true"
 1.30 03-Jun-1996  christos Fix PR/2504: return with no args returns 0 instead of the return value of
the previous command in functions
 1.29 06-Mar-1996  pk branches: 1.29.4;
Return zero status if `else' clause is empty.
 1.28 05-Mar-1996  christos - parser.c: Fix prompting in old style backquote expansion. Fixes PR/2139
and many user complaints why the shell hangs in echo "`"
- eval.c: Fix exitstatus invalid resetting in `if' statements were:
if (exit 3); then
echo foo $?
else
echo bar $?
fi
printed 'bar 0' instead of bar 3
 1.27 11-Sep-1995  christos Fix return builtin to work like it does in ksh:
When not in a function, it skips the rest of the current input file.
Instances of `return' outside function definitions were previously ignored.
What does joe posix have to say about this?
[fixes PR/1444]
 1.26 09-Jun-1995  christos Changed so that 'PATH=newpath command' works, instead of looking at the
old path. Synced input.c with vangogh.
 1.25 19-May-1995  christos Changed so that syntax errors (EXERROR) set the exit status to 2,
and commands that are not found set the exit status to 1 like all
other bourne shells.
[It used to be 0 and 2 respectively]
 1.24 15-May-1995  cgd re-add an #endif that was (apprently) clobbered.
 1.23 15-May-1995  christos Fixed new bug the previous fix introduced:

false
foo=bar
echo $?

would print 1
Also fixed the long standing bug:

false
echo `echo $?`

would print 0
The exitstatus needs rethinking and rewriting. The trial and error method
is not very efficient
 1.22 14-May-1995  christos Fixed bug caused by previous x=`false` not preserving the exit status fix.
The if statement exit status broke...
 1.21 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.20 31-Mar-1995  christos 1. Don't core dump on 'fc -l' (From Gerard J van der Grinten)
2. PATH=xxx ls, does the PATH assignment first and then tries to find ls in xxx
3. VAR=xxx exec ls, does the variable assignment.
 1.19 21-Mar-1995  cgd convert to new RCS id conventions.
 1.18 23-Dec-1994  cgd be more careful with casts.
 1.17 05-Dec-1994  cgd clean up further. more patches from Jim Jegers
 1.16 04-Dec-1994  cgd from James Jegers <jimj@miller.cs.uwm.edu>: quiet -Wall, and squelch
some of the worst style errors.
 1.15 24-Aug-1994  mycroft Fix a core dump and another parse error related to null commands.
 1.14 14-Jun-1994  jtc branches: 1.14.2;
From Christos:
1. Fix `-' quoting in [ ] expressions.
2. Fix expansion of variables in redirections
 1.13 12-Jun-1994  jtc Set the status variable ($?) to 0 after a successful variable assignment.
 1.12 11-Jun-1994  mycroft Add RCS ids.
 1.11 21-May-1994  cgd a few more things to omit when NO_HISTORY defined. from noel@cs.oberlin.edu
 1.10 14-May-1994  cgd add back in support for building w/o obj dir. also, add NO_HISTORY
define, which (if you invoke mkbuiltins properly) gets you a sh w/o
history of command line editing (for floppy sh).
 1.9 12-May-1994  jtc Include appropriate header files to bring function prototypes into scope.
 1.8 11-May-1994  jtc reintegrate NetBSD's false builtin
 1.7 11-May-1994  jtc sync with 4.4lite
 1.6 09-Sep-1993  cgd fix from Jim Wilson <wilson@cygnus.com> for nothing-between-backquotes core
 1.5 01-Aug-1993  mycroft Add RCS identifiers.
 1.4 07-Jul-1993  jtc IEEE 1003.2 (D11.2.2.3) requires that the system's true and false be accessed
instead of searching $PATH. The best way to satisfy this requirement is to
make them builtins.

True was allready builtin, this patch adds false.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.14.2.1 24-Aug-1994  mycroft update from trunk
 1.29.4.2 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.29.4.1 10-Jun-1996  jtc pulled up from version 1.30 at christos' request
 1.39.2.1 08-May-1998  mycroft Sync with trunk, per request of christos.
 1.45.2.1 01-Jul-1999  perry pullup 1.45->1.46 (christos)
 1.47.2.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.56.4.1 23-Feb-2002  he Pull up revision 1.58 (requested by christos):
When ``-e'' is in effect, do not exit if the failing command is
part of an && or || list, or preceded by the ``!'' reserved word.
Fixes PR#11542.
 1.58.2.1 27-Mar-2002  elric Doing the vfork work on ash on a branch to try to shake out the
problems before I expose everyone to them. This checkin represents
a merge of the prior work, which I backed out a while ago, to the
HEAD only and does not incorporate any additional bugfixes. The
additional bugfixes and code-cleanup will occur in later checkins.

For reference the patches that were used are:
cvs diff -kk -r1.51 -r1.55 eval.c | patch
cvs diff -kk -r1.27 -r1.28 exec.c | patch
cvs diff -kk -r1.15 -r1.16 exec.h | patch
cvs diff -kk -r1.32 -r1.33 input.c | patch
cvs diff -kk -r1.10 -r1.11 input.h | patch
cvs diff -kk -r1.32 -r1.35 jobs.c | patch
cvs diff -kk -r1.9 -r1.11 jobs.h | patch
cvs diff -kk -r1.36 -r1.37 main.c | patch
cvs diff -kk -r1.20 -r1.21 redir.c | patch
cvs diff -kk -r1.10 -r1.11 redir.h | patch
cvs diff -kk -r1.10 -r1.12 shell.h | patch
cvs diff -kk -r1.22 -r1.23 trap.c | patch
cvs diff -kk -r1.12 -r1.13 trap.h | patch
cvs diff -kk -r1.23 -r1.24 var.c | patch
cvs diff -kk -r1.16 -r1.17 var.h | patch

All other changes were simply the resolution of the resulting
conflicts, which occured only in the merge of jobs.c.

Begins to address PR: bin/5475
 1.81.2.1 13-Jun-2005  tron Pull up revision 1.82 (requested by lukem in ticket #397):
Mark temp_path volatile so that it won't get clobbered after longjmp.
(Also appeases gcc -Wuninitialized.)
 1.88.8.1 23-Mar-2008  matt sync with HEAD
 1.88.4.1 04-Sep-2008  skrll Sync with netbsd-4.
 1.88.2.1 08-Jun-2008  bouyer Pull up following revision(s) (requested by tron in ticket #1157):
bin/sh/eval.c: revision 1.90
Port revision 1.44 of "src/bin/sh/eval.c" from FreeBSD to fix PR bin/38584.
Reviewed by Michael van Elst.
 1.89.6.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.89.4.1 04-Jun-2008  yamt sync with head
 1.100.2.1 05-Mar-2011  bouyer Sync with HEAD
 1.102.2.4 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.102.2.3 23-Jan-2013  yamt sync with head
 1.102.2.2 30-Oct-2012  yamt sync with head
 1.102.2.1 17-Apr-2012  yamt sync with head
 1.104.2.3 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.104.2.2 23-Jun-2013  tls resync from head
 1.104.2.1 25-Feb-2013  tls resync with head
 1.108.2.1 10-Aug-2014  tls Rebase.
 1.128.2.2 26-Apr-2017  pgoyette Sync with HEAD
 1.128.2.1 20-Mar-2017  pgoyette Sync with HEAD
 1.129.2.1 21-Apr-2017  bouyer Sync with HEAD
 1.131.2.2 19-May-2017  pgoyette Resolve conflicts from previous merge (all resulting from $NetBSD
keywork expansion)
 1.131.2.1 11-May-2017  pgoyette Sync with HEAD
 1.140.2.7 25-Aug-2018  martin Fix merge mishap due to #983 / #989 pullup order.
 1.140.2.6 25-Aug-2018  martin Pull up following revision(s) (requested by kre in ticket #989):

bin/sh/eval.c: revision 1.156
bin/sh/eval.h: revision 1.20
bin/sh/exec.c: revision 1.53

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
 1.140.2.5 25-Aug-2018  martin Pull up following revision(s) (requested by kre in ticket #983):

bin/sh/eval.c: revision 1.158
bin/sh/eval.h: revision 1.21
bin/sh/main.c: revision 1.74

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
 1.140.2.4 25-Aug-2018  martin Pull up following revision(s) (requested by kre in ticket #982):

bin/sh/eval.c: revision 1.157

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
 1.140.2.3 13-Jul-2018  martin Pull up following revision(s) (requested by kre in ticket #906):

bin/sh/eval.c: revision 1.155
bin/sh/mknodes.sh: revision 1.3
bin/sh/nodes.c.pat: revision 1.14
bin/sh/exec.h: revision 1.27
bin/sh/exec.c: revision 1.52

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).
 1.140.2.2 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.140.2.1 05-Jun-2017  snj Pull up following revision(s) (requested by kre in ticket #5):
bin/sh/cd.c: revision 1.48
bin/sh/eval.c: revision 1.141
bin/sh/exec.c: revision 1.48
bin/sh/exec.h: revision 1.25
bin/sh/mail.c: revisions 1.17, 1.18
bin/sh/sh.1: revision 1.147
Make cd (really) do cd -P, and not just claim that is what it is doing
while doing a half-hearted, broken, partial, version of cd -L instead.
The latter (as the manual says) is not supported, what's more, it is an
abomination, and should never be supported (anywhere.)
Fix the doc so that the pretense that we notice when a path given crosses
a symlink (and turns on printing of the destination directory) is claimed
no more (that used to be true until late Dec 2016, but was changed). Now
the print happens if -o cdprint is set, or if an entry from CDPATH that is
not "" or "." is used (or if the "cd dest repl" cd cmd variant is used.)
Fix CDPATH processing: avoid the magic '%' processing that is used for
PATH and MAILPATH from corrupting CDPATH. The % magic (both variants)
remains undocumented.
Also, don't double the '/' if an entry in PATH or CDPATH ends in '/'
(as in CDPATH=":/usr/src/"). A "cd usr.bin" used to do
chdir("/usr/src//usr.bin"). No more. This is almost invisible,
and relatively harmless, either way....
Also fix a bug where if a plausible destination directory in CDPATH
was located, but the chdir() failed (eg: permission denied) and then
a later "." or "" CDPATH entry succeeded, "print" mode was turned on.
That is:
cd /tmp; mkdir bin
mkdir -p P/bin; chmod 0 P/bin
CDPATH=/tmp/P:
cd bin
would cd to /tmp/bin (correctly) but print it (incorrectly).
Also when in "cd dest replace" mode, if the result of the replacement
generates '-' as the path named, as in:
cd $PWD -
then simply change to '-' (or attempt to, with CDPATH search), rather
than having this being equivalent to "cd -")
Because of these changes, the pwd command (and $PWD) essentially
always acts as pwd -P, even when called as pwd -L (which is still
the default.) That is, even more than it did before.
Also fixed a (kind of minor) mem management error (CDPATH related)
"whosoever shall padvance must stunalloc before repeating" (and the
same for MAILPATH).
--
If we are going to keep the MAILPATH % hack, then at least do something
rational. Since it isn't documented, what "rational" is is up for
discussion, but what it did before was not it (it was nonsense...).
 1.153.2.8 26-Jan-2019  pgoyette Sync with HEAD
 1.153.2.7 18-Jan-2019  pgoyette Synch with HEAD
 1.153.2.6 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.153.2.5 26-Nov-2018  pgoyette Sync with HEAD, resolve a couple of conflicts
 1.153.2.4 20-Oct-2018  pgoyette Sync with head
 1.153.2.3 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.153.2.2 28-Jul-2018  pgoyette Sync with HEAD
 1.153.2.1 25-Jun-2018  pgoyette Sync with HEAD
 1.155.2.4 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.155.2.3 21-Apr-2020  martin Sync with HEAD
 1.155.2.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.155.2.1 10-Jun-2019  christos Sync with HEAD
 1.175.2.4 14-Jan-2024  martin Pull up following revision(s) (requested by kre in ticket #1787):

bin/sh/eval.c: revision 1.191
bin/sh/expand.c: revision 1.144

PR bin/57773

Fix a bug reported by Jarle Fredrik Greipsland in PR bin/57773,
where a substring expansion where the substring to be removed from
a variable expansion is itself a var expansion where the value
contains one (or more) of sh's CTLxxx chars - the pattern had
CTLESC inserted, the string to be matched against did not. Fail.

We fix that by always inserting CTLESC in var assign expansions.
See the PR for all the gory details.

Thanks for the PR.

PR bin/57773

Fix another bug reported by Jarle Fredrik Greipsland and added
to PR bin/57773, which relates to calculating the length of a
positional parameter which contains CTL chars -- yes, this one
really is that specific, though it would also affect the special
param $0 if it were to contain CTL chars, and its length was
requested - that is fixed with the same change. And note: $0
is not affected because it looks like a positional param (it
isn't, ${00} would be, but is always unset, ${0} isn't) all
special parame would be affected the same way, but the only one
that can ever contain a CTL char is $0 I believe. ($@ and $*
were affected, but just because they're expanding the positional
params ... ${#@} and ${#*} are both technically unspecified
expansions - and different shells produce different results.

See the PR for the details of this one (and the previous).

Thanks for the PR.
 1.175.2.3 28-Apr-2021  martin Pull up following revision(s) (requested by kre in ticket #1259):

bin/sh/jobs.h: revision 1.24
bin/sh/eval.c: revision 1.182
bin/sh/jobs.c: revision 1.110

Related to PR bin/48875

Correct an issue found by Oguz <oguzismailuysal@gmail.com> and reported
in e-mail (on the bug-bash list initially!) with the code changed to deal
with PR bin/48875

With:
sh -c 'echo start at $SECONDS;
(sleep 3 & (sleep 1& wait) );
echo end at $SECONDS'

The shell should say "start at 0\nend at 1\n", but instead (before
this fix, in -9 and HEAD, but not -8) does "start at 0\nend at 3\n"
(Not in -8 as the 48875 changes were never pulled up)>

There was an old problem, fixed years ago, which cause the same symptom,
related to the way the jobs table was cleared (or not) in subshells, and
it seemed like that might have resurfaced.

But not so, the issue here is the sub-shell elimination, which was part
of the 48875 "fix" (not really, it wasn't really a bug, just sub-optimal
and unexpected behaviour).

What the shell actually has been running in this case is:

sh -c 'echo start at $SECONDS;
(sleep 3 & sleep 1& wait );
echo end at $SECONDS'

as the inner subshell was deemed unnecessary - all its parent would
do is wait for its exit status, and then exit with that status - we
may as well simply replace the current sub-shell with the new one,
let it do its thing, and we're done...

But not here, the running "sleep 3" will remain a child of that merged
sub-shell, and the "wait" will thus wait for it, along with the sleep 1
which is all it should be seeing.

For now, fix this by not eliminating a sub-shell if there are existing
unwaited upon children in the current one. It might be possible to
simply disregard the old child for the purposes of wait (and "jobs", etc,
all cmds which look at the jobs table) but the bookkeeping required to
make that work reliably is likely to take some time to get correct...

Along with this fix comes a fix to DEBUG mode shells, which, in situations
like this, could dump core in the debug code if the relevant tracing was
enabled, and add a new trace for when the jobs table is cleared (which was
added predating the discovery of the actual cause of this issue, but seems
worth keeping.) Neither of these changes have any effect on shells
compiled normally.

XXX pullup -9
 1.175.2.2 26-Dec-2019  martin Pull up following revision(s) (requested by kre in ticket #582):

bin/sh/eval.c: revision 1.177

Use fork() rather than vfork() when forking to run a background
process with redirects. If we use vfork() and a redirect hangs
(eg: opening a fifo) which the parent was intended to unhang,
then the parent never gets to continue to unhang the child.
eg: mkfifo f; cat <f &; echo foo>f

The parent should not be waiting for a background process, even
just for its exec() to complete. if there are no redirects there
is (should be) nothing left that might be done that will cause any
noticeable delay, so vfork() should be safe in all other cases.
 1.175.2.1 11-Dec-2019  martin Pull up following revision(s) (requested by kre in ticket #542):

bin/sh/eval.c: revision 1.176
bin/sh/trap.c: revision 1.53

PR bin/54743

Having traps set should not enforce a fork for the next command,
whatever that command happens to be, only for commands which would
normally fork if they weren't the last command expected to be
executed (ie: builtins and functions shouldn't be exexuted in a
sub-shell merely because a trap is set).

As it was (for example)
trap 'whatever' SIGANY; wait $anypid
was guaranteed to fail the wait, as the subshell it was executed
in could not have any children.

XXX pullup -9

PR bin/54743

If a builtin command or function is the final command intended to be
executed, and is interrupted by a caught signal, the trap handler for
that signal was not executed - the shell simply exited (an exit trap
handler would still have been run - if there was one the handler
for the signal may have been invoked during the execution of the
exit trap handler, which, if it happened, is incorrect sequencing).

Now, if we're exiting, and there are pending signals, run their handlers
just before running the EXIT trap handler, if any.
There are almost certainly plenty more issues with traps that need
solving. Later,

XXX pullup -9
(-8 is too different in this area, and this problem suitably obscure,
that we won't bother) (the -7 sh is simply obsolete).
 1.188.2.2 25-Nov-2024  martin Apply patch, requested by kre in ticket #1016:

bin/sh/eval.c (apply patch)

Fix "exec cmd" redirections to never close-on-exec

Correct the bug reported by Edgar Fu� in:
https://mail-index.netbsd.org/tech-userlevel/2024/11/05/msg014588.html
where when /bin/sh evaluates
exec command 3>/some/file
fd 3 (any redirection for any fd > 2) gets "close on exec" set
(inappropriately) causing the redirection to be evaluated, then
immediately closed when the exec happens.
 1.188.2.1 14-Jan-2024  martin Pull up following revision(s) (requested by kre in ticket #535):

bin/sh/eval.c: revision 1.191
bin/sh/expand.c: revision 1.144

PR bin/57773

Fix a bug reported by Jarle Fredrik Greipsland in PR bin/57773,
where a substring expansion where the substring to be removed from
a variable expansion is itself a var expansion where the value
contains one (or more) of sh's CTLxxx chars - the pattern had
CTLESC inserted, the string to be matched against did not. Fail.

We fix that by always inserting CTLESC in var assign expansions.
See the PR for all the gory details.

Thanks for the PR.

PR bin/57773

Fix another bug reported by Jarle Fredrik Greipsland and added
to PR bin/57773, which relates to calculating the length of a
positional parameter which contains CTL chars -- yes, this one
really is that specific, though it would also affect the special
param $0 if it were to contain CTL chars, and its length was
requested - that is fixed with the same change. And note: $0
is not affected because it looks like a positional param (it
isn't, ${00} would be, but is always unset, ${0} isn't) all
special parame would be affected the same way, but the only one
that can ever contain a CTL char is $0 I believe. ($@ and $*
were affected, but just because they're expanding the positional
params ... ${#@} and ${#*} are both technically unspecified
expansions - and different shells produce different results.

See the PR for the details of this one (and the previous).

Thanks for the PR.
 1.192.2.1 02-Aug-2025  perseant Sync with HEAD
 1.24 03-Aug-2024  kre Change the "string" argument to evalstring() and setinputstring()
from being "char *" to being "const char *".

This is needed for a forthcoming change which needs to pass a const char *
to evalstring (and through it to setinputstring) and be assured that
nothing will alter the characters in the string supplied.

This is (aside from the additional compile time protection provided)
a no-op change, all evalstring() does with its string is pass it to
setinputstring() and all that does with it is determine its length
(strlen() which expects a const char *) and assign the string pointer
to parsenextc which is already a const char * - there never has been
any reason for these two functions to not include the "const" in
the arg declaration -- except that when originally written (early
1990's) I suspect "const" either didn't exist at all, or wasn't
supported by relevant compilers.

NFCI. Most probably (though I didn't check) no binary change at all.
 1.23 04-Feb-2019  kre branches: 1.23.12;
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).
 1.22 03-Dec-2018  kre 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).
 1.21 19-Aug-2018  kre 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
 1.20 25-Jul-2018  kre 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
 1.19 09-May-2016  kre branches: 1.19.8; 1.19.14; 1.19.16;

PR bin/48875 - avoid holding (replaced) file descriptors open when running a
command in the current shell (so they can be restored for the next command)
in cases where it is obvious that there is not going to be a following
command to use them. This fixes the problem reported in the PR (though
there are still plenty of situations where a FD could be closed but isn't,
we do not do full fd flow eveluation to determine whether a fd will be
used or not).

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

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

OK christos@
 1.16 31-May-2014  christos PR/48843: Jarmo Jaakkola: dot commands mess up scope nesting tracking

Evaluation of commands goes completely haywire if a file containing
a break/continue/return command outside its "intended" scope is sourced
using a dot command inside its "intended" scope. The main symptom is
not exiting from the sourced file when supposed to, leading to evaluation
of commands that were not supposed to be evaluated. A secondary symptom
is that these extra commands are not evaluated correctly, as some of them
are skipped. Some examples are listed in the How-To-Repeat section.

According to the POSIX standard, this is how it should work:
dot:
The shell shall execute commands from the file in the current
environment.
break:
The break utility shall exit from the smallest enclosing for, while,
or until loop, [...]
continue:
The continue utility shall return to the top of the smallest
enclosing for, while, or until loop, [...]
return:
The return utility shall cause the shell to stop executing
the current function or dot script. If the shell is not currently
executing a function or dot script, the results are unspecified.

It is clear that return should return from a sourced file, which
it does not do. Whether break and continue should work from the sourced
file might be debatable. Because the dot command says "in the current
environment", I'd say yes. In any case, it should not fail in weird
ways like it does now!

The problems occur with return (a) and break/continue (b) because:
1) dotcmd() does not record the function nesting level prior to
sourcing the file nor does it touch the loopnest variable,
leading to either
2 a) returncmd() being unable to detect that it should not set
evalskip to SKIPFUNC but SKIPFILE, or
b) breakcmd() setting evalskip to SKIPCONT or SKIPBREAK,
leading to
3) cmdloop() not detecting that it should skip the rest of
the file, due to only checking for SKIPFILE.
The result is that cmdloop() keeps executing lines from the file
whilst evalskip is set, which is the main symptom. Because
evalskip is checked in multiple places in eval.c, the secondary
symptom appears.
>How-To-Repeat:
Run the following script:

printf "break\necho break1; echo break2" >break
printf "continue\necho continue1; echo continue2" >continue
printf "return\necho return1; echo return2" >return

while true; do . ./break; done

for i in 1 2; do . ./continue; done

func() {
. ./return
}
func

No output should be produced, but instead this is the result:
break1
continue1
continue1
return1

The main symptom is evident from the unexpected output and the secondary
one from the fact that there are no lines with '2' in them.
>Fix:
Here is patch to src/bin/sh to fix the above problems. It keeps
track of the function nesting level at the beginning of a dot command
to enable the return command to work properly.

I also changed the undefined-by-standard functionality of the return
command when it's not in a dot command or function from (indirectly)
exiting the shell to being silently ignored. This was done because
the previous way has at least one bug: the shell exits without asking
for confirmation when there are stopped jobs.

Because I read the standard to mean that break and continue should have
an effect outside the sourced file, that's how I implemented it. For what
it's worth, this also seems to be what bash does. Also laziness, because
this way required no changes to loopnesting tracking. If this is not
wanted, it might make sense to move the nesting tracking to the inputfile
stack.

The patch also does some clean-up to reduce the amount of global
variables by moving the dotcmd() and the find_dot_file() functions from
main.c to eval.c and making in_function() a proper function.
 1.15 15-Feb-2008  matt branches: 1.15.32; 1.15.46;
Fix inconsistent definitions
 1.14 07-Aug-2003  agc branches: 1.14.22;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.13 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.12 28-Sep-2002  christos Revert previous change. No need to save rootshell. It is only affecting
the non-vfork case. Having said that, it would be nice if pipelines of
simple commands were vforked too. Right now they are not.
Explain that setpgid() might fail because we are doing it both in the
parent and the child case, because we don't know which one will come
first.
Suspending a pipeline prints %1 Suspended n times where n is the number
of processes, but that was there before. It is easy to fix, but I'll
leave the code alone for now.
 1.11 27-Sep-2002  christos Deal with rootshell not being maintained correctly in the vfork() case.
Propagate isroot, throughout the eval process and maintain it properly.
Fixes sleep 10 | cat^C not exiting because sleep and cat ended up in
their own process groups, because wasroot was always true in the children.
 1.10 27-Jan-2000  christos Fix bin/9184, bin/9194, bin/9265, bin/9266
Exitcode and negation problems (From Martin Husemann)
 1.9 11-Sep-1995  christos Fix return builtin to work like it does in ksh:
When not in a function, it skips the rest of the current input file.
Instances of `return' outside function definitions were previously ignored.
What does joe posix have to say about this?
[fixes PR/1444]
 1.8 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.7 21-Mar-1995  cgd convert to new RCS id conventions.
 1.6 11-Jun-1994  mycroft Add RCS ids.
 1.5 11-May-1994  jtc sync with 4.4lite
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.14.22.1 23-Mar-2008  matt sync with HEAD
 1.15.46.1 10-Aug-2014  tls Rebase.
 1.15.32.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.19.16.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.19.16.2 21-Apr-2020  martin Sync with HEAD
 1.19.16.1 10-Jun-2019  christos Sync with HEAD
 1.19.14.3 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.19.14.2 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.19.14.1 28-Jul-2018  pgoyette Sync with HEAD
 1.19.8.2 25-Aug-2018  martin Pull up following revision(s) (requested by kre in ticket #989):

bin/sh/eval.c: revision 1.156
bin/sh/eval.h: revision 1.20
bin/sh/exec.c: revision 1.53

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
 1.19.8.1 25-Aug-2018  martin Pull up following revision(s) (requested by kre in ticket #983):

bin/sh/eval.c: revision 1.158
bin/sh/eval.h: revision 1.21
bin/sh/main.c: revision 1.74

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
 1.23.12.1 02-Aug-2025  perseant Sync with HEAD
 1.59 12-Jul-2024  kre Improve safety in var imports from the environment.

Add a new var flag VUNSAFE - set on all vars imported from the environment.

Add setvareqsafe() (which is to setvareq() as setvarsafe() is to setvar())
and use that instead of setvareq() when processing the environment, so
errors don't cause the shell to abort. Use VUNSAFE in that call.

Add flags arguments to all var callback functions which are used when setting
variables, and pass the flags given to the setvar*() functions to those
functions, so they can act differently in different situations (if desired).
Most of them just ignore the flags.

When unsetting a variable, call setvar() to clear things (and call the
callback function) both when the variable had a value which needs to be freed,
and when unsetting a variable which wasn't unset previously, so the VUNSET
flag can be seen by that callback func.

When setting HISTSIZE, use the flags passed to determine whether to ignore
bad values (if VUNSAFE) or treat them as an error. This replaces the
earlier temporary hack to always ignore bad data there (histedit.c 1.68).

Miscellaneous associated minor changes.

These changes should largely be invisible in normal use.
 1.58 19-Mar-2023  kre branches: 1.58.2;

Do a better job handling EACCES errors from exec() calls. If the
EACCES is from the namei(), treat it just like ENOENT or ENOTDIR
(and if that is the final error, the exit status from a failed exec
will be 127). If the EACCES is from the exec() itself, that indicates
the file to be run exists, but has no 'x' permission. That's a
meaningful error (as distinct from just "yet another PATH element
search failure").

While here, return the first meaingful error we encountered while
searching PATH, rather than the last (and ENOENT if there are none
of those).

This change results in some failed command executions returning status
127 now, where they returned 126 before - which better reflects the
intent of those values (127 is simply "not found" whereas 126 is "found
but couldn't be executed").

We still do nothing to distinguish errors encountered looking up the
command name give, with errors encountered (by the kernel) attempting to
run an interpreter needed for the exec to succeed (#! line path, or
/libexec/ld.elf_so and similar - or anything else of a similar nature).
 1.57 16-Nov-2021  kre PR bin/56491

Make "hash" exit(!=0) (ie: exit(1)) if it writes an error message to
stderr as required by POSIX (it was writing "not found" errors, yet
still doing exit(0)).

Whether, when doing "hash foobar", and "foobar" is not found as a command
(not a built-in, not a function, and not found via a PATH search), that
should be considered an error differs between shells. All of the ksh
descendant shells say "no", write no error message in this case, and
exit(0) if no other errors occur. Other shells (essentially all) do
consider it an error, write a message to stderr, and exit(1) when this happens.

POSIX isn't clear, the bug report:
https://austingroupbugs.net/view.php?id=1460
which is not yet resolved, suggests that the outcome will be that
this is to be unspecified. Given the diversity, there might be no
other choice.

Have a foot in both camps - default to the "other shell" behaviour,
but add a -e option (no errors ... applies only to these "not found"
errors) to generate the ksh behaviour. Without other errors (like an
unknown option, etc) "hash -e anyname" will always exit(0).

See the PR for details on how it all works now, or read the updated man page.

While here, when hash is in its other mode (reporting what is in the
table) check for I/O errors on stdout, and exit(1) (with an error
message!) if any occurred. This does not apply to output generated
by the -v option when command names are given (that output is incidental).

In sh.1 document all of this. Also add documentation for a bunch of
other options the hash command has had for years, but which were never
documented. And while there, clean up some other sections I noticed
needed improving (either formatting or content or both).
 1.56 10-Oct-2021  rillig sh: make find_command simpler

Lint complained about the do-while-0 loop that contained a continue. It
didn't state the reason for it, but indeed the code looked complicated.
Rewrite the code to be less verbose and to use common coding patterns.

No functional change.
 1.55 16-Feb-2021  kre PR bin/55979

This fixes the MSAN detected reference to an unitialised variable
(an unitialised field in a struct) which happens when a command is
not found after a PATH search.

Aside from skipping some known to be going to fail exec*() calls
in some cases, the setting of the relevant field is irrelevant,
so this problem makes no practical difference to the shell, or any
shell script.

XXX (maybe) pullup -9
 1.54 01-Aug-2020  kre PR bin/55526

Fix a bug that has existed since the "command" command was added in
2003. "command foo" would cause the definition of a function "foo"
to be lost (not freed, simply discarded) if "foo" is (in addition to
being a function) a filesystem command. The case where "foo" is
a builtin was handled.

For now, when a function exists with the same name as a filesystem
command, the latter can never appear in the command hash table, and
when used (which can only be via "command foo", just "foo" finds
the function) will always result in a full PATH search.

XXX pullup everything (from NetBSD 2.0 onwards). (really -8 and -9)
 1.53 25-Jul-2018  kre branches: 1.53.2;
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
 1.52 22-Jun-2018  kre branches: 1.52.2;
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).
 1.51 05-Jul-2017  kre branches: 1.51.4;

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.
 1.50 17-Jun-2017  kre Many internal memory management type fixes.

PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)

echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)

(Recently added) Problems with ~ expansion fixed (mem management related).

Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)

And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.

Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,

More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.

[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]

More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).

User visible changes:

Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in

cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END

(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)

With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)

POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).

Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
 1.49 07-Jun-2017  kre A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)

This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)

The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.

This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).

I would not call the results of this perfect yet, but it is close.
 1.48 04-Jun-2017  kre Make cd (really) do cd -P, and not just claim that is what it is doing
while doing a half-hearted, broken, partial, version of cd -L instead.
The latter (as the manual says) is not supported, what's more, it is an
abomination, and should never be supported (anywhere.)

Fix the doc so that the pretense that we notice when a path given crosses
a symlink (and turns on printing of the destination directory) is claimed
no more (that used to be true until late Dec 2016, but was changed). Now
the print happens if -o cdprint is set, or if an entry from CDPATH that is
not "" or "." is used (or if the "cd dest repl" cd cmd variant is used.)

Fix CDPATH processing: avoid the magic '%' processing that is used for
PATH and MAILPATH from corrupting CDPATH. The % magic (both variants)
remains undocumented.

Also, don't double the '/' if an entry in PATH or CDPATH ends in '/'
(as in CDPATH=":/usr/src/"). A "cd usr.bin" used to do
chdir("/usr/src//usr.bin"). No more. This is almost invisible,
and relatively harmless, either way....

Also fix a bug where if a plausible destination directory in CDPATH
was located, but the chdir() failed (eg: permission denied) and then
a later "." or "" CDPATH entry succeeded, "print" mode was turned on.
That is:
cd /tmp; mkdir bin
mkdir -p P/bin; chmod 0 P/bin
CDPATH=/tmp/P:
cd bin
would cd to /tmp/bin (correctly) but print it (incorrectly).

Also when in "cd dest replace" mode, if the result of the replacement
generates '-' as the path named, as in:
cd $PWD -
then simply change to '-' (or attempt to, with CDPATH search), rather
than having this being equivalent to "cd -")

Because of these changes, the pwd command (and $PWD) essentially
always acts as pwd -P, even when called as pwd -L (which is still
the default.) That is, even more than it did before.

Also fixed a (kind of minor) mem management error (CDPATH related)
"whosoever shall padvance must stunalloc before repeating" (and the
same for MAILPATH).
 1.47 15-May-2017  kre branches: 1.47.2;

(Perhaps temporarary) updated "hash" command. New options, and
more flexible behaviour.
 1.46 03-May-2016  christos branches: 1.46.6;
add missing forward declaration for the STATIC= case.
 1.45 01-Nov-2013  christos PR/48312: Dieter Roelands: According to TOG, unset should not return an error
for functions are variables that were not previously set:
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
 1.44 31-Dec-2012  dsl Add support for '%n' being a shorthand for 'fg %n'.
 1.43 20-Mar-2012  matt branches: 1.43.2;
Use C89 function definitions
 1.42 16-Oct-2008  dholland branches: 1.42.18; 1.42.20;
Wrap declaration of a STATIC function that's only conditionally defined
in a suitable ifdef, so things still compile if STATIC is defined as
"static", which is for some reason not the default.

(In the long run STATIC should go away - it might have once been a
portability hack but now definitely serves no purpose.)
 1.41 15-Feb-2008  matt Fix inconsistent definitions
 1.40 24-Jun-2007  christos branches: 1.40.4;
PR/36531: Greg A. Woods: another very helpful DEBUG TRACE() call for execve()
failures in /bin/sh
 1.39 18-Mar-2006  christos Coverity CID 890: Possible NULL pointer deref.
 1.38 18-Mar-2006  christos Coverity CID 1329: Possible NULL deref.
 1.37 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.36 04-Feb-2003  dsl Fix bin/20185 - builtin called from function of same name mustn't be hashed.
Make 'hash' only report utilities that are not builtins (posix), the
non-posix 'hash -v' will report everything.
(agreed by christos)
 1.35 22-Jan-2003  dsl Support command -p, -v and -V as posix
Stop temporary PATH assigments messing up hash table
Fix sh -c -e "echo $0 $*" -a x (as posix)
(agreed by christos)
 1.34 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.33 27-Sep-2002  christos VFork()ing shell: From elric@netbsd.org:
Plus my changes:
- walking process group fix in foregrounding a job.
- reset of process group in parent shell if interrupted before the wait.
- move INTON lower in the dowait so that the job structure is
consistent.
- error check all setpgid(), tcsetpgrp() calls.
- eliminate unneeded strpgid() call.
- check that we don't belong in the process group before we try to
set it.
 1.32 04-Feb-2001  christos branches: 1.32.2;
remove redundant declarations and nexted externs.
 1.31 01-Nov-2000  christos handle type command on names that contain slashes, and print a : in the not
found case. From FreeBSD.
 1.30 03-Jul-2000  matt include <stdlib.h>, <string.h>, or whatever as appropriate to shut up
gcc 2.96
 1.29 22-May-2000  elric branches: 1.29.4;
Back out previous vfork changes.
 1.28 13-May-2000  elric Now we use vfork(2) instead of fork(2) when we can.
 1.27 09-Jul-1999  christos compile with WARNS = 2
 1.26 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.25 28-Jul-1998  mycroft Delint.
 1.24 20-Jul-1997  christos PR/3888: Chris Demetriou: type command-with-slash prints
$PATH[0]/command-with-slash...
 1.23 04-Jul-1997  christos Fix compiler warnings.
 1.22 06-Feb-1997  christos add type builtin.
 1.21 11-Jan-1997  tls kill 'register'
 1.20 19-Oct-1996  abrown Remove leftover printf("here") from last change.
 1.19 16-Oct-1996  christos PR/287: Shell does not exit with 126/127 when permission denied/
command not found. Add extra exception type and generalize
error handling routines to take that exception type. Use
a global variable exerrno to keep the last exec error.
 1.18 25-Jun-1996  christos const poisoning.
 1.17 09-Jun-1995  christos branches: 1.17.6;
Changed so that 'PATH=newpath command' works, instead of looking at the
old path. Synced input.c with vangogh.
 1.16 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.15 21-Mar-1995  cgd convert to new RCS id conventions.
 1.14 30-Jan-1995  mycroft Use S_IS*().
 1.13 15-Jan-1995  mycroft Remove the gratuitous `security' warning.
 1.12 05-Dec-1994  cgd clean up further. more patches from Jim Jegers
 1.11 23-Sep-1994  mycroft Eliminate uses of some obsolete functions.
 1.10 11-Jun-1994  mycroft Add RCS ids.
 1.9 12-May-1994  jtc Include appropriate header files to bring function prototypes into scope.
 1.8 11-May-1994  jtc sync with 4.4lite
 1.7 01-Apr-1994  jtc Getgroup's second argument is now a gid_t ptr.
 1.6 23-Sep-1993  mycroft Root can execute anything with at least one execute bit set.
 1.5 01-Aug-1993  mycroft Add RCS identifiers.
 1.4 10-Apr-1993  cgd from "Andrew A. Chernov, Black Mage" <ache@astral.msk.su>
When sh tried to find_command() for execution (without full path),
it checked ONLY ONE current effective group execution permissions,
but didn't check another valid groups from /etc/group.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.17.6.2 04-Mar-1997  mycroft Pull up latest sh(1). Fixes yet more bugs.
 1.17.6.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.29.4.1 03-Nov-2000  tv Pullup 1.31 [hubertf]:
Fixes bug in 'type' command WRT slashes.
 1.32.2.1 27-Mar-2002  elric Doing the vfork work on ash on a branch to try to shake out the
problems before I expose everyone to them. This checkin represents
a merge of the prior work, which I backed out a while ago, to the
HEAD only and does not incorporate any additional bugfixes. The
additional bugfixes and code-cleanup will occur in later checkins.

For reference the patches that were used are:
cvs diff -kk -r1.51 -r1.55 eval.c | patch
cvs diff -kk -r1.27 -r1.28 exec.c | patch
cvs diff -kk -r1.15 -r1.16 exec.h | patch
cvs diff -kk -r1.32 -r1.33 input.c | patch
cvs diff -kk -r1.10 -r1.11 input.h | patch
cvs diff -kk -r1.32 -r1.35 jobs.c | patch
cvs diff -kk -r1.9 -r1.11 jobs.h | patch
cvs diff -kk -r1.36 -r1.37 main.c | patch
cvs diff -kk -r1.20 -r1.21 redir.c | patch
cvs diff -kk -r1.10 -r1.11 redir.h | patch
cvs diff -kk -r1.10 -r1.12 shell.h | patch
cvs diff -kk -r1.22 -r1.23 trap.c | patch
cvs diff -kk -r1.12 -r1.13 trap.h | patch
cvs diff -kk -r1.23 -r1.24 var.c | patch
cvs diff -kk -r1.16 -r1.17 var.h | patch

All other changes were simply the resolution of the resulting
conflicts, which occured only in the merge of jobs.c.

Begins to address PR: bin/5475
 1.40.4.1 23-Mar-2008  matt sync with HEAD
 1.42.20.1 16-Nov-2016  snj Pull up following revision(s) (requested by dholland in ticket #1412):
bin/sh/exec.c: revision 1.45
bin/sh/var.c: revision 1.43
PR/48312: Dieter Roelants: According to TOG, unset should not return an error
for functions are variables that were not previously set:
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
 1.42.18.3 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.42.18.2 23-Jan-2013  yamt sync with head
 1.42.18.1 17-Apr-2012  yamt sync with head
 1.43.2.2 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.43.2.1 25-Feb-2013  tls resync with head
 1.46.6.1 19-May-2017  pgoyette Resolve conflicts from previous merge (all resulting from $NetBSD
keywork expansion)
 1.47.2.5 07-Dec-2020  martin Pull up following revision(s) (requested by kre in ticket #1629):

bin/sh/exec.c: revision 1.54

PR bin/55526

Fix a bug that has existed since the "command" command was added in
2003. "command foo" would cause the definition of a function "foo"
to be lost (not freed, simply discarded) if "foo" is (in addition to
being a function) a filesystem command. The case where "foo" is
a builtin was handled.

For now, when a function exists with the same name as a filesystem
command, the latter can never appear in the command hash table, and
when used (which can only be via "command foo", just "foo" finds
the function) will always result in a full PATH search.

XXX pullup everything (from NetBSD 2.0 onwards). (really -8 and -9)
 1.47.2.4 25-Aug-2018  martin Pull up following revision(s) (requested by kre in ticket #989):

bin/sh/eval.c: revision 1.156
bin/sh/eval.h: revision 1.20
bin/sh/exec.c: revision 1.53

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
 1.47.2.3 13-Jul-2018  martin Pull up following revision(s) (requested by kre in ticket #906):

bin/sh/eval.c: revision 1.155
bin/sh/mknodes.sh: revision 1.3
bin/sh/nodes.c.pat: revision 1.14
bin/sh/exec.h: revision 1.27
bin/sh/exec.c: revision 1.52

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).
 1.47.2.2 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.47.2.1 05-Jun-2017  snj Pull up following revision(s) (requested by kre in ticket #5):
bin/sh/cd.c: revision 1.48
bin/sh/eval.c: revision 1.141
bin/sh/exec.c: revision 1.48
bin/sh/exec.h: revision 1.25
bin/sh/mail.c: revisions 1.17, 1.18
bin/sh/sh.1: revision 1.147
Make cd (really) do cd -P, and not just claim that is what it is doing
while doing a half-hearted, broken, partial, version of cd -L instead.
The latter (as the manual says) is not supported, what's more, it is an
abomination, and should never be supported (anywhere.)
Fix the doc so that the pretense that we notice when a path given crosses
a symlink (and turns on printing of the destination directory) is claimed
no more (that used to be true until late Dec 2016, but was changed). Now
the print happens if -o cdprint is set, or if an entry from CDPATH that is
not "" or "." is used (or if the "cd dest repl" cd cmd variant is used.)
Fix CDPATH processing: avoid the magic '%' processing that is used for
PATH and MAILPATH from corrupting CDPATH. The % magic (both variants)
remains undocumented.
Also, don't double the '/' if an entry in PATH or CDPATH ends in '/'
(as in CDPATH=":/usr/src/"). A "cd usr.bin" used to do
chdir("/usr/src//usr.bin"). No more. This is almost invisible,
and relatively harmless, either way....
Also fix a bug where if a plausible destination directory in CDPATH
was located, but the chdir() failed (eg: permission denied) and then
a later "." or "" CDPATH entry succeeded, "print" mode was turned on.
That is:
cd /tmp; mkdir bin
mkdir -p P/bin; chmod 0 P/bin
CDPATH=/tmp/P:
cd bin
would cd to /tmp/bin (correctly) but print it (incorrectly).
Also when in "cd dest replace" mode, if the result of the replacement
generates '-' as the path named, as in:
cd $PWD -
then simply change to '-' (or attempt to, with CDPATH search), rather
than having this being equivalent to "cd -")
Because of these changes, the pwd command (and $PWD) essentially
always acts as pwd -P, even when called as pwd -L (which is still
the default.) That is, even more than it did before.
Also fixed a (kind of minor) mem management error (CDPATH related)
"whosoever shall padvance must stunalloc before repeating" (and the
same for MAILPATH).
--
If we are going to keep the MAILPATH % hack, then at least do something
rational. Since it isn't documented, what "rational" is is up for
discussion, but what it did before was not it (it was nonsense...).
 1.51.4.2 28-Jul-2018  pgoyette Sync with HEAD
 1.51.4.1 25-Jun-2018  pgoyette Sync with HEAD
 1.52.2.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.52.2.2 21-Apr-2020  martin Sync with HEAD
 1.52.2.1 10-Jun-2019  christos Sync with HEAD
 1.53.2.2 06-Apr-2021  martin Pull up following revision(s) (requested by kre in ticket #1242):

bin/sh/input.c: revision 1.72
bin/sh/exec.c: revision 1.55

PR bin/55979

This fixes the MSAN detected reference to an unitialised variable
(an unitialised field in a struct) which happens when a command is
not found after a PATH search.
Aside from skipping some known to be going to fail exec*() calls
in some cases, the setting of the relevant field is irrelevant,
so this problem makes no practical difference to the shell, or any
shell script.

XXX (maybe) pullup -9


PR bin/55979

Correctly handle (ie: ignore completely) \0 chars (nuls) in the
shell command input stream (script, dot file, or stdin).
Previously nul chars were ignored correctly in the line in which
they occurred, but would cause trailing chars of that line to reappear
as the start of the following line. If there was just one \0 skipped,
this would generally result in an extra \n in the sh input, which in
most cases has no effect. With multiple \0's in a single line, more
of the end of that line was duplicated into the following one. This
usually manifested as a weird "command not found" error.

Note that any \0 chars in the sh input make the script non-conforming,
so fixing this is not crucial (no \0's should really ever be seen) but
it was an obvious bug in the code, which was attempting to ignore nul
chars (as do many other shells), so let it be fixed.

XXX pullup -9
 1.53.2.1 27-Aug-2020  martin Pull up following revision(s) (requested by kre in ticket #1064):

bin/sh/exec.c: revision 1.54

PR bin/55526

Fix a bug that has existed since the "command" command was added in
2003. "command foo" would cause the definition of a function "foo"
to be lost (not freed, simply discarded) if "foo" is (in addition to
being a function) a filesystem command. The case where "foo" is
a builtin was handled.

For now, when a function exists with the same name as a filesystem
command, the latter can never appear in the command hash table, and
when used (which can only be via "command foo", just "foo" finds
the function) will always result in a full PATH search.

XXX pullup everything (from NetBSD 2.0 onwards). (really -8 and -9)
 1.58.2.1 02-Aug-2025  perseant Sync with HEAD
 1.28 12-Jul-2024  kre Improve safety in var imports from the environment.

Add a new var flag VUNSAFE - set on all vars imported from the environment.

Add setvareqsafe() (which is to setvareq() as setvarsafe() is to setvar())
and use that instead of setvareq() when processing the environment, so
errors don't cause the shell to abort. Use VUNSAFE in that call.

Add flags arguments to all var callback functions which are used when setting
variables, and pass the flags given to the setvar*() functions to those
functions, so they can act differently in different situations (if desired).
Most of them just ignore the flags.

When unsetting a variable, call setvar() to clear things (and call the
callback function) both when the variable had a value which needs to be freed,
and when unsetting a variable which wasn't unset previously, so the VUNSET
flag can be seen by that callback func.

When setting HISTSIZE, use the flags passed to determine whether to ignore
bad values (if VUNSAFE) or treat them as an error. This replaces the
earlier temporary hack to always ignore bad data there (histedit.c 1.68).

Miscellaneous associated minor changes.

These changes should largely be invisible in normal use.
 1.27 22-Jun-2018  kre branches: 1.27.2; 1.27.14;
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).
 1.26 07-Jun-2017  kre branches: 1.26.4;
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)

This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)

The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.

This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).

I would not call the results of this perfect yet, but it is close.
 1.25 04-Jun-2017  kre Make cd (really) do cd -P, and not just claim that is what it is doing
while doing a half-hearted, broken, partial, version of cd -L instead.
The latter (as the manual says) is not supported, what's more, it is an
abomination, and should never be supported (anywhere.)

Fix the doc so that the pretense that we notice when a path given crosses
a symlink (and turns on printing of the destination directory) is claimed
no more (that used to be true until late Dec 2016, but was changed). Now
the print happens if -o cdprint is set, or if an entry from CDPATH that is
not "" or "." is used (or if the "cd dest repl" cd cmd variant is used.)

Fix CDPATH processing: avoid the magic '%' processing that is used for
PATH and MAILPATH from corrupting CDPATH. The % magic (both variants)
remains undocumented.

Also, don't double the '/' if an entry in PATH or CDPATH ends in '/'
(as in CDPATH=":/usr/src/"). A "cd usr.bin" used to do
chdir("/usr/src//usr.bin"). No more. This is almost invisible,
and relatively harmless, either way....

Also fix a bug where if a plausible destination directory in CDPATH
was located, but the chdir() failed (eg: permission denied) and then
a later "." or "" CDPATH entry succeeded, "print" mode was turned on.
That is:
cd /tmp; mkdir bin
mkdir -p P/bin; chmod 0 P/bin
CDPATH=/tmp/P:
cd bin
would cd to /tmp/bin (correctly) but print it (incorrectly).

Also when in "cd dest replace" mode, if the result of the replacement
generates '-' as the path named, as in:
cd $PWD -
then simply change to '-' (or attempt to, with CDPATH search), rather
than having this being equivalent to "cd -")

Because of these changes, the pwd command (and $PWD) essentially
always acts as pwd -P, even when called as pwd -L (which is still
the default.) That is, even more than it did before.

Also fixed a (kind of minor) mem management error (CDPATH related)
"whosoever shall padvance must stunalloc before repeating" (and the
same for MAILPATH).
 1.24 03-May-2016  kre branches: 1.24.8;

Fix things so that STATIC can me made static (-DSTATIC=static)
and have the shell still compile, link, and run...

ok christos@
 1.23 15-Mar-2012  joerg Add __printflike attribution to use vprintf and friends with an argument
as format string.
 1.22 18-Jun-2011  christos branches: 1.22.2;
PR/45069: Henning Petersen: Use prototypes from builtins.h .
 1.21 07-Aug-2003  agc branches: 1.21.52;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.20 22-Jan-2003  dsl Support command -p, -v and -V as posix
Stop temporary PATH assigments messing up hash table
Fix sh -c -e "echo $0 $*" -a x (as posix)
(agreed by christos)
 1.19 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.18 27-Sep-2002  christos VFork()ing shell: From elric@netbsd.org:
Plus my changes:
- walking process group fix in foregrounding a job.
- reset of process group in parent shell if interrupted before the wait.
- move INTON lower in the dowait so that the job structure is
consistent.
- error check all setpgid(), tcsetpgrp() calls.
- eliminate unneeded strpgid() call.
- check that we don't belong in the process group before we try to
set it.
 1.17 22-May-2000  elric branches: 1.17.6;
Back out previous vfork changes.
 1.16 13-May-2000  elric Now we use vfork(2) instead of fork(2) when we can.
 1.15 09-Jul-1999  christos compile with WARNS = 2
 1.14 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.13 20-Jul-1997  christos PR/3888: Chris Demetriou: type command-with-slash prints
$PATH[0]/command-with-slash...
 1.12 06-Feb-1997  christos add type builtin.
 1.11 16-Oct-1996  christos PR/287: Shell does not exit with 126/127 when permission denied/
command not found. Add extra exception type and generalize
error handling routines to take that exception type. Use
a global variable exerrno to keep the last exec error.
 1.10 25-Jun-1996  christos const poisoning.
 1.9 09-Jun-1995  christos branches: 1.9.6;
Changed so that 'PATH=newpath command' works, instead of looking at the
old path. Synced input.c with vangogh.
 1.8 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.7 21-Mar-1995  cgd convert to new RCS id conventions.
 1.6 11-Jun-1994  mycroft Add RCS ids.
 1.5 11-May-1994  jtc sync with 4.4lite
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.9.6.2 04-Mar-1997  mycroft Pull up latest sh(1). Fixes yet more bugs.
 1.9.6.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.17.6.1 27-Mar-2002  elric Doing the vfork work on ash on a branch to try to shake out the
problems before I expose everyone to them. This checkin represents
a merge of the prior work, which I backed out a while ago, to the
HEAD only and does not incorporate any additional bugfixes. The
additional bugfixes and code-cleanup will occur in later checkins.

For reference the patches that were used are:
cvs diff -kk -r1.51 -r1.55 eval.c | patch
cvs diff -kk -r1.27 -r1.28 exec.c | patch
cvs diff -kk -r1.15 -r1.16 exec.h | patch
cvs diff -kk -r1.32 -r1.33 input.c | patch
cvs diff -kk -r1.10 -r1.11 input.h | patch
cvs diff -kk -r1.32 -r1.35 jobs.c | patch
cvs diff -kk -r1.9 -r1.11 jobs.h | patch
cvs diff -kk -r1.36 -r1.37 main.c | patch
cvs diff -kk -r1.20 -r1.21 redir.c | patch
cvs diff -kk -r1.10 -r1.11 redir.h | patch
cvs diff -kk -r1.10 -r1.12 shell.h | patch
cvs diff -kk -r1.22 -r1.23 trap.c | patch
cvs diff -kk -r1.12 -r1.13 trap.h | patch
cvs diff -kk -r1.23 -r1.24 var.c | patch
cvs diff -kk -r1.16 -r1.17 var.h | patch

All other changes were simply the resolution of the resulting
conflicts, which occured only in the merge of jobs.c.

Begins to address PR: bin/5475
 1.21.52.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.22.2.1 17-Apr-2012  yamt sync with head
 1.24.8.3 13-Jul-2018  martin Pull up following revision(s) (requested by kre in ticket #906):

bin/sh/eval.c: revision 1.155
bin/sh/mknodes.sh: revision 1.3
bin/sh/nodes.c.pat: revision 1.14
bin/sh/exec.h: revision 1.27
bin/sh/exec.c: revision 1.52

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).
 1.24.8.2 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.24.8.1 05-Jun-2017  snj Pull up following revision(s) (requested by kre in ticket #5):
bin/sh/cd.c: revision 1.48
bin/sh/eval.c: revision 1.141
bin/sh/exec.c: revision 1.48
bin/sh/exec.h: revision 1.25
bin/sh/mail.c: revisions 1.17, 1.18
bin/sh/sh.1: revision 1.147
Make cd (really) do cd -P, and not just claim that is what it is doing
while doing a half-hearted, broken, partial, version of cd -L instead.
The latter (as the manual says) is not supported, what's more, it is an
abomination, and should never be supported (anywhere.)
Fix the doc so that the pretense that we notice when a path given crosses
a symlink (and turns on printing of the destination directory) is claimed
no more (that used to be true until late Dec 2016, but was changed). Now
the print happens if -o cdprint is set, or if an entry from CDPATH that is
not "" or "." is used (or if the "cd dest repl" cd cmd variant is used.)
Fix CDPATH processing: avoid the magic '%' processing that is used for
PATH and MAILPATH from corrupting CDPATH. The % magic (both variants)
remains undocumented.
Also, don't double the '/' if an entry in PATH or CDPATH ends in '/'
(as in CDPATH=":/usr/src/"). A "cd usr.bin" used to do
chdir("/usr/src//usr.bin"). No more. This is almost invisible,
and relatively harmless, either way....
Also fix a bug where if a plausible destination directory in CDPATH
was located, but the chdir() failed (eg: permission denied) and then
a later "." or "" CDPATH entry succeeded, "print" mode was turned on.
That is:
cd /tmp; mkdir bin
mkdir -p P/bin; chmod 0 P/bin
CDPATH=/tmp/P:
cd bin
would cd to /tmp/bin (correctly) but print it (incorrectly).
Also when in "cd dest replace" mode, if the result of the replacement
generates '-' as the path named, as in:
cd $PWD -
then simply change to '-' (or attempt to, with CDPATH search), rather
than having this being equivalent to "cd -")
Because of these changes, the pwd command (and $PWD) essentially
always acts as pwd -P, even when called as pwd -L (which is still
the default.) That is, even more than it did before.
Also fixed a (kind of minor) mem management error (CDPATH related)
"whosoever shall padvance must stunalloc before repeating" (and the
same for MAILPATH).
--
If we are going to keep the MAILPATH % hack, then at least do something
rational. Since it isn't documented, what "rational" is is up for
discussion, but what it did before was not it (it was nonsense...).
 1.26.4.1 25-Jun-2018  pgoyette Sync with HEAD
 1.27.14.1 02-Aug-2025  perseant Sync with HEAD
 1.27.2.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.27.2.1 21-Apr-2020  martin Sync with HEAD
 1.147 07-May-2025  kre Obliterate the global var herefd

For the past several years (since Nov 2021) the global var "herefd"
has led a rather meaningless existence.

It gets statically init'd to -1 (at compile time), and several
times its value is carefully saved, set to -1, then restored later.
(Lots of copying around of -1 going on there!)

The only use of its value was removed in the immediately previous update
to memalloc.c, where it was tested, and if its value were >= 0 ...
(no point mentioning what would happen, since its value is always -1,
since Nov 2021).

Bye bye herefd, it was nice knowing you. May we never meet again.
 1.146 21-Oct-2024  kre Fix processing of unknown variable expansion types.

Our shell is (was) one of the last not to do this correctly.

Expansions are supposed to happen only when the command in which
they occur is being executed, not while it is being parsed.
If the expansion only happens them, errors should only be
detected then.

Make it work like that (I saw after I fixed this that FreeBSD
had done it, long ago, almost the same way - it is kind of an
obvious thing to do).

This will allow code like

if test it is shell X
then
commands using shell X specific expansion ops
else if it is shell Y
then
commands using shell Y specific expansion ops
else ...
fi

Previously expansion errors were detected while parsing, so
if we're not shell X, and don't implement something that it
does (some extension to the standard) that would have generated
a parser syntax error, and the script could not be executed
(despite the line with the error never being executed).

Note that this change does not handle all such possible
extensions, just this one. Others are much harder.

One side effect of this change is that sh will now continue
reading a variable expansion until it locates the terminating
'}' (in ${var} forms) regardless of how broken it obviously
is (to our shell) whereas previously it would have bailed out
as soon as an oddity was spotted.
 1.145 03-Oct-2024  rillig bin: fix lint warning "effectively discards 'const'"

For example: src/bin/ed/io.c(339): warning: call to 'strchr' effectively
discards 'const' from argument [346]

No binary change.
 1.144 29-Dec-2023  kre branches: 1.144.2;
PR bin/57773

Fix another bug reported by Jarle Fredrik Greipsland and added
to PR bin/57773, which relates to calculating the length of a
positional parameter which contains CTL chars -- yes, this one
really is that specific, though it would also affect the special
param $0 if it were to contain CTL chars, and its length was
requested - that is fixed with the same change. And note: $0
is not affected because it looks like a positional param (it
isn't, ${00} would be, but is always unset, ${0} isn't) all
special parame would be affected the same way, but the only one
that can ever contain a CTL char is $0 I believe. ($@ and $*
were affected, but just because they're expanding the positional
params ... ${#@} and ${#*} are both technically unspecified
expansions - and different shells produce different results.

See the PR for the details of this one (and the previous).

Thanks for the PR.

XXX pullup to everything.
 1.143 25-Dec-2023  kre Correct a bizarre piece of source formatting that crept in by
accident several years ago (change a space into newline tab).

NFC
 1.142 06-Mar-2023  kre Adjust tilde expansion as will be documented in the forthcoming
version of the POSIX standard (Issue 8). I believe we were already
compliant with what is to be required, but POSIX is now encouraging
(and will likely require in a later version) that if a tilde expansion
produces a string which ends in a '/' and the '~' that was expanded
is immediately followed by a '/' in the input word, that one of those
two slashes be omitted. The worst (current) example of this is
when HOME=/ and we expand ~/foo - previously producing //foo which is
(in POSIX) a path with implementation defined semantics, and so not
what we should be generating by accident. Change that, so now if
the ~ prefix expansion ends in a '/' and there is a '/' following
immediately after, the resulting word contains only one of those
chars (in the example just given, we will now produce /foo instead).

POSIX is also making it clear that the expansion that results from
the tilde expansion is treated as quoted (not subject to pathname
expansion, or field splitting, or any var/arith/command substitutions)
and that if HOME="" the expansion of ~ must generate "" (not nothing).
Our implementation did all of that already (though older versions
used to treat an empty expansion of HOME the same as if HOME was
unset - that was fixed some time ago).

The actual modification made here is probably smaller than this log entry,
and without added comments, certainly is!
 1.141 22-Nov-2021  kre branches: 1.141.2;

PR bin/53550

Here we go again... One more time to redo how here docs are
processed (it has been a few years since the last time!)

This is actually a relatively minor change, mostly to timimg
(to just when things happen). Now here docs are expanded at the
same time the "filename" word in a redirect is expanded, rather than
later when the heredoc was being sent to its process. This actually
makes things more consistent - but does break one of the ATF tests
which was testing that we were (effectively) internally inconsistent
in this area.

Not all shells agree on the context in which redirection expansions
should happen, some make any side effects visible to the parent shell
(the majority do) others do the redirection expansions in a subshell
so any side effcts are lost. We used to have a foot in each camp,
with the majority for everything but here docs, and the minority for
here docs. Now we're all the way with LBJ ... (or something like that).
 1.140 10-Nov-2021  kre DEBUG mode changes only. NFC (NC) for any normally compiled shell.

Mostly adding DEBUG mode tracing (when appropriate verbose tracing
is enabled generally) whenever a shell (including sushell) process
exits, so shells that the tracing should indicate why ehslls that
vanish did that.

Note for future investigators: if the relevant tracing is enabled,
and a (sub-)shell still simply seems to have vanished without trace,
the likely cause is that it was killed by a signal - and of those,
the most common that occurs is SIGPIPE.
 1.139 10-Sep-2021  rillig bin: remove unnecessary lint comment CONSTCOND

Since 2021-01-31, lint no longer warns about 'do ... while (0)'.

No functional change.
 1.138 01-Aug-2020  kre Remove a redundant set of parentheses that were added (along with a
extra && or || or something ... forgotten now) as part a failed attempt
to fix an earlier bug (later fixed a better way) - when the extra
test (never committed) was removed, the now-redundant parentheses got
forgotten...

NFC.
 1.137 13-Feb-2020  kre When expanding a here-doc (NXHERE - the type with an unquoted end delim)
the output will not be further processed (at all) so there is no need
to escape magic chars in the output, and doing so leaves stray CTLESC
chars in the here doc text. Not good. So don't do that...

To save a strlen() of the result, to determine the size of the here doc,
make rmescapes() return the length of the resulting string (this isn't
needed for other uses, so didn't happen previously).

Reported on current-users@ (2020-02-06) by Jun Ebihara

XXX pullup -9
 1.136 14-Oct-2019  christos remove masking and cast (requested by kre@)
 1.135 13-Oct-2019  christos prevent sign extension from making expression always false.
 1.134 08-Oct-2019  kre Remove a (completely harmless) duplicate assignment introduced in a
code merge from FreeBSD in 2017. NFC.

Pointed out by Roland Illig.
 1.133 08-Oct-2019  kre Open code the validity test & copy of the character class name in
a bracket expression in a pattern (ie: [[:THISNAME:]]). Previously
the code used strspn() to look for invalid chars in the name, and
then memcpy(), now we do the test and copy a character at a time.
This might, or might not, be faster, but it now correctly handles
\ quoted characters in the name (' and " quoting were already
dealt with, \ was too in an earlier version, but when the \ handling
changes were made, this piece of code broke).

Not exactly a vital bug fix (who writes [[:\alpha:]] or similar?)
but it should work correctly regardless of how obscure the usage is.

Problem noted by Harald van Dijk

XXX pullup -9
 1.132 10-Apr-2019  kre branches: 1.132.2;
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.
 1.131 27-Feb-2019  kre 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.
 1.130 04-Feb-2019  kre 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).
 1.129 03-Dec-2018  kre 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...
 1.128 18-Nov-2018  kre 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).
 1.127 22-Jul-2018  kre 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.)
 1.126 22-Jul-2018  kre NFC: Whitespace cleanups
 1.125 22-Jul-2018  kre DEBUG mode only change (ie: no effect to any normal shell).

Add tracing of pattern matching (aid in debugging various issues.)
 1.124 20-Jul-2018  kre 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),
 1.123 22-Jun-2018  kre branches: 1.123.2;

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).
 1.122 22-Jun-2018  kre 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.
 1.121 06-Oct-2017  kre branches: 1.121.2;
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).
 1.120 21-Aug-2017  kre 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 ...
 1.119 30-Jun-2017  kre Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
 1.118 19-Jun-2017  kre Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
 1.117 18-Jun-2017  kre NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
 1.116 17-Jun-2017  kre Many internal memory management type fixes.

PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)

echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)

(Recently added) Problems with ~ expansion fixed (mem management related).

Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)

And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.

Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,

More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.

[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]

More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).

User visible changes:

Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in

cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END

(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)

With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)

POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).

Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
 1.115 07-Jun-2017  kre PR bin/52280

removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
 1.114 07-Jun-2017  kre Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
 1.113 07-Jun-2017  kre A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)

This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)

The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.

This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).

I would not call the results of this perfect yet, but it is close.
 1.112 05-Jun-2017  kre Another arithmetic expansion recordregion() fix, this time
calculate the lenght (used to calculate the end) based upon the
correct starting point.

Thanks to John Klos for finding and reporting this one.
 1.111 04-Jun-2017  kre PR bin/52272 - fix an off-by one that broke ~ expansions.
 1.110 03-Jun-2017  kre branches: 1.110.2;

DEBUG mode only change. Convert old trace style to new, and add some more.
NFC for any non-DEBUG shell.
 1.109 03-Jun-2017  kre NFC: Code style only. Rather than being perverse and adding the
negative of a negative number, just add a positive number instead...
(the previous version came about purely as an accident of the way the
relevant piece of code was added and debugged.... that's my story anyway!)
 1.108 03-Jun-2017  kre The correct usage of recordregion() is (begin, end) not (begin, length).

Fixing this fixes a regression introduced earlier today (UTC) where
arithmetic expressions would be split correctly when the arithmetic
started at the beginning of a word:
echo $(( expression ))
where "begin" is 0, and so (begin, length) is the same as (begin, begin+length)
(aka: (begin,end) - and yes, "end" means 1 after last to consider).
but did not work correctly when the usage was
echo XXX$( expression ))
(begin !+ 0) and would only split (some part of) the result of the expression.

This regression was also foung by the new t_fsplit:split_arith
test case added earlier to the ATF tests for sh.
 1.107 03-Jun-2017  kre Fixes to shell expand (that is, $ stuff) from FreeBSD (implemented
differently...)

In particular ${01} is now $1 not $0 (for ${0any-digits})

${4294967297} is most probably now ""
(unless you have a very large number of params)
it is no longer an alias for $1 (4294967297 & 0xFFFFFFFF) == 1

$(( expr $(( more )) stuff )) is no longer the same as
$(( expr (( more )) stuff )) which was sometimes OK, as in:
$(( 3 + $(( 2 - 1 )) * 3 ))
but not always as in:
$(( 1$((1 + 1))1 ))
which should be 121, but was an arith syntax error as
1((1 + 1))1
is meaningless.

Probably some more. This also sprinkles a little const, splits a big
func that had 2 (kind of unrelated) purposes into two simpler ones,
and avoids some (semi-dubious) modifications (and restores) in the input
string to insert \0's when they were needed.
 1.106 28-May-2017  kre Arrange for set -o and $- output to be sorted, rather than more
or less random (and becoming worse as more options are added.)
Since the data is known at compile time, sort at compile time,
rather than at run time.
 1.105 26-Apr-2017  christos Convert the pattern matcher from recursive to backtracking (from FreeBSD).
 1.104 20-Mar-2017  kre branches: 1.104.2;

PR bin/52090 - fix expansion of unquoted $*
 1.103 20-Mar-2017  kre Finish support for all required $(( )) (shell arithmetic) operators,
closing PR bin/50958

That meant adding the assignment operators ('=', and all of the +=, *= ...)
Currently, ++, --, and ',' are not implemented (none of those are required
by posix) but support for them (most likely ',' first) might be added later.

To do this, I removed the yacc/lex arithmetic parser completely, and
replaced it with a hand written recursive descent parser, that I obtained
from FreeBSD, who earlier had obtained it from dash (Herbert Xu).

While doing the import, I cleaned up the sources (changed some file names
to avoid requiring a clean build, or signifigant surgery to the obj
directories if "build.sh -u" was to be used - "build.sh -u" should work
fine as it is now) removed some dashisms, applied some KNF, ...
 1.102 12-Mar-2017  kre Fix for the "${unset-var#$(cmd1)}$(cmd2)" runs the wrong command bug.
... From FreeBSD
 1.101 31-Mar-2016  christos branches: 1.101.2; 1.101.4;
Implement the NETBSD_SHELL readonly unexportable unimportable
variable (with its current value set at 20160401) as discussed on
current-users and tech-userlevel. This also includes the necessary
support to implement it properly (particularly the unexportable
part) and adds options to the export command to support unexportable
variables. Also implement the "posix" option (no single letter
equivalent) which gets its default value from whether or not
POSIXLY_CORRECT is set in the environment when the shell starts
(but can be changed just like any other option using -o and +o on
the command line, or the set builtin command.) While there, fix
all uses of options so it is possible to have options that have a
short (one char) name, and no long name, just as it has been possible
to have options with a long name and no short name, though there
are currently none (with no long name). For now, the only use of
the posix option is to control whether ${ENV} is read at startup
by a non-interactive shell, so changing it with set is not usful
- that might change in the future. (from kre@)
 1.100 31-Mar-2016  christos After discussions with Jilles Tjoelker (FreeBSD shell) and following
a suggestion from him, the way the fix to PR bin/50993 was implemented
has changed a little. There are three steps involved in processing
a here document, reading it, parsing it, and then evaluating it
before applying it to the correct file descriptor for the command
to use. The third of those is not related to this problem, and
has not changed. The bug was caused by combining the first two
steps into one (and not doing it correctly - which would be hard
that way.) The fix is to split the first two stages into separate
events. The original fix moved the 2nd stage (parsing) to just
immediately before the 3rd stage (evaluation.) Jilles pointed out
some unwanted side effects from doing it that way, and suggested
moving the 2nd stage to immediately after the first. This commit
makes that change. The effect is to revert the changes to expand.c
and parser.h (which are no longer needed) and simplify slightly
the change to parser.c. (from kre@)
 1.99 27-Mar-2016  christos PR bin/50993 - this is a significant rewrite of the way that here
documents are processed. Now, when first detected, they are
simply read (the only change made to the text is to join lines
ended with a \ to the subsequent line, otherwise end marker detection
does not work correctly (for here docs with an unquoted endmarker
only of course.) This patch also moves the "internal subroutine"
for looking for the end marker out of readtoken1() (which had to
happen as readtoken1 is no longer reading the here doc when it is
needed) - that uses code mostly taken from FreeBSD's sh (thanks!)
and along the way results in some restrictions on what the end
marker can be being removed. We still do not allow all we should.
(from kre@)
 1.98 27-Mar-2016  christos General KNF and source code cleanups, avoid scattering the
magic string " \t\n" all over the place, slightly improved
syntax error messages, restructured some of the code for
clarity, don't allow IFS to be imported through the environment,
and remove the (never) conditionally compiled ATTY option.
Apart from one or two syntax error messages, and ignoring IFS
if present in the environment, this is intended to have no
user visible changes. (from kre@)
 1.97 16-Mar-2016  christos PR/19832, PR/35423: Fix handling 0x81 and 0x82 characters in expansions
($VAR etc) that are used to generate filenames for redirections. (from kre)
 1.96 08-Mar-2016  christos PR bin/50834o: fix expansions of (unquoted) ${unset_var-} and ""$@ (from kre)
 1.95 27-Feb-2016  christos remove useless casts
 1.94 22-Feb-2016  christos PR bin/43469 - correctly handle quoting of the pattern part of ${var%pat}
type expansions. (from kre)
 1.93 27-Aug-2015  christos PR/50179: Timo Buhrmester: sh(1) variable expansion bug
 1.92 06-Jun-2015  joerg Use an explicit body for a "until not EINTR" loop.
 1.91 20-Jan-2014  roy branches: 1.91.4;
Add wctype(3) support to Shell Patterns.
Obtained from FreeBSD.
 1.90 06-Oct-2013  ast Fix PR bin/48202 [non-critical/low]:
sh +nounset and `for X; do` iteration fails if parameter set empty
by applying and testing FreeBSD's patch of Oct 24 2009 for this; see
http://svnweb.freebsd.org/base/head/bin/sh/expand.c?r1=198453&r2=198454
Also created an ATF test in tests/bin/sh/t_expand.sh for this error and
corrected a space->tabs problem there as well.
 1.89 02-Oct-2013  christos add crude $LINENO support for FreeBSD
 1.88 22-Dec-2012  dsl Fix the expansion of "$(foo-$bar}" so that IFS isn't applied when
expanding $bar.
Noted by Greg Troxel on tech-userlevel running some 'git' tests.
Should fix PR bin/47361
 1.87 28-Mar-2012  christos branches: 1.87.2;
include <limits.h> for CHAR_MIN/CHAR_MAX
 1.86 31-Aug-2011  plunky branches: 1.86.2; 1.86.4;
NULL does not need a cast
 1.85 23-Aug-2011  christos PR/45269: Andreas Gustafsson: Instead of falling off the edge when eating trailing newlines
if the block has moved, arrange so that trailing newlines are never placed in the string
in the first place, by accumulating them and adding them only after we've encountered a
non-newline character. This allows also for more efficient appending since we know how much
we need beforehand. From FreeBSD.
 1.84 18-Jun-2011  christos PR/45069: Henning Petersen: Use prototypes from builtins.h .
 1.83 27-Nov-2009  tsutsui branches: 1.83.4;
Use %zu in printf format for size_t value.
 1.82 18-Jan-2009  lukem fix -Wsign-compare issues
 1.81 21-Dec-2008  christos use EXP_CASE only when trimming and unquoted.
 1.80 20-Dec-2008  christos PR/36954: Roland Illig: don't eat backlash escapes in variable patterns.
Makes ${line%%\**} work.
 1.79 16-Oct-2008  dholland branches: 1.79.2;
The field width passed for a %.*s printf format is supposed to be int, not
ptrdiff_t; on 64-bit platforms the latter will be too wide.
Adjust accordingly.
 1.78 25-Mar-2007  apb Make /bin/sh use intmax_t (instead of int) for arithmetic in $((...)).
 1.77 24-Nov-2006  wiz branches: 1.77.2;
s/apparant/apparent/, from Zafer.
 1.76 22-Aug-2006  dsl Set the 'not a parameter' flag when we skip initial whitespace.
Otherwise:
./sh -c 'x=" "; for a in $x; do echo a${a}a; done'
is processed as a single empty parameter (instead of no parameters).
Should fix the breakage I introdiced in rev 1.75 and PR/34256 and PR/34254
 1.75 21-Aug-2006  dsl Rework the code changes from revisions 1.69, 1.70 and 1.74 so that the code
behaves correctly.
As far as I can tell, "x$@y" now expands correctly, as does IFS=:; set -$IFS.
Fixes PR/33472 (again) and PR/33956
 1.74 20-May-2006  dsl branches: 1.74.2;
When expanding "$@" add a \0 byte after the last argument (as well as all
the earlier ones) so that a separator is added before it when it is empty.
This wasn't needed before a recent change that chenged the behaviour of
trailing whitespace IFS characters.
Fixed PR/33472
 1.73 18-Mar-2006  christos Coverity CID 620: Remove dead code.
 1.72 13-Dec-2005  dsl TOG require that 'set +o' output the options in a form suitable for
restoring them - make it so.
 1.71 01-Jun-2005  lukem appease gcc -Wuninitialized
 1.70 19-Mar-2005  dsl Check quoting before merging ifs regions.
sh -c 'set -- a; x="b c"; set -- "$@"$x'
now correctly gives $1=ab, $2=c
 1.69 19-Mar-2005  dsl Don't merge ifs regions with different quoting requirements
 1.68 14-Feb-2005  dsl branches: 1.68.2;
expbackq() was incorrectly backing up a temporary buffer when removing \n
from the end of output of commands inside $(...) substitutions.
If the program output is n*128+1 bytes long (ending in a \n) then the code
checks buf[-1] for another \n - looking an uninitialised stack.
On a big-endian system an integer of value 10 will satisfy this (unlikely
on little endian) and can happen depending on the last code path to use
a lot of stack!
This caused the problem with newvers.sh on sparc64 after ', 2005' was
added to the date list.
Fixed PR/28852
 1.67 13-Jul-2004  seb Add new builtin `wordexp' to support wordexp(3).

From FreeBSD.
Provided in PR lib/26123.
Approved by kleink@.
 1.66 26-Jun-2004  dsl Correctly apply IFS to unquoted text in ${x-text}.
Fixes PR/26058 and the 'for i in ${x-a b c}; do ...' and ${x-'a b' c}.
I can't find a PR for the latter problem.
Regression test goind in shortly.
 1.65 26-Jun-2004  dsl Remove a broken optimistion that crept in earlier today.
 1.64 26-Jun-2004  dsl Kill a diagnostic I accidentally left in.
 1.63 26-Jun-2004  dsl No functional changes (intended).
Rename some variables, add some comments, and restructure a little.
In preparation for fixing "set ${x-a b c}" and friends.
 1.62 09-Jun-2004  christos Undo previous fix, breaks:
#!/bin/sh
echo ${1+"$@"}
./sh.new foo.sh a b c
a b c b c
I'll revisit this when I have some more time.
 1.61 08-Jun-2004  christos "for i in ${x-a b c}; do echo $i; done" should print "a\nb\nc\n" not "a b c\n"
like other shells do. mark the expansion for ifs splitting. XXX: linux has a
very complicated fix for this. I wonder why.
 1.60 21-Dec-2003  jdolecek branches: 1.60.2; 1.60.4;
minor optimization in evalvar()
change sent in bin/23813 by VaX#n8
 1.59 22-Sep-2003  dsl Fix 'set "*" b; case "* b" in "$@") ...' and 'set "*"; case 1 in "${#1}") ...'
Which got broken by the previous fix.
 1.58 17-Sep-2003  christos PR/22640: Paul Jarc: sh mishandles positional parameters in case. Fixed
from FreeBSD PR 56147.
 1.57 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.56 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.55 28-Sep-2002  christos Revert previous change. No need to save rootshell. It is only affecting
the non-vfork case. Having said that, it would be nice if pipelines of
simple commands were vforked too. Right now they are not.
Explain that setpgid() might fail because we are doing it both in the
parent and the child case, because we don't know which one will come
first.
Suspending a pipeline prints %1 Suspended n times where n is the number
of processes, but that was there before. It is easy to fix, but I'll
leave the code alone for now.
 1.54 27-Sep-2002  christos Deal with rootshell not being maintained correctly in the vfork() case.
Propagate isroot, throughout the eval process and maintain it properly.
Fixes sleep 10 | cat^C not exiting because sleep and cat ended up in
their own process groups, because wasroot was always true in the children.
 1.53 15-May-2002  christos Implement unset variable error messages from Ben Harris.
 1.52 19-Sep-2001  itojun make sure we do not truncate arith expresssion > 10 digits.
freebsd bin/sh/expand.c revision 1.15. NetBSD PR 13943.
 1.51 30-Mar-2001  mycroft Globbing should match broken symlinks. stat()->lstat() to fix this.
 1.50 04-Feb-2001  christos remove redundant declarations and nexted externs.
 1.49 13-Mar-2000  soren branches: 1.49.4;
Fix doubled 'the' in comment.
 1.48 09-Jul-1999  christos compile with WARNS = 2
 1.47 30-Apr-1999  he Fix for bin/7502, from Tor Egge / FreeBSD. Their commit message:
> During variable expansion, the internal representation of the expression
> might be relocated. Handle this case.
 1.46 06-Apr-1999  he Correct a rather obvious typo (once Tor Egge pointed it out to me)
in the last change.
 1.45 26-Mar-1999  christos branches: 1.45.2;
PR/7231: Havard Eidnes: Shell quoting/trimming problem
 1.44 06-Feb-1999  christos Fix off-by-one error in the starting point to search for an arithmetic
expression.
 1.43 05-Feb-1999  christos PR/5577: Craig M. Chase: sh does not build with PARALLEL set.
- Added YHEADER in Makefile, removed arith.h and adjusted the sources.
 1.42 25-Jan-1999  mycroft Patches from Tor Egge (via Havard Eidnes) to fix various bugs in field
splitting and combining.
(Note: Some of this are not strictly bugs, but differences between traditional
Bourne shell and POSIX.)
 1.41 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.40 26-Jul-1998  mycroft const poisoning.
 1.39 29-Mar-1998  mrg - change "extern" variables into int's
- remove extern'd variables not actually referenced
- don't use char as an array index
 1.38 23-Mar-1998  christos Fix the VSTRIMRIGHT* bugs... The problem was not the string length computation,
but lack of '\0' termination. Factor this segment out as common code too, while
I am there.
 1.37 10-Mar-1998  christos off by one error in ${%%}
 1.36 17-Feb-1998  christos Previous fix broke $var quoting. Try again differently :-)
 1.35 05-Feb-1998  christos Fix bug introduced by EXP_RECORD, where in case there was a variable expansion
involved in the `for' list, the list was recorded twice, leading to incorrect
argument expansion.
Introduce ifsfree() function that free's the IFS region list, GC'ing duplicated
code.
 1.34 31-Jan-1998  christos PR/4851: Benjamin Lorenz: In the "for <var> in <args>" construct <args>
was not marked as a region to be handled by ifsbreakup. Add EXP_RECORD
to indicate that the argument string needs to be recorded.
 1.33 01-Dec-1997  christos Unfortunately (as I expected) the previous change broke:

sleep
cmd='set `type "sleep"`; eval echo \$$#'
which=`eval $cmd`
echo $which

because the region did not get recorded at all, and it was interpreted as
a single word. I modified the code to keep track when the result of a
backquote expansion has been recorded to avoid recording it twice. I still
feel that this is not the right fix... More to come.
 1.32 30-Nov-1997  christos PR/4547: Joern Clausen: Incorrect argument expansion in backquote variable
assignment. E.g.
echo ${foo:=`echo 1 2 3 4`}
prints:
1 2 3 1 2 3 4
because when the arquments are not quoted, the backquote result
gets recorded twice. The fix right now is to comment out the
record_region() call in expbackq(). I hope that it does not break
anything else.
 1.31 07-Jul-1997  phil branches: 1.31.2;
Make code agree with man page in processing expansion of "$*".
Fix from PR 2647.
 1.30 04-Jul-1997  christos Fix compiler warnings.
 1.29 18-Mar-1997  christos PR/3352: From Hiroyuki Ito: ${#1} was not being expanded properly if there
was a need to allocated another stack block.
 1.28 03-Mar-1997  christos varisset fixes:
- treat $0 specially since it is not in shellparams
- check the number of parameters instead of walking
the parameters array to avoid checking against the
null terminated element.
 1.27 01-Mar-1997  christos PR/3269: Off by one in varisset(), caused variable substitution not to
count the last positional parameter as set.
 1.26 24-Jan-1997  christos - varisset(): In positional arguments, take into account VSNUL so that:
set -- ""; echo ${1:-wwww} works.
- when expanding arithmetic, discard previous ifs recorded regions, since we
are doing our own scanning. x=ab; echo $((${#x}+1)) now works.
- in ${var#word} fix two bugs:
* if there was an exact match, there was an off-by-one bug in the
comparison of the words. x=abcd; echo ${x#abcd}
* if there was no match, the stack region was not adjusted and the rest
of the word was getting written in the wrong place. x=123; echo ${x#abc}X
 1.25 11-Jan-1997  tls kill 'register'
 1.24 02-Nov-1996  christos A correction to the previous patch from Todd Miller.
 1.23 02-Nov-1996  christos echo ${1:-empty} did not do the substitution; from Todd Miller (OpenBSD)
 1.22 16-Oct-1996  christos PR/2808: Fix parsing of $n where n > 9 (from FreeBSD)
 1.21 02-Sep-1996  christos Don't infinite loop with:
unset var
echo ${var:=}
 1.20 12-Feb-1996  christos branches: 1.20.4;
Fix PR/2070: Ksh style variable modifiers were broken in /bin/sh, from
enami tsugutomo
 1.19 15-May-1995  christos Fixed new bug the previous fix introduced:

false
foo=bar
echo $?

would print 1
Also fixed the long standing bug:

false
echo `echo $?`

would print 0
The exitstatus needs rethinking and rewriting. The trial and error method
is not very efficient
 1.18 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.17 21-Mar-1995  cgd convert to new RCS id conventions.
 1.16 28-Feb-1995  christos Oops... typo in the IFS previous fix.
 1.15 28-Feb-1995  christos Changed so that backquote expansion eats all trailing newlines, not just
the last one.
Reported by guido@gvr.win.tue.nl (Guido van Rooij).
Repeat By:

echo "`cat file-with-many-newlines`"
 1.14 28-Feb-1995  christos Changed IFS string-splitting so that it breaks spaces even when IFS does not
begin with a space, but contains one. Fixes PR bin/809.

#!/bin/sh
list="a b c "
echo "With ordinary IFS"
for i in $list;do
echo $i
done
IFS=":${IFS}"
echo "With changed IFS"
for i in $list;do
echo $i
done

Note that before the fix ":${IFS}" behaved differently than "${IFS}:".
 1.13 23-Jan-1995  christos I added the documented in the manual but not implemented variable expansions:

${#WORD}
${WORD%PAT}
${WORD%%PAT}
${WORD#PAT}
${WORD##PAT}
 1.12 04-Dec-1994  cgd from James Jegers <jimj@miller.cs.uwm.edu>: quiet -Wall, and squelch
some of the worst style errors.
 1.11 29-Aug-1994  mycroft Fix problem with character classes matching a terminating NUL, from Henry
Spencer.
 1.10 11-Jun-1994  mycroft branches: 1.10.2;
Add RCS ids.
 1.9 12-May-1994  jtc Include appropriate header files to bring function prototypes into scope.
 1.8 11-May-1994  jtc sync with 4.4lite
 1.7 22-Oct-1993  mycroft Last patch was wrong; just save argbackq around the argstr() call.
 1.6 22-Oct-1993  mycroft evalvar(): If subtype is VSASSIGN (or VSQUESTION), argstr() already rolled
forward the backquote queue. If VSQUESTION it doesn't matter because we
already exited with an error.
 1.5 01-Aug-1993  mycroft Add RCS identifiers.
 1.4 02-May-1993  sef Jim "wilson@moria.cygnus.com" Wilson's patches to make C News (and other
things) work.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.10.2.1 29-Aug-1994  mycroft update from trunk
 1.20.4.3 04-Mar-1997  mycroft Pull up latest sh(1). Fixes yet more bugs.
 1.20.4.2 02-Mar-1997  mycroft Pull up off-by-one fix.
 1.20.4.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.31.2.1 08-May-1998  mycroft Sync with trunk, per request of christos.
 1.45.2.2 14-Jan-2002  he Pull up revision 1.52 (requested by itojun):
Do not truncate expr > 10 digits. Fixes PR#13943.
 1.45.2.1 07-Apr-1999  he Pull up 1.45-1.46. Corrects what's obviously a typo.
 1.49.4.1 08-Oct-2001  he Pull up revision 1.52 (requested by itojun):
Do not truncate expr > 10 digits. Fixes PR#13943.
 1.60.4.1 11-May-2005  riz Pull up revision 1.68 (requested by martin in ticket #1418):
expbackq() was incorrectly backing up a temporary buffer when removing \n
from the end of output of commands inside $(...) substitutions.
If the program output is n*128+1 bytes long (ending in a \n) then the code
checks buf[-1] for another \n - looking an uninitialised stack.
On a big-endian system an integer of value 10 will satisfy this (unlikely
on little endian) and can happen depending on the last code path to use
a lot of stack!
This caused the problem with newvers.sh on sparc64 after ', 2005' was
added to the date list.
Fixed PR/28852
 1.60.2.1 28-Oct-2005  riz Pull up following revision(s) (requested by martin in ticket #1418):
bin/sh/expand.c: revision 1.68
expbackq() was incorrectly backing up a temporary buffer when removing \n
from the end of output of commands inside $(...) substitutions.
If the program output is n*128+1 bytes long (ending in a \n) then the code
checks buf[-1] for another \n - looking an uninitialised stack.
On a big-endian system an integer of value 10 will satisfy this (unlikely
on little endian) and can happen depending on the last code path to use
a lot of stack!
This caused the problem with newvers.sh on sparc64 after ', 2005' was
added to the date list.
Fixed PR/28852
 1.68.2.5 02-Sep-2006  ghen Pull up following revision(s) (requested by dsl in ticket #1488):
bin/sh/expand.c: revision 1.76
Set the 'not a parameter' flag when we skip initial whitespace.
Otherwise:
./sh -c 'x=" "; for a in $x; do echo a${a}a; done'
is processed as a single empty parameter (instead of no parameters).
Should fix the breakage I introdiced in rev 1.75 and PR/34256 and PR/34254
 1.68.2.4 02-Sep-2006  ghen Pull up following revision(s) (requested by dsl in ticket #1487):
bin/sh/expand.c: revision 1.75
Rework the code changes from revisions 1.69, 1.70 and 1.74 so that the code
behaves correctly.
As far as I can tell, "x$@y" now expands correctly, as does IFS=:; set -$IFS.
Fixes PR/33472 (again) and PR/33956
 1.68.2.3 12-Jun-2006  tron Pull up following revision(s) (requested by dsl in ticket #1336):
bin/sh/expand.c: revision 1.74
When expanding "$@" add a \0 byte after the last argument (as well as all
the earlier ones) so that a separator is added before it when it is empty.
This wasn't needed before a recent change that chenged the behaviour of
trailing whitespace IFS characters.
Fixed PR/33472
 1.68.2.2 07-Apr-2005  tron branches: 1.68.2.2.2;
Pull up revision 1.70 (requested by dsl in ticket #119):
Check quoting before merging ifs regions.
sh -c 'set -- a; x="b c"; set -- "$@"$x'
now correctly gives $1=ab, $2=c
 1.68.2.1 07-Apr-2005  tron Pull up revision 1.69 (requested by dsl in ticket #118):
Don't merge ifs regions with different quoting requirements
 1.68.2.2.2.1 12-Jun-2006  tron Pull up following revision(s) (requested by dsl in ticket #1336):
bin/sh/expand.c: revision 1.74
When expanding "$@" add a \0 byte after the last argument (as well as all
the earlier ones) so that a separator is added before it when it is empty.
This wasn't needed before a recent change that chenged the behaviour of
trailing whitespace IFS characters.
Fixed PR/33472
 1.74.2.2 01-Sep-2006  tron Pull up following revision(s) (requested by dsl in ticket #84):
bin/sh/expand.c: revision 1.76
Set the 'not a parameter' flag when we skip initial whitespace.
Otherwise:
./sh -c 'x=" "; for a in $x; do echo a${a}a; done'
is processed as a single empty parameter (instead of no parameters).
Should fix the breakage I introdiced in rev 1.75 and PR/34256 and PR/34254
 1.74.2.1 01-Sep-2006  tron Pull up following revision(s) (requested by dsl in ticket #83):
bin/sh/expand.c: revision 1.75
Rework the code changes from revisions 1.69, 1.70 and 1.74 so that the code
behaves correctly.
As far as I can tell, "x$@y" now expands correctly, as does IFS=:; set -$IFS.
Fixes PR/33472 (again) and PR/33956
 1.77.2.1 16-Apr-2007  bouyer Pull up following revision(s) (requested by apb in ticket #570):
bin/sh/expand.c: revision 1.78
bin/sh/arith.y: revision 1.18
bin/sh/expand.h: revision 1.17
regress/bin/sh/expand.sh: revision 1.4
bin/sh/sh.1: revision 1.86
bin/sh/arith_lex.l: revision 1.14
Make /bin/sh use intmax_t (instead of int) for arithmetic in $((...)).
 1.79.2.2 09-Jun-2013  msaitoh Pull up following revision(s) (requested by gdt in ticket #1851):
bin/sh/expand.c: revision 1.88
bin/sh/expand.h: revision 1.19
Fix the expansion of "$(foo-$bar}" so that IFS isn't applied when
expanding $bar.
Noted by Greg Troxel on tech-userlevel running some 'git' tests.
Should fix PR bin/47361
 1.79.2.1 02-Nov-2011  riz Pull up following revision(s) (requested by christos in ticket #1665):
bin/sh/expand.c: revision 1.85
PR/45269: Andreas Gustafsson: Instead of falling off the edge when eating
trailing newlines
if the block has moved, arrange so that trailing newlines are never placed in
the string
in the first place, by accumulating them and adding them only after we've
encountered a
non-newline character. This allows also for more efficient appending since we
know how much
we need beforehand. From FreeBSD.
 1.83.4.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.86.4.1 02-Feb-2013  sborrill Pull up the following revisions(s) (requested by dsl in ticket #773):
bin/sh/expand.c: revision 1.88
bin/sh/expand.h: revision 1.19

Fix the expansion of "$(foo-$bar}" so that IFS isn't applied when
expanding $bar. Should fix PR bin/47361
 1.86.2.3 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.86.2.2 23-Jan-2013  yamt sync with head
 1.86.2.1 17-Apr-2012  yamt sync with head
 1.87.2.2 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.87.2.1 25-Feb-2013  tls resync with head
 1.91.4.1 19-Apr-2017  snj Pull up following revision(s) (requested by kre in ticket #1388):
bin/sh/expand.c: revision 1.102
Fix for the "${unset-var#$(cmd1)}$(cmd2)" runs the wrong command bug.
... From FreeBSD
 1.101.4.1 21-Apr-2017  bouyer Sync with HEAD
 1.101.2.2 26-Apr-2017  pgoyette Sync with HEAD
 1.101.2.1 20-Mar-2017  pgoyette Sync with HEAD
 1.104.2.1 02-May-2017  pgoyette Sync with HEAD - tag prg-localcount2-base1
 1.110.2.5 10-Sep-2018  martin Pull up following revision(s) via patch (requested by kre in ticket #1015):

bin/sh/expand.c: revision 1.124
bin/sh/expand.c: revision 1.127
bin/sh/parser.c: revision 1.148
bin/sh/parser.c: revision 1.149
bin/sh/syntax.c: revision 1.6
bin/sh/syntax.h: revision 1.9 (partial)

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),

-

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.)
 1.110.2.4 13-Jul-2018  martin Pull up following revision(s) (requested by kre in ticket #907):

bin/sh/expand.c: revision 1.122

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.
 1.110.2.3 25-Oct-2017  snj Pull up following revision(s) (requested by kre in ticket #310):
bin/sh/expand.c: revision 1.121
bin/sh/sh.1: revision 1.167 via patch
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).
 1.110.2.2 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.110.2.1 05-Jun-2017  snj Pull up following revision(s) (requested by kre in ticket #7):
bin/sh/expand.c: revisions 1.111, 1.112
PR bin/52272 - fix an off-by one that broke ~ expansions.
--
Another arithmetic expansion recordregion() fix, this time
calculate the lenght (used to calculate the end) based upon the
correct starting point.
Thanks to John Klos for finding and reporting this one.
 1.121.2.4 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.121.2.3 26-Nov-2018  pgoyette Sync with HEAD, resolve a couple of conflicts
 1.121.2.2 28-Jul-2018  pgoyette Sync with HEAD
 1.121.2.1 25-Jun-2018  pgoyette Sync with HEAD
 1.123.2.5 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.123.2.4 21-Apr-2020  martin Sync with HEAD
 1.123.2.3 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.123.2.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.123.2.1 10-Jun-2019  christos Sync with HEAD
 1.132.2.3 14-Jan-2024  martin Pull up following revision(s) (requested by kre in ticket #1787):

bin/sh/eval.c: revision 1.191
bin/sh/expand.c: revision 1.144

PR bin/57773

Fix a bug reported by Jarle Fredrik Greipsland in PR bin/57773,
where a substring expansion where the substring to be removed from
a variable expansion is itself a var expansion where the value
contains one (or more) of sh's CTLxxx chars - the pattern had
CTLESC inserted, the string to be matched against did not. Fail.

We fix that by always inserting CTLESC in var assign expansions.
See the PR for all the gory details.

Thanks for the PR.

PR bin/57773

Fix another bug reported by Jarle Fredrik Greipsland and added
to PR bin/57773, which relates to calculating the length of a
positional parameter which contains CTL chars -- yes, this one
really is that specific, though it would also affect the special
param $0 if it were to contain CTL chars, and its length was
requested - that is fixed with the same change. And note: $0
is not affected because it looks like a positional param (it
isn't, ${00} would be, but is always unset, ${0} isn't) all
special parame would be affected the same way, but the only one
that can ever contain a CTL char is $0 I believe. ($@ and $*
were affected, but just because they're expanding the positional
params ... ${#@} and ${#*} are both technically unspecified
expansions - and different shells produce different results.

See the PR for the details of this one (and the previous).

Thanks for the PR.
 1.132.2.2 07-Jun-2020  martin Pull up following revision(s) (requested by kre in ticket #940):

bin/sh/expand.h: revision 1.25
bin/sh/expand.c: revision 1.134
bin/sh/expand.c: revision 1.135
bin/sh/expand.c: revision 1.136
bin/sh/expand.c: revision 1.137

Remove a (completely harmless) duplicate assignment introduced in a
code merge from FreeBSD in 2017. NFC.

Pointed out by Roland Illig.

prevent sign extension from making expression always false.
remove masking and cast (requested by kre@)

When expanding a here-doc (NXHERE - the type with an unquoted end delim)
the output will not be further processed (at all) so there is no need
to escape magic chars in the output, and doing so leaves stray CTLESC
chars in the here doc text. Not good. So don't do that...

To save a strlen() of the result, to determine the size of the here doc,
make rmescapes() return the length of the resulting string (this isn't
needed for other uses, so didn't happen previously).

Reported on current-users@ (2020-02-06) by Jun Ebihara

XXX pullup -9
 1.132.2.1 24-Nov-2019  martin Pull up following revision(s) (requested by kre in ticket #467):

bin/sh/expand.c: revision 1.133

Open code the validity test & copy of the character class name in
a bracket expression in a pattern (ie: [[:THISNAME:]]). Previously
the code used strspn() to look for invalid chars in the name, and
then memcpy(), now we do the test and copy a character at a time.

This might, or might not, be faster, but it now correctly handles
\ quoted characters in the name (' and " quoting were already
dealt with, \ was too in an earlier version, but when the \ handling
changes were made, this piece of code broke).

Not exactly a vital bug fix (who writes [[:\alpha:]] or similar?)
but it should work correctly regardless of how obscure the usage is.

Problem noted by Harald van Dijk

XXX pullup -9
 1.141.2.1 14-Jan-2024  martin Pull up following revision(s) (requested by kre in ticket #535):

bin/sh/eval.c: revision 1.191
bin/sh/expand.c: revision 1.144

PR bin/57773

Fix a bug reported by Jarle Fredrik Greipsland in PR bin/57773,
where a substring expansion where the substring to be removed from
a variable expansion is itself a var expansion where the value
contains one (or more) of sh's CTLxxx chars - the pattern had
CTLESC inserted, the string to be matched against did not. Fail.

We fix that by always inserting CTLESC in var assign expansions.
See the PR for all the gory details.

Thanks for the PR.

PR bin/57773

Fix another bug reported by Jarle Fredrik Greipsland and added
to PR bin/57773, which relates to calculating the length of a
positional parameter which contains CTL chars -- yes, this one
really is that specific, though it would also affect the special
param $0 if it were to contain CTL chars, and its length was
requested - that is fixed with the same change. And note: $0
is not affected because it looks like a positional param (it
isn't, ${00} would be, but is always unset, ${0} isn't) all
special parame would be affected the same way, but the only one
that can ever contain a CTL char is $0 I believe. ($@ and $*
were affected, but just because they're expanding the positional
params ... ${#@} and ${#*} are both technically unspecified
expansions - and different shells produce different results.

See the PR for the details of this one (and the previous).

Thanks for the PR.
 1.144.2.1 02-Aug-2025  perseant Sync with HEAD
 1.26 22-Nov-2021  kre PR bin/53550

Here we go again... One more time to redo how here docs are
processed (it has been a few years since the last time!)

This is actually a relatively minor change, mostly to timimg
(to just when things happen). Now here docs are expanded at the
same time the "filename" word in a redirect is expanded, rather than
later when the heredoc was being sent to its process. This actually
makes things more consistent - but does break one of the ATF tests
which was testing that we were (effectively) internally inconsistent
in this area.

Not all shells agree on the context in which redirection expansions
should happen, some make any side effects visible to the parent shell
(the majority do) others do the redirection expansions in a subshell
so any side effcts are lost. We used to have a foot in each camp,
with the majority for everything but here docs, and the minority for
here docs. Now we're all the way with LBJ ... (or something like that).
 1.25 13-Feb-2020  kre When expanding a here-doc (NXHERE - the type with an unquoted end delim)
the output will not be further processed (at all) so there is no need
to escape magic chars in the output, and doing so leaves stray CTLESC
chars in the here doc text. Not good. So don't do that...

To save a strlen() of the result, to determine the size of the here doc,
make rmescapes() return the length of the resulting string (this isn't
needed for other uses, so didn't happen previously).

Reported on current-users@ (2020-02-06) by Jun Ebihara

XXX pullup -9
 1.24 18-Nov-2018  kre branches: 1.24.2;
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).
 1.23 07-Jun-2017  kre branches: 1.23.4; 1.23.6;
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)

This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)

The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.

This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).

I would not call the results of this perfect yet, but it is close.
 1.22 03-Jun-2017  kre branches: 1.22.2;
Fixes to shell expand (that is, $ stuff) from FreeBSD (implemented
differently...)

In particular ${01} is now $1 not $0 (for ${0any-digits})

${4294967297} is most probably now ""
(unless you have a very large number of params)
it is no longer an alias for $1 (4294967297 & 0xFFFFFFFF) == 1

$(( expr $(( more )) stuff )) is no longer the same as
$(( expr (( more )) stuff )) which was sometimes OK, as in:
$(( 3 + $(( 2 - 1 )) * 3 ))
but not always as in:
$(( 1$((1 + 1))1 ))
which should be 121, but was an arith syntax error as
1((1 + 1))1
is meaningless.

Probably some more. This also sprinkles a little const, splits a big
func that had 2 (kind of unrelated) purposes into two simpler ones,
and avoids some (semi-dubious) modifications (and restores) in the input
string to insert \0's when they were needed.
 1.21 26-Apr-2017  christos Convert the pattern matcher from recursive to backtracking (from FreeBSD).
 1.20 20-Mar-2017  kre branches: 1.20.2;


Finish support for all required $(( )) (shell arithmetic) operators,
closing PR bin/50958

That meant adding the assignment operators ('=', and all of the +=, *= ...)
Currently, ++, --, and ',' are not implemented (none of those are required
by posix) but support for them (most likely ',' first) might be added later.

To do this, I removed the yacc/lex arithmetic parser completely, and
replaced it with a hand written recursive descent parser, that I obtained
from FreeBSD, who earlier had obtained it from dash (Herbert Xu).

While doing the import, I cleaned up the sources (changed some file names
to avoid requiring a clean build, or signifigant surgery to the obj
directories if "build.sh -u" was to be used - "build.sh -u" should work
fine as it is now) removed some dashisms, applied some KNF, ...
 1.19 22-Dec-2012  dsl branches: 1.19.14; 1.19.18;
Fix the expansion of "$(foo-$bar}" so that IFS isn't applied when
expanding $bar.
Noted by Greg Troxel on tech-userlevel running some 'git' tests.
Should fix PR bin/47361
 1.18 18-Jun-2011  christos branches: 1.18.2; 1.18.4; 1.18.8;
PR/45069: Henning Petersen: Use prototypes from builtins.h .
 1.17 25-Mar-2007  apb branches: 1.17.18; 1.17.32;
Make /bin/sh use intmax_t (instead of int) for arithmetic in $((...)).
 1.16 13-Jul-2004  seb branches: 1.16.10;
Add new builtin `wordexp' to support wordexp(3).

From FreeBSD.
Provided in PR lib/26123.
Approved by kleink@.
 1.15 26-Jun-2004  dsl No functional changes (intended).
Rename some variables, add some comments, and restructure a little.
In preparation for fixing "set ${x-a b c}" and friends.
 1.14 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.13 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.12 09-Jul-1999  christos compile with WARNS = 2
 1.11 26-Mar-1999  christos PR/7231: Havard Eidnes: Shell quoting/trimming problem
 1.10 05-Feb-1999  christos PR/5577: Craig M. Chase: sh does not build with PARALLEL set.
- Added YHEADER in Makefile, removed arith.h and adjusted the sources.
 1.9 31-Jan-1998  christos PR/4851: Benjamin Lorenz: In the "for <var> in <args>" construct <args>
was not marked as a region to be handled by ifsbreakup. Add EXP_RECORD
to indicate that the argument string needs to be recorded.
 1.8 11-May-1995  christos branches: 1.8.4;
Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.7 21-Mar-1995  cgd convert to new RCS id conventions.
 1.6 11-Jun-1994  mycroft Add RCS ids.
 1.5 11-May-1994  jtc sync with 4.4lite
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.8.4.1 08-May-1998  mycroft Sync with trunk, per request of christos.
 1.16.10.1 16-Apr-2007  bouyer Pull up following revision(s) (requested by apb in ticket #570):
bin/sh/expand.c: revision 1.78
bin/sh/arith.y: revision 1.18
bin/sh/expand.h: revision 1.17
regress/bin/sh/expand.sh: revision 1.4
bin/sh/sh.1: revision 1.86
bin/sh/arith_lex.l: revision 1.14
Make /bin/sh use intmax_t (instead of int) for arithmetic in $((...)).
 1.17.32.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.17.18.1 09-Jun-2013  msaitoh Pull up following revision(s) (requested by gdt in ticket #1851):
bin/sh/expand.c: revision 1.88
bin/sh/expand.h: revision 1.19
Fix the expansion of "$(foo-$bar}" so that IFS isn't applied when
expanding $bar.
Noted by Greg Troxel on tech-userlevel running some 'git' tests.
Should fix PR bin/47361
 1.18.8.1 25-Feb-2013  tls resync with head
 1.18.4.1 02-Feb-2013  sborrill Pull up the following revisions(s) (requested by dsl in ticket #773):
bin/sh/expand.c: revision 1.88
bin/sh/expand.h: revision 1.19

Fix the expansion of "$(foo-$bar}" so that IFS isn't applied when
expanding $bar. Should fix PR bin/47361
 1.18.2.1 23-Jan-2013  yamt sync with head
 1.19.18.1 21-Apr-2017  bouyer Sync with HEAD
 1.19.14.1 26-Apr-2017  pgoyette Sync with HEAD
 1.20.2.1 02-May-2017  pgoyette Sync with HEAD - tag prg-localcount2-base1
 1.22.2.1 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.23.6.4 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.23.6.3 21-Apr-2020  martin Sync with HEAD
 1.23.6.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.23.6.1 10-Jun-2019  christos Sync with HEAD
 1.23.4.1 26-Nov-2018  pgoyette Sync with HEAD, resolve a couple of conflicts
 1.24.2.1 07-Jun-2020  martin Pull up following revision(s) (requested by kre in ticket #940):

bin/sh/expand.h: revision 1.25
bin/sh/expand.c: revision 1.134
bin/sh/expand.c: revision 1.135
bin/sh/expand.c: revision 1.136
bin/sh/expand.c: revision 1.137

Remove a (completely harmless) duplicate assignment introduced in a
code merge from FreeBSD in 2017. NFC.

Pointed out by Roland Illig.

prevent sign extension from making expression always false.
remove masking and cast (requested by kre@)

When expanding a here-doc (NXHERE - the type with an unquoted end delim)
the output will not be further processed (at all) so there is no need
to escape magic chars in the output, and doing so leaves stray CTLESC
chars in the here doc text. Not good. So don't do that...

To save a strlen() of the result, to determine the size of the here doc,
make rmescapes() return the length of the resulting string (this isn't
needed for other uses, so didn't happen previously).

Reported on current-users@ (2020-02-06) by Jun Ebihara

XXX pullup -9
 1.4 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.3 21-Mar-1995  cgd convert to new RCS id conventions.
 1.2 06-Dec-1994  cgd adjust; from jimj.
 1.1 05-Dec-1994  cgd clean up further. more patches from Jim Jegers
 1.73 03-Aug-2024  kre Revert a part of a change made on 1999-07-09 (in rev 1.21).

That change ("compile with WARNS = 2") added "const" qualifiers
in many places.

One of them, in this file, became a const char *, which needed to be
passed to sh function defined as taking a char * arg. (evalstring()).

To do that safely (in 1999), a copy of the original string was made,
and that copy was passed to evalstring() instead of the original.
The copy is simply char *, and if something altered it, no-one would
care. The original string (in some cases) must not be altered.

That was the cause of the bug fixed (a different way) in the
previous commit to this file. To copy the string, space needed
to be allocated, and when that was done, if the string was just
(the right length, its terminating \0 (which previously was stored
outside the string itself) and if the current shell stack memory block
was big enough that a new one wasn't needed, the \0 would be trampled
(then the strcpy() would become overwriting, and havoc result).

Now evalstring() has been updated to take a const char *, and
the string passed to it truly is treated as const data (nothing
ever attempts to alter it) so making a copy of the string isn't
needed, and we can go back to the way the code used to be, pre-1999.

That is, modulo other unrelated changes (or one change actually)
that has been made since, which has nothing to do with the issues here.

The change made in the previous revision could be undone, but won't
be, as it is logically the right thing to do.

NFCI.
 1.72 03-Aug-2024  kre Fix a very old core dump causing bug found by RVP in the history code.
That is, truly very old - it is in rev 1.1 from 1994 (and so is probably
even older than that).

If one uses the (very rarely used)
fc -s string=otherstring
builtin command, to rerun the previous command (or with additional args,
any other command) after replacing the first instance of "string" in that
command with "otherstring" and the resulting command line just happens to
be a length that is a multiple of the shell's memory allocation alignment
constant, then the \0 string terminator that is appended to the result
to mark its end stood a very small chance (in 1994 probably no chance at all,
but made considerably more likely in 1999 when other changes were made -
certain in the right circumstances) of being destroyed by other sh memory
allocation before the string was finished being used.

The fix (also suggested by RVP) is to make that \0 an actual part of
the allocated result string, rather than an extra byte tacked on the
end of it -- in itself, doing the latter is common in sh, and not at all
improper, sometimes even required, but only when the string as a string
will be consumed before more (shell) stack memory allocation is performed.
It 1994 it would have been. Since 1999, it isn't.

The 1999 change is going to be undone in a later commit, but this one
is simpler to pull up to earlier releases, and probably the right thing
to do anyway, even if not strictly essential.

XXX pullup -9, -10 (and everything back to 1.5 - it looks as if 1.4 is OK).
 1.71 13-Jul-2024  kre Implement the HISTFILE and HISTAPPEND variables.

See the (newly updated) sh(1) for details.
Also add the -z option to fc (clear history).

None of this exists in SMALL shells.
 1.70 12-Jul-2024  kre Improve safety in var imports from the environment.

Add a new var flag VUNSAFE - set on all vars imported from the environment.

Add setvareqsafe() (which is to setvareq() as setvarsafe() is to setvar())
and use that instead of setvareq() when processing the environment, so
errors don't cause the shell to abort. Use VUNSAFE in that call.

Add flags arguments to all var callback functions which are used when setting
variables, and pass the flags given to the setvar*() functions to those
functions, so they can act differently in different situations (if desired).
Most of them just ignore the flags.

When unsetting a variable, call setvar() to clear things (and call the
callback function) both when the variable had a value which needs to be freed,
and when unsetting a variable which wasn't unset previously, so the VUNSET
flag can be seen by that callback func.

When setting HISTSIZE, use the flags passed to determine whether to ignore
bad values (if VUNSAFE) or treat them as an error. This replaces the
earlier temporary hack to always ignore bad data there (histedit.c 1.68).

Miscellaneous associated minor changes.

These changes should largely be invisible in normal use.
 1.69 05-Jul-2024  kre Don't emit a user causable "internal error" message.

Non-numeric args to "fc" simply fail to find the string
in an empty history buffer, as they might in a non-empty
history, and generate a reasonable error message.

But normally, as numeric args to the "fc" utility are trimmed
to the bounds of the contents of the history buffer, it
is impossible for them to fail to find an entry, as at the
very least the "fc" command itself should be there.

Hence "sh" claimed such a thing was an "internal error".

But it is possible to run fc commands, not entered from
stdin, and so not eligible for the history buffer, before
any interactive commands are entered. In such a case
the history buffer will be empty, which "sh" has been
reporting as an "internal error". It isn't.
Generate a more meaningful (and less scary) message instead.

No pullups planned - the code has been like this for a
very long time, and no-one ever noticed, as it would be
very (VERY) unusual for the "fc" utility to be invoked
at a time when the history buffer is active & empty, given
the way the code currently exists (unlikely, not impossible).

This change is more in preparation for future possible
changes which would make this error easier to encounter.
 1.68 05-Jul-2024  kre Ignore non-numeric values for HISTSIZE

This is a temporary change (which can be pulled up to -9 and -10)
to avoid having
HISTSIZE=foo /bin/sh
cause sh to fail to start.

A better change, but one requiring far more code changes, so that
bad HISTSIZE is ignored, only when read from the environment, but
will be an error otherwise, will come later.

XXX pullup -9, -10
 1.67 03-Jul-2024  kre General source cleanup, whitespace, KNF, 80-col police, ...

No executable binary change intended (certainly NFCI).
Some debugging info will change as line numbers have altered.
 1.66 07-Apr-2023  kre branches: 1.66.2;

The great shell trailing whitespace cleanup of 2023...
Inspired by private e-mail comments from mouse@

NFCI.
 1.65 22-Aug-2022  kre branches: 1.65.2;

Add debugging trace points for history and the editline interface.
NFC for any normal shell (not compiled with debugging (sh DEBUG) enabled.

We have had a defined debug mode for this for years, but since I have
not often played in this arena, never used it. Until recently (relatively).
This (or a small part of it) played a part in discovering the fc -e
bug cause. I have had it in my tree a while now - recent changes
kept causing merge conflicts (all because I hadn't bothered to commit
this), so I think now is the time...
 1.64 21-Aug-2022  nia sh(1): revert previous because it interferes with custom user bindings
 1.63 18-Aug-2022  nia sh(1): Allow an explicit set -o vi or set -o emacs to override ~/.editrc
 1.62 17-Aug-2022  nia sh(1): Assign the tab completion key binding last so a user having
"bind -v" or "bind -e" in ~/.editrc doesn't cause tab completion
to no longer function.
 1.61 08-Feb-2022  rillig sh: fix typo in comment
 1.60 02-Feb-2022  kre After (a few days short of) 21 years, revert 1.25, which did nothing except
make the -e option to "fc" fail to work (the commit message was about some
other changes entirely, so I an only assume this was committed by mistake).

It says a lot about the use of the fc command that no-one noticed that
this did not work properly for all this time.

Internally in sh, it is possible for built in commands to use either
getopt(3) (from libc) or the much simpler internal shell nextopt() routine
for option (flag) parsing. However it makes no sense to use getopt()
and then access a global variable set only by nextopt() instead of the
one getopt() sets (which is what the code had used previously, forever).

Use the correct variable again.

XXX pullup -9 -8 (-7 -6 -5 ...)
 1.59 31-Jan-2022  kre When we initialize libedit (editline) always call ourselves "sh" no
matter what $0 is (or is not) set to. This means that editrc(5)
lines that start "sh:" are used (in addition to those with no prefix,
which will usually be most of them), regardless of the name or manner in
which we were invoked.

OK christos@
 1.58 31-Jan-2022  kre Add some comments explaining accesses to the environment via
getenv()/setenv()/unsetenv() which manipulate the envornoment
the shell was passed at entry.

These are a little odd in sh as that environment is copied into
the shell's internal variable data struct at shell startup, and
normally never accessed after that - in builtin commands (test.
printf, ...) getenv() is #defined to become an internal sh lookup
function instead, so even those never use the startup environment).

NFCI
 1.57 14-Sep-2021  christos Quote the filenames like before
 1.56 15-Aug-2021  christos - Add command completion (from FreeBSD)
- Use EL_SAFEREAD
 1.55 10-Feb-2019  kre branches: 1.55.2;

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.
 1.54 09-Feb-2019  kre 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.
 1.53 13-Jul-2018  kre 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.)
 1.52 28-Jun-2017  kre branches: 1.52.4; 1.52.6;

Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
 1.51 27-Jun-2017  christos Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
 1.50 27-Jun-2017  kre Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
 1.49 26-Jun-2017  christos source .editrc after we initialize so that commands persist!
 1.48 16-Mar-2016  christos branches: 1.48.8;
Sprinkle volatile for gcc 5!
 1.47 18-Jun-2014  christos Adjust to the new alias text libedit API.
 1.46 23-Mar-2014  dholland branches: 1.46.2;
don't use sprintf
 1.45 20-Mar-2012  matt branches: 1.45.2;
Use C89 function definitions
 1.44 12-Jul-2011  joerg branches: 1.44.2;
Move the savehandler assignment before setjmp() to avoid triggering
warnings about use before initialization with clang.
 1.43 18-Jun-2011  christos PR/45069: Henning Petersen: Use prototypes from builtins.h .
 1.42 07-Jan-2011  jmmv branches: 1.42.4;
Call el_source before initializing sh-specific editline properties (i.e.
the editor type and the tab completion binding).

This allows tab completion to work when a user has an ~/.editrc file.

Addresses PR bin/43404.
 1.41 13-Feb-2008  joerg Set unique mode for the history, so that repeating a command doesn't
spam the history.
 1.40 16-Oct-2006  christos branches: 1.40.8;
sprinkle volatile.
 1.39 10-May-2006  mrg quell GCC 4.1 uninitialised variable warnings.

XXX: we should audit the tree for which old ones are no longer needed
after getting the older compilers out of the tree..
 1.38 06-Mar-2006  aymeric Make the -s flag of fc work correctly. Ensure that only one command is specified
and ensure that we break out of the loop after executing it.
With the previous code, because the re-executed command was added to the
history, it would think that it still had to execute it, leading to an infinite
loop.

The last thing which differs from ksh is that we get the "fc -s " command into
the history, but this is actually rather a feature in my humble opinion.

To test:
$ echo hello
$ fc -s ec
<infinite loop or correct behaviour>
 1.37 15-Jul-2005  christos Only do certain actions when we are trully interactive, not just when we
are connected to a tty:
1. enable editor
2. print directory on directory change.
3. print job messages
 1.36 09-May-2005  christos Only add the function when we initialize. Restore the binding of tab to
ed-insert if tabcomplete is cleared.
 1.35 07-May-2005  dsl If 'set -o tabcomplete' it set, then bind <tab> to the libedit filename
completion function.
Note that the libedit code will probably want fine-tuning!
While editing the man page, add a note that non-whitespace IFS chars are
terminators and can generate null arguments.
 1.34 27-Oct-2003  lukem branches: 1.34.6;
skip a leading `-' on the arg0 name when calling el_init()
 1.33 19-Oct-2003  christos Let libedit handle signals so we resize properly. I might change my mind
later on that.
 1.32 19-Oct-2003  lukem Ensure that $TERM is set in the real environment before calling el_init(3)
when executing "set -o (emacs|vi)". Fixes [bin/23175] from me.
 1.31 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.30 13-Jul-2003  itojun use bounded string op
 1.29 04-May-2003  gmcgarry ANSIify.
 1.28 04-May-2003  gmcgarry Add new builtin 'inputrc' which allows keybindings to be redefined
for the current shell. From Arne H Juul in PR#10097.
 1.27 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.26 12-Mar-2002  christos get rid of sp completely, we were free-ing a bogus pointer; found by simonb.
 1.25 04-Feb-2001  christos remove redundant declarations and nexted externs.
 1.24 06-Nov-2000  mycroft Call el_source() to use .editrc.
 1.23 14-Apr-2000  simonb Don't declare 'extern opt*' getopt variables.
 1.22 27-Jan-2000  christos Fix bin/9184, bin/9194, bin/9265, bin/9266
Exitcode and negation problems (From Martin Husemann)
 1.21 09-Jul-1999  christos compile with WARNS = 2
 1.20 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.19 20-May-1998  christos fix bug where if moving in history during a multi-line event, the
append to history event would end up in the last event where the history
was moved to instead of the multi-line event; reported by Mycroft
 1.18 07-Apr-1998  fair Change a few things to reference /usr/include/paths.h instead of local
references. Fixing the default PATH is a bit more effort.
 1.17 12-Nov-1997  cjs Make this compile with -DSMALL and warnings on.
 1.16 14-Oct-1997  christos branches: 1.16.2;
PR/4257: Jaromir Dolecek: Update for libedit interface change.
 1.15 14-Sep-1997  lukem getopt returns -1 not EOF
 1.14 04-Jul-1997  christos Fix compiler warnings.
 1.13 11-Apr-1997  christos Track $TERM and call the appropriate editline(3) routine to update the
terminal type.
 1.12 14-Mar-1997  christos NO_HISTORY->SMALL
 1.11 13-Mar-1997  christos Fix problems with -DNO_HISTORY
 1.10 16-Oct-1996  christos PR/2808: Remove extraneous newlines from error routines (from FreeBSD)
 1.9 25-Jun-1996  christos change sethistsize() to take the history size as an argument so it can
be used directly by the new variable callback functions.
 1.8 11-May-1995  christos branches: 1.8.6;
Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.7 31-Mar-1995  christos 1. Don't core dump on 'fc -l' (From Gerard J van der Grinten)
2. PATH=xxx ls, does the PATH assignment first and then tries to find ls in xxx
3. VAR=xxx exec ls, does the variable assignment.
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 05-Dec-1994  cgd clean up further. more patches from Jim Jegers
 1.4 04-Dec-1994  cgd from James Jegers <jimj@miller.cs.uwm.edu>: quiet -Wall, and squelch
some of the worst style errors.
 1.3 11-Jun-1994  mycroft Add RCS ids.
 1.2 12-May-1994  jtc Include appropriate header files to bring function prototypes into scope.
 1.1 11-May-1994  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 11-May-1994  jtc 44lite code
 1.8.6.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.16.2.2 08-May-1998  mycroft Sync with trunk, per request of christos.
 1.16.2.1 13-Nov-1997  mellon Pull rev 1.17 up from trunk (cjs)
 1.34.6.1 31-Mar-2007  bouyer Pull up following revision(s) (requested by mjf in ticket #1689):
bin/sh/histedit.c: revision 1.38
Make the -s flag of fc work correctly. Ensure that only one command
is specified and ensure that we break out of the loop after executing it.
With the previous code, because the re-executed command was added to the
history, it would think that it still had to execute it, leading to
an infinite loop.
 1.40.8.1 23-Mar-2008  matt sync with HEAD
 1.42.4.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.44.2.2 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.44.2.1 17-Apr-2012  yamt sync with head
 1.45.2.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.46.2.1 10-Aug-2014  tls Rebase.
 1.48.8.3 24-Feb-2022  martin Pull up following revision(s) (requested by kre in ticket #1736):

bin/sh/histedit.c: revision 1.60

After (a few days short of) 21 years, revert 1.25, which did nothing except
make the -e option to "fc" fail to work (the commit message was about some
other changes entirely, so I an only assume this was committed by mistake).

It says a lot about the use of the fc command that no-one noticed that
this did not work properly for all this time.

Internally in sh, it is possible for built in commands to use either
getopt(3) (from libc) or the much simpler internal shell nextopt() routine
for option (flag) parsing. However it makes no sense to use getopt()
and then access a global variable set only by nextopt() instead of the
one getopt() sets (which is what the code had used previously, forever).

Use the correct variable again.

XXX pullup -9 -8 (-7 -6 -5 ...)
 1.48.8.2 25-Aug-2018  martin Pull up following revision(s) (requested by kre in ticket #988):

bin/sh/parser.c: revision 1.147
bin/sh/var.c: revision 1.70
bin/sh/mystring.c: revision 1.18
bin/sh/options.c: revision 1.53
bin/sh/histedit.c: revision 1.53

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.)
 1.48.8.1 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.52.6.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.52.6.2 21-Apr-2020  martin Sync with HEAD
 1.52.6.1 10-Jun-2019  christos Sync with HEAD
 1.52.4.1 28-Jul-2018  pgoyette Sync with HEAD
 1.55.2.3 07-Aug-2024  martin Pull up following revision(s) (requested by kre in ticket #1860):

bin/sh/histedit.c: revision 1.72

Fix a very old core dump causing bug found by RVP in the history code.

That is, truly very old - it is in rev 1.1 from 1994 (and so is probably
even older than that).

If one uses the (very rarely used)

fc -s string=otherstring

builtin command, to rerun the previous command (or with additional args,
any other command) after replacing the first instance of "string" in that
command with "otherstring" and the resulting command line just happens to
be a length that is a multiple of the shell's memory allocation alignment
constant, then the \0 string terminator that is appended to the result
to mark its end stood a very small chance (in 1994 probably no chance at all,
but made considerably more likely in 1999 when other changes were made -
certain in the right circumstances) of being destroyed by other sh memory
allocation before the string was finished being used.

The fix (also suggested by RVP) is to make that \0 an actual part of
the allocated result string, rather than an extra byte tacked on the
end of it -- in itself, doing the latter is common in sh, and not at all
improper, sometimes even required, but only when the string as a string
will be consumed before more (shell) stack memory allocation is performed.

It 1994 it would have been. Since 1999, it isn't.

The 1999 change is going to be undone in a later commit, but this one
is simpler to pull up to earlier releases, and probably the right thing
to do anyway, even if not strictly essential.
 1.55.2.2 20-Jul-2024  martin Pull up following revision(s) (requested by kre in ticket #1856):

bin/sh/histedit.c: revision 1.68

Ignore non-numeric values for HISTSIZE

This is a temporary change (which can be pulled up to -9 and -10)
to avoid having

HISTSIZE=foo /bin/sh

cause sh to fail to start.

A better change, but one requiring far more code changes, so that
bad HISTSIZE is ignored, only when read from the environment, but
will be an error otherwise, will come later.
 1.55.2.1 21-Feb-2022  martin Pull up following revision(s) (requested by kre in ticket #1429):

bin/sh/histedit.c: revision 1.60

After (a few days short of) 21 years, revert 1.25, which did nothing except
make the -e option to "fc" fail to work (the commit message was about some
other changes entirely, so I an only assume this was committed by mistake).

It says a lot about the use of the fc command that no-one noticed that
this did not work properly for all this time.

Internally in sh, it is possible for built in commands to use either
getopt(3) (from libc) or the much simpler internal shell nextopt() routine
for option (flag) parsing. However it makes no sense to use getopt()
and then access a global variable set only by nextopt() instead of the
one getopt() sets (which is what the code had used previously, forever).

Use the correct variable again.

XXX pullup -9 -8 (-7 -6 -5 ...)
 1.65.2.2 07-Aug-2024  martin Pull up following revision(s) (requested by kre in ticket #774):

bin/sh/histedit.c: revision 1.72

Fix a very old core dump causing bug found by RVP in the history code.

That is, truly very old - it is in rev 1.1 from 1994 (and so is probably
even older than that).

If one uses the (very rarely used)

fc -s string=otherstring

builtin command, to rerun the previous command (or with additional args,
any other command) after replacing the first instance of "string" in that
command with "otherstring" and the resulting command line just happens to
be a length that is a multiple of the shell's memory allocation alignment
constant, then the \0 string terminator that is appended to the result
to mark its end stood a very small chance (in 1994 probably no chance at all,
but made considerably more likely in 1999 when other changes were made -
certain in the right circumstances) of being destroyed by other sh memory
allocation before the string was finished being used.

The fix (also suggested by RVP) is to make that \0 an actual part of
the allocated result string, rather than an extra byte tacked on the
end of it -- in itself, doing the latter is common in sh, and not at all
improper, sometimes even required, but only when the string as a string
will be consumed before more (shell) stack memory allocation is performed.

It 1994 it would have been. Since 1999, it isn't.

The 1999 change is going to be undone in a later commit, but this one
is simpler to pull up to earlier releases, and probably the right thing
to do anyway, even if not strictly essential.
 1.65.2.1 20-Jul-2024  martin Pull up following revision(s) (requested by kre in ticket #761):

bin/sh/histedit.c: revision 1.68

Ignore non-numeric values for HISTSIZE

This is a temporary change (which can be pulled up to -9 and -10)
to avoid having

HISTSIZE=foo /bin/sh

cause sh to fail to start.

A better change, but one requiring far more code changes, so that
bad HISTSIZE is ignored, only when read from the environment, but
will be an error otherwise, will come later.
 1.66.2.1 02-Aug-2025  perseant Sync with HEAD
 1.10 07-Aug-2003  agc branches: 1.10.98;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.9 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.8 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.7 21-Mar-1995  cgd convert to new RCS id conventions.
 1.6 11-Jun-1994  mycroft Add RCS ids.
 1.5 11-May-1994  jtc sync with 4.4lite
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.10.98.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.10.98.1 21-Apr-2020  martin Sync with HEAD
 1.76 14-Oct-2024  kre Apologies for that commit message ... it should have been the same
as for revision 1.124 of Makefile (and as below).

(This change changes nothing).

Reject nul characters in shell input.

At the request of Thomas Klausner (wiz@) copy an idea from OpenBSD,
and have the shell simply reject any (sh) input containing a \0 (nul)
character. Previously nul characters were simply ignored (removed
from the input before it was examined in any other way).

Note this affects data read by the shell itself only, and has no
impact on other utilities, including those that are built into
the shell (so 'read' can still use -d '' to process output using
\0 as the record separator).

While I have tested that this works, there are so many places
where a nul might appear, that I cannot possibly test them all
(or even imagine all the possible places), so if this change
causes any problems, let me know (if from a script, send me the
script).

To undo this change, simply comment out (or delete) the line
CPPFLAGS+= -DREJECT_NULS
from the Makefile, and build again. Eventually if this causes
no problems, that option (conditional compilation) will probably
just go away, and this change would be permanent.

While the conditional compilation (on or off) persists, the
NETBSD_SHELL variable value will contain the word REJECT_NULLS
so you can easily verify if you are running a shell with this
included or not.

No pullups planned.
 1.75 14-Oct-2024  kre # $NetBSD: Makefile,v 1.123 2023/10/19 04:27:24 mrg Exp $
# @(#)Makefile 8.4 (Berkeley) 5/5/95

.include <bsd.own.mk>

PROG= sh
SHSRCS= alias.c arith_token.c arithmetic.c cd.c echo.c error.c eval.c exec.c \
expand.c histedit.c input.c jobs.c mail.c main.c memalloc.c \
miscbltin.c mystring.c options.c parser.c redir.c show.c trap.c \
output.c var.c test.c kill.c syntax.c
GENSRCS=builtins.c init.c nodes.c
GENHDRS=builtins.h nodes.h token.h nodenames.h optinit.h
SRCS= ${SHSRCS} ${GENSRCS}

DPSRCS+=${GENHDRS}

LDADD+= -ledit -lterminfo
DPADD+= ${LIBEDIT} ${LIBTERMINFO}

# Environment for scripts executed during build.
SCRIPT_ENV= \
AWK=${TOOL_AWK:Q} \
MKTEMP=${TOOL_MKTEMP:Q} \
SED=${TOOL_SED:Q}

CPPFLAGS+=-DSHELL -I. -I${.CURDIR} -I${NETBSDSRCDIR}/lib/libedit
CPPFLAGS+= -DUSE_LRAND48
CPPFLAGS+= -DREJECT_NULS

#XXX: For testing only.
#CPPFLAGS+=-DDEBUG=1
#COPTS+=-g
#CFLAGS+=-funsigned-char
#TARGET_CHARFLAG?= -DTARGET_CHAR="unsigned char" -funsigned-char

# Reproducible build parameters ... export into sh for NETBSD_SHELL setting
.if ${MKREPRO_TIMESTAMP:Uno} != "no"
BUILD_DATE!= ${TOOL_DATE} -u -r "${MKREPRO_TIMESTAMP}" "+%Y%m%d%H%M%S"
# These are (should be) equivalent, but the 2nd is easier to understand
#CPPFLAGS+= -DBUILD_DATE='"${BUILD_DATE:C/([^0]0?)(00)*$/\1/}Z"'
CPPFLAGS+= -DBUILD_DATE='"${BUILD_DATE:S/00$//:S/00$//:S/00$//}Z"'
.endif

.ifdef SMALLPROG
CPPFLAGS+=-DSMALL
.endif
.ifdef TINYPROG
CPPFLAGS+=-DTINY
.else
SRCS+=printf.c
.endif

.PATH: ${.CURDIR}/bltin ${NETBSDSRCDIR}/bin/test \
${NETBSDSRCDIR}/usr.bin/printf \
${NETBSDSRCDIR}/bin/kill

CLEANFILES+= ${GENSRCS} ${GENHDRS} sh.html1
CLEANFILES+= trace.*

token.h: mktokens
${_MKTARGET_CREATE}
${SCRIPT_ENV} ${HOST_SH} ${.ALLSRC}

.ORDER: builtins.h builtins.c
builtins.h builtins.c: mkbuiltins shell.h builtins.def
${_MKTARGET_CREATE}
${SCRIPT_ENV} ${HOST_SH} ${.ALLSRC} ${.OBJDIR}
[ -f builtins.h ]

init.c: mkinit.sh ${SHSRCS}
${_MKTARGET_CREATE}
${SCRIPT_ENV} ${HOST_SH} ${.ALLSRC}

.ORDER: nodes.h nodes.c
nodes.c nodes.h: mknodes.sh nodetypes nodes.c.pat
${_MKTARGET_CREATE}
${SCRIPT_ENV} ${HOST_SH} ${.ALLSRC} ${.OBJDIR}
[ -f nodes.h ]

nodenames.h: mknodenames.sh nodes.h
${_MKTARGET_CREATE}
${SCRIPT_ENV} ${HOST_SH} ${.ALLSRC} > ${.TARGET}

optinit.h: mkoptions.sh option.list
${_MKTARGET_CREATE}
${SCRIPT_ENV} ${HOST_SH} ${.ALLSRC} ${.TARGET} ${.OBJDIR}

.if ${USETOOLS} == "yes"
NBCOMPATLIB= -L${TOOLDIR}/lib -lnbcompat
.endif

SUBDIR.roff+=USD.doc

COPTS.printf.c = -Wno-format-nonliteral
COPTS.jobs.c = -Wno-format-nonliteral
COPTS.var.c = -Wno-format-nonliteral

# XXXGCC12 - only on some targets
COPTS.parser.c+= ${${ACTIVE_CC} == "gcc" && ${HAVE_GCC:U0} >= 12:? -Wno-error=clobbered :}

.include <bsd.prog.mk>
.include <bsd.subdir.mk>

${OBJS}: Makefile
 1.74 03-Aug-2024  kre Change the "string" argument to evalstring() and setinputstring()
from being "char *" to being "const char *".

This is needed for a forthcoming change which needs to pass a const char *
to evalstring (and through it to setinputstring) and be assured that
nothing will alter the characters in the string supplied.

This is (aside from the additional compile time protection provided)
a no-op change, all evalstring() does with its string is pass it to
setinputstring() and all that does with it is determine its length
(strlen() which expects a const char *) and assign the string pointer
to parsenextc which is already a const char * - there never has been
any reason for these two functions to not include the "const" in
the arg declaration -- except that when originally written (early
1990's) I suspect "const" either didn't exist at all, or wasn't
supported by relevant compilers.

NFCI. Most probably (though I didn't check) no binary change at all.
 1.73 13-Jul-2024  kre Implement the HISTFILE and HISTAPPEND variables.

See the (newly updated) sh(1) for details.
Also add the -z option to fc (clear history).

None of this exists in SMALL shells.
 1.72 16-Feb-2021  kre branches: 1.72.8;
PR bin/55979

Correctly handle (ie: ignore completely) \0 chars (nuls) in the
shell command input stream (script, dot file, or stdin).

Previously nul chars were ignored correctly in the line in which
they occurred, but would cause trailing chars of that line to reappear
as the start of the following line. If there was just one \0 skipped,
this would generally result in an extra \n in the sh input, which in
most cases has no effect. With multiple \0's in a single line, more
of the end of that line was duplicated into the following one. This
usually manifested as a weird "command not found" error.

Note that any \0 chars in the sh input make the script non-conforming,
so fixing this is not crucial (no \0's should really ever be seen) but
it was an obvious bug in the code, which was attempting to ignore nul
chars (as do many other shells), so let it be fixed.

XXX pullup -9
 1.71 09-Feb-2019  kre branches: 1.71.2;

KNF - white space changes, indent using tabs not spaces. NFC.
 1.70 09-Feb-2019  kre 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.
 1.69 16-Jan-2019  kre 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).
 1.68 15-Jan-2019  kre 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.
 1.67 09-Jan-2019  kre Correct an (old) typo in a comment. NFC - it is just a comment.
 1.66 09-Jan-2019  kre 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.
 1.65 09-Jan-2019  kre 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.
 1.64 03-Dec-2018  kre 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.
 1.63 19-Aug-2018  kre 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)
 1.62 19-Aug-2017  kre branches: 1.62.2; 1.62.4;

NFC - DEBUG mode change only - add some sanity to a debug printf format string
 1.61 05-Aug-2017  kre 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.
 1.60 05-Jul-2017  kre 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.)
 1.59 30-Jun-2017  kre Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
 1.58 07-Jun-2017  kre A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)

This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)

The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.

This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).

I would not call the results of this perfect yet, but it is close.
 1.57 07-Jun-2017  kre An initial attempt at implementing LINENO to meet the specs.

Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.

Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)

This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.

POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)

This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.

This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).

Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
 1.56 03-May-2017  kre branches: 1.56.2;

Another fix from FreeBSD. I'm not sure how to trigger the problem
fixed (there might be no way) - but it "feels right"!

When popping an (exhausted) input string off the input stack, allow
for the possibility that the previous string might also just happened
to have run out of steam as well, so keep poppin' along until we
run out of pop, or find something to consume.
 1.55 03-May-2017  kre Deal with \newline line continuations more correctly.
They can occur anywhere (*anywhere*) not only where it
happens to be convenient to the parser...

This fix from FreeBSD (thanks again folks).

To make this work, pushstring()'s signature needed to change to allow a
const char * as its string arg, which meant sprinkling some const other
places for a brighter appearance (and handling fallout).

All this because I wanted to see what number would come from

echo $\
{\
L\
I\
N\
E\
N\
O\
}

and was surprised at the result! That works now...

The bug would also affect stuff like

true &\
& false

and all kinds of other uses where the \newline occurred in the
"wrong" place.

An ATF test for sh syntax is coming... (sometime.)
 1.54 03-May-2017  kre Fix idiot typos in previous (this is not the advertised :next commit")
Same typo - two different places. Ugh!
 1.53 03-May-2017  kre NFC: Change prototype of pushstring() to give a real type for the 3rd
arg (struct alias *) rather than using void * and then casting it
when used. For callers, the arg either is a struct alias *, or is NULL,
so nothing to adjust there.

NB: This change untested by itself, it was going to be a part of the next
change (coming in a few minutes) but is logically unrelated, so ...
 1.52 29-Apr-2017  kre Keep track of which file descriptors the shell is using for its
own purposes, and move them elsewhere whenever a user redirection
happens to pick the same number. With this we can move the shell
file descriptors back to lower values (be slightly kinder to the kernel)
since we can no longer clash. (Also get rid of a little old unneeded code.)

This also completes the fdflags command, which no longer permits access
to (by way or either obtaining, or changing) the shell's internal fds.
 1.51 01-Jun-2016  kre branches: 1.51.6;

PR bin/51207 Only check for ELF bnaries in regular files.
 1.50 07-May-2016  kre PR bin/51119 - don't leak FDs in unusual error cases. OK christos@
 1.49 02-May-2016  christos Fix handing of user file descriptors outside the 0..9 range.
Also, move (most of) the shell's internal use fd's to much
higher values (depending upon what ulimit -n allows) so they
are less likely to clash with user supplied fd numbers. A future
patch will (hopefully) avoid this problem completely by dynamically
moving the shell's internal fds around as needed. (From kre@)
 1.48 27-Mar-2016  christos General KNF and source code cleanups, avoid scattering the
magic string " \t\n" all over the place, slightly improved
syntax error messages, restructured some of the code for
clarity, don't allow IFS to be imported through the environment,
and remove the (never) conditionally compiled ATTY option.
Apart from one or two syntax error messages, and ignoring IFS
if present in the environment, this is intended to have no
user visible changes. (from kre@)
 1.47 04-Jan-2016  christos Don't leak redirected rescriptors to exec'ed processes. This is what ksh
does, but bash does not. For example:

$ cat test1
#!/bin/sh
exec 6> out
echo "test" >&6
sh ./test2
exec 6>&-
$ cat test2
echo "test2" >&6
$ ./test1
./test2: 6: Bad file descriptor

This fixes by side effect the problem of the rc system leaking file descriptors
7 and 8 to all starting daemons:

$ fstat -p 1359
USER CMD PID FD MOUNT INUM MODE SZ|DV R/W
root powerd 1359 wd / 2 drwxr-xr-x 512 r
root powerd 1359 0 / 63029 crw-rw-rw- null rw
root powerd 1359 1 / 63029 crw-rw-rw- null rw
root powerd 1359 2 / 63029 crw-rw-rw- null rw
root powerd 1359 3* kqueue pending 0
root powerd 1359 4 / 64463 crw-r----- power r
root powerd 1359 7 flags 0x80034<ISTTY,MPSAFE,LOCKSWORK,CLEAN>
root powerd 1359 8 flags 0x80034<ISTTY,MPSAFE,LOCKSWORK,CLEAN>
root powerd 1359 9* pipe 0xfffffe815d7bfdc0 -> 0x0 w

Note fd=7,8 pointing to the revoked pty from the parent rc process.
 1.46 30-Oct-2013  mrg #ifdef a variable decl/setting with it's use.
 1.45 28-Mar-2012  christos branches: 1.45.2;
include <limits.h> for CHAR_MIN/CHAR_MAX
 1.44 17-Feb-2011  pooka branches: 1.44.4;
Tell copyfd if the caller wants the exact tofd to just fd >= tofd.
Fixes "echo foo > /rump/bar" in a rump hijacked shell.

reviewed by christos
 1.43 30-Aug-2010  christos branches: 1.43.2;
dprintf is claimed by posix.
 1.42 10-Mar-2009  roy el_gets now sets el_len to -1 on error so we can distinguish
between a NULL string and an error.
This fixes sh from exiting with libedit now allowing EINTR to return.
We may need to expand this to an errno check in the future.
 1.41 16-Oct-2008  dholland branches: 1.41.4;
Use "extern" properly for referencing globals defined in other modules.
Now builds cleanly with -warn-common.
 1.40 15-Feb-2007  rillig Since interpreting ELF binaries as shell scripts is not very useful, and
since the current error message is less than helpful, improve it.
 1.39 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.38 15-May-2003  dsl Don't close any script files if vfork is set.
If a fork() is done later in order to feed a 'here' document into a pipe
then it is possible that one end of the pipe might get closed.
 1.37 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.36 27-Sep-2002  christos VFork()ing shell: From elric@netbsd.org:
Plus my changes:
- walking process group fix in foregrounding a job.
- reset of process group in parent shell if interrupted before the wait.
- move INTON lower in the dowait so that the job structure is
consistent.
- error check all setpgid(), tcsetpgrp() calls.
- eliminate unneeded strpgid() call.
- check that we don't belong in the process group before we try to
set it.
 1.35 04-Feb-2001  christos branches: 1.35.2;
remove redundant declarations and nexted externs.
 1.34 22-May-2000  elric Back out previous vfork changes.
 1.33 13-May-2000  elric Now we use vfork(2) instead of fork(2) when we can.
 1.32 09-Jul-1999  christos compile with WARNS = 2
 1.31 20-May-1998  christos Simplify interface for general use.
 1.30 20-May-1998  christos fix bug where if moving in history during a multi-line event, the
append to history event would end up in the last event where the history
was moved to instead of the multi-line event; reported by Mycroft
 1.29 29-Mar-1998  mrg - change "extern" variables into int's
- remove extern'd variables not actually referenced
- don't use char as an array index
 1.28 14-Oct-1997  christos branches: 1.28.2;
PR/4257: Jaromir Dolecek: Update for libedit interface change.
 1.27 04-Jul-1997  christos Fix compiler warnings.
 1.26 11-Apr-1997  christos Rename pread to preadfd
 1.25 14-Mar-1997  christos NO_HISTORY->SMALL
 1.24 13-Mar-1997  christos Fix problems with -DNO_HISTORY
 1.23 13-Mar-1997  mycroft Set input files to close-on-exec mode.
 1.22 11-Jan-1997  tls kill 'register'
 1.21 02-Nov-1996  christos Fix problems that gcc -Wall found (from Todd Miller, OpenBSD)
 1.20 16-Oct-1996  christos PR/2808: Remove trailing whitespace (from FreeBSD)
 1.19 19-Oct-1995  christos branches: 1.19.4;
- fix PR1620, -DNO_HISTORY did not work.
- restore parsing state after parsing old style command substitution.
The ';' in '`echo z;`' broke the following:
for i in 1; do
cat > /dev/tty << __EOF__
`echo z;`
__EOF__
done

cVS: Enter Log. Lines beginning with `CVS: ' are removed automatically
 1.18 06-Oct-1995  christos fixed previous booboo that broke command line editing input.
 1.17 26-Sep-1995  christos - Fix -v flag, so that it works properly when the shell reads from scripts.
- Bad style to fix my own PR, but I'd like to commit the parallel make
changes soon, and this is a necessary prerequisite.
 1.16 09-Jun-1995  christos Changed so that 'PATH=newpath command' works, instead of looking at the
old path. Synced input.c with vangogh.
 1.15 07-Jun-1995  cgd needs string.h
 1.14 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.13 21-Mar-1995  cgd convert to new RCS id conventions.
 1.12 23-Dec-1994  cgd pull prototypes into scope for string functions.
 1.11 04-Dec-1994  cgd from James Jegers <jimj@miller.cs.uwm.edu>: quiet -Wall, and squelch
some of the worst style errors.
 1.10 11-Jun-1994  mycroft Add RCS ids.
 1.9 08-Jun-1994  jtc Fix from Christos for when NO_HISTORY is defined
 1.8 21-May-1994  cgd a few more things to omit when NO_HISTORY defined. from noel@cs.oberlin.edu
 1.7 14-May-1994  cgd add back in support for building w/o obj dir. also, add NO_HISTORY
define, which (if you invoke mkbuiltins properly) gets you a sh w/o
history of command line editing (for floppy sh).
 1.6 12-May-1994  jtc Include appropriate header files to bring function prototypes into scope.
 1.5 11-May-1994  jtc sync with 4.4lite
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.19.4.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.28.2.1 08-May-1998  mycroft Sync with trunk, per request of christos.
 1.35.2.1 27-Mar-2002  elric Doing the vfork work on ash on a branch to try to shake out the
problems before I expose everyone to them. This checkin represents
a merge of the prior work, which I backed out a while ago, to the
HEAD only and does not incorporate any additional bugfixes. The
additional bugfixes and code-cleanup will occur in later checkins.

For reference the patches that were used are:
cvs diff -kk -r1.51 -r1.55 eval.c | patch
cvs diff -kk -r1.27 -r1.28 exec.c | patch
cvs diff -kk -r1.15 -r1.16 exec.h | patch
cvs diff -kk -r1.32 -r1.33 input.c | patch
cvs diff -kk -r1.10 -r1.11 input.h | patch
cvs diff -kk -r1.32 -r1.35 jobs.c | patch
cvs diff -kk -r1.9 -r1.11 jobs.h | patch
cvs diff -kk -r1.36 -r1.37 main.c | patch
cvs diff -kk -r1.20 -r1.21 redir.c | patch
cvs diff -kk -r1.10 -r1.11 redir.h | patch
cvs diff -kk -r1.10 -r1.12 shell.h | patch
cvs diff -kk -r1.22 -r1.23 trap.c | patch
cvs diff -kk -r1.12 -r1.13 trap.h | patch
cvs diff -kk -r1.23 -r1.24 var.c | patch
cvs diff -kk -r1.16 -r1.17 var.h | patch

All other changes were simply the resolution of the resulting
conflicts, which occured only in the merge of jobs.c.

Begins to address PR: bin/5475
 1.41.4.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.43.2.1 05-Mar-2011  bouyer Sync with HEAD
 1.44.4.2 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.44.4.1 17-Apr-2012  yamt sync with head
 1.45.2.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.51.6.2 11-May-2017  pgoyette Sync with HEAD
 1.51.6.1 02-May-2017  pgoyette Sync with HEAD - tag prg-localcount2-base1
 1.56.2.2 09-Aug-2017  snj Pull up following revision(s) (requested by kre in ticket #199):
bin/sh/input.c: revision 1.61
bin/sh/parser.c: revision 1.143
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.
 1.56.2.1 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.62.4.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.62.4.2 21-Apr-2020  martin Sync with HEAD
 1.62.4.1 10-Jun-2019  christos Sync with HEAD
 1.62.2.3 18-Jan-2019  pgoyette Synch with HEAD
 1.62.2.2 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.62.2.1 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.71.2.1 06-Apr-2021  martin Pull up following revision(s) (requested by kre in ticket #1242):

bin/sh/input.c: revision 1.72
bin/sh/exec.c: revision 1.55

PR bin/55979

This fixes the MSAN detected reference to an unitialised variable
(an unitialised field in a struct) which happens when a command is
not found after a PATH search.
Aside from skipping some known to be going to fail exec*() calls
in some cases, the setting of the relevant field is irrelevant,
so this problem makes no practical difference to the shell, or any
shell script.

XXX (maybe) pullup -9


PR bin/55979

Correctly handle (ie: ignore completely) \0 chars (nuls) in the
shell command input stream (script, dot file, or stdin).
Previously nul chars were ignored correctly in the line in which
they occurred, but would cause trailing chars of that line to reappear
as the start of the following line. If there was just one \0 skipped,
this would generally result in an extra \n in the sh input, which in
most cases has no effect. With multiple \0's in a single line, more
of the end of that line was duplicated into the following one. This
usually manifested as a weird "command not found" error.

Note that any \0 chars in the sh input make the script non-conforming,
so fixing this is not crucial (no \0's should really ever be seen) but
it was an obvious bug in the code, which was attempting to ignore nul
chars (as do many other shells), so let it be fixed.

XXX pullup -9
 1.72.8.1 02-Aug-2025  perseant Sync with HEAD
 1.22 03-Aug-2024  kre Change the "string" argument to evalstring() and setinputstring()
from being "char *" to being "const char *".

This is needed for a forthcoming change which needs to pass a const char *
to evalstring (and through it to setinputstring) and be assured that
nothing will alter the characters in the string supplied.

This is (aside from the additional compile time protection provided)
a no-op change, all evalstring() does with its string is pass it to
setinputstring() and all that does with it is determine its length
(strlen() which expects a const char *) and assign the string pointer
to parsenextc which is already a const char * - there never has been
any reason for these two functions to not include the "const" in
the arg declaration -- except that when originally written (early
1990's) I suspect "const" either didn't exist at all, or wasn't
supported by relevant compilers.

NFCI. Most probably (though I didn't check) no binary change at all.
 1.21 19-Aug-2018  kre branches: 1.21.12;
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)
 1.20 30-Jun-2017  kre branches: 1.20.4; 1.20.6;

Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
 1.19 07-Jun-2017  kre An initial attempt at implementing LINENO to meet the specs.

Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.

Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)

This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.

POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)

This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.

This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).

Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
 1.18 03-May-2017  kre branches: 1.18.2;

Deal with \newline line continuations more correctly.
They can occur anywhere (*anywhere*) not only where it
happens to be convenient to the parser...

This fix from FreeBSD (thanks again folks).

To make this work, pushstring()'s signature needed to change to allow a
const char * as its string arg, which meant sprinkling some const other
places for a brighter appearance (and handling fallout).

All this because I wanted to see what number would come from

echo $\
{\
L\
I\
N\
E\
N\
O\
}

and was surprised at the result! That works now...

The bug would also affect stuff like

true &\
& false

and all kinds of other uses where the \newline occurred in the
"wrong" place.

An ATF test for sh syntax is coming... (sometime.)
 1.17 03-May-2017  kre Fix idiot typos in previous (this is not the advertised :next commit")
Same typo - two different places. Ugh!
 1.16 03-May-2017  kre NFC: Change prototype of pushstring() to give a real type for the 3rd
arg (struct alias *) rather than using void * and then casting it
when used. For callers, the arg either is a struct alias *, or is NULL,
so nothing to adjust there.

NB: This change untested by itself, it was going to be a part of the next
change (coming in a few minutes) but is logically unrelated, so ...
 1.15 07-Aug-2003  agc branches: 1.15.88;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.14 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.13 27-Sep-2002  christos VFork()ing shell: From elric@netbsd.org:
Plus my changes:
- walking process group fix in foregrounding a job.
- reset of process group in parent shell if interrupted before the wait.
- move INTON lower in the dowait so that the job structure is
consistent.
- error check all setpgid(), tcsetpgrp() calls.
- eliminate unneeded strpgid() call.
- check that we don't belong in the process group before we try to
set it.
 1.12 22-May-2000  elric branches: 1.12.6;
Back out previous vfork changes.
 1.11 13-May-2000  elric Now we use vfork(2) instead of fork(2) when we can.
 1.10 09-Jul-1999  christos compile with WARNS = 2
 1.9 16-Oct-1996  christos PR/2808: Remove trailing whitespace (from FreeBSD)
 1.8 11-May-1995  christos branches: 1.8.6;
Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.7 21-Mar-1995  cgd convert to new RCS id conventions.
 1.6 11-Jun-1994  mycroft Add RCS ids.
 1.5 11-May-1994  jtc sync with 4.4lite
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.8.6.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.12.6.1 27-Mar-2002  elric Doing the vfork work on ash on a branch to try to shake out the
problems before I expose everyone to them. This checkin represents
a merge of the prior work, which I backed out a while ago, to the
HEAD only and does not incorporate any additional bugfixes. The
additional bugfixes and code-cleanup will occur in later checkins.

For reference the patches that were used are:
cvs diff -kk -r1.51 -r1.55 eval.c | patch
cvs diff -kk -r1.27 -r1.28 exec.c | patch
cvs diff -kk -r1.15 -r1.16 exec.h | patch
cvs diff -kk -r1.32 -r1.33 input.c | patch
cvs diff -kk -r1.10 -r1.11 input.h | patch
cvs diff -kk -r1.32 -r1.35 jobs.c | patch
cvs diff -kk -r1.9 -r1.11 jobs.h | patch
cvs diff -kk -r1.36 -r1.37 main.c | patch
cvs diff -kk -r1.20 -r1.21 redir.c | patch
cvs diff -kk -r1.10 -r1.11 redir.h | patch
cvs diff -kk -r1.10 -r1.12 shell.h | patch
cvs diff -kk -r1.22 -r1.23 trap.c | patch
cvs diff -kk -r1.12 -r1.13 trap.h | patch
cvs diff -kk -r1.23 -r1.24 var.c | patch
cvs diff -kk -r1.16 -r1.17 var.h | patch

All other changes were simply the resolution of the resulting
conflicts, which occured only in the merge of jobs.c.

Begins to address PR: bin/5475
 1.15.88.1 11-May-2017  pgoyette Sync with HEAD
 1.18.2.1 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.20.6.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.20.6.2 21-Apr-2020  martin Sync with HEAD
 1.20.6.1 10-Jun-2019  christos Sync with HEAD
 1.20.4.1 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.21.12.1 02-Aug-2025  perseant Sync with HEAD
 1.124 09-Apr-2025  kre DEBUG mode change only (NFC any normal build)

Don't potentially refer to an un-init'd variable in a DEBUG mode trace msg,
and don't bother setting the variable when it won't be used when DEBUG is
not enabled.
 1.123 09-Oct-2024  kre PR bin/58687 -- implement suspend as a builtin in sh

Requested by uwe@ in PR bin/58687 without objections from
anyone except me, here is an implementation of a suspend
builtin command for /bin/sh

The sh.1 man page is updated, naturally, to describe it.

This new builtin does not exist in SMALL shells -- as used
on (some) boot media, etc.

If this turns out not to be useful, it can easily be removed.
 1.122 18-Jun-2024  kre branches: 1.122.2;
Remove the possibility that all jobs won't be cleaned up by "wait"

There was a possibility that in a wait (no options or args) not
all jobs would be cleaned up, which that should be, as such a wait
should not finish (uninterrupted) until everything else is done.

This was unlikely to happen - but it's easy to make sure it doesn't.
 1.121 15-Jun-2024  kre When generating the text that identifies the command for the job tree
from the parse tree data, be more intelligent about where we stick ';'
characters.

Previously, given:

(sleep 2 & sleep 3 & sleep 10&) & jobs -l

The output would be something like

[1] + 23631 Running (sleep 2 &; sleep 3 &; sleep 10 &)

That's nonsense (even if clear enough). After this change the
output will be:

[1] + 12116 Running (sleep 2 & sleep 3 & sleep 10 &)

There are a few other cases where an incorrect ';' will also be suppressed.
 1.120 15-Jun-2024  kre POSIX.1-2024 requires that when an async (background) job is started
at the top level (ie: not in any kind of subshell environment) of an
interactive shell, that the shell print the job number assigned, and
the process id of the lead (or only) process in the job, in the form:

[JN] pid

Make that happen. (Other shells have been doing this for ages).
 1.119 30-Jan-2024  kre PR bin/57894

For jobs -p for a non-job-control job, avoid just printing 0 (as
there is no process group pid) and instead output what we used to,
the pid of one of the processes in the job (usually the right one!)

XXX pullup -10 (9 and earlier not affected).
 1.118 07-Apr-2023  kre The great shell trailing whitespace cleanup of 2023...
Inspired by private e-mail comments from mouse@

NFCI.
 1.117 30-Oct-2022  kre branches: 1.117.2;
PR bin/57053 is related (peripherally) here.

sh has been remembering the process group of a job for a while now, but
using that for almost nothing.

The old way to resume a job, was to try each pid in the job with a
SIGCONT (using it as the process group identifier via killpg()) until
one worked (or none did, in which case resuming would be impossible,
but that never actually happened). This wasn't as bad as it seems,
as in practice the first process attempted was *always* the correct
one. Why the loop was considered necessary I am not sure. Nothing
but the first could possibly work.

This worked until a fix for an obscure possible bug was added a
while ago - now a process which has already finished, and had its
zombie collected via wait*() is no longer ever considered to have
a pid which is a candidate for use in any system call. That's
because the kernel might have reassigned that pid for some newly
created process (we have no idea how much time might have passed
since the pid was returned to the kernel for reuse, it might have
happened weeks ago).

This is where the example in bin/57053 revealed a problem.

That PR is really about a quite different problem in zsh (from pksrc)
and should be pkg/57053, but as the test case also hit the problem
here, it was assumed (by some) they were the same issue.

The example is (in a small directory)
ls | less
which is then suspended (^Z), and resumed (fg). Since the directory
is small, ls will be finished, and reaped by sh - so the code would
now refuse to use its pid for the killpg() call to send the SIGCONT.
The (useless) loop would attempt to use less's pid for this purpose
(it is still alive at this point) but that would fail, as that pid
is not a process group identifier, of anything. Hence the job
could not be resumed.

Before the PR (or preceding mailing list discussion) the change here
had already been made (part of a much bigger set of changes, some of
which might follow - sometime). We now actually use the job's
remembered process group identifier when we want the process group
identifier, instead of trying to guess which pid it happens to be
(which actually never took any guessing, it was, and is always the
pid of the first process created for the job). A couple of minor
fixes to how the pgrp is obtained, and used, accompany the changes
to use it when appropriate.
 1.116 18-Apr-2022  kre Introduce a new macro JNUM to replace the idiom jp-jobtab+1
(the job number, given jp a pointer to a jobs table entry)
used open coded previously in many places (mostly in DEBUG mode
trace messages, so not included in most shells, but there are
a few others).

Make the type of JNUM() be int rather than the ptrdiff_t the
open coded version became ... which when used in some printf()
type function arg list was cast to some other arbitrary (but not
consistent) int type for which there is a standard %Xd type
format conversion. Now we can (and do) just use %d for this.

If the number of jobs ever exceeds the range of an int, we would
have far more serious problems than the broken output this would
cause.

While here improve a comment or two, and use JOBRUNNING instead
of 0 where the intent is the former (JOBRUNNING is #defined as 0).

NFCI.
 1.115 19-Dec-2021  andvar s/forground/foreground/ in comments.
 1.114 10-Oct-2021  rillig sh: remove useless do-while-0 loop

28 years was more than enough for the useless 'continue' statement in
this do-while-0 "loop". Without the 'continue' statement, there is no
need for the "loop" anymore. The comment at its top was confusing since
the word 'while' suggested a loop, but there was none, so remove that as
well.

Pointed out by Tom Ivar Helbekkmo on source-changes-d.

No change to the resulting binary.
 1.113 10-Oct-2021  rillig sh: remove no-op 'continue' from do-while-0 loop

With Clang, the only change to the binary are the line number changes
from __LINE__, GCC generates a bit different code.

No functional change.
 1.112 12-Sep-2021  kre Don't dereference NULL on "jobs -Z" (with no title given), instead
do setproctitle(NULL) (which is not the same thing at all). Do the
same with jobs -Z '' as setting the title to "sh: " isn't useful.

Improve the way this is documented, and note that it is only done
this way because zsh did it first (ie: pass on the balme, doing this
in the jobs command is simply absurd.)
 1.111 11-Sep-2021  christos Add jobs -Z (like in zsh(1)) to setproctitle(3).
 1.110 04-Apr-2021  kre Related to PR bin/48875

Correct an issue found by Oguz <oguzismailuysal@gmail.com> and reported
in e-mail (on the bug-bash list initially!) with the code changed to deal
with PR bin/48875

With:

sh -c 'echo start at $SECONDS;
(sleep 3 & (sleep 1& wait) );
echo end at $SECONDS'

The shell should say "start at 0\nend at 1\n", but instead (before
this fix, in -9 and HEAD, but not -8) does "start at 0\nend at 3\n"
(Not in -8 as the 48875 changes were never pulled up)>

There was an old problem, fixed years ago, which cause the same symptom,
related to the way the jobs table was cleared (or not) in subshells, and
it seemed like that might have resurfaced.

But not so, the issue here is the sub-shell elimination, which was part
of the 48875 "fix" (not really, it wasn't really a bug, just sub-optimal
and unexpected behaviour).

What the shell actually has been running in this case is:

sh -c 'echo start at $SECONDS;
(sleep 3 & sleep 1& wait );
echo end at $SECONDS'

as the inner subshell was deemed unnecessary - all its parent would
do is wait for its exit status, and then exit with that status - we
may as well simply replace the current sub-shell with the new one,
let it do its thing, and we're done...

But not here, the running "sleep 3" will remain a child of that merged
sub-shell, and the "wait" will thus wait for it, along with the sleep 1
which is all it should be seeing.

For now, fix this by not eliminating a sub-shell if there are existing
unwaited upon children in the current one. It might be possible to
simply disregard the old child for the purposes of wait (and "jobs", etc,
all cmds which look at the jobs table) but the bookkeeping required to
make that work reliably is likely to take some time to get correct...

Along with this fix comes a fix to DEBUG mode shells, which, in situations
like this, could dump core in the debug code if the relevant tracing was
enabled, and add a new trace for when the jobs table is cleared (which was
added predating the discovery of the actual cause of this issue, but seems
worth keeping.) Neither of these changes have any effect on shells
compiled normally.

XXX pullup -9
 1.109 30-Aug-2020  kre Since "struct job" gained a pgrp member some time ago now, use it
instead of simply assuming that the pid of the first (leftmost) process
in a pipeline is the pgrp - someday we may switch things around and
create pipelines right to left instead, which has several advantages,
but which would invalidate the assumption which was being made here.
 1.108 20-Aug-2020  kre Add lots of comments explaining what is happening in here.

Also enhance some of the DEBUG mode trace output (nothing visible
in a normal shell build).

A couple of very minor code changes that no-one should ever notice
(eg: one less wait() call in the case that there is nothing pending).
 1.107 07-Feb-2020  kre Avoid a core dump if a child process that is not one of our
children happens to exit while we are waiting for another child
to exit.

This can happen with code like

sh -c '
sleep 5 &
exec sh -c "sleep 10 & wait !$"
'

when the inner "sh" is waiting for the 10 second sleep to be
done, the 5 second sleep started earlier terminates. It is
a child of our process, as the inner shell is the same process
as the outer one, but not a known child (the inner shell has no
idea what the outer one did before it started).

This was observed in the wild by Martijn Dekker (where the outer
shell was bash but that's irrelevant).

XXX pullup -9
 1.106 26-Mar-2019  kre branches: 1.106.2;
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).
 1.105 09-Feb-2019  kre 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).
 1.104 09-Feb-2019  kre 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.
 1.103 03-Dec-2018  kre 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
 1.102 28-Oct-2018  kre 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).
 1.101 13-Sep-2018  kre 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
 1.100 04-Sep-2018  kre 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.
 1.99 04-Sep-2018  kre 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.
 1.98 30-Dec-2017  kre branches: 1.98.2; 1.98.4;
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.
 1.97 30-Dec-2017  christos initialize just used and prev_job
 1.96 30-Dec-2017  christos initialize the jobtab; it is easier than putting checks for used everywhere.
 1.95 28-Oct-2017  kre 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...)
 1.94 28-Oct-2017  kre Another %zu for size_t (this one in a DEBUG mode trace call, so it
doesn't actually ever bother anyone in practice.)
 1.93 25-Oct-2017  martin Use %zu for size_t
 1.92 25-Oct-2017  kre 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.
 1.91 23-Oct-2017  kre 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
 1.90 19-Oct-2017  kre 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).
 1.89 29-Sep-2017  kre DEBUG only changes (non-debug, ie: normal, shell unaffected)
Add a little extra info in a few of the trace messages.
 1.88 24-Jul-2017  kre 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.
 1.87 17-Jun-2017  kre NFC - DEBUG mode only change - convert this to the new TRACE() format.
 1.86 07-Jun-2017  kre A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)

This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)

The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.

This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).

I would not call the results of this perfect yet, but it is close.
 1.85 18-May-2017  kre branches: 1.85.2;

DEBUG mode only change - correctly track internal shell sub-shell nesting
levels for debug output. This change accidentally omitted earlier (only
effect is incorrect nesting levels shown in trace output when the option
to show them is enabled.) NFC for any normal shell build.
 1.84 11-May-2017  kre Avoid truncating the command string saved with background jobs if
one of the words happens to contain ${#var}. (This is the command
string shown by the "jobs" command, and when a background job completes)

While here, undo the LINENO hack when building that string.
And one ot two other foibles...
 1.83 09-May-2017  kre If we are going to permit
! ! pipeline
(And for now the other places where ! is permitted)
we should at least generate the logically correct exit
status:
! ! (exit 5); echo $?
should print 1, not 5. ksh and bosh do it this way - and it makes sense.
bash and the FreeBSD sh echo "5" (as did we until now.)
dash, zsh, yash all enforce the standard syntax, and prohibit this.
 1.82 04-May-2017  kre Implement the ';&' (used instead of ';;') case statement list terminator
which causes fall through the to command list of the following pattern
(wuthout evaluating that pattern). This has been approved for inclusion
in the next major version of the POSIX standard (Issue 8), and is
implemented by most other shells.

Now all form a circle and together attempt to summon the great wizd
in the hopes that his magic spells can transform the poor attempt
at documenting this feature into something rational...
 1.81 03-May-2017  kre Don't forget the ! reserved word exists (node ype NNOT) when displaying
"jobs" output (or other places where the cmd string is shown - like
when reporting status when a background job completes.)

Without this fix, try
! sleep 5 &
jobs
wait
and try not to wonder at the '???" that appears instead of "! sleep 5"
 1.80 29-Apr-2017  kre Keep track of which file descriptors the shell is using for its
own purposes, and move them elsewhere whenever a user redirection
happens to pick the same number. With this we can move the shell
file descriptors back to lower values (be slightly kinder to the kernel)
since we can no longer clash. (Also get rid of a little old unneeded code.)

This also completes the fdflags command, which no longer permits access
to (by way or either obtaining, or changing) the shell's internal fds.
 1.79 07-May-2016  kre branches: 1.79.6;

Slightly improve "jobs" command output in cases where a job includes
embedded background commands or pipelines. (just slightly...)
OK christos@
 1.78 03-May-2016  kre Unbreak build ... again... gcc is insane.
 1.77 03-May-2016  kre PR bin/51114 - print the correct values for >&- and >& N (N > 9)
in output from the "jobs" command (and other places that use the
same routines.)
 1.76 02-May-2016  christos Fix handing of user file descriptors outside the 0..9 range.
Also, move (most of) the shell's internal use fd's to much
higher values (depending upon what ulimit -n allows) so they
are less likely to clash with user supplied fd numbers. A future
patch will (hopefully) avoid this problem completely by dynamically
moving the shell's internal fds around as needed. (From kre@)
 1.75 22-Aug-2015  christos report the signal that wait was interrupted by, which is not always SIGINT
anymore.
 1.74 22-Aug-2015  christos Process pending signals while waiting for a job:
$ cat << EOF > hup.sh
#!/bin/sh
trap 'echo SIGHUP; exit 1' 1
sleep 10000 &
wait
EOF
$ chmod +x ./hup.sh
$ ./hup.sh &
$ kill -HUP %1
 1.73 11-Apr-2014  christos branches: 1.73.2;
PR/48729: Torbj�rn Granlund: Avoid negative index in array ref.
 1.72 26-Jan-2014  christos branches: 1.72.2;
explain why forks fail
 1.71 31-Dec-2012  dsl Add support for '%n' being a shorthand for 'fg %n'.
 1.70 23-Feb-2012  joerg branches: 1.70.2;
Don't use a for-loop with empty body.
 1.69 18-Jun-2011  christos branches: 1.69.2; 1.69.4;
PR/45069: Henning Petersen: Use prototypes from builtins.h .
 1.68 21-Dec-2008  christos branches: 1.68.6;
don't free other jobs when we are specifically using the wait built-in.
 1.67 21-Dec-2008  christos PR/35055: Wada Keiji: wait did not keep the status of terminated jobs as
expected.
 1.66 16-Oct-2008  dholland Wrap declaration of a STATIC function that's only conditionally defined
in a suitable ifdef, so things still compile if STATIC is defined as
"static", which is for some reason not the default.
 1.65 24-Apr-2006  snj It's "its."
 1.64 30-Sep-2005  tv Make slightly more portable without extra conditionals needed:

- Use getpgrp() rather than getpgid(0).
(getpgrp() is already used elsewhere in this same file.)

- Use waitpid(-1, a, b) rather than wait3(a, b, NULL).
 1.63 01-Jun-2005  lukem appease gcc -Wuninitialized
 1.62 18-Dec-2003  christos PR/23786: Robert Nestor: wait command returns the wrong status code. This
seems to be an off-by-one error, since njobs is decremented before we use
it. I hope this does not break the vfork case!
 1.61 27-Nov-2003  dsl Generate command line when SMALL is defined and make buffer full sized.
All the code has been present for a while, and the memory cost is (about)
180 bytes per process.
Fixes PR bin/23545
 1.60 14-Nov-2003  dsl Add '\n' to "fork failed" trace messages.
 1.59 26-Aug-2003  jmmv s/FD_SETFD/F_SETFD/
 1.58 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.57 27-Jan-2003  christos - use FD_CLOEXEC properly
- move ttyfd to a high fd
suggested by Tim Robbins at FreeBSD.
 1.56 25-Nov-2002  agc Make this compile on some of the more esoteric architectures (e.g. those
which are not i386)
 1.55 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.54 23-Oct-2002  christos From David Laight:

> Also 'jobs' fails in a non-interactive shell.
> In showjobs(), the code that puts the process back into its
> own process group should only be run if the shell is actually
> doing job control - eg if 'mflag' is set.
 1.53 28-Sep-2002  mycroft Revert the previous for now. This is truly gross.
 1.52 28-Sep-2002  mycroft Remove the duplicate setpgid() in forkparent(). The real bug was freeing the
job structure in the child and referencing freed memory.
 1.51 28-Sep-2002  christos Revert previous change. No need to save rootshell. It is only affecting
the non-vfork case. Having said that, it would be nice if pipelines of
simple commands were vforked too. Right now they are not.
Explain that setpgid() might fail because we are doing it both in the
parent and the child case, because we don't know which one will come
first.
Suspending a pipeline prints %1 Suspended n times where n is the number
of processes, but that was there before. It is easy to fix, but I'll
leave the code alone for now.
 1.50 27-Sep-2002  christos Deal with rootshell not being maintained correctly in the vfork() case.
Propagate isroot, throughout the eval process and maintain it properly.
Fixes sleep 10 | cat^C not exiting because sleep and cat ended up in
their own process groups, because wasroot was always true in the children.
 1.49 27-Sep-2002  mycroft Clean up INTOFF/INTON usage a little -- none of fork{shell,parent,child}()
screw with them now, only their callers.
 1.48 27-Sep-2002  christos off by one in nprocs.
 1.47 27-Sep-2002  christos VFork()ing shell: From elric@netbsd.org:
Plus my changes:
- walking process group fix in foregrounding a job.
- reset of process group in parent shell if interrupted before the wait.
- move INTON lower in the dowait so that the job structure is
consistent.
- error check all setpgid(), tcsetpgrp() calls.
- eliminate unneeded strpgid() call.
- check that we don't belong in the process group before we try to
set it.
 1.46 15-May-2002  christos implement noclobber. From Ben Harris, with minor tweaks from me. Two
unimplemented comments to go. Go Ben!
 1.45 10-Apr-2002  christos Try harder to find a tty.
 1.44 03-Apr-2002  christos PR/16174: Matthias Buelow: Job control fails on jobs that fd == 2 does not
point to a tty.

Unfortunately the shell assumed that it could do all process group
handling ioctls to fd=2, but this is not correct. Jobs that redirected
fd=2 would be unable to perform the ioctls and silently fail since
the error reporting channel is fd=2... Instead open /dev/tty set
it to close on exec, and use that instead (like all other shells
do). We don't handle the case where the OS does not provide FD_CLOEXEC
or FIOCLEX, because I am lazy.

While I am there:
- Simplify the code by defining functions for tc{g,s}pgrp when OLD_TTY_DRIVER
is defined.
- make sure that 'sh &' works by stopping itself. Don't kill the shell's
process group, kill the shell itself.
 1.43 22-Mar-2002  christos branches: 1.43.2;
PR/16010: Andreas Gustafsson: wait is not interruptible.
 1.42 12-Mar-2002  simonb In forkshell() don't free the current job. Fixes problem with previous
commit where pipeline commands didn't inherit the correct process group.
Reviewed by Christos.

Change a trace format string arg to use %p instead of %x and a long cast.
 1.41 12-Mar-2002  christos PR/15877: Ed Ravin: make sure that we cleanup enough of the job structure,
in jobfree() so that we don't fail when we use a malloc where free() trashes
freed memory.
 1.40 16-Sep-2001  wiz Spell 'occurred' with two 'r's.
 1.39 13-Sep-2001  assar (waitproc): handle jp == NULL, it happens
 1.38 10-Sep-2001  christos Don't wait with UNTRACED for jobs that have not been started with job control
(i.e. processes started from shell scripts). Fixes problem where kill -STOP'ing
a subprocess of a shell script would cause the shell to proceed to the next
command.
 1.37 13-Jun-2001  lukem fix grammar
 1.36 22-May-2000  elric Back out previous vfork changes.
 1.35 15-May-2000  elric INTON and FORCEINTON modify global variables, and so should not be
executed while we are vforked.
 1.34 13-May-2000  elric Unused variable pgrp on line 576.
 1.33 13-May-2000  elric Now we use vfork(2) instead of fork(2) when we can.
 1.32 31-Aug-1999  mycroft It turns out that the code disabled in revision 1.30 *does* have an important
function (which nobody was able to explain): it's critical to allowing a
complex command run from an interactive shell to be terminated. So, reinstate
it and fix it correctly. See the comment if you really want the gory details.
 1.31 09-Jul-1999  christos compile with WARNS = 2
 1.30 05-Apr-1999  mycroft Disable bogus-looking code that cause us to throw away SIGINTs.
 1.29 04-Feb-1999  christos branches: 1.29.2;
PR/4966: Joel Reicher: Implement <> redirections which are documented in
the man page.
 1.28 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.27 20-May-1998  christos cast pid's to longs for OS's where this is the case.
 1.26 07-Apr-1998  fair Change a few things to reference /usr/include/paths.h instead of local
references. Fixing the default PATH is a bit more effort.
 1.25 04-Feb-1998  thorpej Sigh, have to cast pointer diffs to long.
 1.24 04-Feb-1998  thorpej Fix printf formats so they work on the Alpha.
 1.23 08-Oct-1997  christos branches: 1.23.2;
PR/3662: David Holland: Make /bin/sh use the symbolic macros for wait().
 1.22 04-Jul-1997  christos Fix compiler warnings.
 1.21 11-Jan-1997  tls kill 'register'
 1.20 02-Nov-1996  christos Fix problems that gcc -Wall found (from Todd Miller, OpenBSD)
 1.19 16-Oct-1996  christos PR/2808: Don't include <sgtty.h> unless OLD_TTY_DRIVER is defined.
Remove extraneous newline from error messages.
 1.18 17-Sep-1996  mycroft ioctl(TIOC[GS]PGRP) -> tc[gs]etpgrp()
 1.17 04-Jul-1995  pk branches: 1.17.6;
Relocate pointers after re-allocation of the job table.
 1.16 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.15 21-Mar-1995  cgd convert to new RCS id conventions.
 1.14 23-Dec-1994  cgd be more careful with casts.
 1.13 04-Dec-1994  cgd from James Jegers <jimj@miller.cs.uwm.edu>: quiet -Wall, and squelch
some of the worst style errors.
 1.12 23-Sep-1994  mycroft Eliminate uses of some obsolete functions.
 1.11 11-Jun-1994  mycroft Add RCS ids.
 1.10 12-May-1994  jtc POSIX.1 changed getpgrp
 1.9 12-May-1994  jtc Use libc's sys_siglist[] instead of building and using our own array of
signal names. (from charles)
 1.8 11-May-1994  jtc sync with 4.4lite
 1.7 06-Aug-1993  mycroft Use sys_signame[].
 1.6 01-Aug-1993  mycroft Add RCS identifiers.
 1.5 02-May-1993  sef Jim "wilson@moria.cygnus.com" Wilson's patches to make C News (and other
things) work.
 1.4 26-Apr-1993  dpassage Removed a cast that was causing another stoopid compile error.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.17.6.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.23.2.1 08-May-1998  mycroft Sync with trunk, per request of christos.
 1.29.2.2 09-Oct-1999  cgd pull up rev 1.32 from trunk (requested by mycroft):
Allow the tty interrupt character (e.g. ^C) to interrupt loops run
in an interactive shell again.
 1.29.2.1 19-Apr-1999  mycroft Pull up rev 1.30:
Disable bogus-looking code that cause us to throw away SIGINTs.
 1.43.2.1 27-Mar-2002  elric Doing the vfork work on ash on a branch to try to shake out the
problems before I expose everyone to them. This checkin represents
a merge of the prior work, which I backed out a while ago, to the
HEAD only and does not incorporate any additional bugfixes. The
additional bugfixes and code-cleanup will occur in later checkins.

For reference the patches that were used are:
cvs diff -kk -r1.51 -r1.55 eval.c | patch
cvs diff -kk -r1.27 -r1.28 exec.c | patch
cvs diff -kk -r1.15 -r1.16 exec.h | patch
cvs diff -kk -r1.32 -r1.33 input.c | patch
cvs diff -kk -r1.10 -r1.11 input.h | patch
cvs diff -kk -r1.32 -r1.35 jobs.c | patch
cvs diff -kk -r1.9 -r1.11 jobs.h | patch
cvs diff -kk -r1.36 -r1.37 main.c | patch
cvs diff -kk -r1.20 -r1.21 redir.c | patch
cvs diff -kk -r1.10 -r1.11 redir.h | patch
cvs diff -kk -r1.10 -r1.12 shell.h | patch
cvs diff -kk -r1.22 -r1.23 trap.c | patch
cvs diff -kk -r1.12 -r1.13 trap.h | patch
cvs diff -kk -r1.23 -r1.24 var.c | patch
cvs diff -kk -r1.16 -r1.17 var.h | patch

All other changes were simply the resolution of the resulting
conflicts, which occured only in the merge of jobs.c.

Begins to address PR: bin/5475
 1.68.6.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.69.4.2 15-Nov-2015  bouyer Pull up following revision(s) (requested by christos in ticket #1323):
bin/sh/jobs.c: revision 1.74
bin/sh/jobs.c: revision 1.75
bin/sh/trap.c: revision 1.36
bin/sh/trap.c: revision 1.37
bin/sh/trap.h: revision 1.21
bin/sh/trap.h: revision 1.22
Process pending signals while waiting for a job:
$ cat << EOF > hup.sh
#!/bin/sh
trap 'echo SIGHUP; exit 1' 1
sleep 10000 &
wait
EOF
$ chmod +x ./hup.sh
$ ./hup.sh &
$ kill -HUP %1
report the signal that wait was interrupted by, which is not always SIGINT
anymore.
 1.69.4.1 07-Dec-2014  martin Pullup the following revisions, requested by spz in #1205:
src/bin/sh/jobs.c 1.73

PR/48729: Torbj�rn Granlund: Avoid negative index in array ref.
 1.69.2.3 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.69.2.2 23-Jan-2013  yamt sync with head
 1.69.2.1 17-Apr-2012  yamt sync with head
 1.70.2.2 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.70.2.1 25-Feb-2013  tls resync with head
 1.72.2.1 10-Aug-2014  tls Rebase.
 1.73.2.1 04-Nov-2015  riz Pull up following revision(s) (requested by christos in ticket #964):
bin/sh/jobs.c: revision 1.74
bin/sh/jobs.c: revision 1.75
bin/sh/trap.c: revision 1.36
bin/sh/trap.c: revision 1.37
bin/sh/trap.h: revision 1.21
bin/sh/trap.h: revision 1.22
Process pending signals while waiting for a job:
$ cat << EOF > hup.sh
#!/bin/sh
trap 'echo SIGHUP; exit 1' 1
sleep 10000 &
wait
EOF
$ chmod +x ./hup.sh
$ ./hup.sh &
$ kill -HUP %1
report the signal that wait was interrupted by, which is not always SIGINT
anymore.
 1.79.6.3 19-May-2017  pgoyette Resolve conflicts from previous merge (all resulting from $NetBSD
keywork expansion)
 1.79.6.2 11-May-2017  pgoyette Sync with HEAD
 1.79.6.1 02-May-2017  pgoyette Sync with HEAD - tag prg-localcount2-base1
 1.85.2.3 09-Oct-2018  martin Pull up following revision(s) (requested by kre in ticket #1050):

bin/sh/jobs.c: revision 1.101

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
 1.85.2.2 17-Nov-2017  martin Pull up following revision(s) (requested by kre in ticket #337):
bin/sh/jobs.c: revision 1.91 (patch)


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.
 1.85.2.1 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.98.4.4 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.98.4.3 21-Apr-2020  martin Sync with HEAD
 1.98.4.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.98.4.1 10-Jun-2019  christos Sync with HEAD
 1.98.2.4 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.98.2.3 26-Nov-2018  pgoyette Sync with HEAD, resolve a couple of conflicts
 1.98.2.2 30-Sep-2018  pgoyette Ssync with HEAD
 1.98.2.1 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.106.2.2 28-Apr-2021  martin Pull up following revision(s) (requested by kre in ticket #1259):

bin/sh/jobs.h: revision 1.24
bin/sh/eval.c: revision 1.182
bin/sh/jobs.c: revision 1.110

Related to PR bin/48875

Correct an issue found by Oguz <oguzismailuysal@gmail.com> and reported
in e-mail (on the bug-bash list initially!) with the code changed to deal
with PR bin/48875

With:
sh -c 'echo start at $SECONDS;
(sleep 3 & (sleep 1& wait) );
echo end at $SECONDS'

The shell should say "start at 0\nend at 1\n", but instead (before
this fix, in -9 and HEAD, but not -8) does "start at 0\nend at 3\n"
(Not in -8 as the 48875 changes were never pulled up)>

There was an old problem, fixed years ago, which cause the same symptom,
related to the way the jobs table was cleared (or not) in subshells, and
it seemed like that might have resurfaced.

But not so, the issue here is the sub-shell elimination, which was part
of the 48875 "fix" (not really, it wasn't really a bug, just sub-optimal
and unexpected behaviour).

What the shell actually has been running in this case is:

sh -c 'echo start at $SECONDS;
(sleep 3 & sleep 1& wait );
echo end at $SECONDS'

as the inner subshell was deemed unnecessary - all its parent would
do is wait for its exit status, and then exit with that status - we
may as well simply replace the current sub-shell with the new one,
let it do its thing, and we're done...

But not here, the running "sleep 3" will remain a child of that merged
sub-shell, and the "wait" will thus wait for it, along with the sleep 1
which is all it should be seeing.

For now, fix this by not eliminating a sub-shell if there are existing
unwaited upon children in the current one. It might be possible to
simply disregard the old child for the purposes of wait (and "jobs", etc,
all cmds which look at the jobs table) but the bookkeeping required to
make that work reliably is likely to take some time to get correct...

Along with this fix comes a fix to DEBUG mode shells, which, in situations
like this, could dump core in the debug code if the relevant tracing was
enabled, and add a new trace for when the jobs table is cleared (which was
added predating the discovery of the actual cause of this issue, but seems
worth keeping.) Neither of these changes have any effect on shells
compiled normally.

XXX pullup -9
 1.106.2.1 10-Feb-2020  martin Pull up following revision(s) (requested by kre in ticket #684):

bin/sh/jobs.c: revision 1.107
bin/sh/main.c: revision 1.83
bin/sh/main.c: revision 1.84
bin/sh/main.c: revision 1.85

If we are invoked with SIGCHLD ignored, we fail badly, as we assume
that we can always wait(2) for our children, and an ignored SIGCHLD
prevents that. Recent versions of bash can be convinced (due to a
bug most likely) to invoke us that way. Always return SIGCHLD to
SIG_DFL during init - we already prevent scripts from fiddling it.

All ash derived shells apparently have this problem (observed by
Martijn Dekker, and notified on the bash-bug list). Actual issue
diagnosed by Harald van Dijk (same list).

Actually, the issue with bash (in previous) is more likely that the
SIGCHLD is blocked rather than ignored. We want neither. Make sure
SIGCHLD is unblocked as well as SIG_DFL.

XXX pullup -9

bin/sh: Fixes -Werror=shadow causing build breaks.
Conflicting variable name, sigset_t sigs has been renamed to sigset_t mask

Reviewed by: kamil@

Avoid a core dump if a child process that is not one of our
children happens to exit while we are waiting for another child
to exit.

This can happen with code like
sh -c '
sleep 5 &
exec sh -c "sleep 10 & wait !$"
'

when the inner "sh" is waiting for the 10 second sleep to be
done, the 5 second sleep started earlier terminates. It is
a child of our process, as the inner shell is the same process
as the outer one, but not a known child (the inner shell has no
idea what the outer one did before it started).

This was observed in the wild by Martijn Dekker (where the outer
shell was bash but that's irrelevant).

XXX pullup -9
 1.117.2.1 03-Feb-2024  martin Pull up following revision(s) (requested by kre in ticket #577):

bin/sh/jobs.c: revision 1.119

PR bin/57894

For jobs -p for a non-job-control job, avoid just printing 0 (as
there is no process group pid) and instead output what we used to,
the pid of one of the processes in the job (usually the right one!)
 1.122.2.1 02-Aug-2025  perseant Sync with HEAD
 1.27 15-Jun-2024  kre POSIX.1-2024 requires that when an async (background) job is started
at the top level (ie: not in any kind of subshell environment) of an
interactive shell, that the shell print the job number assigned, and
the process id of the lead (or only) process in the job, in the form:

[JN] pid

Make that happen. (Other shells have been doing this for ages).
 1.26 07-Apr-2023  kre The great shell trailing whitespace cleanup of 2023...
Inspired by private e-mail comments from mouse@

NFCI.
 1.25 11-Sep-2021  christos Add jobs -Z (like in zsh(1)) to setproctitle(3).
 1.24 04-Apr-2021  kre Related to PR bin/48875

Correct an issue found by Oguz <oguzismailuysal@gmail.com> and reported
in e-mail (on the bug-bash list initially!) with the code changed to deal
with PR bin/48875

With:

sh -c 'echo start at $SECONDS;
(sleep 3 & (sleep 1& wait) );
echo end at $SECONDS'

The shell should say "start at 0\nend at 1\n", but instead (before
this fix, in -9 and HEAD, but not -8) does "start at 0\nend at 3\n"
(Not in -8 as the 48875 changes were never pulled up)>

There was an old problem, fixed years ago, which cause the same symptom,
related to the way the jobs table was cleared (or not) in subshells, and
it seemed like that might have resurfaced.

But not so, the issue here is the sub-shell elimination, which was part
of the 48875 "fix" (not really, it wasn't really a bug, just sub-optimal
and unexpected behaviour).

What the shell actually has been running in this case is:

sh -c 'echo start at $SECONDS;
(sleep 3 & sleep 1& wait );
echo end at $SECONDS'

as the inner subshell was deemed unnecessary - all its parent would
do is wait for its exit status, and then exit with that status - we
may as well simply replace the current sub-shell with the new one,
let it do its thing, and we're done...

But not here, the running "sleep 3" will remain a child of that merged
sub-shell, and the "wait" will thus wait for it, along with the sleep 1
which is all it should be seeing.

For now, fix this by not eliminating a sub-shell if there are existing
unwaited upon children in the current one. It might be possible to
simply disregard the old child for the purposes of wait (and "jobs", etc,
all cmds which look at the jobs table) but the bookkeeping required to
make that work reliably is likely to take some time to get correct...

Along with this fix comes a fix to DEBUG mode shells, which, in situations
like this, could dump core in the debug code if the relevant tracing was
enabled, and add a new trace for when the jobs table is cleared (which was
added predating the discovery of the actual cause of this issue, but seems
worth keeping.) Neither of these changes have any effect on shells
compiled normally.

XXX pullup -9
 1.23 11-Sep-2018  kre branches: 1.23.2;

Whitespace cleanup from last update. NFC.
 1.22 04-Sep-2018  kre 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.
 1.21 28-Oct-2017  kre branches: 1.21.2; 1.21.4;
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...)
 1.20 18-Jun-2011  christos PR/45069: Henning Petersen: Use prototypes from builtins.h .
 1.19 27-Nov-2003  dsl branches: 1.19.52;
Generate command line when SMALL is defined and make buffer full sized.
All the code has been present for a while, and the memory cost is (about)
180 bytes per process.
Fixes PR bin/23545
 1.18 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.17 22-Jan-2003  dsl Support command -p, -v and -V as posix
Stop temporary PATH assigments messing up hash table
Fix sh -c -e "echo $0 $*" -a x (as posix)
(agreed by christos)
 1.16 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.15 28-Sep-2002  christos Revert previous change. No need to save rootshell. It is only affecting
the non-vfork case. Having said that, it would be nice if pipelines of
simple commands were vforked too. Right now they are not.
Explain that setpgid() might fail because we are doing it both in the
parent and the child case, because we don't know which one will come
first.
Suspending a pipeline prints %1 Suspended n times where n is the number
of processes, but that was there before. It is easy to fix, but I'll
leave the code alone for now.
 1.14 27-Sep-2002  christos Deal with rootshell not being maintained correctly in the vfork() case.
Propagate isroot, throughout the eval process and maintain it properly.
Fixes sleep 10 | cat^C not exiting because sleep and cat ended up in
their own process groups, because wasroot was always true in the children.
 1.13 27-Sep-2002  christos VFork()ing shell: From elric@netbsd.org:
Plus my changes:
- walking process group fix in foregrounding a job.
- reset of process group in parent shell if interrupted before the wait.
- move INTON lower in the dowait so that the job structure is
consistent.
- error check all setpgid(), tcsetpgrp() calls.
- eliminate unneeded strpgid() call.
- check that we don't belong in the process group before we try to
set it.
 1.12 22-May-2000  elric branches: 1.12.6;
Back out previous vfork changes.
 1.11 13-May-2000  elric Quick fix.

jobs.h:91: warning: declaration of `vfork' shadows global declaration
 1.10 13-May-2000  elric Now we use vfork(2) instead of fork(2) when we can.
 1.9 15-Oct-1997  christos pid should be pid_t and status should be int! Avoid alignment problems...
 1.8 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.7 21-Mar-1995  cgd convert to new RCS id conventions.
 1.6 11-Jun-1994  mycroft Add RCS ids.
 1.5 11-May-1994  jtc sync with 4.4lite
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.12.6.1 27-Mar-2002  elric Doing the vfork work on ash on a branch to try to shake out the
problems before I expose everyone to them. This checkin represents
a merge of the prior work, which I backed out a while ago, to the
HEAD only and does not incorporate any additional bugfixes. The
additional bugfixes and code-cleanup will occur in later checkins.

For reference the patches that were used are:
cvs diff -kk -r1.51 -r1.55 eval.c | patch
cvs diff -kk -r1.27 -r1.28 exec.c | patch
cvs diff -kk -r1.15 -r1.16 exec.h | patch
cvs diff -kk -r1.32 -r1.33 input.c | patch
cvs diff -kk -r1.10 -r1.11 input.h | patch
cvs diff -kk -r1.32 -r1.35 jobs.c | patch
cvs diff -kk -r1.9 -r1.11 jobs.h | patch
cvs diff -kk -r1.36 -r1.37 main.c | patch
cvs diff -kk -r1.20 -r1.21 redir.c | patch
cvs diff -kk -r1.10 -r1.11 redir.h | patch
cvs diff -kk -r1.10 -r1.12 shell.h | patch
cvs diff -kk -r1.22 -r1.23 trap.c | patch
cvs diff -kk -r1.12 -r1.13 trap.h | patch
cvs diff -kk -r1.23 -r1.24 var.c | patch
cvs diff -kk -r1.16 -r1.17 var.h | patch

All other changes were simply the resolution of the resulting
conflicts, which occured only in the merge of jobs.c.

Begins to address PR: bin/5475
 1.19.52.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.21.4.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.21.4.2 21-Apr-2020  martin Sync with HEAD
 1.21.4.1 10-Jun-2019  christos Sync with HEAD
 1.21.2.2 30-Sep-2018  pgoyette Ssync with HEAD
 1.21.2.1 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.23.2.1 28-Apr-2021  martin Pull up following revision(s) (requested by kre in ticket #1259):

bin/sh/jobs.h: revision 1.24
bin/sh/eval.c: revision 1.182
bin/sh/jobs.c: revision 1.110

Related to PR bin/48875

Correct an issue found by Oguz <oguzismailuysal@gmail.com> and reported
in e-mail (on the bug-bash list initially!) with the code changed to deal
with PR bin/48875

With:
sh -c 'echo start at $SECONDS;
(sleep 3 & (sleep 1& wait) );
echo end at $SECONDS'

The shell should say "start at 0\nend at 1\n", but instead (before
this fix, in -9 and HEAD, but not -8) does "start at 0\nend at 3\n"
(Not in -8 as the 48875 changes were never pulled up)>

There was an old problem, fixed years ago, which cause the same symptom,
related to the way the jobs table was cleared (or not) in subshells, and
it seemed like that might have resurfaced.

But not so, the issue here is the sub-shell elimination, which was part
of the 48875 "fix" (not really, it wasn't really a bug, just sub-optimal
and unexpected behaviour).

What the shell actually has been running in this case is:

sh -c 'echo start at $SECONDS;
(sleep 3 & sleep 1& wait );
echo end at $SECONDS'

as the inner subshell was deemed unnecessary - all its parent would
do is wait for its exit status, and then exit with that status - we
may as well simply replace the current sub-shell with the new one,
let it do its thing, and we're done...

But not here, the running "sleep 3" will remain a child of that merged
sub-shell, and the "wait" will thus wait for it, along with the sleep 1
which is all it should be seeing.

For now, fix this by not eliminating a sub-shell if there are existing
unwaited upon children in the current one. It might be possible to
simply disregard the old child for the purposes of wait (and "jobs", etc,
all cmds which look at the jobs table) but the bookkeeping required to
make that work reliably is likely to take some time to get correct...

Along with this fix comes a fix to DEBUG mode shells, which, in situations
like this, could dump core in the debug code if the relevant tracing was
enabled, and add a new trace for when the jobs table is cleared (which was
added predating the discovery of the actual cause of this issue, but seems
worth keeping.) Neither of these changes have any effect on shells
compiled normally.

XXX pullup -9
 1.11 07-Aug-2003  agc branches: 1.11.98;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.10 07-Oct-2002  christos explain what's breaking with alignments > 16, but no fix yet.
 1.9 04-Oct-2002  christos Rename ALIGN to SHELL_ALIGN and simplify macro so that it does not have side
effects, and add double to it, so that it aligns doubles correctly too. This
is just a workaround to fix the sparc64 problem where ALIGN() is now defined
in some include file to be 16 instead of 8. Thanks to martin for debugging this.
 1.8 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.7 21-Mar-1995  cgd convert to new RCS id conventions.
 1.6 11-Jun-1994  mycroft Add RCS ids.
 1.5 11-May-1994  jtc sync with 4.4lite
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.11.98.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.11.98.1 21-Apr-2020  martin Sync with HEAD
 1.18 04-Jun-2017  kre branches: 1.18.6;
If we are going to keep the MAILPATH % hack, then at least do something
rational. Since it isn't documented, what "rational" is is up for
discussion, but what it did before was not it (it was nonsense...).
 1.17 04-Jun-2017  kre Make cd (really) do cd -P, and not just claim that is what it is doing
while doing a half-hearted, broken, partial, version of cd -L instead.
The latter (as the manual says) is not supported, what's more, it is an
abomination, and should never be supported (anywhere.)

Fix the doc so that the pretense that we notice when a path given crosses
a symlink (and turns on printing of the destination directory) is claimed
no more (that used to be true until late Dec 2016, but was changed). Now
the print happens if -o cdprint is set, or if an entry from CDPATH that is
not "" or "." is used (or if the "cd dest repl" cd cmd variant is used.)

Fix CDPATH processing: avoid the magic '%' processing that is used for
PATH and MAILPATH from corrupting CDPATH. The % magic (both variants)
remains undocumented.

Also, don't double the '/' if an entry in PATH or CDPATH ends in '/'
(as in CDPATH=":/usr/src/"). A "cd usr.bin" used to do
chdir("/usr/src//usr.bin"). No more. This is almost invisible,
and relatively harmless, either way....

Also fix a bug where if a plausible destination directory in CDPATH
was located, but the chdir() failed (eg: permission denied) and then
a later "." or "" CDPATH entry succeeded, "print" mode was turned on.
That is:
cd /tmp; mkdir bin
mkdir -p P/bin; chmod 0 P/bin
CDPATH=/tmp/P:
cd bin
would cd to /tmp/bin (correctly) but print it (incorrectly).

Also when in "cd dest replace" mode, if the result of the replacement
generates '-' as the path named, as in:
cd $PWD -
then simply change to '-' (or attempt to, with CDPATH search), rather
than having this being equivalent to "cd -")

Because of these changes, the pwd command (and $PWD) essentially
always acts as pwd -P, even when called as pwd -L (which is still
the default.) That is, even more than it did before.

Also fixed a (kind of minor) mem management error (CDPATH related)
"whosoever shall padvance must stunalloc before repeating" (and the
same for MAILPATH).
 1.16 07-Aug-2003  agc branches: 1.16.90;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.15 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.14 03-Jul-2000  matt include <stdlib.h>, <string.h>, or whatever as appropriate to shut up
gcc 2.96
 1.13 09-Jul-1999  christos compile with WARNS = 2
 1.12 31-Jan-1998  christos Include our local includes after the system's ones.
 1.11 04-Jul-1997  christos branches: 1.11.2;
Fix compiler warnings.
 1.10 11-Jan-1997  tls kill 'register'
 1.9 11-May-1995  christos branches: 1.9.6;
Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.8 21-Mar-1995  cgd convert to new RCS id conventions.
 1.7 04-Dec-1994  cgd from James Jegers <jimj@miller.cs.uwm.edu>: quiet -Wall, and squelch
some of the worst style errors.
 1.6 11-Jun-1994  mycroft Add RCS ids.
 1.5 11-May-1994  jtc sync with 4.4lite
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.9.6.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.11.2.1 08-May-1998  mycroft Sync with trunk, per request of christos.
 1.16.90.1 05-Jun-2017  snj Pull up following revision(s) (requested by kre in ticket #5):
bin/sh/cd.c: revision 1.48
bin/sh/eval.c: revision 1.141
bin/sh/exec.c: revision 1.48
bin/sh/exec.h: revision 1.25
bin/sh/mail.c: revisions 1.17, 1.18
bin/sh/sh.1: revision 1.147
Make cd (really) do cd -P, and not just claim that is what it is doing
while doing a half-hearted, broken, partial, version of cd -L instead.
The latter (as the manual says) is not supported, what's more, it is an
abomination, and should never be supported (anywhere.)
Fix the doc so that the pretense that we notice when a path given crosses
a symlink (and turns on printing of the destination directory) is claimed
no more (that used to be true until late Dec 2016, but was changed). Now
the print happens if -o cdprint is set, or if an entry from CDPATH that is
not "" or "." is used (or if the "cd dest repl" cd cmd variant is used.)
Fix CDPATH processing: avoid the magic '%' processing that is used for
PATH and MAILPATH from corrupting CDPATH. The % magic (both variants)
remains undocumented.
Also, don't double the '/' if an entry in PATH or CDPATH ends in '/'
(as in CDPATH=":/usr/src/"). A "cd usr.bin" used to do
chdir("/usr/src//usr.bin"). No more. This is almost invisible,
and relatively harmless, either way....
Also fix a bug where if a plausible destination directory in CDPATH
was located, but the chdir() failed (eg: permission denied) and then
a later "." or "" CDPATH entry succeeded, "print" mode was turned on.
That is:
cd /tmp; mkdir bin
mkdir -p P/bin; chmod 0 P/bin
CDPATH=/tmp/P:
cd bin
would cd to /tmp/bin (correctly) but print it (incorrectly).
Also when in "cd dest replace" mode, if the result of the replacement
generates '-' as the path named, as in:
cd $PWD -
then simply change to '-' (or attempt to, with CDPATH search), rather
than having this being equivalent to "cd -")
Because of these changes, the pwd command (and $PWD) essentially
always acts as pwd -P, even when called as pwd -L (which is still
the default.) That is, even more than it did before.
Also fixed a (kind of minor) mem management error (CDPATH related)
"whosoever shall padvance must stunalloc before repeating" (and the
same for MAILPATH).
--
If we are going to keep the MAILPATH % hack, then at least do something
rational. Since it isn't documented, what "rational" is is up for
discussion, but what it did before was not it (it was nonsense...).
 1.18.6.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.18.6.1 21-Apr-2020  martin Sync with HEAD
 1.10 07-Aug-2003  agc branches: 1.10.98;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.9 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.8 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.7 21-Mar-1995  cgd convert to new RCS id conventions.
 1.6 11-Jun-1994  mycroft Add RCS ids.
 1.5 11-May-1994  jtc sync with 4.4lite
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.10.98.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.10.98.1 21-Apr-2020  martin Sync with HEAD
 1.90 07-Apr-2023  kre The great shell trailing whitespace cleanup of 2023...
Inspired by private e-mail comments from mouse@

NFCI.
 1.89 18-Sep-2022  kre Add the -l option (aka -o login): be a login shell. Meaningful only on
the command line (with both - and + forms) - overrides the presence (or
otherwise) of a '-' as argv[0][0].

Since this allows any shell to be a login shell (which simply means that
it runs /etc/profile and ~/.profile at shell startup - there are no other
side effects) add a new, always set at startup, variable NBSH_INVOCATION
which has a char string as its value, where each char has a meaning,
more or less related to how the shell was started. See sh(1).
This is intended to allow those startup scripts to tailor their behaviour
to the nature of this particular login shell (it is possible to detect
whether a shell is a login shell merely because of -l, or whether it would
have been anyway, before the -l option was added - and more). The
var could also be used to set different values for $ENV for different
uses of the shell.
 1.88 26-Oct-2021  kre Use a type-correct end marker for strstrcat() rather than NULL, as
for a function with unknown number & types of args, the compiler isn't
able to automatically convert to the correct type. Issue pointed out
in off list e-mail by Rolland Illig ... Thanks.

The first arg (pointer to where to put length of result) is of a known
type, so doesn't have the same issue - we can keep using NULL for that
one when the length isn't needed.

Also, make sure to return a correctly null terminated null string in
the (absurd) case that there are no non-null args to strstrcat() (though
there are much better ways to generate "" on the stack). Since there is
currently just one call in the code, and it has real string args, this
isn't an issue for now, but who knows, some day.

NFCI - if there is any real change, then it is a change that is required.

XXX pullup -9 (together with the previous changes)
 1.87 26-Oct-2021  kre PR bin/56464

After almost 30 years, finally do the right thing and read $HOME/.profile
rather than .profile in the initial directory (it was that way in version
1.1 ...) All other ash descendants seem to have fixed this long ago.

While here, copy a feature from FreeBSD which allows "set +p" (if a
shell run by a setuid process with the -p flag is privileged) to reset
the privileges. Once done (the set +p) it cannot be undone (a later
set -p sets the 'p' flag, but that's all it does) - that just becomes a
one bit storage location.

We do this, as (also copying from FreeBSD, and because it is the right
thing to do) we don't run .profile in a privileged shell - FreeBSD run
/etc/suid_profile in that case (not a good name, it also applies to setgid
shells) but I see no real need for that, we run /etc/profile in any case,
anything that would go in /etc/suid_profile can just go in /etc/profile
instead (with suitable guards so the commands only run in priv'd shells).

One or two minor DEBUG mode changes (notably having priv'd shells identify
themselves in the DEBUG trace) and sh.1 changes with doc of the "set +p"
change, the effect that has on $PSc and a few other wording tweaks.

XXX pullup -9 (not -8, this isn't worth it for the short lifetime
that has left - if it took 28+ years for anyone to notice this, it
cannot be having all that much effect).
 1.86 15-Sep-2021  kre Improve the solution for the 2nd access to a fd which shouldn't
be available ("13") issue reported by Jan Schaumann on netbsd-users.

This fixes a bug in the earlier fix (a day or so ago) which could allow the
shell's idea of which fd range was in use by the script to get wildly
incorrect, but now also actually looks to see which fd's are in use as
renamed other user fd's during the lifetime of a redirection which needs
to be able to be undone (most redirections occur after a fork and are
permanent in the child process). Attempting to access such a fd (as with
attempts to access a fd in use by the shell for its own purposes) is treated
as an attempt to access a closed fd (EBADF). Attempting to reuse the fd
for some other purpose (which should be rare, even for scripts attempting
to cause problems, since the shell generally knows which fds the script
wants to use, and avoids them) will cause the renamed (renumbered) fd
to be renamed again (moved aside to some other available fd), just as
happens with the shell's private fds.

Also, when a generic fd is required, don't give up because of EMFILE
or similar unless there are no available fds at all (we might prefer >10
or bigger, but if there are none there, use anything). This avoids
redirection errors when ulimit -n has been set small, and all the fds >10
that are available have been used, but we need somewhere to park the old
user of a fd while we reuse that fd for the redirection.
 1.85 07-Feb-2020  fox bin/sh: Fixes -Werror=shadow causing build breaks.

Conflicting variable name, sigset_t sigs has been renamed to sigset_t mask

Reviewed by: kamil@
 1.84 06-Feb-2020  kre Actually, the issue with bash (in previous) is more likely that the
SIGCHLD is blocked rather than ignored. We want neither. Make sure
SIGCHLD is unblocked as well as SIG_DFL.

XXX pullup -9
 1.83 06-Feb-2020  kre If we are invoked with SIGCHLD ignored, we fail badly, as we assume
that we can always wait(2) for our children, and an ignored SIGCHLD
prevents that. Recent versions of bash can be convinced (due to a
bug most likely) to invoke us that way. Always return SIGCHLD to
SIG_DFL during init - we already prevent scripts from fiddling it.

All ash derived shells apparently have this problem (observed by
Martijn Dekker, and notified on the bash-bug list). Actual issue
diagnosed by Harald van Dijk (same list).
 1.82 09-Feb-2019  kre branches: 1.82.2;

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.
 1.81 04-Feb-2019  kre 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).
 1.80 19-Jan-2019  kre 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.
 1.79 11-Dec-2018  kre 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).
 1.78 03-Dec-2018  kre 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).
 1.77 03-Dec-2018  kre 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
 1.76 22-Aug-2018  kre 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.
 1.75 19-Aug-2018  kre 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)
 1.74 19-Aug-2018  kre 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
 1.73 23-Jan-2018  sevan branches: 1.73.2; 1.73.4;
drop main() prototype
 1.72 05-Jul-2017  kre DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
 1.71 19-Jun-2017  kre Correct the initial line number used for processing -c arg strings.

(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
 1.70 29-May-2017  kre branches: 1.70.2;

More DEBUG mode changes. As usual, read the source if you care.
 1.69 18-May-2017  kre NFC: added a minor comment (and enev then, in DEBUG code only)
 1.68 22-Apr-2017  kre branches: 1.68.2;

Keep track of the biggest fd used by, or available to, the user/script
and use that to control which fd's are examined by a (bare) fdflags
(with no fd args).

Usually this will mean that fdflags will no longer show the shell's
internal use fds, only user fds.

This is only a partial fix however, a user can easily discover the
shell's fd usage (eg: using fstat) and can then still use fdflags to
manipulate those fds (or even send output to them).

The shell needs to monitor its own fd usage better, and keep out of
the way of user fds - coming sometime later...
 1.67 09-May-2016  kre branches: 1.67.2;

PR bin/48875 - avoid holding (replaced) file descriptors open when running a
command in the current shell (so they can be restored for the next command)
in cases where it is obvious that there is not going to be a following
command to use them. This fixes the problem reported in the PR (though
there are still plenty of situations where a FD could be closed but isn't,
we do not do full fd flow eveluation to determine whether a fd will be
used or not).

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

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

OK christos@
 1.64 31-Mar-2016  christos Implement the NETBSD_SHELL readonly unexportable unimportable
variable (with its current value set at 20160401) as discussed on
current-users and tech-userlevel. This also includes the necessary
support to implement it properly (particularly the unexportable
part) and adds options to the export command to support unexportable
variables. Also implement the "posix" option (no single letter
equivalent) which gets its default value from whether or not
POSIXLY_CORRECT is set in the environment when the shell starts
(but can be changed just like any other option using -o and +o on
the command line, or the set builtin command.) While there, fix
all uses of options so it is possible to have options that have a
short (one char) name, and no long name, just as it has been possible
to have options with a long name and no short name, though there
are currently none (with no long name). For now, the only use of
the posix option is to control whether ${ENV} is read at startup
by a non-interactive shell, so changing it with set is not usful
- that might change in the future. (from kre@)
 1.63 27-Mar-2016  christos General KNF and source code cleanups, avoid scattering the
magic string " \t\n" all over the place, slightly improved
syntax error messages, restructured some of the code for
clarity, don't allow IFS to be imported through the environment,
and remove the (never) conditionally compiled ATTY option.
Apart from one or two syntax error messages, and ignoring IFS
if present in the environment, this is intended to have no
user visible changes. (from kre@)
 1.62 01-Mar-2016  martin Fix wrong condition in previous causing bogus 'Use "exit" to leave
shell.' messages (from kre)
 1.61 29-Feb-2016  christos Complete implementation of the noexec option (-n) including
disabling noexec, if the shell is interactive, each time that
a new command is about to be read. Also correct the -I
(ignoreeof) option so that it only applies to interactive shells,
as required by posix. (from kre)
 1.60 27-Feb-2016  christos Improve debugging, from kre (I hooked it to the build).
 1.59 26-May-2015  christos Drop privileges when executed set{u,g}id unless -p is specified like other
shells do to avoid system() and popen() abuse.
 1.58 31-May-2014  christos PR/48843: Jarmo Jaakkola: dot commands mess up scope nesting tracking

Evaluation of commands goes completely haywire if a file containing
a break/continue/return command outside its "intended" scope is sourced
using a dot command inside its "intended" scope. The main symptom is
not exiting from the sourced file when supposed to, leading to evaluation
of commands that were not supposed to be evaluated. A secondary symptom
is that these extra commands are not evaluated correctly, as some of them
are skipped. Some examples are listed in the How-To-Repeat section.

According to the POSIX standard, this is how it should work:
dot:
The shell shall execute commands from the file in the current
environment.
break:
The break utility shall exit from the smallest enclosing for, while,
or until loop, [...]
continue:
The continue utility shall return to the top of the smallest
enclosing for, while, or until loop, [...]
return:
The return utility shall cause the shell to stop executing
the current function or dot script. If the shell is not currently
executing a function or dot script, the results are unspecified.

It is clear that return should return from a sourced file, which
it does not do. Whether break and continue should work from the sourced
file might be debatable. Because the dot command says "in the current
environment", I'd say yes. In any case, it should not fail in weird
ways like it does now!

The problems occur with return (a) and break/continue (b) because:
1) dotcmd() does not record the function nesting level prior to
sourcing the file nor does it touch the loopnest variable,
leading to either
2 a) returncmd() being unable to detect that it should not set
evalskip to SKIPFUNC but SKIPFILE, or
b) breakcmd() setting evalskip to SKIPCONT or SKIPBREAK,
leading to
3) cmdloop() not detecting that it should skip the rest of
the file, due to only checking for SKIPFILE.
The result is that cmdloop() keeps executing lines from the file
whilst evalskip is set, which is the main symptom. Because
evalskip is checked in multiple places in eval.c, the secondary
symptom appears.
>How-To-Repeat:
Run the following script:

printf "break\necho break1; echo break2" >break
printf "continue\necho continue1; echo continue2" >continue
printf "return\necho return1; echo return2" >return

while true; do . ./break; done

for i in 1 2; do . ./continue; done

func() {
. ./return
}
func

No output should be produced, but instead this is the result:
break1
continue1
continue1
return1

The main symptom is evident from the unexpected output and the secondary
one from the fact that there are no lines with '2' in them.
>Fix:
Here is patch to src/bin/sh to fix the above problems. It keeps
track of the function nesting level at the beginning of a dot command
to enable the return command to work properly.

I also changed the undefined-by-standard functionality of the return
command when it's not in a dot command or function from (indirectly)
exiting the shell to being silently ignored. This was done because
the previous way has at least one bug: the shell exits without asking
for confirmation when there are stopped jobs.

Because I read the standard to mean that break and continue should have
an effect outside the sourced file, that's how I implemented it. For what
it's worth, this also seems to be what bash does. Also laziness, because
this way required no changes to loopnesting tracking. If this is not
wanted, it might make sense to move the nesting tracking to the inputfile
stack.

The patch also does some clean-up to reduce the amount of global
variables by moving the dotcmd() and the find_dot_file() functions from
main.c to eval.c and making in_function() a proper function.
 1.57 18-Jun-2011  christos branches: 1.57.8; 1.57.20;
PR/45069: Henning Petersen: Use prototypes from builtins.h .
 1.56 21-Feb-2010  christos branches: 1.56.4;
fix faulty logic in previous change.
 1.55 20-Feb-2010  christos default to the original behavior for $ENV unless POSIXLY_CORRECT is set.
 1.54 17-Feb-2010  christos PR/42828: Richard Hansen: Don't evaluate ${ENV} if not interactive.
http://www.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html#tag_18_05_03
 1.53 18-Jan-2009  lukem fix -Wsign-compare issues
 1.52 16-Oct-2008  dholland Remove unused global variables
 1.51 20-Jul-2008  lukem Remove the \n and tabs from the __COPYRIGHT() strings.
 1.50 10-Oct-2005  christos branches: 1.50.20;
PR/31548: Alexander Rigbo: $PWD not set when shell starts up, before pwd
or cd get executed.
 1.49 15-Jul-2005  christos Only do certain actions when we are trully interactive, not just when we
are connected to a tty:
1. enable editor
2. print directory on directory change.
3. print job messages
 1.48 14-Sep-2003  jmmv Fix crash when DEBUG=2, by opening the log file ASAP. Reviewed by simonb@.
 1.47 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.46 11-Dec-2002  christos PR/19453: Christopher Richards: Call setlocale() in /bin/sh
 1.45 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.44 28-Sep-2002  christos Revert previous change. No need to save rootshell. It is only affecting
the non-vfork case. Having said that, it would be nice if pipelines of
simple commands were vforked too. Right now they are not.
Explain that setpgid() might fail because we are doing it both in the
parent and the child case, because we don't know which one will come
first.
Suspending a pipeline prints %1 Suspended n times where n is the number
of processes, but that was there before. It is easy to fix, but I'll
leave the code alone for now.
 1.43 27-Sep-2002  christos Deal with rootshell not being maintained correctly in the vfork() case.
Propagate isroot, throughout the eval process and maintain it properly.
Fixes sleep 10 | cat^C not exiting because sleep and cat ended up in
their own process groups, because wasroot was always true in the children.
 1.42 27-Sep-2002  christos VFork()ing shell: From elric@netbsd.org:
Plus my changes:
- walking process group fix in foregrounding a job.
- reset of process group in parent shell if interrupted before the wait.
- move INTON lower in the dowait so that the job structure is
consistent.
- error check all setpgid(), tcsetpgrp() calls.
- eliminate unneeded strpgid() call.
- check that we don't belong in the process group before we try to
set it.
 1.41 11-Jun-2001  wiz branches: 1.41.2;
Fix various misspellings of compatible/compatibility.
 1.40 04-Feb-2001  christos remove redundant declarations and nexted externs.
 1.39 01-Nov-2000  christos PR/11283: Hubert Feyrer: random memory corruption executing commands:
Fix from FreeBSD:

growstackblock() sometimes relocates a stack_block considered empty
without properly relocating stack marks referencing that block.
The first call to popstackmark() with the unrelocated stack mark
as argument then causes sh to abort.

Relocating the relevant stack marks seems to solve this problem.

The patch changes the semantics of popstackmark() somewhat. It can
only be called once after a call to setstackmark(), thus cmdloop() in
main.c needs an extra call to setstackmark().
 1.38 22-May-2000  elric branches: 1.38.4;
Back out previous vfork changes.
 1.37 13-May-2000  elric Now we use vfork(2) instead of fork(2) when we can.
 1.36 14-Apr-2000  simonb Include <errno.h> instead of using "extern int errno;".
 1.35 27-Jan-2000  christos Fix bin/9184, bin/9194, bin/9265, bin/9266
Exitcode and negation problems (From Martin Husemann)
 1.34 09-Jul-1999  christos compile with WARNS = 2
 1.33 27-Mar-1999  christos When we execute commands from a shell script, make sure that the signals
are being caught (reported by Alexis Rosen), similar to the -c case.

#!/bin/sh
vi "$@"

^C when the script is running...
 1.32 05-Feb-1999  christos Fix the -c problem differently. We cannot just ignore SIGINT etc, otherwise
we cannot interrupt sh -c <command>
 1.31 04-Feb-1999  christos PR/1788: Shinya Taniguchi: sh -c does not setup signals properly.
[Reminded to me by Alexis Rosen -- thx!]
 1.30 04-Feb-1999  cjs Add -q option, which when used with -v and/or -x, turns off the tracing
during the execution of /etc/profile, .profile and $ENV.
 1.29 02-Feb-1999  itohy The builtin . (dot) command no longer sees the current working directory
even if the specified file is not in the PATH.
This change enforces security and makes it conform to POSIX.

Closes PR #6794.
I thought Christos committed this but not appeared yet. :)

The setstackmark()/popstackmark() pair in dotcmd(), used for freeing
stack storage possibly allocated by find_dot_file(), is redundant for now
since dotcmd() is surrounded by another pair in evalcommand().
This redundancy, however, may help future modifications
(suggested by Christos).
 1.28 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.27 28-Jul-1998  mycroft Delint.
 1.26 04-Jul-1997  christos Fix compiler warnings.
 1.25 11-Apr-1997  christos Don't call getpwd() because if that fails the shell cannot start. (from scott)
 1.24 16-Oct-1996  christos PR/287: Shell does not exit with 126/127 when permission denied/
command not found. Add extra exception type and generalize
error handling routines to take that exception type. Use
a global variable exerrno to keep the last exec error.
 1.23 19-Nov-1995  christos branches: 1.23.4;
Fix PR/1760, where 'cd -' before any other command could cause a reference
to an uninitialized pointer. Use getcwd() to get the current working directory,
instead of forking /bin/pwd [per Scott's suggestion]
 1.22 11-Sep-1995  christos Fix return builtin to work like it does in ksh:
When not in a function, it skips the rest of the current input file.
Instances of `return' outside function definitions were previously ignored.
What does joe posix have to say about this?
[fixes PR/1444]
 1.21 20-Jul-1995  christos Fix bin/1240, from Dennis Ferguson. the exit builtin with no arguments
should return the exitcode of the previous command but it always returned 0.
 1.20 28-May-1995  christos Fixed bug where
$ cat foo
echo $FOO

$ FOO=1 . ./foo
$ echo $FOO

Did not echo 1 at all. Now variable assignments before sourced scripts
are honored during and after the execution of the script.
 1.19 19-May-1995  christos Changed so that syntax errors (EXERROR) set the exit status to 2,
and commands that are not found set the exit status to 1 like all
other bourne shells.
[It used to be 0 and 2 respectively]
 1.18 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.17 21-Mar-1995  cgd convert to new RCS id conventions.
 1.16 05-Dec-1994  cgd clean up further. more patches from Jim Jegers
 1.15 04-Dec-1994  cgd from James Jegers <jimj@miller.cs.uwm.edu>: quiet -Wall, and squelch
some of the worst style errors.
 1.14 03-Aug-1994  jtc Don't source the file specified by $ENV if either the real and effective
user IDs or the real and effective group IDs of the process are different.
Thanks to Mark Weaver who pointed this out in PR 262.
 1.13 11-Jun-1994  mycroft branches: 1.13.2;
Add RCS ids.
 1.12 12-May-1994  jtc Include appropriate header files to bring function prototypes into scope.
 1.11 11-May-1994  jtc forgot some include files
 1.10 11-May-1994  jtc integrate NetBSD's path searching for dot files
 1.9 11-May-1994  jtc sync with 4.4lite
 1.8 26-Jan-1994  jtc Removed theo's change to check S_ISLNK() for dot commands. This is
unnecessary, since stat() allready resolves symlinks (and fails, if
the link is broken).
 1.7 25-Jan-1994  deraadt source/$PATH thing for symbolic links too, reportedly
 1.6 25-Jan-1994  deraadt but don't try that for absolute or relative paths
 1.5 25-Jan-1994  deraadt make . (source) command search $PATH as it should. from dkionka@Cadence.COM (Daniel P. Kionka) and fixed by me
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.13.2.2 18-Aug-1994  mycroft update from trunk
 1.13.2.1 03-Aug-1994  cgd from trunk.
 1.23.4.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.38.4.1 03-Nov-2000  tv Pullup 1.39 [hubertf]:
Fixes PR 11283.
 1.41.2.1 27-Mar-2002  elric Doing the vfork work on ash on a branch to try to shake out the
problems before I expose everyone to them. This checkin represents
a merge of the prior work, which I backed out a while ago, to the
HEAD only and does not incorporate any additional bugfixes. The
additional bugfixes and code-cleanup will occur in later checkins.

For reference the patches that were used are:
cvs diff -kk -r1.51 -r1.55 eval.c | patch
cvs diff -kk -r1.27 -r1.28 exec.c | patch
cvs diff -kk -r1.15 -r1.16 exec.h | patch
cvs diff -kk -r1.32 -r1.33 input.c | patch
cvs diff -kk -r1.10 -r1.11 input.h | patch
cvs diff -kk -r1.32 -r1.35 jobs.c | patch
cvs diff -kk -r1.9 -r1.11 jobs.h | patch
cvs diff -kk -r1.36 -r1.37 main.c | patch
cvs diff -kk -r1.20 -r1.21 redir.c | patch
cvs diff -kk -r1.10 -r1.11 redir.h | patch
cvs diff -kk -r1.10 -r1.12 shell.h | patch
cvs diff -kk -r1.22 -r1.23 trap.c | patch
cvs diff -kk -r1.12 -r1.13 trap.h | patch
cvs diff -kk -r1.23 -r1.24 var.c | patch
cvs diff -kk -r1.16 -r1.17 var.h | patch

All other changes were simply the resolution of the resulting
conflicts, which occured only in the merge of jobs.c.

Begins to address PR: bin/5475
 1.50.20.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.56.4.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.57.20.1 10-Aug-2014  tls Rebase.
 1.57.8.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.67.2.1 26-Apr-2017  pgoyette Sync with HEAD
 1.68.2.1 19-May-2017  pgoyette Resolve conflicts from previous merge (all resulting from $NetBSD
keywork expansion)
 1.70.2.2 25-Aug-2018  martin Pull up following revision(s) (requested by kre in ticket #983):

bin/sh/eval.c: revision 1.158
bin/sh/eval.h: revision 1.21
bin/sh/main.c: revision 1.74

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
 1.70.2.1 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.73.4.4 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.73.4.3 21-Apr-2020  martin Sync with HEAD
 1.73.4.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.73.4.1 10-Jun-2019  christos Sync with HEAD
 1.73.2.3 26-Jan-2019  pgoyette Sync with HEAD
 1.73.2.2 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.73.2.1 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.82.2.2 06-Nov-2021  martin Pull up following revision(s) (requested by kre in ticket #1371):

bin/sh/main.c: revision 1.87
bin/sh/main.c: revision 1.88
bin/sh/memalloc.h: revision 1.20
bin/sh/sh.1: revision 1.235
bin/sh/memalloc.c: revision 1.34
bin/sh/memalloc.c: revision 1.35
bin/sh/memalloc.h: revision 1.19
bin/sh/shell.h: revision 1.31
bin/sh/options.c: revision 1.56

PR bin/56464

After almost 30 years, finally do the right thing and read $HOME/.profile
rather than .profile in the initial directory (it was that way in version
1.1 ...) All other ash descendants seem to have fixed this long ago.
While here, copy a feature from FreeBSD which allows "set +p" (if a
shell run by a setuid process with the -p flag is privileged) to reset
the privileges. Once done (the set +p) it cannot be undone (a later
set -p sets the 'p' flag, but that's all it does) - that just becomes a
one bit storage location.

We do this, as (also copying from FreeBSD, and because it is the right
thing to do) we don't run .profile in a privileged shell - FreeBSD run
/etc/suid_profile in that case (not a good name, it also applies to setgid
shells) but I see no real need for that, we run /etc/profile in any case,
anything that would go in /etc/suid_profile can just go in /etc/profile
instead (with suitable guards so the commands only run in priv'd shells).

One or two minor DEBUG mode changes (notably having priv'd shells identify
themselves in the DEBUG trace) and sh.1 changes with doc of the "set +p"
change, the effect that has on $PSc and a few other wording tweaks.

XXX pullup -9 (not -8, this isn't worth it for the short lifetime
that has left - if it took 28+ years for anyone to notice this, it
cannot be having all that much effect).

Use a type-correct end marker for strstrcat() rather than NULL, as
for a function with unknown number & types of args, the compiler isn't
able to automatically convert to the correct type. Issue pointed out
in off list e-mail by Rolland Illig ... Thanks.

The first arg (pointer to where to put length of result) is of a known
type, so doesn't have the same issue - we can keep using NULL for that
one when the length isn't needed.
Also, make sure to return a correctly null terminated null string in
the (absurd) case that there are no non-null args to strstrcat() (though
there are much better ways to generate "" on the stack). Since there is
currently just one call in the code, and it has real string args, this
isn't an issue for now, but who knows, some day.

NFCI - if there is any real change, then it is a change that is required.

XXX pullup -9 (together with the previous changes)
 1.82.2.1 10-Feb-2020  martin Pull up following revision(s) (requested by kre in ticket #684):

bin/sh/jobs.c: revision 1.107
bin/sh/main.c: revision 1.83
bin/sh/main.c: revision 1.84
bin/sh/main.c: revision 1.85

If we are invoked with SIGCHLD ignored, we fail badly, as we assume
that we can always wait(2) for our children, and an ignored SIGCHLD
prevents that. Recent versions of bash can be convinced (due to a
bug most likely) to invoke us that way. Always return SIGCHLD to
SIG_DFL during init - we already prevent scripts from fiddling it.

All ash derived shells apparently have this problem (observed by
Martijn Dekker, and notified on the bash-bug list). Actual issue
diagnosed by Harald van Dijk (same list).

Actually, the issue with bash (in previous) is more likely that the
SIGCHLD is blocked rather than ignored. We want neither. Make sure
SIGCHLD is unblocked as well as SIG_DFL.

XXX pullup -9

bin/sh: Fixes -Werror=shadow causing build breaks.
Conflicting variable name, sigset_t sigs has been renamed to sigset_t mask

Reviewed by: kamil@

Avoid a core dump if a child process that is not one of our
children happens to exit while we are waiting for another child
to exit.

This can happen with code like
sh -c '
sleep 5 &
exec sh -c "sleep 10 & wait !$"
'

when the inner "sh" is waiting for the 10 second sleep to be
done, the 5 second sleep started earlier terminates. It is
a child of our process, as the inner shell is the same process
as the outer one, but not a known child (the inner shell has no
idea what the outer one did before it started).

This was observed in the wild by Martijn Dekker (where the outer
shell was bash but that's irrelevant).

XXX pullup -9
 1.12 03-Dec-2018  kre 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
 1.11 18-Jun-2011  christos branches: 1.11.42; 1.11.44;
PR/45069: Henning Petersen: Use prototypes from builtins.h .
 1.10 07-Aug-2003  agc branches: 1.10.52;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.9 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.8 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.7 21-Mar-1995  cgd convert to new RCS id conventions.
 1.6 11-Jun-1994  mycroft Add RCS ids.
 1.5 11-May-1994  jtc sync with 4.4lite
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.10.52.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.11.44.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.11.44.2 21-Apr-2020  martin Sync with HEAD
 1.11.44.1 10-Jun-2019  christos Sync with HEAD
 1.11.42.1 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.41 07-May-2025  kre Obliterate the global var herefd

For the past several years (since Nov 2021) the global var "herefd"
has led a rather meaningless existence.

It gets statically init'd to -1 (at compile time), and several
times its value is carefully saved, set to -1, then restored later.
(Lots of copying around of -1 going on there!)

The only use of its value was removed in the immediately previous update
to memalloc.c, where it was tested, and if its value were >= 0 ...
(no point mentioning what would happen, since its value is always -1,
since Nov 2021).

Bye bye herefd, it was nice knowing you. May we never meet again.
 1.40 06-May-2025  kre PR bin/59395 Remove some dead code.

Back in Nov 2021, the way here docs were implemented was changed
to fix problem report 53550 and this code can no longer ever
be executed (it is impossible without reintroducing the 53550
problems), so time for it to go.

This will make no functional difference to sh in HEAD,
and wouldn't in -10, if this were worth pulling up there (it isn't).

However, in -9 this code still runs (in the relatively rare cases
that it applies), but is broken, leading to the problem in 59395.
Removing this code, which is just an optimisation, will correct
that problem (for current releases only -9 is affected).

XXX pullup -9 (will fix PR bin/59395)
 1.39 07-Apr-2023  kre branches: 1.39.2;

Remove an end of file trailing blank line that served no purpose.

NFCI
 1.38 07-Apr-2023  kre The great shell trailing whitespace cleanup of 2023...
Inspired by private e-mail comments from mouse@

NFCI.
 1.37 31-May-2022  andvar fix various typos in comments, documentation and messages.
 1.36 10-Apr-2022  andvar fix various typos in comments and output/log messages.
 1.35 26-Oct-2021  kre Use a type-correct end marker for strstrcat() rather than NULL, as
for a function with unknown number & types of args, the compiler isn't
able to automatically convert to the correct type. Issue pointed out
in off list e-mail by Rolland Illig ... Thanks.

The first arg (pointer to where to put length of result) is of a known
type, so doesn't have the same issue - we can keep using NULL for that
one when the length isn't needed.

Also, make sure to return a correctly null terminated null string in
the (absurd) case that there are no non-null args to strstrcat() (though
there are much better ways to generate "" on the stack). Since there is
currently just one call in the code, and it has real string args, this
isn't an issue for now, but who knows, some day.

NFCI - if there is any real change, then it is a change that is required.

XXX pullup -9 (together with the previous changes)
 1.34 26-Oct-2021  kre PR bin/56464

After almost 30 years, finally do the right thing and read $HOME/.profile
rather than .profile in the initial directory (it was that way in version
1.1 ...) All other ash descendants seem to have fixed this long ago.

While here, copy a feature from FreeBSD which allows "set +p" (if a
shell run by a setuid process with the -p flag is privileged) to reset
the privileges. Once done (the set +p) it cannot be undone (a later
set -p sets the 'p' flag, but that's all it does) - that just becomes a
one bit storage location.

We do this, as (also copying from FreeBSD, and because it is the right
thing to do) we don't run .profile in a privileged shell - FreeBSD run
/etc/suid_profile in that case (not a good name, it also applies to setgid
shells) but I see no real need for that, we run /etc/profile in any case,
anything that would go in /etc/suid_profile can just go in /etc/profile
instead (with suitable guards so the commands only run in priv'd shells).

One or two minor DEBUG mode changes (notably having priv'd shells identify
themselves in the DEBUG trace) and sh.1 changes with doc of the "set +p"
change, the effect that has on $PSc and a few other wording tweaks.

XXX pullup -9 (not -8, this isn't worth it for the short lifetime
that has left - if it took 28+ years for anyone to notice this, it
cannot be having all that much effect).
 1.33 09-Feb-2019  kre branches: 1.33.2;

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.
 1.32 22-Aug-2018  kre 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.
 1.31 22-Jul-2018  kre Minor cleanups to growstackblock(). This should really change
nothing that matters, but might be slightly more robust/complete.
 1.30 17-Jun-2017  kre branches: 1.30.4; 1.30.6;
Many internal memory management type fixes.

PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)

echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)

(Recently added) Problems with ~ expansion fixed (mem management related).

Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)

And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.

Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,

More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.

[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]

More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).

User visible changes:

Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in

cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END

(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)

With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)

POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).

Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
 1.29 15-Feb-2008  matt branches: 1.29.62;
Fix inconsistent definitions
 1.28 07-Aug-2003  agc branches: 1.28.22;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.27 22-Jan-2003  dsl Support command -p, -v and -V as posix
Stop temporary PATH assigments messing up hash table
Fix sh -c -e "echo $0 $*" -a x (as posix)
(agreed by christos)
 1.26 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.25 07-Oct-2002  christos - it is wrong to put inton/intoff arount ckmalloc(), because the code
around it is the one that does this.
- whitespace fixes.
 1.24 04-Oct-2002  christos Rename ALIGN to SHELL_ALIGN and simplify macro so that it does not have side
effects, and add double to it, so that it aligns doubles correctly too. This
is just a workaround to fix the sparc64 problem where ALIGN() is now defined
in some include file to be 16 instead of 8. Thanks to martin for debugging this.
 1.23 01-Nov-2000  christos PR/11283: Hubert Feyrer: random memory corruption executing commands:
Fix from FreeBSD:

growstackblock() sometimes relocates a stack_block considered empty
without properly relocating stack marks referencing that block.
The first call to popstackmark() with the unrelocated stack mark
as argument then causes sh to abort.

Relocating the relevant stack marks seems to solve this problem.

The patch changes the semantics of popstackmark() somewhat. It can
only be called once after a call to setstackmark(), thus cmdloop() in
main.c needs an extra call to setstackmark().
 1.22 21-Oct-2000  mycroft Wrap malloc() calls with an INTOFF/INTON pair. Fixes PR 8414.
 1.21 31-Jan-1998  christos branches: 1.21.10;
Include our local includes after the system's ones.
 1.20 04-Jul-1997  christos branches: 1.20.2;
Fix compiler warnings.
 1.19 11-Jan-1997  tls kill 'register'
 1.18 16-Oct-1996  christos PR/2808: Remove trailing whitespace (from FreeBSD)
 1.17 20-May-1996  cgd branches: 1.17.2;
Fix growstackblock() 'newlen' calculations:
(1) it needs to be ALIGNed for both halves of the 'if,' and
(2) if you're going to claim that you now have ALIGN(newlen)
bytes left, you should have actually allocated
ALIGN(newlen), rather than just 'newlen' bytes.
 1.16 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.15 21-Mar-1995  cgd convert to new RCS id conventions.
 1.14 31-Dec-1994  mycroft Fix that last bug in a less expensive way.
 1.13 31-Dec-1994  cgd take two: make grabstackstr() work correctly, in the face of strange filling.
 1.12 30-Dec-1994  mycroft Remove previous misguided change.
 1.11 23-Dec-1994  cgd don't play fast and loose with memory block allcation. try to allocate
in more-round sizes, but realize that not everybody will fill them up
exactly.
 1.10 04-Dec-1994  cgd from James Jegers <jimj@miller.cs.uwm.edu>: quiet -Wall, and squelch
some of the worst style errors.
 1.9 23-Sep-1994  mycroft Eliminate uses of some obsolete functions.
 1.8 11-Jun-1994  mycroft Add RCS ids.
 1.7 12-May-1994  jtc use prototypes provided by header files instead of our own
 1.6 12-May-1994  jtc Include appropriate header files to bring function prototypes into scope.
 1.5 11-May-1994  jtc sync with 4.4lite
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.17.2.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.20.2.1 08-May-1998  mycroft Sync with trunk, per request of christos.
 1.21.10.2 03-Nov-2000  tv Pullup 1.23 [hubertf]:
Fixes PR 11283.
 1.21.10.1 22-Oct-2000  tv Pullup 1.22 [he]:
Wrap malloc() calls with an INTOFF/INTON pair. Fixes PR 8414.
 1.28.22.1 23-Mar-2008  matt sync with HEAD
 1.29.62.1 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.30.6.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.30.6.2 21-Apr-2020  martin Sync with HEAD
 1.30.6.1 10-Jun-2019  christos Sync with HEAD
 1.30.4.2 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.30.4.1 28-Jul-2018  pgoyette Sync with HEAD
 1.33.2.2 09-May-2025  martin Pull up following revision(s) (requested by kre in ticket #1948):

bin/sh/memalloc.c: revision 1.40

PR bin/59395 Remove some dead code.

Back in Nov 2021, the way here docs were implemented was changed
to fix problem report 53550 and this code can no longer ever
be executed (it is impossible without reintroducing the 53550
problems), so time for it to go.

This will make no functional difference to sh in HEAD,
and wouldn't in -10, if this were worth pulling up there (it isn't).
However, in -9 this code still runs (in the relatively rare cases
that it applies), but is broken, leading to the problem in 59395.

Removing this code, which is just an optimisation, will correct
that problem (for current releases only -9 is affected).
PR bin/59395
 1.33.2.1 06-Nov-2021  martin Pull up following revision(s) (requested by kre in ticket #1371):

bin/sh/main.c: revision 1.87
bin/sh/main.c: revision 1.88
bin/sh/memalloc.h: revision 1.20
bin/sh/sh.1: revision 1.235
bin/sh/memalloc.c: revision 1.34
bin/sh/memalloc.c: revision 1.35
bin/sh/memalloc.h: revision 1.19
bin/sh/shell.h: revision 1.31
bin/sh/options.c: revision 1.56

PR bin/56464

After almost 30 years, finally do the right thing and read $HOME/.profile
rather than .profile in the initial directory (it was that way in version
1.1 ...) All other ash descendants seem to have fixed this long ago.
While here, copy a feature from FreeBSD which allows "set +p" (if a
shell run by a setuid process with the -p flag is privileged) to reset
the privileges. Once done (the set +p) it cannot be undone (a later
set -p sets the 'p' flag, but that's all it does) - that just becomes a
one bit storage location.

We do this, as (also copying from FreeBSD, and because it is the right
thing to do) we don't run .profile in a privileged shell - FreeBSD run
/etc/suid_profile in that case (not a good name, it also applies to setgid
shells) but I see no real need for that, we run /etc/profile in any case,
anything that would go in /etc/suid_profile can just go in /etc/profile
instead (with suitable guards so the commands only run in priv'd shells).

One or two minor DEBUG mode changes (notably having priv'd shells identify
themselves in the DEBUG trace) and sh.1 changes with doc of the "set +p"
change, the effect that has on $PSc and a few other wording tweaks.

XXX pullup -9 (not -8, this isn't worth it for the short lifetime
that has left - if it took 28+ years for anyone to notice this, it
cannot be having all that much effect).

Use a type-correct end marker for strstrcat() rather than NULL, as
for a function with unknown number & types of args, the compiler isn't
able to automatically convert to the correct type. Issue pointed out
in off list e-mail by Rolland Illig ... Thanks.

The first arg (pointer to where to put length of result) is of a known
type, so doesn't have the same issue - we can keep using NULL for that
one when the length isn't needed.
Also, make sure to return a correctly null terminated null string in
the (absurd) case that there are no non-null args to strstrcat() (though
there are much better ways to generate "" on the stack). Since there is
currently just one call in the code, and it has real string args, this
isn't an issue for now, but who knows, some day.

NFCI - if there is any real change, then it is a change that is required.

XXX pullup -9 (together with the previous changes)
 1.39.2.1 02-Aug-2025  perseant Sync with HEAD
 1.21 07-May-2025  kre Obliterate the global var herefd

For the past several years (since Nov 2021) the global var "herefd"
has led a rather meaningless existence.

It gets statically init'd to -1 (at compile time), and several
times its value is carefully saved, set to -1, then restored later.
(Lots of copying around of -1 going on there!)

The only use of its value was removed in the immediately previous update
to memalloc.c, where it was tested, and if its value were >= 0 ...
(no point mentioning what would happen, since its value is always -1,
since Nov 2021).

Bye bye herefd, it was nice knowing you. May we never meet again.
 1.20 26-Oct-2021  kre branches: 1.20.4;
Use a type-correct end marker for strstrcat() rather than NULL, as
for a function with unknown number & types of args, the compiler isn't
able to automatically convert to the correct type. Issue pointed out
in off list e-mail by Rolland Illig ... Thanks.

The first arg (pointer to where to put length of result) is of a known
type, so doesn't have the same issue - we can keep using NULL for that
one when the length isn't needed.

Also, make sure to return a correctly null terminated null string in
the (absurd) case that there are no non-null args to strstrcat() (though
there are much better ways to generate "" on the stack). Since there is
currently just one call in the code, and it has real string args, this
isn't an issue for now, but who knows, some day.

NFCI - if there is any real change, then it is a change that is required.

XXX pullup -9 (together with the previous changes)
 1.19 26-Oct-2021  kre PR bin/56464

After almost 30 years, finally do the right thing and read $HOME/.profile
rather than .profile in the initial directory (it was that way in version
1.1 ...) All other ash descendants seem to have fixed this long ago.

While here, copy a feature from FreeBSD which allows "set +p" (if a
shell run by a setuid process with the -p flag is privileged) to reset
the privileges. Once done (the set +p) it cannot be undone (a later
set -p sets the 'p' flag, but that's all it does) - that just becomes a
one bit storage location.

We do this, as (also copying from FreeBSD, and because it is the right
thing to do) we don't run .profile in a privileged shell - FreeBSD run
/etc/suid_profile in that case (not a good name, it also applies to setgid
shells) but I see no real need for that, we run /etc/profile in any case,
anything that would go in /etc/suid_profile can just go in /etc/profile
instead (with suitable guards so the commands only run in priv'd shells).

One or two minor DEBUG mode changes (notably having priv'd shells identify
themselves in the DEBUG trace) and sh.1 changes with doc of the "set +p"
change, the effect that has on $PSc and a few other wording tweaks.

XXX pullup -9 (not -8, this isn't worth it for the short lifetime
that has left - if it took 28+ years for anyone to notice this, it
cannot be having all that much effect).
 1.18 22-Aug-2018  kre branches: 1.18.2;
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.
 1.17 17-Jun-2017  kre branches: 1.17.4; 1.17.6;
Many internal memory management type fixes.

PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)

echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)

(Recently added) Problems with ~ expansion fixed (mem management related).

Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)

And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.

Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,

More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.

[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]

More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).

User visible changes:

Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in

cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END

(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)

With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)

POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).

Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
 1.16 03-Jun-2017  kre branches: 1.16.2;
Fixes to shell expand (that is, $ stuff) from FreeBSD (implemented
differently...)

In particular ${01} is now $1 not $0 (for ${0any-digits})

${4294967297} is most probably now ""
(unless you have a very large number of params)
it is no longer an alias for $1 (4294967297 & 0xFFFFFFFF) == 1

$(( expr $(( more )) stuff )) is no longer the same as
$(( expr (( more )) stuff )) which was sometimes OK, as in:
$(( 3 + $(( 2 - 1 )) * 3 ))
but not always as in:
$(( 1$((1 + 1))1 ))
which should be 121, but was an arith syntax error as
1((1 + 1))1
is meaningless.

Probably some more. This also sprinkles a little const, splits a big
func that had 2 (kind of unrelated) purposes into two simpler ones,
and avoids some (semi-dubious) modifications (and restores) in the input
string to insert \0's when they were needed.
 1.15 15-Feb-2008  matt Fix inconsistent definitions
 1.14 07-Aug-2003  agc branches: 1.14.22;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.13 22-Jan-2003  dsl Support command -p, -v and -V as posix
Stop temporary PATH assigments messing up hash table
Fix sh -c -e "echo $0 $*" -a x (as posix)
(agreed by christos)
 1.12 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.11 01-Nov-2000  christos PR/11283: Hubert Feyrer: random memory corruption executing commands:
Fix from FreeBSD:

growstackblock() sometimes relocates a stack_block considered empty
without properly relocating stack marks referencing that block.
The first call to popstackmark() with the unrelocated stack mark
as argument then causes sh to abort.

Relocating the relevant stack marks seems to solve this problem.

The patch changes the semantics of popstackmark() somewhat. It can
only be called once after a call to setstackmark(), thus cmdloop() in
main.c needs an extra call to setstackmark().
 1.10 11-May-1995  christos branches: 1.10.16;
Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.9 21-Mar-1995  cgd convert to new RCS id conventions.
 1.8 31-Dec-1994  mycroft Fix that last bug in a less expensive way.
 1.7 31-Dec-1994  cgd take two: make grabstackstr() work correctly, in the face of strange filling.
 1.6 11-Jun-1994  mycroft Add RCS ids.
 1.5 11-May-1994  jtc sync with 4.4lite
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.10.16.1 03-Nov-2000  tv Pullup 1.11 [hubertf]:
Fixes PR 11283.
 1.14.22.1 23-Mar-2008  matt sync with HEAD
 1.16.2.1 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.17.6.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.17.6.2 21-Apr-2020  martin Sync with HEAD
 1.17.6.1 10-Jun-2019  christos Sync with HEAD
 1.17.4.1 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.18.2.1 06-Nov-2021  martin Pull up following revision(s) (requested by kre in ticket #1371):

bin/sh/main.c: revision 1.87
bin/sh/main.c: revision 1.88
bin/sh/memalloc.h: revision 1.20
bin/sh/sh.1: revision 1.235
bin/sh/memalloc.c: revision 1.34
bin/sh/memalloc.c: revision 1.35
bin/sh/memalloc.h: revision 1.19
bin/sh/shell.h: revision 1.31
bin/sh/options.c: revision 1.56

PR bin/56464

After almost 30 years, finally do the right thing and read $HOME/.profile
rather than .profile in the initial directory (it was that way in version
1.1 ...) All other ash descendants seem to have fixed this long ago.
While here, copy a feature from FreeBSD which allows "set +p" (if a
shell run by a setuid process with the -p flag is privileged) to reset
the privileges. Once done (the set +p) it cannot be undone (a later
set -p sets the 'p' flag, but that's all it does) - that just becomes a
one bit storage location.

We do this, as (also copying from FreeBSD, and because it is the right
thing to do) we don't run .profile in a privileged shell - FreeBSD run
/etc/suid_profile in that case (not a good name, it also applies to setgid
shells) but I see no real need for that, we run /etc/profile in any case,
anything that would go in /etc/suid_profile can just go in /etc/profile
instead (with suitable guards so the commands only run in priv'd shells).

One or two minor DEBUG mode changes (notably having priv'd shells identify
themselves in the DEBUG trace) and sh.1 changes with doc of the "set +p"
change, the effect that has on $PSc and a few other wording tweaks.

XXX pullup -9 (not -8, this isn't worth it for the short lifetime
that has left - if it took 28+ years for anyone to notice this, it
cannot be having all that much effect).

Use a type-correct end marker for strstrcat() rather than NULL, as
for a function with unknown number & types of args, the compiler isn't
able to automatically convert to the correct type. Issue pointed out
in off list e-mail by Rolland Illig ... Thanks.

The first arg (pointer to where to put length of result) is of a known
type, so doesn't have the same issue - we can keep using NULL for that
one when the length isn't needed.
Also, make sure to return a correctly null terminated null string in
the (absurd) case that there are no non-null args to strstrcat() (though
there are much better ways to generate "" on the stack). Since there is
currently just one call in the code, and it has real string args, this
isn't an issue for now, but who knows, some day.

NFCI - if there is any real change, then it is a change that is required.

XXX pullup -9 (together with the previous changes)
 1.20.4.1 02-Aug-2025  perseant Sync with HEAD
 1.57 03-Jul-2025  kre Don't allow read to make use of the shell's internal '='
terminates var names feature (which exists so in things
like "external foo=bar" the shell can simply set the "variable"
"foo=bar" to "bar" and doesn't need to put \0 on top of the '=',
or copy the var name part elsewhere, and other similar internal
advantages) - in most cases either allowing the '=' is intended, (as
in the export example) or other checks make it impossible (${var} etc),
but nothing was checking the var names passed to the read command.

Fix that ... (side effect is that now if an invalid name is
given, it will be detected before anything is read, before a
prompt is written, rather than after the read, when the vars
are being set to the fields from the line read).

Don't bother doing this in SMALL shells, avoid the (small) extra
code bloat - SMALL shells can just treat being able to say
read a b=hello c (which means the same as read a b c)
as a harmless foible...
 1.56 12-Oct-2024  kre Undo an idiotic attempted micro optimisation in the previous
version which broke things...
 1.55 11-Oct-2024  kre Add -b and -nMAX options to the read builtin.

As requested on (perhaps more than one) mailing list, this adds
a -n MAX option, to allow the amount of data read by the read
builtin to be limited to MAX bytes (in case the record delimiter
doesn't appear in the input for a long time). There is
currently an upper bound of 8MiB on the value of MAX.

Also add a -b option, which allows for buffered input (with
some usage caveats) rather than 1 byte at a time.

Neither option exists in SMALL shells.

Note that the proposed -z option got deleted ... I couldn't
find a rational way to explain what the final state would be
if a \0 on input generated an error, so rather than have
things ambiguous, better just not to have the option, and
simply keep ignoring input \0's as always.

See the (updated) sh(1) man page for more details.

No pullups planned (new feature, only for new releases).
 1.54 05-Oct-2023  kre branches: 1.54.2;
If the read builtin is told to read into IFS, we must avoid doing
that until all current uses of IFS are complete (as we have IFS's
value cached in ifs - if IFS alters, ifs might point anywhere).
Handle this by deferring assignments to IFS until everything is done.
This makes us appear to comply with the (currently) proposed requirement
for read by POSIX that field splitting complete before vars are
assigned. (Other shells, like dash, ksh93, yash, bosh behave like this)

That might end up being unspecified though, as other shells (bosh,
mksh) assign each field to its var as it is delimited (though bosh
appears to have bugs). If we wanted to go that route, the issue here
could have been handled by re-doing the init of ifs after every
setvar() that is performed here (except the last, after which it is
no longer needed).

XXX pullup -10
 1.53 11-Dec-2022  kre branches: 1.53.2;

It appears that POSIX intends to add a -d X option to the read command
in its next version, so it can be used as -d '' (to specify a \0 end
character for the record read, rather than the default \n) to accompany
find -print0 and xargs -0 options (also likely to be added).

Add support for -d now. While here fix a bug where escaped nul
chars (\ \0) in non-raw mode were not being dropped, as they are
when not escaped (if not dropped, they're still not used in any
useful way, they just ended the value at that point).
 1.52 19-Aug-2022  kre Don't output the error for bad usage (no var name given)
after already writing the prompt (set with the -p option).

That results in nonsense like:

$ read -p foo
fooread: arg count

While here, improve the error message so it means something.

Now we will get:

$ read -p foo
read: variable name required
Usage: read [-r] [-p prompt] var...

[Detected by code reading while doing the work for the previous fix]
 1.51 19-Aug-2022  kre PR bin/56972 Fix escape ('\') handling in sh read builtin.

In 1.35 (March 2005) (the big read fixup), most escape handling and IFS
processing in the read builtin was corrected. However 2 cases were missed,
one is a word (something to be assigned to any variable but the last) in
which every character is escaped (the code was relying on a non-escaped char
to set the "in a word" status), and second trailing IFS whitespace at
the end of the line was being deleted, even if the chars had been escaped
(the escape chars are no longer present).

See the PR for more details (including the case that detected the problem).

After fixing this, I looked at the FreeBSD code (normally might do it
before, but these fixes were trivial) to check their implementation.
Their code does similar things to ours now does, but in a completely
different way, their read builtin is more complex than ours needs to
be (they handle more options). For anyone tempted to simply incorporate
their code, note that it relies upon infrastructure changes elsewhere
in the shell, so would not be a simple cut and drop in exercise.

This needs pullups to -3 -4 -5 -6 -7 -8 and -9 (fortunately this is
happening before -10 is branched, so will never be broken this way there).
 1.50 16-Apr-2022  kre Redo the way the builtin cmd 'ulimit' getopt() (nextopt() really, but it
is essentially the same) arg string is generated, to lessen the chances
that the table of limits, and the arg string that allows limits to be
reported or set will get out of sync. They weren't (as long as we didn't
grow an RLIMIT_SWAP) this is just tidier.

While here, reorder the limits table fields, and shrink a couple that
were needlessly wasteful, to save some space -- for most architectures
this should save 8 bytes per table entry (there are currently 13).
(Some minor code bloat offsets this slightly because of int type
promotions now required).

NFCI.
 1.49 16-Apr-2022  kre While doing the previous change, I noticed that when used in a
particularly perverse way, the error message for a bad octal
constant as the new umask value could incorrectly claim that the
-S option (which would need to be present to cause this issue)
was the detected bad value. Fix that to report the actual
incorrect arg.

And while fiddling, also check for args to umask that are too big
to be sane mask values (the biggest permitted is 07777) and use
mode_t as the mask variable type, rather than int.
 1.48 16-Apr-2022  kre Avoid generating error messages implying that user errors are illegal.
 1.47 12-Dec-2021  andvar s/Miscelaneous/Miscellaneous/ and s/slahes/slashes/ in comments.
 1.46 16-Nov-2021  kre Detect write errors to stdout, and exit(1) from some built-in
commands which (primarily) are used just to generate output
(or with a particular option combination do so).
 1.45 15-Sep-2021  kre Have the ulimit command watch for ulimit -n (alter number of available fds)
and keep the rest of the shell aware of any changes.

While here, modify 'ulimit -aSH' to print both the soft and hard limits
for the resources, rather than just (in this case, as H comes last) the
hard limit. In any other case when both S and H are present, and we're
examining a limit, use the soft limit (just as if neither were given).

No change for setting limits (both are set, unless exactly one of -H
or -S is given). However, we now check for overflow when converting
the value to be assigned, rather than just truncating the value however
it happens to work out...
 1.44 13-May-2017  gson branches: 1.44.2; 1.44.10; 1.44.12;
Fix inconsistent whitespace
 1.43 09-May-2015  christos branches: 1.43.6;
CID 1225078: check getrlimit return
 1.42 11-Jun-2012  njoly Allow thread limit queries by adding the new -r flag to ulimit. Add
the corresponding documentation in the man page.
 1.41 09-Jun-2012  christos support RLIMIT_NTHR.
 1.40 11-Oct-2011  christos branches: 1.40.2;
print the flag too next to the units like bash does.
 1.39 18-Jun-2011  christos PR/45069: Henning Petersen: Use prototypes from builtins.h .
 1.38 29-Mar-2009  mrg branches: 1.38.4;
- add new RLIMIT_AS (aka RLIMIT_VMEM) resource that limits the total
address space available to processes. this limit exists in most other
modern unix variants, and like most of them, our defaults are unlimited.
remove the old mmap / rlimit.datasize hack.

- adds the VMCMD_STACK flag to all the stack-creation vmcmd callers.
it is currently unused, but was added a few years ago.

- add a pair of new process size values to kinfo_proc2{}. one is the
total size of the process memory map, and the other is the total size
adjusted for unused stack space (since most processes have a lot of
this...)

- patch sh, and csh to notice RLIMIT_AS. (in some cases, the alias
RLIMIT_VMEM was already present and used if availble.)

- patch ps, top and systat to notice the new k_vm_vsize member of
kinfo_proc2{}.

- update irix, svr4, svr4_32, linux and osf1 emulations to support
this information. (freebsd could be done, but that it's best left
as part of the full-update of compat/freebsd.)


this addresses PR 7897. it also gives correct memory usage values,
which have never been entirely correct (since mmap), and have been
very incorrect since jemalloc() was enabled.

tested on i386 and sparc64, build tested on several other platforms.

thanks to many folks for feedback and testing but most espcially
chuq and yamt for critical suggestions that lead to this patch not
having a special ugliness i wasn't happy with anyway :-)
 1.37 28-Dec-2008  christos branches: 1.37.2;
rlim_t will be unsigned as TOG mandates.
 1.36 01-Oct-2005  christos branches: 1.36.26;
fix setmode error handling.
 1.35 19-Mar-2005  dsl Fix the way the 'read' builtin processes IFS. In particular:
- IFS whitespace is now processes correctly,
- Trailing non-whitespace IFS characters are added to the last variable
iff a subsequent variable would have been assigned a non-null string.
Now passes the 'read' tests in http://www.research.att.com/~gsf/public/ifs.sh
 1.34 19-Apr-2004  lukem branches: 1.34.2;
Correct the description of sbsize; it is parsed in bytes not kbytes.
 1.33 17-Apr-2004  christos understand rlimit sbsize
 1.32 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.31 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.30 04-Feb-2001  christos remove redundant declarations and nexted externs.
 1.29 04-Jan-2001  lukem use more standard %ll_ in favour of %q_
 1.28 22-Nov-2000  christos error message cleanup:
- don't print the builtin name twice
- explain why things fail
- no extra newline
 1.27 26-Sep-1998  christos include <stdlib.h> to get the prototype for free()
 1.26 24-Sep-1998  itohy The return value of setmode(3) is a pointer to malloc()'ed area
and must be freed to avoid memory leaks if called repeatedly.
The leaks occured on symbolic umask command, such as "umask go-w",
which is undocumented.
 1.25 20-May-1998  christos Cast is*() args to unsigned chars in case the ctype macros are implemented
using arrays.
 1.24 04-Feb-1998  thorpej Fix printf formats so they work on the Alpha.
 1.23 21-Jan-1998  christos BSD4_4 is a standard symbol in <sys/param.h>; make sure that files
that need this defined, include <sys/param.h> and don't define it in
the Makefile. Add a comment to that effect.
 1.22 16-Jan-1998  christos test for the boundary condition in the previous trailing blank fix
 1.21 15-Jan-1998  christos PR/4805: Ty Sarna: read builtin does not remove trailing blanks.
 1.20 05-Nov-1997  kleink Per 1003.2, the (builtin) read utility shall treat the backslash as an
escape character (including line continuation), unless the `-r' option
is specified:
* adopt to this behaviour, add the `-r' option to disable it;
* remove the `-e' option, which was previously necessary to get this behaviour.
 1.19 04-Jul-1997  christos branches: 1.19.2;
Fix compiler warnings.
 1.18 11-Apr-1997  christos Make this work on systems that don't have quads
 1.17 11-Jan-1997  tls kill 'register'
 1.16 16-Oct-1996  christos PR/2808: Remove trailing whitespace (from FreeBSD)
 1.15 12-Jun-1995  jtc branches: 1.15.6;
Changed type of rlimit values from quad_t to rlim_t. Cast rlim_t's to
quad_t's and use "%qd" in printf.
Eliminated unneccessary conditional.
 1.14 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.13 21-Mar-1995  cgd convert to new RCS id conventions.
 1.12 05-Dec-1994  cgd clean up further. more patches from Jim Jegers
 1.11 11-Jun-1994  mycroft Add RCS ids.
 1.10 12-May-1994  jtc use prototypes provided by header files instead of our own
 1.9 12-May-1994  jtc Include appropriate header files to bring function prototypes into scope.
 1.8 11-May-1994  jtc integrate NetBSD's POSIX.2 compliant umask builtin
 1.7 11-May-1994  jtc sync with 4.4lite
 1.6 06-Apr-1994  cgd do right right thing if 'read' given no args. ptd out by Geoff Rehmet
 1.5 01-Aug-1993  mycroft Add RCS identifiers.
 1.4 21-Jul-1993  jtc Make umask builtin of shell POSIX 1003.2 compliant:
Print out a symbolic mask with the -S option; and accept symbolic mask
specifications.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.15.6.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.19.2.2 08-May-1998  mycroft Sync with trunk, per request of christos.
 1.19.2.1 06-Nov-1997  mellon Pull rev 1.20 up from trunk (kleink)
 1.34.2.1 07-Apr-2005  tron Pull up revision 1.35 (requested by dsl in ticket #117):
Fix the way the 'read' builtin processes IFS. In particular:
- IFS whitespace is now processes correctly,
- Trailing non-whitespace IFS characters are added to the last variable
iff a subsequent variable would have been assigned a non-null string.
Now passes the 'read' tests in http://www.research.att.com/~gsf/public/ifs.sh
 1.36.26.1 01-Apr-2009  snj Pull up following revision(s) (requested by mrg in ticket #622):
bin/csh/csh.1: revision 1.46
bin/csh/func.c: revision 1.37
bin/ps/print.c: revision 1.111
bin/ps/ps.c: revision 1.74
bin/sh/miscbltin.c: revision 1.38
bin/sh/sh.1: revision 1.92 via patch
external/bsd/top/dist/machine/m_netbsd.c: revision 1.7
lib/libkvm/kvm_proc.c: revision 1.82
sys/arch/mips/mips/cpu_exec.c: revision 1.55
sys/compat/darwin/darwin_exec.c: revision 1.57
sys/compat/ibcs2/ibcs2_exec.c: revision 1.73
sys/compat/irix/irix_resource.c: revision 1.15
sys/compat/linux/arch/amd64/linux_exec_machdep.c: revision 1.16
sys/compat/linux/arch/i386/linux_exec_machdep.c: revision 1.12
sys/compat/linux/common/linux_limit.h: revision 1.5
sys/compat/osf1/osf1_resource.c: revision 1.14
sys/compat/svr4/svr4_resource.c: revision 1.18
sys/compat/svr4_32/svr4_32_resource.c: revision 1.17
sys/kern/exec_subr.c: revision 1.62
sys/kern/init_sysctl.c: revision 1.160
sys/kern/kern_exec.c: revision 1.288
sys/kern/kern_resource.c: revision 1.151
sys/sys/param.h: patch
sys/sys/resource.h: revision 1.31
sys/sys/sysctl.h: revision 1.184
sys/uvm/uvm_extern.h: revision 1.153
sys/uvm/uvm_glue.c: revision 1.136
sys/uvm/uvm_mmap.c: revision 1.128
usr.bin/systat/ps.c: revision 1.32
- - add new RLIMIT_AS (aka RLIMIT_VMEM) resource that limits the total
address space available to processes. this limit exists in most other
modern unix variants, and like most of them, our defaults are unlimited.
remove the old mmap / rlimit.datasize hack.
- - adds the VMCMD_STACK flag to all the stack-creation vmcmd callers.
it is currently unused, but was added a few years ago.
- - add a pair of new process size values to kinfo_proc2{}. one is the
total size of the process memory map, and the other is the total size
adjusted for unused stack space (since most processes have a lot of
this...)
- - patch sh, and csh to notice RLIMIT_AS. (in some cases, the alias
RLIMIT_VMEM was already present and used if availble.)
- - patch ps, top and systat to notice the new k_vm_vsize member of
kinfo_proc2{}.
- - update irix, svr4, svr4_32, linux and osf1 emulations to support
this information. (freebsd could be done, but that it's best left
as part of the full-update of compat/freebsd.)
this addresses PR 7897. it also gives correct memory usage values,
which have never been entirely correct (since mmap), and have been
very incorrect since jemalloc() was enabled.
tested on i386 and sparc64, build tested on several other platforms.
thanks to many folks for feedback and testing but most espcially
chuq and yamt for critical suggestions that lead to this patch not
having a special ugliness i wasn't happy with anyway :-)
 1.37.2.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.38.4.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.40.2.1 30-Oct-2012  yamt sync with head
 1.43.6.1 19-May-2017  pgoyette Resolve conflicts from previous merge (all resulting from $NetBSD
keywork expansion)
 1.44.12.1 27-Oct-2022  martin Pull up following revision(s) (requested by kre in ticket #1549):

bin/sh/miscbltin.c: revision 1.51
bin/sh/miscbltin.c: revision 1.52

PR bin/56972 Fix escape ('\') handling in sh read builtin.

In 1.35 (March 2005) (the big read fixup), most escape handling and IFS
processing in the read builtin was corrected. However 2 cases were missed,
one is a word (something to be assigned to any variable but the last) in
which every character is escaped (the code was relying on a non-escaped char
to set the "in a word" status), and second trailing IFS whitespace at
the end of the line was being deleted, even if the chars had been escaped
(the escape chars are no longer present).

See the PR for more details (including the case that detected the problem).

After fixing this, I looked at the FreeBSD code (normally might do it
before, but these fixes were trivial) to check their implementation.

Their code does similar things to ours now does, but in a completely
different way, their read builtin is more complex than ours needs to
be (they handle more options). For anyone tempted to simply incorporate
their code, note that it relies upon infrastructure changes elsewhere
in the shell, so would not be a simple cut and drop in exercise.

This needs pullups to -3 -4 -5 -6 -7 -8 and -9 (fortunately this is
happening before -10 is branched, so will never be broken this way there).

-

Don't output the error for bad usage (no var name given)
after already writing the prompt (set with the -p option).

That results in nonsense like:
$ read -p foo
fooread: arg count

While here, improve the error message so it means something.

Now we will get:
$ read -p foo
read: variable name required
Usage: read [-r] [-p prompt] var...

[Detected by code reading while doing the work for the previous fix]
 1.44.10.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.44.10.1 21-Apr-2020  martin Sync with HEAD
 1.44.2.1 27-Oct-2022  martin Pull up following revision(s) (requested by kre in ticket #1779):

bin/sh/miscbltin.c: revision 1.51
bin/sh/miscbltin.c: revision 1.52

PR bin/56972 Fix escape ('\') handling in sh read builtin.

In 1.35 (March 2005) (the big read fixup), most escape handling and IFS
processing in the read builtin was corrected. However 2 cases were missed,
one is a word (something to be assigned to any variable but the last) in
which every character is escaped (the code was relying on a non-escaped char
to set the "in a word" status), and second trailing IFS whitespace at
the end of the line was being deleted, even if the chars had been escaped
(the escape chars are no longer present).

See the PR for more details (including the case that detected the problem).

After fixing this, I looked at the FreeBSD code (normally might do it
before, but these fixes were trivial) to check their implementation.

Their code does similar things to ours now does, but in a completely
different way, their read builtin is more complex than ours needs to
be (they handle more options). For anyone tempted to simply incorporate
their code, note that it relies upon infrastructure changes elsewhere
in the shell, so would not be a simple cut and drop in exercise.

This needs pullups to -3 -4 -5 -6 -7 -8 and -9 (fortunately this is
happening before -10 is branched, so will never be broken this way there).

-

Don't output the error for bad usage (no var name given)
after already writing the prompt (set with the -p option).

That results in nonsense like:
$ read -p foo
fooread: arg count

While here, improve the error message so it means something.

Now we will get:
$ read -p foo
read: variable name required
Usage: read [-r] [-p prompt] var...

[Detected by code reading while doing the work for the previous fix]
 1.53.2.1 03-Nov-2023  martin Pull up following revision(s) (requested by kre in ticket #454):

bin/sh/miscbltin.c: revision 1.54

If the read builtin is told to read into IFS, we must avoid doing
that until all current uses of IFS are complete (as we have IFS's
value cached in ifs - if IFS alters, ifs might point anywhere).
Handle this by deferring assignments to IFS until everything is done.

This makes us appear to comply with the (currently) proposed requirement
for read by POSIX that field splitting complete before vars are
assigned. (Other shells, like dash, ksh93, yash, bosh behave like this)

That might end up being unspecified though, as other shells (bosh,
mksh) assign each field to its var as it is delimited (though bosh
appears to have bugs). If we wanted to go that route, the issue here
could have been handled by re-doing the init of ifs after every
setvar() that is performed here (except the last, after which it is
no longer needed).
 1.54.2.1 02-Aug-2025  perseant Sync with HEAD
 1.3 21-Aug-2003  christos branches: 1.3.98;
remove clause 3
 1.2 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.1 04-Jul-1997  christos Fix compiler warnings.
 1.3.98.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.3.98.1 21-Apr-2020  martin Sync with HEAD
 1.22 06-Oct-2009  apb branches: 1.22.48;
Make this slightly more portable; it has to run on arbitary host
platforms at build time. Previousy, some shells were confused by
some of the "[ ... ]" tests.
 1.21 06-Jun-2004  christos don't include the printf builtin if we are SMALL; saves 10K.
 1.20 22-Aug-2003  agc Move 4 more files from UCB 4 clause to 3 clause licence.

Thanks to Julio Merino for pointing them out.
 1.19 15-May-2003  dsl Use echo instead of cat to add prologue to builtins.[ch]
Under some circumstances the headers got overwritten.
 1.18 02-Feb-2003  dsl Parse end of line comments correctly
(approved by christos)
 1.17 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.16 15-Oct-2002  lukem don't compile in `-h' as a synonym for the command that the argument is for.
this fix is hokey, but works as well as the previous solution for -j.
 1.15 09-Jul-1999  christos compile with WARNS = 2
 1.14 29-Mar-1998  christos Pr/5221: Jason Thorpe: Simplify sh Makefile, so that builts from other
directories work.
 1.13 04-Jul-1997  christos branches: 1.13.2;
Fix compiler warnings.
 1.12 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.11 21-Mar-1995  cgd convert to new RCS id conventions.
 1.10 07-Jul-1994  mycroft Bandaid quoting.
 1.9 24-Jun-1994  jtc Renamed builtins to builtins.def to eliminate make's (incorrect) circular
dependancy between the builtins data file and builtins.c.

The bug only occured when there was no obj directory, and is a result of
NetBSD's better (compared to 4.4lite's) default make rules.

Fixes bug #301.
 1.8 11-Jun-1994  mycroft Add RCS ids.
 1.7 14-May-1994  cgd add back in support for building w/o obj dir. also, add NO_HISTORY
define, which (if you invoke mkbuiltins properly) gets you a sh w/o
history of command line editing (for floppy sh).
 1.6 11-May-1994  jtc sync with 4.4lite
 1.5 28-Dec-1993  davidb Changed to use make's new .OBJDIR
 1.4 18-Apr-1993  mycroft Don't lose if obj dir not present.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.13.2.1 08-May-1998  mycroft Sync with trunk, per request of christos.
 1.22.48.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.22.48.1 21-Apr-2020  martin Sync with HEAD
 1.24 17-Jan-2004  dsl Replace mkinit.c with mkinit.sh
Build mksyntax directly from mksyntax.c so that the -DTARGET_CHAR=xxx
is applied when it is build.
OTOH mksyntax is broken as it tries to determine properties of the
target system by running code on the build system.
 1.23 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.22 13-Jul-2003  itojun use bounded string op
 1.21 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.20 18-Jul-2000  cgd host program portability: don't use <sys/cdefs.h>, __COPYRIGHT(),
__RCSID(), or __P(). (these programs have been de-__P()'d.)
Repeat after me: "Not all cross-compilation host systems are NetBSD."
 1.19 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.18 28-Jul-1998  mycroft Delint.
 1.17 04-Jul-1997  christos Fix compiler warnings.
 1.16 11-Jan-1997  tls kill 'register'
 1.15 16-Oct-1996  christos PR/2808: #undef things that we #define to avoid duplicate definitions.
(from FreeBSD)
 1.14 18-Feb-1996  mycroft branches: 1.14.4;
Fix problems with the way init.o is built:
* Prevent gratuitous rebuilds when nothing has changed.
* Make sure it's rebuilt if a .h file is updated.
From Mike Long, PR 1454.
 1.13 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.12 21-Mar-1995  cgd convert to new RCS id conventions.
 1.11 23-Dec-1994  cgd pull some prototypes into scope.
 1.10 04-Dec-1994  cgd from James Jegers <jimj@miller.cs.uwm.edu>: quiet -Wall, and squelch
some of the worst style errors.
 1.9 15-Jun-1994  mycroft Re-fix an old bug.
 1.8 11-Jun-1994  mycroft Add RCS ids.
 1.7 11-May-1994  jtc sync with 4.4lite
 1.6 25-Apr-1994  cgd lseek long lossage.
 1.5 27-Mar-1994  deraadt avoid spilling garbage into the file init.c. this happened if libc
was compiled -g..
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.14.4.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.10 05-Dec-2018  kre 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.
 1.9 18-Oct-2018  kre 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
 1.8 16-Sep-2018  kre Begone unspecified test-a ... and be more obious what it was doing.
 1.7 27-Mar-2016  christos branches: 1.7.8; 1.7.14; 1.7.16;
General KNF and source code cleanups, avoid scattering the
magic string " \t\n" all over the place, slightly improved
syntax error messages, restructured some of the code for
clarity, don't allow IFS to be imported through the environment,
and remove the (never) conditionally compiled ATTY option.
Apart from one or two syntax error messages, and ignoring IFS
if present in the environment, this is intended to have no
user visible changes. (from kre@)
 1.6 08-Mar-2016  christos avoid duplicate includes (from kre)
 1.5 23-Oct-2008  apb Don't try to remove trailing newlines from shell variables via
constructs like ${var%$nl}. Apparently this allows the Ubuntu Linux
/bin/sh (dash) to be used as a host shell during the build.
Patch from PR 39786 by Andy Shevchenko.
 1.4 29-Apr-2008  martin Convert to new 2 clause license
 1.3 27-Feb-2008  dsl branches: 1.3.2;
Generate ANSI functions.
Just indent the code by a single tab - no need to compare against the
C program generated version any more.
 1.2 15-Jun-2004  dsl branches: 1.2.16; 1.2.22;
Use shell variables to get '(' and '\' inside shell substitution patterns.
Solves problems with different shells having differntly buggy parsers
(and the standard probably allowing random behaviour).
Should fix bin/25938
 1.1 17-Jan-2004  dsl Replace mkinit.c with mkinit.sh
Build mksyntax directly from mksyntax.c so that the -DTARGET_CHAR=xxx
is applied when it is build.
OTOH mksyntax is broken as it tries to determine properties of the
target system by running code on the build system.
 1.2.22.1 24-Mar-2008  keiichi sync with head.
 1.2.16.1 23-Mar-2008  matt sync with HEAD
 1.3.2.1 18-May-2008  yamt sync with head.
 1.7.16.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.7.16.2 21-Apr-2020  martin Sync with HEAD
 1.7.16.1 10-Jun-2019  christos Sync with HEAD
 1.7.14.3 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.7.14.2 20-Oct-2018  pgoyette Sync with head
 1.7.14.1 30-Sep-2018  pgoyette Ssync with HEAD
 1.7.8.1 21-Oct-2018  martin Pull up following revision(s) (requested by kre in ticket #1067):

bin/sh/mkinit.sh: revision 1.9

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
 1.7 14-Oct-2024  kre Use tools awk where appropriate

And while here, switch from using echo to printf (via a
function echo() which just uses printf, but is limited to
this file only - it isn't general enough for use elsewhere).

NFCI
 1.6 18-Aug-2018  kre branches: 1.6.12;

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!)
 1.5 16-Aug-2018  kre 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.
 1.4 19-Jun-2017  kre branches: 1.4.4; 1.4.6;

It is amazing what nonsense appears to work sometimes... (all my nonsense too!)

Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...

So as it happens these fixes change nothing, but they are needed anyway.

(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
 1.3 27-Mar-2016  christos branches: 1.3.8;
General KNF and source code cleanups, avoid scattering the
magic string " \t\n" all over the place, slightly improved
syntax error messages, restructured some of the code for
clarity, don't allow IFS to be imported through the environment,
and remove the (never) conditionally compiled ATTY option.
Apart from one or two syntax error messages, and ignoring IFS
if present in the environment, this is intended to have no
user visible changes. (from kre@)
 1.2 27-Feb-2016  christos Improve debugging, from kre (I hooked it to the build).
 1.1 24-Feb-2016  christos Simple script to create node names for debugging
 1.3.8.1 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.4.6.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.4.6.2 21-Apr-2020  martin Sync with HEAD
 1.4.6.1 10-Jun-2019  christos Sync with HEAD
 1.4.4.1 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.6.12.1 02-Aug-2025  perseant Sync with HEAD
 1.24 16-Jan-2004  dsl Replace the C program mknodes.c with a shell script mknodes.sh
(mkinit and mksyntax may also die soon...)
 1.23 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.22 13-Jul-2003  itojun use bounded string op
 1.21 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.20 04-Oct-2002  christos Rename ALIGN to SHELL_ALIGN and simplify macro so that it does not have side
effects, and add double to it, so that it aligns doubles correctly too. This
is just a workaround to fix the sparc64 problem where ALIGN() is now defined
in some include file to be 16 instead of 8. Thanks to martin for debugging this.
 1.19 25-May-2002  wiz __STDC__ is always defined on NetBSD, so remove #ifdef __STDC__ (and
unnecessary #else cases).
 1.18 27-Jul-2000  cgd host program portability: set infp to stdin at runtime, since apparently
some systems (e.g. linux) define stdin in such a way that it can't
be used to initialize values at compile time.
 1.17 18-Jul-2000  cgd host program portability: don't use <sys/cdefs.h>, __COPYRIGHT(),
__RCSID(), or __P(). (these programs have been de-__P()'d.)
Repeat after me: "Not all cross-compilation host systems are NetBSD."
 1.16 28-Jul-1998  mycroft Delint.
 1.15 04-Jul-1997  christos Fix compiler warnings.
 1.14 11-Apr-1997  christos Pointer arithmetic on void * is illegal but gcc does not care.
 1.13 11-Jan-1997  tls kill 'register'
 1.12 16-Oct-1996  christos PR/2808: Remove extraneous newline from error message (from FreeBSD)
 1.11 11-May-1995  christos branches: 1.11.6;
Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.10 21-Mar-1995  cgd convert to new RCS id conventions.
 1.9 23-Dec-1994  cgd pull some prototypes into scope.
 1.8 04-Dec-1994  cgd from James Jegers <jimj@miller.cs.uwm.edu>: quiet -Wall, and squelch
some of the worst style errors.
 1.7 11-Jun-1994  mycroft Add RCS ids.
 1.6 11-May-1994  jtc sync with 4.4lite
 1.5 05-Sep-1993  mycroft From Vincent Broman:
error.c: Add , in initializer list if ENOLINK defined.
output.c: Gratuitously remove extra ;.
mknodes.c: main() should return a value.
Not from Vincent Broman:
mknodes.c: main() should return an error condition when approriate, too.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.11.6.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.4 19-Jan-2019  kre 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.
 1.3 22-Jun-2018  kre branches: 1.3.2;
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).
 1.2 29-Apr-2008  martin branches: 1.2.56; 1.2.62;
Convert to new 2 clause license
 1.1 16-Jan-2004  dsl branches: 1.1.30;
Replace the C program mknodes.c with a shell script mknodes.sh
(mkinit and mksyntax may also die soon...)
 1.1.30.1 18-May-2008  yamt sync with head.
 1.2.62.2 26-Jan-2019  pgoyette Sync with HEAD
 1.2.62.1 25-Jun-2018  pgoyette Sync with HEAD
 1.2.56.1 13-Jul-2018  martin Pull up following revision(s) (requested by kre in ticket #906):

bin/sh/eval.c: revision 1.155
bin/sh/mknodes.sh: revision 1.3
bin/sh/nodes.c.pat: revision 1.14
bin/sh/exec.h: revision 1.27
bin/sh/exec.c: revision 1.52

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).
 1.3.2.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.3.2.2 21-Apr-2020  martin Sync with HEAD
 1.3.2.1 10-Jun-2019  christos Sync with HEAD
 1.9 11-Oct-2024  kre Allow '+' to be specified as the "one char option name" for long options
that don't have a 1 char equivalent, but do want to become a member of
an option set. The '+' is otherwise ignored. This is similar to '-'
in that position, except that skips past the option set field, and is
followed directly by the default value, '+' does not do that.

This currently changes nothing, as there are no current options that
use it (or would want to).
 1.8 16-Apr-2024  christos branches: 1.8.2;
Be more explicit with sort fields to produce consistent results with gnu
sort (Jan-Benedict Glaw)
 1.7 06-Apr-2024  kre Redo the mktemp(1) part - some mktemp's (including ours) require the
XXXX's to be at the end of the name (like mk*temp(3)) so however well
it will work with mktemp implementations which allow the X's to be
anywhere in the final component of the name, it will work just as
well on them with the X's at the end.

But we don't normally need all of that mess - knowing which temp
file is which is useful only when debugging the script, and that's
(mostly) long done. So, in normal uses now just use $(mktemp) and
allow mktemp to pick its own name - we don't need to know what it is.
Every mktemp(1) supports that mode of operation.

Bug when debugging the script (which for current purposes will be
taken to be when the -x flag is passed to the shell running it, to
trace what it does) then we will make the temp files have names we
can recognise (and in that case, also don't delete them when done).

While here, check for mktemp(1) failing, and abort if that
happens (we assume that if it fails it will write an error
message to stderr, so the script does not need to.)

As for the purpose of the script ... of course the header file
generated (or an equivalent elsewhere) could be generated and
maintained by hand, but why would anyone want to do all that
work when software can do it for us, and do it correctly without
human thought?

This also allows the options in the master list (option.list) to be
arranged in a way that is meaningful for them, unrelated to the order
the shell needs to have them in (or rearrange them to be at run time)
and have that order shuffled however is convenient. Currently all
the posix standard options are first, then the "hybrid" options, and
finally the local ones for this shell. Currently "pipefail" is in the
final set, but once the next posix version is published, that will
become a standard option, and get moved in the list - the shell won't
even notice as this script puts the options into shell desired order.
 1.6 05-Apr-2024  christos From Jan-Benedict Glaw:

Fix a redirection and prepare a stable sort for upper-/lowercase
option letters

This script is a mess, I strongly believe that it should be rewritten.
However, I'm not 100% sure why it was invented in the first place
(come on, the generated header file isn't _that_ complicated that
it couldn't be sanely managed by hand!), but let's fix the sorting
order by using LC_ALL=C.

Also add a few 'X' to the `mktemp` template to make non-BSD
implementations happy. As a bonus, actually *use* the initial `sed`
output instead of throwing it away by piping it into `sort` with
also connecting `sort`'s stdin with the original input file...
 1.5 15-Nov-2017  kre branches: 1.5.4; 1.5.6; 1.5.14;
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.)
 1.4 01-Jul-2017  kre Make $- list flags in the same order they appear in sh(1)
 1.3 19-Jun-2017  kre "b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
 1.2 28-May-2017  kre branches: 1.2.2;
Redo mkoptions.sh .. much better this way, now fully automated
option sorting (no longer required option.list to be manually
sorted by long option name) and properly handles conditional
options. Cleaner output format as well.

This allows option.list to be reordered to group related options
together ... also added more comments to it.
 1.1 28-May-2017  kre Arrange for set -o and $- output to be sorted, rather than more
or less random (and becoming worse as more options are added.)
Since the data is known at compile time, sort at compile time,
rather than at run time.
 1.2.2.1 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.5.14.1 01-Oct-2025  martin Pull up following revision(s) (requested by kre in ticket #1165):

bin/sh/option.list: revision 1.12
bin/sh/mkoptions.sh: revision 1.6
bin/sh/mkoptions.sh: revision 1.7
bin/sh/mkoptions.sh: revision 1.8
bin/sh/mkoptions.sh: revision 1.9

From Jan-Benedict Glaw:
Fix a redirection and prepare a stable sort for upper-/lowercase
option letters

This script is a mess, I strongly believe that it should be rewritten.
However, I'm not 100% sure why it was invented in the first place
(come on, the generated header file isn't _that_ complicated that
it couldn't be sanely managed by hand!), but let's fix the sorting
order by using LC_ALL=C.

Also add a few 'X' to the `mktemp` template to make non-BSD
implementations happy. As a bonus, actually *use* the initial `sed`
output instead of throwing it away by piping it into `sort` with
also connecting `sort`'s stdin with the original input file...

Redo the mktemp(1) part - some mktemp's (including ours) require the
XXXX's to be at the end of the name (like mk*temp(3)) so however well
it will work with mktemp implementations which allow the X's to be
anywhere in the final component of the name, it will work just as
well on them with the X's at the end.

But we don't normally need all of that mess - knowing which temp
file is which is useful only when debugging the script, and that's
(mostly) long done. So, in normal uses now just use $(mktemp) and
allow mktemp to pick its own name - we don't need to know what it is.
Every mktemp(1) supports that mode of operation.
Bug when debugging the script (which for current purposes will be
taken to be when the -x flag is passed to the shell running it, to
trace what it does) then we will make the temp files have names we
can recognise (and in that case, also don't delete them when done).

While here, check for mktemp(1) failing, and abort if that
happens (we assume that if it fails it will write an error
message to stderr, so the script does not need to.)

As for the purpose of the script ... of course the header file
generated (or an equivalent elsewhere) could be generated and
maintained by hand, but why would anyone want to do all that
work when software can do it for us, and do it correctly without
human thought?

This also allows the options in the master list (option.list) to be
arranged in a way that is meaningful for them, unrelated to the order
the shell needs to have them in (or rearrange them to be at run time)
and have that order shuffled however is convenient. Currently all
the posix standard options are first, then the "hybrid" options, and
finally the local ones for this shell. Currently "pipefail" is in the
final set, but once the next posix version is published, that will
become a standard option, and get moved in the list - the shell won't
even notice as this script puts the options into shell desired order.

Be more explicit with sort fields to produce consistent results with gnu
sort (Jan-Benedict Glaw)

Allow '+' to be specified as the "one char option name" for long options
that don't have a 1 char equivalent, but do want to become a member of
an option set. The '+' is otherwise ignored. This is similar to '-'
in that position, except that skips past the option set field, and is
followed directly by the default value, '+' does not do that.
This currently changes nothing, as there are no current options that
use it (or would want to).

PR bin/59646 option.list wsp consistency
Be consistent with use of tab/space in fields used as
sort keys, so versions of sort which might not behave
exactly as we expect should still produce the same results.
 1.5.6.1 01-Oct-2025  martin Pull up following revision(s) (requested by kre in ticket #1970):

bin/sh/option.list: revision 1.12
bin/sh/mkoptions.sh: revision 1.6
bin/sh/mkoptions.sh: revision 1.7
bin/sh/mkoptions.sh: revision 1.8
bin/sh/mkoptions.sh: revision 1.9

From Jan-Benedict Glaw:
Fix a redirection and prepare a stable sort for upper-/lowercase
option letters

This script is a mess, I strongly believe that it should be rewritten.
However, I'm not 100% sure why it was invented in the first place
(come on, the generated header file isn't _that_ complicated that
it couldn't be sanely managed by hand!), but let's fix the sorting
order by using LC_ALL=C.

Also add a few 'X' to the `mktemp` template to make non-BSD
implementations happy. As a bonus, actually *use* the initial `sed`
output instead of throwing it away by piping it into `sort` with
also connecting `sort`'s stdin with the original input file...

Redo the mktemp(1) part - some mktemp's (including ours) require the
XXXX's to be at the end of the name (like mk*temp(3)) so however well
it will work with mktemp implementations which allow the X's to be
anywhere in the final component of the name, it will work just as
well on them with the X's at the end.

But we don't normally need all of that mess - knowing which temp
file is which is useful only when debugging the script, and that's
(mostly) long done. So, in normal uses now just use $(mktemp) and
allow mktemp to pick its own name - we don't need to know what it is.
Every mktemp(1) supports that mode of operation.
Bug when debugging the script (which for current purposes will be
taken to be when the -x flag is passed to the shell running it, to
trace what it does) then we will make the temp files have names we
can recognise (and in that case, also don't delete them when done).

While here, check for mktemp(1) failing, and abort if that
happens (we assume that if it fails it will write an error
message to stderr, so the script does not need to.)

As for the purpose of the script ... of course the header file
generated (or an equivalent elsewhere) could be generated and
maintained by hand, but why would anyone want to do all that
work when software can do it for us, and do it correctly without
human thought?

This also allows the options in the master list (option.list) to be
arranged in a way that is meaningful for them, unrelated to the order
the shell needs to have them in (or rearrange them to be at run time)
and have that order shuffled however is convenient. Currently all
the posix standard options are first, then the "hybrid" options, and
finally the local ones for this shell. Currently "pipefail" is in the
final set, but once the next posix version is published, that will
become a standard option, and get moved in the list - the shell won't
even notice as this script puts the options into shell desired order.

Be more explicit with sort fields to produce consistent results with gnu
sort (Jan-Benedict Glaw)

Allow '+' to be specified as the "one char option name" for long options
that don't have a 1 char equivalent, but do want to become a member of
an option set. The '+' is otherwise ignored. This is similar to '-'
in that position, except that skips past the option set field, and is
followed directly by the default value, '+' does not do that.
This currently changes nothing, as there are no current options that
use it (or would want to).

PR bin/59646 option.list wsp consistency
Be consistent with use of tab/space in fields used as
sort keys, so versions of sort which might not behave
exactly as we expect should still produce the same results.
 1.5.4.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.5.4.1 21-Apr-2020  martin Sync with HEAD
 1.8.2.1 02-Aug-2025  perseant Sync with HEAD
 1.5 06-Aug-1993  mycroft Clean up deleted files.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.31 17-Jan-2004  dsl Put a syntax.c under CVS instead of building if with the mksyntax program.
Kill mksyntax.c - no longer possible to get the 'wrong sort of chars'.
/bin/sh now has no helper binaries.
syntax.c uses C99 initialisers, run time initialisation could be used
for systems where the compiler doesn't support them.
I've used some #defines to help make this possible - but writing the code
starts making it rather messy.
 1.30 17-Jan-2004  dsl Put syntax.h under CVS instead of having it generated by mksyntax.
Use CHAR_MIN (from limits.h) to determine whether target char are signed
or unsigned - the syntax tables will not be indexed properly.
Rip out all the stuff from mksyntax.c that wrote syntax.h.
syntax.c can stiff be generated incorrectly...
 1.29 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.28 13-Jul-2003  itojun use bounded string op
 1.27 12-Jan-2003  christos Make sure that UPEOF is compared to the right type on the left hand
side. Sometimes it was compared to int, which was -1 on EOF, and on
unsigned char machines UPEOF was (unsigned char)-1. This worked
by chance because isalpha((unsigned char)-1) returns false usually,
but it does not when the locale is invalid!
 1.26 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.25 31-May-2002  christos deal with char c; c != -129 warning. Hi jason!
 1.24 12-Feb-2002  christos branches: 1.24.4;
Since we should be able to handle nested double quotes, don't use
the syntax maps to determine the beginning and end quotes (kill
CENDQUOTE). Handle single quotes opening and closing via checking
the current syntax map. Keep a bitmap of doublequote state one bit
per variable nesting level. For the first 32 nested double quotes,
we don't need any additional memory, but for more we allocate
dynamically.
 1.23 18-Jul-2000  cgd host program portability: don't use <sys/cdefs.h>, __COPYRIGHT(),
__RCSID(), or __P(). (these programs have been de-__P()'d.)
Repeat after me: "Not all cross-compilation host systems are NetBSD."
 1.22 08-Mar-1999  castor Add '#include <sys/types.h>' since that's where int8_t and u_int8_t can
usually be found.
 1.21 23-Feb-1999  wrstuden Make cross-building from i386 to powerpc (from signed to unsigned char)
ports work right.
 1.20 25-Jan-1999  mycroft Patches from Tor Egge (via Havard Eidnes) to fix various bugs in field
splitting and combining.
(Note: Some of this are not strictly bugs, but differences between traditional
Bourne shell and POSIX.)
 1.19 12-Sep-1998  wrstuden Patch to make sh cross-compile right. mksyntax reports unsigned char for
powerpc, and signed for m68k & i386.
 1.18 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.17 28-Jul-1998  mycroft Delint.
 1.16 18-Jul-1997  thorpej Cast the result of pointer arithmetic to "long", and use %ld to print it.
 1.15 05-Jul-1997  christos Fix problem introduced in yesterday's commit that broke both signed and
unsigned character machines. So that people don't have to reverse engineer
this again:
mksyntax detects if characters are signed or not and builts a syntax
table that has a base of 129 for signed characters or 1 for unsigned
characters. This is so the largest negative signed char [-128] + the
base == 1. 0 is special and means end of file in both cases. PEOF
is -1 for the unsigned character case and -129 for the signed
character case, so that syntax[PEOF + base] == syntax[0] == CEOF
So PEOF has to be -1, but it is explicitly compared with
unsigned characters on machines where characters are unsigned.
The quick fix is to define UPEOF the (unsigned char) version of PEOF
and use that. A better fix is to always use unsigned characters
when referencing symbol table entries, but that would require
extensive changes to the shell. So to summarize

syntax[0] == CEOF, base + PEOF == 0

unsigned signed
base 1 129
PEOF -1 -129
 1.14 04-Jul-1997  christos Make the syntax tables work on machines where characters are unsigned.
 1.13 04-Jul-1997  christos Fix compiler warnings.
 1.12 16-Oct-1996  christos PR/2808: Use the <ctype.h> functions instead of our homegrown tables to
determine if a character is alpha or alphanumeric, so that NLS
works... now if only our NLS really worked... (from FreeBSD).
 1.11 11-May-1995  christos branches: 1.11.6;
Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.10 21-Mar-1995  cgd convert to new RCS id conventions.
 1.9 23-Dec-1994  cgd pull some prototypes into scope.
 1.8 04-Dec-1994  cgd from James Jegers <jimj@miller.cs.uwm.edu>: quiet -Wall, and squelch
some of the worst style errors.
 1.7 14-Jun-1994  jtc From Christos:
1. Fix `-' quoting in [ ] expressions.
2. Fix expansion of variables in redirections
 1.6 11-Jun-1994  mycroft Add RCS ids.
 1.5 11-May-1994  jtc sync with 4.4lite
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.11.6.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.24.4.1 31-May-2002  tv Pull up revision 1.25 (requested by mycroft in ticket #110):
deal with char c; c != -129 warning. Hi jason!
 1.15 07-Apr-2023  kre The great shell trailing whitespace cleanup of 2023...
Inspired by private e-mail comments from mouse@

NFCI.
 1.14 26-Jul-2017  kre branches: 1.14.4;
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.)
 1.13 04-May-2017  kre Implement the ';&' (used instead of ';;') case statement list terminator
which causes fall through the to command list of the following pattern
(wuthout evaluating that pattern). This has been approved for inclusion
in the next major version of the POSIX standard (Issue 8), and is
implemented by most other shells.

Now all form a circle and together attempt to summon the great wizd
in the hopes that his magic spells can transform the poor attempt
at documenting this feature into something rational...
 1.12 25-Oct-2008  apb branches: 1.12.52;
In shell scripts run during the build, add a SED variable, defaulting
to "sed". SED=${TOOL_SED:Q} should be passed in the environment to
override this.
 1.11 19-Oct-2008  apb In shell scripts invoked during a build, and in crunchgen, use ${AWK}
instead of plain "awk". The Makefiles that invoke these scripts
or programs will pass AWK=${HOST_AWK:Q}.
 1.10 22-Aug-2003  agc Move 4 more files from UCB 4 clause to 3 clause licence.

Thanks to Julio Merino for pointing them out.
 1.9 09-Jul-1999  christos compile with WARNS = 2
 1.8 16-Oct-1996  christos PR/2808: tokens.def -> tokens.h
 1.7 11-May-1995  christos branches: 1.7.6;
Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 11-Jun-1994  mycroft Add RCS ids.
 1.4 11-May-1994  jtc sync with 4.4lite
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.7.6.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.12.52.1 11-May-2017  pgoyette Sync with HEAD
 1.14.4.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.14.4.1 21-Apr-2020  martin Sync with HEAD
 1.4 26-Jan-1994  mycroft Clean up deleted files.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.16 13-Jul-2024  kre Implement the HISTFILE and HISTAPPEND variables.

See the (newly updated) sh(1) for details.
Also add the -z option to fc (clear history).

None of this exists in SMALL shells.
 1.15 12-Jul-2024  kre Improve safety in var imports from the environment.

Add a new var flag VUNSAFE - set on all vars imported from the environment.

Add setvareqsafe() (which is to setvareq() as setvarsafe() is to setvar())
and use that instead of setvareq() when processing the environment, so
errors don't cause the shell to abort. Use VUNSAFE in that call.

Add flags arguments to all var callback functions which are used when setting
variables, and pass the flags given to the setvar*() functions to those
functions, so they can act differently in different situations (if desired).
Most of them just ignore the flags.

When unsetting a variable, call setvar() to clear things (and call the
callback function) both when the variable had a value which needs to be freed,
and when unsetting a variable which wasn't unset previously, so the VUNSET
flag can be seen by that callback func.

When setting HISTSIZE, use the flags passed to determine whether to ignore
bad values (if VUNSAFE) or treat them as an error. This replaces the
earlier temporary hack to always ignore bad data there (histedit.c 1.68).

Miscellaneous associated minor changes.

These changes should largely be invisible in normal use.
 1.14 15-Aug-2021  christos branches: 1.14.4;
- Add command completion (from FreeBSD)
- Use EL_SAFEREAD
 1.13 28-Jun-2017  kre branches: 1.13.6;

Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
 1.12 27-Jun-2017  kre Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
 1.11 18-Jun-2011  christos branches: 1.11.36;
PR/45069: Henning Petersen: Use prototypes from builtins.h .
 1.10 07-Aug-2003  agc branches: 1.10.52;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.9 04-May-2003  gmcgarry Add new builtin 'inputrc' which allows keybindings to be redefined
for the current shell. From Arne H Juul in PR#10097.
 1.8 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.7 09-Jul-1999  christos compile with WARNS = 2
 1.6 11-Apr-1997  christos Track $TERM and call the appropriate editline(3) routine to update the
terminal type.
 1.5 25-Jun-1996  christos change sethistsize() to take the history size as an argument so it can
be used directly by the new variable callback functions.
 1.4 11-May-1995  christos branches: 1.4.6;
Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.3 21-Mar-1995  cgd convert to new RCS id conventions.
 1.2 11-Jun-1994  mycroft Add RCS ids.
 1.1 11-May-1994  jtc branches: 1.1.1;
Initial revision
 1.1.1.1 11-May-1994  jtc 44lite code
 1.4.6.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.10.52.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.11.36.1 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.13.6.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.13.6.1 21-Apr-2020  martin Sync with HEAD
 1.14.4.1 02-Aug-2025  perseant Sync with HEAD
 1.21 13-Jul-2024  kre Implement the HISTFILE and HISTAPPEND variables.

See the (newly updated) sh(1) for details.
Also add the -z option to fc (clear history).

None of this exists in SMALL shells.
 1.20 07-Apr-2023  kre branches: 1.20.2;

The great shell trailing whitespace cleanup of 2023...
Inspired by private e-mail comments from mouse@

NFCI.
 1.19 16-Apr-2022  kre Avoid generating error messages implying that user errors are illegal.
 1.18 13-Jul-2018  kre 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.)
 1.17 28-Apr-2013  dholland branches: 1.17.22; 1.17.28; 1.17.30;
Add const.
 1.16 07-Aug-2003  agc branches: 1.16.54; 1.16.60;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.15 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.14 09-Jul-1999  christos compile with WARNS = 2
 1.13 04-Jul-1997  christos Fix compiler warnings.
 1.12 11-Jan-1997  tls kill 'register'
 1.11 16-Oct-1996  christos PR/2808: Kill use of error2() (from FreeBSD)
 1.10 11-May-1995  christos branches: 1.10.6;
Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.9 21-Mar-1995  cgd convert to new RCS id conventions.
 1.8 11-Jun-1994  mycroft Add RCS ids.
 1.7 12-May-1994  jtc Include appropriate header files to bring function prototypes into scope.
 1.6 11-May-1994  jtc sync with 4.4lite
 1.5 04-May-1994  jtc Remove private versions of strchr() & bcopy(); there is nothing wrong with
the versions in our C library.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.10.6.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.16.60.1 23-Jun-2013  tls resync from head
 1.16.54.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.17.30.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.17.30.2 21-Apr-2020  martin Sync with HEAD
 1.17.30.1 10-Jun-2019  christos Sync with HEAD
 1.17.28.1 28-Jul-2018  pgoyette Sync with HEAD
 1.17.22.1 25-Aug-2018  martin Pull up following revision(s) (requested by kre in ticket #988):

bin/sh/parser.c: revision 1.147
bin/sh/var.c: revision 1.70
bin/sh/mystring.c: revision 1.18
bin/sh/options.c: revision 1.53
bin/sh/histedit.c: revision 1.53

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.)
 1.20.2.1 02-Aug-2025  perseant Sync with HEAD
 1.12 13-Jul-2024  kre Implement the HISTFILE and HISTAPPEND variables.

See the (newly updated) sh(1) for details.
Also add the -z option to fc (clear history).

None of this exists in SMALL shells.
 1.11 07-Aug-2003  agc branches: 1.11.98; 1.11.110;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.10 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.9 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.8 21-Mar-1995  cgd convert to new RCS id conventions.
 1.7 11-Jun-1994  mycroft Add RCS ids.
 1.6 11-May-1994  jtc sync with 4.4lite
 1.5 04-May-1994  jtc Remove private versions of strchr() & bcopy(); there is nothing wrong with
the versions in our C library.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.11.110.1 02-Aug-2025  perseant Sync with HEAD
 1.11.98.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.11.98.1 21-Apr-2020  martin Sync with HEAD
 1.14 22-Jun-2018  kre branches: 1.14.2;
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).
 1.13 20-Mar-2012  matt branches: 1.13.26; 1.13.32;
Use C89 function definitions
 1.12 15-Jun-2004  dsl branches: 1.12.48;
Change '\0' to 0 so that the SVR4 'echo' doesn't convert it to a null byte.
Fixes bin/25938
 1.11 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.10 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.9 04-Oct-2002  christos Rename ALIGN to SHELL_ALIGN and simplify macro so that it does not have side
effects, and add double to it, so that it aligns doubles correctly too. This
is just a workaround to fix the sparc64 problem where ALIGN() is now defined
in some include file to be 16 instead of 8. Thanks to martin for debugging this.
 1.8 11-Apr-1997  christos Pointer arithmetic on void * is illegal but gcc does not care.
 1.7 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 11-Jun-1994  mycroft Add RCS ids.
 1.4 11-May-1994  jtc sync with 4.4lite
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.12.48.1 17-Apr-2012  yamt sync with head
 1.13.32.1 25-Jun-2018  pgoyette Sync with HEAD
 1.13.26.1 13-Jul-2018  martin Pull up following revision(s) (requested by kre in ticket #906):

bin/sh/eval.c: revision 1.155
bin/sh/mknodes.sh: revision 1.3
bin/sh/nodes.c.pat: revision 1.14
bin/sh/exec.h: revision 1.27
bin/sh/exec.c: revision 1.52

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).
 1.14.2.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.14.2.1 21-Apr-2020  martin Sync with HEAD
 1.20 22-Nov-2021  kre PR bin/53550

Here we go again... One more time to redo how here docs are
processed (it has been a few years since the last time!)

This is actually a relatively minor change, mostly to timimg
(to just when things happen). Now here docs are expanded at the
same time the "filename" word in a redirect is expanded, rather than
later when the heredoc was being sent to its process. This actually
makes things more consistent - but does break one of the ATF tests
which was testing that we were (effectively) internally inconsistent
in this area.

Not all shells agree on the context in which redirection expansions
should happen, some make any side effects visible to the parent shell
(the majority do) others do the redirection expansions in a subshell
so any side effcts are lost. We used to have a foot in each camp,
with the majority for everything but here docs, and the minority for
here docs. Now we're all the way with LBJ ... (or something like that).
 1.19 16-Nov-2021  kre Fix value of ${LINENO} in "for" commands.

This affects (as best I can tell) only uses of ${LINENO} in PS4
when -x is enabled (and perhaps only when the list contains no
expansions). "for" like "case" (which was already handled) is
special in that it generates trace output before actually executing
any kind of simple command.
 1.18 08-Jun-2017  kre branches: 1.18.6;

Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
 1.17 07-Jun-2017  kre A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)

This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)

The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.

This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).

I would not call the results of this perfect yet, but it is close.
 1.16 07-Jun-2017  kre An initial attempt at implementing LINENO to meet the specs.

Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.

Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)

This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.

POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)

This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.

This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).

Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
 1.15 09-May-2017  kre branches: 1.15.2;

If we are going to permit
! ! pipeline
(And for now the other places where ! is permitted)
we should at least generate the logically correct exit
status:
! ! (exit 5); echo $?
should print 1, not 5. ksh and bosh do it this way - and it makes sense.
bash and the FreeBSD sh echo "5" (as did we until now.)
dash, zsh, yash all enforce the standard syntax, and prohibit this.
 1.14 04-May-2017  kre Implement the ';&' (used instead of ';;') case statement list terminator
which causes fall through the to command list of the following pattern
(wuthout evaluating that pattern). This has been approved for inclusion
in the next major version of the POSIX standard (Issue 8), and is
implemented by most other shells.

Now all form a circle and together attempt to summon the great wizd
in the hopes that his magic spells can transform the poor attempt
at documenting this feature into something rational...
 1.13 26-May-2009  joerg branches: 1.13.38;
Make indentation consistent.
 1.12 22-Aug-2003  agc Move 4 more files from UCB 4 clause to 3 clause licence.

Thanks to Julio Merino for pointing them out.
 1.11 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.10 15-May-2002  christos implement noclobber. From Ben Harris, with minor tweaks from me. Two
unimplemented comments to go. Go Ben!
 1.9 04-Feb-1999  christos PR/4966: Joel Reicher: Implement <> redirections which are documented in
the man page.
 1.8 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.7 21-Mar-1995  cgd convert to new RCS id conventions.
 1.6 14-Jun-1994  jtc From Christos:
1. Fix `-' quoting in [ ] expressions.
2. Fix expansion of variables in redirections
 1.5 11-Jun-1994  mycroft Add RCS ids.
 1.4 11-May-1994  jtc sync with 4.4lite
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.13.38.1 11-May-2017  pgoyette Sync with HEAD
 1.15.2.1 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.18.6.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.18.6.1 21-Apr-2020  martin Sync with HEAD
 1.12 10-Sep-2025  kre PR bin/59646 option.list wsp consistency

Be consistent with use of tab/space in fields used as
sort keys, so versions of sort which might not behave
exactly as we expect should still produce the same results.

XXX pullup -9 -10 -11
 1.11 14-Oct-2024  kre branches: 1.11.2;
Add a -r (version) option to sh

This new -r (or +r) option is for command line use only.
When encountered, the shell simply prints its version info
(such as it has, which isn't much) and exits (immediately).

This allows "funny" uses like
sh -version
the -v and -e options are standard, and processed as normal
(changing nothing, yet, except setting the option). Then the
'r' option is seen, the version info is printed, and the shell
exits. Any remaining "options" (there is no "-o n" option) are
ignored, as are any other args given to the shell.

The string printed (currently) is just "NetBSD shell:" followed
by the value of the NETBSD_SHELL variable (which has been established
already by the time options are processed).
 1.10 18-Sep-2022  kre branches: 1.10.2; 1.10.4;
Add the -l option (aka -o login): be a login shell. Meaningful only on
the command line (with both - and + forms) - overrides the presence (or
otherwise) of a '-' as argv[0][0].

Since this allows any shell to be a login shell (which simply means that
it runs /etc/profile and ~/.profile at shell startup - there are no other
side effects) add a new, always set at startup, variable NBSH_INVOCATION
which has a char string as its value, where each char has a meaning,
more or less related to how the shell was started. See sh(1).
This is intended to allow those startup scripts to tailor their behaviour
to the nature of this particular login shell (it is possible to detect
whether a shell is a login shell merely because of -l, or whether it would
have been anyway, before the -l option was added - and more). The
var could also be used to set different values for $ENV for different
uses of the shell.
 1.9 23-Nov-2018  kre branches: 1.9.2;

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.
 1.8 21-Nov-2017  kre branches: 1.8.2; 1.8.4;
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).
 1.7 19-Nov-2017  kre 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.
 1.6 24-Jul-2017  kre 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.
 1.5 30-Jun-2017  kre Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
 1.4 17-Jun-2017  kre Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
 1.3 07-Jun-2017  kre A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)

This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)

The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.

This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).

I would not call the results of this perfect yet, but it is close.
 1.2 28-May-2017  kre branches: 1.2.2;
Redo mkoptions.sh .. much better this way, now fully automated
option sorting (no longer required option.list to be manually
sorted by long option name) and properly handles conditional
options. Cleaner output format as well.

This allows option.list to be reordered to group related options
together ... also added more comments to it.
 1.1 28-May-2017  kre Arrange for set -o and $- output to be sorted, rather than more
or less random (and becoming worse as more options are added.)
Since the data is known at compile time, sort at compile time,
rather than at run time.
 1.2.2.1 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.8.4.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.8.4.2 21-Apr-2020  martin Sync with HEAD
 1.8.4.1 10-Jun-2019  christos Sync with HEAD
 1.8.2.1 26-Nov-2018  pgoyette Sync with HEAD, resolve a couple of conflicts
 1.9.2.1 01-Oct-2025  martin Pull up following revision(s) (requested by kre in ticket #1970):

bin/sh/option.list: revision 1.12
bin/sh/mkoptions.sh: revision 1.6
bin/sh/mkoptions.sh: revision 1.7
bin/sh/mkoptions.sh: revision 1.8
bin/sh/mkoptions.sh: revision 1.9

From Jan-Benedict Glaw:
Fix a redirection and prepare a stable sort for upper-/lowercase
option letters

This script is a mess, I strongly believe that it should be rewritten.
However, I'm not 100% sure why it was invented in the first place
(come on, the generated header file isn't _that_ complicated that
it couldn't be sanely managed by hand!), but let's fix the sorting
order by using LC_ALL=C.

Also add a few 'X' to the `mktemp` template to make non-BSD
implementations happy. As a bonus, actually *use* the initial `sed`
output instead of throwing it away by piping it into `sort` with
also connecting `sort`'s stdin with the original input file...

Redo the mktemp(1) part - some mktemp's (including ours) require the
XXXX's to be at the end of the name (like mk*temp(3)) so however well
it will work with mktemp implementations which allow the X's to be
anywhere in the final component of the name, it will work just as
well on them with the X's at the end.

But we don't normally need all of that mess - knowing which temp
file is which is useful only when debugging the script, and that's
(mostly) long done. So, in normal uses now just use $(mktemp) and
allow mktemp to pick its own name - we don't need to know what it is.
Every mktemp(1) supports that mode of operation.
Bug when debugging the script (which for current purposes will be
taken to be when the -x flag is passed to the shell running it, to
trace what it does) then we will make the temp files have names we
can recognise (and in that case, also don't delete them when done).

While here, check for mktemp(1) failing, and abort if that
happens (we assume that if it fails it will write an error
message to stderr, so the script does not need to.)

As for the purpose of the script ... of course the header file
generated (or an equivalent elsewhere) could be generated and
maintained by hand, but why would anyone want to do all that
work when software can do it for us, and do it correctly without
human thought?

This also allows the options in the master list (option.list) to be
arranged in a way that is meaningful for them, unrelated to the order
the shell needs to have them in (or rearrange them to be at run time)
and have that order shuffled however is convenient. Currently all
the posix standard options are first, then the "hybrid" options, and
finally the local ones for this shell. Currently "pipefail" is in the
final set, but once the next posix version is published, that will
become a standard option, and get moved in the list - the shell won't
even notice as this script puts the options into shell desired order.

Be more explicit with sort fields to produce consistent results with gnu
sort (Jan-Benedict Glaw)

Allow '+' to be specified as the "one char option name" for long options
that don't have a 1 char equivalent, but do want to become a member of
an option set. The '+' is otherwise ignored. This is similar to '-'
in that position, except that skips past the option set field, and is
followed directly by the default value, '+' does not do that.
This currently changes nothing, as there are no current options that
use it (or would want to).

PR bin/59646 option.list wsp consistency
Be consistent with use of tab/space in fields used as
sort keys, so versions of sort which might not behave
exactly as we expect should still produce the same results.
 1.10.4.1 02-Aug-2025  perseant Sync with HEAD
 1.10.2.1 01-Oct-2025  martin Pull up following revision(s) (requested by kre in ticket #1165):

bin/sh/option.list: revision 1.12
bin/sh/mkoptions.sh: revision 1.6
bin/sh/mkoptions.sh: revision 1.7
bin/sh/mkoptions.sh: revision 1.8
bin/sh/mkoptions.sh: revision 1.9

From Jan-Benedict Glaw:
Fix a redirection and prepare a stable sort for upper-/lowercase
option letters

This script is a mess, I strongly believe that it should be rewritten.
However, I'm not 100% sure why it was invented in the first place
(come on, the generated header file isn't _that_ complicated that
it couldn't be sanely managed by hand!), but let's fix the sorting
order by using LC_ALL=C.

Also add a few 'X' to the `mktemp` template to make non-BSD
implementations happy. As a bonus, actually *use* the initial `sed`
output instead of throwing it away by piping it into `sort` with
also connecting `sort`'s stdin with the original input file...

Redo the mktemp(1) part - some mktemp's (including ours) require the
XXXX's to be at the end of the name (like mk*temp(3)) so however well
it will work with mktemp implementations which allow the X's to be
anywhere in the final component of the name, it will work just as
well on them with the X's at the end.

But we don't normally need all of that mess - knowing which temp
file is which is useful only when debugging the script, and that's
(mostly) long done. So, in normal uses now just use $(mktemp) and
allow mktemp to pick its own name - we don't need to know what it is.
Every mktemp(1) supports that mode of operation.
Bug when debugging the script (which for current purposes will be
taken to be when the -x flag is passed to the shell running it, to
trace what it does) then we will make the temp files have names we
can recognise (and in that case, also don't delete them when done).

While here, check for mktemp(1) failing, and abort if that
happens (we assume that if it fails it will write an error
message to stderr, so the script does not need to.)

As for the purpose of the script ... of course the header file
generated (or an equivalent elsewhere) could be generated and
maintained by hand, but why would anyone want to do all that
work when software can do it for us, and do it correctly without
human thought?

This also allows the options in the master list (option.list) to be
arranged in a way that is meaningful for them, unrelated to the order
the shell needs to have them in (or rearrange them to be at run time)
and have that order shuffled however is convenient. Currently all
the posix standard options are first, then the "hybrid" options, and
finally the local ones for this shell. Currently "pipefail" is in the
final set, but once the next posix version is published, that will
become a standard option, and get moved in the list - the shell won't
even notice as this script puts the options into shell desired order.

Be more explicit with sort fields to produce consistent results with gnu
sort (Jan-Benedict Glaw)

Allow '+' to be specified as the "one char option name" for long options
that don't have a 1 char equivalent, but do want to become a member of
an option set. The '+' is otherwise ignored. This is similar to '-'
in that position, except that skips past the option set field, and is
followed directly by the default value, '+' does not do that.
This currently changes nothing, as there are no current options that
use it (or would want to).

PR bin/59646 option.list wsp consistency
Be consistent with use of tab/space in fields used as
sort keys, so versions of sort which might not behave
exactly as we expect should still produce the same results.
 1.11.2.1 01-Oct-2025  martin Pull up following revision(s) (requested by kre in ticket #37):

bin/sh/option.list: revision 1.12

PR bin/59646 option.list wsp consistency

Be consistent with use of tab/space in fields used as
sort keys, so versions of sort which might not behave
exactly as we expect should still produce the same results.
 1.62 14-Oct-2024  kre 80 column police (for the recent -r change). NFC.
 1.61 14-Oct-2024  kre Add a -r (version) option to sh

This new -r (or +r) option is for command line use only.
When encountered, the shell simply prints its version info
(such as it has, which isn't much) and exits (immediately).

This allows "funny" uses like
sh -version
the -v and -e options are standard, and processed as normal
(changing nothing, yet, except setting the option). Then the
'r' option is seen, the version info is printed, and the shell
exits. Any remaining "options" (there is no "-o n" option) are
ignored, as are any other args given to the shell.

The string printed (currently) is just "NetBSD shell:" followed
by the value of the NETBSD_SHELL variable (which has been established
already by the time options are processed).
 1.60 21-Sep-2024  kre In !TINYPROG versions of sh, make the builtin "shift" builtin command
perform a rotate instead or shift if given a numeric arg (which is
otherwise an error).

"set -- a b c; shift -1; echo $*" will echo "c a b".

While here, make the shift builtin comply with POSIX, and accept
(and ignore) a '--' arg before the shift (or rotate) count.

Document the variant of shift in sh(1)

Adapt the shell test for shift to not expect "shift -1" to be an
error, test that rotate works as expected, and include some tests
that use the (useless, but required) "--" arg.
 1.59 12-Jul-2024  kre Improve safety in var imports from the environment.

Add a new var flag VUNSAFE - set on all vars imported from the environment.

Add setvareqsafe() (which is to setvareq() as setvarsafe() is to setvar())
and use that instead of setvareq() when processing the environment, so
errors don't cause the shell to abort. Use VUNSAFE in that call.

Add flags arguments to all var callback functions which are used when setting
variables, and pass the flags given to the setvar*() functions to those
functions, so they can act differently in different situations (if desired).
Most of them just ignore the flags.

When unsetting a variable, call setvar() to clear things (and call the
callback function) both when the variable had a value which needs to be freed,
and when unsetting a variable which wasn't unset previously, so the VUNSET
flag can be seen by that callback func.

When setting HISTSIZE, use the flags passed to determine whether to ignore
bad values (if VUNSAFE) or treat them as an error. This replaces the
earlier temporary hack to always ignore bad data there (histedit.c 1.68).

Miscellaneous associated minor changes.

These changes should largely be invisible in normal use.
 1.58 18-Sep-2022  kre branches: 1.58.4;
Add the -l option (aka -o login): be a login shell. Meaningful only on
the command line (with both - and + forms) - overrides the presence (or
otherwise) of a '-' as argv[0][0].

Since this allows any shell to be a login shell (which simply means that
it runs /etc/profile and ~/.profile at shell startup - there are no other
side effects) add a new, always set at startup, variable NBSH_INVOCATION
which has a char string as its value, where each char has a meaning,
more or less related to how the shell was started. See sh(1).
This is intended to allow those startup scripts to tailor their behaviour
to the nature of this particular login shell (it is possible to detect
whether a shell is a login shell merely because of -l, or whether it would
have been anyway, before the -l option was added - and more). The
var could also be used to set different values for $ENV for different
uses of the shell.
 1.57 16-Apr-2022  kre Avoid generating error messages implying that user errors are illegal.
 1.56 26-Oct-2021  kre PR bin/56464

After almost 30 years, finally do the right thing and read $HOME/.profile
rather than .profile in the initial directory (it was that way in version
1.1 ...) All other ash descendants seem to have fixed this long ago.

While here, copy a feature from FreeBSD which allows "set +p" (if a
shell run by a setuid process with the -p flag is privileged) to reset
the privileges. Once done (the set +p) it cannot be undone (a later
set -p sets the 'p' flag, but that's all it does) - that just becomes a
one bit storage location.

We do this, as (also copying from FreeBSD, and because it is the right
thing to do) we don't run .profile in a privileged shell - FreeBSD run
/etc/suid_profile in that case (not a good name, it also applies to setgid
shells) but I see no real need for that, we run /etc/profile in any case,
anything that would go in /etc/suid_profile can just go in /etc/profile
instead (with suitable guards so the commands only run in priv'd shells).

One or two minor DEBUG mode changes (notably having priv'd shells identify
themselves in the DEBUG trace) and sh.1 changes with doc of the "set +p"
change, the effect that has on $PSc and a few other wording tweaks.

XXX pullup -9 (not -8, this isn't worth it for the short lifetime
that has left - if it took 28+ years for anyone to notice this, it
cannot be having all that much effect).
 1.55 05-Feb-2020  kre Oops, the previous didn't do what was promised. Rather that ignoring
just "--" for exec & "." it ignored any first arg starting '-'.
Do it properly.
 1.54 04-Feb-2020  kre After bug report 262 (from 2010)
https://austingroupbugs.net/view.php?id=252
the Austin Group decided to require processing of "--" by the "."
and "exec" commands to solve a problem where some shells did
option processing for those commands (permitted) and others did
not (also permitted) which left no safe way to process a file
with a name beginning with "-".

This has finally made its way into what will be the next version of
the POSIX standard.

Since this shell did no option processing at all for those commands,
we need to update. This is that update.

The sole effect is that a "--" 'option' (to "." or "exec") is ignored.
This means that if you want to use "--" as the arg to one of those
commands, it needs to be given twice ". -- --". Apart from that there
should be no difference at all (though the "--" can now be used in other
situations, where we did not require it before, and still do not).
 1.53 13-Jul-2018  kre branches: 1.53.2;
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.)
 1.52 21-Nov-2017  kre branches: 1.52.2; 1.52.4;
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).
 1.51 19-Nov-2017  kre 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.
 1.50 24-Jul-2017  kre 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.
 1.49 29-May-2017  kre branches: 1.49.2;

More DEBUG mode changes. As usual, read the source if you care.
 1.48 18-May-2017  kre Command line, and "set" command options processing cleanup.

sh +c "command string" no longer works (it must be -c)
sh +o and sh -o no longer work (if you could call what they did
before working.) nb: this is without an option name.
-ooo Opt1 Opt2 Opt3 no longer works (set & cmd line), this should be
-o Opt1 -o Opt2 -o Opt3 (same with +ooo of course).
-oOpt is now supported - option value (name of option in
this case) immediately following -o (or +o).
(as with other commands that use std opt parsing)
Both set comamnd and command line.

In addition, the output from "set +o" has shrunk dramatically, by borrowing
a trick from ksh93 (but implemented in a more traditional syntax).
"set +o" is required to produce a command (or commands) which when executed
later, will return all options to the state they were in when "set +o"
was done. Previously that was done by generating a set command, with
every option listed (set -o opt +o other-opt ...) to set them all back
to their current setings. Now we have a new "magic option" ("default")
which sets all options to their default values, so now set +o output
need only be "set -o default -o changed-opt ..." (only the options that
have been changed from their default values need be explicitly mentioned.)
The definition of "default value" for this is the value the shell set the
option to, after startup, after processing the command line (with any
flags, or -o option type settings), but before beginning processing any
user input (incuding startup files, like $ENV etc).

Anyone can execute "set -o default" of course, but only from a "set"
command (it makes no sense at all as a -o option to sh). This also
causes "set +o" to be slightly more useful as a general command, as
ignoring the "set -o default" part of the result, it lists just those
options that have been altered after sh startup. There is no +o default.
There isn't an option called "default" at all...

This causes some of the commented out text from sh.1 to become uncommented.
 1.47 15-May-2017  kre DEBUG mode shell update (changes nothing for shells which are not
compiled for DEBUG.)

Add debug builtin command, and corresponding -D command line option.
As usual, for DEBUG related stuff, read the source for info, that's
all there is about this.

This completes the infrastructure changes for the updated DEBUG TRACE
mechanism, so now converting the rest of the shell's internal tracing
can happen as desired - piecemeal.
 1.46 31-Mar-2016  christos branches: 1.46.6;
Implement the NETBSD_SHELL readonly unexportable unimportable
variable (with its current value set at 20160401) as discussed on
current-users and tech-userlevel. This also includes the necessary
support to implement it properly (particularly the unexportable
part) and adds options to the export command to support unexportable
variables. Also implement the "posix" option (no single letter
equivalent) which gets its default value from whether or not
POSIXLY_CORRECT is set in the environment when the shell starts
(but can be changed just like any other option using -o and +o on
the command line, or the set builtin command.) While there, fix
all uses of options so it is possible to have options that have a
short (one char) name, and no long name, just as it has been possible
to have options with a long name and no short name, though there
are currently none (with no long name). For now, the only use of
the posix option is to control whether ${ENV} is read at startup
by a non-interactive shell, so changing it with set is not usful
- that might change in the future. (from kre@)
 1.45 08-Mar-2016  christos PR bin/50896: make shift with more than 1 arg give a usage message, from kre
 1.44 24-Feb-2016  christos If we don't have shared address space vfork fail back to using fork since
we are depending on the shared address space feature (from kre)
 1.43 20-Mar-2012  matt Use C89 function definitions
 1.42 18-Jun-2011  christos branches: 1.42.2;
PR/45069: Henning Petersen: Use prototypes from builtins.h .
 1.41 18-Jan-2009  lukem branches: 1.41.6;
fix -Wsign-compare issues
 1.40 13-Dec-2005  dsl TOG require that 'set +o' output the options in a form suitable for
restoring them - make it so.
 1.39 15-Jul-2005  christos If we have a script and we are interactive, set interactive to 2. This
allows us to run scripts with -i without printing the prompt like the
other shells do.
 1.38 20-Mar-2005  dsl Add 'continue' as body of empty loop.
 1.37 30-Oct-2004  christos Pass WARNS=3
 1.36 05-Jan-2004  jmmv Homogenize usage messages: make the 'usage' word all lowercase, as this seems
to be the most common practice in our tree.
 1.35 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.34 15-Apr-2003  itojun %d is 12 chars, not 10 chars. hinted by deraadt
 1.33 22-Jan-2003  dsl Support command -p, -v and -V as posix
Stop temporary PATH assigments messing up hash table
Fix sh -c -e "echo $0 $*" -a x (as posix)
(agreed by christos)
 1.32 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.31 26-Feb-2001  wiz Fix command name in error message for 'sh nonexistingfile'.
 1.30 04-Feb-2001  christos remove redundant declarations and nexted externs.
 1.29 09-Jul-1999  christos compile with WARNS = 2
 1.28 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.27 28-Jul-1998  mycroft Delint.
 1.26 02-May-1998  christos PR/5315: Dan Winship: options parsing code should print errors to stderr.
 1.25 04-Jul-1997  christos branches: 1.25.2;
Fix compiler warnings.
 1.24 14-Mar-1997  christos NO_HISTORY->SMALL
 1.23 25-Feb-1997  christos Fix PR/3258 sh -c 'echo $0' causes segmentation fault.
 1.22 11-Jan-1997  tls kill 'register'
 1.21 15-Dec-1996  christos Getopts bugs again (I'll never get this right).
- make sure that OPTIND is set correctly even in the absence of options.
- don't keep stale state between getopts calls.
 1.20 24-Nov-1996  christos Fix bug in getopts code where the argument list got truncated.
From Todd Miller.
 1.19 06-Nov-1996  christos Fix miscellaneous getopts problems:
- the 3 argument version of getopts would not reset properly
- OPTARG did not get cleared after a non argument option was found
- OPTIND was not set properly after a non argument option.
 1.18 02-Nov-1996  christos Fix problems that gcc -Wall found (from Todd Miller, OpenBSD)
 1.17 16-Oct-1996  christos PR/2808: POSIX 1003.2: first arg after -c cmd is $0, remainder $1...
(from FreeBSD)
 1.16 25-Jun-1996  christos - Add getoptsreset to be used as a callback function when OPTIND is set.
This is used to implement the POSIX behavior when OPTIND=1
- Call setvarsafe instead of setvar. If one ran
"getopts optstr badvariable"
where badvariable contained an illegal variable name, there was no way
to recover, since setvar() would longjmp on the error.
 1.15 04-Jun-1996  christos getopts fixes:

1. OPTIND value was not computed correctly when the argument was part of the
option string (i.e. for "l:" "-l 1" was working "-l1" was not). (PR/2505).
2. OPTARG was not being unset in case of errors [in the non POSIX error case].
3. optvar could be set to random values.
4. Option string starting with a : was not treated specially as POSIX
specifies (if the option string starts with a :, then there is no
error printed when there are missing option arguments or illegal options,
and OPTARG and optvar are being set specially).
5. Implemented getopts "opts" optvar [arg]. The optional argument case
was not implemented.

To do:
- what does Posix say about resetting the getopts state? Bash does it
by setting OPTIND=0; is that correct? Should we be doing the same thing?
- should we be using getopt(3) for everything internal to the shell? Is that
feasible because we might need to handle multiple invocations at once.
 1.14 11-May-1995  christos branches: 1.14.6;
Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.13 26-Mar-1995  christos Reverted to set - turning off -x and -v. Charles corrected me... It is part
of POSIX.
 1.12 25-Mar-1995  christos set -
does not anymore clear the -x and -v flags. This was incompatible with
all other bourne shell implementations.
 1.11 21-Mar-1995  cgd convert to new RCS id conventions.
 1.10 05-Dec-1994  cgd clean up further. more patches from Jim Jegers
 1.9 04-Dec-1994  cgd from James Jegers <jimj@miller.cs.uwm.edu>: quiet -Wall, and squelch
some of the worst style errors.
 1.8 11-Jun-1994  mycroft Add RCS ids.
 1.7 14-May-1994  cgd add back in support for building w/o obj dir. also, add NO_HISTORY
define, which (if you invoke mkbuiltins properly) gets you a sh w/o
history of command line editing (for floppy sh).
 1.6 12-May-1994  jtc Include appropriate header files to bring function prototypes into scope.
 1.5 11-May-1994  jtc sync with 4.4lite
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.14.6.2 04-Mar-1997  mycroft Pull up latest sh(1). Fixes yet more bugs.
 1.14.6.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.25.2.1 08-May-1998  mycroft Sync with trunk, per request of christos.
 1.41.6.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.42.2.1 17-Apr-2012  yamt sync with head
 1.46.6.1 19-May-2017  pgoyette Resolve conflicts from previous merge (all resulting from $NetBSD
keywork expansion)
 1.49.2.1 25-Aug-2018  martin Pull up following revision(s) (requested by kre in ticket #988):

bin/sh/parser.c: revision 1.147
bin/sh/var.c: revision 1.70
bin/sh/mystring.c: revision 1.18
bin/sh/options.c: revision 1.53
bin/sh/histedit.c: revision 1.53

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.)
 1.52.4.4 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.52.4.3 21-Apr-2020  martin Sync with HEAD
 1.52.4.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.52.4.1 10-Jun-2019  christos Sync with HEAD
 1.52.2.1 28-Jul-2018  pgoyette Sync with HEAD
 1.53.2.1 06-Nov-2021  martin Pull up following revision(s) (requested by kre in ticket #1371):

bin/sh/main.c: revision 1.87
bin/sh/main.c: revision 1.88
bin/sh/memalloc.h: revision 1.20
bin/sh/sh.1: revision 1.235
bin/sh/memalloc.c: revision 1.34
bin/sh/memalloc.c: revision 1.35
bin/sh/memalloc.h: revision 1.19
bin/sh/shell.h: revision 1.31
bin/sh/options.c: revision 1.56

PR bin/56464

After almost 30 years, finally do the right thing and read $HOME/.profile
rather than .profile in the initial directory (it was that way in version
1.1 ...) All other ash descendants seem to have fixed this long ago.
While here, copy a feature from FreeBSD which allows "set +p" (if a
shell run by a setuid process with the -p flag is privileged) to reset
the privileges. Once done (the set +p) it cannot be undone (a later
set -p sets the 'p' flag, but that's all it does) - that just becomes a
one bit storage location.

We do this, as (also copying from FreeBSD, and because it is the right
thing to do) we don't run .profile in a privileged shell - FreeBSD run
/etc/suid_profile in that case (not a good name, it also applies to setgid
shells) but I see no real need for that, we run /etc/profile in any case,
anything that would go in /etc/suid_profile can just go in /etc/profile
instead (with suitable guards so the commands only run in priv'd shells).

One or two minor DEBUG mode changes (notably having priv'd shells identify
themselves in the DEBUG trace) and sh.1 changes with doc of the "set +p"
change, the effect that has on $PSc and a few other wording tweaks.

XXX pullup -9 (not -8, this isn't worth it for the short lifetime
that has left - if it took 28+ years for anyone to notice this, it
cannot be having all that much effect).

Use a type-correct end marker for strstrcat() rather than NULL, as
for a function with unknown number & types of args, the compiler isn't
able to automatically convert to the correct type. Issue pointed out
in off list e-mail by Rolland Illig ... Thanks.

The first arg (pointer to where to put length of result) is of a known
type, so doesn't have the same issue - we can keep using NULL for that
one when the length isn't needed.
Also, make sure to return a correctly null terminated null string in
the (absurd) case that there are no non-null args to strstrcat() (though
there are much better ways to generate "" on the stack). Since there is
currently just one call in the code, and it has real string args, this
isn't an issue for now, but who knows, some day.

NFCI - if there is any real change, then it is a change that is required.

XXX pullup -9 (together with the previous changes)
 1.58.4.1 02-Aug-2025  perseant Sync with HEAD
 1.28 12-Jul-2024  kre Improve safety in var imports from the environment.

Add a new var flag VUNSAFE - set on all vars imported from the environment.

Add setvareqsafe() (which is to setvareq() as setvarsafe() is to setvar())
and use that instead of setvareq() when processing the environment, so
errors don't cause the shell to abort. Use VUNSAFE in that call.

Add flags arguments to all var callback functions which are used when setting
variables, and pass the flags given to the setvar*() functions to those
functions, so they can act differently in different situations (if desired).
Most of them just ignore the flags.

When unsetting a variable, call setvar() to clear things (and call the
callback function) both when the variable had a value which needs to be freed,
and when unsetting a variable which wasn't unset previously, so the VUNSET
flag can be seen by that callback func.

When setting HISTSIZE, use the flags passed to determine whether to ignore
bad values (if VUNSAFE) or treat them as an error. This replaces the
earlier temporary hack to always ignore bad data there (histedit.c 1.68).

Miscellaneous associated minor changes.

These changes should largely be invisible in normal use.
 1.27 28-May-2017  kre branches: 1.27.10; 1.27.22;
Arrange for set -o and $- output to be sorted, rather than more
or less random (and becoming worse as more options are added.)
Since the data is known at compile time, sort at compile time,
rather than at run time.
 1.26 18-May-2017  kre Command line, and "set" command options processing cleanup.

sh +c "command string" no longer works (it must be -c)
sh +o and sh -o no longer work (if you could call what they did
before working.) nb: this is without an option name.
-ooo Opt1 Opt2 Opt3 no longer works (set & cmd line), this should be
-o Opt1 -o Opt2 -o Opt3 (same with +ooo of course).
-oOpt is now supported - option value (name of option in
this case) immediately following -o (or +o).
(as with other commands that use std opt parsing)
Both set comamnd and command line.

In addition, the output from "set +o" has shrunk dramatically, by borrowing
a trick from ksh93 (but implemented in a more traditional syntax).
"set +o" is required to produce a command (or commands) which when executed
later, will return all options to the state they were in when "set +o"
was done. Previously that was done by generating a set command, with
every option listed (set -o opt +o other-opt ...) to set them all back
to their current setings. Now we have a new "magic option" ("default")
which sets all options to their default values, so now set +o output
need only be "set -o default -o changed-opt ..." (only the options that
have been changed from their default values need be explicitly mentioned.)
The definition of "default value" for this is the value the shell set the
option to, after startup, after processing the command line (with any
flags, or -o option type settings), but before beginning processing any
user input (incuding startup files, like $ENV etc).

Anyone can execute "set -o default" of course, but only from a "set"
command (it makes no sense at all as a -o option to sh). This also
causes "set +o" to be slightly more useful as a general command, as
ignoring the "set -o default" part of the result, it lists just those
options that have been altered after sh startup. There is no +o default.
There isn't an option called "default" at all...

This causes some of the commented out text from sh.1 to become uncommented.
 1.25 31-Mar-2016  christos branches: 1.25.6;
Implement the NETBSD_SHELL readonly unexportable unimportable
variable (with its current value set at 20160401) as discussed on
current-users and tech-userlevel. This also includes the necessary
support to implement it properly (particularly the unexportable
part) and adds options to the export command to support unexportable
variables. Also implement the "posix" option (no single letter
equivalent) which gets its default value from whether or not
POSIXLY_CORRECT is set in the environment when the shell starts
(but can be changed just like any other option using -o and +o on
the command line, or the set builtin command.) While there, fix
all uses of options so it is possible to have options that have a
short (one char) name, and no long name, just as it has been possible
to have options with a long name and no short name, though there
are currently none (with no long name). For now, the only use of
the posix option is to control whether ${ENV} is read at startup
by a non-interactive shell, so changing it with set is not usful
- that might change in the future. (from kre@)
 1.24 23-Feb-2016  christos name the long option after the ksh name (trackall) to indicate the intended
posix behavior not the bash one.
 1.23 23-Feb-2016  christos update that -n is implemented and add -h using the long name that bash
uses. (from kre, long name from me)
 1.22 26-May-2015  christos Drop privileges when executed set{u,g}id unless -p is specified like other
shells do to avoid system() and popen() abuse.
 1.21 02-Jan-2015  christos Define an undocumented -F option to only use fork instead of vfork for
debugging purposes.
 1.20 18-Jun-2011  christos PR/45069: Henning Petersen: Use prototypes from builtins.h .
 1.19 13-Dec-2005  dsl branches: 1.19.40;
TOG require that 'set +o' output the options in a form suitable for
restoring them - make it so.
 1.18 07-May-2005  dsl If 'set -o tabcomplete' it set, then bind <tab> to the libedit filename
completion function.
Note that the libedit code will probably want fine-tuning!
While editing the man page, add a note that non-whitespace IFS chars are
terminators and can generate null arguments.
 1.17 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.16 22-Jan-2003  dsl Support command -p, -v and -V as posix
Stop temporary PATH assigments messing up hash table
Fix sh -c -e "echo $0 $*" -a x (as posix)
(agreed by christos)
 1.15 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.14 04-Feb-2001  christos remove redundant declarations and nexted externs.
 1.13 09-Jul-1999  christos compile with WARNS = 2
 1.12 04-Feb-1999  cjs Add -q option, which when used with -v and/or -x, turns off the tracing
during the execution of /etc/profile, .profile and $ENV.
 1.11 06-Nov-1996  christos Fix miscellaneous getopts problems:
- the 3 argument version of getopts would not reset properly
- OPTARG did not get cleared after a non argument option was found
- OPTIND was not set properly after a non argument option.
 1.10 16-Oct-1996  christos PR/2808: Remove trailing whitespace (from FreeBSD)
 1.9 25-Jun-1996  christos - Add getoptsreset to be used as a callback function when OPTIND is set.
This is used to implement the POSIX behavior when OPTIND=1
- Call setvarsafe instead of setvar. If one ran
"getopts optstr badvariable"
where badvariable contained an illegal variable name, there was no way
to recover, since setvar() would longjmp on the error.
 1.8 11-May-1995  christos branches: 1.8.6;
Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.7 21-Mar-1995  cgd convert to new RCS id conventions.
 1.6 11-Jun-1994  mycroft Add RCS ids.
 1.5 11-May-1994  jtc sync with 4.4lite
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.8.6.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.19.40.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.25.6.1 19-May-2017  pgoyette Resolve conflicts from previous merge (all resulting from $NetBSD
keywork expansion)
 1.27.22.1 02-Aug-2025  perseant Sync with HEAD
 1.27.10.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.27.10.1 21-Apr-2020  martin Sync with HEAD
 1.42 03-Oct-2024  rillig bin: fix lint warning "effectively discards 'const'"

For example: src/bin/ed/io.c(339): warning: call to 'strchr' effectively
discards 'const' from argument [346]

No binary change.
 1.41 07-Apr-2023  kre branches: 1.41.2;

The great shell trailing whitespace cleanup of 2023...
Inspired by private e-mail comments from mouse@

NFCI.
 1.40 21-Nov-2017  kre branches: 1.40.4;
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).
 1.39 19-Nov-2017  kre 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.
 1.38 19-Nov-2017  kre 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.
 1.37 16-Nov-2017  kre 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.
 1.36 18-May-2017  kre branches: 1.36.2;

Added comma and plus to the "don't need quoting" set. This affects
output from "sh -x" only (tracing execution), not quoting + is better,
as it makes tracing commands with + and - options, or numbers, more
consistent.

Also one minor white space change (excess indentation removed).
 1.35 12-Mar-2016  christos branches: 1.35.6;
Improve quoting in the output from sh -x - use less unnecessary
quotes ('_' and '.' do not need quoting) and never quote the '=' in
an assignment (or it would not be one.) From kre, with some refactoring
to be blamed to me.
 1.34 28-Feb-2016  christos Bug fixes to handling of unterminated here documents
(they should be, and now are, a syntax error), and
miscellaneous other minor cleanups. (from kre)
 1.33 30-Aug-2010  christos dprintf is claimed by posix.
 1.32 19-Jul-2010  joerg Don't quite `+', `-' and `/' in set -x output.
 1.31 31-Oct-2008  christos show better quoting output for sh -x, from Aleksey Cheusov
 1.30 12-Oct-2008  dholland output.c output.h: expose OUTPUT_ERR (flag for an exposed flags variable)
bltin.h: support ferror()
echo.c: use ferror() to fail on output write errors

Another piece of PR bin/39574.
 1.29 17-Mar-2006  rumble branches: 1.29.4;
Handle asprintf failing to allocate.
 1.28 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.27 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.26 25-May-2002  wiz __STDC__ is always defined on NetBSD, so remove #ifdef __STDC__ (and
unnecessary #else cases).
 1.25 09-Apr-2002  thorpej Move the declaration of digit[] into the #ifdef'd code block that
uses it. Prevents a warning from gcc 3.2.
 1.24 24-Sep-2001  wiz va_{start,end} audit:
Make sure that each va_start has one and only one matching va_end,
especially in error cases.
If the va_list is used multiple times, do multiple va_starts/va_ends.
If a function gets va_list as argument, don't let it use va_end (since
it's the callers responsibility).

Improved by comments from enami and christos -- thanks!

Heimdal/krb4/KAME changes already fed back, rest to follow.

Inspired by, but not not based on, OpenBSD.
 1.23 07-Jan-2001  lukem if HAVE_VASPRINTF (set ifdef BSD4_4), use vasprintf() instead of homegrown
code in doformat(). results in slightly smaller /bin/sh. idea suggested by
Witold J. Wnuk <witek@pd37.warszawa.sdi.tpnet.pl>, approved by christos.
 1.22 07-Jan-2001  lukem support %ll (as synonym for %q) in doformat(), since my changes to %q -> %ll
broke this :/
problem reported in private email by Witold J. Wnuk.
<witek@pd37.warszawa.sdi.tpnet.pl>.
 1.21 31-Jan-1998  christos - Add gcc attributes to printf like functions
- Fix format strings to be const
- Fix buffer length to be size_t
- Add %p format
- Avoid code duplication in varargs/stdarg cases
 1.20 21-Jan-1998  christos BSD4_4 is a standard symbol in <sys/param.h>; make sure that files
that need this defined, include <sys/param.h> and don't define it in
the Makefile. Add a comment to that effect.
 1.19 04-Jul-1997  christos branches: 1.19.2;
Fix compiler warnings.
 1.18 11-Apr-1997  christos Fix so that this compiles on systems without quads and remove the double
inclusion of varargs/stdarg
 1.17 11-Jan-1997  tls kill 'register'
 1.16 16-Oct-1996  christos No need to have 2 declarations for the same constant string, one for K&R and
one for STDC...
 1.15 14-Sep-1995  jtc branches: 1.15.6;
Handle %q format directive; from Mike Long (PR #1454).
 1.14 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.13 21-Mar-1995  cgd convert to new RCS id conventions.
 1.12 23-Dec-1994  cgd pull prototypes into scope for string functions.
 1.11 05-Dec-1994  cgd clean up further. more patches from Jim Jegers
 1.10 11-Jun-1994  mycroft Add RCS ids.
 1.9 12-May-1994  jtc Include appropriate header files to bring function prototypes into scope.
 1.8 12-May-1994  jtc add const qualifier to get rid of compilation warnings.
 1.7 11-May-1994  jtc sync with 4.4lite
 1.6 04-May-1994  jtc Added const qualifier to char * function arguments to eliminate
compiler warning.
 1.5 05-Sep-1993  mycroft From Vincent Broman:
error.c: Add , in initializer list if ENOLINK defined.
output.c: Gratuitously remove extra ;.
mknodes.c: main() should return a value.
Not from Vincent Broman:
mknodes.c: main() should return an error condition when approriate, too.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.15.6.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.19.2.1 08-May-1998  mycroft Sync with trunk, per request of christos.
 1.29.4.1 18-Nov-2008  bouyer Pull up following revision(s) (requested by dholland in ticket #1232):
bin/echo/echo.c: revision 1.18
bin/sh/bltin/bltin.h: revision 1.13
bin/sh/bltin/echo.c: revision 1.14
bin/sh/output.c: revision 1.30
bin/sh/output.h: revision 1.19
Check ferror(stdout) and exit(1) if there's been a problem.
Fixes PR bin/39574 from Ed Ravin, but with a simpler patch.
output.c output.h: expose OUTPUT_ERR (flag for an exposed flags variable)
bltin.h: support ferror()
echo.c: use ferror() to fail on output write errors
Another piece of PR bin/39574.
 1.35.6.1 19-May-2017  pgoyette Resolve conflicts from previous merge (all resulting from $NetBSD
keywork expansion)
 1.36.2.1 23-Nov-2017  martin Pull up following revision(s) (requested by kre in ticket #384):
bin/sh/output.c: revision 1.37
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.
 1.40.4.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.40.4.1 21-Apr-2020  martin Sync with HEAD
 1.41.2.1 02-Aug-2025  perseant Sync with HEAD
 1.28 10-Nov-2021  kre Add a couple of macro definitions for dealing with shell I/O errors.
(Macros akin to stdio's ferror() and clearerr()).

NFC: the macros are defined, but not yet used anywhere. Uses coming soonish.
 1.27 21-Nov-2017  kre branches: 1.27.4;
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).
 1.26 19-Nov-2017  kre 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.
 1.25 19-Nov-2017  kre 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.
 1.24 15-Mar-2012  joerg Add __printflike attribution to use vprintf and friends with an argument
as format string.
 1.23 23-Aug-2011  christos branches: 1.23.2;
add more gcc printf format attributes
 1.22 29-Oct-2010  stacktic Make sh build in debug mode
 1.21 30-Aug-2010  christos dprintf is claimed by posix.
 1.20 31-Oct-2008  christos show better quoting output for sh -x, from Aleksey Cheusov
 1.19 12-Oct-2008  dholland output.c output.h: expose OUTPUT_ERR (flag for an exposed flags variable)
bltin.h: support ferror()
echo.c: use ferror() to fail on output write errors

Another piece of PR bin/39574.
 1.18 29-Mar-2008  apb Remove trailing ';' in definitions of out1c and out2c macros.
 1.17 07-Aug-2003  agc branches: 1.17.16;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.16 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.15 25-May-2002  wiz __STDC__ is always defined on NetBSD.
 1.14 31-Jan-1998  christos - Add gcc attributes to printf like functions
- Fix format strings to be const
- Fix buffer length to be size_t
- Add %p format
- Avoid code duplication in varargs/stdarg cases
 1.13 11-Apr-1997  christos branches: 1.13.2;
#if __STDC__ -> #ifdef __STDC__
 1.12 16-Oct-1996  christos PR/2808: Remove trailing whitespace (from FreeBSD)
 1.11 11-May-1995  christos branches: 1.11.6;
Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.10 21-Mar-1995  cgd convert to new RCS id conventions.
 1.9 05-Dec-1994  cgd clean up further. more patches from Jim Jegers
 1.8 11-Jun-1994  mycroft Add RCS ids.
 1.7 12-May-1994  jtc add const qualifier to get rid of compilation warnings.
 1.6 11-May-1994  jtc sync with 4.4lite
 1.5 04-May-1994  jtc Added const qualifier to char * function arguments to eliminate
compiler warning.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.11.6.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.13.2.1 08-May-1998  mycroft Sync with trunk, per request of christos.
 1.17.16.1 18-Nov-2008  bouyer Pull up following revision(s) (requested by dholland in ticket #1232):
bin/echo/echo.c: revision 1.18
bin/sh/bltin/bltin.h: revision 1.13
bin/sh/bltin/echo.c: revision 1.14
bin/sh/output.c: revision 1.30
bin/sh/output.h: revision 1.19
Check ferror(stdout) and exit(1) if there's been a problem.
Fixes PR bin/39574 from Ed Ravin, but with a simpler patch.
output.c output.h: expose OUTPUT_ERR (flag for an exposed flags variable)
bltin.h: support ferror()
echo.c: use ferror() to fail on output write errors
Another piece of PR bin/39574.
 1.23.2.1 17-Apr-2012  yamt sync with head
 1.27.4.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.27.4.1 21-Apr-2020  martin Sync with HEAD
 1.184 21-Oct-2024  kre Fix processing of unknown variable expansion types.

Our shell is (was) one of the last not to do this correctly.

Expansions are supposed to happen only when the command in which
they occur is being executed, not while it is being parsed.
If the expansion only happens them, errors should only be
detected then.

Make it work like that (I saw after I fixed this that FreeBSD
had done it, long ago, almost the same way - it is kind of an
obvious thing to do).

This will allow code like

if test it is shell X
then
commands using shell X specific expansion ops
else if it is shell Y
then
commands using shell Y specific expansion ops
else ...
fi

Previously expansion errors were detected while parsing, so
if we're not shell X, and don't implement something that it
does (some extension to the standard) that would have generated
a parser syntax error, and the script could not be executed
(despite the line with the error never being executed).

Note that this change does not handle all such possible
extensions, just this one. Others are much harder.

One side effect of this change is that sh will now continue
reading a variable expansion until it locates the terminating
'}' (in ${var} forms) regardless of how broken it obviously
is (to our shell) whereas previously it would have bailed out
as soon as an oddity was spotted.
 1.183 03-Oct-2024  rillig bin: fix lint warning "effectively discards 'const'"

For example: src/bin/ed/io.c(339): warning: call to 'strchr' effectively
discards 'const' from argument [346]

No binary change.
 1.182 12-Jul-2024  kre Implement expandvar() : runs var/arith/cmdsub expansions on var value

expandvar() is like expandenv() and expandstr() - each expands
variable values in different contexts, expandenv() when processing
environment vars at startup (and there is no existing state to lose),
but is always using unsafe data, so never runs command substitutions,
expandstr() while in the middle of processing commands (mostly to
expand the prompt variables, so there is probably half a command
tree in the process of being built) - it uses the promptcmds option
to decide whether to run command substitutions. expandvar() is for
intermediate situations, where a variable is to be used during
processing. It will run command substitutions if the variable
value concerned was not imported from the environment.

There are currently no uses of expandvar(), so this change alters
nothing in the shell - but there will be in the near future.
 1.181 20-Oct-2023  kre branches: 1.181.2;

Work around a probably gcc12 bug in detecting "potentially clobbered"
variables after longjmp() for some architectures (sh3 at least).

This should allow the workaround to disable those warnings for this
file to be removed.

In the affected function the extra var & assignment added should simply
be deleted by any good optimiser, but if not, it doesn't matter, as
performance of this function (expandonstack()) is almost irrelevant.
 1.180 07-Apr-2023  kre The great shell trailing whitespace cleanup of 2023...
Inspired by private e-mail comments from mouse@

NFCI.
 1.179 17-Apr-2022  andvar fix various typos in comments.
 1.178 16-Apr-2022  kre Avoid generating error messages implying that user errors are illegal.
 1.177 08-Dec-2021  andvar s/desireable/desirable/ in comments.
 1.176 05-Dec-2021  msaitoh s/existance/existence/ in comment.
 1.175 16-Nov-2021  kre Fix value of ${LINENO} in "for" commands.

This affects (as best I can tell) only uses of ${LINENO} in PS4
when -x is enabled (and perhaps only when the list contains no
expansions). "for" like "case" (which was already handled) is
special in that it generates trace output before actually executing
any kind of simple command.
 1.174 15-Sep-2021  kre Improve the solution for the 2nd access to a fd which shouldn't
be available ("13") issue reported by Jan Schaumann on netbsd-users.

This fixes a bug in the earlier fix (a day or so ago) which could allow the
shell's idea of which fd range was in use by the script to get wildly
incorrect, but now also actually looks to see which fd's are in use as
renamed other user fd's during the lifetime of a redirection which needs
to be able to be undone (most redirections occur after a fork and are
permanent in the child process). Attempting to access such a fd (as with
attempts to access a fd in use by the shell for its own purposes) is treated
as an attempt to access a closed fd (EBADF). Attempting to reuse the fd
for some other purpose (which should be rare, even for scripts attempting
to cause problems, since the shell generally knows which fds the script
wants to use, and avoids them) will cause the renamed (renumbered) fd
to be renamed again (moved aside to some other available fd), just as
happens with the shell's private fds.

Also, when a generic fd is required, don't give up because of EMFILE
or similar unless there are no available fds at all (we might prefer >10
or bigger, but if there are none there, use anything). This avoids
redirection errors when ulimit -n has been set small, and all the fds >10
that are available have been used, but we need somewhere to park the old
user of a fd while we reuse that fd for the redirection.
 1.173 14-Sep-2021  kre Deal with some issues where fds intended only for internal use
by the shell were available for manipulation by scripts (or the user).
These issues were reported by Jan Schaumann on netbsd-users.

The first allows the user to reference sh internal fds, and is
a simple fix - any sh internal fd is simply treated as if it were closed
when referenced by the script. These fds can be discovered by
examining /proc/N/fd so it is not difficult for a script to discover
which fd it should attempt to access.

The second allows the user to reference a user level fd which is
one that is normally available to it, but at a point where it should
no longer be visible (when that fd has been redirected, for a built
in command, so the original fd needs to be saved so it can be restored,
the saving fd should not be accessible). It is not as easy for the
script to determine which fd to attempt here, as the relevant one
exists only during the lifetime of a built-in command (and similar),
but there are ways in some cases (aside from looking at /proc from
another process).

Fix this one by watching which fds the user script is attempting
to use, and avoid using those as temporary fds. This is possible in
this case as we know what command is being run, before we need to
save the fds it uses. That's different from the earlier case where
when the shell allocates its fds we have no idea what it might
reference later.

Also clean up a couple of other minor code issues (NFC intended) that
I noticed while here...
 1.172 09-Sep-2021  kre Fix a bug with here document processing reported on the austin group list
by oguzismailuysal@gmail.com (2021-09-08) (applies to all ash descendant
shells).

There were places in the parser where newline tokens were handled
without reading any pending here documents (leaving those until a
later newline token).

Eg: in
: << do | for x in xxx
do
do echo $x
done

The here doc text (<<do) should start immediately after the next newline
(which is after xxx). But it wasn't happening until after the following
newline (after the line containing only "do").

Bizarrely, this
: << do | for x in xxx
do echo $x
do
done
"worked" because of that.

For other cases that also failed, see the hard_cases test case in
src/tests/bin/sh/t_here.sh Note that there's nothing magic about
the particular end delimiter word chosen here, any will do, using
the ones selected for the tests (and shown here) simply makes it
all more mysterious! The one here is the exact case reported.

Fix this by reading the here docs when processing newline tokens
when they are encountered in other than the "normal" way. Whether
this catches every possibility is unknown currently - may never be
known for certain, but there are no more I can currently think of.

No pullups needed, this isn't a significant enough bug (ie: no one
actually writes code like this) to warrant that.
 1.171 19-Aug-2020  kre For now, probably forever, prohibit unquoted $ and ` in the names of
functions being defined (they can still be included if quoted).

If we parsed the way POSIX specifies (leaving the exact input text of
$ and ` expansions unaltered, until required to be expanded) this would
not be needed, as the name of a function being defined does not underbo
parameter, command, or arith expansions, so xxx$3() { : ; } would just
work. But for many reasons we don't do that (and are unlikely to ever,
though maintaing both forms might be an option someday) - which led to
very obscure behaviour (if sh were compiled in DEBUG mode, even an abort())
and certainly nothing useful. So just prohibit these uses for now.
(A portable function name must be a "name" so this makes no difference
at all to posix compat applications/scripts).

A doc update is pending (the updated sh.1 also contains updates in other
areas not yet appropriate to commit).
 1.170 14-May-2020  msaitoh Remove extra semicolon.
 1.169 10-Dec-2019  kre Correct a typo in a comment, 08x0 was meant to be 0x80 (duh!). NFC.
 1.168 04-May-2019  kre Fix an (apparent) ancient ash bug, that was apparently fixed sometime
in the past, but managed to re-surface...

The expression "${0+\}}" should expand to "}" not "\}"
Almost all other shells handle it that way (incl FreeBSD & dash).

Issue pointed out by Martijn Dekker.

Add ATF sub-tests for the 4 old var expand operators (${var+word}
${var-word} ${var-word} and ${var?word} - including the forms
with the ':' included) and amongst those tests include test cases
for this issue, so if the bug tries to appear again, we can squash
it quicker. (The newer pattern matching operators are already
well tested as part of testing patterns.)
 1.167 27-Feb-2019  kre 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.
 1.166 09-Feb-2019  kre 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.)
 1.165 04-Feb-2019  kre 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).
 1.164 22-Jan-2019  kre 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.
 1.163 22-Jan-2019  kre 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.)
 1.162 21-Jan-2019  kre 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
 1.161 15-Jan-2019  kre 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.
 1.160 09-Jan-2019  kre 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.
 1.159 11-Dec-2018  kre 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).
 1.158 09-Dec-2018  christos comment out unused.
 1.157 03-Dec-2018  kre 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...
 1.156 03-Dec-2018  kre 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.
 1.155 01-Dec-2018  kre 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.
 1.154 01-Dec-2018  kre NFC. Need a grain of const
 1.153 18-Nov-2018  kre 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).
 1.152 09-Nov-2018  kre PR bin/53712

Avoid crash from redirect on null compound command.
 1.151 08-Nov-2018  kre 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.
 1.150 19-Aug-2018  kre 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)
 1.149 22-Jul-2018  kre 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.)
 1.148 20-Jul-2018  kre 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),
 1.147 13-Jul-2018  kre 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.)
 1.146 21-Apr-2018  kre branches: 1.146.2;

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
 1.145 10-Nov-2017  kre branches: 1.145.2;
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
 1.144 21-Aug-2017  kre 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 ...
 1.143 05-Aug-2017  kre 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.
 1.142 26-Jul-2017  kre 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.)
 1.141 03-Jul-2017  kre 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...).
 1.140 30-Jun-2017  kre Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
 1.139 24-Jun-2017  kre 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.
 1.138 17-Jun-2017  kre Many internal memory management type fixes.

PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)

echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)

(Recently added) Problems with ~ expansion fixed (mem management related).

Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)

And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.

Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,

More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.

[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]

More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).

User visible changes:

Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in

cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END

(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)

With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)

POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).

Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
 1.137 08-Jun-2017  kre (Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
 1.136 08-Jun-2017  kre Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
 1.135 07-Jun-2017  kre Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
 1.134 07-Jun-2017  kre A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)

This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)

The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.

This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).

I would not call the results of this perfect yet, but it is close.
 1.133 07-Jun-2017  kre An initial attempt at implementing LINENO to meet the specs.

Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.

Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)

This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.

POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)

This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.

This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).

Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
 1.132 03-Jun-2017  kre branches: 1.132.2;

When we record an arithmetic expression ($(( ))) as being quoted,
what matters is the quoting state just before we switch into arithmetic
syntax parsing mode, not the state after...

This fixes the regiression introduced earlier today (UTC) where
quoted arithmetic expressions were being subjected to word splitting.
 1.131 03-Jun-2017  kre Fixes to shell expand (that is, $ stuff) from FreeBSD (implemented
differently...)

In particular ${01} is now $1 not $0 (for ${0any-digits})

${4294967297} is most probably now ""
(unless you have a very large number of params)
it is no longer an alias for $1 (4294967297 & 0xFFFFFFFF) == 1

$(( expr $(( more )) stuff )) is no longer the same as
$(( expr (( more )) stuff )) which was sometimes OK, as in:
$(( 3 + $(( 2 - 1 )) * 3 ))
but not always as in:
$(( 1$((1 + 1))1 ))
which should be 121, but was an arith syntax error as
1((1 + 1))1
is meaningless.

Probably some more. This also sprinkles a little const, splits a big
func that had 2 (kind of unrelated) purposes into two simpler ones,
and avoids some (semi-dubious) modifications (and restores) in the input
string to insert \0's when they were needed.
 1.130 29-May-2017  kre NFC (normal builds): DEBUG only change - convert parser to newer trace method.

parser tracing is useful when debugging the parser (which admittedly is
fairly often...) but there is a lot of it, and it gets in the way when
looking at something else. Now we can turn it off when not wanted.
 1.129 27-May-2017  kre More standard (and saner) implementation of the ! reserved word.
Unless the shell is compiled with the (compilation time) option
BOGUS_NOT_COMMAND (as in CFLAGS+=-DBOGUS_NOT_COMMAND) which it
will not normally be, the ! command (reserved word) will only
be permitted at the start of a pipeline (which includes the
degenerate pipeline with no '|'s in it of course - ie: a simple cmd)
and not in the middle of a pipeline sequence (no "cmd | ! cmd" nonsense.)
If the latter is really required, then "cmd | { ! cmd; }" works as
a standard equivalent.

In POSIX mode, permit only one ! ("! pipeline" is ok. "! ! pipeline" is not).
Again, if needed (and POSIX conformance is wanted) "! { ! pipeline; }"
works as an alternative - and is safer, some shells treat "! ! cmd" as
being identical to "cmd" (this one did until recently.)
 1.128 14-May-2017  kre NFC: changes to comments only - expand/add comments relating to ${#...}
parsing, and all its peculiarities.
 1.127 11-May-2017  kre Fix some parser weirdness...
${#VAR:-foo} (or any other modifier on ${#VAR} is a syntax error.
On the other hand ${##} is not, nor is ${##13} though they mean
quite different things (the latter is an idiom everyone should learn,
... $# except we refuse to admit the possibility that it is 13...
Even I cannot explain what ${#-foo} used to do, but it wasn't sane!
(It should be just $# as $# is never unset, but ...)
Shell syntax is truly a wondrous thing!
 1.126 10-May-2017  kre NFC: Whitespace, KNF, and (some) consistency.
 1.125 09-May-2017  kre If we are going to permit
! ! pipeline
(And for now the other places where ! is permitted)
we should at least generate the logically correct exit
status:
! ! (exit 5); echo $?
should print 1, not 5. ksh and bosh do it this way - and it makes sense.
bash and the FreeBSD sh echo "5" (as did we until now.)
dash, zsh, yash all enforce the standard syntax, and prohibit this.
 1.124 09-May-2017  kre Remove bogus extra \n from syntax error message.
 1.123 04-May-2017  kre Implement the ';&' (used instead of ';;') case statement list terminator
which causes fall through the to command list of the following pattern
(wuthout evaluating that pattern). This has been approved for inclusion
in the next major version of the POSIX standard (Issue 8), and is
implemented by most other shells.

Now all form a circle and together attempt to summon the great wizd
in the hopes that his magic spells can transform the poor attempt
at documenting this feature into something rational...
 1.122 03-May-2017  kre Fix the heredoc line counting bug that I caused when the heredoc
processing was changed just over a year ago (rev 1.111).
 1.121 03-May-2017  kre Deal with \newline line continuations more correctly.
They can occur anywhere (*anywhere*) not only where it
happens to be convenient to the parser...

This fix from FreeBSD (thanks again folks).

To make this work, pushstring()'s signature needed to change to allow a
const char * as its string arg, which meant sprinkling some const other
places for a brighter appearance (and handling fallout).

All this because I wanted to see what number would come from

echo $\
{\
L\
I\
N\
E\
N\
O\
}

and was surprised at the result! That works now...

The bug would also affect stuff like

true &\
& false

and all kinds of other uses where the \newline occurred in the
"wrong" place.

An ATF test for sh syntax is coming... (sometime.)
 1.120 01-Jun-2016  kre branches: 1.120.6;

PR bin/51145 PR bin/48489
More fixes to the shell parser to prevent empty simple commands (where
empty means no significant text at all) - as discussed on tech-userlevel
this still allows { } (which can be useful in function definitions, not
really anywhere else though) except in posix mode. ( ) now generates
a syntax error, as should any other place where commands are required but
nothing is present. (nb, redirections, var assignments, even var expansions
that expand to nothing, are all OK, and avoid the error - just comments, or
whits space, are not.) This is (aside from allowing { } at all) all in
accordance with the posix spec.
 1.119 09-May-2016  kre PR bin/48489 -- Shell "simple commands" are now not allowed to be
completely empty, they must have something (var assignment, redirect,
or command, or multiple of those) to avoid a syntax error. This
matches the requirements of the grammar in the standard. Correct the
parser (using FreeBSD's sh as a guide) and update the man page to
remove text that noted a couple of places this was previously OK.

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

ok christos@
 1.117 02-May-2016  christos Fix handing of user file descriptors outside the 0..9 range.
Also, move (most of) the shell's internal use fd's to much
higher values (depending upon what ulimit -n allows) so they
are less likely to clash with user supplied fd numbers. A future
patch will (hopefully) avoid this problem completely by dynamically
moving the shell's internal fds around as needed. (From kre@)
 1.116 04-Apr-2016  christos Allow a heredoc to be positioned outside a `` command substitution.
POSIX just says "here docs begin after the next newline [token]".
Nothing about "provided it is inside any `` the redirect operator
appears in... As best I can tell, NetBSD now has the only shell to
handle this "correctly" (which raises the question whether it is
correct - but if not, only erroneous scripts are affected.)
This is required by some (probably broken) autoconfigure related
scripts. (from kre@)
 1.115 31-Mar-2016  christos After discussions with Jilles Tjoelker (FreeBSD shell) and
following a suggestion from him, the way the fix to PR bin/50993
was implemented has changed a little. There are three steps involved
in processing a here document, reading it, parsing it, and then
evaluating it before applying it to the correct file descriptor for
the command to use. The third of those is not related to this
problem, and has not changed. The bug was caused by combining the
first two steps into one (and not doing it correctly - which would be
hard that way.) The fix is to split the first two stages into
separate events. The original fix moved the 2nd stage (parsing)
to just immediately before the 3rd stage (evaluation.) Jilles
pointed out some unwanted side effects from doing it that way, and
suggested moving the 2nd stage to immediately after the first.
This commit makes that change. The effect is to revert the changes
to expand.c and parser.h (which are no longer needed) and simplify
slightly the change to parser.c. (from kre@)
 1.114 31-Mar-2016  christos PR bin/51027 - fix the parsing of references to shell parameters
when given without braces (ie: $2 etc). Only the first 9 shell
parameters ($1 .. $9) and the special parameter ($0) can be
referenced this way, $10 is ${1}0 not ${10}. Make it so.
This bug brought to notice by Sven Mascheck's web pages which
discuss (among other things) the history of this (and other ash
based) shells .. see http://www.in-ulm.de/~mascheck/ (from kre@)
 1.113 31-Mar-2016  christos This is an internally visible change - no effect visible to the
user is expected. With the previous commits to parser.c, we no
longer need to handle reading here documents in the (massive)
readtoken1() function. That allows its code to be simplified and
made easier to read and understand (several goto's goto goto heaven.
RIP) (from kre@)
 1.112 27-Mar-2016  christos Move the parseredir internal subroutine out of readtoken1() into being
a real function of its own (also inspired by FreeBSD - though for
this one other sh differences require slightly different code.) (from kre@)
 1.111 27-Mar-2016  christos PR bin/50993 - this is a significant rewrite of the way that here
documents are processed. Now, when first detected, they are
simply read (the only change made to the text is to join lines
ended with a \ to the subsequent line, otherwise end marker detection
does not work correctly (for here docs with an unquoted endmarker
only of course.) This patch also moves the "internal subroutine"
for looking for the end marker out of readtoken1() (which had to
happen as readtoken1 is no longer reading the here doc when it is
needed) - that uses code mostly taken from FreeBSD's sh (thanks!)
and along the way results in some restrictions on what the end
marker can be being removed. We still do not allow all we should.
(from kre@)
 1.110 27-Mar-2016  christos Cease "support" for <redirect> fn() { ...
Any redirect (or redirects) before a function definition were
allowed by the parser, but otherwise totally ignored. The standard
syntax does not permit redirects there, now, neither do we. (from kre@)
 1.109 27-Mar-2016  christos Finish constifying the new parsebackquote() function. Save a
variable or two... Should change nothing. (from kre@)
 1.108 27-Mar-2016  christos General KNF and source code cleanups, avoid scattering the
magic string " \t\n" all over the place, slightly improved
syntax error messages, restructured some of the code for
clarity, don't allow IFS to be imported through the environment,
and remove the (never) conditionally compiled ATTY option.
Apart from one or two syntax error messages, and ignoring IFS
if present in the environment, this is intended to have no
user visible changes. (from kre@)
 1.107 21-Mar-2016  christos fix constness (from kre)
 1.106 20-Mar-2016  christos Move the command substitution "internal subroutine" part of
readtoken1() into a real function of its own (inspired by a
similar change made by FreeBSD - the other internal routines
they moved out are expected to move out here as well soon.)
This change helps avoid gcc 5.3 demanded (not required!) volatile
noise which would slow down parsing. (from kre)
 1.105 18-Mar-2016  christos sprinkle more volatile (needed for the rescue build with gcc-5.3)
 1.104 08-Mar-2016  christos put back some volatile, gcc complains (x86_64)
 1.103 08-Mar-2016  christos Remove most volatility from readtoken1() restoring execution
speed to approximately where it was before the changes made
in version 1.96 (which assumed this would eventually happen.)
 1.102 28-Feb-2016  christos Bug fixes to handling of unterminated here documents
(they should be, and now are, a syntax error), and
miscellaneous other minor cleanups. (from kre)
 1.101 27-Feb-2016  christos remove useless casts
 1.100 27-Feb-2016  christos CID 1354293: handle EOF
 1.99 23-Feb-2016  christos Fix quoting inside heredoc's. (from kre)
 1.98 22-Feb-2016  christos PR bin/43469 - correctly handle quoting of the pattern part of ${var%pat}
type expansions. (from kre)
 1.97 22-Feb-2016  christos Fix for PR bin/48631 - allow commands controlled
by case statements to be nothing more than redirects (from kre)
 1.96 22-Feb-2016  christos Finish the fix for PR/48631 - that is, make the parser correctly
handle the token syntax it really should be handling (including
some that posix does not require, but is right anyway.) This is
quite similar to, and to some extent inspired by the way the FreeBSD
sh parser.c works, but the actual implementation is quite different.
(from kre)
 1.95 22-Feb-2016  christos KNF / whitespace fixes. No changes of substance. (from kre)
 1.94 19-Feb-2016  christos PR/50827: Richard Hansen: Fix default variable assignment with arithmetic,
from kre.
 1.93 29-Aug-2014  christos remove unused assignment
 1.92 29-Aug-2014  christos Eat trailing backslash like bash and pdksh (not zsh). CBACK+CEOF = TEOF
 1.91 19-Aug-2014  christos PR/49125: Havard Eidnes: /bin/sh does not support redirecting to or from FDs > 9
According to:
http://pubs.opengroup.org/onlinepubs/009604599/utilities/xcu_chap02.html#tag_02_07

Redirection support for fds > 9 is optional but allowed.
 1.90 01-Jan-2014  christos branches: 1.90.4;
whitespace fixes
 1.89 01-Jan-2014  christos There was a case where \n did not increase plinno
 1.88 01-Jan-2014  christos clarify further.
 1.87 01-Jan-2014  christos explain the previous fix.
 1.86 31-Dec-2013  christos allow case statement without any patterns.
 1.85 02-Oct-2013  christos add stdio.h
 1.84 02-Oct-2013  christos add crude $LINENO support for FreeBSD
 1.83 17-Jun-2012  wiz branches: 1.83.2;
Initialize two variables for clang.
 1.82 28-Mar-2012  christos include <limits.h> for CHAR_MIN/CHAR_MAX
 1.81 25-Mar-2012  christos PR/43597: Don't break from parsing word tokens in we are in double quotes.
Fixes: sh -c 'echo "${foo:="first-word"} second-word"'
 1.80 31-Aug-2011  plunky branches: 1.80.2; 1.80.4;
NULL does not need a cast
 1.79 13-Dec-2010  christos PR/44229: Henning Petersen: Remove dup check for whitespace.
 1.78 17-Nov-2010  christos revert again, since this breaks libtool amongst other things.
 1.77 16-Nov-2010  christos PR/43469: Antii Kantee: test/util/sh/t_expand:strip fails.
Bring back fixes from revision 1.75:

- Fix a couple of bugs to make the following two echo statements print the
same output as they should:

line='#define bindir "/usr/bin" /* comment */'
echo "${line%%/\**}"
echo ${line%%/\**}

1. ISDBLQUOTE() was not working properly for non VSNORMAL expansions because
varnest was incremented before the variable was completely parsed. Add
an insub adjustment to keep track of that.
2. When we have a quoted backslash, we either need to escape twice, because
one level of escaping will be stripped later (in the variable substitution
case) or simply enter the backslash.
 1.76 14-Nov-2010  christos revert previous. breaks other stuff.
 1.75 14-Nov-2010  christos - Fix a couple of bugs to make the following two echo statements print the
same output as they should:

line='#define bindir "/usr/bin" /* comment */'
echo "${line%%/\**}"
echo ${line%%/\**}

1. ISDBLQUOTE() was not working properly for non VSNORMAL expansions because
varnest was incremented before the variable was completely parsed. Add
an insub adjustment to keep track of that.
2. When we have a quoted backslash, we need to escape twice, because one
level of escaping will be stripped later. (XXX: Do that when insub == 1
only?)

- Make macros statements
 1.74 18-Jan-2009  lukem fix -Wsign-compare issues
 1.73 08-Nov-2008  christos and if you don't succeed twice, try again.
 1.72 07-Nov-2008  christos Try a different fix for PR/11317: Don't ignore errors in list().
 1.71 07-Nov-2008  christos Break PR/11317 again. The counting parentheses fix does not handle case
statements properly. Fixes PR/39873.
 1.70 05-Nov-2008  christos PR/11317: Hubert Feyrer: Recognize mismatched parentheses inside old style
command substitution.
 1.69 23-Aug-2008  christos Fix here documents that end abruptly without NL before EOF.
(Andy Shevchenko)
 1.68 25-Apr-2008  christos branches: 1.68.2;
Detect unmatched quotes inside old style command substitution.
echo `"`
 1.67 27-Feb-2008  dsl branches: 1.67.2;
Change spaces to tabs for consistency with adjacent lines.
 1.66 15-Dec-2007  perry branches: 1.66.2;
convert __attribute__s to applicable cdefs.h macros
 1.65 07-Jun-2007  dsl branches: 1.65.4;
Fix sh -c 'true && ! true | false; echo $?'
Add some more TRACE((...)) calls to aid such debugging.
Fixes PR bin/36435
Clearly no one tried this test when the changes of rev 1.31 and 1.44 were done!
 1.64 13-Jan-2007  christos PR/35410: Valeriy E. Ushakov: /bin/sh mishandles shell function definitions
with function names that are not plain words
1. remove the escape annotations from the function name.
2. check if the function has a valid name before storing it.
 1.63 16-Oct-2006  christos branches: 1.63.2; 1.63.4;
sprinkle volatile.
 1.62 04-Oct-2006  christos Prefix "Syntax Error" with the program name, if the command name is not
available. At least this way we get an idea of what program gives us the
message.
 1.61 20-Jun-2006  christos PR/33775: YAMAMOTO Takashi: /bin/sh doesn't accept "(" before case patterns
 1.60 10-May-2006  mrg quell GCC 4.1 uninitialised variable warnings.

XXX: we should audit the tree for which old ones are no longer needed
after getting the older compilers out of the tree..
 1.59 21-Mar-2005  dsl Back out previous, amongst other things it breaks $((0x10))
 1.58 20-Mar-2005  dsl Treat $((x)) as equivalent to $(($x)) - posix seems to require this now.
 1.57 27-Jun-2004  dsl Fix treatment of ' inside a 'here document' with a quoted EOF marker.
Fixes a breakage from the previous version.
 1.56 26-Jun-2004  dsl Correctly apply IFS to unquoted text in ${x-text}.
Fixes PR/26058 and the 'for i in ${x-a b c}; do ...' and ${x-'a b' c}.
I can't find a PR for the latter problem.
Regression test goind in shortly.
 1.55 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.54 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.53 15-May-2002  christos implement noclobber. From Ben Harris, with minor tweaks from me. Two
unimplemented comments to go. Go Ben!
 1.52 20-Feb-2002  christos enami convinced me that it is a good idea to use the first word of
the allocated area.
 1.51 12-Feb-2002  christos Since we should be able to handle nested double quotes, don't use
the syntax maps to determine the beginning and end quotes (kill
CENDQUOTE). Handle single quotes opening and closing via checking
the current syntax map. Keep a bitmap of doublequote state one bit
per variable nesting level. For the first 32 nested double quotes,
we don't need any additional memory, but for more we allocate
dynamically.
 1.50 12-Feb-2002  ross back this directory up a day, systems won't even boot (rc.subr splodes)

suggested back-to-the-drawing-board test: $ echo "${PWD:-notlikely}"
 1.49 11-Feb-2002  christos PR/15579: Alan Barrett: }'s inside variable specs were taken into account
even if quoted:
foo=${foo:-"'{}'"}; echo $foo
would display '{'} instead of '{}'.
 1.48 02-Nov-2001  christos Fix switch alias handling. Inspired from FreeBSD, but corrected to handle
alias expansion inside the switch as appropriate. This is achieved by a
flag noalias which is turned on and off in as we parse. In the following
example [1] and [0] indicate the value of noalias.

[0] case <expr> in
[1] <lit> ) [0] <expr> ;;
[1] <lit> ) [0] <expr> ;;
...
[1] esac [0]

FreeBSD does:

[0] case <expr> in [1]
<lit> ) <expr> ;;
<lit> ) <expr> ;;
...
esac [0]

This handles the following shell script:

alias a=ls

case $1 in
a) echo a;
a;;
f) echo f;;
*) echo default;;
esac
 1.47 03-Apr-2001  christos PR/12533: Koji Mori: eval gets misparsed when it has a trailing semi-colon.
 1.46 04-Feb-2001  christos remove redundant declarations and nexted externs.
 1.45 27-Jul-2000  cgd un-__P functions declared in parser.h. host programs include parser.h,
and so it shouldn't use __P. (this should probably be done better, by
not declaring the parser functions in headers used by host programs,
but this works well enough.)
 1.44 27-Jan-2000  christos branches: 1.44.4;
Fix bin/9184, bin/9194, bin/9265, bin/9266
Exitcode and negation problems (From Martin Husemann)
 1.43 09-Jul-1999  christos compile with WARNS = 2
 1.42 04-Feb-1999  christos PR/4966: Joel Reicher: Implement <> redirections which are documented in
the man page.
 1.41 25-Jan-1999  mycroft Patches from Tor Egge (via Havard Eidnes) to fix various bugs in field
splitting and combining.
(Note: Some of this are not strictly bugs, but differences between traditional
Bourne shell and POSIX.)
 1.40 26-Sep-1998  itohy Fixed memory leak on old style command substitution
such as sh -c 'echo `echo foo`' .
The memory allocated with ckmalloc() at
parser.c:1349:readtoken1() (search for "done:" label)
was never freed.

I changed this to use 'string stack' framework of Ash.
Note that a string on string stack is properly freed on
exception and end of command parsing, and no explicit free
or signal handlings required.
See TOUR for an overview, and memalloc.[ch] for details
of string stack.
 1.39 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.38 29-Mar-1998  mrg - change "extern" variables into int's
- remove extern'd variables not actually referenced
- don't use char as an array index
 1.37 15-Jul-1997  christos branches: 1.37.2;
Fix Sparcworks warnings.
 1.36 04-Jul-1997  christos Fix compiler warnings.
 1.35 14-Mar-1997  christos NO_HISTORY->SMALL
 1.34 26-Jan-1997  christos Fix parsing problem introduced in the previous ${10} fix, where $#digit
or $digit# would get mis-parsed as a positional parameter.
 1.33 24-Jan-1997  christos enable parsing of multi-digit positional arguments i.e. ${10} works and it
is not a bad substitution.
 1.32 11-Jan-1997  tls kill 'register'
 1.31 25-Nov-1996  christos - fix parsing bug reported by mycroft: ! was only recognized in the beginning
of pipelines. Now we are recognizing it everywhere like ksh, so:

! if [ a = b ]; then ! echo c && ! true; else ! echo d; fi

works.
 1.30 16-Oct-1996  christos PR/2808: - detect eof inside backquotes
- handle && and || shortcircuits properly
- tokens.def -> tokens.h
(from FreeBSD)
 1.29 09-May-1996  christos branches: 1.29.4;
Close PR/2384 backquoted backslash-newline was not eaten.
 1.28 05-Mar-1996  christos - parser.c: Fix prompting in old style backquote expansion. Fixes PR/2139
and many user complaints why the shell hangs in echo "`"
- eval.c: Fix exitstatus invalid resetting in `if' statements were:
if (exit 3); then
echo foo $?
else
echo bar $?
fi
printed 'bar 0' instead of bar 3
 1.27 19-Oct-1995  christos - fix PR1620, -DNO_HISTORY did not work.
- restore parsing state after parsing old style command substitution.
The ';' in '`echo z;`' broke the following:
for i in 1; do
cat > /dev/tty << __EOF__
`echo z;`
__EOF__
done

cVS: Enter Log. Lines beginning with `CVS: ' are removed automatically
 1.26 17-May-1995  christos Fixed bug where ${#} was interpreted incorrectly as ${#var} and thus
misparsed. Keith Bostic reported it.
 1.25 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.24 21-Mar-1995  cgd convert to new RCS id conventions.
 1.23 23-Jan-1995  christos I added the documented in the manual but not implemented variable expansions:

${#WORD}
${WORD%PAT}
${WORD%%PAT}
${WORD#PAT}
${WORD##PAT}
 1.22 05-Dec-1994  cgd clean up further. more patches from Jim Jegers
 1.21 23-Sep-1994  mycroft Eliminate uses of some obsolete functions.
 1.20 24-Aug-1994  mycroft Fix a core dump and another parse error related to null commands.
 1.19 07-Jul-1994  mycroft Fix some problems with empty commands.
 1.18 14-Jun-1994  jtc branches: 1.18.2;
From Christos:
1. Fix `-' quoting in [ ] expressions.
2. Fix expansion of variables in redirections
 1.17 11-Jun-1994  mycroft Add RCS ids.
 1.16 01-Jun-1994  jtc Fixed another case statement parsing bug that was introduced by my
last fix :-(. Thanks to Theo for letting me know about it...
 1.15 30-May-1994  jtc Re-worked case statement parsing. With luck, this fixes bug #268.
 1.14 21-May-1994  cgd a few more things to omit when NO_HISTORY defined. from noel@cs.oberlin.edu
 1.13 17-May-1994  jtc Fix backslash parsing within backquoted string
 1.12 11-May-1994  jtc Mark Weaver's background/list fix (for bug #236)
 1.11 11-May-1994  jtc sync with 4.4lite
 1.10 09-Sep-1993  cgd from jim wilson: (command): Handle TEOF like TNL.
 1.9 06-Sep-1993  mycroft Make `>file;' work.
 1.8 01-Aug-1993  mycroft Add RCS identifiers.
 1.7 15-Jul-1993  jtc Another change from Jim Wilson.
 1.6 07-Jul-1993  jtc IEEE 1003.2 (D11.2.2.3) requires that the system's true and false be accessed
instead of searching $PATH. The best way to satisfy this requirement is to
make them builtins.

True was allready builtin, this patch adds false.
 1.5 02-May-1993  sef Jim "wilson@moria.cygnus.com" Wilson's patches to make C News (and other
things) work.
 1.4 26-Apr-1993  dpassage Fixed incorrect calls to longjmp.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.18.2.2 24-Aug-1994  mycroft update from trunk
 1.18.2.1 08-Jul-1994  cgd from trunk: Fix some problems with empty commands.
 1.29.4.2 26-Jan-1997  rat Pullup request 1.33 -> 1.34 from Christos Zoulas.
"Fix parsing problem introduced in the previous ${10} fix, where $#digit
or $digit# would get mis-parsed as a positional parameter."
 1.29.4.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.37.2.1 08-May-1998  mycroft Sync with trunk, per request of christos.
 1.44.4.1 25-Nov-2001  he Pull up revision 1.48 (requested by hubertf):
Prevent alias expansion in case labels, where it is unwanted.
 1.63.4.1 03-Sep-2007  wrstuden Sync w/ NetBSD-4-RC_1
 1.63.2.2 13-Jun-2007  liamjfoy Pull up following revision(s) (requested by dsl in ticket #718):
bin/sh/parser.c: revision 1.64
PR/35410: Valeriy E. Ushakov: /bin/sh mishandles shell function definitions
with function names that are not plain words
1. remove the escape annotations from the function name.
2. check if the function has a valid name before storing it.
 1.63.2.1 13-Jun-2007  liamjfoy Pull up following revision(s) (requested by dsl in ticket #719):
bin/sh/parser.c: revision 1.65
Fix sh -c 'true && ! true | false; echo $?'
Add some more TRACE((...)) calls to aid such debugging.
Fixes PR bin/36435
Clearly no one tried this test when the changes of rev 1.31 and 1.44 were done!
 1.65.4.2 23-Mar-2008  matt sync with HEAD
 1.65.4.1 09-Jan-2008  matt sync with HEAD
 1.66.2.1 24-Mar-2008  keiichi sync with head.
 1.67.2.1 18-May-2008  yamt sync with head.
 1.68.2.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.80.4.1 27-Aug-2016  bouyer Pull up following revision(s) (requested by kre in ticket #1397):
bin/sh/parser.c: revision 1.114 via patch
PR bin/51027 - fix the parsing of references to shell parameters
when given without braces (ie: $2 etc). Only the first 9 shell
parameters ($1 .. $9) and the special parameter ($0) can be
referenced this way, $10 is ${1}0 not ${10}. Make it so.
This bug brought to notice by Sven Mascheck's web pages which
discuss (among other things) the history of this (and other ash
based) shells .. see http://www.in-ulm.de/~mascheck/ (from kre@)
 1.80.2.3 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.80.2.2 30-Oct-2012  yamt sync with head
 1.80.2.1 17-Apr-2012  yamt sync with head
 1.83.2.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.90.4.2 10-Dec-2018  martin Pull up following revision(s) (requested by kre in ticket #1663):

bin/sh/parser.c: revision 1.152 (via patch)

PR bin/53712

Avoid crash from redirect on null compound command.
 1.90.4.1 27-Aug-2016  bouyer Pull up following revision(s) (requested by kre in ticket #1212):
bin/sh/parser.c: revision 1.114 via patch
PR bin/51027 - fix the parsing of references to shell parameters
when given without braces (ie: $2 etc). Only the first 9 shell
parameters ($1 .. $9) and the special parameter ($0) can be
referenced this way, $10 is ${1}0 not ${10}. Make it so.
This bug brought to notice by Sven Mascheck's web pages which
discuss (among other things) the history of this (and other ash
based) shells .. see http://www.in-ulm.de/~mascheck/ (from kre@)
 1.120.6.2 19-May-2017  pgoyette Resolve conflicts from previous merge (all resulting from $NetBSD
keywork expansion)
 1.120.6.1 11-May-2017  pgoyette Sync with HEAD
 1.132.2.7 12-Nov-2018  martin Pull up following revision(s) (requested by kre in ticket #1086):

bin/sh/parser.c: revision 1.152

PR bin/53712

Avoid crash from redirect on null compound command.
 1.132.2.6 10-Sep-2018  martin Pull up following revision(s) via patch (requested by kre in ticket #1015):

bin/sh/expand.c: revision 1.124
bin/sh/expand.c: revision 1.127
bin/sh/parser.c: revision 1.148
bin/sh/parser.c: revision 1.149
bin/sh/syntax.c: revision 1.6
bin/sh/syntax.h: revision 1.9 (partial)

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),

-

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.)
 1.132.2.5 25-Aug-2018  martin Pull up following revision(s) (requested by kre in ticket #988):

bin/sh/parser.c: revision 1.147
bin/sh/var.c: revision 1.70
bin/sh/mystring.c: revision 1.18
bin/sh/options.c: revision 1.53
bin/sh/histedit.c: revision 1.53

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.)
 1.132.2.4 06-May-2018  martin Pull up following revision(s) (requested by kre in ticket #804):

bin/sh/parser.c: revision 1.146

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
 1.132.2.3 17-Nov-2017  snj Pull up following revision(s) (requested by kre in ticket #355):
bin/sh/parser.c: revision 1.145
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.)
 1.132.2.2 09-Aug-2017  snj Pull up following revision(s) (requested by kre in ticket #199):
bin/sh/input.c: revision 1.61
bin/sh/parser.c: revision 1.143
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.
 1.132.2.1 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.145.2.7 26-Jan-2019  pgoyette Sync with HEAD
 1.145.2.6 18-Jan-2019  pgoyette Synch with HEAD
 1.145.2.5 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.145.2.4 26-Nov-2018  pgoyette Sync with HEAD, resolve a couple of conflicts
 1.145.2.3 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.145.2.2 28-Jul-2018  pgoyette Sync with HEAD
 1.145.2.1 22-Apr-2018  pgoyette Sync with HEAD
 1.146.2.4 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.146.2.3 21-Apr-2020  martin Sync with HEAD
 1.146.2.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.146.2.1 10-Jun-2019  christos Sync with HEAD
 1.181.2.1 02-Aug-2025  perseant Sync with HEAD
 1.30 21-Oct-2024  kre Fix processing of unknown variable expansion types.

Our shell is (was) one of the last not to do this correctly.

Expansions are supposed to happen only when the command in which
they occur is being executed, not while it is being parsed.
If the expansion only happens them, errors should only be
detected then.

Make it work like that (I saw after I fixed this that FreeBSD
had done it, long ago, almost the same way - it is kind of an
obvious thing to do).

This will allow code like

if test it is shell X
then
commands using shell X specific expansion ops
else if it is shell Y
then
commands using shell Y specific expansion ops
else ...
fi

Previously expansion errors were detected while parsing, so
if we're not shell X, and don't implement something that it
does (some extension to the standard) that would have generated
a parser syntax error, and the script could not be executed
(despite the line with the error never being executed).

Note that this change does not handle all such possible
extensions, just this one. Others are much harder.

One side effect of this change is that sh will now continue
reading a variable expansion until it locates the terminating
'}' (in ${var} forms) regardless of how broken it obviously
is (to our shell) whereas previously it would have bailed out
as soon as an oddity was spotted.
 1.29 12-Jul-2024  kre Implement expandvar() : runs var/arith/cmdsub expansions on var value

expandvar() is like expandenv() and expandstr() - each expands
variable values in different contexts, expandenv() when processing
environment vars at startup (and there is no existing state to lose),
but is always using unsafe data, so never runs command substitutions,
expandstr() while in the middle of processing commands (mostly to
expand the prompt variables, so there is probably half a command
tree in the process of being built) - it uses the promptcmds option
to decide whether to run command substitutions. expandvar() is for
intermediate situations, where a variable is to be used during
processing. It will run command substitutions if the variable
value concerned was not imported from the environment.

There are currently no uses of expandvar(), so this change alters
nothing in the shell - but there will be in the near future.
 1.28 13-Feb-2019  kre branches: 1.28.12;

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.
 1.27 11-Dec-2018  kre 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).
 1.26 03-Dec-2018  kre 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.
 1.25 01-Dec-2018  kre NFC. Need a grain of const
 1.24 21-Aug-2017  kre branches: 1.24.2; 1.24.4;
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 ...
 1.23 30-Jun-2017  kre Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
 1.22 07-Jun-2017  kre A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)

This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)

The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.

This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).

I would not call the results of this perfect yet, but it is close.
 1.21 31-Mar-2016  christos branches: 1.21.8;
After discussions with Jilles Tjoelker (FreeBSD shell) and
following a suggestion from him, the way the fix to PR bin/50993
was implemented has changed a little. There are three steps involved
in processing a here document, reading it, parsing it, and then
evaluating it before applying it to the correct file descriptor for
the command to use. The third of those is not related to this
problem, and has not changed. The bug was caused by combining the
first two steps into one (and not doing it correctly - which would be
hard that way.) The fix is to split the first two stages into
separate events. The original fix moved the 2nd stage (parsing)
to just immediately before the 3rd stage (evaluation.) Jilles
pointed out some unwanted side effects from doing it that way, and
suggested moving the 2nd stage to immediately after the first.
This commit makes that change. The effect is to revert the changes
to expand.c and parser.h (which are no longer needed) and simplify
slightly the change to parser.c. (from kre@)
 1.20 27-Mar-2016  christos PR bin/50993 - this is a significant rewrite of the way that here
documents are processed. Now, when first detected, they are
simply read (the only change made to the text is to join lines
ended with a \ to the subsequent line, otherwise end marker detection
does not work correctly (for here docs with an unquoted endmarker
only of course.) This patch also moves the "internal subroutine"
for looking for the end marker out of readtoken1() (which had to
happen as readtoken1 is no longer reading the here doc when it is
needed) - that uses code mostly taken from FreeBSD's sh (thanks!)
and along the way results in some restrictions on what the end
marker can be being removed. We still do not allow all we should.
(from kre@)
 1.19 22-Feb-2016  christos PR bin/43469 - correctly handle quoting of the pattern part of ${var%pat}
type expansions. (from kre)
 1.18 02-Oct-2013  christos add crude $LINENO support for FreeBSD
 1.17 26-Jun-2004  dsl branches: 1.17.48; 1.17.54;
Correctly apply IFS to unquoted text in ${x-text}.
Fixes PR/26058 and the 'for i in ${x-a b c}; do ...' and ${x-'a b' c}.
I can't find a PR for the latter problem.
Regression test goind in shortly.
 1.16 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.15 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.14 27-Jul-2000  cgd un-__P functions declared in parser.h. host programs include parser.h,
and so it shouldn't use __P. (this should probably be done better, by
not declaring the parser functions in headers used by host programs,
but this works well enough.)
 1.13 09-Jul-1999  christos compile with WARNS = 2
 1.12 25-Jan-1999  mycroft Patches from Tor Egge (via Havard Eidnes) to fix various bugs in field
splitting and combining.
(Note: Some of this are not strictly bugs, but differences between traditional
Bourne shell and POSIX.)
 1.11 16-Oct-1996  christos PR/2808: Remove trailing whitespace (from FreeBSD)
 1.10 11-May-1995  christos branches: 1.10.6;
Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.9 21-Mar-1995  cgd convert to new RCS id conventions.
 1.8 23-Jan-1995  christos I added the documented in the manual but not implemented variable expansions:

${#WORD}
${WORD%PAT}
${WORD%%PAT}
${WORD#PAT}
${WORD##PAT}
 1.7 14-Jun-1994  jtc From Christos:
1. Fix `-' quoting in [ ] expressions.
2. Fix expansion of variables in redirections
 1.6 11-Jun-1994  mycroft Add RCS ids.
 1.5 11-May-1994  jtc sync with 4.4lite
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.10.6.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.17.54.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.17.48.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.21.8.1 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.24.4.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.24.4.2 21-Apr-2020  martin Sync with HEAD
 1.24.4.1 10-Jun-2019  christos Sync with HEAD
 1.24.2.1 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.28.12.1 02-Aug-2025  perseant Sync with HEAD
 1.76 11-Nov-2024  kre This commit is intended to be what was intended to happen in the
commit of Sun Nov 10 01:22:24 UTC 2024, see:

http://mail-index.netbsd.org/source-changes/2024/11/10/msg154310.html

The commit message for that applies to this one (wholly). I believe that
the problem with that version which caused it to be reverted has been found
and fixed in this version (a necessary change was made as part of one of
the fixes, but the side-effect implications of that were missed -- bad bad me.)

In addition, I found some more issues with setting close-on-exec on other
command lines

With:
func 3>whatever

fd 3 (anything > 2) got close on exec set. That makes no difference
to the function itself (nothing gets exec'd therefore nothing gets closed)
but does to any exec that might happen running a command within the function.

I believe that if this is done (just as if "func" was a regular command,
and not a function) such open fds should be passed through to anything
they exec - unless the function (or other command) takes care to close the
fd passed to it, or explicitly turn on close-on exec.

I expect this usage to be quite rare, and not make much practical difference.

The same applies do builtin commands, but is even less relevant there, eg:

printf 3>whatever

would have set close-on-exec on fd 3 for printf. This is generally
completely immaterial, as printf - and most other built-in commands -
neither uses any fd other than (some of) 0 1 & 2, nor do they exec anything.

That is, except for the "exec" built-in which was the focus of the original
fix (mentioned above) and which should remain fixed here, and for the "."
command.

Because of that last one (".") close-on-exec should not be set on built-in
commands (any of them) for redirections on the command line. This will
almost never make a difference - any such redirections last only as long
as the built-in command lasts (same with functions) and so will generally
never care about the state of close-on-exec, and I have never seen a use
of the "." command with any redirections other than stderr (which is unaffected
here, fd's <= 2 never get close-on-exec set). That's probably why no-one
ever noticed.

There are still "fd issues" when running a (non #!) shell script, that
are hard to fix, which we should probably handle the way most other shells
have, by simply abandoning the optimisation of not exec'ing a whole new
shell (#! scripts do that exec) and just doing it that way. Issues solved!
One day.
 1.75 10-Nov-2024  kre Revert the recent change until I can work out how things are broken.
 1.74 10-Nov-2024  kre Back out the fcntl() 3rd arg -> (void *) change, leave it as an
untouched int. The other way seems to break things.
 1.73 10-Nov-2024  kre exec builtin command redirection fixes

Several changes, all related to the exec special built in command,
or to close on exec, one way or another. (Except a few white space
and comment additions, KNF, etc)

1. The bug found by Edgar Fuß reported in:
http://mail-index.netbsd.org/tech-userlevel/2024/11/05/msg014588.html
has been fixed, now "exec N>whatever" will set close-on-exec for fd N
(as do ksh versions, and allowed by POSIX, though other shells do not)
which has happened now for many years. But "exec cmd N>whatever"
(which looks like the same command when redirections are processed)
which was setting close-on-exec on N, now no longer does, so fd N
can be passed to cmd as an open fd.

For anyone who cares, the big block of change just after "case CMDBUILTIN:"
in evalcommand() in eval.c is the fix for this (one line replaced by
about 90 ... though most of that is comments or #if 0'd example code
for later). It is a bit ugly, and will get worse if our exec command
ever gets any options, as others have, but it does work.

2. when the exec builtin utility is used to alter the shell's redirections
it is now "all or nothing". Previously the redirections were executed
left to right. If one failed, no more were attempted, but the earlier
ones remained. This makes no practical difference to a non-interactive
shell, as a redirection error causes that shell to exit, but it makes
a difference to interactive shells. Now if a redirection fails, any
earlier ones which had been performed are undone. Note however that
side-effects of redirections (like creating, or truncating, files in
the filesystem, cannot be reversed - just the shell's file descriptors
returned to how they were before the error).

Similarly usage errors on exec now exist .. our exec takes no options
(but does handle "--" as POSIX says it must - has done for ages).
Until now, that was the only magic piece of exec, running
exec -a name somecommand
(which several other shells support) would attempt to exec the "-a"
command, and most likely fail, causing immediate exit from the shell.
Now that is a usage error - a non-interactive shell still exits, as
exec is a special builtin, and any error from a special builtin causes
a non-interactive shell to exit. But now, an interactive shell will
no longer exit (and any redirections that were on the command will be
undone, the same as for a redirection error).

3. When a "close on exec" file descriptor is temporarily saved, so the
same fd can be redirected for another command (only built-in commands
and functions matter, redirects for file system commands happen after
a fork() and at that stage if anything goes wrong, the child simply
exits - but for non-forking commands, doing something like printf >file
required the previous stdout to be saved elsewhere, "file" opened to
be the new stdout, then when printf is finished, the old stdout moved
back. Anyway, if the fd being moved had close on exec set, then
when it was moved back, the close on exec was lost. That is now fixed.

4. The fdflags command no longer allows setting close on exec on stdin,
stdout, or stderr - POSIX requires that those 3 fd's always be open
(to something) when any normal command is invoked. With close-on-exec
set on one of these, that is impossible, so simply refuse it (when
"exec N>file" sets close on exec, it only does it for N>2).

Minor changes (should be invisible)

a. The shell now keeps track of the highest fd number it sees doing
normal operations (there are a few internal pipe() calls that aren't
monitored and a couple of others, but in general the shell will now
know the highest fd it ever saw allocated to it). This is mostly
for debugging.

b. calls to fcntl() passing an int as the "arg" are now all properly
cast to the void * that the fcntl kernel is expecting to receive.
I suspect that makes no actual difference to anything, but ...
 1.72 22-Nov-2021  kre branches: 1.72.4;

PR bin/53550

Here we go again... One more time to redo how here docs are
processed (it has been a few years since the last time!)

This is actually a relatively minor change, mostly to timimg
(to just when things happen). Now here docs are expanded at the
same time the "filename" word in a redirect is expanded, rather than
later when the heredoc was being sent to its process. This actually
makes things more consistent - but does break one of the ATF tests
which was testing that we were (effectively) internally inconsistent
in this area.

Not all shells agree on the context in which redirection expansions
should happen, some make any side effects visible to the parent shell
(the majority do) others do the redirection expansions in a subshell
so any side effcts are lost. We used to have a foot in each camp,
with the majority for everything but here docs, and the minority for
here docs. Now we're all the way with LBJ ... (or something like that).
 1.71 16-Nov-2021  kre Detect write errors to stdout, and exit(1) from some built-in
commands which (primarily) are used just to generate output
(or with a particular option combination do so).
 1.70 10-Nov-2021  kre DEBUG mode changes only. NFC (NC) for any normally compiled shell.

Mostly adding DEBUG mode tracing (when appropriate verbose tracing
is enabled generally) whenever a shell (including sushell) process
exits, so shells that the tracing should indicate why ehslls that
vanish did that.

Note for future investigators: if the relevant tracing is enabled,
and a (sub-)shell still simply seems to have vanished without trace,
the likely cause is that it was killed by a signal - and of those,
the most common that occurs is SIGPIPE.
 1.69 15-Sep-2021  kre Fix an ordering error in the previous (and even earlier, going back
a way, but made more serious with the recent changes).

The n>&n operation (more or less a no-op, except it clears CLOEXEC)
should precede almost everything else - and simply be made to fail if
an attempt is made to apply it to a sh internal fd.

We were renumbering the internal fd (the n> part considered first)
which was dumb, but OK, before, but now rejecting the operation
(the >&n) part when n should not be visible to the script. That
made something of a mess (and could lead to the shell believing its
job control tty was at a fd it never got moved to).

Do things in the correct order, and simply fail that case for internal
fds (for every other n>xxx for any xxx sh simply renumbers its internal fd
n to some other fd before attempting the operation, even n>&- ... those are
all fine).

[In all the above the '>' is used in place of any redirect operator].
 1.68 15-Sep-2021  kre Improve the solution for the 2nd access to a fd which shouldn't
be available ("13") issue reported by Jan Schaumann on netbsd-users.

This fixes a bug in the earlier fix (a day or so ago) which could allow the
shell's idea of which fd range was in use by the script to get wildly
incorrect, but now also actually looks to see which fd's are in use as
renamed other user fd's during the lifetime of a redirection which needs
to be able to be undone (most redirections occur after a fork and are
permanent in the child process). Attempting to access such a fd (as with
attempts to access a fd in use by the shell for its own purposes) is treated
as an attempt to access a closed fd (EBADF). Attempting to reuse the fd
for some other purpose (which should be rare, even for scripts attempting
to cause problems, since the shell generally knows which fds the script
wants to use, and avoids them) will cause the renamed (renumbered) fd
to be renamed again (moved aside to some other available fd), just as
happens with the shell's private fds.

Also, when a generic fd is required, don't give up because of EMFILE
or similar unless there are no available fds at all (we might prefer >10
or bigger, but if there are none there, use anything). This avoids
redirection errors when ulimit -n has been set small, and all the fds >10
that are available have been used, but we need somewhere to park the old
user of a fd while we reuse that fd for the redirection.
 1.67 14-Sep-2021  kre Deal with some issues where fds intended only for internal use
by the shell were available for manipulation by scripts (or the user).
These issues were reported by Jan Schaumann on netbsd-users.

The first allows the user to reference sh internal fds, and is
a simple fix - any sh internal fd is simply treated as if it were closed
when referenced by the script. These fds can be discovered by
examining /proc/N/fd so it is not difficult for a script to discover
which fd it should attempt to access.

The second allows the user to reference a user level fd which is
one that is normally available to it, but at a point where it should
no longer be visible (when that fd has been redirected, for a built
in command, so the original fd needs to be saved so it can be restored,
the saving fd should not be accessible). It is not as easy for the
script to determine which fd to attempt here, as the relevant one
exists only during the lifetime of a built-in command (and similar),
but there are ways in some cases (aside from looking at /proc from
another process).

Fix this one by watching which fds the user script is attempting
to use, and avoid using those as temporary fds. This is possible in
this case as we know what command is being run, before we need to
save the fds it uses. That's different from the earlier case where
when the shell allocates its fds we have no idea what it might
reference later.

Also clean up a couple of other minor code issues (NFC intended) that
I noticed while here...
 1.66 01-Mar-2019  kre 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.
 1.65 01-Mar-2019  kre 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.
 1.64 09-Feb-2019  kre DEBUG mode change only. Add one extra trace point. NFC for normal builds.
 1.63 09-Feb-2019  kre 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.
 1.62 26-Nov-2018  kamil Fix typo: O_ALTIO -> O_ALT_IO

Noted by @jbeich via GitHub.
 1.61 23-Nov-2018  kre 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
 1.60 13-Aug-2018  kre NFC: DEBUG (compile time) mode only change:
Add some extra redirection (fd manipulation) tracing.
While here, some white space fixes, and very minor KNF.
 1.59 15-Nov-2017  kre branches: 1.59.2; 1.59.4;
DEBUG mode only change. Add some tracing. NFC (without DEBUG).
 1.58 30-Jun-2017  kre Include redirections in trace output from "set -x"
 1.57 29-May-2017  kre branches: 1.57.2;

Now that the shell is protecting its internal fds properly, moving
them whenever the user tries to step on one, we can change our behaviour
back to what the kernel considers to be that of a well behaved shell
(wrt file descriptor usage). If our user causes problems, we will soon
move into recalcitrant process territory, but that should normally be
rare. This should reduce kernel overheads a little.
 1.56 18-May-2017  kre Allow abbreviations of option names for the "fdflags -s" command.
While documenting that, cleanup markup of the fdflags section of the
man page.
 1.55 14-May-2017  kre When opening a file descritor with "exec n>/file" (and similar) only
set the close-on-exec bit when not in posix mode (to comply with posix...)
OK: christos@
 1.54 29-Apr-2017  kre Keep track of which file descriptors the shell is using for its
own purposes, and move them elsewhere whenever a user redirection
happens to pick the same number. With this we can move the shell
file descriptors back to lower values (be slightly kinder to the kernel)
since we can no longer clash. (Also get rid of a little old unneeded code.)

This also completes the fdflags command, which no longer permits access
to (by way or either obtaining, or changing) the shell's internal fds.
 1.53 22-Apr-2017  kre branches: 1.53.2;

Keep track of the biggest fd used by, or available to, the user/script
and use that to control which fd's are examined by a (bare) fdflags
(with no fd args).

Usually this will mean that fdflags will no longer show the shell's
internal use fds, only user fds.

This is only a partial fix however, a user can easily discover the
shell's fd usage (eg: using fstat) and can then still use fdflags to
manipulate those fds (or even send output to them).

The shell needs to monitor its own fd usage better, and keep out of
the way of user fds - coming sometime later...
 1.52 22-Apr-2017  kre When verifying the size of the fd arg for fdflags skip leading 0's
(fdflags 0000000001 should work, fdflags 10000000 should not)
 1.51 03-Feb-2017  kre Fiddle the (new) fdflags implementation:

Remove some unnecessary cuteness that limited error reporting.
Permit just one -s arg to fdflags
Be deterministic in the case of fdflags -s +cloexec,-cloexec 0
(and similar) - use the last specified, always.
Allow:
FD_0_FLAGS=$( fdflags -v 0 )
# do stuff, manipulating the flags
fdflags -s "FD_0_FLAGS" 0
to save/restore flags for a fd.
Correctly mask result of fcntl(fd, F_GETFD) with FD_CLOEXEC as the
specs require before deciding close on exec is set.

Improve portability as a tool, don't assume strtoi(), nor __arraycount()
and avoid needlessly requiring recent C versions (ie: there's no need to
sprinkle declarations in the middle of the code, it just makes them hard
to find, and benefits nothing.)

Still to do: As currently implemented, both user, and shell internal fds
are reported, and can be manipulated. Allowing users to touch the shell's
internal fds is bogus, and providing this easy way to allow users to
discover which values they have is poor. Fixing this means getting rid
of the use of fcntl(F_MAXFD) and replacing it with a shell maintained
memory of what fds the user (script) has allocated. The shell's fd
manipulation really still needs major work (including properly fixing
bin/48875)
 1.50 02-Feb-2017  christos Add fdflags builtin. Discussed with Chet and he has implemented it for
bash too.
 1.49 21-Jan-2017  christos Don't let set cloexec for "exec n>&1" like ksh does (but not bash). Unit
tests pass... If we don't like that, we should add some unittests that fail.
 1.48 10-Jan-2017  christos branches: 1.48.2;
add missing <sys/stat.h>
 1.47 12-May-2016  kre branches: 1.47.2;

More work on file descriptors... This is the copyfd() cleanup.
copyfd() duplicates file descriptors - it used to be widely used,
but these days has seen its popularity dwindle. Strip it of an
option that ceased to be variable (simplifying code) and cause all
its users to check its result, so it does not need to handle errors
itself (simplifying code further), and make it become a private inernal
routine in redir.c (all callers from other places have switched to a
more modern interface.) Make sure we error() if N>&N fails (if N is closed.)
 1.46 09-May-2016  kre Finish the fd reassignment fixes from 1.43 and 1.45 ... if we are moving
a fd to an unspecified high fd number, we certainly do not want to hand
that high fd off to other processes after an exec, so always set close-on-exec
on the result (even if lack of fd's means no fd alteration happens.)
This will (eventually) allow some other code that sets close-on-exec to
be removed, but for now, doing it twice won't hurt. Also, in a N>&M
type redirection, do not set close-on-exec if we don't want it.

OK christos@
 1.45 08-May-2016  kre PR bin/51123 - make >&- work in all cases, and while doing that fix
things so that >/dev/stdout </dev/stdin (etc) work as well (in all cases).

ok christos@
 1.44 08-May-2016  kre Whitespace fixes. No functional change.
 1.43 02-May-2016  christos Fix handing of user file descriptors outside the 0..9 range.
Also, move (most of) the shell's internal use fd's to much
higher values (depending upon what ulimit -n allows) so they
are less likely to clash with user supplied fd numbers. A future
patch will (hopefully) avoid this problem completely by dynamically
moving the shell's internal fds around as needed. (From kre@)
 1.42 13-Mar-2016  christos dedup.
 1.41 13-Mar-2016  christos Test for REDIR_KEEP in the non-copy case:
$ cat other1
#!/bin/sh
./other2 3>out

$ cat other2
#!/bin/sh
echo other2 1>&3

$ ./other1
 1.40 12-Mar-2016  christos Don't close-on-exec redirections created explicitly for the command being
ran; i.e. we want this to work:
$ cat succ1
#!/bin/sh
./succ2 6>out

$ cat succ2
#!/bin/sh
echo succ2 >&6

$ ./succ1

And this to fail:
$ cat fail1
#!/bin/sh
exec 6> out
echo "fail1" >&6
./fail2
exec 6>&-

$ cat fail2
#!obj.amd64/sh
echo "fail2" >&6

$ ./fail1
./fail2: 6: Bad file descriptor

XXX: Do we want a -k (keep flag on exec to make redirections not close-on-exec?
 1.39 04-Jan-2016  christos PR/50619: Fix reversed test.
 1.38 04-Jan-2016  christos Don't leak redirected rescriptors to exec'ed processes. This is what ksh
does, but bash does not. For example:

$ cat test1
#!/bin/sh
exec 6> out
echo "test" >&6
sh ./test2
exec 6>&-
$ cat test2
echo "test2" >&6
$ ./test1
./test2: 6: Bad file descriptor

This fixes by side effect the problem of the rc system leaking file descriptors
7 and 8 to all starting daemons:

$ fstat -p 1359
USER CMD PID FD MOUNT INUM MODE SZ|DV R/W
root powerd 1359 wd / 2 drwxr-xr-x 512 r
root powerd 1359 0 / 63029 crw-rw-rw- null rw
root powerd 1359 1 / 63029 crw-rw-rw- null rw
root powerd 1359 2 / 63029 crw-rw-rw- null rw
root powerd 1359 3* kqueue pending 0
root powerd 1359 4 / 64463 crw-r----- power r
root powerd 1359 7 flags 0x80034<ISTTY,MPSAFE,LOCKSWORK,CLEAN>
root powerd 1359 8 flags 0x80034<ISTTY,MPSAFE,LOCKSWORK,CLEAN>
root powerd 1359 9* pipe 0xfffffe815d7bfdc0 -> 0x0 w

Note fd=7,8 pointing to the revoked pty from the parent rc process.
 1.37 23-Oct-2014  christos simplify and eliminate TOCTOU.
 1.36 15-Oct-2014  christos PR/48201: Miwa Susumu: Fix set -C (no clobber) for POSIX; from FreeBSD
Can't use O_EXCL because of device nodes; also truncate.
 1.35 27-Jun-2013  yamt fix descriptor leaks. PR/47805

this fix was taken from FreeBSD SVN rev 199953 (Jilles Tjoelker)
------------------------------------------------------------------------
r199953 | jilles | 2009-11-30 07:33:59 +0900 (Mon, 30 Nov 2009) | 16 lines

Fix some cases where file descriptors from redirections leak to programs.

- Redirecting fds that were not open before kept two copies of the
redirected file.
sh -c '{ :; } 7>/dev/null; fstat -p $$; true'
(both fd 7 and 10 remained open)
- File descriptors used to restore things after redirection were not
set close-on-exec, instead they were explicitly closed before executing
a program normally and before executing a shell procedure. The latter
must remain but the former is replaced by close-on-exec.
sh -c 'exec 7</; { exec fstat -p $$; } 7>/dev/null; true'
(fd 10 remained open)

The examples above are simpler than the testsuite because I do not want to
use fstat or procstat in the testsuite.
 1.34 12-Jun-2013  yamt constify
 1.33 20-Mar-2012  matt branches: 1.33.2;
Use C89 function definitions
 1.32 31-Aug-2011  plunky branches: 1.32.2;
NULL does not need a cast
 1.31 17-Feb-2011  pooka Tell copyfd if the caller wants the exact tofd to just fd >= tofd.
Fixes "echo foo > /rump/bar" in a rump hijacked shell.

reviewed by christos
 1.30 21-Jan-2008  msaitoh branches: 1.30.22;
Conform to XCU Section 2.8.2 (Exit Status for Commands)
 1.29 08-Jul-2004  christos branches: 1.29.2; 1.29.10; 1.29.16;
PR/25699: David Laight: sh(1) hangs opening a named pipe as stdin for
background process
This happens because we vfork, and then open a named pipe with O_RDONLY
and block in the child. We avoid this, by opening the file with O_NONBLOCK,
and then reset it if we are vforked. XXX: this is an ugly fix.
 1.28 07-Aug-2003  agc branches: 1.28.2;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.27 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.26 28-Sep-2002  christos Revert previous change. No need to save rootshell. It is only affecting
the non-vfork case. Having said that, it would be nice if pipelines of
simple commands were vforked too. Right now they are not.
Explain that setpgid() might fail because we are doing it both in the
parent and the child case, because we don't know which one will come
first.
Suspending a pipeline prints %1 Suspended n times where n is the number
of processes, but that was there before. It is easy to fix, but I'll
leave the code alone for now.
 1.25 27-Sep-2002  christos Deal with rootshell not being maintained correctly in the vfork() case.
Propagate isroot, throughout the eval process and maintain it properly.
Fixes sleep 10 | cat^C not exiting because sleep and cat ended up in
their own process groups, because wasroot was always true in the children.
 1.24 27-Sep-2002  christos VFork()ing shell: From elric@netbsd.org:
Plus my changes:
- walking process group fix in foregrounding a job.
- reset of process group in parent shell if interrupted before the wait.
- move INTON lower in the dowait so that the job structure is
consistent.
- error check all setpgid(), tcsetpgrp() calls.
- eliminate unneeded strpgid() call.
- check that we don't belong in the process group before we try to
set it.
 1.23 15-May-2002  christos implement noclobber. From Ben Harris, with minor tweaks from me. Two
unimplemented comments to go. Go Ben!
 1.22 22-May-2000  elric branches: 1.22.6;
Back out previous vfork changes.
 1.21 13-May-2000  elric Now we use vfork(2) instead of fork(2) when we can.
 1.20 04-Feb-1999  christos PR/4966: Joel Reicher: Implement <> redirections which are documented in
the man page.
 1.19 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.18 28-Jul-1998  mycroft Delint.
 1.17 27-Jul-1998  christos PR/5848: David Holland: Use PIPE_BUF instead of hardcoding 4k
 1.16 04-Jul-1997  christos Fix compiler warnings.
 1.15 21-Apr-1997  christos PR/3452: Jerry Peek: Redirections of unopened fd to file failed.
for arg in a b c
do
echo hi this is $arg 1>&3
done 3> foo
 1.14 11-Jan-1997  tls kill 'register'
 1.13 16-Oct-1996  christos PR/2808: fix redirection to the same file descriptor
better error messages for failed pipes
(from FreeBSD)
 1.12 11-May-1995  christos branches: 1.12.6;
Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.11 21-Mar-1995  cgd convert to new RCS id conventions.
 1.10 23-Dec-1994  cgd pull prototypes into scope for string functions.
 1.9 05-Dec-1994  cgd clean up further. more patches from Jim Jegers
 1.8 11-Jun-1994  mycroft Add RCS ids.
 1.7 11-May-1994  jtc sync with 4.4lite
 1.6 25-Apr-1994  cgd lseek long lossage.
 1.5 01-Aug-1993  mycroft Add RCS identifiers.
 1.4 02-May-1993  sef Jim "wilson@moria.cygnus.com" Wilson's patches to make C News (and other
things) work.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.12.6.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.22.6.1 27-Mar-2002  elric Doing the vfork work on ash on a branch to try to shake out the
problems before I expose everyone to them. This checkin represents
a merge of the prior work, which I backed out a while ago, to the
HEAD only and does not incorporate any additional bugfixes. The
additional bugfixes and code-cleanup will occur in later checkins.

For reference the patches that were used are:
cvs diff -kk -r1.51 -r1.55 eval.c | patch
cvs diff -kk -r1.27 -r1.28 exec.c | patch
cvs diff -kk -r1.15 -r1.16 exec.h | patch
cvs diff -kk -r1.32 -r1.33 input.c | patch
cvs diff -kk -r1.10 -r1.11 input.h | patch
cvs diff -kk -r1.32 -r1.35 jobs.c | patch
cvs diff -kk -r1.9 -r1.11 jobs.h | patch
cvs diff -kk -r1.36 -r1.37 main.c | patch
cvs diff -kk -r1.20 -r1.21 redir.c | patch
cvs diff -kk -r1.10 -r1.11 redir.h | patch
cvs diff -kk -r1.10 -r1.12 shell.h | patch
cvs diff -kk -r1.22 -r1.23 trap.c | patch
cvs diff -kk -r1.12 -r1.13 trap.h | patch
cvs diff -kk -r1.23 -r1.24 var.c | patch
cvs diff -kk -r1.16 -r1.17 var.h | patch

All other changes were simply the resolution of the resulting
conflicts, which occured only in the merge of jobs.c.

Begins to address PR: bin/5475
 1.28.2.1 22-Aug-2004  tron Pull up revision 1.29 (requested by chs in ticket #777):
PR/25699: David Laight: sh(1) hangs opening a named pipe as stdin for
background process
This happens because we vfork, and then open a named pipe with O_RDONLY
and block in the child. We avoid this, by opening the file with O_NONBLOCK,
and then reset it if we are vforked. XXX: this is an ugly fix.
 1.29.16.1 23-Mar-2008  matt sync with HEAD
 1.29.10.1 26-Feb-2009  snj Pull up following revision(s) (requested by msaitoh in ticket #1281):
bin/sh/redir.c: revision 1.30
Conform to XCU Section 2.8.2 (Exit Status for Commands)
 1.29.2.1 26-Feb-2009  snj Pull up following revision(s) (requested by msaitoh in ticket #1992):
bin/sh/redir.c: revision 1.30
Conform to XCU Section 2.8.2 (Exit Status for Commands)
 1.30.22.1 05-Mar-2011  bouyer Sync with HEAD
 1.32.2.4 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.32.2.3 12-Jun-2013  yamt revert the previous (wrong branch)
 1.32.2.2 12-Jun-2013  yamt constify
 1.32.2.1 17-Apr-2012  yamt sync with head
 1.33.2.2 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.33.2.1 23-Jun-2013  tls resync from head
 1.47.2.2 26-Apr-2017  pgoyette Sync with HEAD
 1.47.2.1 20-Mar-2017  pgoyette Sync with HEAD
 1.48.2.1 21-Apr-2017  bouyer Sync with HEAD
 1.53.2.2 19-May-2017  pgoyette Resolve conflicts from previous merge (all resulting from $NetBSD
keywork expansion)
 1.53.2.1 02-May-2017  pgoyette Sync with HEAD - tag prg-localcount2-base1
 1.57.2.2 07-Dec-2018  martin Pull up following revision(s) (requested by kre in ticket #1125):

bin/sh/redir.c: revision 1.61

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
 1.57.2.1 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.59.4.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.59.4.2 21-Apr-2020  martin Sync with HEAD
 1.59.4.1 10-Jun-2019  christos Sync with HEAD
 1.59.2.3 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.59.2.2 26-Nov-2018  pgoyette Sync with HEAD, resolve a couple of conflicts
 1.59.2.1 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.72.4.1 02-Aug-2025  perseant Sync with HEAD
 1.29 11-Nov-2024  kre This commit is intended to be what was intended to happen in the
commit of Sun Nov 10 01:22:24 UTC 2024, see:

http://mail-index.netbsd.org/source-changes/2024/11/10/msg154310.html

The commit message for that applies to this one (wholly). I believe that
the problem with that version which caused it to be reverted has been found
and fixed in this version (a necessary change was made as part of one of
the fixes, but the side-effect implications of that were missed -- bad bad me.)

In addition, I found some more issues with setting close-on-exec on other
command lines

With:
func 3>whatever

fd 3 (anything > 2) got close on exec set. That makes no difference
to the function itself (nothing gets exec'd therefore nothing gets closed)
but does to any exec that might happen running a command within the function.

I believe that if this is done (just as if "func" was a regular command,
and not a function) such open fds should be passed through to anything
they exec - unless the function (or other command) takes care to close the
fd passed to it, or explicitly turn on close-on exec.

I expect this usage to be quite rare, and not make much practical difference.

The same applies do builtin commands, but is even less relevant there, eg:

printf 3>whatever

would have set close-on-exec on fd 3 for printf. This is generally
completely immaterial, as printf - and most other built-in commands -
neither uses any fd other than (some of) 0 1 & 2, nor do they exec anything.

That is, except for the "exec" built-in which was the focus of the original
fix (mentioned above) and which should remain fixed here, and for the "."
command.

Because of that last one (".") close-on-exec should not be set on built-in
commands (any of them) for redirections on the command line. This will
almost never make a difference - any such redirections last only as long
as the built-in command lasts (same with functions) and so will generally
never care about the state of close-on-exec, and I have never seen a use
of the "." command with any redirections other than stderr (which is unaffected
here, fd's <= 2 never get close-on-exec set). That's probably why no-one
ever noticed.

There are still "fd issues" when running a (non #!) shell script, that
are hard to fix, which we should probably handle the way most other shells
have, by simply abandoning the optimisation of not exec'ing a whole new
shell (#! scripts do that exec) and just doing it that way. Issues solved!
One day.
 1.28 10-Nov-2024  kre Revert the recent change until I can work out how things are broken.
 1.27 10-Nov-2024  kre exec builtin command redirection fixes

Several changes, all related to the exec special built in command,
or to close on exec, one way or another. (Except a few white space
and comment additions, KNF, etc)

1. The bug found by Edgar Fuß reported in:
http://mail-index.netbsd.org/tech-userlevel/2024/11/05/msg014588.html
has been fixed, now "exec N>whatever" will set close-on-exec for fd N
(as do ksh versions, and allowed by POSIX, though other shells do not)
which has happened now for many years. But "exec cmd N>whatever"
(which looks like the same command when redirections are processed)
which was setting close-on-exec on N, now no longer does, so fd N
can be passed to cmd as an open fd.

For anyone who cares, the big block of change just after "case CMDBUILTIN:"
in evalcommand() in eval.c is the fix for this (one line replaced by
about 90 ... though most of that is comments or #if 0'd example code
for later). It is a bit ugly, and will get worse if our exec command
ever gets any options, as others have, but it does work.

2. when the exec builtin utility is used to alter the shell's redirections
it is now "all or nothing". Previously the redirections were executed
left to right. If one failed, no more were attempted, but the earlier
ones remained. This makes no practical difference to a non-interactive
shell, as a redirection error causes that shell to exit, but it makes
a difference to interactive shells. Now if a redirection fails, any
earlier ones which had been performed are undone. Note however that
side-effects of redirections (like creating, or truncating, files in
the filesystem, cannot be reversed - just the shell's file descriptors
returned to how they were before the error).

Similarly usage errors on exec now exist .. our exec takes no options
(but does handle "--" as POSIX says it must - has done for ages).
Until now, that was the only magic piece of exec, running
exec -a name somecommand
(which several other shells support) would attempt to exec the "-a"
command, and most likely fail, causing immediate exit from the shell.
Now that is a usage error - a non-interactive shell still exits, as
exec is a special builtin, and any error from a special builtin causes
a non-interactive shell to exit. But now, an interactive shell will
no longer exit (and any redirections that were on the command will be
undone, the same as for a redirection error).

3. When a "close on exec" file descriptor is temporarily saved, so the
same fd can be redirected for another command (only built-in commands
and functions matter, redirects for file system commands happen after
a fork() and at that stage if anything goes wrong, the child simply
exits - but for non-forking commands, doing something like printf >file
required the previous stdout to be saved elsewhere, "file" opened to
be the new stdout, then when printf is finished, the old stdout moved
back. Anyway, if the fd being moved had close on exec set, then
when it was moved back, the close on exec was lost. That is now fixed.

4. The fdflags command no longer allows setting close on exec on stdin,
stdout, or stderr - POSIX requires that those 3 fd's always be open
(to something) when any normal command is invoked. With close-on-exec
set on one of these, that is impossible, so simply refuse it (when
"exec N>file" sets close on exec, it only does it for N>2).

Minor changes (should be invisible)

a. The shell now keeps track of the highest fd number it sees doing
normal operations (there are a few internal pipe() calls that aren't
monitored and a couple of others, but in general the shell will now
know the highest fd it ever saw allocated to it). This is mostly
for debugging.

b. calls to fcntl() passing an int as the "arg" are now all properly
cast to the void * that the fcntl kernel is expecting to receive.
I suspect that makes no actual difference to anything, but ...
 1.26 15-Sep-2021  kre branches: 1.26.4;
Improve the solution for the 2nd access to a fd which shouldn't
be available ("13") issue reported by Jan Schaumann on netbsd-users.

This fixes a bug in the earlier fix (a day or so ago) which could allow the
shell's idea of which fd range was in use by the script to get wildly
incorrect, but now also actually looks to see which fd's are in use as
renamed other user fd's during the lifetime of a redirection which needs
to be able to be undone (most redirections occur after a fork and are
permanent in the child process). Attempting to access such a fd (as with
attempts to access a fd in use by the shell for its own purposes) is treated
as an attempt to access a closed fd (EBADF). Attempting to reuse the fd
for some other purpose (which should be rare, even for scripts attempting
to cause problems, since the shell generally knows which fds the script
wants to use, and avoids them) will cause the renamed (renumbered) fd
to be renamed again (moved aside to some other available fd), just as
happens with the shell's private fds.

Also, when a generic fd is required, don't give up because of EMFILE
or similar unless there are no available fds at all (we might prefer >10
or bigger, but if there are none there, use anything). This avoids
redirection errors when ulimit -n has been set small, and all the fds >10
that are available have been used, but we need somewhere to park the old
user of a fd while we reuse that fd for the redirection.
 1.25 03-Apr-2020  joerg Don't define max_user_fd in the header.
 1.24 30-Jun-2017  kre branches: 1.24.6;

Include redirections in trace output from "set -x"
 1.23 29-Apr-2017  kre branches: 1.23.2;

Keep track of which file descriptors the shell is using for its
own purposes, and move them elsewhere whenever a user redirection
happens to pick the same number. With this we can move the shell
file descriptors back to lower values (be slightly kinder to the kernel)
since we can no longer clash. (Also get rid of a little old unneeded code.)

This also completes the fdflags command, which no longer permits access
to (by way or either obtaining, or changing) the shell's internal fds.
 1.22 22-Apr-2017  kre branches: 1.22.2;

Keep track of the biggest fd used by, or available to, the user/script
and use that to control which fd's are examined by a (bare) fdflags
(with no fd args).

Usually this will mean that fdflags will no longer show the shell's
internal use fds, only user fds.

This is only a partial fix however, a user can easily discover the
shell's fd usage (eg: using fstat) and can then still use fdflags to
manipulate those fds (or even send output to them).

The shell needs to monitor its own fd usage better, and keep out of
the way of user fds - coming sometime later...
 1.21 12-May-2016  kre branches: 1.21.2;

More work on file descriptors... This is the copyfd() cleanup.
copyfd() duplicates file descriptors - it used to be widely used,
but these days has seen its popularity dwindle. Strip it of an
option that ceased to be variable (simplifying code) and cause all
its users to check its result, so it does not need to handle errors
itself (simplifying code further), and make it become a private inernal
routine in redir.c (all callers from other places have switched to a
more modern interface.) Make sure we error() if N>&N fails (if N is closed.)
 1.20 03-May-2016  kre Remove unnecessary extern var declaration that was a
remnant of an earlier version of the previous (fd>10) fixes.

ok christos@
 1.19 02-May-2016  christos Fix handing of user file descriptors outside the 0..9 range.
Also, move (most of) the shell's internal use fd's to much
higher values (depending upon what ulimit -n allows) so they
are less likely to clash with user supplied fd numbers. A future
patch will (hopefully) avoid this problem completely by dynamically
moving the shell's internal fds around as needed. (From kre@)
 1.18 12-Mar-2016  christos Don't close-on-exec redirections created explicitly for the command being
ran; i.e. we want this to work:
$ cat succ1
#!/bin/sh
./succ2 6>out

$ cat succ2
#!/bin/sh
echo succ2 >&6

$ ./succ1

And this to fail:
$ cat fail1
#!/bin/sh
exec 6> out
echo "fail1" >&6
./fail2
exec 6>&-

$ cat fail2
#!obj.amd64/sh
echo "fail2" >&6

$ ./fail1
./fail2: 6: Bad file descriptor

XXX: Do we want a -k (keep flag on exec to make redirections not close-on-exec?
 1.17 04-Jan-2016  christos Don't leak redirected rescriptors to exec'ed processes. This is what ksh
does, but bash does not. For example:

$ cat test1
#!/bin/sh
exec 6> out
echo "test" >&6
sh ./test2
exec 6>&-
$ cat test2
echo "test2" >&6
$ ./test1
./test2: 6: Bad file descriptor

This fixes by side effect the problem of the rc system leaking file descriptors
7 and 8 to all starting daemons:

$ fstat -p 1359
USER CMD PID FD MOUNT INUM MODE SZ|DV R/W
root powerd 1359 wd / 2 drwxr-xr-x 512 r
root powerd 1359 0 / 63029 crw-rw-rw- null rw
root powerd 1359 1 / 63029 crw-rw-rw- null rw
root powerd 1359 2 / 63029 crw-rw-rw- null rw
root powerd 1359 3* kqueue pending 0
root powerd 1359 4 / 64463 crw-r----- power r
root powerd 1359 7 flags 0x80034<ISTTY,MPSAFE,LOCKSWORK,CLEAN>
root powerd 1359 8 flags 0x80034<ISTTY,MPSAFE,LOCKSWORK,CLEAN>
root powerd 1359 9* pipe 0xfffffe815d7bfdc0 -> 0x0 w

Note fd=7,8 pointing to the revoked pty from the parent rc process.
 1.16 17-Feb-2011  pooka Tell copyfd if the caller wants the exact tofd to just fd >= tofd.
Fixes "echo foo > /rump/bar" in a rump hijacked shell.

reviewed by christos
 1.15 07-Aug-2003  agc branches: 1.15.50;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.14 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.13 27-Sep-2002  christos VFork()ing shell: From elric@netbsd.org:
Plus my changes:
- walking process group fix in foregrounding a job.
- reset of process group in parent shell if interrupted before the wait.
- move INTON lower in the dowait so that the job structure is
consistent.
- error check all setpgid(), tcsetpgrp() calls.
- eliminate unneeded strpgid() call.
- check that we don't belong in the process group before we try to
set it.
 1.12 22-May-2000  elric branches: 1.12.6;
Back out previous vfork changes.
 1.11 13-May-2000  elric Now we use vfork(2) instead of fork(2) when we can.
 1.10 16-Oct-1996  christos PR/2808: Remove trailing whitespace (from FreeBSD)
 1.9 11-May-1995  christos branches: 1.9.6;
Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.8 21-Mar-1995  cgd convert to new RCS id conventions.
 1.7 11-Jun-1994  mycroft Add RCS ids.
 1.6 11-May-1994  jtc sync with 4.4lite
 1.5 01-Aug-1993  mycroft Add RCS identifiers.
 1.4 02-May-1993  sef Jim "wilson@moria.cygnus.com" Wilson's patches to make C News (and other
things) work.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.9.6.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.12.6.1 27-Mar-2002  elric Doing the vfork work on ash on a branch to try to shake out the
problems before I expose everyone to them. This checkin represents
a merge of the prior work, which I backed out a while ago, to the
HEAD only and does not incorporate any additional bugfixes. The
additional bugfixes and code-cleanup will occur in later checkins.

For reference the patches that were used are:
cvs diff -kk -r1.51 -r1.55 eval.c | patch
cvs diff -kk -r1.27 -r1.28 exec.c | patch
cvs diff -kk -r1.15 -r1.16 exec.h | patch
cvs diff -kk -r1.32 -r1.33 input.c | patch
cvs diff -kk -r1.10 -r1.11 input.h | patch
cvs diff -kk -r1.32 -r1.35 jobs.c | patch
cvs diff -kk -r1.9 -r1.11 jobs.h | patch
cvs diff -kk -r1.36 -r1.37 main.c | patch
cvs diff -kk -r1.20 -r1.21 redir.c | patch
cvs diff -kk -r1.10 -r1.11 redir.h | patch
cvs diff -kk -r1.10 -r1.12 shell.h | patch
cvs diff -kk -r1.22 -r1.23 trap.c | patch
cvs diff -kk -r1.12 -r1.13 trap.h | patch
cvs diff -kk -r1.23 -r1.24 var.c | patch
cvs diff -kk -r1.16 -r1.17 var.h | patch

All other changes were simply the resolution of the resulting
conflicts, which occured only in the merge of jobs.c.

Begins to address PR: bin/5475
 1.15.50.1 05-Mar-2011  bouyer Sync with HEAD
 1.21.2.1 26-Apr-2017  pgoyette Sync with HEAD
 1.22.2.1 02-May-2017  pgoyette Sync with HEAD - tag prg-localcount2-base1
 1.23.2.1 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.24.6.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.24.6.2 21-Apr-2020  martin Sync with HEAD
 1.24.6.1 08-Apr-2020  martin Merge changes from current as of 20200406
 1.26.4.1 02-Aug-2025  perseant Sync with HEAD
 1.272 24-Jun-2025  andvar sh(1): fix few relatively recently introduced typos (rev 1.265).
s/agument/argument/
s/suport/support/
s/prvileges/privileges/
 1.271 09-Apr-2025  kre Better explain the expansions in case statements.

nb: no Dd bump, this change is not significant enough to warrant that.
 1.270 11-Nov-2024  kre This commit is intended to be what was intended to happen in the
commit of Sun Nov 10 01:22:24 UTC 2024, see:

http://mail-index.netbsd.org/source-changes/2024/11/10/msg154310.html

The commit message for that applies to this one (wholly). I believe that
the problem with that version which caused it to be reverted has been found
and fixed in this version (a necessary change was made as part of one of
the fixes, but the side-effect implications of that were missed -- bad bad me.)

In addition, I found some more issues with setting close-on-exec on other
command lines

With:
func 3>whatever

fd 3 (anything > 2) got close on exec set. That makes no difference
to the function itself (nothing gets exec'd therefore nothing gets closed)
but does to any exec that might happen running a command within the function.

I believe that if this is done (just as if "func" was a regular command,
and not a function) such open fds should be passed through to anything
they exec - unless the function (or other command) takes care to close the
fd passed to it, or explicitly turn on close-on exec.

I expect this usage to be quite rare, and not make much practical difference.

The same applies do builtin commands, but is even less relevant there, eg:

printf 3>whatever

would have set close-on-exec on fd 3 for printf. This is generally
completely immaterial, as printf - and most other built-in commands -
neither uses any fd other than (some of) 0 1 & 2, nor do they exec anything.

That is, except for the "exec" built-in which was the focus of the original
fix (mentioned above) and which should remain fixed here, and for the "."
command.

Because of that last one (".") close-on-exec should not be set on built-in
commands (any of them) for redirections on the command line. This will
almost never make a difference - any such redirections last only as long
as the built-in command lasts (same with functions) and so will generally
never care about the state of close-on-exec, and I have never seen a use
of the "." command with any redirections other than stderr (which is unaffected
here, fd's <= 2 never get close-on-exec set). That's probably why no-one
ever noticed.

There are still "fd issues" when running a (non #!) shell script, that
are hard to fix, which we should probably handle the way most other shells
have, by simply abandoning the optimisation of not exec'ing a whole new
shell (#! scripts do that exec) and just doing it that way. Issues solved!
One day.
 1.269 10-Nov-2024  kre Revert the recent change until I can work out how things are broken.
 1.268 10-Nov-2024  kre exec builtin command redirection fixes

Several changes, all related to the exec special built in command,
or to close on exec, one way or another. (Except a few white space
and comment additions, KNF, etc)

1. The bug found by Edgar Fuß reported in:
http://mail-index.netbsd.org/tech-userlevel/2024/11/05/msg014588.html
has been fixed, now "exec N>whatever" will set close-on-exec for fd N
(as do ksh versions, and allowed by POSIX, though other shells do not)
which has happened now for many years. But "exec cmd N>whatever"
(which looks like the same command when redirections are processed)
which was setting close-on-exec on N, now no longer does, so fd N
can be passed to cmd as an open fd.

For anyone who cares, the big block of change just after "case CMDBUILTIN:"
in evalcommand() in eval.c is the fix for this (one line replaced by
about 90 ... though most of that is comments or #if 0'd example code
for later). It is a bit ugly, and will get worse if our exec command
ever gets any options, as others have, but it does work.

2. when the exec builtin utility is used to alter the shell's redirections
it is now "all or nothing". Previously the redirections were executed
left to right. If one failed, no more were attempted, but the earlier
ones remained. This makes no practical difference to a non-interactive
shell, as a redirection error causes that shell to exit, but it makes
a difference to interactive shells. Now if a redirection fails, any
earlier ones which had been performed are undone. Note however that
side-effects of redirections (like creating, or truncating, files in
the filesystem, cannot be reversed - just the shell's file descriptors
returned to how they were before the error).

Similarly usage errors on exec now exist .. our exec takes no options
(but does handle "--" as POSIX says it must - has done for ages).
Until now, that was the only magic piece of exec, running
exec -a name somecommand
(which several other shells support) would attempt to exec the "-a"
command, and most likely fail, causing immediate exit from the shell.
Now that is a usage error - a non-interactive shell still exits, as
exec is a special builtin, and any error from a special builtin causes
a non-interactive shell to exit. But now, an interactive shell will
no longer exit (and any redirections that were on the command will be
undone, the same as for a redirection error).

3. When a "close on exec" file descriptor is temporarily saved, so the
same fd can be redirected for another command (only built-in commands
and functions matter, redirects for file system commands happen after
a fork() and at that stage if anything goes wrong, the child simply
exits - but for non-forking commands, doing something like printf >file
required the previous stdout to be saved elsewhere, "file" opened to
be the new stdout, then when printf is finished, the old stdout moved
back. Anyway, if the fd being moved had close on exec set, then
when it was moved back, the close on exec was lost. That is now fixed.

4. The fdflags command no longer allows setting close on exec on stdin,
stdout, or stderr - POSIX requires that those 3 fd's always be open
(to something) when any normal command is invoked. With close-on-exec
set on one of these, that is impossible, so simply refuse it (when
"exec N>file" sets close on exec, it only does it for N>2).

Minor changes (should be invisible)

a. The shell now keeps track of the highest fd number it sees doing
normal operations (there are a few internal pipe() calls that aren't
monitored and a couple of others, but in general the shell will now
know the highest fd it ever saw allocated to it). This is mostly
for debugging.

b. calls to fcntl() passing an int as the "arg" are now all properly
cast to the void * that the fcntl kernel is expecting to receive.
I suspect that makes no actual difference to anything, but ...
 1.267 14-Oct-2024  kre Add a -r (version) option to sh

This new -r (or +r) option is for command line use only.
When encountered, the shell simply prints its version info
(such as it has, which isn't much) and exits (immediately).

This allows "funny" uses like
sh -version
the -v and -e options are standard, and processed as normal
(changing nothing, yet, except setting the option). Then the
'r' option is seen, the version info is printed, and the shell
exits. Any remaining "options" (there is no "-o n" option) are
ignored, as are any other args given to the shell.

The string printed (currently) is just "NetBSD shell:" followed
by the value of the NETBSD_SHELL variable (which has been established
already by the time options are processed).
 1.266 11-Oct-2024  kre Add -b and -nMAX options to the read builtin.

As requested on (perhaps more than one) mailing list, this adds
a -n MAX option, to allow the amount of data read by the read
builtin to be limited to MAX bytes (in case the record delimiter
doesn't appear in the input for a long time). There is
currently an upper bound of 8MiB on the value of MAX.

Also add a -b option, which allows for buffered input (with
some usage caveats) rather than 1 byte at a time.

Neither option exists in SMALL shells.

Note that the proposed -z option got deleted ... I couldn't
find a rational way to explain what the final state would be
if a \0 on input generated an error, so rather than have
things ambiguous, better just not to have the option, and
simply keep ignoring input \0's as always.

See the (updated) sh(1) man page for more details.

No pullups planned (new feature, only for new releases).
 1.265 09-Oct-2024  kre PR bin/58687 -- implement suspend as a builtin in sh

Requested by uwe@ in PR bin/58687 without objections from
anyone except me, here is an implementation of a suspend
builtin command for /bin/sh

The sh.1 man page is updated, naturally, to describe it.

This new builtin does not exist in SMALL shells -- as used
on (some) boot media, etc.

If this turns out not to be useful, it can easily be removed.
 1.264 21-Sep-2024  kre In !TINYPROG versions of sh, make the builtin "shift" builtin command
perform a rotate instead or shift if given a numeric arg (which is
otherwise an error).

"set -- a b c; shift -1; echo $*" will echo "c a b".

While here, make the shift builtin comply with POSIX, and accept
(and ignore) a '--' arg before the shift (or rotate) count.

Document the variant of shift in sh(1)

Adapt the shell test for shift to not expect "shift -1" to be an
error, test that rotate works as expected, and include some tests
that use the (useless, but required) "--" arg.
 1.263 10-Aug-2024  uwe sh(1): fix up formatting of syntax examples

.Bd -literal with all lines explicitly formatted (usually with .Ic)
doesn't seem to work too well with mandoc shipped with the heirloom
doctools - the next paragraph after the display remains literal.
Use .Bd -unfilled instead.
 1.262 13-Jul-2024  kre Implement the HISTFILE and HISTAPPEND variables.

See the (newly updated) sh(1) for details.
Also add the -z option to fc (clear history).

None of this exists in SMALL shells.
 1.261 13-Jul-2024  kre Remove caveat about $'' quoting, now POSIX.1-2004 exists.

nb: no Dd bump, that's coming soon (not needed for this change anyway)
 1.260 12-Apr-2024  kre branches: 1.260.2;
Edgar Fuß pointed out that sh(1) did not mention comments (at all).
This has been true forever, and no-one else (including me) ever seems
to have noticed this ommission.

Correct that.

While in the area, improve the general sections on the Lexical structure
of the shell's input, and including some refinements to how quoting is
described.
 1.259 16-Jan-2024  kre Remove an ancient incorrect notion which somehow survived intact for ages.
"$@" is (as it is in double quotes) not subject to field splitting. "$@"
generates (potentially) multiple words, but field splitting has nothing
to do with it.

While here, rename the section from "White Space Splitting (Field Splitting)"
to simply be "Field Splitting" as white space is only relevant if it happens
to occur in IFS (which is the default case, but IFS can be anything, and
isn't required to contain any white space at all).
 1.258 12-Oct-2023  uwe sh(1): touch up markup for the ENV example

Don't use Dq in a literal display, ascii quotes are \*q
While here mark up as literal a few things around this example.
 1.257 01-Sep-2023  kre At the request of bad@ enhance the synopsis of the set built-in
command to include explicit mention of the -o opt and +o opt forms.

Fix the synopsis to have the 4 forms that the description of the
utility discusses, rather than expecting users to understand that
the 3rd and 4th forms of the command were combined into the 3rd
synopsis format. After doing that, the options in the 3rd format
no longer need to be optional, so now all 4 formats are distinct
(previously, the third, omitting everything that was optional, and
the first, could not be distinguished).

While here, some wording and formatting "improvements" as well (nothing
too serious).
 1.256 04-Aug-2023  jschauma tyops:
* redicection -> redirection
* escaoed -> escaped

Noted by J. Lewis Muir on netbsd-docs@
 1.255 20-Dec-2022  kre More markup errors. \+ was intended to be \&+ and .EV .Ev of course.
As best I can tell, the rest of what mandoc -Wall complains about is
incorrect (it could probably be avoided by adding more markup, but
there doesn't seem to be any point).
 1.254 20-Dec-2022  kre Using .Cm Cm makes no sense at all - no idea what I was thinking there
(perhaps just an editing error).
 1.253 20-Dec-2022  uwe sh(1): Fix markup. -compact must be last.
 1.252 11-Dec-2022  kre branches: 1.252.2;

It appears that POSIX intends to add a -d X option to the read command
in its next version, so it can be used as -d '' (to specify a \0 end
character for the record read, rather than the default \n) to accompany
find -print0 and xargs -0 options (also likely to be added).

Add support for -d now. While here fix a bug where escaped nul
chars (\ \0) in non-raw mode were not being dropped, as they are
when not escaped (if not dropped, they're still not used in any
useful way, they just ended the value at that point).
 1.251 30-Oct-2022  kre Note in the description of "jobs -p" that the process id returned is
also the process group identifier (that's a requirement from POSIX, and
is what we have always done - just not been explicit about in sh.1).
Add a note that this value and $! are not necessarily the same (currently,
and perhaps forever, never the same in a pipeline with 2 or more elements).
 1.250 18-Sep-2022  kre Add the -l option (aka -o login): be a login shell. Meaningful only on
the command line (with both - and + forms) - overrides the presence (or
otherwise) of a '-' as argv[0][0].

Since this allows any shell to be a login shell (which simply means that
it runs /etc/profile and ~/.profile at shell startup - there are no other
side effects) add a new, always set at startup, variable NBSH_INVOCATION
which has a char string as its value, where each char has a meaning,
more or less related to how the shell was started. See sh(1).
This is intended to allow those startup scripts to tailor their behaviour
to the nature of this particular login shell (it is possible to detect
whether a shell is a login shell merely because of -l, or whether it would
have been anyway, before the -l option was added - and more). The
var could also be used to set different values for $ENV for different
uses of the shell.
 1.249 16-Sep-2022  kre More wording improvements. There might be more to come.
 1.248 16-Sep-2022  kre Minor wording improvements.

Note these do not alter anything about what the man page specifies,
just say a couple of things in a slightly better way, hence no Dd
update accompanies this change (deliberately).
 1.247 16-Sep-2022  kre Move a comment that used to be in the correct place, once upon a time,
back where it belongs, and make it stand out more, so other text is
less likely to find itself pushed between the comment and the text
to which it appears. This change should make no visible difference
to the man page displayed.
 1.246 16-Sep-2022  kre Whitespace.
 1.245 15-Sep-2022  kre Correct spelling of terminal (it doesn't have a 2nd m).
 1.244 19-Aug-2022  kre Improve the description of the read builtin command.
 1.243 07-Jan-2022  lukem sh(1): improve getopts docs for optstring leading :

getopts has different behaviour if the leading character
of optstring is `:', so describe in more detail:
- no errors are printed (already there)
- unknown options set var to `?' and OPTARG to the unknown option
- missing arguments set var to `:' and OPTARG to the option name

Slight rewording of other paragraphs for more clarity.
 1.242 07-Jan-2022  lukem sh(1): fix formatting warnings
 1.241 21-Nov-2021  kre Improve the however-many-negatives wording even more.
 1.240 20-Nov-2021  rillig sh.1: replace triple negation with single negation, fix typo
 1.239 20-Nov-2021  kre Improve the wording of the "Argument List Processing" section (where
all the sh options, also used with "set", are listed) in response to
a discussion on icb conveyed to me by Darrin B. Jewell.
A few improvements to the description of the "set" built-in as well.

Bump Dd to cover all of this month's changes (so far).
 1.238 16-Nov-2021  rillig sh.1: fix typos
 1.237 16-Nov-2021  kre PR bin/56491

Make "hash" exit(!=0) (ie: exit(1)) if it writes an error message to
stderr as required by POSIX (it was writing "not found" errors, yet
still doing exit(0)).

Whether, when doing "hash foobar", and "foobar" is not found as a command
(not a built-in, not a function, and not found via a PATH search), that
should be considered an error differs between shells. All of the ksh
descendant shells say "no", write no error message in this case, and
exit(0) if no other errors occur. Other shells (essentially all) do
consider it an error, write a message to stderr, and exit(1) when this happens.

POSIX isn't clear, the bug report:
https://austingroupbugs.net/view.php?id=1460
which is not yet resolved, suggests that the outcome will be that
this is to be unspecified. Given the diversity, there might be no
other choice.

Have a foot in both camps - default to the "other shell" behaviour,
but add a -e option (no errors ... applies only to these "not found"
errors) to generate the ksh behaviour. Without other errors (like an
unknown option, etc) "hash -e anyname" will always exit(0).

See the PR for details on how it all works now, or read the updated man page.

While here, when hash is in its other mode (reporting what is in the
table) check for I/O errors on stdout, and exit(1) (with an error
message!) if any occurred. This does not apply to output generated
by the -v option when command names are given (that output is incidental).

In sh.1 document all of this. Also add documentation for a bunch of
other options the hash command has had for years, but which were never
documented. And while there, clean up some other sections I noticed
needed improving (either formatting or content or both).
 1.236 31-Oct-2021  kre PR bin/45390

Be explicit about what happens to PWD after a successful cd command.
Also be very clear that "cd" and "cd -P" are the same thing, and
the only cd variant implemented.

Also, when it is appropriate to print the new directory after a cd
command, note that it happens if interactive (as it always has here)
and also if the posix option is set (for POSIX compat, where "interactive"
is irrelevant). Mention that "cd -" is a case where the new directory
is printed (along with paths relative to a non-empty CDPATH entry,
and where the "cd old new" (string replacement in curdir) is used.

While here document the new -e option to cd.

XXX pullup -9
 1.235 26-Oct-2021  kre PR bin/56464

After almost 30 years, finally do the right thing and read $HOME/.profile
rather than .profile in the initial directory (it was that way in version
1.1 ...) All other ash descendants seem to have fixed this long ago.

While here, copy a feature from FreeBSD which allows "set +p" (if a
shell run by a setuid process with the -p flag is privileged) to reset
the privileges. Once done (the set +p) it cannot be undone (a later
set -p sets the 'p' flag, but that's all it does) - that just becomes a
one bit storage location.

We do this, as (also copying from FreeBSD, and because it is the right
thing to do) we don't run .profile in a privileged shell - FreeBSD run
/etc/suid_profile in that case (not a good name, it also applies to setgid
shells) but I see no real need for that, we run /etc/profile in any case,
anything that would go in /etc/suid_profile can just go in /etc/profile
instead (with suitable guards so the commands only run in priv'd shells).

One or two minor DEBUG mode changes (notably having priv'd shells identify
themselves in the DEBUG trace) and sh.1 changes with doc of the "set +p"
change, the effect that has on $PSc and a few other wording tweaks.

XXX pullup -9 (not -8, this isn't worth it for the short lifetime
that has left - if it took 28+ years for anyone to notice this, it
cannot be having all that much effect).
 1.234 15-Sep-2021  kre Have the ulimit command watch for ulimit -n (alter number of available fds)
and keep the rest of the shell aware of any changes.

While here, modify 'ulimit -aSH' to print both the soft and hard limits
for the resources, rather than just (in this case, as H comes last) the
hard limit. In any other case when both S and H are present, and we're
examining a limit, use the soft limit (just as if neither were given).

No change for setting limits (both are set, unless exactly one of -H
or -S is given). However, we now check for overflow when converting
the value to be assigned, rather than just truncating the value however
it happens to work out...
 1.233 12-Sep-2021  wiz Mark up NULL with Dv.
 1.232 12-Sep-2021  kre Improve the formatting of the list of Built-in commands for those
commands with multiple synopsis lines (eg: trap).

But there really must be a better way to achieve this effect than
the way it is accomplished here, and I'm hoping some wizard who
understands mdoc much better than I do will revert this change and
do it using some inspired magic incantation instead.
 1.231 12-Sep-2021  kre Don't dereference NULL on "jobs -Z" (with no title given), instead
do setproctitle(NULL) (which is not the same thing at all). Do the
same with jobs -Z '' as setting the title to "sh: " isn't useful.

Improve the way this is documented, and note that it is only done
this way because zsh did it first (ie: pass on the balme, doing this
in the jobs command is simply absurd.)
 1.230 11-Sep-2021  christos Add jobs -Z (like in zsh(1)) to setproctitle(3).
 1.229 18-Sep-2020  wiz Remove superfluous Ed.
 1.228 18-Sep-2020  kre Correct an incorrectly quoted (unquoted, but should be) example used in
the "local" built-in command description (pointed out by mrg@ via uwe@ in
private e-mail).

Add a description to the export command of why this quoting is required,
and then refer to it from local and readonly (explained in export as that
one comes first).

Note that some shells parse export/local/readonly (and often more) as
"declarative" commands, and this quoting isn't needed (provided the
command name is literal and not the result of an expansion) making
X=$Y type args not require quoting, as they often don't in a regular
variable assignment (preceding, or not part of, another command).
POSIX is going to allow, but not require, that behaviour. We do not
implement it.
 1.227 25-Aug-2020  kre Idiot typo, generated by an idiot, fixed by the same one.
 1.226 21-Aug-2020  wiz Remove unmatched .El and mark up signal name with Dv.
 1.225 20-Aug-2020  kre Man page enhancements.

Better describe the command search procedure.
Document "trap -P"
Describe what works as a function name.
More accurate description of reserved word recognition.
Be more accurate about when field splittng happens after
expansions (and in particular note that tilde expansions are
not subject to field splitting). Be clear that "$@" is
not field split, it simply produces multiple fields as part
of its expansion (hence IFS is irrelevant to this), but if
used as $@ (unquoted) each field produced is potentially subject
to field splitting. Other minor wording changes.
 1.224 20-Feb-2020  pgoyette Typo: s/./,/
 1.223 22-Apr-2019  kre branches: 1.223.2;

Bump date for previous.
 1.222 22-Apr-2019  kre 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.
 1.221 15-Apr-2019  uwe -compact must come last
 1.220 14-Feb-2019  kre 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.
 1.219 04-Feb-2019  wiz Remove leading zero from date.
 1.218 04-Feb-2019  kre 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).
 1.217 21-Jan-2019  kre 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).
 1.216 12-Dec-2018  kre 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...
 1.215 12-Dec-2018  kre 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...
 1.214 12-Dec-2018  kre Fix Oo Op Oc syntax error (which seemed to work OK to me....)
Pointed out by wiz@
 1.213 12-Dec-2018  kre 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).
 1.212 11-Dec-2018  kre 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).
 1.211 04-Dec-2018  kre 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
 1.210 03-Dec-2018  kre 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).
 1.209 23-Nov-2018  kre 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.
 1.208 04-Sep-2018  kre 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.
 1.207 25-Aug-2018  kre 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).
 1.206 03-May-2018  wiz branches: 1.206.2;
Remove Pps without effect.
 1.205 03-May-2018  kre Simplify convoluted language, and remove incorrect statement
(that I added a while ago) about what is required by POSIX.
 1.204 02-May-2018  pgoyette Minor grammatical correction (don't end a sentence/phrase with a
preposition).
 1.203 17-Mar-2018  uwe 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.
 1.202 17-Mar-2018  uwe Tweak "ulimit" synopsis.
 1.201 17-Mar-2018  uwe Cleanup markup in the "Command Line Editing" section.
 1.200 17-Mar-2018  uwe Cleanup markup in the "Job Control" section.
 1.199 17-Mar-2018  uwe Use .Dv, not .Ev, to refer to LINENO, it's not an environment variable.
 1.198 16-Mar-2018  uwe Default values of PS1 and friends have only single space. Use .Li to
typeset them to make that space more visible in PostScript output.
 1.197 16-Mar-2018  uwe Use .Bd -literal for code example.
 1.196 16-Mar-2018  kre Markup fixes (partly from uwe@) and change some tabs to spaces, they
seem to work better...
 1.195 16-Mar-2018  kre Restore some (*roff) comments deleted in previous (partially unshave
the yak) for which the purpose was misunderstood. But trim one more hair.
 1.194 16-Mar-2018  kre Give the yak a quick trim and shave, and make one or two minor
wording changes (which are, hopefully, improvements).
 1.193 15-Mar-2018  uwe 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.
 1.192 14-Mar-2018  uwe 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.
 1.191 14-Mar-2018  uwe Small markup tweaks in Argument List Processing
 1.190 14-Mar-2018  uwe Instead of .Oo/.Oc use .Op directly where possible.
 1.189 14-Mar-2018  uwe Revert previous. Fix the real problem properly.
 1.188 14-Mar-2018  wiz Remove Ic macro without effect.
 1.187 13-Mar-2018  uwe Try to improve markup in the Built-ins section.
Mostly sprinkle missing .Ic and .Ar
 1.186 13-Mar-2018  uwe Try to improve markup in the Parameter Expansion section.
 1.185 13-Mar-2018  uwe Try to improve markup of the redirections definitions.
 1.184 13-Mar-2018  uwe Fix horrendous markup abuse in the here-document example.
Consistently spell "here-document" in full.
 1.183 13-Mar-2018  uwe Spell "here-document" with a hyphen, don't mark it up as a command.
 1.182 13-Mar-2018  uwe Mark up "in" (of the "for" command) appropriately.
 1.181 13-Mar-2018  uwe Use \(or not \*(Ba when discussing case patterns.
 1.180 13-Mar-2018  uwe Use \(em for em-dash
 1.179 13-Mar-2018  uwe Standalone | means \[ba] while we want \[or] so add \& protection to
the few places where it was missing.
 1.178 13-Mar-2018  uwe .Dl is a a single line .Bd -literal -offset indent so don't abuse
multiple consecutive .Dl and use proper .Bd instead.
 1.177 13-Mar-2018  uwe .Bd expects the display type to come first, so move -compact to the end.
 1.176 13-Mar-2018  uwe Add missing word.
 1.175 15-Jan-2018  kre branches: 1.175.2;

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.
 1.174 19-Nov-2017  kre 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.
 1.173 15-Nov-2017  kre Correct a typo: s/ at / an /
 1.172 30-Oct-2017  wiz Minor spellchecking changes.
 1.171 29-Oct-2017  kre Correct a markup typo (Sv -> Dv)
 1.170 28-Oct-2017  kre 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...)
 1.169 25-Oct-2017  kre 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.
 1.168 15-Oct-2017  pgoyette Fix typo: s/one or mode/one or more/
 1.167 06-Oct-2017  kre 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).
 1.166 27-Aug-2017  wiz Whitespace fixes. Fix a typo. Refer to emacs using Ic, since emacs(1)
does not exist in the base system.
 1.165 27-Aug-2017  wiz Remove unnecessary Tn macro.
 1.164 21-Aug-2017  kre 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 ...
 1.163 25-Jul-2017  wiz Remove trailing whitespace.
 1.162 24-Jul-2017  kre 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.
 1.161 24-Jul-2017  kre Add support for ++ and -- (pre & post) and ',' to arithmetic.
 1.160 24-Jul-2017  kre 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.
 1.159 01-Jul-2017  wiz Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
 1.158 30-Jun-2017  kre Correct a markup typo (why did I not see this before the prev commit??)
 1.157 30-Jun-2017  kre Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
 1.156 28-Jun-2017  kre Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
 1.155 27-Jun-2017  kre 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.
 1.154 27-Jun-2017  wiz Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
 1.153 27-Jun-2017  kre Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
 1.152 17-Jun-2017  kre Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
 1.151 08-Jun-2017  kre Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
 1.150 07-Jun-2017  wiz New sentence, new line. Whitespace.
 1.149 07-Jun-2017  kre A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)

This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)

The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.

This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).

I would not call the results of this perfect yet, but it is close.
 1.148 06-Jun-2017  kre Fix a typo (or rather a remnant of an earlier intent).
 1.147 04-Jun-2017  kre Make cd (really) do cd -P, and not just claim that is what it is doing
while doing a half-hearted, broken, partial, version of cd -L instead.
The latter (as the manual says) is not supported, what's more, it is an
abomination, and should never be supported (anywhere.)

Fix the doc so that the pretense that we notice when a path given crosses
a symlink (and turns on printing of the destination directory) is claimed
no more (that used to be true until late Dec 2016, but was changed). Now
the print happens if -o cdprint is set, or if an entry from CDPATH that is
not "" or "." is used (or if the "cd dest repl" cd cmd variant is used.)

Fix CDPATH processing: avoid the magic '%' processing that is used for
PATH and MAILPATH from corrupting CDPATH. The % magic (both variants)
remains undocumented.

Also, don't double the '/' if an entry in PATH or CDPATH ends in '/'
(as in CDPATH=":/usr/src/"). A "cd usr.bin" used to do
chdir("/usr/src//usr.bin"). No more. This is almost invisible,
and relatively harmless, either way....

Also fix a bug where if a plausible destination directory in CDPATH
was located, but the chdir() failed (eg: permission denied) and then
a later "." or "" CDPATH entry succeeded, "print" mode was turned on.
That is:
cd /tmp; mkdir bin
mkdir -p P/bin; chmod 0 P/bin
CDPATH=/tmp/P:
cd bin
would cd to /tmp/bin (correctly) but print it (incorrectly).

Also when in "cd dest replace" mode, if the result of the replacement
generates '-' as the path named, as in:
cd $PWD -
then simply change to '-' (or attempt to, with CDPATH search), rather
than having this being equivalent to "cd -")

Because of these changes, the pwd command (and $PWD) essentially
always acts as pwd -P, even when called as pwd -L (which is still
the default.) That is, even more than it did before.

Also fixed a (kind of minor) mem management error (CDPATH related)
"whosoever shall padvance must stunalloc before repeating" (and the
same for MAILPATH).
 1.146 02-Jun-2017  abhinav branches: 1.146.2;
Fix typo
 1.145 27-May-2017  kre More standard (and saner) implementation of the ! reserved word.
Unless the shell is compiled with the (compilation time) option
BOGUS_NOT_COMMAND (as in CFLAGS+=-DBOGUS_NOT_COMMAND) which it
will not normally be, the ! command (reserved word) will only
be permitted at the start of a pipeline (which includes the
degenerate pipeline with no '|'s in it of course - ie: a simple cmd)
and not in the middle of a pipeline sequence (no "cmd | ! cmd" nonsense.)
If the latter is really required, then "cmd | { ! cmd; }" works as
a standard equivalent.

In POSIX mode, permit only one ! ("! pipeline" is ok. "! ! pipeline" is not).
Again, if needed (and POSIX conformance is wanted) "! { ! pipeline; }"
works as an alternative - and is safer, some shells treat "! ! cmd" as
being identical to "cmd" (this one did until recently.)
 1.144 27-May-2017  kre It turns out that most shells do not do variable value/attribute
inheritance when a variable is declared local, but instead leave
the local var unset (if not given a value) in the function.
Only ash derived shells do inheritance it seems.

So, to compensate for that, and get one step closer to making
"local" part of POSIX, so we can really rely upon it, a compromise
has been suggested, where "local x" is implementation defined
when it comes to this issue, and we add "local -I x" to specify
inheritance, and "local -N x" to specify "not" (something...)
(not inherited, or not set, or whatever you prefer to imagine!)
The option names took a lot of hunting to find something reasonable
that no shell (we know of) had already used for some other purpose...
The I was easy, but 'u' 'U' 'X' ... all in use somewhere.

This implements that (well, semi-) agreement.

While here, add "local -x" (which many other shells already have)
which causes the local variable to be made exported. Not a lot
of gain in that (since "export x" can always be done immediately
after "local x") but it is very cheap to add and allows more other
scripts to work with out shell.

Note that while 'local x="${x}"' always works to specify inheritance
(while making the shell work harder), "local x; unset x" does not
always work to specify the alternative, as some shells have
"re-interpreted" unset of a local variable to mean something that
would best be described as "unlocal" instead - ie: after the unset
you might be back with the variable from the outer scope, rather
than with an unset local variable.

Also add "unset -x" to allow unsetting a variable without removing
any exported status it has.

There are gazillions of other options that are not supported here!
 1.143 18-May-2017  kre Allow abbreviations of option names for the "fdflags -s" command.
While documenting that, cleanup markup of the fdflags section of the
man page.
 1.142 18-May-2017  kre Command line, and "set" command options processing cleanup.

sh +c "command string" no longer works (it must be -c)
sh +o and sh -o no longer work (if you could call what they did
before working.) nb: this is without an option name.
-ooo Opt1 Opt2 Opt3 no longer works (set & cmd line), this should be
-o Opt1 -o Opt2 -o Opt3 (same with +ooo of course).
-oOpt is now supported - option value (name of option in
this case) immediately following -o (or +o).
(as with other commands that use std opt parsing)
Both set comamnd and command line.

In addition, the output from "set +o" has shrunk dramatically, by borrowing
a trick from ksh93 (but implemented in a more traditional syntax).
"set +o" is required to produce a command (or commands) which when executed
later, will return all options to the state they were in when "set +o"
was done. Previously that was done by generating a set command, with
every option listed (set -o opt +o other-opt ...) to set them all back
to their current setings. Now we have a new "magic option" ("default")
which sets all options to their default values, so now set +o output
need only be "set -o default -o changed-opt ..." (only the options that
have been changed from their default values need be explicitly mentioned.)
The definition of "default value" for this is the value the shell set the
option to, after startup, after processing the command line (with any
flags, or -o option type settings), but before beginning processing any
user input (incuding startup files, like $ENV etc).

Anyone can execute "set -o default" of course, but only from a "set"
command (it makes no sense at all as a -o option to sh). This also
causes "set +o" to be slightly more useful as a general command, as
ignoring the "set -o default" part of the result, it lists just those
options that have been altered after sh startup. There is no +o default.
There isn't an option called "default" at all...

This causes some of the commented out text from sh.1 to become uncommented.
 1.141 14-May-2017  kre When opening a file descritor with "exec n>/file" (and similar) only
set the close-on-exec bit when not in posix mode (to comply with posix...)
OK: christos@
 1.140 14-May-2017  kre Add mention of ;& in the list of control operators (forgotten before).
Document the (slightly) enhanced NETBSD_SHELL.
Fix a typo (one of my typos...)
Move a commented out section to align with current planned changes
(and fix its commented out markup.)
 1.139 14-May-2017  wiz Use more, or more appropriate, markup.
 1.138 12-May-2017  kre Improve the description of option processing (including the shell's
arg list processing), and the set command in general.
Also add some (new) commented out text related to options which may
be commented back in sometime soon...
 1.137 12-May-2017  kre Corrected some typos, added some (hopefully improving) extra text,
sorted stuff that needed sorting, and added some (probably incorrect)
markup...
 1.136 07-May-2017  kre Enhance the trap command to make it possible to do what POSIX wants
(even if no shell in existence, that I am aware of, does that).

That is, POSIX says ... [of the trap command with no args]

The shell shall format the output, including the proper use of
quoting, so that it is suitable for re-input to the shell as commands
that achieve the same trapping results. For example:

save_traps=$(trap)

...

eval "$save_traps"

It is obvious what the intent is there. But no shell makes it work.

An example using bash (as the NetBSD shell, still does not do the save_traps=
stuff correctly - but that is a problem for a different time and place...)

Given this script

printf 'At start: '; trap
printf '\n'

traps=$(trap)
trap 'echo hello' INT
printf 'inside : '; trap
printf '\n'
eval "${traps}"

printf 'At end : '; trap
printf '\n'

One would expect that (assuming no traps are set at the start, and
there aren't) that the first trap will print nothing, then the inside
trap will show the trap that was set, and then when we get to the
end everything will be back to nothing again.

But:

At start:
inside : trap -- 'echo hello' SIGINT

At end : trap -- 'echo hello' SIGINT

And of course. when you think about it, it is obvious why this happens.
The first "trap" command prints nothing ... nothing has changed when we
get to the "traps=$(trap)" command ... that trap command also prints
nothing. So this does traps=''. When we do eval "${traps}" we are
doing eval "", and it is hardly surprising that this accomplishes nothing!

Now we cannot rationally change the "trap" command without args to
behave in a way that would make it useful for the posix purpose (and
here, what they're aiming for is good, it should be possible to
accomplish that objective) so is there some other way?

I think I have seen some shell (but I do not remember which one) that
actually has "trap -" that resets all traps to the default, so with that,
if we changed the 'eval "${traps}"' line to 'trap -; eval "${traps}"'
then things would actually work - kind of - that version has race conditions,
so is not really safe to use (it will work, most of the time...)

But, both ksh93 and bash have a -p arg to "trap" that allows information
about the current trap status of named signals to be reported. Unfortunately
they don't do quite the same thing, but that's not important right now,
either would be usable, and they are, but it is a lot of effort, not
nearly as simple as the posix example.

First, while "trap -p" (with no signals specified) works, it works just
the same (in both bash and ksh93, aside from output format) as "trap".
That is, that is useless. But we can to

trap_int=$(trap -p int)
trap_hup=$(trap -p hup)
...

and then reset them all, one by one, later...

(bash syntax)
test -n "${trap_int}" && eval "${trap_int}" || trap - int
test -n "${trap_hup}" && eval "${trap_hup}" || trap - hup
(ksh93 syntax)
trap "${trap_int:-}" int
trap "${trap_hup:-}" hup

the test (for bash) and variable with default for ksh93, is needed
because they both still print nothing if the signal action is the default.

So, this modification attempts to fix all of that...

1) we add trap -p, but make it always output something for every signal
listed (all of the signals if none are given) even if the signal
action is the default.

2) choose the bash output format for trap -p, over the ksh93 format,
even though the simpler usage just above makes the ksh93 form seem
better. But it isn't. Consider:

ksh93$ trap -p int hup
echo hello

One of the two traps has "echo hello" as its action, the other is
still at the default, but which?

From bash...
bash$ trap -p int hup
trap -- 'echo hello' SIGINT

And now we know! Given the bash 'trap -p' format, the following function
produces ksh93 format output (for use with named signals only) instead...

ksh93_trap_p() {
for _ARG_ do
_TRAP_=$(trap -p "${_ARG_}") || return 1
eval set -- "${_TRAP_}"
printf '%s' "$3${3:+
}"
done
return 0
}

[ It needs to be entered without the indentation, that '}"' line has to be
at the margin. If the shell running that has local vars (bash does) then
_ARG_ and _TRAP_ should be made local. ]

So the bash format was chosen (except we do not include the "SIG" on the
signal names. That's irrelevant.)

If no traps are set, "trap -p" will say (on NetBSD of course)...

trap -- - EXIT HUP INT QUIT ILL TRAP ABRT EMT FPE KILL BUS SEGV SYS
trap -- - PIPE ALRM TERM URG STOP TSTP CONT CHLD TTIN TTOU IO XCPU XFSZ
trap -- - VTALRM PROF WINCH INFO USR1 USR2 PWR RT0 RT1 RT2 RT3 RT4 RT5
trap -- - RT6 RT7 RT8 RT9 RT10 RT11 RT12 RT13 RT14 RT15 RT16 RT17 RT18
trap -- - RT19 RT20 RT21 RT22 RT23 RT24 RT25 RT26 RT27 RT28 RT29 RT30

Obviously if traps are set, the relevant signal names will be removed from
that list, and additional lines added for the trapped signals.

With args, the signals names are listed, one line each, whatever
the status of the trap for that signal is:

$ trap -p HUP INT QUIT
trap -- - HUP
trap -- 'echo interrupted' INT
trap -- - QUIT

3) we add "trap -" to reset all traps to default. (It is easy, and seems
useful.)

4) While here, lots of generic cleanup. In particular, get rid of the
NSIG+1 nonsense, and anything that ever believes a signo == NSIG
is in any way rational. Before there was a bunch of confusion,
as we need all the signals for traps, plus one more for the EXIT
trap, which looks like we then need NSIG+1. But EXIT is 0, NSIG
includes signals from 0..NSIG-1 but there is no signal 0, EXIT
uses that slot, so we do not need to add and extra one, NSIG is
enough. (To see the effect of this, use a /bin/sh from before
this fix, and compare the output from

trap '' 64
and trap '' 65

both invalid signal numbers.

Then try just "trap" and watch your shell drop core...)

Eventually NSIG needs to go away completely (from user apps), it
is not POSIX, it isn't really useful (unless we make lots of
assumptions about how signals are numbered, which are not guaranteed,
so even if apps, like this sh, work on NetBSD, they're not portable,)
and it isn't necessary (or will not be, soon.)

But that is for another day...

5) As is kind of obvious above, when listing "all" traps, list all the
ones still at their defaults, and all the ignored signals, on as
few lines as possible (it could all be on one line - technically it
would work as well, but it would have made this cvs log message
really ugly...) Signals with a non-null action still get listed
one to a line (even if several do have the exact same action.)

6) Man page updates as well.

After this change, the following script:

printf 'At start: '; trap
printf '\n'

trap -p >/tmp/out.$$
trap 'echo hello' INT
printf 'inside : '; trap
printf '\n'
. /tmp/out.$$; rm /tmp/out.$$

printf 'At end : '; trap
printf '\n'

which is just the example from above,
using "trap -p" instead of just "trap" to save the traps,
and modified to a form that will work with the NetBSD shell today
produces:

At start:
inside : trap -- 'echo hello' INT

At end :

[Do I get a prize for longest commit log message of the year?]
 1.135 04-May-2017  kre Implement the ';&' (used instead of ';;') case statement list terminator
which causes fall through the to command list of the following pattern
(wuthout evaluating that pattern). This has been approved for inclusion
in the next major version of the POSIX standard (Issue 8), and is
implemented by most other shells.

Now all form a circle and together attempt to summon the great wizd
in the hopes that his magic spells can transform the poor attempt
at documenting this feature into something rational...
 1.134 03-May-2017  kre Undocument (comment out) the description of the unimplemented MAILCHECK
variable, and while here, enhance the description of MAIL a little.
 1.133 30-Apr-2017  wiz Uppercase UID. Fix typo.
 1.132 29-Apr-2017  kre Correct description of the trap command (make it posix compatible)
and add a couple more examples. Also terminate a few sentences...
 1.131 14-Apr-2017  abhinav branches: 1.131.2;
Fix mandoc -Tlint warnings:
s/PP/Pp
Remove Pp before It
 1.130 14-Apr-2017  abhinav Instead of removing markup as I did in the last commit, use markup but properly.
Hint taken from FreeBSD man page.

ok wiz@
 1.129 03-Apr-2017  abhinav Use \- instead of .Fl for the -number argument.

.Fl causes the -number argument to be rendered in bold, which causes confusion
with the [+]number argument right above it.

ok wiz@
 1.128 23-Mar-2017  kre PR bin/14578

Add a reference to editline(7) so we document the "-o vi" and "-o emacs"
bindings (defaults, and what can be set.)
 1.127 20-Mar-2017  kre At the suggestion of Matthew Sporleder (on current-users@) reword some
of the description of arithmetic expressions to make it a bit more
human friendly.

While here fix a few other minor errors, and bump date.
 1.126 20-Mar-2017  kre Finish support for all required $(( )) (shell arithmetic) operators,
closing PR bin/50958

That meant adding the assignment operators ('=', and all of the +=, *= ...)
Currently, ++, --, and ',' are not implemented (none of those are required
by posix) but support for them (most likely ',' first) might be added later.

To do this, I removed the yacc/lex arithmetic parser completely, and
replaced it with a hand written recursive descent parser, that I obtained
from FreeBSD, who earlier had obtained it from dash (Herbert Xu).

While doing the import, I cleaned up the sources (changed some file names
to avoid requiring a clean build, or signifigant surgery to the obj
directories if "build.sh -u" was to be used - "build.sh -u" should work
fine as it is now) removed some dashisms, applied some KNF, ...
 1.125 04-Feb-2017  wiz Remove trailing space.
 1.124 02-Feb-2017  christos Add fdflags builtin. Discussed with Chet and he has implemented it for
bash too.
 1.123 12-May-2016  kre branches: 1.123.2; 1.123.4;

Document that a N>&N (or N<&N) redirection turns off close-on-exec for N
(where N is a decimal fd number) either when used as
some-command N>&N
(where fd N is passed, open, to some-command - which is obviously what is
wanted)

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

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

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

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

OK christos@
 1.121 04-Apr-2016  wiz Remove some double quotes.

Parity is kept.
 1.120 31-Mar-2016  christos Document the NETBSD_SHELL variable, the enhancements to export,
the posix option, and a whole bunch of miscellaneous updates and
corrections. (from kre@)
 1.119 24-Feb-2016  wiz file system police.
 1.118 24-Feb-2016  christos Make sh.1 catch up with reality. Document -h (such as it is...)
and also added doc for some other stuff that was missing.

Take the opportunity to clean up the way the flags are set in the
man page, so every new flag doesn't have to be added 6 times!
(Some of the lists were different from others, in ordering, and
content, for no good reason at all.)

Make a few other cleanups ... Add text about AND-OR lists,
This can be also used to justify closing an open PR:
(that "sh -c 'command &&'" is not a syntax error...).

Add doc for -F, which should default to set if the shell somehow
gets compiled without DO_SHAREDVFORK defined, (to be committed
separately)

XXX: Consider disabling DO_SHAREDVFORK if SMALL is defined?

From kre
 1.117 06-Jan-2016  wiz Whitespace.
 1.116 05-Jan-2016  christos Document close-on-exec redirection behavior.
 1.115 26-May-2015  christos Drop privileges when executed set{u,g}id unless -p is specified like other
shells do to avoid system() and popen() abuse.
 1.114 01-Jun-2014  christos PR/48843: Jarmo Jaakkola: Soften the language in the manual page,
making less promises about behavior not explicitly stated in the standard.
 1.113 31-May-2014  christos PR/48843: Jarmo Jaakkola: dot commands mess up scope nesting tracking

Evaluation of commands goes completely haywire if a file containing
a break/continue/return command outside its "intended" scope is sourced
using a dot command inside its "intended" scope. The main symptom is
not exiting from the sourced file when supposed to, leading to evaluation
of commands that were not supposed to be evaluated. A secondary symptom
is that these extra commands are not evaluated correctly, as some of them
are skipped. Some examples are listed in the How-To-Repeat section.

According to the POSIX standard, this is how it should work:
dot:
The shell shall execute commands from the file in the current
environment.
break:
The break utility shall exit from the smallest enclosing for, while,
or until loop, [...]
continue:
The continue utility shall return to the top of the smallest
enclosing for, while, or until loop, [...]
return:
The return utility shall cause the shell to stop executing
the current function or dot script. If the shell is not currently
executing a function or dot script, the results are unspecified.

It is clear that return should return from a sourced file, which
it does not do. Whether break and continue should work from the sourced
file might be debatable. Because the dot command says "in the current
environment", I'd say yes. In any case, it should not fail in weird
ways like it does now!

The problems occur with return (a) and break/continue (b) because:
1) dotcmd() does not record the function nesting level prior to
sourcing the file nor does it touch the loopnest variable,
leading to either
2 a) returncmd() being unable to detect that it should not set
evalskip to SKIPFUNC but SKIPFILE, or
b) breakcmd() setting evalskip to SKIPCONT or SKIPBREAK,
leading to
3) cmdloop() not detecting that it should skip the rest of
the file, due to only checking for SKIPFILE.
The result is that cmdloop() keeps executing lines from the file
whilst evalskip is set, which is the main symptom. Because
evalskip is checked in multiple places in eval.c, the secondary
symptom appears.
>How-To-Repeat:
Run the following script:

printf "break\necho break1; echo break2" >break
printf "continue\necho continue1; echo continue2" >continue
printf "return\necho return1; echo return2" >return

while true; do . ./break; done

for i in 1 2; do . ./continue; done

func() {
. ./return
}
func

No output should be produced, but instead this is the result:
break1
continue1
continue1
return1

The main symptom is evident from the unexpected output and the secondary
one from the fact that there are no lines with '2' in them.
>Fix:
Here is patch to src/bin/sh to fix the above problems. It keeps
track of the function nesting level at the beginning of a dot command
to enable the return command to work properly.

I also changed the undefined-by-standard functionality of the return
command when it's not in a dot command or function from (indirectly)
exiting the shell to being silently ignored. This was done because
the previous way has at least one bug: the shell exits without asking
for confirmation when there are stopped jobs.

Because I read the standard to mean that break and continue should have
an effect outside the sourced file, that's how I implemented it. For what
it's worth, this also seems to be what bash does. Also laziness, because
this way required no changes to loopnesting tracking. If this is not
wanted, it might make sense to move the nesting tracking to the inputfile
stack.

The patch also does some clean-up to reduce the amount of global
variables by moving the dotcmd() and the find_dot_file() functions from
main.c to eval.c and making in_function() a proper function.
 1.112 20-Jan-2014  roy branches: 1.112.2;
Add wctype(3) support to Shell Patterns.
Obtained from FreeBSD.
 1.111 02-Oct-2013  christos document LINENO
XXX: someone should fix all the .Ev stuff because some of them are just
shell variables .Va and are not really exported to the environment. See
the FreeBSD man page.
 1.110 09-May-2013  simonb Document that a here-document can finish at an EOF as well as at the
delimiter.
 1.109 03-Oct-2012  wiz - Correct macro usage;
- improve wording, including creating more consistency therein.

From Bug Hunting.
 1.108 26-Aug-2012  wiz branches: 1.108.2;
- improve punctuation;
- improve (create more consistency in) spelling;
- remove unnecessary (and in part ignored) macros, as well as an
unnecessary argument to `.Bl' (fixes mandoc(1) warnings);
- improve wording;
- bump date.

Patch from Bug Hunting.
 1.107 11-Jun-2012  njoly Allow thread limit queries by adding the new -r flag to ulimit. Add
the corresponding documentation in the man page.
 1.106 05-Oct-2011  christos branches: 1.106.2;
Merge duplicate information.
 1.105 04-Oct-2011  apb .Dq Dv \&:
 1.104 04-Oct-2011  christos Mention what happens when we don't include :. It would be nice to use
.Dv :
but it produces ``'':
 1.103 11-Sep-2011  dholland A feature that wasn't implemented for 4.4alpha and still isn't implemented
is just plain not implemented.
 1.102 13-Jun-2011  wiz Sort sections. Remove trailing whitespace.
 1.101 13-Jun-2011  uebayasi Typos.
 1.100 11-Jun-2011  christos document OLDPWD and cd -
 1.99 03-Jun-2010  dholland branches: 1.99.4;
Note that set -o tabcomplete requires either set -o emacs or set -o vi
to work.
 1.98 01-Mar-2010  joerg \\ -> \e
 1.97 01-Jan-2010  wiz Bump date for cd -P support.
 1.96 01-Jan-2010  dholland fix another typo
 1.95 01-Jan-2010  dholland Make the cd builtin accept and ignore -P, which is a kshism that has been
allowed to leak into POSIX and selects the behavior cd already implements.
Closes PR bin/42557 and also relevant to PR pkg/42168.

I suppose this should probably be pulled up to both -4 and -5...
 1.94 01-Jan-2010  dholland fix typo
 1.93 29-Mar-2009  wiz Bump date for previous.
 1.92 29-Mar-2009  mrg - add new RLIMIT_AS (aka RLIMIT_VMEM) resource that limits the total
address space available to processes. this limit exists in most other
modern unix variants, and like most of them, our defaults are unlimited.
remove the old mmap / rlimit.datasize hack.

- adds the VMCMD_STACK flag to all the stack-creation vmcmd callers.
it is currently unused, but was added a few years ago.

- add a pair of new process size values to kinfo_proc2{}. one is the
total size of the process memory map, and the other is the total size
adjusted for unused stack space (since most processes have a lot of
this...)

- patch sh, and csh to notice RLIMIT_AS. (in some cases, the alias
RLIMIT_VMEM was already present and used if availble.)

- patch ps, top and systat to notice the new k_vm_vsize member of
kinfo_proc2{}.

- update irix, svr4, svr4_32, linux and osf1 emulations to support
this information. (freebsd could be done, but that it's best left
as part of the full-update of compat/freebsd.)


this addresses PR 7897. it also gives correct memory usage values,
which have never been entirely correct (since mmap), and have been
very incorrect since jemalloc() was enabled.

tested on i386 and sparc64, build tested on several other platforms.

thanks to many folks for feedback and testing but most espcially
chuq and yamt for critical suggestions that lead to this patch not
having a special ugliness i wasn't happy with anyway :-)
 1.91 10-Mar-2009  joerg Explicitly escape -- as it is not an argment to the Cm macro.
 1.90 10-Mar-2009  joerg Don't use .Xo/.Xc to workaround ancient macro argument limit.
 1.89 09-Mar-2009  joerg Fix preamble to match order set out by mdoc(7). Discussed with wiz.
 1.88 11-Dec-2008  yamt branches: 1.88.2;
document "EXIT" pseudo signal.
 1.87 24-Jun-2007  christos branches: 1.87.18;
PR/36533: Greg A. Woods: minor doc fixes for sh(1)
 1.86 25-Mar-2007  apb Document that shell arithmetic now uses intmax_t. Document that
variables in shell arithmetic don't need "$" signs.
 1.85 04-Sep-2006  dsl branches: 1.85.2;
Fix typo, update date.
Fixes PR/34472
 1.84 10-Sep-2005  wiz In mdoc, use .Pp for new paragraphs, not .br.
 1.83 20-Aug-2005  dsl Don't apply CDPATH if the the first component of the target is "." or "..".
Fixes PR/30973 and applies the principle of least surprise.
Update documentation to match (including date).
(matches behaviour of pdksh - if not it's documentation)
 1.82 15-Jul-2005  wiz Aspell, fix an Xref, drop trailing whitespace.
 1.81 15-Jul-2005  christos Allow trap to work on ignored signals when the shell is interactive.
 1.80 24-May-2005  wiz Whitespace and punctuation fixes.
 1.79 07-May-2005  dsl If 'set -o tabcomplete' it set, then bind <tab> to the libedit filename
completion function.
Note that the libedit code will probably want fine-tuning!
While editing the man page, add a note that non-whitespace IFS chars are
terminators and can generate null arguments.
 1.78 03-Jun-2004  hubertf Fix typo: and the -> and then
 1.77 23-Apr-2004  wiz Fix typo noted by Patrick Welche.
 1.76 17-Apr-2004  wiz Bump date for previous.
 1.75 17-Apr-2004  christos understand rlimit sbsize
 1.74 21-Dec-2003  wiz Fix example added in previous.
 1.73 21-Dec-2003  jdolecek add a note to Short-Circuit operators section about the somewhat
nonintuitive evaluation in case there is both || and && specified
pointed out in bin/23814 by VaX#n8
 1.72 18-Dec-2003  heas correct 2 typos
 1.71 14-Nov-2003  dsl Posix requires that 'pwd -P' reset the shells saved cwd value - so a
subsequent 'pwd -L' will report the same value.
Update man page to be a closer match to reality.
 1.70 27-Oct-2003  wiz passwd(5), not passwd(4). From Igor Sobrado in PR 23278.
 1.69 27-Oct-2003  wiz Do not xref alias(1) since that points to csh(1).
Noted by Igor Sobrado in PR 23278, fixed following a suggestion by Greg A. Woods.
 1.68 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.67 27-Jun-2003  wiz Quote some punctuation.
 1.66 06-May-2003  wiz Drop trailing space.
 1.65 04-May-2003  gmcgarry Add new builtin 'inputrc' which allows keybindings to be redefined
for the current shell. From Arne H Juul in PR#10097.
 1.64 02-May-2003  gmcgarry Expand documentation of emacs and vi modes. From
Jeremy C. Reed in PR#14578.
 1.63 12-Apr-2003  zuntum add missing parenthesis
 1.62 22-Mar-2003  kristerw Change "if" to "if and only if" per discussion in PR 20794.
 1.61 25-Feb-2003  wiz .Nm does not need a dummy argument ("") before punctuation or
for correct formatting of the SYNOPSIS any longer.
 1.60 12-Feb-2003  wiz New sentence, new line; bump date for last change.
 1.59 12-Feb-2003  gmcgarry Introduce LANG environment variable and Xref to nls(7).
Comment out the statement: "We expect POSIX conformance
by the time 4.4BSD is released."
 1.58 23-Jan-2003  wiz Fix indentation of continuation of first line in SYNOPSIS.
 1.57 22-Jan-2003  wiz More markup, more commas, less typos.
 1.56 22-Jan-2003  dsl Support command -p, -v and -V as posix
Stop temporary PATH assigments messing up hash table
Fix sh -c -e "echo $0 $*" -a x (as posix)
(agreed by christos)
 1.55 28-Dec-2002  uebayasi trap '' SIGINT -> trap '' INT.
 1.54 19-Dec-2002  kleink Another it's -> its.
 1.53 12-Dec-2002  uebayasi `` [n1]>&n2 Duplicate standard output (or n1) _TO_ n2.''
 1.52 02-Oct-2002  wiz filesystem -> file system, automaticly -> automatically.
 1.51 01-Oct-2002  wiz Use more mdoc, particularly to get rid of some \*[Lt] and \*[Gt].
 1.50 25-Sep-2002  wiz New policy: New sentences start on a new line.
Patches by Robert Elz <kre at munnari oz au>, with minimal changes by me.
 1.49 15-May-2002  bjh21 Implement sh -a (allexport). Code (all two lines of it) from FreeBSD
(FreeBSD var.c 1.13, sh.1 1.27).
 1.48 15-May-2002  christos implement noclobber. From Ben Harris, with minor tweaks from me. Two
unimplemented comments to go. Go Ben!
 1.47 15-May-2002  christos Implement unset variable error messages from Ben Harris.
 1.46 24-Feb-2002  lukem first variable argument to "read" is not optional
 1.45 08-Feb-2002  ross Generate <>& symbolically. I'm avoiding .../dist/... directories for now.
 1.44 20-Dec-2001  wiz Punctuation nits, drop unnecessary .Pps, sort sections.
 1.43 03-Apr-2001  wiz Don't xref set(1) and case(1), since they are builtins and we don't
have separate man pages for them.
Xref passwd 5 instead of 4, environ 7 instead of 5, and comment out xref
to profile(4), which we don't have.
Improve markup of SYNOPSIS.
Some whitespace fixes while I'm here.
 1.42 01-Apr-2001  toddpw Correct {list;} example and fix formatting/typo in the operator lists.
 1.41 18-Mar-2001  wulf Extended functionality of the trap builtin, which now closely follows
POSIX recommendations.

- trap now accepts signal names and signal numbers
e.g. INT, SIGINT, 2
- added option -l that outputs a list of valid signals
- added signal EXIT to list of valid signals
- a `-' in the action part will reset specified signal to their
default behaviour
- changed standard output format to make it suitable as an input
to another shell that achieves the same trapping results
 1.40 20-Nov-2000  christos fix typo.
 1.39 20-Nov-2000  christos Add an example on how to use getopts, stolen from the getopt manual page :-)
 1.38 21-Sep-2000  phil .Bl takes parameter "-offset indent", not "-indent".
 1.37 04-Sep-2000  kleink For commands and utilities, use EXIT STATUS rather than RETURN VALUES as
appropriate (and documented in mdoc(7)).
 1.36 28-Aug-2000  hubertf Add 'RETURN VALUE' section header.
 1.35 18-Jul-2000  jhawk Various mandoc updates to the Builtins
section; mostly .Ic, a few other nits.
 1.34 17-Jul-2000  jhawk Note the meaning of 'trap 0' (execute on exit from shell)
 1.33 16-Nov-1999  hubertf branches: 1.33.4;
Add under which conditions the "read" builtin returns success/failure.

Suggested in PR 8813 by Eric Mumpower <nocturne@arepa.com>
 1.32 28-Sep-1999  bouyer xref sysctl(8) (for proc.<pid>.rlimits)
 1.31 27-Sep-1999  mjl Mention -c option to sh(1), noticed by Matthew Aldous in PR/8499.
 1.30 06-Jul-1999  christos branches: 1.30.2;
add -q in the synopsis line
 1.29 23-Mar-1999  ross Make the `...' actually appear in the case/esac syntax section.
Fix a space botch in the $@ example.
Kill warnings caused by the effective but wrong use of \[ and \] to
perform the function of \&[ and \&].
 1.28 04-Feb-1999  cjs Add -q option, which when used with -v and/or -x, turns off the tracing
during the execution of /etc/profile, .profile and $ENV.
 1.27 28-Jan-1999  kleink Add support for the export and readonly -p option.
 1.26 11-Jan-1999  garbled Massive fixup:
Rewrite man page in mandoc format rather than nasty man format.
Fix a ton of parsing errors, and generate proper .Xr's.
document all known environment variables.
suggest ksh rather than bash.

The last two fix PR #1966. Wheee!

Somebody with access to the POSIX spec needs to go in here, and document
our adherence, or lack thereof.
 1.25 17-Nov-1998  msaitoh fix some bugs
 1.24 29-Oct-1998  garbled Modify to better document getopts. This fixes PR# 704. Much thanks to
Christos for helping me out with this.
 1.23 04-Jul-1998  ross Small edit to n1>&n2 description.
 1.22 05-Nov-1997  kleink Per 1003.2, the (builtin) read utility shall treat the backslash as an
escape character (including line continuation), unless the `-r' option
is specified:
* adopt to this behaviour, add the `-r' option to disable it;
* remove the `-e' option, which was previously necessary to get this behaviour.
 1.21 10-Jul-1997  phil branches: 1.21.2;
Add a missing ) in the description of the builtin "set".
 1.20 23-May-1997  cjs Add documentation for ulimit command, courtsey of
Eric Fischer <eric@fudge.uchicago.edu>.
 1.19 08-Mar-1997  mouse alternate -> alternative, per PR 2643
 1.18 06-Feb-1997  christos add type builtin.
 1.17 16-Oct-1996  christos PR/2808: Add HISTORY section and documentation of getopts. (from FreeBSD)
 1.16 02-Sep-1996  christos Apply PR#2721 from VaX#n8: make man page more lucid in places.
 1.15 11-May-1995  christos branches: 1.15.6;
Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.14 21-Mar-1995  cgd convert to new RCS id conventions.
 1.13 23-Jan-1995  christos I added the documented in the manual but not implemented variable expansions:

${#WORD}
${WORD%PAT}
${WORD%%PAT}
${WORD#PAT}
${WORD##PAT}
 1.12 12-Jan-1995  jtc Describe the : shell builtin.
Fixes PR #712.
 1.11 11-Jun-1994  mycroft Add RCS ids.
 1.10 11-May-1994  jtc sync with 4.4lite
 1.9 04-May-1994  jtc Comment out sections of the manpages that are not, and will probably never
be, appropriate for ash as configured for NetBSD. In particular the /u
"magic" directory, and atty(1) support.
 1.8 03-Feb-1994  jtc spelling mistakes
 1.7 27-Jan-1994  jtc Remove text describing how the dot command does not do a $PATH search,
since we added that behavior to get closer to POSIX.2.
 1.6 01-Aug-1993  mycroft Add RCS identifiers.
 1.5 02-May-1993  mycroft Fix typo.
 1.4 22-Apr-1993  mycroft Fix various bugs in man pages (from 386BSD patch 130).
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.15.6.2 04-Mar-1997  mycroft Pull up latest sh(1). Fixes yet more bugs.
 1.15.6.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.21.2.1 06-Nov-1997  mellon Pull rev 1.22 up from trunk (kleink)
 1.30.2.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.33.4.7 23-Feb-2002  he Pull up revision 1.41 (requested by jonb):
Extend functionality of the trap builtin, which now more closely
follows POSIX recommendations:
o accept signal names as well as signal numbers
o add ``-l'' option which outputs list of valid signals
o add signal EXIT to list of valid signals
o an ``-'' in the action part will reset signal to default behaviour
o changed standard output of ``trap'' to make it suitable as
subsequent input
 1.33.4.6 04-Apr-2001  he Pull up revision 1.43 (via patch, requested by wiz):
Don't xref set(1) and case(1), since they are builtins and we don't
have separate man pages for them.
Xref passwd 5 instead of 4, environ 7 instead of 5, and comment out xref
to profile(4), which we don't have.
Improve markup of SYNOPSIS.
Some whitespace fixes while I'm here.
 1.33.4.5 18-Mar-2001  wulf Reversed submission of trap.c and sh.1 to wrong branch
 1.33.4.4 17-Mar-2001  wulf Extended functionality of the trap builtin, which now closely follows
POSIX recommendations.

- trap now accepts signal names and signal numbers
e.g. INT, SIGINT, 2
- added option -l that outputs a list of valid signals
- added signal EXIT to list of valid signals
- a `-' in the action part will reset specified signal to their
default behaviour
- changed standard output format to make it suitable as an input
to another shell that achieves the same trapping results
 1.33.4.3 21-Sep-2000  phil .Bl argument is "-offset indent", not "-indent" (Approved thropej.)
 1.33.4.2 28-Aug-2000  hubertf Pull up to netbsd-1-5 branch, OK'd by thorpej:

Log Message:
> Add 'RETURN VALUE' section header.

Files & Revisionis:
> cvs rdiff -r1.19 -r1.20 basesrc/bin/cat/cat.1
> cvs rdiff -r1.12 -r1.13 basesrc/bin/chmod/chmod.1
> cvs rdiff -r1.14 -r1.15 basesrc/bin/cp/cp.1
> cvs rdiff -r1.8 -r1.9 basesrc/bin/dd/dd.1
> cvs rdiff -r1.9 -r1.10 basesrc/bin/echo/echo.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/expr/expr.1
> cvs rdiff -r1.25 -r1.26 basesrc/bin/ls/ls.1
> cvs rdiff -r1.10 -r1.11 basesrc/bin/mkdir/mkdir.1
> cvs rdiff -r1.23 -r1.24 basesrc/bin/mt/mt.1
> cvs rdiff -r1.12 -r1.13 basesrc/bin/mv/mv.1
> cvs rdiff -r1.16 -r1.17 basesrc/bin/pwd/pwd.1
> cvs rdiff -r1.9 -r1.10 basesrc/bin/rm/rm.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/rmdir/rmdir.1
> cvs rdiff -r1.35 -r1.36 basesrc/bin/sh/sh.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/sleep/sleep.1
> cvs rdiff -r1.20 -r1.21 basesrc/bin/stty/stty.1
 1.33.4.1 18-Jul-2000  jhawk Pullup revs 1.34, 1.35, approved by jhawk.
rev 1.34:
Note the meaning of 'trap 0' (execute on exit from shell)
rev 1.35:
Various mandoc updates to the Builtins
section; mostly .Ic, a few other nits.
 1.85.2.2 27-Jan-2010  bouyer Pull up following revision(s) (requested by dholland in ticket #1380):
bin/sh/cd.c: revision 1.40
bin/sh/sh.1: revision 1.95
bin/sh/sh.1: revision 1.97
Make the cd builtin accept and ignore -P, which is a kshism that has been
allowed to leak into POSIX and selects the behavior cd already implements.
Closes PR bin/42557 and also relevant to PR pkg/42168.
I suppose this should probably be pulled up to both -4 and -5...
Bump date for cd -P support.
 1.85.2.1 16-Apr-2007  bouyer Pull up following revision(s) (requested by apb in ticket #570):
bin/sh/expand.c: revision 1.78
bin/sh/arith.y: revision 1.18
bin/sh/expand.h: revision 1.17
regress/bin/sh/expand.sh: revision 1.4
bin/sh/sh.1: revision 1.86
bin/sh/arith_lex.l: revision 1.14
Make /bin/sh use intmax_t (instead of int) for arithmetic in $((...)).
 1.87.18.3 30-Jan-2010  snj Pull up following revision(s) (requested by dholland in ticket #1274):
bin/sh/sh.1: revision 1.97 via patch
Bump date for cd -P support.
 1.87.18.2 30-Jan-2010  snj Pull up following revision(s) (requested by dholland in ticket #1274):
bin/sh/cd.c: revision 1.40
bin/sh/sh.1: revision 1.95
Make the cd builtin accept and ignore -P, which is a kshism that has been
allowed to leak into POSIX and selects the behavior cd already implements.
Closes PR bin/42557 and also relevant to PR pkg/42168.
I suppose this should probably be pulled up to both -4 and -5...
 1.87.18.1 01-Apr-2009  snj branches: 1.87.18.1.4;
Pull up following revision(s) (requested by mrg in ticket #622):
bin/csh/csh.1: revision 1.46
bin/csh/func.c: revision 1.37
bin/ps/print.c: revision 1.111
bin/ps/ps.c: revision 1.74
bin/sh/miscbltin.c: revision 1.38
bin/sh/sh.1: revision 1.92 via patch
external/bsd/top/dist/machine/m_netbsd.c: revision 1.7
lib/libkvm/kvm_proc.c: revision 1.82
sys/arch/mips/mips/cpu_exec.c: revision 1.55
sys/compat/darwin/darwin_exec.c: revision 1.57
sys/compat/ibcs2/ibcs2_exec.c: revision 1.73
sys/compat/irix/irix_resource.c: revision 1.15
sys/compat/linux/arch/amd64/linux_exec_machdep.c: revision 1.16
sys/compat/linux/arch/i386/linux_exec_machdep.c: revision 1.12
sys/compat/linux/common/linux_limit.h: revision 1.5
sys/compat/osf1/osf1_resource.c: revision 1.14
sys/compat/svr4/svr4_resource.c: revision 1.18
sys/compat/svr4_32/svr4_32_resource.c: revision 1.17
sys/kern/exec_subr.c: revision 1.62
sys/kern/init_sysctl.c: revision 1.160
sys/kern/kern_exec.c: revision 1.288
sys/kern/kern_resource.c: revision 1.151
sys/sys/param.h: patch
sys/sys/resource.h: revision 1.31
sys/sys/sysctl.h: revision 1.184
sys/uvm/uvm_extern.h: revision 1.153
sys/uvm/uvm_glue.c: revision 1.136
sys/uvm/uvm_mmap.c: revision 1.128
usr.bin/systat/ps.c: revision 1.32
- - add new RLIMIT_AS (aka RLIMIT_VMEM) resource that limits the total
address space available to processes. this limit exists in most other
modern unix variants, and like most of them, our defaults are unlimited.
remove the old mmap / rlimit.datasize hack.
- - adds the VMCMD_STACK flag to all the stack-creation vmcmd callers.
it is currently unused, but was added a few years ago.
- - add a pair of new process size values to kinfo_proc2{}. one is the
total size of the process memory map, and the other is the total size
adjusted for unused stack space (since most processes have a lot of
this...)
- - patch sh, and csh to notice RLIMIT_AS. (in some cases, the alias
RLIMIT_VMEM was already present and used if availble.)
- - patch ps, top and systat to notice the new k_vm_vsize member of
kinfo_proc2{}.
- - update irix, svr4, svr4_32, linux and osf1 emulations to support
this information. (freebsd could be done, but that it's best left
as part of the full-update of compat/freebsd.)
this addresses PR 7897. it also gives correct memory usage values,
which have never been entirely correct (since mmap), and have been
very incorrect since jemalloc() was enabled.
tested on i386 and sparc64, build tested on several other platforms.
thanks to many folks for feedback and testing but most espcially
chuq and yamt for critical suggestions that lead to this patch not
having a special ugliness i wasn't happy with anyway :-)
 1.87.18.1.4.1 21-Apr-2010  matt sync to netbsd-5
 1.88.2.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.99.4.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.106.2.2 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.106.2.1 30-Oct-2012  yamt sync with head
 1.108.2.3 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.108.2.2 23-Jun-2013  tls resync from head
 1.108.2.1 20-Nov-2012  tls Resync to 2012-11-19 00:00:00 UTC
 1.112.2.1 10-Aug-2014  tls Rebase.
 1.123.4.1 21-Apr-2017  bouyer Sync with HEAD
 1.123.2.2 26-Apr-2017  pgoyette Sync with HEAD
 1.123.2.1 20-Mar-2017  pgoyette Sync with HEAD
 1.131.2.3 19-May-2017  pgoyette Resolve conflicts from previous merge (all resulting from $NetBSD
keywork expansion)
 1.131.2.2 11-May-2017  pgoyette Sync with HEAD
 1.131.2.1 02-May-2017  pgoyette Sync with HEAD - tag prg-localcount2-base1
 1.146.2.6 07-Dec-2018  martin Pull up following revision(s) (requested by kre in ticket #1127):

bin/sh/var.h: revision 1.38 (via patch)
bin/sh/var.c: revision 1.72
bin/sh/sh.1: revision 1.211 (via patch)

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
 1.146.2.5 25-Oct-2017  snj Pull up following revision(s) (requested by kre in ticket #323):
bin/sh/sh.1: revision 1.168
Fix typo: s/one or mode/one or more/
 1.146.2.4 25-Oct-2017  snj Pull up following revision(s) (requested by kre in ticket #310):
bin/sh/expand.c: revision 1.121
bin/sh/sh.1: revision 1.167 via patch
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).
 1.146.2.3 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.146.2.2 09-Jun-2017  snj Pull up following revision(s) (requested by kre in ticket #15):
bin/sh/sh.1: revision 1.148
Fix a typo (or rather a remnant of an earlier intent).
 1.146.2.1 05-Jun-2017  snj Pull up following revision(s) (requested by kre in ticket #5):
bin/sh/cd.c: revision 1.48
bin/sh/eval.c: revision 1.141
bin/sh/exec.c: revision 1.48
bin/sh/exec.h: revision 1.25
bin/sh/mail.c: revisions 1.17, 1.18
bin/sh/sh.1: revision 1.147
Make cd (really) do cd -P, and not just claim that is what it is doing
while doing a half-hearted, broken, partial, version of cd -L instead.
The latter (as the manual says) is not supported, what's more, it is an
abomination, and should never be supported (anywhere.)
Fix the doc so that the pretense that we notice when a path given crosses
a symlink (and turns on printing of the destination directory) is claimed
no more (that used to be true until late Dec 2016, but was changed). Now
the print happens if -o cdprint is set, or if an entry from CDPATH that is
not "" or "." is used (or if the "cd dest repl" cd cmd variant is used.)
Fix CDPATH processing: avoid the magic '%' processing that is used for
PATH and MAILPATH from corrupting CDPATH. The % magic (both variants)
remains undocumented.
Also, don't double the '/' if an entry in PATH or CDPATH ends in '/'
(as in CDPATH=":/usr/src/"). A "cd usr.bin" used to do
chdir("/usr/src//usr.bin"). No more. This is almost invisible,
and relatively harmless, either way....
Also fix a bug where if a plausible destination directory in CDPATH
was located, but the chdir() failed (eg: permission denied) and then
a later "." or "" CDPATH entry succeeded, "print" mode was turned on.
That is:
cd /tmp; mkdir bin
mkdir -p P/bin; chmod 0 P/bin
CDPATH=/tmp/P:
cd bin
would cd to /tmp/bin (correctly) but print it (incorrectly).
Also when in "cd dest replace" mode, if the result of the replacement
generates '-' as the path named, as in:
cd $PWD -
then simply change to '-' (or attempt to, with CDPATH search), rather
than having this being equivalent to "cd -")
Because of these changes, the pwd command (and $PWD) essentially
always acts as pwd -P, even when called as pwd -L (which is still
the default.) That is, even more than it did before.
Also fixed a (kind of minor) mem management error (CDPATH related)
"whosoever shall padvance must stunalloc before repeating" (and the
same for MAILPATH).
--
If we are going to keep the MAILPATH % hack, then at least do something
rational. Since it isn't documented, what "rational" is is up for
discussion, but what it did before was not it (it was nonsense...).
 1.175.2.7 26-Jan-2019  pgoyette Sync with HEAD
 1.175.2.6 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.175.2.5 26-Nov-2018  pgoyette Sync with HEAD, resolve a couple of conflicts
 1.175.2.4 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.175.2.3 21-May-2018  pgoyette Sync with HEAD
 1.175.2.2 22-Mar-2018  pgoyette Synch with HEAD, resolve conflicts
 1.175.2.1 15-Mar-2018  pgoyette Synch with HEAD
 1.206.2.4 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.206.2.3 21-Apr-2020  martin Sync with HEAD
 1.206.2.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.206.2.1 10-Jun-2019  christos Sync with HEAD
 1.223.2.2 06-Nov-2021  martin Pull up following revision(s) (requested by kre in ticket #1372):

bin/sh/sh.1: revision 1.236 (patch)
bin/sh/cd.c: revision 1.51

PR bin/45390 - fix for folly four

In the pwd builtin, verify that curdir names '.' before
simply printing it. Never alter PWD or OLDPWD in the
pwd command.

Also while here, implement the (new: coming in POSIX, but has existed
for a while in several other shells) -e option to cd (with -e, cd -P
will exit(1) if the chdir() succeeds, but PWD cannot be discovered).
cd now prints the directory name used (if different from that given,
or cdprint is on) if interactive or (the new bit)in posix mode.

Some additional/changed comments added, and a DEBUG mode trace call
that was accidentally put inside an #if 0 block moved to where it
can do some good.

XXX pullup -9

PR bin/45390

Be explicit about what happens to PWD after a successful cd command.
Also be very clear that "cd" and "cd -P" are the same thing, and
the only cd variant implemented.
Also, when it is appropriate to print the new directory after a cd
command, note that it happens if interactive (as it always has here)
and also if the posix option is set (for POSIX compat, where "interactive"
is irrelevant). Mention that "cd -" is a case where the new directory
is printed (along with paths relative to a non-empty CDPATH entry,
and where the "cd old new" (string replacement in curdir) is used.

While here document the new -e option to cd.

XXX pullup -9
 1.223.2.1 06-Nov-2021  martin Pull up following revision(s) (requested by kre in ticket #1371):

bin/sh/main.c: revision 1.87
bin/sh/main.c: revision 1.88
bin/sh/memalloc.h: revision 1.20
bin/sh/sh.1: revision 1.235
bin/sh/memalloc.c: revision 1.34
bin/sh/memalloc.c: revision 1.35
bin/sh/memalloc.h: revision 1.19
bin/sh/shell.h: revision 1.31
bin/sh/options.c: revision 1.56

PR bin/56464

After almost 30 years, finally do the right thing and read $HOME/.profile
rather than .profile in the initial directory (it was that way in version
1.1 ...) All other ash descendants seem to have fixed this long ago.
While here, copy a feature from FreeBSD which allows "set +p" (if a
shell run by a setuid process with the -p flag is privileged) to reset
the privileges. Once done (the set +p) it cannot be undone (a later
set -p sets the 'p' flag, but that's all it does) - that just becomes a
one bit storage location.

We do this, as (also copying from FreeBSD, and because it is the right
thing to do) we don't run .profile in a privileged shell - FreeBSD run
/etc/suid_profile in that case (not a good name, it also applies to setgid
shells) but I see no real need for that, we run /etc/profile in any case,
anything that would go in /etc/suid_profile can just go in /etc/profile
instead (with suitable guards so the commands only run in priv'd shells).

One or two minor DEBUG mode changes (notably having priv'd shells identify
themselves in the DEBUG trace) and sh.1 changes with doc of the "set +p"
change, the effect that has on $PSc and a few other wording tweaks.

XXX pullup -9 (not -8, this isn't worth it for the short lifetime
that has left - if it took 28+ years for anyone to notice this, it
cannot be having all that much effect).

Use a type-correct end marker for strstrcat() rather than NULL, as
for a function with unknown number & types of args, the compiler isn't
able to automatically convert to the correct type. Issue pointed out
in off list e-mail by Rolland Illig ... Thanks.

The first arg (pointer to where to put length of result) is of a known
type, so doesn't have the same issue - we can keep using NULL for that
one when the length isn't needed.
Also, make sure to return a correctly null terminated null string in
the (absurd) case that there are no non-null args to strstrcat() (though
there are much better ways to generate "" on the stack). Since there is
currently just one call in the code, and it has real string args, this
isn't an issue for now, but who knows, some day.

NFCI - if there is any real change, then it is a change that is required.

XXX pullup -9 (together with the previous changes)
 1.252.2.1 20-Dec-2022  martin Pull up following revision(s) (requested by uwe in ticket #11):

bin/sh/sh.1: revision 1.253

sh(1): Fix markup. -compact must be last.
 1.260.2.1 02-Aug-2025  perseant Sync with HEAD
 1.34 27-Feb-2025  andvar Fix various typos in comments.
 1.33 09-Oct-2024  kre PR bin/58687 -- implement suspend as a builtin in sh

Requested by uwe@ in PR bin/58687 without objections from
anyone except me, here is an implementation of a suspend
builtin command for /bin/sh

The sh.1 man page is updated, naturally, to describe it.

This new builtin does not exist in SMALL shells -- as used
on (some) boot media, etc.

If this turns out not to be useful, it can easily be removed.
 1.32 09-Feb-2024  andvar branches: 1.32.2;
fix spelling mistakes, mainly in comments and log messages.
 1.31 26-Oct-2021  kre PR bin/56464

After almost 30 years, finally do the right thing and read $HOME/.profile
rather than .profile in the initial directory (it was that way in version
1.1 ...) All other ash descendants seem to have fixed this long ago.

While here, copy a feature from FreeBSD which allows "set +p" (if a
shell run by a setuid process with the -p flag is privileged) to reset
the privileges. Once done (the set +p) it cannot be undone (a later
set -p sets the 'p' flag, but that's all it does) - that just becomes a
one bit storage location.

We do this, as (also copying from FreeBSD, and because it is the right
thing to do) we don't run .profile in a privileged shell - FreeBSD run
/etc/suid_profile in that case (not a good name, it also applies to setgid
shells) but I see no real need for that, we run /etc/profile in any case,
anything that would go in /etc/suid_profile can just go in /etc/profile
instead (with suitable guards so the commands only run in priv'd shells).

One or two minor DEBUG mode changes (notably having priv'd shells identify
themselves in the DEBUG trace) and sh.1 changes with doc of the "set +p"
change, the effect that has on $PSc and a few other wording tweaks.

XXX pullup -9 (not -8, this isn't worth it for the short lifetime
that has left - if it took 28+ years for anyone to notice this, it
cannot be having all that much effect).
 1.30 10-Sep-2021  rillig bin: remove unnecessary lint comment CONSTCOND

Since 2021-01-31, lint no longer warns about 'do ... while (0)'.

No functional change.
 1.29 22-Jan-2019  kre branches: 1.29.2;

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.)
 1.28 21-Jan-2019  kre 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".
 1.27 18-Oct-2018  kre 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).
 1.26 22-Jul-2018  kre DEBUG mode only change (ie: no effect to any normal shell).

Add tracing of pattern matching (aid in debugging various issues.)
 1.25 26-Jul-2017  kre branches: 1.25.2; 1.25.4;

NFC: DEBUG mode change - add a couple more TRACE macros to allow for
conditional (apart from the normal debug conditions) tracing.
 1.24 17-Jun-2017  kre NFC: DEBUG related comment change - catch up with reality.
 1.23 29-May-2017  kre branches: 1.23.2;

More DEBUG mode changes. As usual, read the source if you care.
 1.22 27-May-2017  kre More standard (and saner) implementation of the ! reserved word.
Unless the shell is compiled with the (compilation time) option
BOGUS_NOT_COMMAND (as in CFLAGS+=-DBOGUS_NOT_COMMAND) which it
will not normally be, the ! command (reserved word) will only
be permitted at the start of a pipeline (which includes the
degenerate pipeline with no '|'s in it of course - ie: a simple cmd)
and not in the middle of a pipeline sequence (no "cmd | ! cmd" nonsense.)
If the latter is really required, then "cmd | { ! cmd; }" works as
a standard equivalent.

In POSIX mode, permit only one ! ("! pipeline" is ok. "! ! pipeline" is not).
Again, if needed (and POSIX conformance is wanted) "! { ! pipeline; }"
works as an alternative - and is safer, some shells treat "! ! cmd" as
being identical to "cmd" (this one did until recently.)
 1.21 13-May-2017  kre The beginnings of the great shell DEBUG (tracing) upgrade of 2017...

First, be aware that the DEBUG spoken of here has nothing whatever to
do with MKDEBUG=true type builds of NetBSD. The only way to get a
DEBUG shell is to build it yourself manually.

That said, for non-DEBUG shells, this change makes only one slight
(trivial really) difference, which should affect nothing.

Previously some code was defined like ...

function(args)
{
#ifdef DEBUG
/* function code goes here */
#endif
}

and called like ...

#ifdef DEBUG
function(params);
#endif

resulting in several empty functions that are never called being
defined in non-DEBUG shells. Those are now gone. If you can detect
the difference any way other than using "nm" or similar, I'd be very
surprised...

For DEBUG shells, this introduces a whole new TRACE() setup to use
to assist in debugging the shell.

I have had this locally (uncommitted) for over a year... it helps.

By itself this change is almost useless, nothing really changes, but
it provides the framework to allow other TRACE() calls to be updated
over time. This is why I had not committed this earlier, my previous
version required a flag day, with all the shell's internal tracing
being updated a once - which I had done, but that shell version has
bit-rotted so badly now it is almost useless...

Future updates will add the mechanism to allow the new stuff to actually
be used in a productive way, and following that, over time, gradual
conversion of all the shell tracing to the updated form (as required,
or when I am bored...)

The one useful change that we do get now is that the fd that the shell
uses for tracing (which was usually 3, but not any more) is now protected
from user/script interference, like all the other shell inernal fds.

There is no doc (nor will there be) on any of this, if you are not reading
the source code it is useless to you, if you are, you know how it works.
 1.20 21-Mar-2017  joerg branches: 1.20.2;
Add includes guards since we can include the header twice and typedef
redefinitions are a C11 feature.
 1.19 03-May-2016  kre branches: 1.19.2; 1.19.4;

Fix things so that STATIC can me made static (-DSTATIC=static)
and have the shell still compile, link, and run...

ok christos@
 1.18 28-Apr-2013  dholland Add const.
 1.17 07-Aug-2003  agc branches: 1.17.54; 1.17.60;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.16 22-Jan-2003  dsl Support command -p, -v and -V as posix
Stop temporary PATH assigments messing up hash table
Fix sh -c -e "echo $0 $*" -a x (as posix)
(agreed by christos)
 1.15 27-Sep-2002  christos VFork()ing shell: From elric@netbsd.org:
Plus my changes:
- walking process group fix in foregrounding a job.
- reset of process group in parent shell if interrupted before the wait.
- move INTON lower in the dowait so that the job structure is
consistent.
- error check all setpgid(), tcsetpgrp() calls.
- eliminate unneeded strpgid() call.
- check that we don't belong in the process group before we try to
set it.
 1.14 25-May-2002  wiz __STDC__ is always defined on NetBSD, so remove #ifdef __STDC__ (and
unnecessary #else cases).
 1.13 22-May-2000  elric branches: 1.13.6;
Back out previous vfork changes.
 1.12 13-May-2000  elric In order to use __NetBSD_Version__, I needed to include sys/param.h.
 1.11 13-May-2000  elric Now we use vfork(2) instead of fork(2) when we can.
 1.10 16-Oct-1996  christos PR/2808: Don't define DEBUG and #ifdef out functions not needed when DEBUG
is not defined (from FreeBSD).
 1.9 01-Mar-1996  jtc branches: 1.9.4;
Changed so that cd builtin doesn't do wierd stuff when cd'ing through
symlinks. From Chris Demetriou <cgd@NetBSD.ORG>. Fixes PR #1776.

Changed so that INTOFF/INTON are paired in getpwd(). From Matthias
Pfaller <leo@marco.de>. Fixes PR #2130.
 1.8 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.7 21-Mar-1995  cgd convert to new RCS id conventions.
 1.6 11-Jun-1994  mycroft Add RCS ids.
 1.5 11-May-1994  jtc sync with 4.4lite
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.9.4.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.13.6.1 27-Mar-2002  elric Doing the vfork work on ash on a branch to try to shake out the
problems before I expose everyone to them. This checkin represents
a merge of the prior work, which I backed out a while ago, to the
HEAD only and does not incorporate any additional bugfixes. The
additional bugfixes and code-cleanup will occur in later checkins.

For reference the patches that were used are:
cvs diff -kk -r1.51 -r1.55 eval.c | patch
cvs diff -kk -r1.27 -r1.28 exec.c | patch
cvs diff -kk -r1.15 -r1.16 exec.h | patch
cvs diff -kk -r1.32 -r1.33 input.c | patch
cvs diff -kk -r1.10 -r1.11 input.h | patch
cvs diff -kk -r1.32 -r1.35 jobs.c | patch
cvs diff -kk -r1.9 -r1.11 jobs.h | patch
cvs diff -kk -r1.36 -r1.37 main.c | patch
cvs diff -kk -r1.20 -r1.21 redir.c | patch
cvs diff -kk -r1.10 -r1.11 redir.h | patch
cvs diff -kk -r1.10 -r1.12 shell.h | patch
cvs diff -kk -r1.22 -r1.23 trap.c | patch
cvs diff -kk -r1.12 -r1.13 trap.h | patch
cvs diff -kk -r1.23 -r1.24 var.c | patch
cvs diff -kk -r1.16 -r1.17 var.h | patch

All other changes were simply the resolution of the resulting
conflicts, which occured only in the merge of jobs.c.

Begins to address PR: bin/5475
 1.17.60.1 23-Jun-2013  tls resync from head
 1.17.54.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.19.4.1 21-Apr-2017  bouyer Sync with HEAD
 1.19.2.1 26-Apr-2017  pgoyette Sync with HEAD
 1.20.2.1 19-May-2017  pgoyette Resolve conflicts from previous merge (all resulting from $NetBSD
keywork expansion)
 1.23.2.1 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.25.4.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.25.4.2 21-Apr-2020  martin Sync with HEAD
 1.25.4.1 10-Jun-2019  christos Sync with HEAD
 1.25.2.3 26-Jan-2019  pgoyette Sync with HEAD
 1.25.2.2 20-Oct-2018  pgoyette Sync with head
 1.25.2.1 28-Jul-2018  pgoyette Sync with HEAD
 1.29.2.1 06-Nov-2021  martin Pull up following revision(s) (requested by kre in ticket #1371):

bin/sh/main.c: revision 1.87
bin/sh/main.c: revision 1.88
bin/sh/memalloc.h: revision 1.20
bin/sh/sh.1: revision 1.235
bin/sh/memalloc.c: revision 1.34
bin/sh/memalloc.c: revision 1.35
bin/sh/memalloc.h: revision 1.19
bin/sh/shell.h: revision 1.31
bin/sh/options.c: revision 1.56

PR bin/56464

After almost 30 years, finally do the right thing and read $HOME/.profile
rather than .profile in the initial directory (it was that way in version
1.1 ...) All other ash descendants seem to have fixed this long ago.
While here, copy a feature from FreeBSD which allows "set +p" (if a
shell run by a setuid process with the -p flag is privileged) to reset
the privileges. Once done (the set +p) it cannot be undone (a later
set -p sets the 'p' flag, but that's all it does) - that just becomes a
one bit storage location.

We do this, as (also copying from FreeBSD, and because it is the right
thing to do) we don't run .profile in a privileged shell - FreeBSD run
/etc/suid_profile in that case (not a good name, it also applies to setgid
shells) but I see no real need for that, we run /etc/profile in any case,
anything that would go in /etc/suid_profile can just go in /etc/profile
instead (with suitable guards so the commands only run in priv'd shells).

One or two minor DEBUG mode changes (notably having priv'd shells identify
themselves in the DEBUG trace) and sh.1 changes with doc of the "set +p"
change, the effect that has on $PSc and a few other wording tweaks.

XXX pullup -9 (not -8, this isn't worth it for the short lifetime
that has left - if it took 28+ years for anyone to notice this, it
cannot be having all that much effect).

Use a type-correct end marker for strstrcat() rather than NULL, as
for a function with unknown number & types of args, the compiler isn't
able to automatically convert to the correct type. Issue pointed out
in off list e-mail by Rolland Illig ... Thanks.

The first arg (pointer to where to put length of result) is of a known
type, so doesn't have the same issue - we can keep using NULL for that
one when the length isn't needed.
Also, make sure to return a correctly null terminated null string in
the (absurd) case that there are no non-null args to strstrcat() (though
there are much better ways to generate "" on the stack). Since there is
currently just one call in the code, and it has real string args, this
isn't an issue for now, but who knows, some day.

NFCI - if there is any real change, then it is a change that is required.

XXX pullup -9 (together with the previous changes)
 1.32.2.1 02-Aug-2025  perseant Sync with HEAD
 1.59 11-Nov-2024  kre This commit is intended to be what was intended to happen in the
commit of Sun Nov 10 01:22:24 UTC 2024, see:

http://mail-index.netbsd.org/source-changes/2024/11/10/msg154310.html

The commit message for that applies to this one (wholly). I believe that
the problem with that version which caused it to be reverted has been found
and fixed in this version (a necessary change was made as part of one of
the fixes, but the side-effect implications of that were missed -- bad bad me.)

In addition, I found some more issues with setting close-on-exec on other
command lines

With:
func 3>whatever

fd 3 (anything > 2) got close on exec set. That makes no difference
to the function itself (nothing gets exec'd therefore nothing gets closed)
but does to any exec that might happen running a command within the function.

I believe that if this is done (just as if "func" was a regular command,
and not a function) such open fds should be passed through to anything
they exec - unless the function (or other command) takes care to close the
fd passed to it, or explicitly turn on close-on exec.

I expect this usage to be quite rare, and not make much practical difference.

The same applies do builtin commands, but is even less relevant there, eg:

printf 3>whatever

would have set close-on-exec on fd 3 for printf. This is generally
completely immaterial, as printf - and most other built-in commands -
neither uses any fd other than (some of) 0 1 & 2, nor do they exec anything.

That is, except for the "exec" built-in which was the focus of the original
fix (mentioned above) and which should remain fixed here, and for the "."
command.

Because of that last one (".") close-on-exec should not be set on built-in
commands (any of them) for redirections on the command line. This will
almost never make a difference - any such redirections last only as long
as the built-in command lasts (same with functions) and so will generally
never care about the state of close-on-exec, and I have never seen a use
of the "." command with any redirections other than stderr (which is unaffected
here, fd's <= 2 never get close-on-exec set). That's probably why no-one
ever noticed.

There are still "fd issues" when running a (non #!) shell script, that
are hard to fix, which we should probably handle the way most other shells
have, by simply abandoning the optimisation of not exec'ing a whole new
shell (#! scripts do that exec) and just doing it that way. Issues solved!
One day.
 1.58 10-Nov-2024  kre Revert the recent change until I can work out how things are broken.
 1.57 10-Nov-2024  kre exec builtin command redirection fixes

Several changes, all related to the exec special built in command,
or to close on exec, one way or another. (Except a few white space
and comment additions, KNF, etc)

1. The bug found by Edgar Fuß reported in:
http://mail-index.netbsd.org/tech-userlevel/2024/11/05/msg014588.html
has been fixed, now "exec N>whatever" will set close-on-exec for fd N
(as do ksh versions, and allowed by POSIX, though other shells do not)
which has happened now for many years. But "exec cmd N>whatever"
(which looks like the same command when redirections are processed)
which was setting close-on-exec on N, now no longer does, so fd N
can be passed to cmd as an open fd.

For anyone who cares, the big block of change just after "case CMDBUILTIN:"
in evalcommand() in eval.c is the fix for this (one line replaced by
about 90 ... though most of that is comments or #if 0'd example code
for later). It is a bit ugly, and will get worse if our exec command
ever gets any options, as others have, but it does work.

2. when the exec builtin utility is used to alter the shell's redirections
it is now "all or nothing". Previously the redirections were executed
left to right. If one failed, no more were attempted, but the earlier
ones remained. This makes no practical difference to a non-interactive
shell, as a redirection error causes that shell to exit, but it makes
a difference to interactive shells. Now if a redirection fails, any
earlier ones which had been performed are undone. Note however that
side-effects of redirections (like creating, or truncating, files in
the filesystem, cannot be reversed - just the shell's file descriptors
returned to how they were before the error).

Similarly usage errors on exec now exist .. our exec takes no options
(but does handle "--" as POSIX says it must - has done for ages).
Until now, that was the only magic piece of exec, running
exec -a name somecommand
(which several other shells support) would attempt to exec the "-a"
command, and most likely fail, causing immediate exit from the shell.
Now that is a usage error - a non-interactive shell still exits, as
exec is a special builtin, and any error from a special builtin causes
a non-interactive shell to exit. But now, an interactive shell will
no longer exit (and any redirections that were on the command will be
undone, the same as for a redirection error).

3. When a "close on exec" file descriptor is temporarily saved, so the
same fd can be redirected for another command (only built-in commands
and functions matter, redirects for file system commands happen after
a fork() and at that stage if anything goes wrong, the child simply
exits - but for non-forking commands, doing something like printf >file
required the previous stdout to be saved elsewhere, "file" opened to
be the new stdout, then when printf is finished, the old stdout moved
back. Anyway, if the fd being moved had close on exec set, then
when it was moved back, the close on exec was lost. That is now fixed.

4. The fdflags command no longer allows setting close on exec on stdin,
stdout, or stderr - POSIX requires that those 3 fd's always be open
(to something) when any normal command is invoked. With close-on-exec
set on one of these, that is impossible, so simply refuse it (when
"exec N>file" sets close on exec, it only does it for N>2).

Minor changes (should be invisible)

a. The shell now keeps track of the highest fd number it sees doing
normal operations (there are a few internal pipe() calls that aren't
monitored and a couple of others, but in general the shell will now
know the highest fd it ever saw allocated to it). This is mostly
for debugging.

b. calls to fcntl() passing an int as the "arg" are now all properly
cast to the void * that the fcntl kernel is expecting to receive.
I suspect that makes no actual difference to anything, but ...
 1.56 12-Jul-2024  kre Meaningless gcc inspired change.

This is in code only compiled in DEBUG builds (so not part of any
normal NetBSD build).

NFC
 1.55 07-Apr-2023  kre branches: 1.55.2;

The great shell trailing whitespace cleanup of 2023...
Inspired by private e-mail comments from mouse@

NFCI.
 1.54 10-Sep-2021  rillig bin: remove unnecessary lint comment CONSTCOND

Since 2021-01-31, lint no longer warns about 'do ... while (0)'.

No functional change.
 1.53 14-Feb-2019  kre 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)
 1.52 22-Jan-2019  kre 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.)
 1.51 21-Jan-2019  kre 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".
 1.50 18-Oct-2018  kre 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.
 1.49 19-Aug-2018  kre NFC: DEBUG mode only change. When tracing, show >&- as ">&-"
rather than ">&-1" (the same op as >&n where internally n < 0
means "close")
 1.48 22-Jul-2018  kre DEBUG mode only change (ie: no effect to any normal shell).

Add tracing of pattern matching (aid in debugging various issues.)
 1.47 30-Jun-2017  kre branches: 1.47.4; 1.47.6;

NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
 1.46 17-Jun-2017  kre NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
 1.45 17-Jun-2017  kre NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
 1.44 08-Jun-2017  kre Correct spelling in comments of DEBUG only code...
 1.43 07-Jun-2017  kre A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)

This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)

The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.

This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).

I would not call the results of this perfect yet, but it is close.
 1.42 29-May-2017  kre branches: 1.42.2;

More DEBUG mode changes. As usual, read the source if you care.
 1.41 18-May-2017  kre DEBUG mode only change - mostly to output when option to show shell
internal sub-process nesting is enabled, and very deep nesting levels exist.

NFC for anyone else.
 1.40 15-May-2017  kre DEBUG mode shell update (changes nothing for shells which are not
compiled for DEBUG.)

Add debug builtin command, and corresponding -D command line option.
As usual, for DEBUG related stuff, read the source for info, that's
all there is about this.

This completes the infrastructure changes for the updated DEBUG TRACE
mechanism, so now converting the rest of the shell's internal tracing
can happen as desired - piecemeal.
 1.39 13-May-2017  kre The beginnings of the great shell DEBUG (tracing) upgrade of 2017...

First, be aware that the DEBUG spoken of here has nothing whatever to
do with MKDEBUG=true type builds of NetBSD. The only way to get a
DEBUG shell is to build it yourself manually.

That said, for non-DEBUG shells, this change makes only one slight
(trivial really) difference, which should affect nothing.

Previously some code was defined like ...

function(args)
{
#ifdef DEBUG
/* function code goes here */
#endif
}

and called like ...

#ifdef DEBUG
function(params);
#endif

resulting in several empty functions that are never called being
defined in non-DEBUG shells. Those are now gone. If you can detect
the difference any way other than using "nm" or similar, I'd be very
surprised...

For DEBUG shells, this introduces a whole new TRACE() setup to use
to assist in debugging the shell.

I have had this locally (uncommitted) for over a year... it helps.

By itself this change is almost useless, nothing really changes, but
it provides the framework to allow other TRACE() calls to be updated
over time. This is why I had not committed this earlier, my previous
version required a flag day, with all the shell's internal tracing
being updated a once - which I had done, but that shell version has
bit-rotted so badly now it is almost useless...

Future updates will add the mechanism to allow the new stuff to actually
be used in a productive way, and following that, over time, gradual
conversion of all the shell tracing to the updated form (as required,
or when I am bored...)

The one useful change that we do get now is that the fd that the shell
uses for tracing (which was usually 3, but not any more) is now protected
from user/script interference, like all the other shell inernal fds.

There is no doc (nor will there be) on any of this, if you are not reading
the source code it is useless to you, if you are, you know how it works.
 1.38 09-May-2017  kre If we are going to permit
! ! pipeline
(And for now the other places where ! is permitted)
we should at least generate the logically correct exit
status:
! ! (exit 5); echo $?
should print 1, not 5. ksh and bosh do it this way - and it makes sense.
bash and the FreeBSD sh echo "5" (as did we until now.)
dash, zsh, yash all enforce the standard syntax, and prohibit this.
 1.37 03-May-2017  kre This change affects shells compiled in DEBUG mode only, for normal
shells (anything made by build.sh) there is no change at all.

In DEBUG shells, when tree dumping, remember to include NNOT (same
omission as was just corrected in jobs.c :1.81) - of course, here there
are lots of other node types not handled as well.

ALso, avoid a core dump bug when doing a tree dump of a pieline
where the commands are not all simple commands (which can only
happen with a command string like " cmd | ! cmd | ... ". The "!"
in the middle is utter nonsense, and should be forbidden, but
for now, at least avoid a core dump.
 1.36 16-Mar-2017  kre branches: 1.36.2;

Undo local changes not intended to be committed (and certainly not
with that commit message) in the previous update. This stuff works,
and will probably appear sometime, but not right now.
 1.35 16-Mar-2017  kre Have "make clean" remove sh.html1 and adapt it to clean trace files
the way they have been generated the past 20 years or so...
 1.34 23-Oct-2016  abhinav branches: 1.34.2;
Remove unused variables.
Fixes the sh(1) build when DEBUG is enabled.
 1.33 11-May-2016  kre branches: 1.33.2;

It was twenty(-two) years ago today
J.T. Conklin told us not this way
Berkeley 4.4 lite's changed which file
And it's traced differently all this while
 1.32 29-Feb-2016  christos Even more debugging improvements (from kre)
 1.31 28-Feb-2016  christos Bug fixes to handling of unterminated here documents
(they should be, and now are, a syntax error), and
miscellaneous other minor cleanups. (from kre)
 1.30 27-Feb-2016  christos More nodenames fixes.
 1.29 27-Feb-2016  christos Improve debugging, from kre (I hooked it to the build).
 1.28 23-Aug-2011  christos - add pid to the trace file so that we don't keep overwriting ourselves
- use va_copy to print the trace arguments so that we don't deplete it for the real printf
 1.27 14-Nov-2010  christos don't core-dump if we cannot open the trace file.
 1.26 14-Nov-2003  dsl This seems to need stdlib.h to get a prototype for abort().
 1.25 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.24 22-Jan-2003  dsl Support command -p, -v and -V as posix
Stop temporary PATH assigments messing up hash table
Fix sh -c -e "echo $0 $*" -a x (as posix)
(agreed by christos)
 1.23 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.22 25-May-2002  wiz __STDC__ is always defined on NetBSD, so remove #ifdef __STDC__ (and
unnecessary #else cases).
 1.21 15-May-2002  christos implement noclobber. From Ben Harris, with minor tweaks from me. Two
unimplemented comments to go. Go Ben!
 1.20 12-Feb-2002  ross back this directory up a day, systems won't even boot (rc.subr splodes)

suggested back-to-the-drawing-board test: $ echo "${PWD:-notlikely}"
 1.19 11-Feb-2002  christos Fix off by one in the display of var trees.
 1.18 08-Oct-1999  pk Sprinkle some `const's in DEBUG bracketed code.
 1.17 04-Feb-1999  christos branches: 1.17.4;
PR/4966: Joel Reicher: Implement <> redirections which are documented in
the man page.
 1.16 01-Dec-1997  christos Remove local declaration of getenv();
 1.15 04-Jul-1997  christos branches: 1.15.2;
Fix compiler warnings.
 1.14 11-Apr-1997  christos Use #ifdef __STDC__ instead of #if __STDC__
 1.13 11-Jan-1997  tls kill 'register'
 1.12 16-Oct-1996  christos PR/2808: Don't define DEBUG and #ifdef out functions not needed when DEBUG
is not defined (from FreeBSD).
 1.11 11-May-1995  christos branches: 1.11.6;
Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.10 21-Mar-1995  cgd convert to new RCS id conventions.
 1.9 23-Jan-1995  christos I added the documented in the manual but not implemented variable expansions:

${#WORD}
${WORD%PAT}
${WORD%%PAT}
${WORD#PAT}
${WORD##PAT}
 1.8 05-Dec-1994  cgd clean up further. more patches from Jim Jegers
 1.7 04-Dec-1994  cgd from James Jegers <jimj@miller.cs.uwm.edu>: quiet -Wall, and squelch
some of the worst style errors.
 1.6 11-Jun-1994  mycroft Add RCS ids.
 1.5 11-May-1994  jtc sync with 4.4lite
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.11.6.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.15.2.1 08-May-1998  mycroft Sync with trunk, per request of christos.
 1.17.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.33.2.2 20-Mar-2017  pgoyette Sync with HEAD
 1.33.2.1 04-Nov-2016  pgoyette Sync with HEAD
 1.34.2.1 21-Apr-2017  bouyer Sync with HEAD
 1.36.2.2 19-May-2017  pgoyette Resolve conflicts from previous merge (all resulting from $NetBSD
keywork expansion)
 1.36.2.1 11-May-2017  pgoyette Sync with HEAD
 1.42.2.1 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.47.6.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.47.6.2 21-Apr-2020  martin Sync with HEAD
 1.47.6.1 10-Jun-2019  christos Sync with HEAD
 1.47.4.4 26-Jan-2019  pgoyette Sync with HEAD
 1.47.4.3 20-Oct-2018  pgoyette Sync with head
 1.47.4.2 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.47.4.1 28-Jul-2018  pgoyette Sync with HEAD
 1.55.2.1 02-Aug-2025  perseant Sync with HEAD
 1.11 30-Jun-2017  kre branches: 1.11.6;

NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
 1.10 13-May-2017  kre branches: 1.10.2;

The beginnings of the great shell DEBUG (tracing) upgrade of 2017...

First, be aware that the DEBUG spoken of here has nothing whatever to
do with MKDEBUG=true type builds of NetBSD. The only way to get a
DEBUG shell is to build it yourself manually.

That said, for non-DEBUG shells, this change makes only one slight
(trivial really) difference, which should affect nothing.

Previously some code was defined like ...

function(args)
{
#ifdef DEBUG
/* function code goes here */
#endif
}

and called like ...

#ifdef DEBUG
function(params);
#endif

resulting in several empty functions that are never called being
defined in non-DEBUG shells. Those are now gone. If you can detect
the difference any way other than using "nm" or similar, I'd be very
surprised...

For DEBUG shells, this introduces a whole new TRACE() setup to use
to assist in debugging the shell.

I have had this locally (uncommitted) for over a year... it helps.

By itself this change is almost useless, nothing really changes, but
it provides the framework to allow other TRACE() calls to be updated
over time. This is why I had not committed this earlier, my previous
version required a flag day, with all the shell's internal tracing
being updated a once - which I had done, but that shell version has
bit-rotted so badly now it is almost useless...

Future updates will add the mechanism to allow the new stuff to actually
be used in a productive way, and following that, over time, gradual
conversion of all the shell tracing to the updated form (as required,
or when I am bored...)

The one useful change that we do get now is that the fd that the shell
uses for tracing (which was usually 3, but not any more) is now protected
from user/script interference, like all the other shell inernal fds.

There is no doc (nor will there be) on any of this, if you are not reading
the source code it is useless to you, if you are, you know how it works.
 1.9 16-Mar-2017  kre branches: 1.9.2;

Undo local changes not intended to be committed (and certainly not
with that commit message) in the previous update. This stuff works,
and will probably appear sometime, but not right now.
 1.8 16-Mar-2017  kre Have "make clean" remove sh.html1 and adapt it to clean trace files
the way they have been generated the past 20 years or so...
 1.7 07-Aug-2003  agc branches: 1.7.80; 1.7.84;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.6 22-Jan-2003  dsl Support command -p, -v and -V as posix
Stop temporary PATH assigments messing up hash table
Fix sh -c -e "echo $0 $*" -a x (as posix)
(agreed by christos)
 1.5 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.4 08-Oct-1999  pk Sprinkle some `const's in DEBUG bracketed code.
 1.3 11-Apr-1997  christos branches: 1.3.6;
add forward declaration of union node;
 1.2 16-Oct-1996  christos PR/2808: Don't define DEBUG and #ifdef out functions not needed when DEBUG
is not defined (from FreeBSD).
 1.1 11-May-1995  christos branches: 1.1.6;
Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.1.6.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.3.6.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.7.84.1 21-Apr-2017  bouyer Sync with HEAD
 1.7.80.1 20-Mar-2017  pgoyette Sync with HEAD
 1.9.2.1 19-May-2017  pgoyette Resolve conflicts from previous merge (all resulting from $NetBSD
keywork expansion)
 1.10.2.1 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.11.6.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.11.6.1 21-Apr-2020  martin Sync with HEAD
 1.8 04-Feb-2019  kre Add a couple of comments. NFC.
 1.7 03-Dec-2018  kre 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.
 1.6 20-Jul-2018  kre 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),
 1.5 21-Aug-2017  kre branches: 1.5.2; 1.5.4;
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 ...
 1.4 07-Jun-2017  kre A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)

This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)

The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.

This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).

I would not call the results of this perfect yet, but it is close.
 1.3 28-Mar-2012  christos branches: 1.3.26;
include <limits.h> for CHAR_MIN/CHAR_MAX
 1.2 12-Dec-2007  lukem branches: 1.2.26;
use __RCSID()
 1.1 17-Jan-2004  dsl branches: 1.1.22;
Put a syntax.c under CVS instead of building if with the mksyntax program.
Kill mksyntax.c - no longer possible to get the 'wrong sort of chars'.
/bin/sh now has no helper binaries.
syntax.c uses C99 initialisers, run time initialisation could be used
for systems where the compiler doesn't support them.
I've used some #defines to help make this possible - but writing the code
starts making it rather messy.
 1.1.22.1 09-Jan-2008  matt sync with HEAD
 1.2.26.1 17-Apr-2012  yamt sync with head
 1.3.26.2 10-Sep-2018  martin Pull up following revision(s) via patch (requested by kre in ticket #1015):

bin/sh/expand.c: revision 1.124
bin/sh/expand.c: revision 1.127
bin/sh/parser.c: revision 1.148
bin/sh/parser.c: revision 1.149
bin/sh/syntax.c: revision 1.6
bin/sh/syntax.h: revision 1.9 (partial)

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),

-

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.)
 1.3.26.1 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.5.4.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.5.4.2 21-Apr-2020  martin Sync with HEAD
 1.5.4.1 10-Jun-2019  christos Sync with HEAD
 1.5.2.2 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.5.2.1 28-Jul-2018  pgoyette Sync with HEAD
 1.14 03-Jul-2025  kre We have our own CEOF, do not want the one from <sys/ttydefaults.h>

Fix the builds...
 1.13 15-Jun-2024  kre branches: 1.13.2;

This file uses CHAR_MIN so needs <limits.h> to be complete.

While here, fix a typo in the alternate (as in #if 0'd out) version
of the ISCTL() macro.

NFCI.
 1.12 27-Feb-2019  kre 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.
 1.11 03-Dec-2018  kre 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.
 1.10 18-Nov-2018  kre 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).
 1.9 21-Aug-2017  kre branches: 1.9.2; 1.9.4;
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 ...
 1.8 07-Jun-2017  kre A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)

This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)

The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.

This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).

I would not call the results of this perfect yet, but it is close.
 1.7 15-May-2017  kre branches: 1.7.2;

DEBUG mode shell update (changes nothing for shells which are not
compiled for DEBUG.)

Add debug builtin command, and corresponding -D command line option.
As usual, for DEBUG related stuff, read the source for info, that's
all there is about this.

This completes the infrastructure changes for the updated DEBUG TRACE
mechanism, so now converting the rest of the shell's internal tracing
can happen as desired - piecemeal.
 1.6 16-Mar-2016  christos branches: 1.6.6;
parenthesize for safety.
 1.5 16-Mar-2016  christos Remove wrong unsigned cast, index can be negative. Cast char to int so that
gcc does not warn. Probably better to do the offset at runtime, but that
would cost more.
 1.4 16-Mar-2016  christos factor out common code in macro.
 1.3 16-Mar-2016  christos Revert (kind of) the change in 1.12 of the ancient mksyntax.sh script
(undoing the effect of that commit on syntax.h when it was
being dynamically generated) from 1996. This means that the shell
parser is now locale independent, so scripts that work anywhere will
work consistently everywhere. Inspired by a similar change in
FreeBSD's sh (from 2010) - the original change in the other direction
came from FreeBSD as well.... Note that this does not in any way
add any kind of support for locales to sh (which is a whole different
problem.) (from kre)
 1.2 17-Jan-2004  dsl Put a syntax.c under CVS instead of building if with the mksyntax program.
Kill mksyntax.c - no longer possible to get the 'wrong sort of chars'.
/bin/sh now has no helper binaries.
syntax.c uses C99 initialisers, run time initialisation could be used
for systems where the compiler doesn't support them.
I've used some #defines to help make this possible - but writing the code
starts making it rather messy.
 1.1 17-Jan-2004  dsl Put syntax.h under CVS instead of having it generated by mksyntax.
Use CHAR_MIN (from limits.h) to determine whether target char are signed
or unsigned - the syntax tables will not be indexed properly.
Rip out all the stuff from mksyntax.c that wrote syntax.h.
syntax.c can stiff be generated incorrectly...
 1.6.6.1 19-May-2017  pgoyette Resolve conflicts from previous merge (all resulting from $NetBSD
keywork expansion)
 1.7.2.2 10-Sep-2018  martin Pull up following revision(s) via patch (requested by kre in ticket #1015):

bin/sh/expand.c: revision 1.124
bin/sh/expand.c: revision 1.127
bin/sh/parser.c: revision 1.148
bin/sh/parser.c: revision 1.149
bin/sh/syntax.c: revision 1.6
bin/sh/syntax.h: revision 1.9 (partial)

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),

-

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.)
 1.7.2.1 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.9.4.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.9.4.2 21-Apr-2020  martin Sync with HEAD
 1.9.4.1 10-Jun-2019  christos Sync with HEAD
 1.9.2.2 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.9.2.1 26-Nov-2018  pgoyette Sync with HEAD, resolve a couple of conflicts
 1.13.2.1 02-Aug-2025  perseant Sync with HEAD
 1.58 09-Oct-2024  kre PR bin/58687 -- implement suspend as a builtin in sh

Requested by uwe@ in PR bin/58687 without objections from
anyone except me, here is an implementation of a suspend
builtin command for /bin/sh

The sh.1 man page is updated, naturally, to describe it.

This new builtin does not exist in SMALL shells -- as used
on (some) boot media, etc.

If this turns out not to be useful, it can easily be removed.
 1.57 13-Jul-2024  kre Implement the HISTFILE and HISTAPPEND variables.

See the (newly updated) sh(1) for details.
Also add the -z option to fc (clear history).

None of this exists in SMALL shells.
 1.56 10-Nov-2021  kre branches: 1.56.4;

DEBUG mode changes only. NFC (NC) for any normally compiled shell.

Mostly adding DEBUG mode tracing (when appropriate verbose tracing
is enabled generally) whenever a shell (including sushell) process
exits, so shells that the tracing should indicate why ehslls that
vanish did that.

Note for future investigators: if the relevant tracing is enabled,
and a (sub-)shell still simply seems to have vanished without trace,
the likely cause is that it was killed by a signal - and of those,
the most common that occurs is SIGPIPE.
 1.55 20-Aug-2020  kre Be less conservative about when we do clear_traps() when we have
traps_invalid (that is, when we actually nuke the parent shell's
caught traps in a subshell). This allows more reasonable use of
"trap -p" (and similar) in subshells than existed before (and in
particular, that command can be in a function now - there can also
be several related commands like
traps=$(trap -p INT; trap -p QUIT; trap -p HUP)
A side effect of all of this is that
(eval "$(trap -p)"; ...)
now allows copying caught traps into a subshell environment, if desired.

Also att the ksh93 variant (the one not picked by POSIX as it isn't
generally as useful) of "trap -p" (but call it "trap -P" which extracts
just the trap action for named signals (giving more than one is usually
undesirable). This allows
eval "$(trap -P INT)"
to run the action for SIGINT traps, without needing to attempt to parse
the "trap -p" output.
 1.54 20-Aug-2020  kre Whitespace. NFCI.
 1.53 09-Dec-2019  kre PR bin/54743

If a builtin command or function is the final command intended to be
executed, and is interrupted by a caught signal, the trap handler for
that signal was not executed - the shell simply exited (an exit trap
handler would still have been run - if there was one the handler
for the signal may have been invoked during the execution of the
exit trap handler, which, if it happened, is incorrect sequencing).

Now, if we're exiting, and there are pending signals, run their handlers
just before running the EXIT trap handler, if any.

There are almost certainly plenty more issues with traps that need
solving. Later,

XXX pullup -9

(-8 is too different in this area, and this problem suitably obscure,
that we won't bother) (the -7 sh is simply obsolete).
 1.52 25-Apr-2019  kre branches: 1.52.2;
Better interactive SIGINT handling (when a trap is set), and other
cleanups to the trap code. No longer silently ignore attempts to
do anything other than set SIGKILL or SIGSTOP to the default ('-")
state. Don't include those in trap or trap -p output (the former
because they cannot be other than in default state, so simply aren't
included, the latter because it is pointless) but do list them
when requested with trap -p SIG.

Interactive mode SIGINT traps are now run ASAP, rather than after
a command has been entered (so the sequence ^C \n is no longer needed
to generate one). Further, when trapped, in interactive mode,
while waiting for a user command, a SIGINT acts (aside from the
trap being run) just like when not trapped, aborts the command being
entered (rather than leaving it, which it did when libedit was in use)
prints a new prompt, and starts again (which is what should happen.)

Traps other than SIGINT (which has always been handled special in
interactive mode) are unaffected by this change, as are SIGINT traps
in non-interactive shells. Or that is the intent anyway.

Fix an in_dotrap ref count bug (was never being decremented... that
was inserted in a place never executed) (relatively harmless) and
add/improve some trap/signal related DEBUG mode tracing.
 1.51 18-Jan-2019  kre 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.
 1.50 12-Dec-2018  kre 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...
 1.49 05-Dec-2018  kre 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.
 1.48 05-Dec-2018  kre 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@
 1.47 03-Dec-2018  kre 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).
 1.46 28-Oct-2018  kre 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.
 1.45 19-Aug-2018  kre 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)
 1.44 22-Jul-2018  kre 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.
 1.43 22-Jul-2018  kre Revert previous, change has nothing to do with DEBUG mode.
COming again (correctly) in a few seconds.
 1.42 22-Jul-2018  kre DEBUG mode only change (ie: no effect to any normal shell).

Add tracing of pattern matching (aid in debugging various issues.)
 1.41 05-Jul-2017  kre branches: 1.41.4; 1.41.6;

DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
 1.40 07-May-2017  kre branches: 1.40.2;

Enhance the trap command to make it possible to do what POSIX wants
(even if no shell in existence, that I am aware of, does that).

That is, POSIX says ... [of the trap command with no args]

The shell shall format the output, including the proper use of
quoting, so that it is suitable for re-input to the shell as commands
that achieve the same trapping results. For example:

save_traps=$(trap)

...

eval "$save_traps"

It is obvious what the intent is there. But no shell makes it work.

An example using bash (as the NetBSD shell, still does not do the save_traps=
stuff correctly - but that is a problem for a different time and place...)

Given this script

printf 'At start: '; trap
printf '\n'

traps=$(trap)
trap 'echo hello' INT
printf 'inside : '; trap
printf '\n'
eval "${traps}"

printf 'At end : '; trap
printf '\n'

One would expect that (assuming no traps are set at the start, and
there aren't) that the first trap will print nothing, then the inside
trap will show the trap that was set, and then when we get to the
end everything will be back to nothing again.

But:

At start:
inside : trap -- 'echo hello' SIGINT

At end : trap -- 'echo hello' SIGINT

And of course. when you think about it, it is obvious why this happens.
The first "trap" command prints nothing ... nothing has changed when we
get to the "traps=$(trap)" command ... that trap command also prints
nothing. So this does traps=''. When we do eval "${traps}" we are
doing eval "", and it is hardly surprising that this accomplishes nothing!

Now we cannot rationally change the "trap" command without args to
behave in a way that would make it useful for the posix purpose (and
here, what they're aiming for is good, it should be possible to
accomplish that objective) so is there some other way?

I think I have seen some shell (but I do not remember which one) that
actually has "trap -" that resets all traps to the default, so with that,
if we changed the 'eval "${traps}"' line to 'trap -; eval "${traps}"'
then things would actually work - kind of - that version has race conditions,
so is not really safe to use (it will work, most of the time...)

But, both ksh93 and bash have a -p arg to "trap" that allows information
about the current trap status of named signals to be reported. Unfortunately
they don't do quite the same thing, but that's not important right now,
either would be usable, and they are, but it is a lot of effort, not
nearly as simple as the posix example.

First, while "trap -p" (with no signals specified) works, it works just
the same (in both bash and ksh93, aside from output format) as "trap".
That is, that is useless. But we can to

trap_int=$(trap -p int)
trap_hup=$(trap -p hup)
...

and then reset them all, one by one, later...

(bash syntax)
test -n "${trap_int}" && eval "${trap_int}" || trap - int
test -n "${trap_hup}" && eval "${trap_hup}" || trap - hup
(ksh93 syntax)
trap "${trap_int:-}" int
trap "${trap_hup:-}" hup

the test (for bash) and variable with default for ksh93, is needed
because they both still print nothing if the signal action is the default.

So, this modification attempts to fix all of that...

1) we add trap -p, but make it always output something for every signal
listed (all of the signals if none are given) even if the signal
action is the default.

2) choose the bash output format for trap -p, over the ksh93 format,
even though the simpler usage just above makes the ksh93 form seem
better. But it isn't. Consider:

ksh93$ trap -p int hup
echo hello

One of the two traps has "echo hello" as its action, the other is
still at the default, but which?

From bash...
bash$ trap -p int hup
trap -- 'echo hello' SIGINT

And now we know! Given the bash 'trap -p' format, the following function
produces ksh93 format output (for use with named signals only) instead...

ksh93_trap_p() {
for _ARG_ do
_TRAP_=$(trap -p "${_ARG_}") || return 1
eval set -- "${_TRAP_}"
printf '%s' "$3${3:+
}"
done
return 0
}

[ It needs to be entered without the indentation, that '}"' line has to be
at the margin. If the shell running that has local vars (bash does) then
_ARG_ and _TRAP_ should be made local. ]

So the bash format was chosen (except we do not include the "SIG" on the
signal names. That's irrelevant.)

If no traps are set, "trap -p" will say (on NetBSD of course)...

trap -- - EXIT HUP INT QUIT ILL TRAP ABRT EMT FPE KILL BUS SEGV SYS
trap -- - PIPE ALRM TERM URG STOP TSTP CONT CHLD TTIN TTOU IO XCPU XFSZ
trap -- - VTALRM PROF WINCH INFO USR1 USR2 PWR RT0 RT1 RT2 RT3 RT4 RT5
trap -- - RT6 RT7 RT8 RT9 RT10 RT11 RT12 RT13 RT14 RT15 RT16 RT17 RT18
trap -- - RT19 RT20 RT21 RT22 RT23 RT24 RT25 RT26 RT27 RT28 RT29 RT30

Obviously if traps are set, the relevant signal names will be removed from
that list, and additional lines added for the trapped signals.

With args, the signals names are listed, one line each, whatever
the status of the trap for that signal is:

$ trap -p HUP INT QUIT
trap -- - HUP
trap -- 'echo interrupted' INT
trap -- - QUIT

3) we add "trap -" to reset all traps to default. (It is easy, and seems
useful.)

4) While here, lots of generic cleanup. In particular, get rid of the
NSIG+1 nonsense, and anything that ever believes a signo == NSIG
is in any way rational. Before there was a bunch of confusion,
as we need all the signals for traps, plus one more for the EXIT
trap, which looks like we then need NSIG+1. But EXIT is 0, NSIG
includes signals from 0..NSIG-1 but there is no signal 0, EXIT
uses that slot, so we do not need to add and extra one, NSIG is
enough. (To see the effect of this, use a /bin/sh from before
this fix, and compare the output from

trap '' 64
and trap '' 65

both invalid signal numbers.

Then try just "trap" and watch your shell drop core...)

Eventually NSIG needs to go away completely (from user apps), it
is not POSIX, it isn't really useful (unless we make lots of
assumptions about how signals are numbered, which are not guaranteed,
so even if apps, like this sh, work on NetBSD, they're not portable,)
and it isn't necessary (or will not be, soon.)

But that is for another day...

5) As is kind of obvious above, when listing "all" traps, list all the
ones still at their defaults, and all the ignored signals, on as
few lines as possible (it could all be on one line - technically it
would work as well, but it would have made this cvs log message
really ugly...) Signals with a non-null action still get listed
one to a line (even if several do have the exact same action.)

6) Man page updates as well.

After this change, the following script:

printf 'At start: '; trap
printf '\n'

trap -p >/tmp/out.$$
trap 'echo hello' INT
printf 'inside : '; trap
printf '\n'
. /tmp/out.$$; rm /tmp/out.$$

printf 'At end : '; trap
printf '\n'

which is just the example from above,
using "trap -p" instead of just "trap" to save the traps,
and modified to a form that will work with the NetBSD shell today
produces:

At start:
inside : trap -- 'echo hello' INT

At end :

[Do I get a prize for longest commit log message of the year?]
 1.39 29-Apr-2017  kre Fix several problems with the implementation of the "trap" command
(that is, with the command itself, not with the traps that are
executed, if any).

- "trap -- -l" is not rational, permit the (non-std) -l option only
when given as the sole arg (ie: "trap -l").
- "trap --" is the same as just "trap" (and -- is ignored for below)
- "trap action" generates a usage message (there must be at least one condition)
- "trap N [condition...]" (the old form with a numeric first arg, to reset
traps to default, instead of "trap - condition...") is properly detected.
In particular while "trap 1 2 3" resets sighup sigint and siquit handlers
to default, "trap hup int quit" runs the "hup" command on sigint or sigquit
and does nothing to sighup at all.
- actions can start with "-" (as can commands in general) - it may be unusual
or even unwise, but it is not prohibited, and should work
- bad conditions (signal names/numbers) are just a usage error (resulting in
non-zero "exit status" (and a diagnostic on stderr)) they do not cause
the script to abort (as a syntax error in a special builtin would.)
(so says posix, very explicitly.)
- when outputting the trap list ("trap") properly quote null actions
(ignored conditions). This has the side effect of also generating an
explicit null string ('') in other cases where null values are output,
such as when reporting var values ("set") but that's OK, and might be
better (VAR= and VAR='' mean the same, but the latter is more obvious.)

We still do not properly handle traps=$(trap) (ie: it does not work at all,
and should) but that's a different problem that needs fixing in another place.
 1.38 26-Apr-2017  kre Deal with traps that reset the (same) trap in the trap handler
(avoid referrencing memory that might have been freed).
From FreeBSD (ages ago, just not committed until now...)
 1.37 22-Aug-2015  christos branches: 1.37.6;
report the signal that wait was interrupted by, which is not always SIGINT
anymore.
 1.36 22-Aug-2015  christos Process pending signals while waiting for a job:
$ cat << EOF > hup.sh
#!/bin/sh
trap 'echo SIGHUP; exit 1' 1
sleep 10000 &
wait
EOF
$ chmod +x ./hup.sh
$ ./hup.sh &
$ kill -HUP %1
 1.35 18-Jun-2011  christos branches: 1.35.4; 1.35.22;
PR/45069: Henning Petersen: Use prototypes from builtins.h .
 1.34 15-Feb-2008  matt branches: 1.34.24;
Fix inconsistent definitions
 1.33 15-Jul-2005  christos branches: 1.33.10;
Allow trap to work on ignored signals when the shell is interactive.
 1.32 11-Jul-2005  christos Don't hard ignore signals that were ignored by our environment, because
when we try to set a trap on them it will not work. Also while I am here:
1. don't change the action status if the signal system call failed.
2. don't try to sigignore it if signal failed.
3. clear the signal mask in case our parent blocked it for us.
 1.31 11-Jan-2005  christos PR/28940: David Laight: /bin/sh doesn't quote the output of trap.
 1.30 26-Aug-2003  jmmv Use '\0' instead of NULL in two checks (we are not checking for a pointer
value). While here, add a missing whitespace.
 1.29 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.28 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.27 27-Sep-2002  christos VFork()ing shell: From elric@netbsd.org:
Plus my changes:
- walking process group fix in foregrounding a job.
- reset of process group in parent shell if interrupted before the wait.
- move INTON lower in the dowait so that the job structure is
consistent.
- error check all setpgid(), tcsetpgrp() calls.
- eliminate unneeded strpgid() call.
- check that we don't belong in the process group before we try to
set it.
 1.26 18-Mar-2001  wulf branches: 1.26.2;
Extended functionality of the trap builtin, which now closely follows
POSIX recommendations.

- trap now accepts signal names and signal numbers
e.g. INT, SIGINT, 2
- added option -l that outputs a list of valid signals
- added signal EXIT to list of valid signals
- a `-' in the action part will reset specified signal to their
default behaviour
- changed standard output format to make it suitable as an input
to another shell that achieves the same trapping results
 1.25 04-Feb-2001  christos remove redundant declarations and nexted externs.
 1.24 22-May-2000  elric branches: 1.24.4;
Back out previous vfork changes.
 1.23 13-May-2000  elric Now we use vfork(2) instead of fork(2) when we can.
 1.22 27-Jan-2000  christos Fix bin/9184, bin/9194, bin/9265, bin/9266
Exitcode and negation problems (From Martin Husemann)
 1.21 27-Mar-1999  christos When we execute commands from a shell script, make sure that the signals
are being caught (reported by Alexis Rosen), similar to the -c case.

#!/bin/sh
vi "$@"

^C when the script is running...
 1.20 05-Feb-1999  christos Fix the -c problem differently. We cannot just ignore SIGINT etc, otherwise
we cannot interrupt sh -c <command>
 1.19 18-Jan-1999  christos PR/6213: Urban Boquist: /bin/sh does not handle a trapped signal correctly
The problem was that system calls got restarted after a signal,
instead of returning EINTR. Thus the read builtin, had no way to
know that a signal occured that could change the course of execution.
Since the code has sprinkled checks for EINTR all over the place,
it is supposed to work properly with non restartable syscalls.
The fix is to use siginterrupt(signo, 1), before setting a signal
handler, to make sure that system calls don't get restarted.
 1.18 28-Jul-1998  mycroft Delint.
 1.17 04-Jul-1997  christos Fix compiler warnings.
 1.16 16-Oct-1996  christos PR/2808: Remove trailing whitespace (from FreeBSD)
 1.15 07-Jun-1995  christos branches: 1.15.6;
Ignore result of sigaction when setting traps. Traps will succeed even
on SIGKILL or SIGSTOP. This is what other bourne shells do. (suggested
by mycroft)
 1.14 05-Jun-1995  christos Avoid trapping SIGKILL. Pretend that we did, so that we will not keep
failing trying to trap it later. This is what the other bourne shells do.
 1.13 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.12 21-Mar-1995  cgd convert to new RCS id conventions.
 1.11 23-Dec-1994  cgd be more careful with casts.
 1.10 05-Dec-1994  cgd clean up further. more patches from Jim Jegers
 1.9 11-Jun-1994  mycroft Add RCS ids.
 1.8 12-May-1994  jtc last sys_signame[] changes; shell can now be built from scratch
 1.7 12-May-1994  jtc Include appropriate header files to bring function prototypes into scope.
 1.6 11-May-1994  jtc sync with 4.4lite
 1.5 06-Aug-1993  mycroft Use sys_signame[].
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.15.6.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.24.4.3 23-Feb-2002  he Pull up revisions 1.25-1.26 (requested by jonb):
Extend functionality of the trap builtin, which now more closely
follows POSIX recommendations:
o accept signal names as well as signal numbers
o add ``-l'' option which outputs list of valid signals
o add signal EXIT to list of valid signals
o an ``-'' in the action part will reset signal to default behaviour
o changed standard output of ``trap'' to make it suitable as
subsequent input
Also various cleanups of redundant declarations and nested externs.
 1.24.4.2 18-Mar-2001  wulf Reversed submission of trap.c and sh.1 to wrong branch
 1.24.4.1 17-Mar-2001  wulf Extended functionality of the trap builtin, which now closely follows
POSIX recommendations.

- trap now accepts signal names and signal numbers
e.g. INT, SIGINT, 2
- added option -l that outputs a list of valid signals
- added signal EXIT to list of valid signals
- a `-' in the action part will reset specified signal to their
default behaviour
- changed standard output format to make it suitable as an input
to another shell that achieves the same trapping results
 1.26.2.1 27-Mar-2002  elric Doing the vfork work on ash on a branch to try to shake out the
problems before I expose everyone to them. This checkin represents
a merge of the prior work, which I backed out a while ago, to the
HEAD only and does not incorporate any additional bugfixes. The
additional bugfixes and code-cleanup will occur in later checkins.

For reference the patches that were used are:
cvs diff -kk -r1.51 -r1.55 eval.c | patch
cvs diff -kk -r1.27 -r1.28 exec.c | patch
cvs diff -kk -r1.15 -r1.16 exec.h | patch
cvs diff -kk -r1.32 -r1.33 input.c | patch
cvs diff -kk -r1.10 -r1.11 input.h | patch
cvs diff -kk -r1.32 -r1.35 jobs.c | patch
cvs diff -kk -r1.9 -r1.11 jobs.h | patch
cvs diff -kk -r1.36 -r1.37 main.c | patch
cvs diff -kk -r1.20 -r1.21 redir.c | patch
cvs diff -kk -r1.10 -r1.11 redir.h | patch
cvs diff -kk -r1.10 -r1.12 shell.h | patch
cvs diff -kk -r1.22 -r1.23 trap.c | patch
cvs diff -kk -r1.12 -r1.13 trap.h | patch
cvs diff -kk -r1.23 -r1.24 var.c | patch
cvs diff -kk -r1.16 -r1.17 var.h | patch

All other changes were simply the resolution of the resulting
conflicts, which occured only in the merge of jobs.c.

Begins to address PR: bin/5475
 1.33.10.1 23-Mar-2008  matt sync with HEAD
 1.34.24.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.35.22.1 04-Nov-2015  riz Pull up following revision(s) (requested by christos in ticket #964):
bin/sh/jobs.c: revision 1.74
bin/sh/jobs.c: revision 1.75
bin/sh/trap.c: revision 1.36
bin/sh/trap.c: revision 1.37
bin/sh/trap.h: revision 1.21
bin/sh/trap.h: revision 1.22
Process pending signals while waiting for a job:
$ cat << EOF > hup.sh
#!/bin/sh
trap 'echo SIGHUP; exit 1' 1
sleep 10000 &
wait
EOF
$ chmod +x ./hup.sh
$ ./hup.sh &
$ kill -HUP %1
report the signal that wait was interrupted by, which is not always SIGINT
anymore.
 1.35.4.1 15-Nov-2015  bouyer Pull up following revision(s) (requested by christos in ticket #1323):
bin/sh/jobs.c: revision 1.74
bin/sh/jobs.c: revision 1.75
bin/sh/trap.c: revision 1.36
bin/sh/trap.c: revision 1.37
bin/sh/trap.h: revision 1.21
bin/sh/trap.h: revision 1.22
Process pending signals while waiting for a job:
$ cat << EOF > hup.sh
#!/bin/sh
trap 'echo SIGHUP; exit 1' 1
sleep 10000 &
wait
EOF
$ chmod +x ./hup.sh
$ ./hup.sh &
$ kill -HUP %1
report the signal that wait was interrupted by, which is not always SIGINT
anymore.
 1.37.6.2 11-May-2017  pgoyette Sync with HEAD
 1.37.6.1 02-May-2017  pgoyette Sync with HEAD - tag prg-localcount2-base1
 1.40.2.2 25-Aug-2018  martin Pull up following revision(s) (requested by kre in ticket #987):

bin/sh/trap.c: revision 1.44

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.
 1.40.2.1 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.41.6.4 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.41.6.3 21-Apr-2020  martin Sync with HEAD
 1.41.6.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.41.6.1 10-Jun-2019  christos Sync with HEAD
 1.41.4.5 18-Jan-2019  pgoyette Synch with HEAD
 1.41.4.4 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.41.4.3 26-Nov-2018  pgoyette Sync with HEAD, resolve a couple of conflicts
 1.41.4.2 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.41.4.1 28-Jul-2018  pgoyette Sync with HEAD
 1.52.2.1 11-Dec-2019  martin Pull up following revision(s) (requested by kre in ticket #542):

bin/sh/eval.c: revision 1.176
bin/sh/trap.c: revision 1.53

PR bin/54743

Having traps set should not enforce a fork for the next command,
whatever that command happens to be, only for commands which would
normally fork if they weren't the last command expected to be
executed (ie: builtins and functions shouldn't be exexuted in a
sub-shell merely because a trap is set).

As it was (for example)
trap 'whatever' SIGANY; wait $anypid
was guaranteed to fail the wait, as the subshell it was executed
in could not have any children.

XXX pullup -9

PR bin/54743

If a builtin command or function is the final command intended to be
executed, and is interrupted by a caught signal, the trap handler for
that signal was not executed - the shell simply exited (an exit trap
handler would still have been run - if there was one the handler
for the signal may have been invoked during the execution of the
exit trap handler, which, if it happened, is incorrect sequencing).

Now, if we're exiting, and there are pending signals, run their handlers
just before running the EXIT trap handler, if any.
There are almost certainly plenty more issues with traps that need
solving. Later,

XXX pullup -9
(-8 is too different in this area, and this problem suitably obscure,
that we won't bother) (the -7 sh is simply obsolete).
 1.56.4.1 02-Aug-2025  perseant Sync with HEAD
 1.26 09-Oct-2024  kre PR bin/58687 -- implement suspend as a builtin in sh

Requested by uwe@ in PR bin/58687 without objections from
anyone except me, here is an implementation of a suspend
builtin command for /bin/sh

The sh.1 man page is updated, naturally, to describe it.

This new builtin does not exist in SMALL shells -- as used
on (some) boot media, etc.

If this turns out not to be useful, it can easily be removed.
 1.25 03-Dec-2018  martin branches: 1.25.12;
Make pendingsigs forward declaration match the definition.
 1.24 03-Dec-2018  kre 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).
 1.23 19-Aug-2018  kre 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)
 1.22 22-Aug-2015  christos branches: 1.22.14; 1.22.16;
report the signal that wait was interrupted by, which is not always SIGINT
anymore.
 1.21 22-Aug-2015  christos Process pending signals while waiting for a job:
$ cat << EOF > hup.sh
#!/bin/sh
trap 'echo SIGHUP; exit 1' 1
sleep 10000 &
wait
EOF
$ chmod +x ./hup.sh
$ ./hup.sh &
$ kill -HUP %1
 1.20 15-Mar-2012  joerg branches: 1.20.12;
Add __printflike attribution to use vprintf and friends with an argument
as format string.
 1.19 18-Jun-2011  christos branches: 1.19.2; 1.19.4;
PR/45069: Henning Petersen: Use prototypes from builtins.h .
 1.18 11-Jul-2005  christos branches: 1.18.40;
make setsig() return sig_t
 1.17 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.16 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.15 27-Sep-2002  christos VFork()ing shell: From elric@netbsd.org:
Plus my changes:
- walking process group fix in foregrounding a job.
- reset of process group in parent shell if interrupted before the wait.
- move INTON lower in the dowait so that the job structure is
consistent.
- error check all setpgid(), tcsetpgrp() calls.
- eliminate unneeded strpgid() call.
- check that we don't belong in the process group before we try to
set it.
 1.14 22-May-2000  elric branches: 1.14.6;
Back out previous vfork changes.
 1.13 13-May-2000  elric Now we use vfork(2) instead of fork(2) when we can.
 1.12 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.11 16-Oct-1996  christos PR/2808: Remove trailing whitespace (from FreeBSD)
 1.10 07-Jun-1995  christos branches: 1.10.6;
Ignore result of sigaction when setting traps. Traps will succeed even
on SIGKILL or SIGSTOP. This is what other bourne shells do. (suggested
by mycroft)
 1.9 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.8 21-Mar-1995  cgd convert to new RCS id conventions.
 1.7 23-Dec-1994  cgd be more careful with casts.
 1.6 11-Jun-1994  mycroft Add RCS ids.
 1.5 11-May-1994  jtc sync with 4.4lite
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.10.6.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.14.6.1 27-Mar-2002  elric Doing the vfork work on ash on a branch to try to shake out the
problems before I expose everyone to them. This checkin represents
a merge of the prior work, which I backed out a while ago, to the
HEAD only and does not incorporate any additional bugfixes. The
additional bugfixes and code-cleanup will occur in later checkins.

For reference the patches that were used are:
cvs diff -kk -r1.51 -r1.55 eval.c | patch
cvs diff -kk -r1.27 -r1.28 exec.c | patch
cvs diff -kk -r1.15 -r1.16 exec.h | patch
cvs diff -kk -r1.32 -r1.33 input.c | patch
cvs diff -kk -r1.10 -r1.11 input.h | patch
cvs diff -kk -r1.32 -r1.35 jobs.c | patch
cvs diff -kk -r1.9 -r1.11 jobs.h | patch
cvs diff -kk -r1.36 -r1.37 main.c | patch
cvs diff -kk -r1.20 -r1.21 redir.c | patch
cvs diff -kk -r1.10 -r1.11 redir.h | patch
cvs diff -kk -r1.10 -r1.12 shell.h | patch
cvs diff -kk -r1.22 -r1.23 trap.c | patch
cvs diff -kk -r1.12 -r1.13 trap.h | patch
cvs diff -kk -r1.23 -r1.24 var.c | patch
cvs diff -kk -r1.16 -r1.17 var.h | patch

All other changes were simply the resolution of the resulting
conflicts, which occured only in the merge of jobs.c.

Begins to address PR: bin/5475
 1.18.40.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.19.4.1 15-Nov-2015  bouyer Pull up following revision(s) (requested by christos in ticket #1323):
bin/sh/jobs.c: revision 1.74
bin/sh/jobs.c: revision 1.75
bin/sh/trap.c: revision 1.36
bin/sh/trap.c: revision 1.37
bin/sh/trap.h: revision 1.21
bin/sh/trap.h: revision 1.22
Process pending signals while waiting for a job:
$ cat << EOF > hup.sh
#!/bin/sh
trap 'echo SIGHUP; exit 1' 1
sleep 10000 &
wait
EOF
$ chmod +x ./hup.sh
$ ./hup.sh &
$ kill -HUP %1
report the signal that wait was interrupted by, which is not always SIGINT
anymore.
 1.19.2.1 17-Apr-2012  yamt sync with head
 1.20.12.1 04-Nov-2015  riz Pull up following revision(s) (requested by christos in ticket #964):
bin/sh/jobs.c: revision 1.74
bin/sh/jobs.c: revision 1.75
bin/sh/trap.c: revision 1.36
bin/sh/trap.c: revision 1.37
bin/sh/trap.h: revision 1.21
bin/sh/trap.h: revision 1.22
Process pending signals while waiting for a job:
$ cat << EOF > hup.sh
#!/bin/sh
trap 'echo SIGHUP; exit 1' 1
sleep 10000 &
wait
EOF
$ chmod +x ./hup.sh
$ ./hup.sh &
$ kill -HUP %1
report the signal that wait was interrupted by, which is not always SIGINT
anymore.
 1.22.16.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.22.16.2 21-Apr-2020  martin Sync with HEAD
 1.22.16.1 10-Jun-2019  christos Sync with HEAD
 1.22.14.2 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.22.14.1 06-Sep-2018  pgoyette Sync with HEAD

Resolve a couple of conflicts (result of the uimin/uimax changes)
 1.25.12.1 02-Aug-2025  perseant Sync with HEAD
 1.4 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.3 11-Apr-1995  christos Added missing resource limits and recognize "unlimited" as RLIM_INFINITY
 1.2 21-Mar-1995  cgd convert to new RCS id conventions.
 1.1 04-Nov-1994  jtc ulimit builtin (PR #388)

This public domain code, originally by Doug Gwyn, Doug Kingston, Eric
Gisin, and Michael Rendell was ripped from pdksh 5.0.8 and hacked for
use with ash.
 1.88 26-Dec-2024  kre Fix a bug from when the ToD variable was added (July 2017) where if
TZ is unset, and ToD_FORMAT contains and strftime() conversions which
need to know the zone, bad things happen.

Amazing that no-one (incl me) ever noticed this.

XXX pullup -9, pullup -10 (and -8 from 8.0_RC1 onwards ... sigh)
 1.87 21-Oct-2024  kre If searching for a variable with no name, bail out faster,
that can never succeed. NFCI.
 1.86 14-Oct-2024  kre Apologies for that commit message ... it should have been the same
as for revision 1.124 of Makefile (and as below).

(This change changes nothing).

Reject nul characters in shell input.

At the request of Thomas Klausner (wiz@) copy an idea from OpenBSD,
and have the shell simply reject any (sh) input containing a \0 (nul)
character. Previously nul characters were simply ignored (removed
from the input before it was examined in any other way).

Note this affects data read by the shell itself only, and has no
impact on other utilities, including those that are built into
the shell (so 'read' can still use -d '' to process output using
\0 as the record separator).

While I have tested that this works, there are so many places
where a nul might appear, that I cannot possibly test them all
(or even imagine all the possible places), so if this change
causes any problems, let me know (if from a script, send me the
script).

To undo this change, simply comment out (or delete) the line
CPPFLAGS+= -DREJECT_NULS
from the Makefile, and build again. Eventually if this causes
no problems, that option (conditional compilation) will probably
just go away, and this change would be permanent.

While the conditional compilation (on or off) persists, the
NETBSD_SHELL variable value will contain the word REJECT_NULLS
so you can easily verify if you are running a shell with this
included or not.

No pullups planned.
 1.85 14-Oct-2024  kre # $NetBSD: Makefile,v 1.123 2023/10/19 04:27:24 mrg Exp $
# @(#)Makefile 8.4 (Berkeley) 5/5/95

.include <bsd.own.mk>

PROG= sh
SHSRCS= alias.c arith_token.c arithmetic.c cd.c echo.c error.c eval.c exec.c \
expand.c histedit.c input.c jobs.c mail.c main.c memalloc.c \
miscbltin.c mystring.c options.c parser.c redir.c show.c trap.c \
output.c var.c test.c kill.c syntax.c
GENSRCS=builtins.c init.c nodes.c
GENHDRS=builtins.h nodes.h token.h nodenames.h optinit.h
SRCS= ${SHSRCS} ${GENSRCS}

DPSRCS+=${GENHDRS}

LDADD+= -ledit -lterminfo
DPADD+= ${LIBEDIT} ${LIBTERMINFO}

# Environment for scripts executed during build.
SCRIPT_ENV= \
AWK=${TOOL_AWK:Q} \
MKTEMP=${TOOL_MKTEMP:Q} \
SED=${TOOL_SED:Q}

CPPFLAGS+=-DSHELL -I. -I${.CURDIR} -I${NETBSDSRCDIR}/lib/libedit
CPPFLAGS+= -DUSE_LRAND48
CPPFLAGS+= -DREJECT_NULS

#XXX: For testing only.
#CPPFLAGS+=-DDEBUG=1
#COPTS+=-g
#CFLAGS+=-funsigned-char
#TARGET_CHARFLAG?= -DTARGET_CHAR="unsigned char" -funsigned-char

# Reproducible build parameters ... export into sh for NETBSD_SHELL setting
.if ${MKREPRO_TIMESTAMP:Uno} != "no"
BUILD_DATE!= ${TOOL_DATE} -u -r "${MKREPRO_TIMESTAMP}" "+%Y%m%d%H%M%S"
# These are (should be) equivalent, but the 2nd is easier to understand
#CPPFLAGS+= -DBUILD_DATE='"${BUILD_DATE:C/([^0]0?)(00)*$/\1/}Z"'
CPPFLAGS+= -DBUILD_DATE='"${BUILD_DATE:S/00$//:S/00$//:S/00$//}Z"'
.endif

.ifdef SMALLPROG
CPPFLAGS+=-DSMALL
.endif
.ifdef TINYPROG
CPPFLAGS+=-DTINY
.else
SRCS+=printf.c
.endif

.PATH: ${.CURDIR}/bltin ${NETBSDSRCDIR}/bin/test \
${NETBSDSRCDIR}/usr.bin/printf \
${NETBSDSRCDIR}/bin/kill

CLEANFILES+= ${GENSRCS} ${GENHDRS} sh.html1
CLEANFILES+= trace.*

token.h: mktokens
${_MKTARGET_CREATE}
${SCRIPT_ENV} ${HOST_SH} ${.ALLSRC}

.ORDER: builtins.h builtins.c
builtins.h builtins.c: mkbuiltins shell.h builtins.def
${_MKTARGET_CREATE}
${SCRIPT_ENV} ${HOST_SH} ${.ALLSRC} ${.OBJDIR}
[ -f builtins.h ]

init.c: mkinit.sh ${SHSRCS}
${_MKTARGET_CREATE}
${SCRIPT_ENV} ${HOST_SH} ${.ALLSRC}

.ORDER: nodes.h nodes.c
nodes.c nodes.h: mknodes.sh nodetypes nodes.c.pat
${_MKTARGET_CREATE}
${SCRIPT_ENV} ${HOST_SH} ${.ALLSRC} ${.OBJDIR}
[ -f nodes.h ]

nodenames.h: mknodenames.sh nodes.h
${_MKTARGET_CREATE}
${SCRIPT_ENV} ${HOST_SH} ${.ALLSRC} > ${.TARGET}

optinit.h: mkoptions.sh option.list
${_MKTARGET_CREATE}
${SCRIPT_ENV} ${HOST_SH} ${.ALLSRC} ${.TARGET} ${.OBJDIR}

.if ${USETOOLS} == "yes"
NBCOMPATLIB= -L${TOOLDIR}/lib -lnbcompat
.endif

SUBDIR.roff+=USD.doc

COPTS.printf.c = -Wno-format-nonliteral
COPTS.jobs.c = -Wno-format-nonliteral
COPTS.var.c = -Wno-format-nonliteral

# XXXGCC12 - only on some targets
COPTS.parser.c+= ${${ACTIVE_CC} == "gcc" && ${HAVE_GCC:U0} >= 12:? -Wno-error=clobbered :}

.include <bsd.prog.mk>
.include <bsd.subdir.mk>

${OBJS}: Makefile
 1.84 13-Jul-2024  kre Implement the HISTFILE and HISTAPPEND variables.

See the (newly updated) sh(1) for details.
Also add the -z option to fc (clear history).

None of this exists in SMALL shells.
 1.83 12-Jul-2024  kre Improve safety in var imports from the environment.

Add a new var flag VUNSAFE - set on all vars imported from the environment.

Add setvareqsafe() (which is to setvareq() as setvarsafe() is to setvar())
and use that instead of setvareq() when processing the environment, so
errors don't cause the shell to abort. Use VUNSAFE in that call.

Add flags arguments to all var callback functions which are used when setting
variables, and pass the flags given to the setvar*() functions to those
functions, so they can act differently in different situations (if desired).
Most of them just ignore the flags.

When unsetting a variable, call setvar() to clear things (and call the
callback function) both when the variable had a value which needs to be freed,
and when unsetting a variable which wasn't unset previously, so the VUNSET
flag can be seen by that callback func.

When setting HISTSIZE, use the flags passed to determine whether to ignore
bad values (if VUNSAFE) or treat them as an error. This replaces the
earlier temporary hack to always ignore bad data there (histedit.c 1.68).

Miscellaneous associated minor changes.

These changes should largely be invisible in normal use.
 1.82 18-Sep-2022  kre branches: 1.82.2; 1.82.4;

Oops, somehow managed to commit an older version where NBSH_INVOCATION
start char was '@' rather than '!' (which meant not lexically ordered).
This is how it was intended to be (and is documented).
 1.81 18-Sep-2022  kre Add the -l option (aka -o login): be a login shell. Meaningful only on
the command line (with both - and + forms) - overrides the presence (or
otherwise) of a '-' as argv[0][0].

Since this allows any shell to be a login shell (which simply means that
it runs /etc/profile and ~/.profile at shell startup - there are no other
side effects) add a new, always set at startup, variable NBSH_INVOCATION
which has a char string as its value, where each char has a meaning,
more or less related to how the shell was started. See sh(1).
This is intended to allow those startup scripts to tailor their behaviour
to the nature of this particular login shell (it is possible to detect
whether a shell is a login shell merely because of -l, or whether it would
have been anyway, before the -l option was added - and more). The
var could also be used to set different values for $ENV for different
uses of the shell.
 1.80 09-Aug-2021  kre Fix the fix to a typo in one of the comments.
 1.79 08-Aug-2021  andvar s/varable/variable s/explictly/explicitly/ s/proerly/properly/ in comments.
 1.78 14-Feb-2019  kre branches: 1.78.2;

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.
 1.77 09-Feb-2019  kre 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.
 1.76 09-Feb-2019  kre 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.
 1.75 21-Jan-2019  kre 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.
 1.74 12-Dec-2018  kre 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).
 1.73 12-Dec-2018  kre 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.
 1.72 04-Dec-2018  kre 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
 1.71 03-Dec-2018  kre 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()
 1.70 13-Jul-2018  kre 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.)
 1.69 19-Nov-2017  kre branches: 1.69.2; 1.69.4;
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.
 1.68 28-Oct-2017  kre Extract the variable name validity test from setname() into a
function of its own. It will soon be needed from another source.
 1.67 31-Aug-2017  kre 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...
 1.66 24-Jul-2017  kre NFC: DEBUG mode only change - add a little more tracing.
 1.65 12-Jul-2017  kre 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.
 1.64 05-Jul-2017  kre DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
 1.63 30-Jun-2017  kre 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.))
 1.62 28-Jun-2017  kre Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
 1.61 27-Jun-2017  kre Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
 1.60 17-Jun-2017  kre Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).

Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)

From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
 1.59 17-Jun-2017  kre s/volatile/const/ -- wonderful how opposites attract like this.
 1.58 07-Jun-2017  kre Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)

While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
 1.57 07-Jun-2017  kre A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)

This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)

The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.

This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).

I would not call the results of this perfect yet, but it is close.
 1.56 07-Jun-2017  kre An initial attempt at implementing LINENO to meet the specs.

Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.

Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)

This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.

POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)

This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.

This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).

Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
 1.55 27-May-2017  kre branches: 1.55.2;
More standard (and saner) implementation of the ! reserved word.
Unless the shell is compiled with the (compilation time) option
BOGUS_NOT_COMMAND (as in CFLAGS+=-DBOGUS_NOT_COMMAND) which it
will not normally be, the ! command (reserved word) will only
be permitted at the start of a pipeline (which includes the
degenerate pipeline with no '|'s in it of course - ie: a simple cmd)
and not in the middle of a pipeline sequence (no "cmd | ! cmd" nonsense.)
If the latter is really required, then "cmd | { ! cmd; }" works as
a standard equivalent.

In POSIX mode, permit only one ! ("! pipeline" is ok. "! ! pipeline" is not).
Again, if needed (and POSIX conformance is wanted) "! { ! pipeline; }"
works as an alternative - and is safer, some shells treat "! ! cmd" as
being identical to "cmd" (this one did until recently.)
 1.54 27-May-2017  kre It turns out that most shells do not do variable value/attribute
inheritance when a variable is declared local, but instead leave
the local var unset (if not given a value) in the function.
Only ash derived shells do inheritance it seems.

So, to compensate for that, and get one step closer to making
"local" part of POSIX, so we can really rely upon it, a compromise
has been suggested, where "local x" is implementation defined
when it comes to this issue, and we add "local -I x" to specify
inheritance, and "local -N x" to specify "not" (something...)
(not inherited, or not set, or whatever you prefer to imagine!)
The option names took a lot of hunting to find something reasonable
that no shell (we know of) had already used for some other purpose...
The I was easy, but 'u' 'U' 'X' ... all in use somewhere.

This implements that (well, semi-) agreement.

While here, add "local -x" (which many other shells already have)
which causes the local variable to be made exported. Not a lot
of gain in that (since "export x" can always be done immediately
after "local x") but it is very cheap to add and allows more other
scripts to work with out shell.

Note that while 'local x="${x}"' always works to specify inheritance
(while making the shell work harder), "local x; unset x" does not
always work to specify the alternative, as some shells have
"re-interpreted" unset of a local variable to mean something that
would best be described as "unlocal" instead - ie: after the unset
you might be back with the variable from the outer scope, rather
than with an unset local variable.

Also add "unset -x" to allow unsetting a variable without removing
any exported status it has.

There are gazillions of other options that are not supported here!
 1.53 14-May-2017  kre Make ${NETBSD_SHELL} value include (a human recognisable form of)
MKREPRO_TIMESTAMP (as an additional word in the value, with a "BUILD:" prefix)
if it is set during the build. (Trailing 00 pairs in the time are removed).

While here, throw in some extra words that list the compilation
options used which alter sh behaviour (mostly by removing stuff.)
Usually that will only be noticed in a SMALL shell compiled for
install media, or similar - none of the others (not that there
are many) are ever changed from the default in a normal build
(default settings are just omitted.) This also allows scripts
to tell if they are running in a DEBUG shell, which can sometimes
make debugging easier.
 1.52 10-May-2017  kre I noticed!

POSIX requires that the output of the "set" command (with no args -- it
gives a list of variables, and their values) be sorted according to
the collating sequence defined by the current locale.

Now I'm not aware of any locale where the collating sequence order of
ascii letters, digits, and '_' are any different than they are in the
C locale (and those are the only characters that can occur in variable
names - unless there is perhaps a locale that defines "dictionary" order
as the sort order) but never mind, that isn't the bug...

What "collating sequence order" does mean however, if not "collating
sequence order, except when we happen to have two variable names, where
one name is a prefix of the other (say X and XY) and the first character
of the 'Y' part of the longer name happens to be a digit..."

"set" is not a frequently used command (particularly in scripts where
it matters - that is, the no args form, nothing here alters anything
about any use of set with args) and is already a bit slow (sluggish...)
because of the sort requirement, so let's make it fractionally even
slower, but correct.
 1.51 03-May-2017  kre Make "export VAR" work correctly ... if VAR was unset before this
command it should remain unset afterwards.

Previouly "export VAR" did much the same as:
export VAR="${VAR}"
(but without the side effects if VAR had previously been VAR='~' or similar)

Also stop unset exported variables from actually making it into the
environment. Previously this was impossible - variables get exported
in just one of 3 ways, by being imported from the environ (which means
the var is set) when -a is set, and a var is given a value (so the var
is set), or using "export" which previously always set a null string
if the var was otheriwse unset.

The same happens for "readonly" (readonly and export use the same mechanism)
- except, once marked readonly, it is no longer possible to set the var, so
(assuming VAR is not already readonly)
unset VAR; readonly VAR
is (now) a way to guarantee that "VAR" can never be set.

This conforms with POISX (though it is not particularly clear on this
point) and with bash and ksh93 (and also with the FreeBSD shell, though
they export unset variables that are marked for export as if set to '')

It s not clear whether
unset VAR; readonly VAR; unset VAR; echo $?
should print 0, or non-0, so for now just leave this as it is (prints 1).
 1.50 29-Apr-2017  kre Fix several problems with the implementation of the "trap" command
(that is, with the command itself, not with the traps that are
executed, if any).

- "trap -- -l" is not rational, permit the (non-std) -l option only
when given as the sole arg (ie: "trap -l").
- "trap --" is the same as just "trap" (and -- is ignored for below)
- "trap action" generates a usage message (there must be at least one condition)
- "trap N [condition...]" (the old form with a numeric first arg, to reset
traps to default, instead of "trap - condition...") is properly detected.
In particular while "trap 1 2 3" resets sighup sigint and siquit handlers
to default, "trap hup int quit" runs the "hup" command on sigint or sigquit
and does nothing to sighup at all.
- actions can start with "-" (as can commands in general) - it may be unusual
or even unwise, but it is not prohibited, and should work
- bad conditions (signal names/numbers) are just a usage error (resulting in
non-zero "exit status" (and a diagnostic on stderr)) they do not cause
the script to abort (as a syntax error in a special builtin would.)
(so says posix, very explicitly.)
- when outputting the trap list ("trap") properly quote null actions
(ignored conditions). This has the side effect of also generating an
explicit null string ('') in other cases where null values are output,
such as when reporting var values ("set") but that's OK, and might be
better (VAR= and VAR='' mean the same, but the latter is more obvious.)

We still do not properly handle traps=$(trap) (ie: it does not work at all,
and should) but that's a different problem that needs fixing in another place.
 1.49 31-Mar-2016  christos branches: 1.49.6;
Implement the NETBSD_SHELL readonly unexportable unimportable
variable (with its current value set at 20160401) as discussed on
current-users and tech-userlevel. This also includes the necessary
support to implement it properly (particularly the unexportable
part) and adds options to the export command to support unexportable
variables. Also implement the "posix" option (no single letter
equivalent) which gets its default value from whether or not
POSIXLY_CORRECT is set in the environment when the shell starts
(but can be changed just like any other option using -o and +o on
the command line, or the set builtin command.) While there, fix
all uses of options so it is possible to have options that have a
short (one char) name, and no long name, just as it has been possible
to have options with a long name and no short name, though there
are currently none (with no long name). For now, the only use of
the posix option is to control whether ${ENV} is read at startup
by a non-interactive shell, so changing it with set is not usful
- that might change in the future. (from kre@)
 1.48 27-Mar-2016  christos General KNF and source code cleanups, avoid scattering the
magic string " \t\n" all over the place, slightly improved
syntax error messages, restructured some of the code for
clarity, don't allow IFS to be imported through the environment,
and remove the (never) conditionally compiled ATTY option.
Apart from one or two syntax error messages, and ignoring IFS
if present in the environment, this is intended to have no
user visible changes. (from kre@)
 1.47 08-Mar-2016  christos Move the PPID installation to the init() section.
 1.46 08-Mar-2016  christos - don't export $PPID (from kre)
- include <stdio.h>
 1.45 08-Mar-2016  christos Provide $PPID, kill vvers (unused)
 1.44 26-May-2015  christos Drop privileges when executed set{u,g}id unless -p is specified like other
shells do to avoid system() and popen() abuse.
 1.43 01-Nov-2013  christos PR/48312: Dieter Roelands: According to TOG, unset should not return an error
for functions are variables that were not previously set:
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
 1.42 13-Dec-2012  christos PR/47317: Henning Petersen: Replace index() with strchr()
 1.41 28-Mar-2012  christos branches: 1.41.2;
include <limits.h> for CHAR_MIN/CHAR_MAX
 1.40 18-Jun-2011  christos branches: 1.40.2; 1.40.4;
PR/45069: Henning Petersen: Use prototypes from builtins.h .
 1.39 16-Oct-2008  dholland branches: 1.39.16;
Use "extern" properly for referencing globals defined in other modules.
Now builds cleanly with -warn-common.
 1.38 18-Dec-2006  christos fix a volatile variable; from Anon Ymous
 1.37 24-Apr-2006  snj It's "its."
 1.36 06-Oct-2004  enami Fix a bug introduced by previous commit. It breaks export command
with multiple arguments if one of them are already set. Fix PR#27155
and probably PR#27143.
 1.35 02-Oct-2004  dsl Save the length of each variable in the name table so that we can
compare the lengths and then use memcmp() in the search code.
Speeds up one of my scripts by a facter of 2.
Increase the size of the variable hash table.
Cuts down time for script to execute from 60 seconds to 10.
Move variable search into a new function to hide the implementation
from most of the code, new version is slightly smaller than old.
 1.34 26-Aug-2003  jmmv Include strings.h, needed for index's prototype.
 1.33 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.32 22-Jan-2003  dsl Support command -p, -v and -V as posix
Stop temporary PATH assigments messing up hash table
Fix sh -c -e "echo $0 $*" -a x (as posix)
(agreed by christos)
 1.31 25-Nov-2002  agc Make this compile on some of the more esoteric architectures (e.g. those
which are not i386)
 1.30 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.29 27-Sep-2002  christos VFork()ing shell: From elric@netbsd.org:
Plus my changes:
- walking process group fix in foregrounding a job.
- reset of process group in parent shell if interrupted before the wait.
- move INTON lower in the dowait so that the job structure is
consistent.
- error check all setpgid(), tcsetpgrp() calls.
- eliminate unneeded strpgid() call.
- check that we don't belong in the process group before we try to
set it.
 1.28 15-May-2002  bjh21 Implement sh -a (allexport). Code (all two lines of it) from FreeBSD
(FreeBSD var.c 1.13, sh.1 1.27).
 1.27 04-Feb-2001  christos branches: 1.27.2;
remove redundant declarations and nexted externs.
 1.26 20-Dec-2000  cgd __CONCAT does token pasting, not string concatnation. if something like:
__CONCAT("PATH=",_PATH_STDPATH);
actually works to concantate strings, it's because the preprocessor expands
it into "PATH=""whatever _PATH_STDPATH is" as separate strings, and then
ANSI string concatenation is performed on that. It's more straightforward
to just use ANSI string concatenation directly, and newer GCCs complain
(rightly) about mis-use of token pasting.
 1.25 22-May-2000  elric Back out previous vfork changes.
 1.24 17-May-2000  elric When vforking ensure that the environment passed to exec is built before
vforking as a set of local variables which can be popped by the parent.

Addresses bin/10124.
 1.23 09-Jul-1999  christos compile with WARNS = 2
 1.22 28-Jan-1999  kleink Add support for the export and readonly -p option.
 1.21 07-Apr-1998  fair fix default PATH to be <paths.h> _PATH_DEFPATH
 1.20 07-Apr-1998  fair Change a few things to reference /usr/include/paths.h instead of local
references. Fixing the default PATH is a bit more effort.
 1.19 04-Jul-1997  christos branches: 1.19.2;
Fix compiler warnings.
 1.18 11-Apr-1997  christos Track $TERM and call the appropriate editline(3) routine to update the
terminal type.
 1.17 14-Mar-1997  christos NO_HISTORY->SMALL
 1.16 11-Jan-1997  tls kill 'register'
 1.15 16-Oct-1996  christos PR/2808: - Don't use p++ in macros.
- Hash using unsigned numbers.
(from FreeBSD)
 1.14 25-Jun-1996  christos - Add function callback capability when variables are set.
- Add setvarsafe that returns an error instead of longjmp() to the
error code.
 1.13 11-May-1995  christos branches: 1.13.6;
Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.12 21-Mar-1995  cgd convert to new RCS id conventions.
 1.11 20-Jan-1995  mycroft Remove `.' from default PATH.
 1.10 05-Dec-1994  cgd clean up further. more patches from Jim Jegers
 1.9 23-Sep-1994  mycroft Eliminate uses of some obsolete functions.
 1.8 11-Jun-1994  mycroft Add RCS ids.
 1.7 14-May-1994  cgd add back in support for building w/o obj dir. also, add NO_HISTORY
define, which (if you invoke mkbuiltins properly) gets you a sh w/o
history of command line editing (for floppy sh).
 1.6 12-May-1994  jtc Include appropriate header files to bring function prototypes into scope.
 1.5 11-May-1994  jtc sync with 4.4lite
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.13.6.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.19.2.1 08-May-1998  mycroft Sync with trunk, per request of christos.
 1.27.2.1 27-Mar-2002  elric Doing the vfork work on ash on a branch to try to shake out the
problems before I expose everyone to them. This checkin represents
a merge of the prior work, which I backed out a while ago, to the
HEAD only and does not incorporate any additional bugfixes. The
additional bugfixes and code-cleanup will occur in later checkins.

For reference the patches that were used are:
cvs diff -kk -r1.51 -r1.55 eval.c | patch
cvs diff -kk -r1.27 -r1.28 exec.c | patch
cvs diff -kk -r1.15 -r1.16 exec.h | patch
cvs diff -kk -r1.32 -r1.33 input.c | patch
cvs diff -kk -r1.10 -r1.11 input.h | patch
cvs diff -kk -r1.32 -r1.35 jobs.c | patch
cvs diff -kk -r1.9 -r1.11 jobs.h | patch
cvs diff -kk -r1.36 -r1.37 main.c | patch
cvs diff -kk -r1.20 -r1.21 redir.c | patch
cvs diff -kk -r1.10 -r1.11 redir.h | patch
cvs diff -kk -r1.10 -r1.12 shell.h | patch
cvs diff -kk -r1.22 -r1.23 trap.c | patch
cvs diff -kk -r1.12 -r1.13 trap.h | patch
cvs diff -kk -r1.23 -r1.24 var.c | patch
cvs diff -kk -r1.16 -r1.17 var.h | patch

All other changes were simply the resolution of the resulting
conflicts, which occured only in the merge of jobs.c.

Begins to address PR: bin/5475
 1.39.16.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.40.4.1 16-Nov-2016  snj Pull up following revision(s) (requested by dholland in ticket #1412):
bin/sh/exec.c: revision 1.45
bin/sh/var.c: revision 1.43
PR/48312: Dieter Roelants: According to TOG, unset should not return an error
for functions are variables that were not previously set:
http://pubs.opengroup.org/onlinepubs/9699919799/utilities/V3_chap02.html
 1.40.2.3 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.40.2.2 23-Jan-2013  yamt sync with head
 1.40.2.1 17-Apr-2012  yamt sync with head
 1.41.2.2 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.41.2.1 25-Feb-2013  tls resync with head
 1.49.6.3 19-May-2017  pgoyette Resolve conflicts from previous merge (all resulting from $NetBSD
keywork expansion)
 1.49.6.2 11-May-2017  pgoyette Sync with HEAD
 1.49.6.1 02-May-2017  pgoyette Sync with HEAD - tag prg-localcount2-base1
 1.55.2.4 07-Dec-2018  martin Pull up following revision(s) (requested by kre in ticket #1127):

bin/sh/var.h: revision 1.38 (via patch)
bin/sh/var.c: revision 1.72
bin/sh/sh.1: revision 1.211 (via patch)

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
 1.55.2.3 25-Aug-2018  martin Pull up following revision(s) (requested by kre in ticket #988):

bin/sh/parser.c: revision 1.147
bin/sh/var.c: revision 1.70
bin/sh/mystring.c: revision 1.18
bin/sh/options.c: revision 1.53
bin/sh/histedit.c: revision 1.53

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.)
 1.55.2.2 31-Aug-2017  martin Pull up following revision(s) (requested by kre in ticket #250):
bin/sh/var.c: revision 1.67
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...
 1.55.2.1 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.69.4.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.69.4.2 21-Apr-2020  martin Sync with HEAD
 1.69.4.1 10-Jun-2019  christos Sync with HEAD
 1.69.2.3 26-Jan-2019  pgoyette Sync with HEAD
 1.69.2.2 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.69.2.1 28-Jul-2018  pgoyette Sync with HEAD
 1.78.2.1 02-Jan-2025  martin Pull up following revision(s) (requested by kre in ticket #1932):

bin/sh/var.c: revision 1.88

Fix a bug from when the ToD variable was added (July 2017) where if
TZ is unset, and ToD_FORMAT contains and strftime() conversions which
need to know the zone, bad things happen.

Amazing that no-one (incl me) ever noticed this.
 1.82.4.1 02-Aug-2025  perseant Sync with HEAD
 1.82.2.1 31-Dec-2024  snj Pull up following revision(s) (requested by kre in ticket #1034):
bin/sh/var.c: 1.88
Fix a bug from when the ToD variable was added (July 2017) where if
TZ is unset, and ToD_FORMAT contains and strftime() conversions which
need to know the zone, bad things happen.
Amazing that no-one (incl me) ever noticed this.
 1.41 13-Jul-2024  kre Implement the HISTFILE and HISTAPPEND variables.

See the (newly updated) sh(1) for details.
Also add the -z option to fc (clear history).

None of this exists in SMALL shells.
 1.40 12-Jul-2024  kre Improve safety in var imports from the environment.

Add a new var flag VUNSAFE - set on all vars imported from the environment.

Add setvareqsafe() (which is to setvareq() as setvarsafe() is to setvar())
and use that instead of setvareq() when processing the environment, so
errors don't cause the shell to abort. Use VUNSAFE in that call.

Add flags arguments to all var callback functions which are used when setting
variables, and pass the flags given to the setvar*() functions to those
functions, so they can act differently in different situations (if desired).
Most of them just ignore the flags.

When unsetting a variable, call setvar() to clear things (and call the
callback function) both when the variable had a value which needs to be freed,
and when unsetting a variable which wasn't unset previously, so the VUNSET
flag can be seen by that callback func.

When setting HISTSIZE, use the flags passed to determine whether to ignore
bad values (if VUNSAFE) or treat them as an error. This replaces the
earlier temporary hack to always ignore bad data there (histedit.c 1.68).

Miscellaneous associated minor changes.

These changes should largely be invisible in normal use.
 1.39 18-Sep-2022  kre branches: 1.39.4;
Add the -l option (aka -o login): be a login shell. Meaningful only on
the command line (with both - and + forms) - overrides the presence (or
otherwise) of a '-' as argv[0][0].

Since this allows any shell to be a login shell (which simply means that
it runs /etc/profile and ~/.profile at shell startup - there are no other
side effects) add a new, always set at startup, variable NBSH_INVOCATION
which has a char string as its value, where each char has a meaning,
more or less related to how the shell was started. See sh(1).
This is intended to allow those startup scripts to tailor their behaviour
to the nature of this particular login shell (it is possible to detect
whether a shell is a login shell merely because of -l, or whether it would
have been anyway, before the -l option was added - and more). The
var could also be used to set different values for $ENV for different
uses of the shell.
 1.38 04-Dec-2018  kre 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
 1.37 03-Dec-2018  kre 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()
 1.36 28-Oct-2017  kre branches: 1.36.2; 1.36.4;
Extract the variable name validity test from setname() into a
function of its own. It will soon be needed from another source.
 1.35 30-Jun-2017  kre 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.))
 1.34 28-Jun-2017  kre Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
 1.33 27-Jun-2017  kre Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
 1.32 26-Jun-2017  kre 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.)
 1.31 17-Jun-2017  kre Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).

Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)

From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
 1.30 07-Jun-2017  kre A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)

This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)

The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.

This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).

I would not call the results of this perfect yet, but it is close.
 1.29 07-Jun-2017  kre An initial attempt at implementing LINENO to meet the specs.

Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.

Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)

This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.

POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)

This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.

This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).

Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
 1.28 31-Mar-2016  christos branches: 1.28.8;
Implement the NETBSD_SHELL readonly unexportable unimportable
variable (with its current value set at 20160401) as discussed on
current-users and tech-userlevel. This also includes the necessary
support to implement it properly (particularly the unexportable
part) and adds options to the export command to support unexportable
variables. Also implement the "posix" option (no single letter
equivalent) which gets its default value from whether or not
POSIXLY_CORRECT is set in the environment when the shell starts
(but can be changed just like any other option using -o and +o on
the command line, or the set builtin command.) While there, fix
all uses of options so it is possible to have options that have a
short (one char) name, and no long name, just as it has been possible
to have options with a long name and no short name, though there
are currently none (with no long name). For now, the only use of
the posix option is to control whether ${ENV} is read at startup
by a non-interactive shell, so changing it with set is not usful
- that might change in the future. (from kre@)
 1.27 27-Mar-2016  christos General KNF and source code cleanups, avoid scattering the
magic string " \t\n" all over the place, slightly improved
syntax error messages, restructured some of the code for
clarity, don't allow IFS to be imported through the environment,
and remove the (never) conditionally compiled ATTY option.
Apart from one or two syntax error messages, and ignoring IFS
if present in the environment, this is intended to have no
user visible changes. (from kre@)
 1.26 26-May-2015  christos Drop privileges when executed set{u,g}id unless -p is specified like other
shells do to avoid system() and popen() abuse.
 1.25 18-Jun-2011  christos PR/45069: Henning Petersen: Use prototypes from builtins.h .
 1.24 16-Oct-2008  dholland branches: 1.24.16;
Use "extern" properly for referencing globals defined in other modules.
Now builds cleanly with -warn-common.
 1.23 02-Oct-2004  dsl Save the length of each variable in the name table so that we can
compare the lengths and then use memcmp() in the search code.
Speeds up one of my scripts by a facter of 2.
Increase the size of the variable hash table.
Cuts down time for script to execute from 60 seconds to 10.
Move variable search into a new function to hide the implementation
from most of the code, new version is slightly smaller than old.
 1.22 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.21 22-Jan-2003  dsl Support command -p, -v and -V as posix
Stop temporary PATH assigments messing up hash table
Fix sh -c -e "echo $0 $*" -a x (as posix)
(agreed by christos)
 1.20 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.19 27-Sep-2002  christos VFork()ing shell: From elric@netbsd.org:
Plus my changes:
- walking process group fix in foregrounding a job.
- reset of process group in parent shell if interrupted before the wait.
- move INTON lower in the dowait so that the job structure is
consistent.
- error check all setpgid(), tcsetpgrp() calls.
- eliminate unneeded strpgid() call.
- check that we don't belong in the process group before we try to
set it.
 1.18 22-May-2000  elric branches: 1.18.6;
Back out previous vfork changes.
 1.17 17-May-2000  elric When vforking ensure that the environment passed to exec is built before
vforking as a set of local variables which can be popped by the parent.

Addresses bin/10124.
 1.16 09-Jul-1999  christos compile with WARNS = 2
 1.15 25-Jan-1999  mycroft Patches from Tor Egge (via Havard Eidnes) to fix various bugs in field
splitting and combining.
(Note: Some of this are not strictly bugs, but differences between traditional
Bourne shell and POSIX.)
 1.14 11-Apr-1997  christos Track $TERM and call the appropriate editline(3) routine to update the
terminal type.
 1.13 14-Mar-1997  christos NO_HISTORY->SMALL
 1.12 02-Nov-1996  christos Fix problems that gcc -Wall found (from Todd Miller, OpenBSD)
 1.11 16-Oct-1996  christos PR/2808: Remove trailing whitespace (from FreeBSD)
 1.10 25-Jun-1996  christos - Add function callback capability when variables are set.
- Add setvarsafe that returns an error instead of longjmp() to the
error code.
 1.9 11-May-1995  christos branches: 1.9.6;
Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.8 21-Mar-1995  cgd convert to new RCS id conventions.
 1.7 05-Dec-1994  cgd clean up further. more patches from Jim Jegers
 1.6 11-Jun-1994  mycroft Add RCS ids.
 1.5 11-May-1994  jtc sync with 4.4lite
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.9.6.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.18.6.1 27-Mar-2002  elric Doing the vfork work on ash on a branch to try to shake out the
problems before I expose everyone to them. This checkin represents
a merge of the prior work, which I backed out a while ago, to the
HEAD only and does not incorporate any additional bugfixes. The
additional bugfixes and code-cleanup will occur in later checkins.

For reference the patches that were used are:
cvs diff -kk -r1.51 -r1.55 eval.c | patch
cvs diff -kk -r1.27 -r1.28 exec.c | patch
cvs diff -kk -r1.15 -r1.16 exec.h | patch
cvs diff -kk -r1.32 -r1.33 input.c | patch
cvs diff -kk -r1.10 -r1.11 input.h | patch
cvs diff -kk -r1.32 -r1.35 jobs.c | patch
cvs diff -kk -r1.9 -r1.11 jobs.h | patch
cvs diff -kk -r1.36 -r1.37 main.c | patch
cvs diff -kk -r1.20 -r1.21 redir.c | patch
cvs diff -kk -r1.10 -r1.11 redir.h | patch
cvs diff -kk -r1.10 -r1.12 shell.h | patch
cvs diff -kk -r1.22 -r1.23 trap.c | patch
cvs diff -kk -r1.12 -r1.13 trap.h | patch
cvs diff -kk -r1.23 -r1.24 var.c | patch
cvs diff -kk -r1.16 -r1.17 var.h | patch

All other changes were simply the resolution of the resulting
conflicts, which occured only in the merge of jobs.c.

Begins to address PR: bin/5475
 1.24.16.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.28.8.2 07-Dec-2018  martin Pull up following revision(s) (requested by kre in ticket #1127):

bin/sh/var.h: revision 1.38 (via patch)
bin/sh/var.c: revision 1.72
bin/sh/sh.1: revision 1.211 (via patch)

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
 1.28.8.1 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.36.4.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.36.4.2 21-Apr-2020  martin Sync with HEAD
 1.36.4.1 10-Jun-2019  christos Sync with HEAD
 1.36.2.1 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.39.4.1 02-Aug-2025  perseant Sync with HEAD
 1.4 22-Jan-2022  kre After 3 and a bit years, it is time...
 1.3 12-Dec-2018  kre 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...
 1.2 31-Mar-2016  christos branches: 1.2.14; 1.2.16;
replace with standard copyright :-)
 1.1 31-Mar-2016  christos Implement the NETBSD_SHELL readonly unexportable unimportable
variable (with its current value set at 20160401) as discussed on
current-users and tech-userlevel. This also includes the necessary
support to implement it properly (particularly the unexportable
part) and adds options to the export command to support unexportable
variables. Also implement the "posix" option (no single letter
equivalent) which gets its default value from whether or not
POSIXLY_CORRECT is set in the environment when the shell starts
(but can be changed just like any other option using -o and +o on
the command line, or the set builtin command.) While there, fix
all uses of options so it is possible to have options that have a
short (one char) name, and no long name, just as it has been possible
to have options with a long name and no short name, though there
are currently none (with no long name). For now, the only use of
the posix option is to control whether ${ENV} is read at startup
by a non-interactive shell, so changing it with set is not usful
- that might change in the future. (from kre@)
 1.2.16.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.2.16.2 21-Apr-2020  martin Sync with HEAD
 1.2.16.1 10-Jun-2019  christos Sync with HEAD
 1.2.14.1 26-Dec-2018  pgoyette Sync with HEAD, resolve a few conflicts
 1.4 05-Jul-2014  dholland branches: 1.4.24;
Reorg docs, part 2:
Rename the following reference documents to match their programs:
shell -> sh
viref -> vi

and rename the following to match their topic better:
ipctut -> sockets
ipc -> sockets-advanced

Also, the old "timed" and "timedop" docs are now ref5/timed and
ref8/timed respectively, as the first of these documented the
protocol.
 1.3 05-Jul-2014  dholland Reorg docs, part 1:
Move all the reference manuals to subdirs of /usr/share/doc/reference.
We have subdirs ref1-ref9, corresponding to man page sections 1-9.

Everything that's the reference manual for a program (sections 1, 6,
8), C interface (sections 2, 3), driver or file system (section 4),
format or configuration (section 5), or kernel internal interface
(section 9) belongs in here.

Section 7 is a little less clear: some things that might go in section
7 if they were a man page aren't really reference manuals. So I'm only
putting things in reference section 7 that are (to me) clearly
reference material, rather than e.g. tutorials, guides, FAQs, etc.
This obviously leaves some room for debate, especially without first
editing the docs with this distinction in mind, but if people hate
what I've done things can always be moved again.

Note also that while roff macro man pages traditionally go in section
7, I have put all the roff documentation (macros, tools, etc.) in one
place in reference/ref1/roff. This will make it easier to find and
also easier to edit it into some kind of coherent form.
 1.2 05-Jul-2014  dholland Rework /usr/share/doc.

Update the <bsd.doc.mk> infrastructure, and update the docs to match
the new infrastructure.

- Build and install text, ps, pdf, and/or html, not roff sources.

- Don't wire the chapter numbers into the build system, or use them in
the installed pathnames. This didn't matter much when the docs were a
museum, but now that we're theoretically going to start maintaining
them again, we're going to add and remove documents periodically and
having the chapter numbers baked in creates a lot of thrashing for no
purpose.

- Specify the document name explicitly, rather than implicitly in a
path. Use this name (instead of other random strings) as the name
of the installed files.

- Specify the document section, which is the subdirectory of
/usr/share/doc to install into.

- Allow multiple subdocuments. (That is, multiple documents in one
output directory.)

- Enumerate the .png files groff emits along with html so they can be
installed.

- Remove assorted hand-rolled rules for running roff and roff widgetry
and add enough variable settings to make these unnecessary. This
includes support for
- explicit use of soelim
- refer
- tbl
- pic
- eqn

- Forcibly apply at least minimal amounts of sanity to certain
autogenerated roff files.

- Don't exclude USD.doc, SMM.doc, and PSD.doc directories from the
build, as they now actually do stuff.

Note: currently we can't generate pdf. This turns out to be a
nontrivial problem with no immediate solution forthcoming. So for now,
as a workaround, install compressed .ps as the printable form.
 1.1 22-Aug-2010  perry branches: 1.1.12; 1.1.24;
Revive S.R. Bourne's original tutorial on using the Bourne Shell.
This initial commit consists of the files as they existed in 4.4BSD,
with the copyrights altered to reflect the subsequent BSD relicensing.
 1.1.24.1 10-Aug-2014  tls Rebase.
 1.1.12.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.4.24.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.4.24.1 21-Apr-2020  martin Sync with HEAD
 1.2 14-Sep-2018  rillig Fix "every" typo in quote from The Mythical Man-Month
 1.1 22-Aug-2010  perry branches: 1.1.46; 1.1.48;
Revive S.R. Bourne's original tutorial on using the Bourne Shell.
This initial commit consists of the files as they existed in 4.4BSD,
with the copyrights altered to reflect the subsequent BSD relicensing.
 1.1.48.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.48.2 21-Apr-2020  martin Sync with HEAD
 1.1.48.1 10-Jun-2019  christos Sync with HEAD
 1.1.46.1 30-Sep-2018  pgoyette Ssync with HEAD
 1.1 05-Jul-2014  dholland branches: 1.1.2; 1.1.6; 1.1.28;
Rework /usr/share/doc.

Update the <bsd.doc.mk> infrastructure, and update the docs to match
the new infrastructure.

- Build and install text, ps, pdf, and/or html, not roff sources.

- Don't wire the chapter numbers into the build system, or use them in
the installed pathnames. This didn't matter much when the docs were a
museum, but now that we're theoretically going to start maintaining
them again, we're going to add and remove documents periodically and
having the chapter numbers baked in creates a lot of thrashing for no
purpose.

- Specify the document name explicitly, rather than implicitly in a
path. Use this name (instead of other random strings) as the name
of the installed files.

- Specify the document section, which is the subdirectory of
/usr/share/doc to install into.

- Allow multiple subdocuments. (That is, multiple documents in one
output directory.)

- Enumerate the .png files groff emits along with html so they can be
installed.

- Remove assorted hand-rolled rules for running roff and roff widgetry
and add enough variable settings to make these unnecessary. This
includes support for
- explicit use of soelim
- refer
- tbl
- pic
- eqn

- Forcibly apply at least minimal amounts of sanity to certain
autogenerated roff files.

- Don't exclude USD.doc, SMM.doc, and PSD.doc directories from the
build, as they now actually do stuff.

Note: currently we can't generate pdf. This turns out to be a
nontrivial problem with no immediate solution forthcoming. So for now,
as a workaround, install compressed .ps as the printable form.
 1.1.28.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.1.28.1 21-Apr-2020  martin Sync with HEAD
 1.1.6.2 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.1.6.1 05-Jul-2014  tls file referargs was added on branch tls-maxphys on 2014-08-19 23:45:11 +0000
 1.1.2.2 10-Aug-2014  tls Rebase.
 1.1.2.1 05-Jul-2014  tls file referargs was added on branch tls-earlyentropy on 2014-08-10 06:41:18 +0000
 1.2 22-Aug-2010  perry branches: 1.2.48;
RCSIds
 1.1 22-Aug-2010  perry Revive S.R. Bourne's original tutorial on using the Bourne Shell.
This initial commit consists of the files as they existed in 4.4BSD,
with the copyrights altered to reflect the subsequent BSD relicensing.
 1.2.48.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.2.48.1 21-Apr-2020  martin Sync with HEAD
 1.3 22-Aug-2010  perry branches: 1.3.48;
RCSIds
 1.2 22-Aug-2010  perry Extremely extensive updates.

This document should now serve as a reasonable tutorial for the
modern POSIX shell. Comments and additional fixes for mistakes I may
have made are solicited.
 1.1 22-Aug-2010  perry Revive S.R. Bourne's original tutorial on using the Bourne Shell.
This initial commit consists of the files as they existed in 4.4BSD,
with the copyrights altered to reflect the subsequent BSD relicensing.
 1.3.48.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.3.48.1 21-Apr-2020  martin Sync with HEAD
 1.3 22-Aug-2010  perry branches: 1.3.48;
RCSIds
 1.2 22-Aug-2010  perry Extremely extensive updates.

This document should now serve as a reasonable tutorial for the
modern POSIX shell. Comments and additional fixes for mistakes I may
have made are solicited.
 1.1 22-Aug-2010  perry Revive S.R. Bourne's original tutorial on using the Bourne Shell.
This initial commit consists of the files as they existed in 4.4BSD,
with the copyrights altered to reflect the subsequent BSD relicensing.
 1.3.48.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.3.48.1 21-Apr-2020  martin Sync with HEAD
 1.3 22-Aug-2010  perry branches: 1.3.48;
RCSIds
 1.2 22-Aug-2010  perry Extremely extensive updates.

This document should now serve as a reasonable tutorial for the
modern POSIX shell. Comments and additional fixes for mistakes I may
have made are solicited.
 1.1 22-Aug-2010  perry Revive S.R. Bourne's original tutorial on using the Bourne Shell.
This initial commit consists of the files as they existed in 4.4BSD,
with the copyrights altered to reflect the subsequent BSD relicensing.
 1.3.48.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.3.48.1 21-Apr-2020  martin Sync with HEAD
 1.3 22-Aug-2010  perry branches: 1.3.48;
RCSIds
 1.2 22-Aug-2010  perry Extremely extensive updates.

This document should now serve as a reasonable tutorial for the
modern POSIX shell. Comments and additional fixes for mistakes I may
have made are solicited.
 1.1 22-Aug-2010  perry Revive S.R. Bourne's original tutorial on using the Bourne Shell.
This initial commit consists of the files as they existed in 4.4BSD,
with the copyrights altered to reflect the subsequent BSD relicensing.
 1.3.48.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.3.48.1 21-Apr-2020  martin Sync with HEAD
 1.16 18-May-2021  kre Fix a bug in the built-in echo in /bin/sh reported in private mail by
Oguz <oguzismailuysal@gmail.com>

If echo detects an I/O error, it does exit(1) (that's fine) but then
the next echo also does exit(1) even without any errors of its own,
and every following echo writing to stdout does the same thing.

eg:

echo foo >&- ; echo $?; echo $?; ( echo $( echo $?; echo $?) ; echo $? )
1
1
1 1
1

The first echo writes nothing (stdout is closed) but does exit(1).
The second echo writes "1" (correct, the exit status of the previous
echo) and should exit(0) - but doesn't. This pattern continues...

While here, conform to the POSIX requirement on echo (and many other
standard utilities, but definitely not all) that when the utility
does exit(>0) a message must be written to stderr (and vice versa
in many cases). Our echo (as shown above) did the exit(1) part
when it detected the I/O error, but no message is sent to stderr.
Fix that while we're here.

Similar changes are required for /bin/echo (coming soon), and
/usr/bin/printf (which is also the sh builtin printf) - except
currently that one kind of conforms, as it ignores errors writing
to stdout (as do large numbers of other utilities). For many
programs that's kind of acceptable, but where the sole purpose of
the program is to write to stdout, it really isn't. Also to be
fixed soon.
 1.15 26-Jun-2017  kre branches: 1.15.6; 1.15.12;
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.)
 1.14 16-Mar-2016  christos branches: 1.14.8;
Avoid warning by using gcc'ism
 1.13 12-Oct-2008  dholland output.c output.h: expose OUTPUT_ERR (flag for an exposed flags variable)
bltin.h: support ferror()
echo.c: use ferror() to fail on output write errors

Another piece of PR bin/39574.
 1.12 29-Mar-2008  apb * define SHELL_BUILTIN, which other headers may use to hide some of their
symbols if appropriate. For example, error.h will use it to hide
declarations that should not be seen by external builtins such as printf.
* The shell's outfmt() function returns void, but the standard fprintf()
function returns int. Similarly for several other functions that are
redefined via macros in bltin.h. Add a _RETURN_INT macro to do the
necessary conversion.
* Delete some declarations that appear in error.h.
* Add comments on some #else/#endif lines.
 1.11 07-Aug-2003  agc branches: 1.11.16;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.10 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.9 04-Jul-1997  christos Fix compiler warnings.
 1.8 16-Oct-1996  christos PR/2808: Add a definition for warnx() (from FreeBSD; currently unused)
 1.7 21-Mar-1995  cgd branches: 1.7.6;
convert to new RCS id conventions.
 1.6 11-Jun-1994  mycroft Add RCS ids.
 1.5 11-May-1994  jtc sync with 4.4lite
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.7.6.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.11.16.1 18-Nov-2008  bouyer Pull up following revision(s) (requested by dholland in ticket #1232):
bin/echo/echo.c: revision 1.18
bin/sh/bltin/bltin.h: revision 1.13
bin/sh/bltin/echo.c: revision 1.14
bin/sh/output.c: revision 1.30
bin/sh/output.h: revision 1.19
Check ferror(stdout) and exit(1) if there's been a problem.
Fixes PR bin/39574 from Ed Ravin, but with a simpler patch.
output.c output.h: expose OUTPUT_ERR (flag for an exposed flags variable)
bltin.h: support ferror()
echo.c: use ferror() to fail on output write errors
Another piece of PR bin/39574.
 1.14.8.1 23-Jul-2017  snj Pull up following revision(s) (requested by kre in ticket #103):
bin/kill/kill.c: 1.28
bin/sh/Makefile: 1.111-1.113
bin/sh/arith_token.c: 1.5
bin/sh/arith_tokens.h: 1.2
bin/sh/arithmetic.c: 1.3
bin/sh/arithmetic.h: 1.2
bin/sh/bltin/bltin.h: 1.15
bin/sh/cd.c: 1.49-1.50
bin/sh/error.c: 1.40
bin/sh/eval.c: 1.142-1.151
bin/sh/exec.c: 1.49-1.51
bin/sh/exec.h: 1.26
bin/sh/expand.c: 1.113-1.119
bin/sh/expand.h: 1.23
bin/sh/histedit.c: 1.49-1.52
bin/sh/input.c: 1.57-1.60
bin/sh/input.h: 1.19-1.20
bin/sh/jobs.c: 1.86-1.87
bin/sh/main.c: 1.71-1.72
bin/sh/memalloc.c: 1.30
bin/sh/memalloc.h: 1.17
bin/sh/mknodenames.sh: 1.4
bin/sh/mkoptions.sh: 1.3-1.4
bin/sh/myhistedit.h: 1.12-1.13
bin/sh/nodetypes: 1.16-1.18
bin/sh/option.list: 1.3-1.5
bin/sh/parser.c: 1.133-1.141
bin/sh/parser.h: 1.22-1.23
bin/sh/redir.c: 1.58
bin/sh/redir.h: 1.24
bin/sh/sh.1: 1.149-1.159
bin/sh/shell.h: 1.24
bin/sh/show.c: 1.43-1.47
bin/sh/show.h: 1.11
bin/sh/syntax.c: 1.4
bin/sh/syntax.h: 1.8
bin/sh/trap.c: 1.41
bin/sh/var.c: 1.56-1.65
bin/sh/var.h: 1.29-1.35
An initial attempt at implementing LINENO to meet the specs.
Aside from one problem (not too hard to fix if it was ever needed) this version
does about as well as most other shell implementations when expanding
$((LINENO)) and better for ${LINENO} as it retains the "LINENO hack" for the
latter, and that is very accurate.
Unfortunately that means that ${LINENO} and $((LINENO)) do not always produce
the same value when used on the same line (a defect that other shells do not
share - aside from the FreeBSD sh as it is today, where only the LINENO hack
exists and so (like for us before this commit) $((LINENO)) is always either
0, or at least whatever value was last set, perhaps by
LINENO=${LINENO}
which does actually work ... for that one line...)
This could be corrected by simply removing the LINENO hack (look for the string
LINENO in parser.c) in which case ${LINENO} and $((LINENO)) would give the
same (not perfectly accurate) values, as do most other shells.
POSIX requires that LINENO be set before each command, and this implementation
does that fairly literally - except that we only bother before the commands
which actually expand words (for, case and simple commands). Unfortunately
this forgot that expansions also occur in redirects, and the other compound
commands can also have redirects, so if a redirect on one of the other compound
commands wants to use the value of $((LINENO)) as a part of a generated file
name, then it will get an incorrect value. This is the "one problem" above.
(Because the LINENO hack is still enabled, using ${LINENO} works.)
This could be fixed, but as this version of the LINENO implementation is just
for reference purposes (it will be superseded within minutes by a better one)
I won't bother. However should anyone else decide that this is a better choice
(it is probably a smaller implementation, in terms of code & data space then
the replacement, but also I would expect, slower, and definitely less accurate)
this defect is something to bear in mind, and fix.
This version retains the *BSD historical practice that line numbers in functions
(all functions) count from 1 from the start of the function, and elsewhere,
start from 1 from where the shell started reading the input file/stream in
question. In an "eval" expression the line number starts at the line of the
"eval" (and then increases if the input is a multi-line string).
Note: this version is not documented (beyond as much as LINENO was before)
hence this slightly longer than usual commit message.
A better LINENO implementation. This version deletes (well, #if 0's out)
the LINENO hack, and uses the LINENO var for both ${LINENO} and $((LINENO)).
(Code to invert the LINENO hack when required, like when de-compiling the
execution tree to provide the "jobs" command strings, is still included,
that can be deleted when the LINENO hack is completely removed - look for
refs to VSLINENO throughout the code. The var funclinno in parser.c can
also be removed, it is used only for the LINENO hack.)
This version produces accurate results: $((LINENO)) was made as accurate
as the LINENO hack made ${LINENO} which is very good. That's why the
LINENO hack is not yet completely removed, so it can be easily re-enabled.
If you can tell the difference when it is in use, or not in use, then
something has broken (or I managed to miss a case somewhere.)
The way that LINENO works is documented in its own (new) section in the
man page, so nothing more about that, or the new options, etc, here.
This version introduces the possibility of having a "reference" function
associated with a variable, which gets called whenever the value of the
variable is required (that's what implements LINENO). There is just
one function pointer however, so any particular variable gets at most
one of the set function (as used for PATH, etc) or the reference function.
The VFUNCREF bit in the var flags indicates which func the variable in
question uses (if any - the func ptr, as before, can be NULL).
I would not call the results of this perfect yet, but it is close.
Unbreak (at least) i386 build .... I have no idea why this built for me on
amd64 (problem was missing prototype for snprintf witout <stdio.h>)
While here, add some (DEBUG mode only) tracing that proved useful in
solving another problem.
Set the line number before expanding args, not after. As the line_number
would have usually been set earlier, this change is mostly an effective
no-op, but it is better this way (just in case) - not observed to have
caused any problems.
Undo some over agressive fixes for a (pre-commit) bug that did not
need these changes to be fixed - and these cause problems in another
absurd use case. Either of these issues is unlikely to be seen by
anyone who isn't an idiot masochist...
PR bin/52280
removescapes_nl in expari() even when not quoted,
CRTNONL's appear regardless of quoting (unlike CTLESC).
New sentence, new line. Whitespace.
Improve the (new) LINENO section, markup changes (with thanks to wiz@ for
assistace) and some better wording in a few placed.
I am an idiot... revert the previous unintended commit.
Remove some left over baggage from the LINENO v1 implementation that
didn't get removed with v2, and should have. This would have had
(I think, without having tested it) one very minor effect on the way
LINENO worked in the v2 implementation, but my guess is it would have
taken a long time before anyone noticed...
Correct spelling in comments of DEBUG only code...
(Perhaps) temporary fix to pkgtools (cwrappers) build (configure).
Expanding `` containing \ \n sequences looks to have been giving
problems. I don't think this is the correct fix, but it will do
no worse harm than (perhaps) incorrectly calculating LINENO in this
kind of (rare) circumstance. I'll look and see if there should be
a better fix later.
s/volatile/const/ -- wonderful how opposites attract like this.
NFC (normal use) - DEBUG only change, when showing empty arg list don't
omit terminating \n.
Free stack memory in a couple of obscure cases where it wasn't
being done (one in probably dead code that is never compiled, the other
in a very rare error case.) Since it is stack memory it wasn't lost
in any case, just held longer than needed.
Many internal memory management type fixes.
PR bin/52302 (core dump with interactive shell, here doc and error
on same line) is fixed. (An old bug.)
echo "$( echo x; for a in $( seq 1000 ); do printf '%s\n'; done; echo y )"
consistently prints 1002 lines (x, 1000 empty ones, then y) as it should
(And you don't want to know what it did before, or why.) (Another old one.)
(Recently added) Problems with ~ expansion fixed (mem management related).
Proper fix for the cwrappers configure problem (which includes the quick
fix that was done earlier, but extends upon that to be correct). (This was
another newly added problem.)
And the really devious (and rare) old bug - if STACKSTRNUL() needs to
allocate a new buffer in which to store the \0, calculate the size of
the string space remaining correctly, unlike when SPUTC() grows the
buffer, there is no actual data being stored in the STACKSTRNUL()
case - the string space remaining was calculated as one byte too few.
That would be harmless, unless the next buffer also filled, in which
case it was assumed that it was really full, not one byte less, meaning
one junk char (a nul, or anything) was being copied into the next (even
bigger buffer) corrupting the data.
Consistent use of stalloc() to allocate a new block of (stack) memory,
and grabstackstr() to claim a block of (stack) memory that had already
been occupied but not claimed as in use. Since grabstackstr is implemented
as just a call to stalloc() this is a no-op change in practice, but makes
it much easier to comprehend what is really happening. Previous code
sometimes used stalloc() when the use case was really for grabstackstr().
Change grabstackstr() to actually use the arg passed to it, instead of
(not much better than) guessing how much space to claim,
More care when using unstalloc()/ungrabstackstr() to return space, and in
particular when the stack must be returned to its previous state, rather than
just returning no-longer needed space, neither of those work. They also don't
work properly if there have been (really, even might have been) any stack mem
allocations since the last stalloc()/grabstackstr(). (If we know there
cannot have been then the alloc/release sequence is kind of pointless.)
To work correctly in general we must use setstackmark()/popstackmark() so
do that when needed. Have those also save/restore the top of stack string
space remaining.
[Aside: for those reading this, the "stack" mentioned is not
in any way related to the thing used for maintaining the C
function call state, ie: the "stack segment" of the program,
but the shell's internal memory management strategy.]
More comments to better explain what is happening in some cases.
Also cleaned up some hopelessly broken DEBUG mode data that were
recently added (no effect on anyone but the poor semi-human attempting
to make sense of it...).
User visible changes:
Proper counting of line numbers when a here document is delimited
by a multi-line end-delimiter, as in
cat << 'REALLY
END'
here doc line 1
here doc line 2
REALLY
END
(which is an obscure case, but nothing says should not work.) The \n
in the end-delimiter of the here doc (the last one) was not incrementing
the line number, which from that point on in the script would be 1 too
low (or more, for end-delimiters with more than one \n in them.)
With tilde expansion:
unset HOME; echo ~
changed to return getpwuid(getuid())->pw_home instead of failing (returning ~)
POSIX says this is unspecified, which makes it difficult for a script to
compensate for being run without HOME set (as in env -i sh script), so
while not able to be used portably, this seems like a useful extension
(and is implemented the same way by some other shells).
Further, with
HOME=; printf %s ~
we now write nothing (which is required by POSIX - which requires ~ to
expand to the value of $HOME if it is set) previously if $HOME (in this
case) or a user's directory in the passwd file (for ~user) were a null
STRING, We failed the ~ expansion and left behind '~' or '~user'.
Changed the long name for the -L option from lineno_fn_relative
to local_lineno as the latter seemed to be marginally more popular,
and perhaps more importantly, is the same length as the peviously
existing quietprofile option, which means the man page indentation
for the list of options can return to (about) what it was before...
(That is, less indented, which means more data/line, which means less
lines of man page - a good thing!)
Cosmetic changes to variable flags - make their values more suited
to my delicate sensibilities... (NFC).
Arrange not to barf (ever) if some turkey makes _ readonly. Do this
by adding a VNOERROR flag that causes errors in var setting to be
ignored (intended use is only for internal shell var setting, like of "_").
(nb: invalid var name errors ignore this flag, but those should never
occur on a var set by the shell itself.)
From FreeBSD: don't simply discard memory if a variable is not set for
any reason (including because it is readonly) if the var's value had
been malloc'd. Free it instead...
NFC - DEBUG changes, update this to new TRACE method.
KNF - white space and comment formatting.
NFC - DEBUG mode only change - convert this to the new TRACE() format.
NFC - DEBUG mode only change - complete a change made earlier (marking
the line number when included in the trace line tag to show whether it
comes from the parser, or the elsewhere as they tend to be quite different).
Initially only one case was changed, while I pondered whether I liked it
or not. Now it is all done... Also when there is a line tag at all,
always include the root/sub-shell indicator character, not only when the
pid is included.
NFC: DEBUG related comment change - catch up with reality.
NFC: DEBUG mode only change. Fix botched cleanup of one TRACE().
"b" more forgiving when sorting options to allow reasonable (and intended)
flexibility in option.list format. Changes nothing for current option.list.
Now that excessive use of STACKSTRNUL has served its purpose (well, accidental
purpose) in exposing the bug in its implementation, go back to not using
it when not needed for DEBUG TRACE purposes. This change should have no
practical effect on either a DEBUG shell (where the STACKSTRNUL() calls
remain) or a non DEBUG shell where they are not needed.
Correct the initial line number used for processing -c arg strings.
(It was inheriting the value from end of profile file processing) - I didn't
notice before as I usually test with empty or no profile files to avoid
complications. Trivial change which should have very limited impact.
Fix from FreeBSD (applied there in July 2008...)
Don't dump core with input like sh -c 'x=; echo >&$x' - that is where
the word after a >& or <& redirect expands to nothing at all.
Another fix from FreeBSD (this one from April 2009).
When processing a string (as in eval, trap, or sh -c) don't allow
trailing \n's to destroy the exit status of the last command executed.
That is:
sh -c 'false
'
echo $?
should produce 1, not 0.
It is amazing what nonsense appears to work sometimes... (all my nonsense too!)
Two bugs here, one benign because of the way the script is used.
The other hidden by NetBSD's sort being stable, and the data not really
requiring sorting at all...
So as it happens these fixes change nothing, but they are needed anyway.
(The contents of the generated file are only used in DEBUG shells, so
this is really even less important than it seems.)
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.
source .editrc after we initialize so that commands persist!
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.)
Properly support EDITRC - use it as (naming) the file when setting
up libedit, and re-do the config whenever EDITRC is set.
Get rid of workarounds for ancient groff html backend.
Simplify macro usage.
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.
Add literal prompt support this allows one to do:
CA="$(printf '\1')"
PS1="${CA}$(tput bold)${CA}\$${CA}$(tput sgr0)${CA} "
Now libedit supports embedded mode switch sequence, improve sh
support for them (adds PSlit variable to set the magic character).
NFC: DEBUG only change - provide an externally visible (to the DEBUG sh
internals) interface to one of the internal (private to trace code) functions
Include redirections in trace output from "set -x"
Implement PS1, PS2 and PS4 expansions (variable expansions, arithmetic
expansions, and if enabled by the promptcmds option, command substitutions.)
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.))
Omnibus manual update for prompt expansions and new variables. Throw in
some random cleanups as a bonus.
Correct a markup typo (why did I not see this before the prev commit??)
Sort options (our default is 0..9AaBbZz).
Fix markup problems and a typo.
Make $- list flags in the same order they appear in sh(1)
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...).
DEBUG and white space changes only. Convert TRACE() calls for DEBUg mode
to the new style. NFC (when not debugging sh).
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.)
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.
DEBUG only changes. Convert the TRACE() calls in the remaining files
that still used it to the new format. NFC.
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.
 1.15.12.1 31-May-2021  cjep sync with head
 1.15.6.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.15.6.1 21-Apr-2020  martin Sync with HEAD
 1.14 03-Jul-2017  wiz branches: 1.14.6;
Remove workaround for ancient HTML generation code.
 1.13 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.12 08-Feb-2002  ross Generate <>& symbolically. I'm avoiding .../dist/... directories for now.
 1.11 20-Dec-2001  wiz Punctuation nit.
 1.10 20-Dec-2001  wiz Whitespace nits.
 1.9 22-Mar-1999  garbled More and more .Os cleanups. .Os is defined in the tmac.doc-common file,
so we shouldn't override it with versions in the manpages. Many more to
come.
 1.8 16-Oct-1996  christos PR/2808: Use .Nm macro properly. (from FreeBSD)
 1.7 21-Mar-1995  cgd branches: 1.7.6;
convert to new RCS id conventions.
 1.6 11-Jun-1994  mycroft Add RCS ids.
 1.5 11-May-1994  jtc sync with 4.4lite
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.7.6.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.14.6.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.14.6.1 21-Apr-2020  martin Sync with HEAD
 1.15 18-May-2021  kre Fix a bug in the built-in echo in /bin/sh reported in private mail by
Oguz <oguzismailuysal@gmail.com>

If echo detects an I/O error, it does exit(1) (that's fine) but then
the next echo also does exit(1) even without any errors of its own,
and every following echo writing to stdout does the same thing.

eg:

echo foo >&- ; echo $?; echo $?; ( echo $( echo $?; echo $?) ; echo $? )
1
1
1 1
1

The first echo writes nothing (stdout is closed) but does exit(1).
The second echo writes "1" (correct, the exit status of the previous
echo) and should exit(0) - but doesn't. This pattern continues...

While here, conform to the POSIX requirement on echo (and many other
standard utilities, but definitely not all) that when the utility
does exit(>0) a message must be written to stderr (and vice versa
in many cases). Our echo (as shown above) did the exit(1) part
when it detected the I/O error, but no message is sent to stderr.
Fix that while we're here.

Similar changes are required for /bin/echo (coming soon), and
/usr/bin/printf (which is also the sh builtin printf) - except
currently that one kind of conforms, as it ignores errors writing
to stdout (as do large numbers of other utilities). For many
programs that's kind of acceptable, but where the sole purpose of
the program is to write to stdout, it really isn't. Also to be
fixed soon.
 1.14 12-Oct-2008  dholland branches: 1.14.62; 1.14.68;
output.c output.h: expose OUTPUT_ERR (flag for an exposed flags variable)
bltin.h: support ferror()
echo.c: use ferror() to fail on output write errors

Another piece of PR bin/39574.
 1.13 12-Dec-2007  lukem use __RCSID()
 1.12 06-Feb-2005  perry branches: 1.12.10; 1.12.16;
remove obsolete register declarations
 1.11 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.10 04-Jan-2003  wiz Spell output with two ts.
 1.9 24-Nov-2002  christos Fixes from David Laight:
- ansification
- format of output of jobs command (etc)
- job identiers %+, %- etc
- $? and $(...)
- correct quoting of output of set, export -p and readonly -p
- differentiation between nornal and 'posix special' builtins
- correct behaviour (posix) for errors on builtins and special builtins
- builtin printf and kill
- set -o debug (if compiled with DEBUG)
- cd src obj (as ksh - too useful to do without)
- unset -e name, remove non-readonly variable from export list.
(so I could unset -e PS1 before running the test shell...)
 1.8 02-Nov-1996  christos Fix problems that gcc -Wall found (from Todd Miller, OpenBSD)
 1.7 21-Mar-1995  cgd branches: 1.7.6;
convert to new RCS id conventions.
 1.6 11-Jun-1994  mycroft Add RCS ids.
 1.5 11-May-1994  jtc sync with 4.4lite
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.7.6.1 26-Jan-1997  rat Update /bin/sh from trunk per request of Christos Zoulas. Fixes
many bugs.
 1.12.16.1 09-Jan-2008  matt sync with HEAD
 1.12.10.1 18-Nov-2008  bouyer Pull up following revision(s) (requested by dholland in ticket #1232):
bin/echo/echo.c: revision 1.18
bin/sh/bltin/bltin.h: revision 1.13
bin/sh/bltin/echo.c: revision 1.14
bin/sh/output.c: revision 1.30
bin/sh/output.h: revision 1.19
Check ferror(stdout) and exit(1) if there's been a problem.
Fixes PR bin/39574 from Ed Ravin, but with a simpler patch.
output.c output.h: expose OUTPUT_ERR (flag for an exposed flags variable)
bltin.h: support ferror()
echo.c: use ferror() to fail on output write errors
Another piece of PR bin/39574.
 1.14.68.1 31-May-2021  cjep sync with head
 1.14.62.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.14.62.1 21-Apr-2020  martin Sync with HEAD
 1.9 09-Apr-2025  kre Modernise these (unused) functions

Much of what is here is truly ancient. It isn't used as
part of the build (never installed anywhere) so are only
available from src/bin/sh/funcs which means that in practice
no-one ever sees or uses any of this.

A notable example is the provided 'suspend' function, which
has been using "set +j" to turn off job control all this time.
That was the old (original ash) name for what is now the 'm'
option ... it was changed (by Berkeley CSRG) in July 1992
(approaching 33 years ago), which means the suspend function
provided here hasn't been used in at least 33 years.
(Since "suspend" is now provided as a sh builtin, the function
here is no real use anyway).

Still, since they exist here, they may as well be made to work
properly.

Note: should anyone decide to install these somewhere, the
"pushd" "popd" and "dirs" scripts are all the same thing,
and (while they could be installed as 3 files) should really
be just one file, linked to 3 names, when installed - it
makes no practical difference which of them gets copied rather
than just being a link.
 1.8 29-Feb-2016  christos branches: 1.8.16; 1.8.28;
delete clauses 3,4
 1.7 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 11-Jun-1994  mycroft Add RCS ids.
 1.4 11-May-1994  jtc sync with 4.4lite
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.8.28.1 02-Aug-2025  perseant Sync with HEAD
 1.8.16.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.8.16.1 21-Apr-2020  martin Sync with HEAD
 1.10 09-Apr-2025  kre Modernise these (unused) functions

Much of what is here is truly ancient. It isn't used as
part of the build (never installed anywhere) so are only
available from src/bin/sh/funcs which means that in practice
no-one ever sees or uses any of this.

A notable example is the provided 'suspend' function, which
has been using "set +j" to turn off job control all this time.
That was the old (original ash) name for what is now the 'm'
option ... it was changed (by Berkeley CSRG) in July 1992
(approaching 33 years ago), which means the suspend function
provided here hasn't been used in at least 33 years.
(Since "suspend" is now provided as a sh builtin, the function
here is no real use anyway).

Still, since they exist here, they may as well be made to work
properly.

Note: should anyone decide to install these somewhere, the
"pushd" "popd" and "dirs" scripts are all the same thing,
and (while they could be installed as 3 files) should really
be just one file, linked to 3 names, when installed - it
makes no practical difference which of them gets copied rather
than just being a link.
 1.9 07-Apr-2023  kre branches: 1.9.2;

The great shell trailing whitespace cleanup of 2023...
Inspired by private e-mail comments from mouse@

NFCI.
 1.8 29-Feb-2016  christos branches: 1.8.16;
delete clauses 3,4
 1.7 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 11-Jun-1994  mycroft Add RCS ids.
 1.4 11-May-1994  jtc sync with 4.4lite
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.8.16.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.8.16.1 21-Apr-2020  martin Sync with HEAD
 1.9.2.1 02-Aug-2025  perseant Sync with HEAD
 1.8 29-Feb-2016  christos branches: 1.8.16;
delete clauses 3,4
 1.7 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 11-Jun-1994  mycroft Add RCS ids.
 1.4 11-May-1994  jtc sync with 4.4lite
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.8.16.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.8.16.1 21-Apr-2020  martin Sync with HEAD
 1.9 09-Apr-2025  kre Modernise these (unused) functions

Much of what is here is truly ancient. It isn't used as
part of the build (never installed anywhere) so are only
available from src/bin/sh/funcs which means that in practice
no-one ever sees or uses any of this.

A notable example is the provided 'suspend' function, which
has been using "set +j" to turn off job control all this time.
That was the old (original ash) name for what is now the 'm'
option ... it was changed (by Berkeley CSRG) in July 1992
(approaching 33 years ago), which means the suspend function
provided here hasn't been used in at least 33 years.
(Since "suspend" is now provided as a sh builtin, the function
here is no real use anyway).

Still, since they exist here, they may as well be made to work
properly.

Note: should anyone decide to install these somewhere, the
"pushd" "popd" and "dirs" scripts are all the same thing,
and (while they could be installed as 3 files) should really
be just one file, linked to 3 names, when installed - it
makes no practical difference which of them gets copied rather
than just being a link.
 1.8 29-Feb-2016  christos branches: 1.8.16; 1.8.28;
delete clauses 3,4
 1.7 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 11-Jun-1994  mycroft Add RCS ids.
 1.4 11-May-1994  jtc sync with 4.4lite
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.8.28.1 02-Aug-2025  perseant Sync with HEAD
 1.8.16.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.8.16.1 21-Apr-2020  martin Sync with HEAD
 1.9 09-Apr-2025  kre Modernise these (unused) functions

Much of what is here is truly ancient. It isn't used as
part of the build (never installed anywhere) so are only
available from src/bin/sh/funcs which means that in practice
no-one ever sees or uses any of this.

A notable example is the provided 'suspend' function, which
has been using "set +j" to turn off job control all this time.
That was the old (original ash) name for what is now the 'm'
option ... it was changed (by Berkeley CSRG) in July 1992
(approaching 33 years ago), which means the suspend function
provided here hasn't been used in at least 33 years.
(Since "suspend" is now provided as a sh builtin, the function
here is no real use anyway).

Still, since they exist here, they may as well be made to work
properly.

Note: should anyone decide to install these somewhere, the
"pushd" "popd" and "dirs" scripts are all the same thing,
and (while they could be installed as 3 files) should really
be just one file, linked to 3 names, when installed - it
makes no practical difference which of them gets copied rather
than just being a link.
 1.8 29-Feb-2016  christos branches: 1.8.16; 1.8.28;
delete clauses 3,4
 1.7 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 11-Jun-1994  mycroft Add RCS ids.
 1.4 11-May-1994  jtc sync with 4.4lite
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.8.28.1 02-Aug-2025  perseant Sync with HEAD
 1.8.16.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.8.16.1 21-Apr-2020  martin Sync with HEAD
 1.10 09-Apr-2025  kre Modernise these (unused) functions

Much of what is here is truly ancient. It isn't used as
part of the build (never installed anywhere) so are only
available from src/bin/sh/funcs which means that in practice
no-one ever sees or uses any of this.

A notable example is the provided 'suspend' function, which
has been using "set +j" to turn off job control all this time.
That was the old (original ash) name for what is now the 'm'
option ... it was changed (by Berkeley CSRG) in July 1992
(approaching 33 years ago), which means the suspend function
provided here hasn't been used in at least 33 years.
(Since "suspend" is now provided as a sh builtin, the function
here is no real use anyway).

Still, since they exist here, they may as well be made to work
properly.

Note: should anyone decide to install these somewhere, the
"pushd" "popd" and "dirs" scripts are all the same thing,
and (while they could be installed as 3 files) should really
be just one file, linked to 3 names, when installed - it
makes no practical difference which of them gets copied rather
than just being a link.
 1.9 07-Apr-2023  kre branches: 1.9.2;

The great shell trailing whitespace cleanup of 2023...
Inspired by private e-mail comments from mouse@

NFCI.
 1.8 29-Feb-2016  christos branches: 1.8.16;
delete clauses 3,4
 1.7 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 11-Jun-1994  mycroft Add RCS ids.
 1.4 11-May-1994  jtc sync with 4.4lite
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.8.16.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.8.16.1 21-Apr-2020  martin Sync with HEAD
 1.9.2.1 02-Aug-2025  perseant Sync with HEAD
 1.10 09-Apr-2025  kre Modernise these (unused) functions

Much of what is here is truly ancient. It isn't used as
part of the build (never installed anywhere) so are only
available from src/bin/sh/funcs which means that in practice
no-one ever sees or uses any of this.

A notable example is the provided 'suspend' function, which
has been using "set +j" to turn off job control all this time.
That was the old (original ash) name for what is now the 'm'
option ... it was changed (by Berkeley CSRG) in July 1992
(approaching 33 years ago), which means the suspend function
provided here hasn't been used in at least 33 years.
(Since "suspend" is now provided as a sh builtin, the function
here is no real use anyway).

Still, since they exist here, they may as well be made to work
properly.

Note: should anyone decide to install these somewhere, the
"pushd" "popd" and "dirs" scripts are all the same thing,
and (while they could be installed as 3 files) should really
be just one file, linked to 3 names, when installed - it
makes no practical difference which of them gets copied rather
than just being a link.
 1.9 07-Apr-2023  kre branches: 1.9.2;

The great shell trailing whitespace cleanup of 2023...
Inspired by private e-mail comments from mouse@

NFCI.
 1.8 29-Feb-2016  christos branches: 1.8.16;
delete clauses 3,4
 1.7 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 11-Jun-1994  mycroft Add RCS ids.
 1.4 11-May-1994  jtc sync with 4.4lite
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.8.16.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.8.16.1 21-Apr-2020  martin Sync with HEAD
 1.9.2.1 02-Aug-2025  perseant Sync with HEAD
 1.9 09-Apr-2025  kre Modernise these (unused) functions

Much of what is here is truly ancient. It isn't used as
part of the build (never installed anywhere) so are only
available from src/bin/sh/funcs which means that in practice
no-one ever sees or uses any of this.

A notable example is the provided 'suspend' function, which
has been using "set +j" to turn off job control all this time.
That was the old (original ash) name for what is now the 'm'
option ... it was changed (by Berkeley CSRG) in July 1992
(approaching 33 years ago), which means the suspend function
provided here hasn't been used in at least 33 years.
(Since "suspend" is now provided as a sh builtin, the function
here is no real use anyway).

Still, since they exist here, they may as well be made to work
properly.

Note: should anyone decide to install these somewhere, the
"pushd" "popd" and "dirs" scripts are all the same thing,
and (while they could be installed as 3 files) should really
be just one file, linked to 3 names, when installed - it
makes no practical difference which of them gets copied rather
than just being a link.
 1.8 29-Feb-2016  christos branches: 1.8.16; 1.8.28;
delete clauses 3,4
 1.7 11-May-1995  christos Merge in my changes from vangogh, and fix the x=`false`; echo $? == 0
bug.
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 11-Jun-1994  mycroft Add RCS ids.
 1.4 11-May-1994  jtc sync with 4.4lite
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 11-May-1994  jtc 44lite code
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.8.28.1 02-Aug-2025  perseant Sync with HEAD
 1.8.16.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.8.16.1 21-Apr-2020  martin Sync with HEAD
 1.9 04-Aug-1997  perry branches: 1.9.112;
Make sleep handle non-integer values. Document the same, noting that
the extension is grotesquely non-portable.

As requested by Denny Gentry in pr-3914. He supplied some code, but I
used all my own code in the change, and wrote documentation for the
man page.

This is creeping featurism at its worst. I added it only because I
can't see a good reason for refusing. However, I'm disgusted with
myself for doing it anyway.
 1.8 20-Jul-1997  christos Remove WARNS=1 from all the subdirectory Makefiles, and add it to Makefile.inc
now that all /bin has been cleaned.
 1.7 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.9.112.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.9.112.1 21-Apr-2020  martin Sync with HEAD
 1.28 19-Feb-2023  uwe sleep(1): minor markup tweaks
 1.27 27-Jan-2019  wiz Sort sections.
 1.26 26-Jan-2019  kre 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.
 1.25 26-Jan-2019  kre 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.
 1.24 03-Jul-2017  wiz branches: 1.24.4; 1.24.6;
Remove workaround for ancient HTML generation code.
 1.23 12-Aug-2016  sevan Document the version sleep first appeared.
Bump date.
 1.22 15-Aug-2011  wiz Improve wording.
From Snader_LB.
 1.21 09-Oct-2010  wiz Bump date for SIGINFO.
 1.20 09-Oct-2010  mrg add SIGINFO support; from freebsd:

when a SIGINFO is delivered, display the approximate remaining seconds.
 1.19 18-Aug-2007  hubertf - Cleanup for sleep.c:
+ mark two functions as static
+ remove case '?' in switch() before default
+ use return instead of exit() in main() function
+ use constants EXIT_SUCCESS/EXIT_FAILURE instead of 0/1

- In man sleep(1):
+ cleanup example

Patch submitted by Slava Semushin <php-coder@altlinux.ru> in private email.
 1.18 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.17 21-Nov-2002  kim Use shell glob expansion in the csh example, instead of ls with glob.
Reported by Jerry Peek <jpeek@jpeek.com>.
 1.16 25-Sep-2002  wiz New policy: New sentences start on a new line.
Patches by Robert Elz <kre at munnari oz au>, with minimal changes by me.
 1.15 08-Feb-2002  ross Generate <>& symbolically. I'm avoiding .../dist/... directories for now.
 1.14 20-Dec-2001  wiz Sort SEE ALSO.
 1.13 04-Sep-2000  kleink For commands and utilities, use EXIT STATUS rather than RETURN VALUES as
appropriate (and documented in mdoc(7)).
 1.12 28-Aug-2000  hubertf Add 'RETURN VALUE' section header.
 1.11 14-Sep-1997  lukem branches: 1.11.12;
- getopt returns -1 not EOF
- use .Nm appropriately
 1.10 04-Aug-1997  perry Make sleep handle non-integer values. Document the same, noting that
the extension is grotesquely non-portable.

As requested by Denny Gentry in pr-3914. He supplied some code, but I
used all my own code in the change, and wrote documentation for the
man page.

This is creeping featurism at its worst. I added it only because I
can't see a good reason for refusing. However, I'm disgusted with
myself for doing it anyway.
 1.9 25-Jul-1995  jtc Use "utility" instead of "command". Modern definitions of these terms
are distinct (See POSIX.2 glossary).

A utility is a executable, script or shell builtin; while a command
can be any of those things plus lists, pipelines, compound commands
(if, for, while) and shell function definitions.
 1.8 21-Mar-1995  cgd convert to new RCS id conventions.
 1.7 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.6 11-Jan-1994  jtc Fix spelling errors.
 1.5 31-Dec-1993  jtc localization & manpage update.
 1.4 01-Aug-1993  mycroft Add RCS indentifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.11.12.1 28-Aug-2000  hubertf Pull up to netbsd-1-5 branch, OK'd by thorpej:

Log Message:
> Add 'RETURN VALUE' section header.

Files & Revisionis:
> cvs rdiff -r1.19 -r1.20 basesrc/bin/cat/cat.1
> cvs rdiff -r1.12 -r1.13 basesrc/bin/chmod/chmod.1
> cvs rdiff -r1.14 -r1.15 basesrc/bin/cp/cp.1
> cvs rdiff -r1.8 -r1.9 basesrc/bin/dd/dd.1
> cvs rdiff -r1.9 -r1.10 basesrc/bin/echo/echo.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/expr/expr.1
> cvs rdiff -r1.25 -r1.26 basesrc/bin/ls/ls.1
> cvs rdiff -r1.10 -r1.11 basesrc/bin/mkdir/mkdir.1
> cvs rdiff -r1.23 -r1.24 basesrc/bin/mt/mt.1
> cvs rdiff -r1.12 -r1.13 basesrc/bin/mv/mv.1
> cvs rdiff -r1.16 -r1.17 basesrc/bin/pwd/pwd.1
> cvs rdiff -r1.9 -r1.10 basesrc/bin/rm/rm.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/rmdir/rmdir.1
> cvs rdiff -r1.35 -r1.36 basesrc/bin/sh/sh.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/sleep/sleep.1
> cvs rdiff -r1.20 -r1.21 basesrc/bin/stty/stty.1
 1.24.6.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.24.6.2 21-Apr-2020  martin Sync with HEAD
 1.24.6.1 10-Jun-2019  christos Sync with HEAD
 1.24.4.1 26-Jan-2019  pgoyette Sync with HEAD
 1.30 10-Mar-2019  kre 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.
 1.29 27-Jan-2019  christos cast to intmax_t instead of long, since time_t is "long long"
 1.28 26-Jan-2019  martin Explicitly cast time_t to match format string - should fix the build on
some 32bit architectures.
 1.27 26-Jan-2019  kre 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.
 1.26 26-Jan-2019  kre 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.
 1.25 19-Jan-2019  kre 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).
 1.24 29-Aug-2011  joerg branches: 1.24.36; 1.24.42; 1.24.44;
Use __dead
 1.23 09-Oct-2010  mrg add SIGINFO support; from freebsd:

when a SIGINFO is delivered, display the approximate remaining seconds.
 1.22 20-Jul-2008  lukem Remove the \n and tabs from the __COPYRIGHT() strings.
 1.21 18-Aug-2007  hubertf branches: 1.21.10;
- Cleanup for sleep.c:
+ mark two functions as static
+ remove case '?' in switch() before default
+ use return instead of exit() in main() function
+ use constants EXIT_SUCCESS/EXIT_FAILURE instead of 0/1

- In man sleep(1):
+ cleanup example

Patch submitted by Slava Semushin <php-coder@altlinux.ru> in private email.
 1.20 17-Oct-2005  elad PR/27140: "sleep time" doesn't error out.
Apply fix from Dheeraj Reddy.
 1.19 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.18 12-Nov-2001  christos PR/14558: Tero Kivinen: Check the return value of nanosleep and don't fail
silently.
 1.17 16-Sep-2001  wiz ANSIfy and KNF by Petri Koistinen.
 1.16 04-Nov-1998  christos char -> unsigned char
 1.15 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.14 28-Jul-1998  mycroft Delint.
 1.13 09-Apr-1998  fair remove #include <sys/time.h> ; the include file bug noted in the comment above it has been fixed.
 1.12 05-Nov-1997  cgd lint
 1.11 14-Sep-1997  lukem - getopt returns -1 not EOF
- use .Nm appropriately
 1.10 04-Aug-1997  perry Make sleep handle non-integer values. Document the same, noting that
the extension is grotesquely non-portable.

As requested by Denny Gentry in pr-3914. He supplied some code, but I
used all my own code in the change, and wrote documentation for the
man page.

This is creeping featurism at its worst. I added it only because I
can't see a good reason for refusing. However, I'm disgusted with
myself for doing it anyway.
 1.9 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.8 21-Mar-1995  cgd convert to new RCS id conventions.
 1.7 22-Sep-1994  mycroft Merge 4.4-Lite version.
 1.6 31-Dec-1993  jtc localization & manpage update.
 1.5 10-Sep-1993  jtc Quiet compiler warnings by explicitly declaring return value of main().
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 22-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.21.10.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.24.44.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.24.44.2 21-Apr-2020  martin Sync with HEAD
 1.24.44.1 10-Jun-2019  christos Sync with HEAD
 1.24.42.1 26-Jan-2019  pgoyette Sync with HEAD
 1.24.36.1 07-Mar-2019  martin Pull up following revision(s) (requested by kre in ticket #1169):

bin/sleep/sleep.c: revision 1.25

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).
 1.11 04-Apr-2012  joerg branches: 1.11.34;
Disable new -Wstring-plus-int warning where needed for now.
Adjust various places that add GCC-only options to check for the active
compiler first.
 1.10 20-Jul-1997  christos branches: 1.10.68;
Remove WARNS=1 from all the subdirectory Makefiles, and add it to Makefile.inc
now that all /bin has been cleaned.
 1.9 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.8 21-Mar-1995  cgd convert to new RCS id conventions.
 1.7 20-Sep-1994  mycroft branches: 1.7.2;
Merge with 4.4-Lite version.
 1.6 23-Mar-1994  mycroft Add VMIN and VTIME support, per Keith Bostic. Use libc err*() and warn*(),
and make usage() a function.
 1.5 01-Aug-1993  mycroft Add RCS identifiers.
 1.4 01-Jun-1993  cgd rcsid cleanup
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.7.2.2 20-Sep-1994  mycroft Merge with 4.4-Lite version.
 1.7.2.1 20-Sep-1994  mycroft file Makefile was added on branch netbsd-1-0 on 1994-09-20 04:52:03 +0000
 1.10.68.1 17-Apr-2012  yamt sync with head
 1.11.34.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.11.34.1 21-Apr-2020  martin Sync with HEAD
 1.16 16-Oct-2006  christos branches: 1.16.84;
c99 initializers
 1.15 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.14 16-Jun-2003  perry ANSIfy, from Petri Koistinen in PR 13975
 1.13 28-Jul-1998  mycroft Oops; the old BSD aliases were not actually used.
 1.12 28-Jul-1998  mycroft Delint.
 1.11 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.10 07-May-1996  jtc Use const qualifier as appropriate
 1.9 21-Mar-1995  cgd convert to new RCS id conventions.
 1.8 20-Sep-1994  mycroft branches: 1.8.2;
Merge with 4.4-Lite version.
 1.7 23-Mar-1994  mycroft Add VMIN and VTIME support, per Keith Bostic. Use libc err*() and warn*(),
and make usage() a function.
 1.6 10-Dec-1993  cgd fix from Piero Serini <piero@strider.st.dsi.unimi.it> so that
"stty eof undef" etc. works
 1.5 01-Aug-1993  mycroft Add RCS identifiers.
 1.4 01-Jun-1993  cgd rcsid cleanup
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.8.2.2 20-Sep-1994  mycroft Merge with 4.4-Lite version.
 1.8.2.1 20-Sep-1994  mycroft file cchar.c was added on branch netbsd-1-0 on 1994-09-20 04:52:04 +0000
 1.16.84.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.16.84.1 21-Apr-2020  martin Sync with HEAD
 1.13 12-Sep-2013  christos branches: 1.13.26;
- print the line discipline using the new ioctl
- print the queue size
 1.12 29-Aug-2011  joerg branches: 1.12.2; 1.12.8;
Use __dead
 1.11 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.10 16-Jun-2003  perry ANSIfy, from Petri Koistinen in PR 13975
 1.9 28-Jul-1998  mycroft Delint.
 1.8 07-May-1996  jtc Use const qualifier as appropriate
 1.7 21-Mar-1995  cgd convert to new RCS id conventions.
 1.6 20-Sep-1994  mycroft branches: 1.6.2;
Merge with 4.4-Lite version.
 1.5 23-Mar-1994  mycroft Add VMIN and VTIME support, per Keith Bostic. Use libc err*() and warn*(),
and make usage() a function.
 1.4 01-Jun-1993  cgd rcsid cleanup
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.6.2.2 20-Sep-1994  mycroft Merge with 4.4-Lite version.
 1.6.2.1 20-Sep-1994  mycroft file extern.h was added on branch netbsd-1-0 on 1994-09-20 04:52:05 +0000
 1.12.8.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.12.2.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.13.26.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.13.26.1 21-Apr-2020  martin Sync with HEAD
 1.17 29-Aug-2011  joerg branches: 1.17.44;
Use __dead
 1.16 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.15 16-Jun-2003  perry ANSIfy, from Petri Koistinen in PR 13975
 1.14 10-Jun-1999  mrg pulling param.h not types.h to get BSD4_4. from NAKAJIMA Yoshihiro <nakayosh@kcn.ne.jp> in PR#7751
 1.13 02-Mar-1999  christos branches: 1.13.2;
- char -> unsigned char
- #ifdef BSD4_4 for for non POSIX features
- prefix with STTY_ all the FMT constants to avoid namespace collisions.
 1.12 28-Jul-1998  mycroft Delint.
 1.11 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.10 07-May-1996  jtc Use const qualifier as appropriate
 1.9 21-Mar-1995  cgd convert to new RCS id conventions.
 1.8 20-Sep-1994  mycroft branches: 1.8.2;
Merge with 4.4-Lite version.
 1.7 23-Mar-1994  mycroft Simplify gfmt parsing.
 1.6 23-Mar-1994  mycroft Add VMIN and VTIME support, per Keith Bostic. Use libc err*() and warn*(),
and make usage() a function.
 1.5 01-Aug-1993  mycroft Add RCS identifiers.
 1.4 01-Jun-1993  cgd rcsid cleanup
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.8.2.2 20-Sep-1994  mycroft Merge with 4.4-Lite version.
 1.8.2.1 20-Sep-1994  mycroft file gfmt.c was added on branch netbsd-1-0 on 1994-09-20 04:52:06 +0000
 1.13.2.1 22-Feb-2000  he Pull up revision 1.14 (requested by dmcmahill):
Make stty accept g-format data. Fixes PR#7751.
 1.17.44.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.17.44.1 21-Apr-2020  martin Sync with HEAD
 1.22 10-Jan-2017  christos branches: 1.22.14;
need <time.h> for time(3)
 1.21 12-Sep-2013  christos branches: 1.21.8;
- print the line discipline using the new ioctl
- print the queue size
 1.20 01-Apr-2004  tsarna branches: 1.20.48; 1.20.54;
Fix longstanding gap in stty functionality: while many operations had
inverses, "sane" didn't. Now there is symmetry.
 1.19 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.18 16-Jun-2003  perry ANSIfy, from Petri Koistinen in PR 13975
 1.17 02-Mar-1999  christos s/BSD/STTY_BSD/ where I missed it before.
 1.16 02-Mar-1999  christos - char -> unsigned char
- #ifdef BSD4_4 for for non POSIX features
- prefix with STTY_ all the FMT constants to avoid namespace collisions.
 1.15 13-Jan-1999  sommerfe Apply patch as found in PR1892
 1.14 28-Jul-1998  mycroft Delint.
 1.13 20-Oct-1997  scottr Add support for DTR/CTS flow control, from Bill Studenmund.
 1.12 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.11 07-Sep-1995  jtc Sync with 4.4lite2
 1.10 21-Mar-1995  cgd convert to new RCS id conventions.
 1.9 20-Sep-1994  mycroft branches: 1.9.2;
Merge with 4.4-Lite version.
 1.8 23-Mar-1994  mycroft Add VMIN and VTIME support, per Keith Bostic. Use libc err*() and warn*(),
and make usage() a function.
 1.7 13-Mar-1994  cgd don't clobber CRTSCTS when 'stty sane'
 1.6 01-Aug-1993  mycroft Add RCS identifiers.
 1.5 01-Jun-1993  cgd rcsid cleanup
 1.4 01-Jun-1993  cgd "ispeed" thought it didn't need an argument... WRONG!!!
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.3 07-Sep-1995  jtc imported from 44lite2
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.9.2.2 20-Sep-1994  mycroft Merge with 4.4-Lite version.
 1.9.2.1 20-Sep-1994  mycroft file key.c was added on branch netbsd-1-0 on 1994-09-20 04:52:07 +0000
 1.20.54.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.20.48.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.21.8.1 20-Mar-2017  pgoyette Sync with HEAD
 1.22.14.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.22.14.1 21-Apr-2020  martin Sync with HEAD
 1.18 01-May-2015  christos branches: 1.18.16;
Simplify by splitting the "simple" mode setting and the "special" ones.
 1.17 16-Oct-2006  christos c99 initializers
 1.16 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.15 16-Jun-2003  perry ANSIfy, from Petri Koistinen in PR 13975
 1.14 28-Jul-1998  mycroft Delint.
 1.13 13-Feb-1998  kleink Add ONOCR and ONLRET output modes, from XPG4.2.
 1.12 20-Oct-1997  scottr Add support for DTR/CTS flow control, from Bill Studenmund.
 1.11 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.10 02-Apr-1997  kleink Implement OCRNL "\r" -> "\n" tty output translation.
Fixes PR standards/3434.
 1.9 07-May-1996  jtc Use const qualifier as appropriate
 1.8 21-Mar-1995  cgd convert to new RCS id conventions.
 1.7 20-Sep-1994  mycroft branches: 1.7.2;
Merge with 4.4-Lite version.
 1.6 12-Apr-1994  cgd put MDMBUF in the right place.
 1.5 01-Aug-1993  mycroft Add RCS identifiers.
 1.4 01-Jun-1993  cgd rcsid cleanup
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.7.2.2 20-Sep-1994  mycroft Merge with 4.4-Lite version.
 1.7.2.1 20-Sep-1994  mycroft file modes.c was added on branch netbsd-1-0 on 1994-09-20 04:52:08 +0000
 1.18.16.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.18.16.1 21-Apr-2020  martin Sync with HEAD
 1.23 12-Sep-2013  christos branches: 1.23.26;
- print the line discipline using the new ioctl
- print the queue size
 1.22 26-Jun-2005  christos branches: 1.22.42; 1.22.48;
sprinkle a little const, and now everything compiles with WARNS=3
 1.21 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.20 16-Jun-2003  perry ANSIfy, from Petri Koistinen in PR 13975
 1.19 02-Mar-1999  christos - char -> unsigned char
- #ifdef BSD4_4 for for non POSIX features
- prefix with STTY_ all the FMT constants to avoid namespace collisions.
 1.18 27-Jul-1998  mycroft Slight code reduction.
 1.17 27-Jul-1998  mycroft __AUDIT__ cleanup.
 1.16 13-Feb-1998  kleink Add ONOCR and ONLRET output modes, from XPG4.2.
 1.15 13-Feb-1998  kleink Recognize STRIPDISC.
 1.14 20-Oct-1997  scottr Add support for DTR/CTS flow control, from Bill Studenmund.
 1.13 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.12 02-Apr-1997  kleink Implement OCRNL "\r" -> "\n" tty output translation.
Fixes PR standards/3434.
 1.11 07-May-1996  jtc Use const qualifier as appropriate
 1.10 21-Mar-1995  cgd convert to new RCS id conventions.
 1.9 20-Sep-1994  mycroft branches: 1.9.2;
Merge with 4.4-Lite version.
 1.8 12-Apr-1994  cgd put MDMBUF in the right place.
 1.7 23-Mar-1994  mycroft Add VMIN and VTIME support, per Keith Bostic. Use libc err*() and warn*(),
and make usage() a function.
 1.6 18-Aug-1993  brezak Identify the PPPDISC on a line.
 1.5 01-Aug-1993  mycroft Add RCS identifiers.
 1.4 01-Jun-1993  cgd rcsid cleanup
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.9.2.2 20-Sep-1994  mycroft Merge with 4.4-Lite version.
 1.9.2.1 20-Sep-1994  mycroft file print.c was added on branch netbsd-1-0 on 1994-09-20 04:52:10 +0000
 1.22.48.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.22.42.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.23.26.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.23.26.1 21-Apr-2020  martin Sync with HEAD
 1.46 14-May-2022  dholland Clarify that "stty 0" will normally hang up the tty. Bump date.
 1.45 30-Oct-2017  wiz branches: 1.45.4;
Remove Tn.
 1.44 25-Oct-2017  kre 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)
 1.43 14-Aug-2016  sevan While the v2 source and man pages are incomplete, TUHS has a scanned copy of the
printed version of the 2nd edition manual. stty is featured in this copy, in the
table of contents listed as meaning "set typewriter modes" and on the actual manual page
headed correctly as "set teletype options"
http://www.tuhs.org/Archive/PDP-11/Distributions/research/1972_stuff/unix_2nd_edition_manual.pdf
Ammend HISTORY and bump date.
 1.42 12-Aug-2016  sevan Document the version stty first appeared.
Bump date.
 1.41 20-Jun-2012  wiz Remove another Pf. From Bug Hunting.
 1.40 20-Jun-2012  wiz - improve wording, and create more consistency within the text;
- correct `SYNOPSIS';
- improve macro usage;
- mention argument name for `-f' (and change list width for it);
- correct misplacement of periods (`.') and a parenthese (`(');
- fix typo;
- bump date.

From Bug Hunting.
 1.39 05-Apr-2010  joerg branches: 1.39.6; 1.39.8;
\\ -> \e
 1.38 14-Dec-2009  wiz Bump date for previous.
 1.37 14-Dec-2009  dholland Document "stty insane" properly. Closes PR bin/25024.
 1.36 13-May-2009  wiz Reduce Pf/Ns usage.
 1.35 15-Mar-2009  joerg Fix markup.
 1.34 24-Jun-2007  christos branches: 1.34.20;
PR/36536: Greg A. Woods: stty(1) should mention tty(4)
 1.33 03-Sep-2006  wiz Bump date for previous.
 1.32 02-Sep-2006  itohy Add description on "discard" character.
Sort control characters by the Vxxx numbers in termios.h.
 1.31 01-Apr-2004  tsarna Fix longstanding gap in stty functionality: while many operations had
inverses, "sane" didn't. Now there is symmetry.
 1.30 13-Feb-2004  wiz Uppercase CPU.
 1.29 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.28 01-Jul-2003  wiz Cm Fl -> Fl.
 1.27 30-Mar-2003  wiz PDP-11 instead of PDP 11.
 1.26 29-Mar-2003  perry Pdp 11 -> PDP 11, from Igor Sobrado in PR 19670
 1.25 25-Sep-2002  wiz New policy: New sentences start on a new line.
Patches by Robert Elz <kre at munnari oz au>, with minimal changes by me.
 1.24 08-Feb-2002  ross Generate <>& symbolically. I'm avoiding .../dist/... directories for now.
 1.23 20-Dec-2001  wiz Punctuation nits, use Bd -literal instead of .nf, drop unnecessary .Pps.
 1.22 04-Sep-2000  kleink For commands and utilities, use EXIT STATUS rather than RETURN VALUES as
appropriate (and documented in mdoc(7)).
 1.21 28-Aug-2000  hubertf Add 'RETURN VALUE' section header.
 1.20 24-Mar-1999  mycroft branches: 1.20.10;
Remove spurious .ne's.
 1.19 13-Jan-1999  sommerfe Apply patch as found in PR1892
 1.18 13-Feb-1998  kleink Add ONOCR and ONLRET output modes, from XPG4.2.
 1.17 06-Feb-1998  perry macroize BSD, NetBSD, FreeBSD and misc cleanup
 1.16 20-Oct-1997  enami Fix .Nm usage.
 1.15 20-Oct-1997  scottr Add support for DTR/CTS flow control, from Bill Studenmund.
 1.14 02-Apr-1997  kleink Implement OCRNL "\r" -> "\n" tty output translation.
Fixes PR standards/3434.
 1.13 13-Mar-1997  mikel tabs is the converse of oxtabs, not itself. pointed out by Geoff Wing
in PR misc/3320.
 1.12 08-Mar-1997  mouse alternate -> alternative, per PR 2643
 1.11 11-Nov-1996  lukem fix the the typos
 1.10 07-Sep-1995  jtc Sync with 4.4lite2
 1.9 18-Aug-1995  pk .DV => .Dv (from Mark Weaver, PR#1370).
 1.8 25-Jul-1995  jtc Use "utility" instead of "command". Modern definitions of these terms
are distinct (See POSIX.2 glossary).

A utility is a executable, script or shell builtin; while a command
can be any of those things plus lists, pipelines, compound commands
(if, for, while) and shell function definitions.
 1.7 21-Mar-1995  cgd convert to new RCS id conventions.
 1.6 20-Sep-1994  mycroft branches: 1.6.2;
Merge with 4.4-Lite version.
 1.5 01-Aug-1993  mycroft Add RCS indentifiers.
 1.4 01-Jun-1993  cgd rcsid cleanup
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.3 07-Sep-1995  jtc imported from 44lite2
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.6.2.2 20-Sep-1994  mycroft Merge with 4.4-Lite version.
 1.6.2.1 20-Sep-1994  mycroft file stty.1 was added on branch netbsd-1-0 on 1994-09-20 04:52:12 +0000
 1.20.10.1 28-Aug-2000  hubertf Pull up to netbsd-1-5 branch, OK'd by thorpej:

Log Message:
> Add 'RETURN VALUE' section header.

Files & Revisionis:
> cvs rdiff -r1.19 -r1.20 basesrc/bin/cat/cat.1
> cvs rdiff -r1.12 -r1.13 basesrc/bin/chmod/chmod.1
> cvs rdiff -r1.14 -r1.15 basesrc/bin/cp/cp.1
> cvs rdiff -r1.8 -r1.9 basesrc/bin/dd/dd.1
> cvs rdiff -r1.9 -r1.10 basesrc/bin/echo/echo.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/expr/expr.1
> cvs rdiff -r1.25 -r1.26 basesrc/bin/ls/ls.1
> cvs rdiff -r1.10 -r1.11 basesrc/bin/mkdir/mkdir.1
> cvs rdiff -r1.23 -r1.24 basesrc/bin/mt/mt.1
> cvs rdiff -r1.12 -r1.13 basesrc/bin/mv/mv.1
> cvs rdiff -r1.16 -r1.17 basesrc/bin/pwd/pwd.1
> cvs rdiff -r1.9 -r1.10 basesrc/bin/rm/rm.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/rmdir/rmdir.1
> cvs rdiff -r1.35 -r1.36 basesrc/bin/sh/sh.1
> cvs rdiff -r1.11 -r1.12 basesrc/bin/sleep/sleep.1
> cvs rdiff -r1.20 -r1.21 basesrc/bin/stty/stty.1
 1.34.20.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.39.8.1 28-Jun-2012  riz Pull up following revision(s) (requested by wiz in ticket #364):
bin/stty/stty.1: revision 1.40
bin/stty/stty.1: revision 1.41
bin/stty/stty.c: revision 1.22
- improve wording, and create more consistency within the text;
- correct `SYNOPSIS';
- improve macro usage;
- mention argument name for `-f' (and change list width for it);
- correct misplacement of periods (`.') and a parenthese (`(');
- fix typo;
- bump date.
From Bug Hunting.
- remove trailing white space;
- sync `usage' with man page.
From patch by Bug Hunting.
Remove another Pf. From Bug Hunting.
 1.39.6.1 30-Oct-2012  yamt sync with head
 1.45.4.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.45.4.1 21-Apr-2020  martin Sync with HEAD
 1.24 06-Sep-2019  christos Don't fail when the line discipline ioctl fails (since it secondary
like the other tty ioctls that we only warn about). Do the main
ioctl (tcgetattr) first, since that provides a better error message
(ENOTTY instead of EINVAL).
 1.23 12-Sep-2013  christos branches: 1.23.26;
- print the line discipline using the new ioctl
- print the queue size
 1.22 20-Jun-2012  wiz branches: 1.22.2;
- remove trailing white space;
- sync `usage' with man page.

From patch by Bug Hunting.
 1.21 20-Jul-2008  lukem branches: 1.21.2; 1.21.20;
Remove the \n and tabs from the __COPYRIGHT() strings.
 1.20 26-May-2008  christos PR/38755: murray armfield: /bin posix programs missing setprogname and/or
setlocale
 1.19 07-Aug-2003  agc branches: 1.19.30; 1.19.32;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.18 16-Jun-2003  perry ANSIfy, from Petri Koistinen in PR 13975
 1.17 02-Mar-1999  christos s/BSD/STTY_BSD/ where I missed it before.
 1.16 02-Mar-1999  christos - char -> unsigned char
- #ifdef BSD4_4 for for non POSIX features
- prefix with STTY_ all the FMT constants to avoid namespace collisions.
 1.15 28-Jul-1998  mycroft Delint.
 1.14 28-Jul-1998  mycroft Delint.
 1.13 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.12 04-Jun-1997  mikel nuke extra : in usage string
 1.11 21-Mar-1995  cgd convert to new RCS id conventions.
 1.10 20-Sep-1994  mycroft branches: 1.10.2;
Merge with 4.4-Lite version.
 1.9 23-Mar-1994  mycroft Add VMIN and VTIME support, per Keith Bostic. Use libc err*() and warn*(),
and make usage() a function.
 1.8 18-Jan-1994  jtc It's 1994 --- remove stdout redirection check.
 1.7 01-Aug-1993  mycroft Add RCS identifiers.
 1.6 05-Jun-1993  deraadt reverse of "stty -g" now works. it forgot to actually ioctl() the
changes in.. patch posted by <ache@astral.msk.su>
 1.5 01-Jun-1993  cgd rcsid cleanup
 1.4 19-Apr-1993  mycroft Don't overrun end of argument list.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.10.2.2 20-Sep-1994  mycroft Merge with 4.4-Lite version.
 1.10.2.1 20-Sep-1994  mycroft file stty.c was added on branch netbsd-1-0 on 1994-09-20 04:52:13 +0000
 1.19.32.2 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.19.32.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.19.30.1 04-Jun-2008  yamt sync with head
 1.21.20.1 28-Jun-2012  riz Pull up following revision(s) (requested by wiz in ticket #364):
bin/stty/stty.1: revision 1.40
bin/stty/stty.1: revision 1.41
bin/stty/stty.c: revision 1.22
- improve wording, and create more consistency within the text;
- correct `SYNOPSIS';
- improve macro usage;
- mention argument name for `-f' (and change list width for it);
- correct misplacement of periods (`.') and a parenthese (`(');
- fix typo;
- bump date.
From Bug Hunting.
- remove trailing white space;
- sync `usage' with man page.
From patch by Bug Hunting.
Remove another Pf. From Bug Hunting.
 1.21.2.2 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.21.2.1 30-Oct-2012  yamt sync with head
 1.22.2.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.23.26.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.23.26.2 21-Apr-2020  martin Sync with HEAD
 1.23.26.1 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.11 12-Sep-2013  christos branches: 1.11.26;
- print the line discipline using the new ioctl
- print the queue size
 1.10 07-Aug-2003  agc branches: 1.10.54; 1.10.60;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.9 16-Jun-2003  perry ANSIfy, from Petri Koistinen in PR 13975
 1.8 02-Mar-1999  christos - char -> unsigned char
- #ifdef BSD4_4 for for non POSIX features
- prefix with STTY_ all the FMT constants to avoid namespace collisions.
 1.7 07-May-1996  jtc Use const qualifier as appropriate
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 20-Sep-1994  mycroft branches: 1.5.2;
Merge with 4.4-Lite version.
 1.4 01-Jun-1993  cgd rcsid cleanup
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.5.2.2 20-Sep-1994  mycroft Merge with 4.4-Lite version.
 1.5.2.1 20-Sep-1994  mycroft file stty.h was added on branch netbsd-1-0 on 1994-09-20 04:52:13 +0000
 1.10.60.1 19-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.10.54.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.11.26.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.11.26.1 21-Apr-2020  martin Sync with HEAD
 1.7 23-Mar-1994  mycroft Clean up deleted files.
 1.6 18-Jan-1994  jtc It's 1994 --- remove stdout redirection check.
 1.5 01-Aug-1993  mycroft Add RCS identifiers.
 1.4 01-Jun-1993  cgd rcsid cleanup
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.9 20-Jul-1997  christos branches: 1.9.112;
Remove WARNS=1 from all the subdirectory Makefiles, and add it to Makefile.inc
now that all /bin has been cleaned.
 1.8 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.7 21-Mar-1995  cgd convert to new RCS id conventions.
 1.6 22-Dec-1994  cgd new way of specifying man pages.
 1.5 20-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.9.112.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.9.112.1 21-Apr-2020  martin Sync with HEAD
 1.16 12-Aug-2016  sevan branches: 1.16.14;
sync was there from v4.
Confirmed from the TUHS hosted copies of man pages.
 1.15 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.14 02-Mar-2003  enami Fix .Nm usage.
 1.13 25-Feb-2003  wiz .Nm does not need a dummy argument ("") before punctuation or
for correct formatting of the SYNOPSIS any longer.
 1.12 20-Dec-2001  wiz Sort SEE ALSO.
 1.11 20-Dec-2001  wiz Whitespace nits.
 1.10 06-Oct-2000  jdolecek kill references to update(8), it doesn't exist any more
this addresses bin/11141 by Nick Hudson
 1.9 05-Oct-2000  ad insure -> ensure
 1.8 22-Mar-1999  garbled branches: 1.8.10;
More and more .Os cleanups. .Os is defined in the tmac.doc-common file,
so we shouldn't override it with versions in the manpages. Many more to
come.
 1.7 28-Apr-1998  fair fix mdoc references
 1.6 21-Mar-1995  cgd convert to new RCS id conventions.
 1.5 20-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 02-Aug-1993  mycroft sync.8
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.8.10.1 07-Oct-2000  jdolecek pullup rev. 1.10 from trunk (approved by thorpej):
remove reference to update(8), it no longer exists
 1.16.14.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.16.14.1 21-Apr-2020  martin Sync with HEAD
 1.14 05-Sep-2016  sevan branches: 1.14.14;
Drop main() prototype.
 1.13 20-Jul-2008  lukem Remove the \n and tabs from the __COPYRIGHT() strings.
 1.12 07-Aug-2003  agc branches: 1.12.32;
Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.11 16-Sep-2001  wiz ANSIfy, use setprogname. Patch by Petri Koistinen.
 1.10 03-Jul-2000  matt include <stdlib.h>, <string.h>, or whatever as appropriate to shut up
gcc 2.96
 1.9 05-Nov-1997  cgd lint
 1.8 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.7 21-Mar-1995  cgd convert to new RCS id conventions.
 1.6 04-Dec-1994  cgd from James Jegers <jimj@miller.cs.uwm.edu>: quiet -Wall, and squelch
some of the worst style errors.
 1.5 20-Sep-1994  mycroft Merge 4.4-Lite version.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 23-Mar-1993  cgd changed "Id" to "Header" for rcsids
 1.2 22-Mar-1993  cgd added rcs ids to all files
 1.1 21-Mar-1993  cgd branches: 1.1.1;
Initial revision
 1.1.1.2 20-Sep-1994  mycroft Import original 4.4-Lite version.
 1.1.1.1 21-Mar-1993  cgd initial import of 386bsd-0.1 sources
 1.12.32.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.14.14.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.14.14.1 21-Apr-2020  martin Sync with HEAD
 1.11 22-Jun-2007  simonb branches: 1.11.78;
Remove a "CFLAGS+=-g" that seems to have snuck in with no good reason.
 1.10 11-Sep-2006  dbj fixes for installing into a case insensitive $DESTDIR
when files may have hard links to a a name that only differs by case
- change install to unconditionally remove its temporary file
when installing hard links with -r. This avoids problems when
built with posix rename(2) semantics and reinstalling an existing
hard link.
- rework hard link targets in bsd.man.mk and bsd.links.mk
to use makefile constructs instead of shell constructs
- always reinstall hard links that may have case conflicts, even
when MKUPDATE=yes, this ensures that they get added to METALOG
- remove man pages which were hard linked to themselves in libform
- remove improper hard link command for existing man page in libkrb5
- fix libl's makefile to include bsd.lib.mk at end
- remove shell quoting in link target for test's [.1 man page
 1.9 04-Aug-2006  christos don't call get/set progname when we are a shell built-in.
 1.8 20-Jul-1997  christos Remove WARNS=1 from all the subdirectory Makefiles, and add it to Makefile.inc
now that all /bin has been cleaned.
 1.7 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.6 21-Mar-1995  cgd clean up Id's, and such. reorganize man page slight to match lite's org.
 1.5 30-Jun-1994  jtc Replaced test with simpler, more feature rich, version derived from pdksh.
 1.4 01-Aug-1993  mycroft Add RCS identifiers.
 1.3 29-Apr-1993  glass ugh. i was right after all.
 1.2 27-Apr-1993  glass bostic was right, and i was wrong.. boo.
 1.1 26-Apr-1993  glass new 'test' from ftp.uu.net:/systems/unix/bsd-sources, got rid of GNU thing
 1.11.78.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.11.78.1 21-Apr-2020  martin Sync with HEAD
 1.2 21-Mar-1995  cgd branches: 1.2.116;
clean up Id's, and such. reorganize man page slight to match lite's org.
 1.1 26-Apr-1993  glass new 'test' from ftp.uu.net:/systems/unix/bsd-sources, got rid of GNU thing
 1.2.116.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.2.116.1 21-Apr-2020  martin Sync with HEAD
 1.2 30-Jun-1994  mycroft Clean up deleted files.
 1.1 26-Apr-1993  glass new 'test' from ftp.uu.net:/systems/unix/bsd-sources, got rid of GNU thing
 1.3 30-Jun-1994  mycroft Clean up deleted files.
 1.2 19-Feb-1994  cgd whack mkops so it 'understands' and/or ops
 1.1 26-Apr-1993  glass new 'test' from ftp.uu.net:/systems/unix/bsd-sources, got rid of GNU thing
 1.6 30-Jun-1994  mycroft Clean up deleted files.
 1.5 19-Feb-1994  cgd add support for 'test -h' a la SunOS
 1.4 19-Feb-1994  cgd whack mkops so it 'understands' and/or ops
 1.3 23-Sep-1993  mycroft From alm:

Added and/or operator test to POSIX special case processing, so now:
$ test ! "abc" -o "abc"
evaluates to true.

The correct fix is probably to yank the superfluous special case code, but
I don't have time to verify the correctness of the result.
 1.2 02-Aug-1993  mycroft Add RCS identifiers, remove some completely useless RCS logs and patchkit
headers, and a few other insignificant changes.
 1.1 29-Apr-1993  glass ugh. i was right after all.
 1.6 30-Jun-1994  mycroft Clean up deleted files.
 1.5 19-Feb-1994  cgd add support for 'test -h' a la SunOS
 1.4 19-Feb-1994  cgd whack mkops so it 'understands' and/or ops
 1.3 23-Sep-1993  mycroft From alm:

Added and/or operator test to POSIX special case processing, so now:
$ test ! "abc" -o "abc"
evaluates to true.

The correct fix is probably to yank the superfluous special case code, but
I don't have time to verify the correctness of the result.
 1.2 02-Aug-1993  mycroft operators.c
 1.1 29-Apr-1993  glass ugh. i was right after all.
 1.33 18-Oct-2017  wiz branches: 1.33.4;
Remove unnecessary Tn.
 1.32 18-Oct-2017  kre 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.
 1.31 03-Jul-2017  wiz Remove workaround for ancient HTML generation code.
 1.30 12-Aug-2016  sevan Document the version test first appeared.
Bump date.
 1.29 27-May-2016  dholland usage nit
 1.28 16-Dec-2012  wiz - Remove unneeded consecutive argument in `.Nm' macros;
- rename `GRAMMAR AMBIGUITY' section to `CAVEATS', and move it to
correct location;
- bump date.

From Bug Hunting.
 1.27 10-Nov-2009  wiz branches: 1.27.6; 1.27.12;
Correct usage for parentheses, and mark up "expression" in more places.

From Bug Hunting in PR 42286.
 1.26 06-May-2009  wiz Remove leading zero in Dd argument.
 1.25 05-May-2009  jschauma Note that all file tests with the exception of -h and -L follow symbolic
links and thus evaluate the test for the file pointed at.
 1.24 12-Apr-2009  joerg Fix markup.
 1.23 15-May-2007  uwe branches: 1.23.20;
Use the same wording for '-p' as for other file related tests.
 1.22 24-Sep-2006  hubertf Revert previous (==) - not worth the trouble.
 1.21 23-Sep-2006  wiz Bump date for previous.
 1.20 22-Sep-2006  hubertf Allow '==' as synonym to '=', just like our ksh and bash do.
This also affects the sh(1) builtin test command.
 1.19 07-Aug-2003  agc Move UCB-licensed code from 4-clause to 3-clause licence.

Patches provided by Joel Baker in PR 22249, verified by myself.
 1.18 25-Sep-2002  wiz New policy: New sentences start on a new line.
Patches by Robert Elz <kre at munnari oz au>, with minimal changes by me.
 1.17 08-Feb-2002  ross Generate <>& symbolically. I'm avoiding .../dist/... directories for now.
 1.16 20-Dec-2001  wiz Whitespace nits.
 1.15 17-Oct-2001  wiz Properly quote `[' as macro argument.
 1.14 04-Sep-2000  kleink For commands and utilities, use EXIT STATUS rather than RETURN VALUES as
appropriate (and documented in mdoc(7)).
 1.13 17-Dec-1999  abs Remove duplicate 'string' / 's1' entry
 1.12 22-Aug-1999  kleink branches: 1.12.4;
Reverse the advice given to (now) prefer -h over -L; the former is defined in
1003.2bD3 and SVID4 and thus supposedly more portable.
 1.11 09-Mar-1999  garbled branches: 1.11.2;
the '[' wasn't being displayed properly as a command. Thanks to Charles
for the fix.
 1.10 12-Nov-1997  mrg add missing .Nm calls and MLINKS. from spz@serpens.swb.de
 1.9 09-Oct-1997  mycroft branches: 1.9.2;
Fix format of description line.
 1.8 05-Jun-1997  mikel add SYNOPSIS line for [; fixes PR bin/3705
 1.7 05-Jan-1996  pk Note compatibility `-h' op. (from Rob Windsor; PR#1740)
 1.6 21-Mar-1995  cgd clean up Id's, and such. reorganize man page slight to match lite's org.
 1.5 30-Jun-1994  jtc Replaced test with simpler, more feature rich, version derived from pdksh.
 1.4 29-Jun-1994  jtc The file_descriptor argument to the -t primary is no longer optional.
 1.3 19-Feb-1994  cgd add support for 'test -h' a la SunOS
 1.2 01-Aug-1993  mycroft Add RCS indentifiers.
 1.1 26-Apr-1993  glass new 'test' from ftp.uu.net:/systems/unix/bsd-sources, got rid of GNU thing
 1.9.2.1 12-Nov-1997  mrg pull up from trunk: add missing .Nm calls and MLINKS. from spz@serpens.swb.de
 1.11.2.1 17-Dec-1999  he Pull up revision 1.10 (requested by abs):
Remove obsolete reference to mkpasswd(8).
 1.12.4.1 27-Dec-1999  wrstuden Pull up to last week's -current.
 1.23.20.1 13-May-2009  jym Sync with HEAD.

Third (and last) commit. See http://mail-index.netbsd.org/source-changes/2009/05/13/msg221222.html
 1.27.12.1 25-Feb-2013  tls resync with head
 1.27.6.1 23-Jan-2013  yamt sync with head
 1.33.4.2 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.33.4.1 21-Apr-2020  martin Sync with HEAD
 1.45 27-Aug-2022  dholland PR 56983 Izumi Tsutsui: fix confusing message in test(1) with -DSMALL
 1.44 05-Dec-2021  msaitoh s/existance/existence/ in comment.
 1.43 13-Sep-2018  kre branches: 1.43.2;

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.
 1.42 12-Sep-2018  kre 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
 1.41 05-Sep-2016  sevan branches: 1.41.12; 1.41.14;
Drop main() prototype.
 1.40 04-May-2013  uebayasi test(1): Compare timestamp in nsec scale in -nt/-ot.
 1.39 15-Mar-2012  joerg branches: 1.39.2;
Add __printflike attribution to use vprintf and friends with an argument
as format string.
 1.38 29-Aug-2011  joerg branches: 1.38.2;
Use __dead
 1.37 10-Sep-2008  christos bin/39446: Aleksey Cheusov: always produce errors on empty numbers.
 1.36 20-Jun-2008  christos make getn return long long to support large numbers on all archs.
 1.35 26-May-2008  christos PR/38755: murray armfield: /bin posix programs missing setprogname and/or
setlocale
 1.34 15-Dec-2007  perry branches: 1.34.4; 1.34.6;
convert __attribute__s to applicable cdefs.h macros
 1.33 24-Jun-2007  christos branches: 1.33.4;
PR/36534: Greg A. Woods: test(1) doesn't do -r and -w quite right
Use stat(2) instead of access(2) per POSIX.
 1.32 24-May-2007  christos Make sure we don't run over the end of the argument array while scanning
for input. This can happen if we have a unary not without an argument. When
we scan for the argument, we are already at the NULL element of the argument
array. Then when we scan ahead for a -a or -o, we end up testing the next
element after the NULL.
 1.31 28-Mar-2007  christos PR/36092: B K: algorithmic inefficiency in bin/test/test.c:t_lex
test(1) scans for "operators" linearly in an array using strcmp() to
find a match. Since the list of "operators" is fixed, split them
into one and two character ones, and ones that start with a `-' and
ones they don't. This way we can optimize the compare function to
just check for one or two characters. Sort and use bsearch(3). We
could have used a single sorted array and bsearch(3), to save some
complexity, but I decided to be a bit fancier.
 1.30 24-Sep-2006  hubertf Revert previous (==) - not worth the trouble.
 1.29 22-Sep-2006  hubertf Allow '==' as synonym to '=', just like our ksh and bash do.
This also affects the sh(1) builtin test command.
 1.28 04-Aug-2006  christos don't call get/set progname when we are a shell built-in.
 1.27 04-Aug-2006  christos use getprogname() instead of argv[0], because some programs that execv, pass
the full path in argv[0] instead of just the basename. In reality, those
programs should be fixed (hello gdb for example).
 1.26 10-Feb-2005  simonb KNF, use NULL instead of (type *)0.
 1.25 25-May-2002  wiz __STDC__ is always defined on NetBSD.
 1.24 16-Sep-2001  wiz Some KNF fixes by Petri Koistinen and me.
 1.23 30-Jul-2001  wiz ANSIfy and slight KNF. Mostly #13594 by Petri Koistinen.
 1.22 09-Apr-2000  christos - Enable test to be a shell builtin
- KNF
- Fix bug where test without arguments could access invalid memory
 1.21 05-Apr-1999  kleink Operator precedence consistency; from "Andrew_L. Moore" <alm@SlewSys.Org>
in PR bin/7015.
 1.20 04-Nov-1998  christos branches: 1.20.2;
char -> unsigned char
 1.19 28-Jul-1998  mycroft Be more retentive about use of NOTREACHED and noreturn.
 1.18 19-Oct-1997  mycroft Use S_IS*(), not S_IF*.
 1.17 20-Jul-1997  christos Fix compiler warnings.
Add WARNS=1
 1.16 09-Jan-1997  tls kill use of register
 1.15 21-Mar-1995  cgd clean up Id's, and such. reorganize man page slight to match lite's org.
 1.14 07-Jul-1994  cgd from jtc: insure that 3 and 4 argument special cases work properly.
 1.13 30-Jun-1994  jtc branches: 1.13.2;
Replaced test with simpler, more feature rich, version derived from pdksh.
 1.12 29-Jun-1994  jtc Removed code that checked if arg was a unary operator when test is called
with only one argument. It's not a syntax error --- it's a special case.
 1.11 10-Apr-1994  cgd fix syntax error when only have one arg. from Jarle Greipsland
 1.10 19-Feb-1994  cgd add support for 'test -h' a la SunOS
 1.9 26-Oct-1993  cgd fix from Andrew Moore <alm@netcom.com> to fix test's handling of
(non-negated) "-a" and "-o".
 1.8 23-Sep-1993  mycroft From alm:

Added and/or operator test to POSIX special case processing, so now:
$ test ! "abc" -o "abc"
evaluates to true.

The correct fix is probably to yank the superfluous special case code, but
I don't have time to verify the correctness of the result.
 1.7 23-Sep-1993  mycroft Put back RCS identifier.
 1.6 15-Sep-1993  alm test "0 " -eq 0 now evaluates to true.
It also does range checking. These override J.T.'s patches.
 1.5 08-Sep-1993  jtc Ignore leading whitespace and handle `-' and `+' for operands of integer
comparisions.
 1.4 08-Sep-1993  alm /bin/test " 5" -eq 5 now evaluates to true, rather than return an error.
/bin/test "5 a" -eq 5 still correclty returns an error.
 1.3 01-Aug-1993  mycroft Add RCS identifiers.
 1.2 15-Jun-1993  cgd get the [ ! "" ] case right.
 1.1 26-Apr-1993  glass new 'test' from ftp.uu.net:/systems/unix/bsd-sources, got rid of GNU thing
 1.13.2.1 07-Jul-1994  cgd from jtc: insure that 3 and 4 argument special cases work properly.
 1.20.2.2 03-Jun-2000  he Pull up revision 1.22 (requested by he):
Enable test as a shell builtin. Fixes PR#9847.
 1.20.2.1 03-Jun-2000  he Pull up revision 1.21 (requested by he):
Fix operator precedence consistency. Fixes PR#7015.
 1.33.4.1 09-Jan-2008  matt sync with HEAD
 1.34.6.2 24-Sep-2008  wrstuden Merge in changes between wrstuden-revivesa-base-2 and
wrstuden-revivesa-base-3.
 1.34.6.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.34.4.1 04-Jun-2008  yamt sync with head
 1.38.2.2 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.38.2.1 17-Apr-2012  yamt sync with head
 1.39.2.1 23-Jun-2013  tls resync from head
 1.41.14.3 21-Apr-2020  martin Ooops, restore accidently removed files from merge mishap
 1.41.14.2 21-Apr-2020  martin Sync with HEAD
 1.41.14.1 10-Jun-2019  christos Sync with HEAD
 1.41.12.1 30-Sep-2018  pgoyette Ssync with HEAD
 1.43.2.1 29-Aug-2022  martin Pull up following revision(s) (requested by dholland in ticket #1507):

bin/test/test.c: revision 1.45

PR 56983 Izumi Tsutsui: fix confusing message in test(1) with -DSMALL
 1.3 30-Jun-1994  mycroft Clean up deleted files.
 1.2 19-Feb-1994  cgd add support for 'test -h' a la SunOS
 1.1 26-Apr-1993  glass new 'test' from ftp.uu.net:/systems/unix/bsd-sources, got rid of GNU thing

RSS XML Feed