-
Notifications
You must be signed in to change notification settings - Fork 0
/
ini
executable file
·139 lines (111 loc) · 3.84 KB
/
ini
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
#!/bin/bash
# Copyright (C) 2015 Benjamin Abendroth
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
################################################################################
# Ini File functions ###########################################################
################################################################################
include string error_messages
ini_set_file () {
declare -g __INI_FILE="${1:?$FUNCNAME: Missing argument}"
}
ini_get_file () {
echo "$__INI_FILE"
}
ini_file_okay () {
local FILE="${__INI_FILE:?$FUNCNAME: \$__INI_FILE not set}"
if [[ -f "$FILE" ]] && [[ -r "$FILE" ]] ; then
return 0
elif ! [[ -e "$FILE" ]] ; then
error_missing_file "$FILE"
return 1
elif [[ -d "$FILE" ]] ; then
error_is_directory "$FILE"
return 1
elif ! [[ -r "$FILE" ]] ; then
error_permission_denied "$FILE"
return 1
fi
}
ini_print_file () {
ini_file_okay || return 1
local S='[[:space:]]' # space class
local N='[^[:space:]]' # non space class
local NP='[^[:print:]]' # non printable class
# remove leading spaces
# remove spaces aside of the equal sign (e.g. "variable = value" turns to "variable=value")
# remove spaces in sections (e.g. "[ section ]" turns to "[section]")
# remove all non printable characters
# remove comments ("#" and ";") and empty lines
sed -r "s/^$S+//g; s/^($N+)$S*=$S*/\1=/g; s/\[$S*($N+)$S*\]/[\1]/g; s/$NP//g" "$__INI_FILE" | grep -Eo '^[^#;].+'
}
ini_list_sections () {
ini_file_okay || return 1
ini_print_file | grep '^\[' | tr -d \[\]
}
ini_list_variables () {
ini_file_okay || return 1
local SECTION="${1:?$FUNCNAME: Missing argument}"
ini_get_section "$SECTION" | sed 's/=.*//g'
}
ini_section_exists () {
ini_file_okay || return 1
local SECTION="${1:?$FUNCNAME: Missing argument}"
ini_print_file | grep -qF "[$SECTION]"
}
ini_get_section () {
ini_file_okay || return 1
local SECTION="${1:?$FUNCNAME: Missing argument}"
local REPLY SEC_OPEN=0
ini_print_file | while read; do
if [[ "${REPLY:0:1}" == "[" ]] ; then # is it a new section?
if [[ "${REPLY}" == "[$SECTION]" ]] ; then # is it the wanted section?
SEC_OPEN=1 # yes: section is now open!
continue
else
SEC_OPEN=0 # no: section is closed
continue
fi
fi
[[ $SEC_OPEN == 1 ]] && echo "$REPLY"
done
}
ini_get_variable () {
ini_file_okay || return 1
local SECTION="${1:?$FUNCNAME: Missing argument}"
local VARIABLE="${2:?$FUNCNAME: Missing argument}"
local VALUE=`ini_get_section "$SECTION" | grep -F "$VARIABLE="`
unquote "${VALUE:$(( ${#VARIABLE} + 1 ))}"
}
ini_variable_exists () {
ini_file_okay || return 1
local SECTION="${1:?$FUNCNAME: Missing argument}"
local VARIABLE="${2:?$FUNCNAME: Missing argument}"
ini_list_variables "$SECTION" | grep -qF "$VARIABLE="
}
ini_remove_section () {
ini_file_okay || return 1
local SECTION="${1:?$FUNCNAME: Missing argument}"
local REPLY SEC_OPEN=0
ini_print_file | while read; do
if [[ "${REPLY:0:1}" == "[" ]] ; then # is it a new section?
if [[ "${REPLY}" == "[$SECTION]" ]] ; then # is it the wanted section?
SEC_OPEN=1 # yes: section is now open!
else
SEC_OPEN=0 # no: section is closed
fi
fi
[[ $SEC_OPEN == 0 ]] && echo "$REPLY"
done
}