1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-02-13 11:42:21 +00:00
Commit graph

889 commits

Author SHA1 Message Date
Johnothan King
eca92ea48c Enable some memory leak tests for standard malloc (re: 05683ec7) (#311)
The memory leak regression tests added in commit 05683ec7 only leak memory
in the C.UTF-8 locale if ksh is compiled with vmalloc. I've ran these
regression tests against ksh93v- and neither fail in that version of
ksh, which indicates the bug causing these tests to fail may be similar to
the one that causes <https://github.com/ksh93/ksh/issues/95>.
Since the memory leak tests work with -D_std_malloc, only set $LANG to
'C' if ksh is compiled with vmalloc enabled.
2021-05-21 01:52:32 +02:00
Martijn Dekker
0dd115e4b4 Fix shell exit on function call redirection error (re: 23f2e23)
This regression also exists on ksh 93v- and ksh2020, from which it
was backported.

Reproducer:

$ (fn() { true; }; fn >/dev/null/ne; true) 2>/dev/null; echo $?
1

Expected output: 0 (as on ksh 93u+).

FreeBSD sh and NetBSD sh are the only other known shells that share
this behaviour. POSIX currently allows both behaviours, but may
require the ksh 93u+ behaviour in future. In any case, this causes
an incompatibility with established ksh behaviour that could easily
break existing ksh scripts.

src/cmd/ksh93/sh/xec.c: sh_exec():
- Commit 23f2e23 introduced a check for jmpval > SH_JMPIO (5).
  When a function call pushes context for a redirection, this is
  done with the jmpval exit value of SH_JMPCMD (6). Change that to
  SH_JMPIO to avoid triggering that check.

src/cmd/ksh93/tests/exit.sh:
- Add regression tests for exit behaviour on various kinds of
  shell errors as listed in the POSIX standard, including an error
  in a redirection of a function call.

Fixes: https://github.com/ksh93/ksh/issues/310
2021-05-19 06:59:18 +02:00
Martijn Dekker
07eb2040e8 Add regression test (re: e5e1d4b5) 2021-05-19 06:13:30 +02:00
Martijn Dekker
e5e1d4b53e Decrease SHLVL before doing 'exec' from main shell
Problem:

$ exec ksh
$ echo $SHLVL
2
$ exec ksh
$ echo $SHLVL
3
$ exec ksh
$ echo $SHLVL
4

...etc. SHLVL is supposed to acount the number of shell processes
that you need to exit before you get logged out. Since ksh was
replacing itself with a new shell in the same process using 'exec',
SHLVL should not increase.

src/cmd/ksh93/bltins/misc.c: b_exec():
- When about to replace the shell and we're not in a subshell,
  decrease SHLVL to cancel out a subsequent increase by the
  replacing shell. Bash and zsh also do this.
2021-05-19 00:08:12 +02:00
Martijn Dekker
c828ea8d0d Fix typeset -u/-l on NetBSD
On NetBSD, for some reason, the wctrans(3) and towctrans(3) C
library functions exist, but have no effect; the "toupper" and
"tolower" maps don't even translate case for ASCII, never mind wide
characters. This kills 'typeset -u' and 'typeset -l' on ksh, which
was the cause of most of the regression test failures on NetBSD.
Fallback versions for these functions are provided in init.c, but
were not being used on NetBSD because the feature test detected the
presence of these functions in the C library.

src/cmd/ksh93/features/locale:
- Replace the simple test for the presence of wctrans(3),
  towctrans(3), and the wctrans_t type by an actual feature test
  that checks that these functions not only compile, but are also
  capable of changing an ASCII 'q' to upper case and back again.

src/cmd/ksh93/sh/init.c: towctrans():
- Add wide character support to the fallback function, for whatever
  good that may do; on NetBSD, the wide-character towupper(3) and
  towlower(3) functions only change case for ASCII.
2021-05-18 18:26:33 +02:00
Martijn Dekker
7cb9318e02 tests/leaks.sh: increase Linux tolerance again (re: 16080141)
After the last increase from 4 to 6 bytes, there are still
intermittent false leaks.sh failures (different ones on each run)
on the GitHub CI runner on the 1.0 branch, which is compiled with
the OS's malloc (as opposed to ast vmalloc). Increase the byte
tolerance for the leaks test from 6 to 8 bytes on Linux when
compiling with standard malloc.
2021-05-16 06:31:33 +02:00
Martijn Dekker
153c4b56e8 Hopefully good fix for va_listval build fails (re: 41ed8047, etc.)
The last commit still failed to build on macOS M1. That va_listval
macro keeps causing trouble. It's an AST thing that is defined in
src/lib/libast/features/common. That looks like some incredibly
opaque attempt to make it compatible with everything, and clearly
it no longer works properly on all systems. I don't dare touch it,
though. That code looks like any minimal change will probably break
the build on some system or other.

src/lib/libast/features/hack:
- Add feature test to check if that macro needs (0) no workaround,
  or (1) the workaround from the 93v- beta, or (2) the FreeBSD one.
  Whichever version compiles first, it will use. If none does, this
  test will not output a value, which will be treated as 0.

src/lib/libast/hash/hashalloc.c,
src/lib/libast/string/tokscan.c:
- Update to use the result of the hack feature test.

src/lib/libast/Mamfile:
- Update for new #include dependencies.
2021-05-16 04:38:30 +02:00
Martijn Dekker
41ed8047d2 fix the @#$%@#$ build on macOS M1, again (re: 841c6800, c0fdc4a3)
This commit excludes all Apple systems from the workaround.
2021-05-15 02:49:25 +02:00
Martijn Dekker
c59d888394 Fix xtrace (shtests -x) for the regression tests
src/cmd/ksh93/tests/{shtests,_common}:
- When xtrace is active, set SECONDS to the float type so that
  the $SECONDS expansion in $PS4 shows fractional seconds.

src/cmd/ksh93/tests/*.sh:
- Various fixes to avoid command substitutions incorporating xtrace
  output into their results. Sometimes this is done by avoiding a
  preceding assignment on a command that redirects 2>&1 (as that
  will also redirect the preceding assignment and its xtrace,
  causing the command substitution to capture the xtrace); other
  times it was easiest to just turn off xtrace outright within the
  command substitution.

src/cmd/ksh93/tests/math.sh:
- Remove an obsolete 'fixme' note.
2021-05-14 19:56:11 +02:00
Martijn Dekker
16080141c5 tests/leaks.sh: increase Linux (standard malloc) tolerance
There are intermittent false failures on the GitHub CI runners on
the 1.0 branch, which is compiled with the OS's malloc (as opposed
to ast vmalloc). Increase the byte tolerance for the leaks test
from 4 to 6 bytes on Linux when compiling with standard malloc.
2021-05-14 16:47:59 +02:00
Martijn Dekker
e521b81636 Fix build on Ubuntu 18.04 ARMv7 (re: 4d7ea081)
hyenias writes, re the referenced commit:
> This has caused my Ubuntu 18.04 ARMv7 to fail to compile.
>
> /dev/shm/ksh/src/lib/libast/hash/hashalloc.c: In function 'hashalloc':
> /dev/shm/ksh/src/lib/libast/hash/hashalloc.c:156:11: error:
> incompatible types when assigning to type 'va_list * {aka
> __va_list *}' from type 'va_list {aka __va_list}'
>     tmpval = va_listval(va_arg(ap, va_listarg));
>            ^
> In file included from ./ast_common.h:192:0,
>                  from /dev/shm/ksh/src/lib/libast/include/ast_std.h:37,
>                  from /dev/shm/ksh/src/lib/libast/include/ast.h:36,
>                  from /dev/shm/ksh/src/lib/libast/hash/hashlib.h:34,
>                  from /dev/shm/ksh/src/lib/libast/hash/hashalloc.c:33:
> /dev/shm/ksh/src/lib/libast/hash/hashalloc.c:157:16: error:
> incompatible type for argument 2 of '__builtin_va_copy'
>     va_copy(ap, tmpval);
>                 ^
> /dev/shm/ksh/src/lib/libast/hash/hashalloc.c:157:16: note: expected
> '__va_list' but argument is of type 'va_list * {aka __va_list *}'
> mamake [lib/libast]: *** exit code 1 making hashalloc.o
> mamake: *** exit code 1 making lib/libast
> mamake: *** exit code 1 making all
> package: make done  at Fri May 14 06:10:16 EDT 2021 in
> /dev/shm/ksh/arch/linux.arm

src/lib/libast/hash/hashalloc.c,
src/lib/libast/string/tokscan.c:
- Revert the FreeBSD fix.
- Backport a conditional workaround for clang from ksh 93v- beta.
2021-05-14 16:38:55 +02:00
Martijn Dekker
4d7ea081d3 Fix build on macOS M1, FreeBSD powerpc64*, et al (?)
On some systems, the following won't compile because of the way the
macros are defined in the system headers:

	va_copy(ap, va_listval(va_arg(ap, va_listarg)));

The error from clang is something like:

  .../hashalloc.c:155:16: error: non-const lvalue reference to type
  '__builtin_va_list' cannot bind to a temporary of type 'va_list'
  (aka 'char *')
     va_copy(ap, va_listval(va_arg(ap, va_listarg)));
     ~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  ./ast_common.h:200:23: note: expanded from macro 'va_listval'
  #define va_listval(p) (p)
                        ^
  .../include/stdarg.h:27:53: note: expanded from macro 'va_copy'
  #define va_copy(dest, src)  __builtin_va_copy(dest, src)
                                                      ^~~
  1 error generated.
  mamake [lib/libast]: *** exit code 1 making hashalloc.o

This commit backports a FreeBSD build fix from:
https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=255308

Thanks to Chase <nicetrynsa@protonmail.ch> for the bug report.

src/lib/libast/hash/hashalloc.c,
src/lib/libast/string/tokscan.c:
- Store va_listval() result in variable and pass that to va_copy().
2021-05-14 04:52:29 +02:00
Martijn Dekker
53f4bc6a53 Re-fix 'test -t 1' in command substitutions (re: 090b65e7)
Since a command substitution no longer forks on non-permanently
redirecting standard output within it for a specific command,
test -t 1, [ -t 1 ], and [[ -t 1 ]] broke as follows:
v=$(test -t 1 >/dev/tty && echo ok) did not assign 'ok' to v.
This is because the assumption in tty_check() that standard output
is never on a terminal in a non-forked command substitution, added
in 55f0f8ce, was made invalid by 090b65e7.

src/cmd/ksh93/edit/edit.c: tty_check():
- Implement a new method. Return false if the file descriptor
  stream is of type SF_STRING, which is the case for non-forked
  command substitutions -- it means the sfio stream writes directly
  into a memory area. This can be checked with the sfset(3)
  function (see src/lib/libast/man/sfio.3). To avoid a segfault
  when accessing sh.sftable, we need to validate the FD first.

src/cmd/ksh93/tests/pty.sh:
- Add the above reproducer.
2021-05-14 04:52:18 +02:00
Martijn Dekker
cd39ea7863 Improve fix for not exporting readonly attribute (re: 7954855f)
The bitmask of attributes to export was repeatedly defined in three
different places, and that fix changed only one of them.

src/cmd/ksh93/sh/name.c:
- Single point of truth: define ATTR_TO_EXPORT macro with the
  bitmask of all the attributes to export (excluding NV_RDONLY).
- attstore(), pushnam(), sh_envgen(): Use the ATTR_TO_EXPORT macro,
  removing superflous NV_RDONLY handling from the former two.
2021-05-13 05:12:31 +02:00
Johnothan King
2e6346ef52 Fix math feature tests when compiling with tcc (re: 92f7ca54) (#303)
Commit 92f7ca54 broke compilation with tcc on Linux. The following
error would occur while compiling ksh with tcc:

  In file included from /home/johno/GitRepos/KornShell/ksh/src/cmd/ksh93/data/strdata.c:105:
  ./FEATURE/math:91: error: too many basic types
  mamake [cmd/ksh93]: *** exit code 1 making strdata.o

The build failure is fixed by backporting the relevant bugfix from
the 93v- version of iffe.

src/cmd/INIT/iffe.sh:
- Backport the 2013 iffe bugfix for the intrinsic function test to
  rule out type names (dated 2013-08-11 in the 93v- changelog).
2021-05-12 08:50:25 +02:00
Martijn Dekker
ac56614a95 Restore Solaris build after introducing 'noreturn' (re: c4f980eb)
The build started failing on Solaris Studio cc when 'noreturn' was
introduced, because the wrappers pass the -xc99 flag which sets the
compiler to C99 mode. 'noreturn' is a C11 feature. The
stdnoreturn.h header was correctly included but the compiler still
threw a syntax error (long path abbreviated below):

".../stk.c", line 124: warning: _Noreturn is a keyword in ISO C11
".../stk.c", line 124: warning: old-style declaration or incorrect
						type for: _Noreturn
".../stk.c", line 124: syntax error before or at: static

src/cmd/INIT/cc.sol11.*:
- Pass -std=c11 to cc instead of -xc99. At least on i386-64, this
  is sufficient to fix the build.

README.md, src/cmd/ksh93/README.md:
- Remove -xc99 from the Solaris build flags example as that is
  incompatible with -std=c11 (and was already redundant with the
  -xc99 in the wrappers).

src/cmd/ksh93/tests/basic.sh:
- Don't run a newly backported 93v- regression test on Solaris
  because it uses the 'join' command with process subsitutions;
  Solaris 11.4's join(1) hangs when trying to read from /dev/fd.
  This is not ksh's fault. (re: 59bacfd4)
2021-05-12 07:32:35 +02:00
Martijn Dekker
bf76268d24 tests/locale.sh: skip SJIS test on non-SJIS locale (re: c2cb0eae)
On Fedora, this regression test failure occurs:

	locale.sh[84]: 'read' doesn't skip multibyte input
				correctly (ja_JP.ujis, \x95\x5c)

This is a problem with the test; this Shift-JIS specific test
should not be run in a non-Shift-JIS locale. So this commit skips
it unless the locale string ends in '.SJIS' (case insensitive).

It also adds cleanup for the 'chr' variable's special attributes
in case that name is ever going to be used in another test.
2021-05-11 21:07:33 +02:00
Martijn Dekker
aa601a397d build system: do not look for nmake in $PATH
nmake was removed long ago (2940b3f5) and so were the outdated
Makefiles (6cc2f6a0). However, the build system still looked for an
AT&T nmake in $PATH. If a user had it installed, the build would
fail as the system tried to use it.

https://groups.google.com/g/korn-shell/c/2VK8kS0_VhA/m/-Rlnv7PRAgAJ

bin/package, src/cmd/INIT/package.sh:
- Remove all the code supporting nmake.
- Make 'bin/package test' work by simply exec'ing bin/shtests.

src/cmd/INIT/Mamfile:
- Do not install *.mk nmake support files.

lib/package/*.mk, src/cmd/INIT/*.mk:
- nmake support files removed.
2021-05-11 01:56:22 +02:00
Martijn Dekker
65fb288564 manual page: misc tweaks
src/cmd/ksh93/sh.1:
- The POSIX option description still said that attributes "such as
  integer and readonly" aren't imported from the environment. But
  as of 7954855f, the readonly attribute is never imported or
  exported. So change that to another example (left/right justify).
- Tweak idiosyncratic use of hyphens.
- be inputted => be input.
2021-05-11 01:56:10 +02:00
Martijn Dekker
7d455c3d1a include/version.h: 1.0.0-beta.2 version bump for 1.0 branch 2021-05-11 01:38:17 +02:00
Martijn Dekker
246062ff0b Release 1.0.0-beta.1
In May 2020, when every KornShell (ksh93) development project was
abandoned, development was rebooted in a new fork based on the last
stable AT&T version: ksh 93u+. Now, one year and hundreds of bug
fixes later, the first beta version is ready, and KornShell lives
again. This new fork is called ksh 93u+m as a permanent nod to its
origin; a standard semantic version number is added starting at
1.0.0-beta.1. Please test the beta and report any bugs you find,
or help us fix known bugs.
2021-05-10 18:42:42 +02:00
hyenias
92f7ca5423
Back port ksh93v- float, int, and exp10 changes from math.tab (#299)
src/cmd/ksh93/data/math.tab:
- Added exp10().
- Remove int() as being an alias to floor().
- Created entries for local float() and local int() which are
  defined in features/math.sh.

src/cmd/ksh93/features/math.sh:
- Backport floor() and int() related code from ksh93v-.

src/cmd/ksh93/sh.1:
- Sync man page to math.tab's potential functions.
2021-05-08 04:43:37 +01:00
Martijn Dekker
6b3805724d Re-obtain PID when optimising ( simple_command & ) (re: af6a32d1)
src/cmd/ksh93/sh/xec.c: sh_exec(): TCOM:
- In the referenced commit I'd accidentally deleted this line:
    shgd->current_pid = getpid();
  from the routine to optimise the ( simple_command & ) case.
  This resulted in the following regression test failure on
  ARM boxes:
    variables.sh[71]: Test 4: $RANDOM seed in ( simple_command & )
  The cause was that the current PID shgd->current_pid, which is
  factored into the seed, was not updated before reseeding.
  Apparently the system clock on ARM systems is not fine-grained
  enough to compensate.
2021-05-07 15:15:24 +01:00
Martijn Dekker
2758e5db19 Update #pragmas to make clang be quiet about historic practice
This adds a #pragma to disable -Wdeprecated-register* on newer
versions of clang. We could remove all use of the register keyword
instead, as modern compilers ignore it. But it's not harmful, and
for the time being I prefer not to do doing any reformatting or
changing the historic character of this code base.

The #pragmas are removed from src/lib/libast/include/ast.h, because
they're better placed in src/lib/libast/features/common which
generates ast_common.h which is included by everything.

* https://clang.llvm.org/docs/DiagnosticsReference.html#wdeprecated-register
2021-05-07 06:36:14 +01:00
Martijn Dekker
38654f0946 tests/builtins.sh: fix disabled 'read -s' test
src/cmd/ksh93/tests/builtins.sh:
- An original AT&T test for 'read -s' was disabled and marked
  FIXME. Fix the invalid invocation and check that 'read -s'
  actually writes to the history file.
- Remove a temporary 'command -p ls' debug test that I accidentally
  committed (re: a197b042).
2021-05-05 03:51:40 +01:00
hyenias
655c4be1c1
Correct regression for compound arithmetic expressions (re: 642a1053) (#297)
I did not realize that lvalue->nosub and lvalue->sub variables are
not reset when another assignment occurs later down the line.
Example: (( arr[0][1]+=1, arr[2]=7 ))

src/cmd/ksh93/sh/arith.c: arith():
- For assignment operations, reset lvalue's nosub and sub variables
  so the target for the next assignment is not redirected.

src/cmd/ksh93/tests/arrays2.sh:
- Add in a few regression tests that utilize compound arithmetic
  expressions having at least an assignment operation (+=) followed
  by a normal assignment (=).
2021-05-05 03:29:19 +01:00
Martijn Dekker
a197b0427a Fix two more 'command' bugs
BUG 1: Though 'command' is specified/documented as a regular
builtin, preceding assignments survive the invocation (as with
special or declaration builtins) if 'command' has no command
arguments in these cases:

$ foo=wrong1 command; echo $foo
wrong1
$ foo=wrong2 command -p; echo $foo
wrong2
$ foo=wrong3 command -x; echo $foo
wrong3

Analysis: sh_exec(), case TCOM (simple command), contains the
following loop that skips over 'command' prefixes, preparsing any
options and remembering the offset in the 'command' variable:

src/cmd/ksh93/sh/xec.c
1059 while(np==SYSCOMMAND || !np && com0
     && nv_search(com0,shp->fun_tree,0)==SYSCOMMAND)
1060 {
1061         register int n = b_command(0,com,&shp->bltindata);
1062         if(n==0)
1063                 break;
1064         command += n;
1065         np = 0;
1066         if(!(com0= *(com+=n)))
1067                 break;
1068         np = nv_bfsearch(com0, shp->bltin_tree, &nq, &cp);
1069 }

This skipping is not done if the preliminary b_command() call on
line 1061 (with argc==0) returns zero. This is currently the case
for command -v/-V, so that 'command' is treated as a plain and
regular builtin for those options.

The cause of the bug is that this skipping is even done if
'command' has no arguments. So something like 'foo=bar command' is
treated as simply 'foo=bar', which of course survives.

So the fix is for b_command() to return zero if there are no
arguments. Then b_command() itself needs changing to not error out
on the second/main b_command() call if there are no arguments.

src/cmd/ksh93/bltins/whence.c: b_command():
- When called with argc==0, return a zero offset not just for -v
  (X_FLAG) or -V (V_FLAG), but also if there are no arguments left
  (!*argv) after parsing options.
- When called with argc>0, do not issue a usage error if there are
  no arguments, but instead return status 0 (or, if -v/-V was given,
  status 2 which was the status of the previous usage message).
  This way, 'command -v $emptyvar' now also works as you'd expect.

BUG 2: 'command -p' sometimes failed after executing certain loops.

src/cmd/ksh93/sh/path.c: defpath_init():
- astconf() returns a pointer to memory that may be overwritten
  later, so duplicate the string returned. Backported from ksh2020.
  (re: f485fe0f, aa4669ad, <https://github.com/att/ast/issues/959>)

src/cmd/ksh93/tests/builtins.sh:
- Update the test for BUG_CMDSPASGN to check every variant of
  'command' (all options and none; invoking/querying all kinds of
  command and none) with a preceding assignment. (re: fae8862c)
  This also covers bug 2 as 'command -p' was failing on macOS prior
  to the fix due to a loop executed earlier in another test.
2021-05-05 02:43:18 +01:00
Martijn Dekker
143ff27a91 emacs: fix keys w/ repeat parameters repeating extra (re: 29b11bba)
@JohnoKing writes:
> In emacs mode, using Alt+D or Alt+H with a repeat parameter
> results in the deletion of extra characters. Reproducer:
>
> $ set -o emacs
> $ foo bar delete add   # <Ctrl+A> <ESC+3+Alt+D>
> $ d  # Should be ' add'
>
> $ foo bar delete add   # <ESC+3+Alt+H>
> $ f  # Should be 'foo '
>
> [...] this bug also affects the Delete and Arrow keys [...].
> Reproducer:
>
> $ test_string <Ctrl+A> <ESC+3+Delete>
>	# This will delete all of 'test', which is four characters
> $ test_string <Ctrl+A> <ESC+4+Right Arrow>
>	# This should move the cursor to '_', not 's'

src/cmd/ksh93/edit/emacs.c: ed_emacsread():
- Revert part of 29b11bba: once again set 'count' to
  'vt220_save_repeat' instead of adding the value.
- do_escape: If the escape() function (which handles both ESC
  repeat counts and commands like ESC d and ESC h) returns a repeat
  count, do not use the saved repeat count for v220 sequences.

src/cmd/ksh93/tests/pty.sh:
- Test the four reproducers above.

Fixes: https://github.com/ksh93/ksh/issues/292
2021-05-04 05:10:32 +01:00
Martijn Dekker
da4bf85f27 COMPATIBILITY: update to 93u+ entry re: 5ed9ffd6, 9f2066f1 2021-05-04 03:14:05 +01:00
hyenias
642a105351
Fix arithmetic assignment operations for multidimensional indexed arrays (#296)
This PR corrects #168 for indexed arrays having more than one
level. Turns out ksh was only keeping track of the subscript number
for assignment in lvalue's nosub variable. By saving the actual
subscript reference, the result can be assigned to its proper
destination instead of putting the result into the last looked
value or subscript location.

src/cmd/ksh93/include/streval.h: struct lval:
- Create a new pointer named sub to hold the reference that nosub
  describes.

src/cmd/ksh93/sh/arith.c: arith():
- Adjust LOOKUP: for lvalue ARITH_ASSIGNOP operations on indexed
  arrays to save the np of the destination subscript for later use.
- Adjust ASSIGN: to act when lvalue's nosub > 0 which happens as
  the last step in the arithmetic parsing loop for assignment
  operations. Only indexed arrays will have a nosub value > 0. All
  others have a nosub of 0 unless they are involved in a unary
  operation (++, --) which sets nosub to -1. All said in the
  context of assignment operations like (( arr[0][1] += 1 )).

src/cmd/ksh93/sh/streval.c:
- Initialize the new sub pointer to 0.

src/cmd/ksh93/tests/arrays2.sh:
- Created a few multidimensional indexed array tests for assignment
  operations like += as an example.

Resolves: https://github.com/ksh93/ksh/issues/168
2021-05-04 03:13:14 +01:00
Martijn Dekker
d309d604e7 POSIX: 'command': don't disable declaration proprts (re: b9d10c5a)
Following the resolution of Austin Group bug 1393[*] that is set to
be included in the next version of the POSIX standard, the
'command' prefix in POSIX mode (set -o posix) no longer disables
the declaration properties of declaration built-ins.
[*] https://austingroupbugs.net/view.php?id=1393

src/cmd/ksh93/sh/parse.c: lex():
- Skip the 'command' prefix even in POSIX mode so that any
  declaration commands prefixed by it are treated as such in xec.c
  (sh_exec()).

src/cmd/ksh93/sh/xec.c: sh_exec():
- The foregoing change reintroduced a variant of BUG_CMDSPEXIT: the
  shell exits on something like 'command export readonlyvar=foo'.
  This now fixes that bug for both POSIX and non-POSIX mode. When
  calling nv_setlist() to process true shell assignments, and there
  is a 'command' prefix, push a shell context and use sigsetjmp to
  intercept any errors in assignments and stop the shell exiting.

src/cmd/ksh93/tests/builtins.sh:
- Borrow the BUG_CMDSPEXIT regression test from modernish and adapt
  it for ksh. (I'm the author so yes, I can do this.) Original:
  https://github.com/modernish/modernish/blob/ae8fe9c3/lib/modernish/tst/builtin.t#L80-L109
2021-05-04 00:52:10 +01:00
Martijn Dekker
1a1e3709c2 tests/subshell.sh: replace use of unportable seq command 2021-05-03 21:51:03 +01:00
Martijn Dekker
7a2d3564b6 emacs/vi: Fix <Tab> behaviour after command substitution
Tab completion in emacs and vi wrongly parses and executes command
substitutions. Example reproducers:

  $ $(~)<Tab>		# Result:
  $ $(~)ksh[1]: /home/johno: cannot execute [Is a directory]

  $ $(~ksh)<Tab>	# Result:
  $ $(~ksh)ksh: /home/johno/GitRepos/KornShell/ksh: cannot execute [Is a directory]

  $ $(echo true)<Tab>	# Result:
  $ /usr/bin/true	# or just 'true' -- it's unpredictable

In addition, backtick command substitutions had the following bug:

  $ `echo hi`<Tab>	# Result:
  $ `echo hi`ksh: line 1: BUG_BRACQUOT_test.sh: not found

(where BUG_BRACQUOT_test.sh happens to be lexically the
first-listed file in my ksh development working directory).

There's also a crash associated with this due to an access beyond
buffer boundaries, which is only triggered on some systems (macOS
included).

src/cmd/ksh93/edit/completion.c:
- find_begin():
  * When finding the beginning of a command substitution and the
    last character is ')', do not increase the character pointer
    cp. Increasing it caused the condition 'if(c && c==endchar)' in
    the 'default:' block to be true, causing 'return(xp);' to be
    executed, which returns a pointer the beginning of the command
    substitution to ed_expand() on line 290, so that ed_expand()
    eventually executes the command substitution with the
    sh_argbuild() call on line 349. After deleting this 'else
    cp++', that statement 'if(c && c==endchar) return(xp);' is not
    executed and `find_begin()` returns the null pointer, which
    avoids anything being executed. Thanks to @JohnoKing:
    https://github.com/ksh93/ksh/issues/268#issuecomment-817249164
  * Add code for properly skipping over backtick-style command
    substitutions, based on the $( ) code.
- ed_expand(): Avoid out[-1] reading one byte to the left of
  outbuff by first checking that out>outbuff. Thanks to @JohnoKing
  for using ASan to find the location of the crash:
  https://github.com/ksh93/ksh/issues/268#issuecomment-825574885

src/cmd/ksh93/tests/pty.sh:
- Test for the bugs detailed above.

Resolves: https://github.com/ksh93/ksh/issues/268
2021-05-03 18:49:10 +01:00
Martijn Dekker
33269cae61 tests/variables.sh: fix ( echo $RANDOM & ) test (re: af6a32d1)
On slower systems it could fail with an arithmetic syntax error
because the output was verified before it had been written.

Also make another test xtrace-proof.
2021-05-03 18:14:34 +01:00
Martijn Dekker
6d54faf9f5 tests/leaks.sh: fix intermittent fails on Gentoo (re: 005d38f41)
This applies when ksh is compiled with standard malloc.

Apparently, 1024 iterations is not enough on Gentoo Linux i386, at
least not when running the full test suite. The leak tests fail
intermittently and different tests fail each time, but always with
a leak of exactly 36864 bytes for each failing test. So those
failures are clearly spurious. Doubling the number of iterations
seems to make them go away.
2021-05-03 16:55:42 +01:00
Martijn Dekker
dd5455ac5f tests/io.sh: fix spurious failure on Gentoo i386 (re: c4f980eb) 2021-05-03 16:38:37 +01:00
Johnothan King
d5b94a3ff8
Fix some bugs in the regression tests and add more regression tests (#295)
src/cmd/ksh93/tests/{basic.sh,builtins.sh,shtests}:
- Redirect error output from the ulimit builtin to silence irrelevant
  errors in the regression tests (these errors may occur when a
  command such as 'ulimit -t 4' is run before the regression tests).
- Shellquote the error messages from the getconf regression tests.

src/cmd/ksh93/tests/{arrays,io,variables}.sh:
- Backport the ksh2020 regression tests for the following bugs:
  https://github.com/att/ast/issues/23
  https://github.com/att/ast/issues/203
  https://github.com/att/ast/issues/472
  https://github.com/att/ast/issues/492
- Minor fix to POSIX mode regression tests in ksh93v-. In ksh93v-,
  [[ -o ?posix ]] doesn't return an error (because it's implemented
  in the bash mode). However, 'set -o posix' will fail in ksh93v-
  if it's not in bash compatibility mode, which causes this test
  script to exit prematurely.

src/cmd/ksh93/tests/{basic,pty}.sh:
- Add test for https://github.com/att/ast/issues/1461
- The ksh2020 fix for [ -t 1 ] in non-forking command substitutions
  caused the following bug in interactive shells:
    $ ( [ -t 1 ]; echo $? )
    1  # Always fails
  To avoid introducing this bug, this commit adds a regression
  test for it.

src/cmd/ksh93/tests/functions.sh:
- Add test for https://github.com/att/ast/issues/1160
  Put the test to the start of functions.sh (if it's at the end
  of the script, it refuses to fail under ksh2020). Output from
  this regression test when run against ksh2020:
    functions.sh[46]: eval'ing function dumps function body to
    stdout (got $' { eval "bar() { FAILURE; }"; }\n { FAILURE; }')
2021-05-03 06:52:27 +01:00
Martijn Dekker
5ed4c71fca Fix regression caused by ${var:-'{}'} fix (re: f31e3687)
The regression is:

  quoting.sh[189]: expansion of "{q:+'}" not correct when q unset

The failure was that, for unset q, "${q:+'}q${q:+'}" yielded empty
and not 'q'. This is because the single quotes within the double
quotes were erroneously parsed as meaningful.

The originally used ST_QUOTE state table (see data/lexstates.c),
where no quote character has any special meaning, was for avoiding
this problem.

The newly introduced ST_MOD1 state table is a copy of ST_QUOTE
except the ' has been given its special meaning back. We need this
to fix #290, but only for unquoted expansions.

So we need to go back to using ST_QUOTE if the string is quoted
(mp->quote) and we're not parsing a substitution that uses patterns
where quotes are significant (newops, ST_MOD2), i.e., only for
old-style ST_MOD1 operators.

src/cmd/ksh93/sh/macro.c: varsub():
- When the ${var<OP>string} expansion is quoted, and of an old
  (S_MOD1) type, then use the ST_QUOTE state table to skip over it
  instead of the new ST_MOD1 one.
2021-05-03 05:55:47 +01:00
Martijn Dekker
af6a32d14f
Fix $RANDOM to act consistently in subshells (#294)
This fixes the following:
1. Using $RANDOM in a virtual/non-forked subshell no longer
   influences the reproducible $RANDOM sequence in the parent
   environment.
2. When invoking a subshell $RANDOM is now re-seeded (as mksh and
   bash do) so that invocations in repeated subshells (including
   forked subshells) longer produce identical sequences by default.
3. Program flow corruption that occurred in scripts on executing
   ( ( simple_command & ) ).

src/cmd/ksh93/include/variables.h:
- Move 'struct rand' here as it will be needed in subshell.c. Add
  rand_seed member to save the pseudorandom generator seed. Remove
  the pointer to the shell state as it's redundant.

src/cmd/ksh93/sh/init.c:
- put_rand(): Store given seed in rand_seed while calling srand().
  No longer pointlessly limit the number of possible seeds with the
  RANDMASK bitmask (that mask is to limit the values to 0-32767,
  it should not limit the number of possible sequences to 32768).
- nget_rand(): Instead of using rand(), use rand_r() to update the
  random_seed value. This makes it possible to save/restore the
  current seed of the pseudorandom generator.
- Add sh_reseed_rand() function that reseeds the pseudorandom
  generator by calling srand() with a bitwise-xor combination of
  the current PID, the current time with a granularity of 1/10000
  seconds, and a sequence number that is increased on each
  invocation.
- nv_init(): Set the initial seed using sh_reseed_rand() here
  instead of in sh_main(), as this is where the other struct rand
  members are initialised.

src/cmd/ksh93/sh/main.c: sh_main():
- Remove the srand() call that was replaced by the sh_reseed_rand()
  call in init.c.

src/cmd/ksh93/sh/subshell.c: sh_subshell():
- Upon entering a virtual subshell, save the current $RANDOM seed
  and state, then reseed $RANDOM for the subshell.
- Upon exiting a virtual subshell, restore $RANDOM seed and state
  and reseed the generator using srand() with the restored seed.

src/cmd/ksh93/sh/xec.c: sh_exec():
- When optimizing out a subshell that is the last command, still
  act like a subshell: reseed $RANDOM and increase ${.sh.subshell}.
- Fix a separate bug discovered while implementing this. Do not
  optimize '( simple_command & )' when in a virtual subshell; doing
  this causes program flow corruption.
- When optimizing '( simple_command & )', also reseed $RANDOM and
  increment ${.sh.subshell}.

src/cmd/ksh93/tests/subshell.sh,
src/cmd/ksh93/tests/variables.sh:
- Add various tests for all of the above.

Co-authored-by: Johnothan King <johnothanking@protonmail.com>
Resolves: https://github.com/ksh93/ksh/issues/285
2021-05-03 04:03:46 +01:00
Martijn Dekker
f31e368795 Fix remaining bug in ${var:-'{}'} (re: d087b031)
The following problems remained:

$ var=x; echo ${var:-'{}'}
x}
$ var=; echo ${var:+'{}'}
}

src/cmd/ksh93/sh/macro.c: varsub():
- Use the new ST_MOD1 state table to skip over ${var-'foo'}, etc.
  instead of ST_QUOTE. In ST_MOD1 the ' is categorised as S_LIT
  which causes the single quotes to be skipped over correctly.
  See d087b031 for more info.

src/cmd/ksh93/tests/quoting2.sh:
- Add tests for this remaining bug.
- Make the new test xtrace-proof.

Resolves: https://github.com/ksh93/ksh/issues/290 (again)
2021-05-03 03:14:30 +01:00
Johnothan King
1aec9b06dd
Fix more bugs that occur when enabling ASan (#293)
src/cmd/ksh93/{bltins/typeset,sh/name,sh/nvtree,sh/nvtype}.c:
- Replace more instances of memcmp with strncmp to fix
  heap-buffer-overflow errors when running the regression tests
  with ASan enabled.

src/cmd/ksh93/edit/vi.c:
- Fix an invalid dereference of the 'p' pointer to fix a crash in
  vi mode when entering a comment in the command history. This
  bugfix was backported from ksh2020:
  https://github.com/att/ast/issues/798

src/cmd/ksh93/tests/pty.sh:
- Add a regression test for the vi mode crash.
2021-05-02 04:06:30 +01:00
Martijn Dekker
88a1f3d661 Fork before entering shared-state command substitution
The code contains various checks to see if a subshell needs to
fork, like this one in the ulimit builtin:

	if(shp->subshell && !shp->subshare)
		sh_subfork();

All checks of this form are fatally broken, as each one of them
causes shared-state command substitutions to ignore parent virtual
subshells.

Currently the only feasible way to fix this is to fork a virtual
subshell before executing a shared-state command substitution in
it. In the long term I think shared-state command substitutions
should probably be redesigned to disassociate them completely from
the virtual subshell mechanism.

src/cmd/ksh93/sh/macro.c: comsubst():
- If we're in a non-subshare virtual subshell, fork it before
  entering a type 2 (subshare) command substitution.

src/cmd/ksh93/sh/subshell.c:
- sh_assignok(): Remove subshare fix from 911d6b06 as it's
  redundant now that the parent of a subshare is never a virtual
  subshell. Go back to not doing anything if the current "subshell"
  is a subshare.
- sh_subtracktree(), sh_subfuntree(): Similarly, remove the
  now-redundant subshare fixes from 13c57e4b.

src/cmd/ksh93/sh/xec.c: sh_exec():
- Fix a separate bug: only fork a virtual subshell before running a
  background job if that "subshell" is not a subshare.

src/cmd/ksh93/tests/subshell.sh:
- Add test for bug fixed in xec.c.
- Add tests for 'ulimit', 'builtin' and 'exec' run in subshare
  within subshell -- all commands that use checks of the form
  'if(sh.subshell && !sh.subshare) sh_subfork();'.

Resolves: https://github.com/ksh93/ksh/issues/289
2021-05-01 00:47:39 +01:00
Martijn Dekker
72fe631b2f typeset: only run sh_assignok() for variables (re: a2f13c19)
src/cmd/ksh93/bltins/typeset.c:
- setall(): Only run sh_assignok() if troot points to the variable
  tree. For instance, it's pointless to run it for an alias.
- Remove vestigial SHOPT_BSH code. The ast-open-history repo shows
  that earlier SHOPT_BSH code was removed on 2008-06-02 and
  2005-05-22. This may have been experimental code for increased
  compatibility with the ancient Bourne shell. There was never any
  documentation.
2021-05-01 00:43:12 +01:00
Govind Kamat
7439e3dffe Parse quotes when extracting words from command history (#291)
This avoids splitting on quoted whitespace when extracting words
from the command history using the emacs M-. or vi _ command.

Example: if the prior command is

$ ls Stairway\ To\ Heaven.mp3

then, M-. in Emacs editing mode (and _ in vi mode) now inserts
Stairway\ To\ Heaven.mp3 instead of Heaven.mp3. The behavior is
similar for 'Stairway To Heaven.mp3' and "Stairway To Heaven.mp3".

src/cmd/ksh93/edit/history.c: hist_word():
- Skip over single-quoted and double-quoted strings and
  backslash-escaped characters.

src/cmd/ksh93/tests/pty.sh:
- Add regression test for this feature in vi mode. Since emacs and
  vi both use the same code for this, that should be good enough.

Co-authored-by: Martijn Dekker <martijn@inlv.org>
2021-04-30 20:18:07 +01:00
Martijn Dekker
d087b031f0 Fix single quotes in expansion operator string (re: 5ed9ffd6)
The referenced commit introduced the following bug:

> The closing quote does not appear to be registering during the
> parse of the following:
>
>	echo ${var:+'{}'}
>
> Within a script, this will result in:
>
>	syntax error at line 1: `'' unmatched

src/cmd/ksh93/data/lexstates.c,
src/cmd/ksh93/include/lexstates.h:
- Add new ST_MOD1 state table that is a copy of ST_QUOTE, but adds
  a special meaning (ST_LIT) for the single quote (position 39).

src/cmd/ksh93/sh/lex.c: sh_lex():
- For parameter expansion operators with old-style quoting
  (S_MOD1), use the new ST_MOD1 state table instead of ST_QUOTE.
  This causes single quotes within them to be processed properly.

src/cmd/ksh93/tests/quoting2.sh:
- Add tests.

Thanks to @gkamat for the bug report.
Resolves: https://github.com/ksh93/ksh/issues/290
2021-04-30 05:28:21 +01:00
Martijn Dekker
090b65e79b Fix fork after redirecting stdout in subshare (re: 500757d7)
Previously, command substitutions executed as virtual subshells
were always forked if any command was run within them that
redireceted standard output, even if the redirection was local to
that command.

Commit 500757d7 removed the check for a shared-state command
substitution (subshare), so introduced a bug where even that would
fork, causing it to stop sharing its state.

We can further improve on that fix by only forking if the
redirection is permanent as with `exec` or `redirect`. There should
be no need to do that if the redirection is local to a command run
within the command substitution, as the file descriptor is restored
when that command finishes, which is still within the command
substitution.

src/cmd/ksh93/sh/io.c: sh_redirect():
- Only fork upon redirecting stdout if the virtual subshell is a
  command substitution, and if the redirection is permanent
  (flag==1 or flag==2).
2021-04-26 18:22:17 +01:00
Martijn Dekker
2aad3cab06 Add ksh 93u+m contributors notice to 964 copyright headers 2021-04-26 00:19:31 +01:00
Johnothan King
1d9093e603
Add support for process substitutions to the deparser (#288)
Like tdump() and trestore() before commit 32d1abb1, sh_deparse() fails
to handle process substitutions correctly. This limitation of the shell
deparser is rather minor since it's unused. However, seeing as the
deparser was left in the code base intentionally it should at least
function properly.

src/cmd/ksh93/sh/deparse.c:
- Add a PROCSUBST flag for handling process substitutions in
  sh_deparse().
- If we're handling a process substitution, add an ending ')'
  without an extra newline.
- Avoid adding an extra ' &' to commands inside of a process
  substitution. An extra ' &' is only added if the FAMP and FINT
  flags are set, which indicates the command was spawned as a separate
  job with '&'.
- Add process substitution handling to 'p_redirect' by calling p_tree()
  when encountering a process substitution.
2021-04-26 00:12:29 +01:00
Martijn Dekker
295cce2c6d Improve fix for 'unset -f' in sub-subshell (re: 07faf384, 13c57e4b)
src/cmd/ksh93/bltins/typeset.c:
- Removing the nv_search() call altogether was actually not
  neccessary, I was just searching the wrong tree: instead of
  sh.fun_base, simply search the current sh.fun_tree which has a
  view to all the layered parent subshell copes. It is not going to
  find it in the current subshell tree but will find it in one of
  the parent trees if it exists. The cost of an unnecessary dummy
  is negligible, but so is the cost of this search, and doing it is
  more correct.
2021-04-24 21:26:17 +01:00
Johnothan King
24b7fcb771
Don't detect unset functions with whence (re: 13c57e4b) (#287)
src/cmd/ksh93/bltins/whence.c:
- The previous commit that fixed 'unset -f' in virtual subshells left
  one bug. The type builtin (or 'whence -v') could still find the unset
  function in virtual subshells:
    $ foo() { echo foo; }
    $ (unset -f foo; type foo)
    foo is an undefined function
  To fix this bug, avoid detecting functions in the whence builtin
  unless they have the NV_FUNCTION flag.

src/cmd/ksh93/tests/subshell.sh:
- Add a regression test for using 'type' on a function unset inside of
  a virtual subshell.
2021-04-24 21:25:45 +01:00