From b52676a64b05fc251f326a6b3c5043f175ca75c6 Mon Sep 17 00:00:00 2001 From: Krzesimir Nowak Date: Fri, 11 Apr 2025 14:49:21 +0200 Subject: [PATCH] pkg-auto: Add function for declaring structs Declaring structs differs a bit from declaring typical variables in that it takes one initializer and applies it to all the declared variables. Will be used a lot by upcoming libraries. --- pkg_auto/impl/util.sh | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/pkg_auto/impl/util.sh b/pkg_auto/impl/util.sh index 11afcdf8b5..b220060574 100644 --- a/pkg_auto/impl/util.sh +++ b/pkg_auto/impl/util.sh @@ -324,4 +324,28 @@ function gen_varname() { __UTIL_SH_COUNTER=$((__UTIL_SH_COUNTER + 1)) } +# Declares variables with a given initializer. +# +# Params: +# +# @: flags passed to declare, followed by variable names, followed by +# an initializer +function struct_declare() { + local -a args=() + while [[ $# -gt 0 ]]; do + if [[ ${1} != -* ]]; then + break + fi + args+=( "${1}" ) + shift + done + if [[ ${#} -lt 2 ]]; then + fail "bad use of struct_declare" + fi + local definition=${*: -1} + set -- "${@:1:$((${#} - 1))}" + set -- "${@/%/=${definition}}" + declare "${args[@]}" "${@}" +} + fi