2 # -*- mode: sh; sh-basic-offset: 3; indent-tabs-mode: nil; -*-
3 # vim: set filetype=sh sw=3 sts=3 expandtab autoindent:
5 # This file contains functions shared between ninjahelper and backupninja.
7 #####################################################
11 # create a temporary file in a secure way.
14 local tempfile=`mktemp /tmp/$1.XXXXXXXX`
18 #####################################################
19 ## CONFIG-FILE RELATED FUNCTIONS
25 function setsection() {
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
33 function printconf() {
34 # Be careful! This function might be called with an empty IFS
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
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.
53 # Be careful! This function might be called with an empty IFS
55 local ret=`printconf "$1" "$2"`
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.
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"
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 "$@"
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"
90 # variable=one\ one two
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
97 # Get the variable name, so $@ only contains the default elements
100 # Get the value from the config
101 local value=`printconf $varname ""`
102 # Init the result variable to an empty array
104 if [ -z "$value" ]; then
105 # Use the default value, i.e., copy $@ into the result
106 append "$varname" "$@"
109 # Read every line and append it to our result
110 while read -a tmp; do
111 append "$varname" "${tmp[@]}"