[pacman-dev] [PATCH v2] scripts/library: add config_parser.sh

lolilolicon lolilolicon at gmail.com
Fri Sep 30 15:01:36 EDT 2011


config_parser.sh provides functions to support parsing simple config
files, pacman.conf for example.  Our scripts should never use a dirty
eval to get config values such as DBPath.

conf_key_get_val() is modified from pacman-key's get_from() function.
All the parser functions implemented read config file from stdin, which
makes chaining a breeze.

Signed-off-by: lolilolicon <lolilolicon at gmail.com>
---
 scripts/library/README           |    3 +
 scripts/library/config_parser.sh |  106 ++++++++++++++++++++++++++++++++++++++
 2 files changed, 109 insertions(+), 0 deletions(-)
 create mode 100644 scripts/library/config_parser.sh

diff --git a/scripts/library/README b/scripts/library/README
index 1e9c962..4fb70dd 100644
--- a/scripts/library/README
+++ b/scripts/library/README
@@ -1,6 +1,9 @@
 This folder contains code snippets that can be reused by multiple
 scripts.  A brief description of each file follows.
 
+config_parser.sh:
+Parses simple config files such as pacman.conf.
+
 output_format.sh:
 Provides basic output formatting functions with levels 'msg', 'msg2',
 'warning' and 'error'.  The 'msg' amd 'msg2' functions print to stdout
diff --git a/scripts/library/config_parser.sh b/scripts/library/config_parser.sh
new file mode 100644
index 0000000..73e40d3
--- /dev/null
+++ b/scripts/library/config_parser.sh
@@ -0,0 +1,106 @@
+# Parse value of a single-value key from a config file.
+#
+# stdin: config file
+# $1: key - shall not contain '=', or start/end with whitespaces
+# $2: default value if not defined in config file [optional]
+# return:
+#   0 - print value of key
+#   1 - print nothing
+conf_key_get_val() {
+	local key val
+	while IFS='=' read -r key val; do
+		# strip whitespaces
+		IFS=$' \t' read -r key <<< "$key"
+		IFS=$' \t' read -r val <<< "$val"
+		# ignore comments and invalid lines
+		[[ ${key:0:1} == '#' || -z $key || -z $val ]] && continue
+		# return success as soon as a value is found
+		if [[ $key == "$1" ]]; then
+			printf '%s' "$val"
+			return 0
+		fi
+	done
+	# fallback to default value if specified
+	if [[ -n $2 ]]; then
+		printf '%s' "$2"
+		return 0
+	fi
+	return 1
+}
+
+# Parse values of a repeatable, multi-value key from a config file.
+#
+# stdin: config file
+# $1: key - shall not contain '=', or start/end with whitespaces
+# $2: default values if not defined in config file [optional]
+# return:
+#   0 - print values of key, one per line
+#   1 - print nothing
+conf_key_get_vals() {
+  local key val vals=()
+	while IFS='=' read -r key val; do
+		# strip whitespaces
+		IFS=$' \t' read -r key <<< "$key"
+		IFS=$' \t' read -r val <<< "$val"
+		# ignore comments and invalid lines
+		[[ ${key:0:1} == '#' || -z $key || -z $val ]] && continue
+		# keep collecting values
+		if [[ $key == "$1" ]]; then
+			vals+=($val)
+		fi
+	done
+	# fallback to default values if specified
+	(( ${#vals[@]} )) || vals=($2)
+	(( ${#vals[@]} )) || return 1
+	printf '%s\n' "${vals[@]}"
+	return 0
+}
+
+# Check whether an on/off key is set in a config file.
+#
+# stdin: config file
+# $1: key - shall not contain '=', or start/end with whitespaces
+# return:
+#   0 - key is set
+#   1 - key is not set
+conf_key_isset() {
+	local key
+	while read -r key; do
+		# strip whitespaces
+		IFS=$' \t' read -r key <<< "$key"
+		# ignore comments and invalid lines
+		[[ ${key:0:1} == '#' || -z $key ]] && continue
+		# return success as soon as the key is found
+		if [[ $key == "$1" ]]; then
+			return 0
+		fi
+	done
+	return 1
+}
+
+# Print the first section with the specified header from a config file.
+#
+# stdin: config file
+# $1: header string - exact header string of the section
+# $2: header pattern - regex matching only header strings
+# return:
+#   0 - print lines in section
+#   1 - print nothing
+conf_get_section() {
+	local line
+	while read -r line; do
+		# strip whitespaces
+		IFS=$' \t' read -r line <<< "$line"
+		# ignore comments and invalid lines
+		[[ ${line:0:1} == '#' || -z $line ]] && continue
+		# print section till EOF or next section
+		if [[ $line == "$1" ]]; then
+			while printf '%s\n' "$line"; do
+				read -r line || break
+				[[ $line =~ $2 ]] && break
+			done
+			return 0
+		fi
+	done
+	return 1
+}
-- 
1.7.6.4



More information about the pacman-dev mailing list