1
0
Fork 0
mirror of git://git.code.sf.net/p/cdesktopenv/code synced 2025-03-09 15:50:02 +00:00

Allow proper tilde expansion overrides (#225)

Until now, when performing any tilde expansion like ~/foo or
~user/foo, ksh added a placeholder built-in command called
'.sh.tilde', ostensibly with the intention to allow users to
override it with a shell function or custom builtin. The multishell
ksh93 repo <https://github.com/multishell/ksh93/> shows this was
added sometime between 2002-06-28 and 2004-02-29. However, it has
never worked and crashed the shell.

This commit replaces that with something that works. Specific tilde
expansions can now be overridden using .set or .get discipline
functions associated with the .sh.tilde variable (see manual,
Discipline Functions).

For example, you can use either of:

.sh.tilde.set()
{
        case ${.sh.value} in
        '~tmp') .sh.value=${XDG_RUNTIME_DIR:-${TMPDIR:-/tmp}} ;;
        '~doc') .sh.value=~/Documents ;;
        '~ksh') .sh.value=/usr/local/src/ksh93/ksh ;;
        esac
}

.sh.tilde.get()
{
        case ${.sh.tilde} in
        '~tmp') .sh.value=${XDG_RUNTIME_DIR:-${TMPDIR:-/tmp}} ;;
        '~doc') .sh.value=~/Documents ;;
        '~ksh') .sh.value=/usr/local/src/ksh93/ksh ;;
        esac
}

src/cmd/ksh93/include/variables.h,
src/cmd/ksh93/data/variables.c:
- Add SH_TILDENOD for a new ${.sh.tilde} predefined variable.
  It is initially unset.

src/cmd/ksh93/sh/macro.c:
- sh_btilde(): Removed.
- tilde_expand2(): Rewritten. I started out with the tiny version
  of this function from the 2002-06-28 version of ksh. It uses the
  stack instead of sfio, which is more efficient. A bugfix for
  $HOME == '/' was retrofitted so that ~/foo does not become
  //foo instead of /foo. The rest is entirely new code.
     To implement the override functionality, it now checks if
  ${.sh.tilde} has any discipline function associated with it.
  If it does, it assigns the tilde expression to ${.sh.tilde} using
  nv_putval(), triggering the .set discipline, and then reads it
  back using nv_getval(), triggering the .get discipline. The
  resulting value is used if it is nonempty and does not still
  start with a tilde.

src/cmd/ksh93/bltins/typeset.c,
src/cmd/ksh93/tests/builtins.sh:
- Since ksh no longer adds a dummy '.sh.tilde' builtin, remove the
  ad-hoc hack that suppressed it from the output of 'builtin'.

src/cmd/ksh93/tests/tilde.sh:
- Add tests verifying everything I can think of, as well as tests
  for bugs found and fixed during this rewrite.

src/cmd/ksh93/tests/pty.sh:
- Add test verifying that the .sh.tilde.set() discipline does not
  modify the exit status value ($?) when performing tilde expansion
  as part of tab completion.

src/cmd/ksh93/sh.1:
- Instead of "tilde substitution", call the basic mechanism "tilde
  expansion", which is the term used everywhere else (including the
  1995 Bolsky/Korn ksh book).
- Document the new override feature.

Resolves: https://github.com/ksh93/ksh/issues/217
This commit is contained in:
Martijn Dekker 2021-03-17 21:07:14 +00:00 committed by GitHub
parent 595a0a5684
commit 936a1939a8
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 190 additions and 76 deletions

View file

@ -782,11 +782,11 @@ Preset aliases can be unset or redefined.
.B "r=\(fmhist \-s\(fm"
.PD
.RE
.SS Tilde Substitution.
.SS Tilde Expansion.
After alias substitution is performed, each word
is checked to see if it begins with an unquoted
.BR \(ap .
For tilde substitution,
For tilde expansion,
.I word\^
also refers to the
.I word\^
@ -794,10 +794,12 @@ portion of parameter expansion
(see
.I "Parameter Expansion\^"
below).
If it does, then the word up to a
If a
.I word\^
is preceded by a tilde, then it is checked up to a
.B /
is checked to see if it matches a user name in the
password database (See
to see if it matches a user name in the
password database (see
.IR getpwname (3).)
If a match is found, the
.B \(ap
@ -810,26 +812,32 @@ by itself, or in front of a
.BR / ,
is replaced by
.SM
.BR $HOME .
.BR $HOME ,
unless the
.B HOME
variable is unset, in which case
the current user's home directory as configured in the operating system
is used.
A
.B \(ap
followed by a
.B +
or
.B \-
is replaced by the value of
is replaced by
.B
.SM $PWD
and
or
.B
.SM $OLDPWD
respectively.
.PP
In addition,
when expanding a
.IR "variable assignment" ,
.I tilde
substitution is attempted when
variable assignment (see
.I Variable Assignments
above),
tilde expansion is attempted when
the value of the assignment
begins with a
.BR \(ap ,
@ -837,11 +845,41 @@ and when a
.B \(ap
appears after a
.BR : .
The
A
.B :
also terminates a
.B \(ap
login name.
also terminates a user name following a
.BR \(ap .
.PP
The tilde expansion mechanism may be extended or modified
by defining one of the discipline functions
.B .sh.tilde.set
or
.B .sh.tilde.get
(see
.I Functions
and
.I Discipline Functions
below).
If either exists,
then upon encountering a tilde word to expand,
that function is called with the tilde word assigned to either
.B .sh.value
(for the
.B .sh.tilde.set
function) or
.B .sh.tilde
(for the
.B .sh.tilde.get
function).
Performing tilde expansion within a discipline function will not recursively
call that function, but default tilde expansion remains active,
so literal tildes should still be quoted where required.
Either function may assign a replacement string to
.BR .sh.value .
If this value is non-empty and does not start with a
.BR \(ap ,
it replaces the default tilde expansion when the function terminates.
Otherwise, the tilde expansion is left unchanged.
.SS Command Substitution.
The standard output from a command list enclosed in
parentheses preceded by a dollar sign (
@ -5549,7 +5587,7 @@ Commands that are preceded by a \(dd symbol below are
Any following words
that are in the format of a variable assignment
are expanded with the same rules as a variable assignment.
This means that tilde substitution is performed after the
This means that tilde expansion is performed after the
.B =
sign, array assignments of the form
\f2varname\^\fP\f3=(\fP\f2assign_list\^\fP\f3)\fP