Re: [PATCH] fix POSIX 'command -pv'

2016-03-04 Thread Martijn Dekker
Thorsten Glaser schreef op 26-02-16 om 21:57:
> In the end, I decided I don’t like the code and rewrote it entirely,
> separating options processing, simplifying, etc. but thanks anyway!

Yes, that code looks much cleaner. No more opaque mixing of two
different command's option logic.

I also missed something in that patch -- aliases still weren't handled
correctly. But your new code fixes that too.

I also submitted a patch to OpenBSD ksh for this and in the process I
wrote a couple of more robust regression tests, one to make sure
'command' behaves correctly and the other to check 'whence' behaviour
hasn't changed. Here they are as a patch against mksh, latest cvs. (They
pass.)

- M.

cvs diff: Diffing .
Index: check.t
===
RCS file: /cvs/src/bin/mksh/check.t,v
retrieving revision 1.726
diff -u -r1.726 check.t
--- check.t 1 Mar 2016 18:30:25 -   1.726
+++ check.t 5 Mar 2016 03:47:29 -
@@ -12196,13 +12196,75 @@
after   0='swc' 1='二' 2=''
= done
 ---
-name: command-path
+name: command-pvV-posix-priorities
 description:
-   Check 'command -p' is not 'whence -p'
-stdin:
-   command -pv if
+   For POSIX compatibility, command -v should find aliases and reserved
+   words, and command -p[vV] should find aliases, reserved words, and
+   builtins over external commands.
+stdin:
+   PATH=/bin:/usr/bin
+   alias foo="bar baz"
+   bar() { :; }
+   for word in 'if' 'foo' 'bar' 'set' 'true'; do
+   command -v "$word"
+   command -pv "$word"
+   command -V "$word"
+   command -pV "$word"
+   done
+expected-stdout:
+   if
+   if
+   if is a reserved word
+   if is a reserved word
+   alias foo='bar baz'
+   alias foo='bar baz'
+   foo is an alias for 'bar baz'
+   foo is an alias for 'bar baz'
+   bar
+   bar
+   bar is a function
+   bar is a function
+   set
+   set
+   set is a special shell builtin
+   set is a special shell builtin
+   true
+   true
+   true is a shell builtin
+   true is a shell builtin
+---
+name: whence-preserve-tradition
+description:
+   This regression test is to ensure that the POSIX compatibility
+   changes for 'command' (see previous test) do not affect traditional
+   'whence' behaviour.
+stdin:
+   PATH=/bin:/usr/bin
+   alias foo="bar baz"
+   bar() { :; }
+   for word in 'if' 'foo' 'bar' 'set' 'true'; do
+   whence "$word"
+   whence -p "$word"
+   whence -v "$word"
+   whence -pv "$word"
+   done
 expected-stdout:
if
+   if is a reserved word
+   if not found
+   'bar baz'
+   foo is an alias for 'bar baz'
+   foo not found
+   bar
+   bar is a function
+   bar not found
+   set
+   set is a special shell builtin
+   set not found
+   true
+   /usr/bin/true
+   true is a shell builtin
+   true is a tracked alias for /usr/bin/true
 ---
 name: duffs-device
 description:




Re: [PATCH] fix POSIX 'command -pv'

2016-02-26 Thread Thorsten Glaser
Martijn Dekker dixit:

>I found another minor POSIX bug in mksh 'command': the options -p and -v
>don't combine correctly.

Thanks. Sorry it took me some time to have a look at this,
but I like to… put off dealing with standards… ☺

In the end, I decided I don’t like the code and rewrote it entirely,
separating options processing, simplifying, etc. but thanks anyway!

bye,
//mirabilos
-- 
FWIW, I'm quite impressed with mksh interactively. I thought it was much
*much* more bare bones. But it turns out it beats the living hell out of
ksh93 in that respect. I'd even consider it for my daily use if I hadn't
wasted half my life on my zsh setup. :-) -- Frank Terbeck in #!/bin/mksh


[PATCH] fix POSIX 'command -pv'

2016-02-14 Thread Martijn Dekker
I found another minor POSIX bug in mksh 'command': the options -p and -v
don't combine correctly.

The -p flag means that 'command' will search the system default PATH
instead of the current PATH, but does not change that builtins take
precedence over external commands. Hence 'command -p test' will execute
the 'test' builtin and not /bin/test.

'-v' output should reflect what would actually be executed without that
flag. Without the -p flag, this works fine:
$ command -v true
true

However, command -pv always shows the external command, even if command
-p does not execute it:

$ command -pv [
/bin/[
(expected output: '[')

$ command -p [
mksh: [: missing ]
 it's the builtin, as expected

This also means that 'command -pv' should find shell keywords such as
'if' the same way 'command -v' should (as is the behaviour on ksh93,
bash, dash, etc.), so my earlier patch for 'command' of 1 July also
needs to be amended.

$ command -pv if
(nothing, expected output: if)

I believe the following patch fixes it (without affecting 'whence -p').

Thanks,

- M.

diff -ur mksh.orig/funcs.c mksh/funcs.c
--- mksh.orig/funcs.c   2016-01-20 22:34:37.0 +0100
+++ mksh/funcs.c2016-02-14 23:27:54.0 +0100
@@ -548,15 +548,14 @@
 * or whence -pv. This should be considered a feature.
 */
vflag = Vflag;
-   }
-   if (pflag)
+   } else if (pflag)
fcflags &= ~(FC_BI | FC_FUNC);

while ((vflag || rv == 0) && (id = *wp++) != NULL) {
uint32_t h = 0;

tp = NULL;
-   if (!pflag)
+   if (!iam_whence || !pflag)
tp = ktsearch(, id, h = hash(id));
if (!tp && !pflag) {
tp = ktsearch(, id, h ? h : hash(id));