functions-parse.sh revision 212337
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 212337 2010-09-08 20:10:24Z imp $
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 [ ! -z "${1}" ]
35  then
36    VAL="`echo ${1} | cut -d '=' -f 2`"
37    export VAL
38  else
39    echo "Error: Did we forgot to supply a string to parse?"
40    exit 1
41  fi
42};
43
44# Get the value from the cfg file including spaces
45get_value_from_cfg_with_spaces()
46{
47  if [ ! -z "${1}" ]
48  then
49    VAL=`grep "^${1}=" ${CFGF} | head -n 1 | cut -d '=' -f 2`
50    export VAL
51  else
52    exit_err "Error: Did we forgot to supply a setting to grab?"
53  fi
54};
55
56
57# Get the value from the cfg file
58get_value_from_cfg()
59{
60  if [ ! -z "${1}" ]
61  then
62    VAL=`grep "^${1}=" ${CFGF} | head -n 1 | cut -d '=' -f 2 | tr -d ' '`
63    export VAL
64  else
65    exit_err "Error: Did we forgot to supply a setting to grab?"
66  fi
67};
68
69# Checks the value of a setting in the provided line with supplied possibilities
70# 1 = setting we are checking,  2 = list of valid values
71if_check_value_exists()
72{
73  if [ ! -z "${1}" -a ! -z "${2}" ]
74  then
75    # Get the first occurance of the setting from the config, strip out whitespace
76
77    VAL=`grep "^${1}" ${CFGF} | head -n 1 | cut -d '=' -f 2 | tr -d ' '`
78    if [ -z "${VAL}" ]
79    then
80      # This value doesn't exist, lets return
81      return 0
82    fi
83
84
85    VALID="1"
86    for i in ${2}
87    do
88      VAL=`echo "$VAL"|tr A-Z a-z`
89      if [ "$VAL" = "${i}" ]
90      then 
91        VALID="0"
92      fi
93    done 
94    if [ "$VALID" = "1" ]
95    then
96      exit_err "Error: ${1} is set to unknown value $VAL"
97    fi
98  else
99    exit_err "Error: Did we forgot to supply a string to parse and setting to grab?"
100  fi
101};
102
103# Checks the value of a setting in the provided line with supplied possibilities
104# 1 = setting we are checking,  2 = list of valid values
105check_value()
106{
107  if [ ! -z "${1}" -a ! -z "${2}" ]
108  then
109    # Get the first occurance of the setting from the config, strip out whitespace
110    VAL=`grep "^${1}" ${CFGF} | head -n 1 | cut -d '=' -f 2 | tr -d ' '`
111    VALID="1"
112    for i in ${2}
113    do
114      if [ "$VAL" = "${i}" ]
115      then 
116        VALID="0"
117      fi
118    done 
119    if [ "$VALID" = "1" ]
120    then
121      exit_err "Error: ${1} is set to unknown value $VAL"
122    fi
123  else
124    exit_err "Error: Did we forgot to supply a string to parse and setting to grab?"
125  fi
126};
127
128# Checks for the presense of the supplied arguements in the config file
129# 1  = values to confirm exist
130file_sanity_check()
131{
132  if [ ! -z "$CFGF" -a ! -z "$1" ]
133  then
134    for i in $1
135    do
136      grep "^${i}=" $CFGF >/dev/null 2>/dev/null
137      if [ "$?" = "0" ]
138      then
139        LN=`grep "^${i}=" ${CFGF} | head -n 1 | cut -d '=' -f 2 | tr -d ' '`
140        if [ -z "${LN}" ]
141        then
142          echo "Error: Config fails sanity test! ${i}= is empty"
143          exit 1
144        fi
145      else
146        echo "Error: Config fails sanity test! Missing ${i}="
147        exit 1
148      fi
149    done
150  else
151    echo "Error: Missing config file, and / or values to sanity check for!"
152    exit 1
153  fi
154};
155
156
157# Function which merges the contents of a new config into the specified old one
158# Only works with <val>= type configurations
159merge_config()
160{
161  OLDCFG="${1}"
162  NEWCFG="${2}"
163  FINALCFG="${3}"
164
165  # Copy our oldcfg to the new one, which will be used as basis
166  cp ${OLDCFG} ${FINALCFG}
167
168  # Remove blank lines from new file
169  cat ${NEWCFG} | sed '/^$/d' > ${FINALCFG}.tmp
170
171  # Set our marker if we've found any
172  FOUNDMERGE="NO"
173
174  while read newline
175  do
176   echo ${newline} | grep "^#" >/dev/null 2>/dev/null
177   if [ "$?" != "0" ] ; then
178     VAL="`echo ${newline} | cut -d '=' -f 1`"
179     cat ${OLDCFG} | grep ${VAL} >/dev/null 2>/dev/null
180     if [ "$?" != "0" ] ; then
181       if [ "${FOUNDMERGE}" = "NO" ] ; then
182         echo "" >> ${FINALCFG}
183         echo "# Auto-merged values from newer ${NEWCFG}" >> ${FINALCFG}
184         FOUNDMERGE="YES"
185       fi
186       echo "${newline}" >> ${FINALCFG}
187     fi
188   fi
189  done < ${FINALCFG}.tmp
190  rm ${FINALCFG}.tmp
191
192};
193
194# Loop to check for a specified mount-point in a list
195check_for_mount()
196{
197  MNTS="${1}"
198  FINDMNT="${2}"
199
200  # Check if we found a valid root partition
201  for CHECKMNT in `echo ${MNTS} | sed 's|,| |g'`
202  do
203    if [ "${CHECKMNT}" = "${FINDMNT}" ] ; then
204      return 0
205    fi
206  done
207    
208  return 1
209};
210
211# Function which returns the next line in the specified config file
212get_next_cfg_line()
213{
214  CURFILE="$1"
215  CURLINE="$2"
216
217  FOUND="1"
218  
219  while read line
220  do
221    if [ "$FOUND" = "0" ] ; then
222      VAL="$line" ; export VAL
223      return
224    fi
225    if [ "$line" = "${CURLINE}" ] ; then
226      FOUND="0"
227    fi
228  done <${CURFILE}
229
230  # Got here, couldn't find this line or at end of file, set VAL to ""
231  VAL="" ; export VAL
232};
233