Allow loops to skip entries if certain variable is kept

Signed-off-by: Alexey Neyman <stilor@att.net>
This commit is contained in:
Alexey Neyman 2017-08-24 15:29:55 -07:00
parent 4c1a12f5dd
commit 961ea19382
2 changed files with 41 additions and 11 deletions

View File

@ -91,7 +91,7 @@ set_iter()
run_if()
{
local cond="${1}"
local cond="$*"
local endline
find_end "if"
@ -125,8 +125,9 @@ do_foreach()
for k in "${!info[@]}"; do
saveinfo["${k}"]=${info["${k}"]}
done
eval "enter_${var} ${v}"
eval "$@"
if eval "enter_${var} ${v}"; then
eval "$@"
fi
info=()
for k in "${!saveinfo[@]}"; do
info["${k}"]=${saveinfo["${k}"]}
@ -136,19 +137,48 @@ do_foreach()
run_foreach()
{
local var="${1}"
local endline
local var="${1}"
shift
if [ "${info[iter_${var}]+set}" != "set" ]; then
error "line ${l}: iterator over '${var}' is not defined"
fi
find_end "foreach"
debug "Loop over '${var}', lines ${l}..${endline}"
do_foreach ${var} run_lines $[l + 1] $[endline - 1]
do_foreach ${var} run_lines_if $[l + 1] $[endline - 1] "$*"
lnext=$[endline + 1]
debug "Continue at line ${lnext}"
}
run_lines_if()
{
local start="${1}"
local end="${2}"
shift 2
local cond="$*"
local a prev
for a in ${cond}; do
if [ -n "${prev}" ]; then
case "${prev}" in
if-differs)
if [ "${info[${a}]}" = "${saveinfo[${a}]}" ]; then
return
fi
;;
*)
error "line ${l}: unknown condition '${prev}' for loop"
;;
esac
prev=
else
prev=${a}
fi
done
run_lines "${start}" "${end}"
}
run_lines()
{
local start="${1}"
@ -215,10 +245,10 @@ run_lines()
debug "Evaluate: ${s}"
case "${s}" in
"#!if "*)
run_if "${s#* }"
run_if ${s#* }
;;
"#!foreach "*)
run_foreach "${s#* }"
run_foreach ${s#* }
;;
"#!//"*)
# Comment, do nothing

View File

@ -248,28 +248,28 @@ config @@fork|@@_VERSION
#!if [ "@@#version@@" -gt 0 ]
config @@fork|@@_MIRRORS
string
#!foreach version
#!foreach version if-differs mirrors
default "@@mirrors@@" if @@fork|@@_V_@@ver_sel|@@
#!end-foreach
default "@@mirrors@@"
config @@fork|@@_ARCHIVE_FILENAME
string
#!foreach version
#!foreach version if-differs archive_filename
default "@@archive_filename@@" if @@fork|@@_V_@@ver_sel|@@
#!end-foreach
default "@@archive_filename@@"
config @@fork|@@_ARCHIVE_DIRNAME
string
#!foreach version
#!foreach version if-differs archive_dirname
default "@@archive_dirname@@" if @@fork|@@_V_@@ver_sel|@@
#!end-foreach
default "@@archive_dirname@@"
config @@fork|@@_ARCHIVE_FORMATS
string
#!foreach version
#!foreach version if-differs archive_formats
default "@@archive_formats@@" if @@fork|@@_V_@@ver_sel|@@
#!end-foreach
default "@@archive_formats@@"