From 983d22f925d46fcaa58c0e4c88d0240d9f161628 Mon Sep 17 00:00:00 2001 From: "Steffen (Daode) Nurpmeso" Date: Tue, 3 May 2016 15:47:58 +0200 Subject: [PATCH] `if'+: allow "if ${VARNAME}" in addition to "if $VARNAME".. on the left- and the right-hand-side. --- cc-test.sh | 26 ++++++++++++++++++++++++-- cmd_cnd.c | 19 ++++++++++++++++++- nail.1 | 21 +++++++++++++++------ 3 files changed, 57 insertions(+), 9 deletions(-) diff --git a/cc-test.sh b/cc-test.sh index 3b7aee58..2b582e10 100755 --- a/cc-test.sh +++ b/cc-test.sh @@ -628,7 +628,29 @@ __behave_ifelse() { else echo 59.err endif - # Unary ! + # Some more en-braced variables + set diet=yo curd=ho + if ${diet} == ${curd} + echo 70.err + else + echo 70.ok + endif + if ${diet} != ${curd} + echo 71.ok + else + echo 71.err + endif + if $diet == ${curd} + echo 72.err + else + echo 72.ok + endif + if ${diet} == $curd + echo 73.err + else + echo 73.ok + endif + # Unary ! if ! 0 && ! ! 1 && ! ! ! ! 2 && 3 echo 80.ok else @@ -721,7 +743,7 @@ __behave_ifelse() { echo 98.err endif __EOT - cksum_test behave:if-normal "${MBOX}" '3542193361 607' + cksum_test behave:if-normal "${MBOX}" '557629289 631' if have_feat REGEX; then ${rm} -f "${MBOX}" diff --git a/cmd_cnd.c b/cmd_cnd.c index d4aeb874..88d118ea 100644 --- a/cmd_cnd.c +++ b/cmd_cnd.c @@ -124,7 +124,14 @@ jesyn: break; case '$': /* Look up the value in question, we need it anyway */ - ++cp; + if(*++cp == '{'){ + size_t i = strlen(cp); + + if(i > 0 && cp[i - 1] == '}') + cp = savestrbuf(++cp, i -= 2); + else + goto jesyn; + } lhv = noop ? NULL : vok_vlook(cp); /* Single argument, "implicit boolean" form? */ @@ -163,6 +170,16 @@ jesyn: if (*rhv == '$') { if (*++rhv == '\0') goto jesyn; + else if(*rhv == '{'){ + size_t i = strlen(rhv); + + if(i > 0 && rhv[i - 1] == '}') + rhv = savestrbuf(++rhv, i -= 2); + else{ + cp = --rhv; + goto jesyn; + } + } rhv = noop ? NULL : vok_vlook(rhv); } diff --git a/nail.1 b/nail.1 index 594f03e1..fa65ab01 100644 --- a/nail.1 +++ b/nail.1 @@ -3334,11 +3334,20 @@ for textual boolean representations) to mark an enwrapped block as .Dq never execute or .Dq always execute . -It is possible to check a variable for existence or compare its -expansion against a user given value or another variable via the +It is possible to check +.Sx "INTERNAL VARIABLES" +as well as +.Sx ENVIRONMENT +variables for existence or compare their expansion against a user given +value or another variable by using the .Ql $ .Pf ( Dq variable next ) -conditional trigger character. +conditional trigger character; +a variable on the right hand side may be signalled using the same +mechanism. +The variable names may be enclosed in a pair of matching braces. +. +.Pp The available comparison operators are .Ql < (less than), @@ -3406,7 +3415,7 @@ if $ttycharset == "UTF-8" echo *ttycharset* is set to UTF-8, case-insensitively endif set t1=one t2=one -if $t1 == $t2 +if ${t1} == ${t2} echo These two variables are equal endif if $version-major >= 15 @@ -3416,10 +3425,10 @@ if $version-major >= 15 echo ..in an X terminal endif endif - if [ [ true ] && [ [ $debug ] || [ $verbose ] ] ] + if [ [ true ] && [ [ ${debug} ] || [ $verbose ] ] ] echo Noisy, noisy endif - if true && $debug || $verbose + if true && $debug || ${verbose} echo Left associativity, as is known from the shell endif if ! ! true && ! [ ! $debug && ! $verbose ] -- 2.11.4.GIT