Commit Graph

4143 Commits

Author SHA1 Message Date
kre 0827e1f954 The great shell trailing whitespace cleanup of 2023...
Inspired by private e-mail comments from mouse@

NFCI.
2023-04-07 10:34:13 +00:00
hannken f438a6a065 Use "sigjmp_buf loc" after switch to sigsetjmp()/siglongjmp().
Fixes errors and aborts on sparc at least.
2023-03-21 08:31:30 +00:00
kre 98b2eb3607 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).
2023-03-19 17:55:57 +00:00
kre 91522b5b9d Switch from using _setjmp()/_longjmp() (on BSD systems which aren't SVR4)
(and setjmp()/longjmp() elsewhere) to using sigsetjmp()/siglongjmp()
everywhere.

NFCI.
2023-03-19 17:47:48 +00:00
kre d45073cdfd Change a few #defines from octal to hex (pdp11 days are long gone).
Improve the layout of those definitions at the same time.

NFC.
2023-03-19 17:45:29 +00:00
kre 726d188a97 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!
2023-03-06 05:54:34 +00:00
kre 64c7eea502 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.
2023-02-24 19:04:54 +00:00
uwe a525a2707f sleep(1): minor markup tweaks 2023-02-19 10:54:35 +00:00
kre d1668f2837 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).
2022-12-20 17:51:54 +00:00
kre d51e0a2e8a Using .Cm Cm makes no sense at all - no idea what I was thinking there
(perhaps just an editing error).
2022-12-20 16:48:57 +00:00
uwe c14a7046e4 sh(1): Fix markup. -compact must be last. 2022-12-20 01:18:42 +00:00
kre e2731928b1 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).
2022-12-11 08:23:10 +00:00
andvar 8a36802c94 fix various typos in comments and one output message. 2022-10-31 21:22:05 +00:00
kre 1c627bdffa 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.
2022-10-30 01:46:16 +00:00
kre ec520a6a57 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).
2022-10-30 01:19:08 +00:00
christos 9337b5ddea Add -f option to set the time. From FreeBSD. 2022-10-22 20:11:43 +00:00
kre 9eccf618bb 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).
2022-09-18 17:11:33 +00:00
kre e7b0505e69 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.
2022-09-18 06:03:19 +00:00
kre e1f658a30d More wording improvements. There might be more to come. 2022-09-16 19:25:09 +00:00
kre f9baa2eaae 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).
2022-09-16 17:32:18 +00:00
kre c5917dbfa8 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.
2022-09-16 17:29:21 +00:00
kre 65d376a4f6 Whitespace. 2022-09-16 17:25:09 +00:00
kre 258641d776 Correct spelling of terminal (it doesn't have a 2nd m). 2022-09-15 18:00:36 +00:00
martin ea9288bf30 Fix the build for variants that do not define EDIT. 2022-09-15 11:35:06 +00:00
christos e473cf6a19 Update editing before prompt printing. 2022-09-14 17:06:16 +00:00
christos d0aa7ee82f defer editing setup/cleanup to when we are interactive. 2022-09-14 16:15:51 +00:00
hgutch ec184f3bfb Change back various occurrences of \*[Le], \*[Ge] (less/greater equal)
and \*(ua (upwards arrow) to literal "<=", ">=" and "^" whenever
appropriate (e.g., in code examples).
2022-08-28 10:48:15 +00:00
dholland cc9598fccf PR 56983 Izumi Tsutsui: fix confusing message in test(1) with -DSMALL 2022-08-27 21:18:39 +00:00
kre f5b9e602e2 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...
2022-08-22 17:33:11 +00:00
nia 29c67fe23d sh(1): revert previous because it interferes with custom user bindings 2022-08-21 21:35:36 +00:00
uwe 21531a4577 ksh(1): fix pasto in the description of the -l option. 2022-08-21 20:12:37 +00:00
kre 16e67595a2 Improve the description of the read builtin command. 2022-08-19 13:37:03 +00:00
kre a1f6605654 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]
2022-08-19 12:52:31 +00:00
kre 143e8d551e 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).
2022-08-19 12:17:18 +00:00
nia 16c8f5ddd4 sh(1): Allow an explicit set -o vi or set -o emacs to override ~/.editrc 2022-08-18 14:10:05 +00:00
nia 635122b300 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.
2022-08-17 22:27:17 +00:00
wiz 1bfd10bdd7 Sync usage with man page. 2022-08-09 08:14:03 +00:00
wiz 3ce29a4c09 Sort options, fix macro, add RCS Id. 2022-08-09 08:13:56 +00:00
kre 2c3bf36b92 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.
2022-08-08 16:50:35 +00:00
andvar 5a58ccfbec fix various typos in comments, documentation and messages.
mainly s/paramater/parameter/ and s/reduntant/redundant/.
2022-08-07 11:06:18 +00:00
andvar ec52c87343 kill(1): s/proceses/processes/ 2022-08-07 09:36:55 +00:00
rin 2316fa6ff0 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...
2022-07-18 13:01:59 +00:00
uwe e7d7b85e33 csh(1): fix punctuation at the end of an .Xr line 2022-07-09 21:19:44 +00:00
kre 8f36a40a4c 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).
2022-07-03 06:30:31 +00:00
rin 1ca2da5b85 Add SIGINFO support. Mostly stolen from scan_ffs(8). 2022-06-26 09:29:59 +00:00
andvar ff23aff6ad fix various typos in comments, documentation and messages. 2022-05-31 08:43:13 +00:00
andvar 86c307248f fix various typos, mainly in comments. 2022-05-28 10:36:21 +00:00
andvar b97019535c fix some misspellings and remove trailing whitespaces. 2022-05-20 07:47:16 +00:00
kre 32cc3b0248 Alter error messages so they no longer claim that bad input is illegal. 2022-05-16 10:53:14 +00:00
dholland 4a2334fa92 Clarify that "stty 0" will normally hang up the tty. Bump date. 2022-05-14 21:04:34 +00:00
uwe 0a751ce1a9 date(1): Use .Dl for one-liners. Same output is generated. 2022-05-10 09:07:57 +00:00
uwe 7638fb4e20 date(1): Fix the offset in previous. 2022-05-10 09:00:39 +00:00
wiz 8147634961 date(1): add example for how to get seconds since the Epoch output 2022-05-10 05:37:33 +00:00
kre 21f0086877 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.
2022-04-18 06:02:27 +00:00
andvar e2710f6fc4 fix various typos in comments. 2022-04-17 21:24:52 +00:00
kre f378cfe59e 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.
2022-04-16 14:26:26 +00:00
kre d17e063cb6 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.
2022-04-16 14:23:36 +00:00
kre 94d6cf1cd6 Avoid generating error messages implying that user errors are illegal. 2022-04-16 14:20:45 +00:00
andvar 75d2abaeb1 fix various typos in comments and output/log messages. 2022-04-10 09:50:44 +00:00
andvar 2fa7e14158 fix various typos, mainly in comments, but also log messages, docs, game text. 2022-04-08 10:17:52 +00:00
rillig d995afa8a4 sh: fix typo in comment 2022-02-08 20:39:59 +00:00
kre f1bd294bee 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 ...)
2022-02-02 01:21:34 +00:00
kre 772973c9a5 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@
2022-01-31 18:15:45 +00:00
kre 05e80ac3ee 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
2022-01-31 16:54:28 +00:00
andvar 7c674e8e6e remove double t from targeted, add missing r to arbitrary
And fix few more typos along the way in comments and man pages.
2022-01-26 11:48:53 +00:00
andvar 492c086f0a s/begining/beginning/ in comments and messages. 2022-01-24 09:14:36 +00:00
kre 3106d82cfe After 3 and a bit years, it is time... 2022-01-22 22:53:58 +00:00
christos f49d542542 update header too. 2022-01-14 23:56:35 +00:00
christos 62a484e15a 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)
2022-01-14 23:55:16 +00:00
lukem e99dcc23fb 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.
2022-01-07 05:30:30 +00:00
lukem c878ae3c69 sh(1): fix formatting warnings 2022-01-07 05:10:30 +00:00
kre 29a26373af 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
2022-01-05 15:25:44 +00:00
andvar 92e3dd6443 s/forground/foreground/ in comments. 2021-12-19 21:15:27 +00:00
andvar 715cc6e2dc s/backgound/background/ 2021-12-19 21:02:49 +00:00
andvar b5a5957fe8 s/Miscelaneous/Miscellaneous/ and s/slahes/slashes/ in comments. 2021-12-12 11:18:46 +00:00
andvar b444e42254 s/desireable/desirable/ in comments. 2021-12-08 20:21:09 +00:00
msaitoh 1e4f69d0ff s/existance/existence/ in comment. 2021-12-05 04:42:54 +00:00
msaitoh 22b6bc1f53 s/commmand/command/ in comment. 2021-12-05 04:35:38 +00:00
msaitoh 344f0d1e04 s/exisit/exist/ in comment. 2021-12-05 02:52:17 +00:00
simonb c115fd08c1 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".
2021-11-29 05:59:58 +00:00
kre 16d8557100 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).
2021-11-22 05:17:43 +00:00
kre 9b13765f1a Improve the however-many-negatives wording even more. 2021-11-21 16:23:20 +00:00
rillig 87e5866877 sh.1: replace triple negation with single negation, fix typo 2021-11-20 17:18:31 +00:00
kre c426788907 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).
2021-11-20 01:52:51 +00:00
rillig 4c9c11dcf9 sh.1: fix typos 2021-11-16 23:39:34 +00:00
rillig 982b8df7ec echo: fix lint error due to strict bool mode
No functional change.
2021-11-16 21:38:29 +00:00
kre b6dd340d3c 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.
2021-11-16 16:57:15 +00:00
kre 8ea2ac47d5 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).
2021-11-16 11:28:29 +00:00
kre b65a5b9068 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).
2021-11-16 11:27:50 +00:00
kre a41dbcb01d 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.
2021-11-16 11:25:44 +00:00
kre a18d86b7be 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.
2021-11-10 18:25:52 +00:00
kre 40f303e024 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)?
2021-11-10 17:35:45 +00:00
kre a3710ae449 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.
2021-11-10 15:58:38 +00:00
kre b8bee70d9c 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.
2021-11-10 15:26:34 +00:00
msaitoh f81569c702 s/writting/writing/ 2021-11-06 06:40:33 +00:00
msaitoh 763440c947 s/maxiumum/maximum/ 2021-11-06 06:38:03 +00:00
gutteridge 45137c4698 pax.1: minor grammar fixes 2021-11-06 01:19:19 +00:00
nia 56978e9154 ed(1): use reallocarr instead of realloc(x * y) 2021-11-02 08:04:20 +00:00
kre 609916a1bc 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
2021-10-31 02:12:08 +00:00
kre 82386044cc 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
2021-10-31 02:12:01 +00:00