add test -v, initially from Martijn Dekker

This commit is contained in:
tg 2017-04-12 17:38:46 +00:00
parent 1513145b0e
commit 833cb27f5c
4 changed files with 104 additions and 42 deletions

62
check.t
View File

@ -1,4 +1,4 @@
# $MirOS: src/bin/mksh/check.t,v 1.774 2017/04/11 12:34:02 tg Exp $
# $MirOS: src/bin/mksh/check.t,v 1.775 2017/04/12 17:38:41 tg Exp $
# -*- mode: sh -*-
#-
# Copyright © 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010,
@ -30,7 +30,7 @@
# (2013/12/02 20:39:44) http://cvsweb.openbsd.org/cgi-bin/cvsweb/src/regress/bin/ksh/?sortby=date
expected-stdout:
@(#)MIRBSD KSH R54 2017/04/07
@(#)MIRBSD KSH R55 2017/04/12
description:
Check version of shell.
stdin:
@ -39,7 +39,7 @@ name: KSH_VERSION
category: !shell:legacy-yes,!shell:textmode-yes
---
expected-stdout:
@(#)LEGACY KSH R54 2017/04/07
@(#)LEGACY KSH R55 2017/04/12
description:
Check version of legacy shell.
stdin:
@ -48,7 +48,7 @@ name: KSH_VERSION-legacy
category: !shell:legacy-no,!shell:textmode-yes
---
expected-stdout:
@(#)MIRBSD KSH R54 2017/04/07 +TEXTMODE
@(#)MIRBSD KSH R55 2017/04/12 +TEXTMODE
description:
Check version of shell.
stdin:
@ -57,7 +57,7 @@ name: KSH_VERSION-textmode
category: !shell:legacy-yes,!shell:textmode-no
---
expected-stdout:
@(#)LEGACY KSH R54 2017/04/07 +TEXTMODE
@(#)LEGACY KSH R55 2017/04/12 +TEXTMODE
description:
Check version of legacy shell.
stdin:
@ -7748,6 +7748,58 @@ expected-stdout:
2- 1 1 1 =
3- 0 0 0 =
---
name: test-varset-1
description:
Test the test -v operator
stdin:
[[ -v a ]]
rv=$?; echo $((++i)) $rv
a=
[[ -v a ]]
rv=$?; echo $((++i)) $rv
unset a
[[ -v a ]]
rv=$?; echo $((++i)) $rv
a=x
[[ -v a ]]
rv=$?; echo $((++i)) $rv
nameref b=a
[[ -v b ]]
rv=$?; echo $((++i)) $rv
unset a
[[ -v b ]]
rv=$?; echo $((++i)) $rv
x[1]=y
[[ -v x ]]
rv=$?; echo $((++i)) $rv
[[ -v x[0] ]]
rv=$?; echo $((++i)) $rv
[[ -v x[1] ]]
rv=$?; echo $((++i)) $rv
[[ -v x[2] ]]
rv=$?; echo $((++i)) $rv
expected-stdout:
1 1
2 0
3 1
4 0
5 0
6 1
7 1
8 1
9 0
10 1
---
name: test-varset-2
description:
test -v works only on scalars
stdin:
[[ -v x[*] ]]
echo ok
expected-exit: e != 0
expected-stderr-pattern:
/unexpected '\*'/
---
name: test-stnze-1
description:
Check that the short form [ $x ] works

12
funcs.c
View File

@ -38,7 +38,7 @@
#endif
#endif
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.338 2017/04/08 01:07:15 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/funcs.c,v 1.339 2017/04/12 17:38:44 tg Exp $");
#if HAVE_KILLPG
/*
@ -189,8 +189,8 @@ static const struct t_op {
{"-f", TO_FILREG },
{"-G", TO_FILGID },
{"-g", TO_FILSETG },
{"-h", TO_FILSYM },
{"-H", TO_FILCDF },
{"-h", TO_FILSYM },
{"-k", TO_FILSTCK },
{"-L", TO_FILSYM },
{"-n", TO_STNZE },
@ -198,10 +198,11 @@ static const struct t_op {
{"-o", TO_OPTION },
{"-p", TO_FILFIFO },
{"-r", TO_FILRD },
{"-s", TO_FILGZ },
{"-S", TO_FILSOCK },
{"-s", TO_FILGZ },
{"-t", TO_FILTT },
{"-u", TO_FILSETU },
{"-v", TO_ISSET },
{"-w", TO_FILWR },
{"-x", TO_FILEX },
{"-z", TO_STZER },
@ -2753,6 +2754,7 @@ test_eval(Test_env *te, Test_op op, const char *opnd1, const char *opnd2,
size_t k;
struct stat b1, b2;
mksh_ari_t v1, v2;
struct tbl *vp;
if (!do_eval)
return (0);
@ -2799,6 +2801,10 @@ test_eval(Test_env *te, Test_op op, const char *opnd1, const char *opnd2,
case TO_STZER:
return (*opnd1 == '\0');
/* -v */
case TO_ISSET:
return ((vp = isglobal(opnd1, false)) && (vp->flag & ISSET));
/* -o */
case TO_OPTION:
if ((i = *opnd1) == '!' || i == '?')

64
mksh.1
View File

@ -1,4 +1,4 @@
.\" $MirOS: src/bin/mksh/mksh.1,v 1.440 2017/04/11 17:40:41 tg Exp $
.\" $MirOS: src/bin/mksh/mksh.1,v 1.441 2017/04/12 17:38:45 tg Exp $
.\" $OpenBSD: ksh.1,v 1.160 2015/07/04 13:27:04 feinerer Exp $
.\"-
.\" Copyright © 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009,
@ -76,7 +76,7 @@
.\" with -mandoc, it might implement .Mx itself, but we want to
.\" use our own definition. And .Dd must come *first*, always.
.\"
.Dd $Mdocdate: April 11 2017 $
.Dd $Mdocdate: April 12 2017 $
.\"
.\" Check which macro package we use, and do other -mdoc setup.
.\"
@ -4472,34 +4472,6 @@ is a symbolic link.
.It Fl O Ar file
.Ar file Ns 's
owner is the shell's effective user ID.
.It Fl o Ar option
Shell
.Ar option
is set (see the
.Ic set
command above for a list of options).
As a non-standard extension, if the option starts with a
.Ql \&! ,
the test is negated; the test always fails if
.Ar option
doesn't exist (so [ \-o foo \-o \-o !foo ] returns true if and only if option
.Ar foo
exists).
The same can be achieved with [ \-o ?foo ] like in
.At
.Nm ksh93 .
.Ar option
can also be the short flag led by either
.Ql \-
or
.Ql +
.Pq no logical negation ,
for example
.Dq Li \-x
or
.Dq Li +x
instead of
.Dq Li xtrace .
.It Fl p Ar file
.Ar file
is a named pipe
@ -4561,6 +4533,38 @@ is not empty.
.It Fl z Ar string
.Ar string
is empty.
.It Fl v Ar name
The shell parameter
.Ar name
is set.
.It Fl o Ar option
Shell
.Ar option
is set (see the
.Ic set
command above for a list of options).
As a non-standard extension, if the option starts with a
.Ql \&! ,
the test is negated; the test always fails if
.Ar option
doesn't exist (so [ \-o foo \-o \-o !foo ] returns true if and only if option
.Ar foo
exists).
The same can be achieved with [ \-o ?foo ] like in
.At
.Nm ksh93 .
.Ar option
can also be the short flag led by either
.Ql \-
or
.Ql +
.Pq no logical negation ,
for example
.Dq Li \-x
or
.Dq Li +x
instead of
.Dq Li xtrace .
.It Ar string No = Ar string
Strings are equal.
.It Ar string No == Ar string

8
sh.h
View File

@ -175,9 +175,9 @@
#endif
#ifdef EXTERN
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.807 2017/04/08 01:07:18 tg Exp $");
__RCSID("$MirOS: src/bin/mksh/sh.h,v 1.808 2017/04/12 17:38:46 tg Exp $");
#endif
#define MKSH_VERSION "R54 2017/04/07"
#define MKSH_VERSION "R55 2017/04/12"
/* arithmetic types: C implementation */
#if !HAVE_CAN_INTTYPES
@ -593,7 +593,7 @@ char *ucstrstr(char *, const char *);
#define mkssert(e) do { } while (/* CONSTCOND */ 0)
#endif
#if (!defined(MKSH_BUILDMAKEFILE4BSD) && !defined(MKSH_BUILDSH)) || (MKSH_BUILD_R != 549)
#if (!defined(MKSH_BUILDMAKEFILE4BSD) && !defined(MKSH_BUILDSH)) || (MKSH_BUILD_R != 551)
#error Must run Build.sh to compile this.
extern void thiswillneverbedefinedIhope(void);
int
@ -2404,7 +2404,7 @@ enum Test_op {
/* non-operator */
TO_NONOP = 0,
/* unary operators */
TO_STNZE, TO_STZER, TO_OPTION,
TO_STNZE, TO_STZER, TO_ISSET, TO_OPTION,
TO_FILAXST,
TO_FILEXST,
TO_FILREG, TO_FILBDEV, TO_FILCDEV, TO_FILSYM, TO_FILFIFO, TO_FILSOCK,