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