1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-03-09 15:50:02 +00:00
cde/src/cmd/ksh93
Martijn Dekker dde8da6769 Fix history comment character in history expansion (re: 41ee12a5)
Reproducer: on an interactive shell with the -H option on,

  $ v=foo
  $ print ${#v}

does not print anything (but should print "3").
The 'print' line also is not added to the history.

This bug was exposed by commit 41ee12a5 which enabled the history
comment character by default, setting it to '#' as on bash. When it
was disabled by default, this bug was rarely exposed.

The problem happens here:

src/cmd/ksh93/edit/hexpand.c
203: if(hc[2] && *cp == hc[2]) /* history comment designator, skip rest of line */
204: {
205: 	stakputc(*cp++);
206: 	stakputs(cp);
207: 	DONE();
208: }

The DONE() macro sets the HIST_ERROR bit flag:

src/cmd/ksh93/edit/hexpand.c
45: #define	DONE()	{flag |= HIST_ERROR; cp = 0; stakseek(0); goto done;}

For the history comment character that is clearly wrong, as no
error has occurred.

There is another problem. The documentation I added for history
expansion states this bit, which is based on bash's behaviour:

    If a word on a command line begins with the history comment
    character #, history expansion is ignored for the rest of that
    line.

With an expansion like ${#v}, the word does not begin with # so
history expansion should not have parsed that as a comment
character. The intention was to act like bash.

src/cmd/ksh93/edit/hexpand.c:
- Split the DONE() macro into DONE and ERROROUT of which only the
  latter sets the HIST_ERROR bit flag. Change usage accordingly.
  Thix fixes the first problem.
- Don't make these new macros function-style macros (with ()) as
  they end in a goto, so that's a bit misleading.
- Add is_wordboundary() which makes a best-effort attempt to
  determine if the character following the specified character is
  considered to start a new word by shell grammar rules. Word
  boundary characters are whitespace and: |&;()`<>
- Only recognise the history comment character if is_wordbounary()
  returns true for the previous character. This fixes the second
  problem.

Thanks to @jghub for the bug report.
Resolves: https://github.com/ksh93/ksh/issues/513
2022-08-16 22:00:20 +01:00
..
bltins Fix crashes on redefining/unsetting predefined alias (re: 7e7f1372) 2022-08-06 11:54:33 +01:00
data remove ${.sh.pool} predefined variable entry (re: 3613da42) 2022-08-07 15:28:52 +01:00
edit Fix history comment character in history expansion (re: 41ee12a5) 2022-08-16 22:00:20 +01:00
features Update author attributions in all source files 2022-07-31 00:47:08 +02:00
fun Update author attributions in all source files 2022-07-31 00:47:08 +02:00
include Fix history comment character in history expansion (re: 41ee12a5) 2022-08-16 22:00:20 +01:00
sh Fix crashes on redefining/unsetting predefined alias (re: 7e7f1372) 2022-08-06 11:54:33 +01:00
tests Fix history comment character in history expansion (re: 41ee12a5) 2022-08-16 22:00:20 +01:00
builtins.mm Various minor capitalization and typo fixes (#371) 2021-12-13 01:49:42 +01:00
COMPATIBILITY Release 93u+m/1.0.0-rc.1 2022-07-25 04:03:16 +02:00
DESIGN [v1.0] remove deparse.c 2022-06-09 03:07:34 +01:00
ksh-regress.rt Fix various typos, man page issues and improve the documentation (#415) 2022-01-07 16:17:55 +00:00
ksh-regress.tst Fix various typos, man page issues and improve the documentation (#415) 2022-01-07 16:17:55 +00:00
mamexec Fix various minor problems and update the documentation (#237) 2021-03-21 14:39:03 +00:00
Mamfile Re-backport atomic job locking from 93v- (re: 52067c3d, 595a0a56) 2022-08-01 05:41:52 +02:00
mamstate.c Update author attributions in all source files 2022-07-31 00:47:08 +02:00
nval.3 Yet more misc. cleanups; rm SHOPT_PFSH, SHOPT_TYPEDEF 2022-02-10 21:04:56 +00:00
OBSOLETE Fix many spelling errors and word repetitions (#188) 2021-02-20 03:22:24 +00:00
PROMO.mm Various minor capitalization and typo fixes (#371) 2021-12-13 01:49:42 +01:00
README src/cmd/INIT/package.sh: add install subcommand 2022-08-01 19:10:37 +02:00
README-AUDIT.md Fix a few minor issues (#473) 2022-03-11 21:18:42 +01:00
RELEASE Yet more accumulated tweaks and cleanups 2022-06-18 23:29:49 +01:00
RELEASE88 Various minor capitalization and typo fixes (#371) 2021-12-13 01:49:42 +01:00
RELEASE93 Some more accumulated minor tweaks and cleanups 2022-01-25 16:13:15 +00:00
sh.1 sh.1: add spacing between paragraphs and a missing word (#502) 2022-08-01 21:43:33 +02:00
sh.memo Some more accumulated minor tweaks and cleanups 2022-01-25 16:13:15 +00:00
shell.3 Various minor cleanups and fixes 2022-01-20 00:54:42 +00:00
SHOPT.sh Remove ineffective check for login shell; require -p for suid/sgid 2022-07-21 05:51:07 +02:00
TYPES Allow proper tilde expansion overrides (#225) 2021-03-17 21:07:14 +00:00

This directory, and its subdirectories contain the source code
for ksh-93; the language described in the second edition of
the book, "The KornShell Command and Programming Language," by
Morris Bolsky and David Korn which is published by Prentice Hall.
ksh-93 has been compiled and run on several machines with several
operating systems.  The end of this file contains a partial list of
operating systems and machines that ksh-93 has been known to run on.

Most of the source code for ksh is in the src/cmd/ksh93/sh
directory. For information on what's where, see the file DESIGN.

#### COMPILE-TIME OPTIONS ####

The SHOPT.sh file contains several compilation options that can be set
before compiling ksh.  Options are of the form SHOPT_option and become
#define inside the code.  These options are set to their recommended
value and some of these may disappear as options in future releases.
A value of 0 represents off, 1 represents on, no value means probe. For
options where no feature probe is available, probe is the same as off.

The options have the following defaults and meanings:

    ACCT         off Shell accounting.
                     Noted by "L" in the version string when enabled.
                     See README-AUDIT.md.

    ACCTFILE     off Enable per user accounting info.
                     See README-AUDIT.md.

    AUDIT        on  For auditing specific users.
                     Noted by "A" in the version string when enabled.
                     See README-AUDIT.md.

    AUDITFILE    "/etc/ksh_audit"
                     See README-AUDIT.md.

    BGX          on  Enables background job extensions. Noted by "J" in the
                     version string when enabled. (1) JOBMAX=n limits the
                     number of concurrent background jobs to n; the (n+1)th
                     background job will block until a running background job
                     completes. (2) SIGCHLD traps are queued so that each
                     completing background job gets its own trap; $! is set to
                     the job PID and $? is set to the job exit status at the
                     beginning of the trap.

    BRACEPAT     on  Brace expansion. Expands abc{d,e}f to abcdf abcef.
                     This feature was inspired by the C shell.

    CMDLIB_HDR   "<cmdlist.h>"
                     The header in which you can provide a custom list of
                     libcmd commands to provide as path-bound built-ins.

    CMDLIB_DIR   "\"/opt/ast/bin\""
                     The default virtual directory prefix for path-bound
                     built-ins. The value must include double quotes.

    CRNL         off <cr><nl> treated as <nl> in shell grammar.

    DEVFD            Use the more secure /dev/fd mechanism instead of FIFOs for
                     process substitutions. On by default on OSs with /dev/fd.

    DYNAMIC      off Dynamic loading of builtins. Requires dlopen() interface
                     and dynamic libshell, libdll and libast libraries.

    ECHOPRINT    off Make echo equivalent to print.

    EDPREDICT    off Enables history pattern search menu. As you begin a line
                     with a #, the following characters are treated as a shell
                     pattern and cause matching lines from the history file to
                     be displayed as a numbered list as you type. You can
                     scroll up and down this list or you can use <ESC>nTAB to
                     make this the current line (n defaults to 1 if omitted).
                     Experimental. Bugs: https://github.com/ksh93/ksh/issues/233

    ESH          on  Compile with emacs command line editing.  The original
                     emacs line editor code was provided by Mike Veach at IH.

    FILESCAN     on  Allows fast reading of files using:
                         while < file; do ...; done
                     Each line is stored in $REPLY and fields in each
                     line can be accessed as positional parameters.

    FIXEDARRAY   on  When using typeset, a name in the format NAME[N]
                     creates a fixed-size array and any attempt to access a
                     subscript N or higher is an error. Multidimensional
                     fixed-size arrays NAME[N1][N2]... are also supported.

    GLOBCASEDET      Adds the 'globcasedetect' shell option. When this shell
                     option is turned on, pathname expansion (globbing)
                     and file name listing and completion automatically become
                     case-insensitive on file systems where the difference
                     between upper- and lowercase is ignored for file names.
                     This compile-time option is enabled by default on operating
                     systems that can support case-insensitive file systems.

    HISTEXPAND   on  Enable !-style history expansion similar to csh(1).
                     This is turned on by the -H/--histexpand shell option and
                     can be modified using --histreedit and --histverify.

    KIA          off Allow generation of shell cross-reference database with -R.
                     As of 2021-05-10, no tool that can parse this database is
                     known. If you know of any, please contact us.

    MULTIBYTE        Multibyte character handling. This is on by default unless
                     the flag -DAST_NOMULTIBYTE is passed to the compiler via
                     CCFLAGS. The UTF-8 character set is fully supported.

    NAMESPACE    on  Adds a 'namespace' reserved word that allows defining name
                     spaces. Variables and functions defined within a block like
                        namespace ExampleSpace { commandlist; }
                     all have their names automatically prefixed with
                     '.ExampleSpace.' when defining or using them, creating a
                     separate space of names unique to the block. Outside of
                     the namespace block, they can be accessed using
                     .ExampleSpace.function or ${.ExampleSpace.variable}.
                     Name spaces within name spaces are also supported.

    MKSERVICE    off Enables the 'mkservice' builtin which creates a TCP or UDP
                     server that is implemented by shell functions, and the
                     'eloop' builtin which causes the shell to block waiting for
                     events to process. Experimental and probably insecure.

    NOECHOE      off Disable the '-e' option to the 'echo' command,
                     unless SHOPT_ECHOPRINT is enabled.

    OLDTERMIO    off Use either termios or termio at runtime.

    OPTIMIZE     on  Optimize loop invariants for with for and while loops.

    P_SUID       0   If set, all real UIDs greater than or equal to this value
                     will require the -p option to run the shell setuid/setgid.

    RAWONLY      on  Turn on if the vi line mode doesn't work right unless
                     you do a set -o viraw.

    REGRESS      off Enable the __regress__ built-in command and instrumented
                     intercepts for testing.

    REMOTE       off Set --rc (read profile scripts) even if ksh was invoked
                     with standard input on a socket, i.e. as a remote shell.

    SPAWN        on  Use posix_spawn(3) as combined fork/exec if job control
                     is not active. Improves speed.

    STATS        on  Add .sh.stats compound variable.

    SUID_EXEC    on  Execute /etc/suid_exec for setuid, setgid script.

    SYSRC            Source /etc/ksh.kshrc on initializing an interactive
                     shell. This is on by default if /etc/ksh.kshrc or
                     /etc/bash.bashrc exists at compile time.

    TEST_L           Add 'test -l' as an alias for 'test -L'. This is on by
                     default if the OS's external 'test' command supports it.

    TIMEOUT      off Set this to the number of seconds for timing out and
                     exiting the shell when you don't enter a command.  If
                     non-zero, TMOUT can not be set larger than this value.

    VSH          on  Compile with vi command line editing.  The original vi
                     line editor code was provided by Pat Sullivan at CB.

#### BUILDING KSH 93U+M ####

To build ksh (as well as libcmd and libast libraries on which ksh depends),
cd to the top directory and run:

	bin/package make

The compiled binaries are stored in the arch directory, in a subdirectory
that corresponds to your architecture. The command 'bin/package host type'
outputs the name of this subdirectory.

If you have trouble or want to tune the binaries, you may pass additional
compiler and linker flags. It is usually best to export these as environment
variables before running bin/package as they could change the name of the
build subdirectory of the arch directory, so exporting them is a convenient
way to keep them consistent between build and test commands. Note that this
system uses CCFLAGS instead of the usual CFLAGS. An example that makes
Solaris Studio cc produce a 64-bit binary:

	export CCFLAGS="-m64 -O" LDFLAGS="-m64"
	bin/package make

Alternatively you can append these to the command, and they will only be
used for that command. You can also specify an alternative shell in which to
run the build scripts this way. For example:

	bin/package make SHELL=/bin/bash CCFLAGS="-O2 -I/opt/local/include" \
		LDFLAGS="-L/opt/local/lib"

Note: Do not add compiler flags that cause the compiler to emit terminal
escape codes, such as -fdiagnostics-color=always; this will cause the build
to fail as the probing code greps compiler diagnostics.

If you are certain that you don't need support for UTF-8 and other multibyte
character locales and really want to save some memory and CPU cycles, add
'-DAST_NOMULTIBYTE' to CCFLAGS to compile out all multibyte character
handling in ksh and supporting libraries. Not recommended for most users.

To install, use: bin/package install DESTINATION_DIRECTORY [ COMMAND ... ]

Any command from the arch directory can be installed. If no COMMAND is
specified, ksh and shcomp are assumed.

The DESTINATION_DIRECTORY is created if it does not exist. Commands are
installed in its bin subdirectory and each command's manual page, if
available, is installed in share/man.

Destination directories with whitespace or shell pattern characters in their
pathnames are not yet supported.

For more information, run:

	bin/package help

Many other commands in this repo self-document via the --help, --man and
--html options; those that do have no separate manual page.

The build should also generate shcomp, a program that will precompile
a script.  ksh93 is able to recognize files in this format and process
them as scripts.  You can use shcomp to send out scripts when you
don't want to give away the original script source.

To be able to run setuid/setgid shell scripts, or scripts without read
permission, the SUID_EXEC compile option must be on, and ksh must be installed
in the /bin directory, the /usr/bin directory, the /usr/lbin directory,
or the /usr/local/bin directory and the name must end in sh. The program
suid_exec must be installed in the /etc directory, must be owned by root,
and must be an SUID program.  If you must install ksh in some other directory
and want to be able to run setuid/setgid and execute only scripts, then
you will have to change the source code file sh/suid_exec.c explicitly.
If you do not have ksh in one of these secure locations, /bin/sh will
be invoked with the -p options and will fail when you execute a setuid/setgid
and/or execute only script.  Note that ksh does not read the .profile
or $ENV file when the real and effective user/group IDs are not equal.

#### TESTING KSH ####

The tests subdirectory contains a number of regression tests for ksh.
To run all these tests with the shell you just built, run the command
	bin/shtests
For help and more options, type
	bin/shtests --man

#### OTHER DOCUMENTATION ####

The file PROMO.mm is an advertisement that extolls the virtues of ksh.
The file sh.1 contains the troff (man) description of this Shell.
The file nval.3 contains the troff (man) description of the name-value
pair library that is needed for writing built-ins that need to
access shell variables.

The file sh.memo contains a draft troff (mm) memo describing ksh.  The
file builtins.mm is a draft troff (mm) memo describing how to write
built-in commands that can be loaded at run time.

The file NEWS in the top-level directory contains bug fixes and other
changes made in the ksh 93u+m fork and supporting libraries.  The file
COMPATIBILITY contains a list of potential incompatibilities.

#### TESTED SYSTEMS ####

ksh 93u+m 1.0.0.beta-1 has been compiled and tested on the following.
An asterisk signifies minor regression test failures (one or two minor
things amiss), two asterisks signify moderate regression test failures
(some functionality does not work), and three asterisks signify serious
failures (crashes, and/or important functionality does not work).

*	AIX 7.1 on RISC (PowerPC)
*	DragonFly BSD 5.8 on x86_64
	FreeBSD 12.2 on x86_64
	FreeBSD 13.0 on x86_64
	FreeBSD 12.2 on arm64 (thanks to hyenias for donating access to a Pi)
	GNU/Linux: Alpine 3.12.3 (musl C library) on x86_64
	GNU/Linux: CentOS 8.2 on x86_64
	GNU/Linux: Debian 10.7 on x86_64
	GNU/Linux: Gentoo 2.7 on i386
	GNU/Linux: NixOS 19.09 on x86_64
	GNU/Linux: Slackware 14.2 on x86_64
	GNU/Linux: Ubuntu 16.04 on x86_64
	GNU/Linux: Ubuntu 18.04 on armv7l (32-bit)
	GNU/Linux: Ubuntu 20.04 on arm64
	GNU/Linux: Void Linux (musl C library) on x86_64
***	Haiku R1/beta3 on x86_64
***	HP-UX B.11.11 on pa-risc
*	illumos: OmniOS 2020-08-19 (gcc) on x86_64
	illumos: OmniOS r151040 (gcc) on x86_64
	macOS 10.13.6 (High Sierra) on x86_64
	macOS 10.14.6 (Mojave) on x86_64
*	macOS 12.0.1 (Monterey) on arm64
*	NetBSD 8.1 on x86_64
*	NetBSD 9.2 on x86_64
*	OpenBSD 6.8 on x86_64
	OpenBSD 7.0 on x86_64
*	QNX Neutrino 6.5.0 on i386
*	Solaris 11.4 (gcc) on x86_64
	Solaris 11.4 (Solaris Studio 12.5 cc) on x86_64
*	UnixWare 7.1.4 on x86
***	Windows 7 using Cygwin on x86
***	Windows 10 using Cygwin on x86_64
***	Windows 11 using Cygwin on x86_64
	Windows 11 using WSL 2 with Ubuntu 20.04 on x86_64

#### REPORTING BUGS ####

Please report any problems or suggestions by opening an issue at:
https://github.com/ksh93/ksh
Alternatively, email martijn@inlv.org (timely response *not* promised).

Good luck!!

The ksh 93u+m contributors
https://github.com/ksh93/ksh

Originally written by:
David Korn
dgk@research.att.com