From 7b301195040c4861dbd15c5f2514c4e9eeadbe61 Mon Sep 17 00:00:00 2001 From: Dennis Camera Date: Mon, 26 Oct 2020 18:52:43 +0100 Subject: [PATCH] [type/__uci] Externalise functions to separate file --- cdist/conf/type/__uci/files/functions.sh | 75 +++++++++++++++++++++ cdist/conf/type/__uci/gencode-local | 85 ++---------------------- cdist/conf/type/__uci/manifest | 4 +- 3 files changed, 85 insertions(+), 79 deletions(-) create mode 100644 cdist/conf/type/__uci/files/functions.sh diff --git a/cdist/conf/type/__uci/files/functions.sh b/cdist/conf/type/__uci/files/functions.sh new file mode 100644 index 00000000..4ddb82e0 --- /dev/null +++ b/cdist/conf/type/__uci/files/functions.sh @@ -0,0 +1,75 @@ +# -*- mode: sh; indent-tabs-mode: t -*- + +in_list() { + printf '%s\n' "$@" | { grep -qxF "$(read -r ndl; echo "${ndl}")"; } +} + +quote() { + for _arg + do + shift + if test -n "$(printf %s "${_arg}" | tr -d -c '\t\n \042-\047\050-\052\073-\077\133\\`|~' | tr -c '' '.')" + then + # needs quoting + set -- "$@" "$(printf "'%s'" "$(printf %s "${_arg}" | sed -e "s/'/'\\\\''/g")")" + else + set -- "$@" "${_arg}" + fi + done + unset _arg + + # NOTE: Use printf because POSIX echo interprets escape sequences + printf '%s' "$*" +} + +uci_cmd() { + # Usage: uci_cmd [UCI ARGUMENTS]... + # Will output a shell command to be executed locally to add the given + # command to $tmpfile. + printf "printf '%%s\\\\n' %s" "$(quote "$(quote "$@")")" + printf ' >>%s\n' "$(quote "${tmpfile}")" +} + +uci_validate_name() { + # like util.c uci_validate_name() + test -n "$*" && test -z "$(echo "$*" | tr -d '[:alnum:]_')" +} + +uci_validate_tuple() ( + tok=${1:?} + case $tok + in + (*.*.*) + # check option + option=${tok##*.} + uci_validate_name "${option}" || { + printf 'Invalid option: %s\n' "${option}" >&2 + return 1 + } + tok=${tok%.*} + ;; + (*.*) + # no option (section definition) + ;; + (*) + printf 'Invalid tuple: %s\n' "$1" >&2 + return 1 + ;; + esac + + case ${tok#*.} + in + (@*) section=$(expr "${tok#*.}" : '@\(.*\)\[-*[0-9]*\]$') ;; + (*) section=${tok#*.} ;; + esac + uci_validate_name "${section}" || { + printf 'Invalid section: %s\n' "${1#*.}" >&2 + return 1 + } + + config=${tok%%.*} + uci_validate_name "${config}" || { + printf 'Invalid config: %s\n' "${config}" >&2 + return 1 + } +) diff --git a/cdist/conf/type/__uci/gencode-local b/cdist/conf/type/__uci/gencode-local index 34482a01..b7664e85 100755 --- a/cdist/conf/type/__uci/gencode-local +++ b/cdist/conf/type/__uci/gencode-local @@ -18,84 +18,6 @@ # along with cdist. If not, see . # -in_list() { - printf '%s\n' "$@" | { grep -qxF "$(read -r ndl; echo "${ndl}")"; } -} - -quote() { - for _arg - do - shift - if test -n "$(printf %s "${_arg}" | tr -d -c '\t\n \042-\047\050-\052\073-\077\133\\`|~' | tr -c '' '.')" - then - # needs quoting - set -- "$@" "$(printf "'%s'" "$(printf %s "${_arg}" | sed -e "s/'/'\\\\''/g")")" - else - set -- "$@" "${_arg}" - fi - done - unset _arg - - # NOTE: Use printf because POSIX echo interprets escape sequences - printf '%s' "$*" -} - -uci_cmd() { - # Usage: uci_cmd [UCI ARGUMENTS]... - # Will output a shell command to be executed locally to add the given - # command to $tmpfile. - printf "printf '%%s\\\\n' %s" "$(quote "$(quote "$@")")" - printf ' >>%s\n' "$(quote "${tmpfile}")" -} - -uci_validate_name() { - # like util.c uci_validate_name() - test -n "$*" && test -z "$(echo "$*" | tr -d '[:alnum:]_')" -} - -uci_validate_tuple() ( - tok=${1:?} - case $tok - in - (*.*.*) - # check option - option=${tok##*.} - uci_validate_name "${option}" || { - printf 'Invalid option: %s\n' "${option}" >&2 - return 1 - } - tok=${tok%.*} - ;; - (*.*) - # no option (section definition) - ;; - (*) - printf 'Invalid tuple: %s\n' "$1" >&2 - return 1 - ;; - esac - - case ${tok#*.} - in - (@*) section=$(expr "${tok#*.}" : '@\(.*\)\[-*[0-9]*\]$') ;; - (*) section=${tok#*.} ;; - esac - uci_validate_name "${section}" || { - printf 'Invalid section: %s\n' "${1#*.}" >&2 - return 1 - } - - config=${tok%%.*} - uci_validate_name "${config}" || { - printf 'Invalid config: %s\n' "${config}" >&2 - return 1 - } -) - - -config=${__object_id:?} -uci_validate_tuple "${config}" - state_is=$(cat "${__object:?}/explorer/state") state_should=$(cat "${__object:?}/parameter/state") @@ -107,6 +29,13 @@ mkdir -p "${tmpdir}" tmpfile="${tmpdir}/${transaction_name}.txt" +# shellcheck source=files/functions.sh +. "${__type:?}/files/functions.sh" + + +config=${__object_id:?} +uci_validate_tuple "${config}" + case ${state_should} in diff --git a/cdist/conf/type/__uci/manifest b/cdist/conf/type/__uci/manifest index e56462e5..d0c8239e 100755 --- a/cdist/conf/type/__uci/manifest +++ b/cdist/conf/type/__uci/manifest @@ -18,7 +18,9 @@ # along with cdist. If not, see . # -in_list() { printf '%s\n' "$@" | { grep -qxF "$(read -r ndl; echo "${ndl}")"; } } +# shellcheck source=files/functions.sh +. "${__type:?}/files/functions.sh" + os=$(cat "${__global:?}/explorer/os")