Deleted Added
full compact
functions-parse.sh (234985) functions-parse.sh (247705)
1#!/bin/sh
2#-
3# Copyright (c) 2010 iXsystems, Inc. All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8# 1. Redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer.
10# 2. Redistributions in binary form must reproduce the above copyright
11# notice, this list of conditions and the following disclaimer in the
12# documentation and/or other materials provided with the distribution.
13#
14# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24# SUCH DAMAGE.
25#
1#!/bin/sh
2#-
3# Copyright (c) 2010 iXsystems, Inc. All rights reserved.
4#
5# Redistribution and use in source and binary forms, with or without
6# modification, are permitted provided that the following conditions
7# are met:
8# 1. Redistributions of source code must retain the above copyright
9# notice, this list of conditions and the following disclaimer.
10# 2. Redistributions in binary form must reproduce the above copyright
11# notice, this list of conditions and the following disclaimer in the
12# documentation and/or other materials provided with the distribution.
13#
14# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24# SUCH DAMAGE.
25#
26# $FreeBSD: head/usr.sbin/pc-sysinstall/backend/functions-parse.sh 234985 2012-05-03 21:15:47Z jpaetzel $
26# $FreeBSD: head/usr.sbin/pc-sysinstall/backend/functions-parse.sh 247705 2013-03-03 09:47:47Z jpaetzel $
27
28# functions.sh
29# Library of functions which pc-sysinstall may call upon for parsing the config
30
31# which gets the value of a setting in the provided line
32get_value_from_string()
33{
34 if [ -n "${1}" ]
35 then
36 export VAL="`echo ${1} | cut -d '=' -f 2-`"
37 else
38 echo "Error: Did we forgot to supply a string to parse?"
39 exit 1
40 fi
41};
42
43# Get the value from the cfg file including spaces
44get_value_from_cfg_with_spaces()
45{
46 if [ -n "${1}" ]
47 then
27
28# functions.sh
29# Library of functions which pc-sysinstall may call upon for parsing the config
30
31# which gets the value of a setting in the provided line
32get_value_from_string()
33{
34 if [ -n "${1}" ]
35 then
36 export VAL="`echo ${1} | cut -d '=' -f 2-`"
37 else
38 echo "Error: Did we forgot to supply a string to parse?"
39 exit 1
40 fi
41};
42
43# Get the value from the cfg file including spaces
44get_value_from_cfg_with_spaces()
45{
46 if [ -n "${1}" ]
47 then
48 export VAL=`grep "^${1}=" ${CFGF} | head -n 1 | cut -d '=' -f 2-`
48 export VAL="`grep ^${1}= ${CFGF} | head -n 1 | cut -d '=' -f 2-`"
49 else
50 exit_err "Error: Did we forgot to supply a setting to grab?"
51 fi
52};
53
54
55# Get the value from the cfg file
56get_value_from_cfg()
57{
58 if [ -n "${1}" ]
59 then
60 export VAL=`grep "^${1}=" ${CFGF} | head -n 1 | cut -d '=' -f 2- | tr -d ' '`
61 else
62 exit_err "Error: Did we forgot to supply a setting to grab?"
63 fi
64};
65
66# Checks the value of a setting in the provided line with supplied possibilities
67# 1 = setting we are checking, 2 = list of valid values
68if_check_value_exists()
69{
70 if [ -n "${1}" -a -n "${2}" ]
71 then
72 # Get the first occurrence of the setting from the config, strip out whitespace
73
74 VAL=`grep "^${1}" ${CFGF} | head -n 1 | cut -d '=' -f 2- | tr -d ' '`
75 if [ -z "${VAL}" ]
76 then
77 # This value doesn't exist, lets return
78 return 0
79 fi
80
81
82 VALID="1"
83 for i in ${2}
84 do
85 VAL=`echo "$VAL"|tr A-Z a-z`
86 if [ "$VAL" = "${i}" ]
87 then
88 VALID="0"
89 fi
90 done
91 if [ "$VALID" = "1" ]
92 then
93 exit_err "Error: ${1} is set to unknown value $VAL"
94 fi
95 else
96 exit_err "Error: Did we forgot to supply a string to parse and setting to grab?"
97 fi
98};
99
100# Checks the value of a setting in the provided line with supplied possibilities
101# 1 = setting we are checking, 2 = list of valid values
102check_value()
103{
104 if [ -n "${1}" -a -n "${2}" ]
105 then
106 # Get the first occurrence of the setting from the config, strip out whitespace
107 VAL=`grep "^${1}" ${CFGF} | head -n 1 | cut -d '=' -f 2- | tr -d ' '`
108 VALID="1"
109 for i in ${2}
110 do
111 if [ "$VAL" = "${i}" ]
112 then
113 VALID="0"
114 fi
115 done
116 if [ "$VALID" = "1" ]
117 then
118 exit_err "Error: ${1} is set to unknown value $VAL"
119 fi
120 else
121 exit_err "Error: Did we forgot to supply a string to parse and setting to grab?"
122 fi
123};
124
125# Checks for the presence of the supplied arguments in the config file
126# 1 = values to confirm exist
127file_sanity_check()
128{
129 if [ -n "$CFGF" -a -n "$1" ]
130 then
131 for i in $1
132 do
133 grep -q "^${i}=" $CFGF 2>/dev/null
134 if [ $? -eq 0 ]
135 then
136 LN=`grep "^${i}=" ${CFGF} | head -n 1 | cut -d '=' -f 2- | tr -d ' '`
137 if [ -z "${LN}" ]
138 then
139 echo "Error: Config fails sanity test! ${i}= is empty"
140 exit 1
141 fi
142 else
143 echo "Error: Config fails sanity test! Missing ${i}="
144 exit 1
145 fi
146 done
147 else
148 echo "Error: Missing config file, and / or values to sanity check for!"
149 exit 1
150 fi
151};
152
153
154# Function which merges the contents of a new config into the specified old one
155# Only works with <val>= type configurations
156merge_config()
157{
158 OLDCFG="${1}"
159 NEWCFG="${2}"
160 FINALCFG="${3}"
161
162 # Copy our oldcfg to the new one, which will be used as basis
163 cp ${OLDCFG} ${FINALCFG}
164
165 # Remove blank lines from new file
166 cat ${NEWCFG} | sed '/^$/d' > ${FINALCFG}.tmp
167
168 # Set our marker if we've found any
169 FOUNDMERGE="NO"
170
171 while read newline
172 do
173 echo ${newline} | grep -q "^#" 2>/dev/null
174 if [ $? -ne 0 ] ; then
175 VAL="`echo ${newline} | cut -d '=' -f 1`"
176 cat ${OLDCFG} | grep -q ${VAL} 2>/dev/null
177 if [ $? -ne 0 ] ; then
178 if [ "${FOUNDMERGE}" = "NO" ] ; then
179 echo "" >> ${FINALCFG}
180 echo "# Auto-merged values from newer ${NEWCFG}" >> ${FINALCFG}
181 FOUNDMERGE="YES"
182 fi
183 echo "${newline}" >> ${FINALCFG}
184 fi
185 fi
186 done < ${FINALCFG}.tmp
187 rm ${FINALCFG}.tmp
188
189};
190
191# Loop to check for a specified mount-point in a list
192check_for_mount()
193{
194 MNTS="${1}"
195 FINDMNT="${2}"
196
197 # Check if we found a valid root partition
198 for CHECKMNT in `echo ${MNTS} | sed 's|,| |g'`
199 do
200 if [ "${CHECKMNT}" = "${FINDMNT}" ] ; then
201 return 0
202 fi
203 done
204
205 return 1
206};
207
208# Function which returns the next line in the specified config file
209get_next_cfg_line()
210{
211 CURFILE="$1"
212 CURLINE="$2"
213
214 FOUND="1"
215
216 while read line
217 do
218 if [ "$FOUND" = "0" ] ; then
219 export VAL="$line"
220 return
221 fi
222 if [ "$line" = "${CURLINE}" ] ; then
223 FOUND="0"
224 fi
225 done <${CURFILE}
226
227 # Got here, couldn't find this line or at end of file, set VAL to ""
228 export VAL=""
229};
49 else
50 exit_err "Error: Did we forgot to supply a setting to grab?"
51 fi
52};
53
54
55# Get the value from the cfg file
56get_value_from_cfg()
57{
58 if [ -n "${1}" ]
59 then
60 export VAL=`grep "^${1}=" ${CFGF} | head -n 1 | cut -d '=' -f 2- | tr -d ' '`
61 else
62 exit_err "Error: Did we forgot to supply a setting to grab?"
63 fi
64};
65
66# Checks the value of a setting in the provided line with supplied possibilities
67# 1 = setting we are checking, 2 = list of valid values
68if_check_value_exists()
69{
70 if [ -n "${1}" -a -n "${2}" ]
71 then
72 # Get the first occurrence of the setting from the config, strip out whitespace
73
74 VAL=`grep "^${1}" ${CFGF} | head -n 1 | cut -d '=' -f 2- | tr -d ' '`
75 if [ -z "${VAL}" ]
76 then
77 # This value doesn't exist, lets return
78 return 0
79 fi
80
81
82 VALID="1"
83 for i in ${2}
84 do
85 VAL=`echo "$VAL"|tr A-Z a-z`
86 if [ "$VAL" = "${i}" ]
87 then
88 VALID="0"
89 fi
90 done
91 if [ "$VALID" = "1" ]
92 then
93 exit_err "Error: ${1} is set to unknown value $VAL"
94 fi
95 else
96 exit_err "Error: Did we forgot to supply a string to parse and setting to grab?"
97 fi
98};
99
100# Checks the value of a setting in the provided line with supplied possibilities
101# 1 = setting we are checking, 2 = list of valid values
102check_value()
103{
104 if [ -n "${1}" -a -n "${2}" ]
105 then
106 # Get the first occurrence of the setting from the config, strip out whitespace
107 VAL=`grep "^${1}" ${CFGF} | head -n 1 | cut -d '=' -f 2- | tr -d ' '`
108 VALID="1"
109 for i in ${2}
110 do
111 if [ "$VAL" = "${i}" ]
112 then
113 VALID="0"
114 fi
115 done
116 if [ "$VALID" = "1" ]
117 then
118 exit_err "Error: ${1} is set to unknown value $VAL"
119 fi
120 else
121 exit_err "Error: Did we forgot to supply a string to parse and setting to grab?"
122 fi
123};
124
125# Checks for the presence of the supplied arguments in the config file
126# 1 = values to confirm exist
127file_sanity_check()
128{
129 if [ -n "$CFGF" -a -n "$1" ]
130 then
131 for i in $1
132 do
133 grep -q "^${i}=" $CFGF 2>/dev/null
134 if [ $? -eq 0 ]
135 then
136 LN=`grep "^${i}=" ${CFGF} | head -n 1 | cut -d '=' -f 2- | tr -d ' '`
137 if [ -z "${LN}" ]
138 then
139 echo "Error: Config fails sanity test! ${i}= is empty"
140 exit 1
141 fi
142 else
143 echo "Error: Config fails sanity test! Missing ${i}="
144 exit 1
145 fi
146 done
147 else
148 echo "Error: Missing config file, and / or values to sanity check for!"
149 exit 1
150 fi
151};
152
153
154# Function which merges the contents of a new config into the specified old one
155# Only works with <val>= type configurations
156merge_config()
157{
158 OLDCFG="${1}"
159 NEWCFG="${2}"
160 FINALCFG="${3}"
161
162 # Copy our oldcfg to the new one, which will be used as basis
163 cp ${OLDCFG} ${FINALCFG}
164
165 # Remove blank lines from new file
166 cat ${NEWCFG} | sed '/^$/d' > ${FINALCFG}.tmp
167
168 # Set our marker if we've found any
169 FOUNDMERGE="NO"
170
171 while read newline
172 do
173 echo ${newline} | grep -q "^#" 2>/dev/null
174 if [ $? -ne 0 ] ; then
175 VAL="`echo ${newline} | cut -d '=' -f 1`"
176 cat ${OLDCFG} | grep -q ${VAL} 2>/dev/null
177 if [ $? -ne 0 ] ; then
178 if [ "${FOUNDMERGE}" = "NO" ] ; then
179 echo "" >> ${FINALCFG}
180 echo "# Auto-merged values from newer ${NEWCFG}" >> ${FINALCFG}
181 FOUNDMERGE="YES"
182 fi
183 echo "${newline}" >> ${FINALCFG}
184 fi
185 fi
186 done < ${FINALCFG}.tmp
187 rm ${FINALCFG}.tmp
188
189};
190
191# Loop to check for a specified mount-point in a list
192check_for_mount()
193{
194 MNTS="${1}"
195 FINDMNT="${2}"
196
197 # Check if we found a valid root partition
198 for CHECKMNT in `echo ${MNTS} | sed 's|,| |g'`
199 do
200 if [ "${CHECKMNT}" = "${FINDMNT}" ] ; then
201 return 0
202 fi
203 done
204
205 return 1
206};
207
208# Function which returns the next line in the specified config file
209get_next_cfg_line()
210{
211 CURFILE="$1"
212 CURLINE="$2"
213
214 FOUND="1"
215
216 while read line
217 do
218 if [ "$FOUND" = "0" ] ; then
219 export VAL="$line"
220 return
221 fi
222 if [ "$line" = "${CURLINE}" ] ; then
223 FOUND="0"
224 fi
225 done <${CURFILE}
226
227 # Got here, couldn't find this line or at end of file, set VAL to ""
228 export VAL=""
229};