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() run_if()
{ {
local cond="${1}" local cond="$*"
local endline local endline
find_end "if" find_end "if"
@ -125,8 +125,9 @@ do_foreach()
for k in "${!info[@]}"; do for k in "${!info[@]}"; do
saveinfo["${k}"]=${info["${k}"]} saveinfo["${k}"]=${info["${k}"]}
done done
eval "enter_${var} ${v}" if eval "enter_${var} ${v}"; then
eval "$@" eval "$@"
fi
info=() info=()
for k in "${!saveinfo[@]}"; do for k in "${!saveinfo[@]}"; do
info["${k}"]=${saveinfo["${k}"]} info["${k}"]=${saveinfo["${k}"]}
@ -136,19 +137,48 @@ do_foreach()
run_foreach() run_foreach()
{ {
local var="${1}"
local endline local endline
local var="${1}"
shift
if [ "${info[iter_${var}]+set}" != "set" ]; then if [ "${info[iter_${var}]+set}" != "set" ]; then
error "line ${l}: iterator over '${var}' is not defined" error "line ${l}: iterator over '${var}' is not defined"
fi fi
find_end "foreach" find_end "foreach"
debug "Loop over '${var}', lines ${l}..${endline}" 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] lnext=$[endline + 1]
debug "Continue at line ${lnext}" 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() run_lines()
{ {
local start="${1}" local start="${1}"
@ -215,10 +245,10 @@ run_lines()
debug "Evaluate: ${s}" debug "Evaluate: ${s}"
case "${s}" in case "${s}" in
"#!if "*) "#!if "*)
run_if "${s#* }" run_if ${s#* }
;; ;;
"#!foreach "*) "#!foreach "*)
run_foreach "${s#* }" run_foreach ${s#* }
;; ;;
"#!//"*) "#!//"*)
# Comment, do nothing # Comment, do nothing

View File

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