Add getconf_lines and getconf_words functions.
[matthijs/upstream/backupninja.git] / lib / tools.in
1 #!@BASH@
2 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
3 # vim: set filetype=sh sw=3 sts=3 expandtab autoindent:
4
5 # This file contains functions shared between ninjahelper and backupninja.
6
7 #####################################################
8 ## MISC FUNCTIONS
9
10 #
11 # create a temporary file in a secure way.
12 #
13 function maketemp() {
14    local tempfile=`mktemp /tmp/$1.XXXXXXXX`
15    echo $tempfile
16 }
17
18 #####################################################
19 ## CONFIG-FILE RELATED FUNCTIONS
20
21 function setfile() {
22    CURRENT_CONF_FILE=$1
23 }
24
25 function setsection() {
26    CURRENT_SECTION=$1
27 }
28
29 #
30 # Retrieves the configuration variable named $1 from the current config
31 # file and section and echoes its value. If it is empty or not found, $2
32 # is used.
33 function printconf() {
34    # Be careful! This function might be called with an empty IFS
35
36    local CURRENT_PARAM=$1
37    local ret=`@AWK@ -f $libdirectory/parseini S=$CURRENT_SECTION P=$CURRENT_PARAM $CURRENT_CONF_FILE`
38    # if nothing is returned, set the default
39    if [ -z "$ret" -a -n "$2" ]; then
40       ret="$2"
41    fi
42
43    echo "$ret"
44 }
45
46
47 #
48 # Retrieves the configuration variable named $1 from the current config
49 # file and section and assigns its value to the global variable with the
50 # same name. If it is empty or not found, $2 is used.
51 #
52 function getconf() {
53    # Be careful! This function might be called with an empty IFS
54
55    local ret=`printconf "$1" "$2"`
56
57    # We use escape the $ in $ret to delay expansion of $ret, so when $1
58    # is foo, eval sees foo=$ret and properly does the assignment
59    # (without the backslash, the right part of the assignment would be
60    # whatever is in ret and be subject to all kinds of expansion.
61    eval $1=\$ret
62 }
63
64 # Reads the variable denoted by $1 from the current configuration file
65 # and section into the array variable pointed to by $1. Each line in the
66 # configuration file is returned as a separate element in the resulting
67 # array. For example, the following ini fragment results in an array
68 # with three values: "one", "two" and "three"
69 #   [section]
70 #   variable=one\
71 #   two
72 #   variable=three
73 # $2, $3, etc. are used as the default value (e.g., each of the
74 # arguments are elements in the resulting array)
75 function getconf_lines() {
76    # Run getconf_words with an empty IFS, so the read in there does not
77    # do any word splitting. This shouldn't affect any other splitting
78    # (since IFS is only used to split expanded parameters, not the
79    # spaces that are in this file directly).
80    IFS='' getconf_words "$@"
81 }
82
83 # Reads the variable denoted by $1 from the current configuration file
84 # and section into the array variable pointed to by $1. Each word in
85 # each line in the configuration file is returned as a separate element
86 # in the resulting array. Whitespace can be escaped using backslashes. For
87 # example, the following ini fragment results in an array with three
88 # values: "one one", "two" and "three"
89 #   [section]
90 #   variable=one\ one two
91 #   variable=three
92 # $2 is used as the default value (which should be just a string, which
93 # will be split on newlines and whitespace)
94 function getconf_words() {
95    # Be careful! This function might be called with an empty IFS
96
97    # Get the variable name, so $@ only contains the default elements
98    local varname="$1"
99    shift
100    # Get the value from the config
101    local value=`printconf $varname ""`
102    # Init the result variable to an empty array
103    eval "$varname=()"
104    if [ -z "$value" ]; then
105       # Use the default value, i.e., copy $@ into the result
106       append "$varname" "$@"
107    else
108       local tmp
109       # Read every line and append it to our result
110       while read -a tmp; do
111          append "$varname" "${tmp[@]}"
112       done <<< "$value"
113    fi
114 }