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

Refactor new b_hash(); better hash table clear (re: d8428a83)

The b_hash() function duplicated much of its code from b_alias(),
while b_alias() retained some code to support being called as
'hash'. There is no reason why 'hash' and 'alias' can't be handled
with a single function, as is the case several other builtins. Note
that option parsing can easily be made dependent on the name the
command was invoked with (in this case, argv[0]=='h').

The new hash builtin's -r option cleared the hash table by
assigning to PATH its existing value, triggering its associated
discipline function (put_restricted() in init.c) which then
actually cleared the hash table. That's a bit of a hack. It's nicer
if we can just do that directly. This requires taking a static
handler function rehash() from init.c, which invalidates one hash
table entry, and making it available to the builtin.

src/cmd/ksh93/bltins/typeset.c,
src/cmd/ksh93/include/builtins.h,
src/cmd/ksh93/include/nval.h,
src/cmd/ksh93/sh/init.c,
src/cmd/ksh93/sh/name.c:
- Merge b_hash() into b_alias().
- The -x option was still uselessly setting the NV_EXPORT flag.
  Exported aliases were in ksh88 but were removed in ksh93.
- Rename rehash() handler function from init.c to nv_rehash
  (avoiding a possible conflict with another rehash() in cd_pwd.c)
  and move it to name.c just above nv_scan(), which it's meant to
  be used with. Make it an extern so typeset.c can use it.
- b_alias(): Replace the PATH assignment by an nv_scan() call to
  clear the hash table directly using the nv_rehash() handler.

src/cmd/ksh93/data/builtins.c:
- POSIX compliance fix: Remove BLT_SPC (special builtin) flag from
  "alias" definition. 'alias' is specified as a regular builtin.
- sh_optalias[]: Fix uninformative -t option documentation.
- sh_opthash[]: Edit for conciseness and clarity.

src/cmd/ksh93/sh.1:
- Edit the 'alias -t' and 'hash' documentation.
- Remove the -- prefix from the 'alias' entry, which indicated that
  it was supposed to be a declaration builtin like 'typeset', with
  assignment-arguments expanding tildes and not being subject to
  field splitting. However, my testing shows that 'alias' has never
  actually behaved that way on ksh93. Even adding the BLT_DCL flag
  in data/builtins.c doesn't seem to change that.

(cherry picked from commit afa68dca5c786daa13213973e8b0f9bf3a1dadf6)
This commit is contained in:
Martijn Dekker 2020-06-11 05:23:38 +02:00
parent 74b4162178
commit 80d9ae2b1c
8 changed files with 68 additions and 98 deletions

View file

@ -5555,7 +5555,7 @@ command and the original positional parameters are restored upon completion.
Otherwise the positional parameters are unchanged.
The exit status is the exit status of the last command executed.
.TP
\(dg\(dg \f3alias\fP \*(OK \f3\-ptx\fP \*(CK \*(OK \f2name\fP\*(OK \f3=\fP\f2value\^\fP \*(CK \*(CK .\|.\|.
\f3alias\fP \*(OK \f3\-ptx\fP \*(CK \*(OK \f2name\fP\*(OK \f3=\fP\f2value\^\fP \*(CK \*(CK .\|.\|.
.B alias\^
with no arguments prints the list of aliases
in the form
@ -5580,16 +5580,13 @@ A trailing space in
.I value\^
causes the next word to be checked for
alias substitution.
The obsolete
With the
.B \-t
option is used to set and list tracked aliases.
The value of a tracked alias is the full pathname
corresponding to the given
.IR name .
The value becomes undefined when the value of
.SM
.B PATH
is reset but the alias remains tracked.
option, each name is looked up as a command in
.B $PATH
and its path is added to the hash table as a 'tracked alias'.
If no name is given, this prints the hash table. See
.BR hash.
Without the
.B \-t
option,
@ -6125,19 +6122,20 @@ The option
can only be specified as the first option.
.TP
\f3hash\fP \*(OK \f3\-r\fP \f2\^\fP\*(CK \*(OK \f3utility\^\fP \*(CK
The
.BR hash
command can be used to display or modify the hash table.
The hash table is a list of the most recently used programs
and their locations.
If
.B hash
displays or modifies the hash table with the locations of recently used
programs. If given no arguments, it lists all command/path associations
(a.k.a. 'tracked aliases') in the hash table. Otherwise,
.B hash
performs a
.B PATH
search for each
.I utility\^
is supplied,
.BR hash
will add that utility to the hash table.
If
supplied and adds the result to the hash table.
The
.B \-r
is selected, the hash table is reset.
option empties the hash table. This can also be achieved by resetting
.BR PATH.
.TP
.PD 0
\f3hist\fP \*(OK \f3\-e\fP \f2ename\^\fP \ \*(CK \*(OK \f3\-nlr\^\fP \*(CK \*(OK \f2first\^\fP \*(OK \f2last\^\fP \*(CK \*(CK