From 7bd91daa6837ea049396ae60cd62af61cd5bb5ef Mon Sep 17 00:00:00 2001 From: Michael Tremer Date: Sat, 8 Sep 2012 09:35:06 +0000 Subject: [PATCH] config: Read all configuration variables into an array. --- functions.config | 42 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 41 insertions(+), 1 deletion(-) diff --git a/functions.config b/functions.config index f10bcf14..e2298056 100644 --- a/functions.config +++ b/functions.config @@ -57,7 +57,47 @@ function config_read() { ;; *) log DEBUG "Invalid line in configuration file: ${line}" - continue + ;; + esac + done < ${file} +} + +function config_read_array() { + local file=${1} + assert isset file + shift + + local array=${1} + assert isset array + shift + + local valid_keys=$@ + + # Exit if the file cannot be read. + [ -r "${file}" ] || return ${EXIT_ERROR} + + local line key val + while read -r line; do + case "${line}" in + *=*) + key=$(cli_get_key ${line}) + + # If valid_keys is set, key must be in the list. + if [ -n "${valid_keys}" ]; then + if ! listmatch ${key} ${valid_keys}; then + log DEBUG "Ignoring configuration setting: ${key}" + continue + fi + fi + + val=$(cli_get_val ${line}) + val=$(config_strip ${val}) + + # Assign variable. + printf -v "${array}["${key}"]" "%s" "${val}" + ;; + *) + log DEBUG "Invalid line in configuration file: ${line}" ;; esac done < ${file} -- 2.47.3