summaryrefslogtreecommitdiff
path: root/bin/ksh
AgeCommit message (Collapse)Author
2022-10-10Trigger ERR trap on permanent I/O redirection failureKlemens Nanni
The following three cases behave identical in bash(1), but our ksh (ksh93 also) fails to run the trap in the last case: (non-zero exit code is trigger, no redirection) $ ksh -c 'trap "echo ERR" ERR ; false' ERR (failed redirection is trigger, 'echo' was not executed) $ ksh -c 'trap "echo ERR" ERR ; echo >/' ksh: cannot create /: Is a directory ERR (failed redirection, no execution, trap was NOT triggered) $ ksh -c 'trap "echo ERR" ERR ; exec >/' ksh: cannot create /: Is a directory bash(1) prints "ERR" in all three cases, as expected. ksh93 behaves like our ksh(1). In ksh `exec' is a builtin (CSHELL), but also special (SPEC_BI): $ type alias alias is a shell builtin $ type exec exec is a special shell builtin Without command and redirection alone, `exec' permanently redirects I/O for the shell itself, not executing anything; it is the only (special) builtin with such a special use-case, implemented as c_sh.c:c_exec(). This corner-case is overlooked in exec.c:execute() which handles iosetup() failure for all commands, incl. builtins. Exclude c_exec() from the rest of special builtins to ensure it runs the ERR trap as expected: $ ./obj/ksh -c 'trap "echo ERR" ERR ; exec >/' ksh: cannot create /: Is a directory ERR Also add three new regress cases covering this; rest keep passing. OK millert
2022-09-13== in [[ does pattern matching as wellKlemens Nanni
OK millert
2022-08-31use the posix phrasing to improve the description of "shift";Jason McIntyre
nudge from luka krmpotic
2022-03-31man pages: add missing commas between subordinate and main clausesChristian Weisgerber
jmc@ dislikes a comma before "then" in a conditional, so leave those untouched. ok jmc@
2021-12-24when getopts prints "unknown option" or "requires argument", it shouldTheo de Raadt
not print the shell script line number where this occured. Doing so is pointless, or an information leak. This change does not affect any other error reporting. ok millert
2021-12-15getcwd() operates on buffers of PATH_MAX including the NUL, and the +1Theo de Raadt
is not unneccesary. Different buffer sizes are actually dangerous, though major problems are strangely rare. ok millert
2021-10-24For open/openat, if the flags parameter does not contain O_CREAT, theTheo de Raadt
3rd (variadic) mode_t parameter is irrelevant. Many developers in the past have passed mode_t (0, 044, 0644, or such), which might lead future people to copy this broken idiom, and perhaps even believe this parameter has some meaning or implication or application. Delete them all. This comes out of a conversation where tb@ noticed that a strange (but intentional) pledge behaviour is to always knock-out high-bits from mode_t on a number of system calls as a safety factor, and his bewilderment that this appeared to be happening against valid modes (at least visually), but no sorry, they are all irrelevant junk. They could all be 0xdeafbeef. ok millert
2021-10-09In ksh(1) emacs search-history mode, emitting a NUL character causesAlexander Hall
invalid matches and unexpected behaviour. Fix this by instead making a NUL character abort the search-history mode, leaving the handling of said input to the "ordinary" command editing. ok tb@
2021-07-05Do not permit an empty list between "while" and "do".Todd C. Miller
This avoids a cpu loop for "while do done" and is consistent with the behavior of AT&T ksh and most other shells. OK jca@ halex@
2021-06-27In addition to 2-byte and 3-byte UTF-8 sequences, correctly identify allIngo Schwarze
4-byte UTF-8 sequences and not just some of them, to keep them together and avoid passing them on byte by byte, helping tools like tmux(1). While here, also do all the range tests with < and > rather than & for uniformity and readability, and add some comments. Input and OK jca@ and nicm@. Soeren at Soeren dash Tempel dot net originally reported the bug and provided an incomplete patch that was used as a starting point, and he also tested this final patch.
2021-05-04shell scripts should use getopts instead of getoptChristian Weisgerber
Add a prominent deprecation notice to getopt.1. Add examples of the getopts idiom to sh.1 and ksh.1. Requested by and ok espie@, ok jmc@
2021-03-12Provide definition of CTRL in vi.c like we do for emacs.c.Todd C. Miller
Fixes a portability issue. From Benjamin Baier
2021-03-11groff complains about the word "An" in an Rs/Re block, believing it a macro,Jason McIntyre
so escape it;
2021-03-10Add support for ^R (redraw) in insert mode too.Todd C. Miller
From gotroyb127, OK tb@
2021-03-10Fix redrawing of a multiline PS1 prompt in vi mode.Todd C. Miller
From gotroyb127 OK tb@
2021-03-08use a journal reference instead of cstr when possibleJonathan Gray
2021-03-08Add some references, most of these were removed when we stopped buildingJonathan Gray
and installing USD/SMM/PSD docs. jmc@ agrees with the direction, ok millert@ on an earlier diff
2021-03-05Fix old ksh bug: wrong variable being looked up when value is provided.Vadim Zhukov
This results, e.g., in allowing the first item of a read-only array to be overwritten, as found by Jordan Geoghegan. okay tb@
2020-10-26In lib/libc/gen/charclass.h r1.3 guenther made cclasses const.Theo Buehler
Mark the pointer used to walk the array in ksh const as well. From Matthew Martin ok guenther
2020-09-20Clear screen before redrawing the line with ^L, also in input mode.Todd C. Miller
This is similar to the emacs mode clear-screen command. Unlike bash, but like zsh, ^L also works in input mode, not just command mode. OK kn@ tb@
2020-09-13Fix "$@" splitting with empty IFSTheo Buehler
One uncommon but useful way of writing shell scripts is to start off by disabling field/word splitting (IFS='') and pathname expansion/globbing (set -f), re-enabling either or both only for the commands that need them, e.g. within a subshell. This helps avoid a lot of snags with field splitting and globbing if you forget to quote a variable somewhere, adding to the general robustness of a script. (In fact it eliminates much of the need to quote variable/parameter expansions, with empty removal remaining as the only issue.) Unfortunately OpenBSD ksh (like all pdksh variants except mksh) has a POSIX compliance bug that is a show stopper for this approach: "$@" does not generate words (arguments) if IFS is empty. As a result, the separate command arguments represented by "$@" become a single argument. So passing on an intact set of positional parameters to a command or function is impossible with field splitting disabled. Of course this is illogical: the quoted special parameter "$@" generates zero or more words, it doesn't split any words, so the contents of IFS (or lack thereof) should be neither here nor there. It's old ksh88 behaviour copied by the original pdksh, but it violates POSIX and it has been fixed many years ago in ksh93 and all other POSIX shells. From Martijn Dekker (who also wrote the above paragraphs) back in 2016. Thanks to Avi Halachmi for reminding us of the issue. ok czarkoff deraadt kn
2020-07-22Collapse consecutive stars to avoid exponential behavior.Todd C. Miller
OK tb@
2020-07-07Add support for set -o pipefailJeremie Courreges-Anglas
With the pipefail option set, the exit status of a pipeline is 0 if all commands succeed, or the return status of the rightmost command that fails. This can help stronger error checking, but is not a silver bullet. For example, commands will exhibit a non-zero exit status if they're killed by a SIGPIPE when writing to a pipe. Yet pipefail was considered useful enough to be included in the next POSIX standard. This implementation remembers the value of the pipefail option when a pipeline is started, as described as option 1) in https://www.austingroupbugs.net/view.php?id=789#c4102 Requested by ajacoutot@, ok millert@
2020-05-22Fix the exit code when eval()uating a || compound list, it wouldSebastian Benoit
terminate the shell when running under -e. See also https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=269067 and Bug reported including fix by Leah Neukirchen, Thanks! ok millert@
2020-05-08Use proper function pointer type instead of void *Jeremie Courreges-Anglas
Mixing up function and void pointers isn't defined by POSIX or the C standard. POSIX only specifies that casting the result of dlsym(3) to an appropriate function pointer works. Avoid all this by using a typedef. from Michael Forney, ok tb@
2020-02-21Enforce that TMOUT is an integer literal to prevent command execution fromTheo Buehler
the environment at shell initialization time. During startup, ksh calls 'eval typeset -i TMOUT="${TMOUT:-0}"'. which allows command injection via arithmetic expansion, e.g., by setting TMOUT to 'x[`/bin/echo Hi >&2`]'. Problem noted by Andras Farkas and tj, inspired by a similar issue in AT&T's ksh. Tested in snaps for two weeks. "go for it" deraadt
2019-11-26some corrections to CDPATH;Jason McIntyre
from chohag
2019-10-27No need for <sys/uio.h> as writev(2) isn't used any more.Jeremie Courreges-Anglas
2019-10-27Don't fail hard if we can't preallocate history storage.Jeremie Courreges-Anglas
Using alloc.c for the history array brings no value and prevents easy handling of memory shortage. Switch to plain reallocarray and keep running if HISTSIZE is too big. The allocation is still done upfront with no sanity checking, it would probably be nicer to allocate space as needed. Issue reported by thomas@habets.se who suggested a different approach.
2019-07-24Add #include <stdlib.h> for mkstemp.Brian Callahan
Spotted by maya@netbsd ok deraadt@
2019-06-28When system calls indicate an error they return -1, not some arbitraryTheo de Raadt
value < 0. errno is only updated in this case. Change all (most?) callers of syscalls to follow this better, and let's see if this strictness helps us in the future.
2019-06-27Some asprintf() calls were checked < 0, rather than the precise == -1.Theo de Raadt
ok millert nicm tb, etc
2019-06-24Fix spellingJeremie Courreges-Anglas
2019-06-24Partial revert of rev. 1.151:Ingo Schwarze
Reference the First Edition (1989) of Bolsky/Korn which is about ksh88, the shell the OpenBSD ksh(1) descends from (via pdksh). The Second Edition (1995) of the book is about ksh93 which we don't provide. Pointed out by Andras Farkas on bugs@.
2019-06-21zap trailing whitespace;Jason McIntyre
2019-06-19Allow string greater/less than than operators to work with test aka [.Todd C. Miller
Previously they were only recognized in [[ ... ]] expressions. This changes sh/ksh to be consistent with test(1) as well as shells like bash and dash. OK jca@ jmc@
2019-05-22mention that using vi command line editing mode requires enabling it;Ingo Schwarze
omission reported by Rudolf Sykora <rsykora at disroot dot org> on misc@; tweak and OK jmc@
2019-04-03Bind ^L (C-l) to clear-screen instead of redrawJeremie Courreges-Anglas
Slightly more useful for some, same defaults as bash. No objection deraadt@ phessler@, ok tb@ kn@ benno@
2019-02-20When evaluating an arithmetical expression, for example inside $(()),Ingo Schwarze
never do substitution (neither parameter, nor command, nor arithmetic, nor tilde substitution) on the values of any variables encountered inside the expression, but do recursively perform arithmetical evaluation of subexpressions as required. This makes behaviour more consistent, without hindering any behaviour promised in the manual page. A quirk originally reported by Andy Chu <andychup at gmail dot com> was that in the past, when encountering an array index, the shell would not only do evaluation, but also substitution on the array index, even though substitution would not be done on the expression in general. tobias@ contributed to initial efforts of understanding the quirk. patch tested in a bulk build by naddy@ "please commit" deraadt@
2019-01-25I am retiring my old email address; replace it with my OpenBSD one.Todd C. Miller
2019-01-14Fix three more buglets:Ingo Schwarze
1. Another off-by-one: if a mail file name ends in an (escaped) percent sign, do not forget to check whether the next byte is the percent sign introducing the message (MAILPATH='filename\%%msg'). 2. If the message is empty, use the default message rather than printing a blank line (MAILPATH='filename%'). 3. If the file name is empty, don't bother with mballoc(): the subsequent stat(2) can never succeed. (MAILPATH='%msg'). Found while reviewing the previous commit by tedu@. OK tedu@.
2019-01-14do not peek before the beginning of a stringTed Unangst
ok deraadt schwarze tb
2019-01-07short circuit mail check if MAIL is unset. ok antonTed Unangst
2018-12-30Delete unnecessary <libgen.h> #includesPhilip Guenther
ok deraadt@
2018-12-16Tweak the syntax displays to show that the list of wordsIngo Schwarze
in "for name in [word ...]; do list; done" can be empty. In sh(1), clarify what happens in that case. In ksh(1), clarify how it can happen that the list is never executed. OK jmc@ tb@
2018-12-08Fix kill [-SIGNAME | -s SIGNAME] and simplifyJeremie Courreges-Anglas
While the code intended to support both -s NAME and -s SIGNAME, the tests performed were wrong. Replace convoluted code with less cryptic conditionals. ok anton@
2018-11-30in getopts, when a option is followed by a colon the parameter is mandatorysolene
ok guenther@ deraadt@
2018-11-20Convert the pledge call to idiomatic format 'cause we love grep.Theo de Raadt
2018-11-20Fix the case where the recursion detection isn't reset when the command isMartijn van Duren
interrupted. Lots of back and forth with anton@ OK jca@, tb@, anton@
2018-11-17Use a very regular call pattern to pledge, so that we can continue toTheo de Raadt
grep and compare the use in all programs..