diff options
author | Oliver Kiddle <opk@zsh.org> | 2016-09-16 00:00:28 +0200 |
---|---|---|
committer | Oliver Kiddle <opk@zsh.org> | 2016-09-16 00:00:28 +0200 |
commit | fbafc5b509e311efee064bbd12396a2e207f3393 (patch) | |
tree | 9fdf8f236fb1d25b54079e81cf75bf05eeca0b26 /Src/params.c | |
parent | 00708285e94ce42a31fd59bbcd06d010a6d0d6ee (diff) | |
download | zsh-fbafc5b509e311efee064bbd12396a2e207f3393.tar.gz zsh-fbafc5b509e311efee064bbd12396a2e207f3393.tar.xz zsh-fbafc5b509e311efee064bbd12396a2e207f3393.zip |
39332: support ksh's [[ -v varname ]] condition for checking if variables are set
Diffstat (limited to 'Src/params.c')
-rw-r--r-- | Src/params.c | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/Src/params.c b/Src/params.c index 842b2f0d1..384c30a1a 100644 --- a/Src/params.c +++ b/Src/params.c @@ -626,6 +626,36 @@ getvaluearr(Value v) return NULL; } +/* Return whether the variable is set * + * checks that array slices are within range * + * used for [[ -v ... ]] condition test */ + +/**/ +int +issetvar(char *name) +{ + struct value vbuf; + Value v; + int slice; + char **arr; + + if (!(v = getvalue(&vbuf, &name, 1)) || *name) + return 0; /* no value or more chars after the variable name */ + if (v->isarr & ~SCANPM_ARRONLY) + return v->end > 1; /* for extracted elements, end gives us a count */ + + slice = v->start != 0 || v->end != -1; + if (PM_TYPE(v->pm->node.flags) != PM_ARRAY || !slice) + return !slice && !(v->pm->node.flags & PM_UNSET); + + if (!v->end) /* empty array slice */ + return 0; + /* get the array and check end is within range */ + if (!(arr = getvaluearr(v))) + return 0; + return arrlen_ge(arr, v->end < 0 ? - v->end : v->end); +} + /* * Split environment string into (name, value) pair. * this is used to avoid in-place editing of environment table |