aboutsummaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
Diffstat (limited to 'lib')
-rw-r--r--lib/Makefile.am19
-rw-r--r--lib/Makefile.in16
-rw-r--r--lib/parseini.in130
-rw-r--r--lib/tools.in35
4 files changed, 188 insertions, 12 deletions
diff --git a/lib/Makefile.am b/lib/Makefile.am
index 2fcce96..08c26c7 100644
--- a/lib/Makefile.am
+++ b/lib/Makefile.am
@@ -1,15 +1,22 @@
-EXTRALIBS = easydialog tools
-CLEANFILES = $(EXTRALIBS)
-dist_pkglib_DATA = $(EXTRALIBS)
-EXTRA_DIST = easydialog.in tools.in
+EXTRA_DIST = easydialog.in parseini.in tools.in
+
+GENERATED_FILES = easydialog parseini tools
+
+dist_pkglib_DATA = $(GENERATED_FILES)
+
+CLEANFILES = $(GENERATED_FILES)
edit = sed \
-e "s,@BASH\@,$(BASH),g"
-easydialog: easydialog.in
+easydialog: #easydialog.in
rm -f easydialog
$(edit) easydialog.in > easydialog
-tools: tools.in
+parseini: #parseini.in
+ rm -f parseini
+ $(edit) parseini.in > parseini
+
+tools: #tools.in
rm -f tools
$(edit) tools.in > tools
diff --git a/lib/Makefile.in b/lib/Makefile.in
index 8ce67a9..3da86b9 100644
--- a/lib/Makefile.in
+++ b/lib/Makefile.in
@@ -111,10 +111,10 @@ sbindir = @sbindir@
sharedstatedir = @sharedstatedir@
sysconfdir = @sysconfdir@
target_alias = @target_alias@
-EXTRALIBS = easydialog tools
-CLEANFILES = $(EXTRALIBS)
-dist_pkglib_DATA = $(EXTRALIBS)
-EXTRA_DIST = easydialog.in tools.in
+EXTRA_DIST = easydialog.in parseini.in tools.in
+GENERATED_FILES = easydialog parseini tools
+dist_pkglib_DATA = $(GENERATED_FILES)
+CLEANFILES = $(GENERATED_FILES)
edit = sed \
-e "s,@BASH\@,$(BASH),g"
@@ -291,11 +291,15 @@ uninstall-am: uninstall-dist_pkglibDATA uninstall-info-am
uninstall-dist_pkglibDATA uninstall-info-am
-easydialog: easydialog.in
+easydialog: #easydialog.in
rm -f easydialog
$(edit) easydialog.in > easydialog
-tools: tools.in
+parseini: #parseini.in
+ rm -f parseini
+ $(edit) parseini.in > parseini
+
+tools: #tools.in
rm -f tools
$(edit) tools.in > tools
# Tell versions [3.59,3.63) of GNU make to not export all variables.
diff --git a/lib/parseini.in b/lib/parseini.in
new file mode 100644
index 0000000..6f56d42
--- /dev/null
+++ b/lib/parseini.in
@@ -0,0 +1,130 @@
+#
+# parseini --- parses 'ini' style configuration files.
+#
+# Usage:
+# awk -f parseini S=<section> P=<param> <ini file>
+#
+# if section is an empty string, then we use the default section
+#
+# example ini file:
+#
+# fruit = apple
+# fruit = pear
+# multiline = this is a multiline \
+# parameter
+#
+# # this is a comment
+#
+# [colors]
+# red = yes
+# green = no
+# blue = maybe
+#
+# [ocean]
+# fish = red
+# fish = blue
+#
+# example usage:
+# > awk -f parseini S=ocean P=fish testfile.ini
+# would return:
+# red
+# blue
+#
+
+BEGIN {
+ readlines = 1
+ implied = 1
+}
+
+# remove lines starting with #, but not #!
+/^#[^!]/ {next}
+
+# skip blank
+/^[ \r\t]*$/ {next}
+
+# we want to read the lines of the matched section
+# and disable for other sections
+/^\[.+\][ \r\t]*$/ {
+ continueline = 0
+ if (S && implied) {
+ nline = 0
+ implied = 0
+ }
+ if (S && match($0, "^\\[" S "\\][ \n]*")) {
+ # we found the section, so start reading.
+ readlines = 1
+ }
+ else {
+ # no section, so stop reading lines
+ if (readlines) readlines = 0
+ }
+ next
+}
+
+# when reading, store lines.
+
+{
+ if (!readlines) next
+ line[nline++] = $0
+ if ($0 ~ /\\[ \r\t]*$/)
+ continueline = 1
+ else
+ continueline = 0
+}
+
+# process the read lines lines, matching parameters
+
+END {
+ # if section is set but implied is still true
+ # then we never found the section, so use everything
+ if (S && implied) {
+ nline = 0
+ }
+
+ # if have P then find P in read lines and get values
+ if (P) {
+ MATCH = "^[ \r\t]*" P "[ \r\t]*="
+ continueline = 0
+ for (x = 0; x < nline; ++x) {
+ v = line[x]
+ if (continueline) {
+ sub(/[ \r\t]+$/, "", v)
+ if (v ~ /\\$/) {
+ v = substr(v, 1, length(v)-1)
+ sub(/[ \r\t]+$/, "", v)
+ }
+ if (v) value[nvalue++] = v
+ }
+ else if (v ~ MATCH) {
+ sub(MATCH, "", v)
+ sub(/^[ \r\t]+/, "", v)
+ sub(/[ \r\t]+$/, "", v)
+ if (v ~ /\\$/) {
+ continueline = 1
+ v = substr(v, 1, length(v)-1)
+ sub(/[ \r\t]+$/, "", v)
+ }
+ if (v) value[nvalue++] = v
+ }
+ }
+ # copy parameter definition to output array
+ nline = nvalue
+ for (x = 0; x < nvalue; ++x)
+ line[x] = value[x]
+ }
+
+ # trim all leading & trailing whitespace;
+ # except for leading whitespace in continuation lines,
+
+ for (x = 0; x < nline; ++x) {
+ sub(/^[ \r\t]+/, "", line[x])
+ sub(/[ \r\t]+$/, "", line[x])
+ }
+
+ # output the final result
+ for (x = 0; x < nline; ++x)
+ print line[x]
+
+ if (nline) exit 0
+ else exit 1
+}
diff --git a/lib/tools.in b/lib/tools.in
index 89f2ff7..90df264 100644
--- a/lib/tools.in
+++ b/lib/tools.in
@@ -1,5 +1,9 @@
#!@BASH@
+# This file contains functions shared between ninjahelper and backupninja.
+
+#####################################################
+## MISC FUNCTIONS
#
# create a temporary file in a secure way.
@@ -16,4 +20,35 @@ function maketemp() {
echo $tempfile
}
+#####################################################
+## CONFIG-FILE RELATED FUNCTIONS
+
+function setfile() {
+ CURRENT_CONF_FILE=$1
+}
+
+function setsection() {
+ CURRENT_SECTION=$1
+}
+
+#
+# sets a global var with name equal to $1
+# to the value of the configuration parameter $1
+# $2 is the default.
+#
+function getconf() {
+ CURRENT_PARAM=$1
+ ret=`awk -f $libdirectory/parseini S=$CURRENT_SECTION P=$CURRENT_PARAM $CURRENT_CONF_FILE`
+ # if nothing is returned, set the default
+ if [ "$ret" == "" -a "$2" != "" ]; then
+ ret="$2"
+ fi
+ # replace * with %, so that it is not globbed.
+ ret="${ret//\\*/__star__}"
+
+ # this is weird, but single quotes are needed to
+ # allow for returned values with spaces. $ret is still expanded
+ # because it is in an 'eval' statement.
+ eval $1='$ret'
+}