1209513Simp#!/bin/sh 2209513Simp#- 3209552Simp# Copyright (c) 2010 iXsystems, Inc. All rights reserved. 4209513Simp# 5209513Simp# Redistribution and use in source and binary forms, with or without 6209513Simp# modification, are permitted provided that the following conditions 7209513Simp# are met: 8209513Simp# 1. Redistributions of source code must retain the above copyright 9209513Simp# notice, this list of conditions and the following disclaimer. 10209513Simp# 2. Redistributions in binary form must reproduce the above copyright 11209513Simp# notice, this list of conditions and the following disclaimer in the 12209513Simp# documentation and/or other materials provided with the distribution. 13209513Simp# 14209513Simp# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 15209513Simp# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 16209513Simp# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 17209513Simp# ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 18209513Simp# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 19209513Simp# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 20209513Simp# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21209513Simp# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 22209513Simp# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 23209513Simp# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 24209513Simp# SUCH DAMAGE. 25209513Simp# 26209513Simp# $FreeBSD: releng/10.2/usr.sbin/pc-sysinstall/backend/functions-parse.sh 247705 2013-03-03 09:47:47Z jpaetzel $ 27209513Simp 28209513Simp# functions.sh 29209513Simp# Library of functions which pc-sysinstall may call upon for parsing the config 30209513Simp 31209513Simp# which gets the value of a setting in the provided line 32209513Simpget_value_from_string() 33209513Simp{ 34220059Sjpaetzel if [ -n "${1}" ] 35209513Simp then 36234985Sjpaetzel export VAL="`echo ${1} | cut -d '=' -f 2-`" 37209513Simp else 38209513Simp echo "Error: Did we forgot to supply a string to parse?" 39209513Simp exit 1 40209513Simp fi 41209513Simp}; 42209513Simp 43209513Simp# Get the value from the cfg file including spaces 44209513Simpget_value_from_cfg_with_spaces() 45209513Simp{ 46220059Sjpaetzel if [ -n "${1}" ] 47209513Simp then 48247705Sjpaetzel export VAL="`grep ^${1}= ${CFGF} | head -n 1 | cut -d '=' -f 2-`" 49209513Simp else 50209513Simp exit_err "Error: Did we forgot to supply a setting to grab?" 51209513Simp fi 52209513Simp}; 53209513Simp 54209513Simp 55209513Simp# Get the value from the cfg file 56209513Simpget_value_from_cfg() 57209513Simp{ 58220059Sjpaetzel if [ -n "${1}" ] 59209513Simp then 60234985Sjpaetzel export VAL=`grep "^${1}=" ${CFGF} | head -n 1 | cut -d '=' -f 2- | tr -d ' '` 61209513Simp else 62209513Simp exit_err "Error: Did we forgot to supply a setting to grab?" 63209513Simp fi 64209513Simp}; 65209513Simp 66209513Simp# Checks the value of a setting in the provided line with supplied possibilities 67209513Simp# 1 = setting we are checking, 2 = list of valid values 68209513Simpif_check_value_exists() 69209513Simp{ 70220059Sjpaetzel if [ -n "${1}" -a -n "${2}" ] 71209513Simp then 72228990Suqs # Get the first occurrence of the setting from the config, strip out whitespace 73209513Simp 74234985Sjpaetzel VAL=`grep "^${1}" ${CFGF} | head -n 1 | cut -d '=' -f 2- | tr -d ' '` 75209513Simp if [ -z "${VAL}" ] 76209513Simp then 77209513Simp # This value doesn't exist, lets return 78209513Simp return 0 79209513Simp fi 80209513Simp 81209513Simp 82209513Simp VALID="1" 83209513Simp for i in ${2} 84209513Simp do 85212337Simp VAL=`echo "$VAL"|tr A-Z a-z` 86209513Simp if [ "$VAL" = "${i}" ] 87209513Simp then 88209513Simp VALID="0" 89209513Simp fi 90209513Simp done 91209513Simp if [ "$VALID" = "1" ] 92209513Simp then 93209513Simp exit_err "Error: ${1} is set to unknown value $VAL" 94209513Simp fi 95209513Simp else 96209513Simp exit_err "Error: Did we forgot to supply a string to parse and setting to grab?" 97209513Simp fi 98209513Simp}; 99209513Simp 100209513Simp# Checks the value of a setting in the provided line with supplied possibilities 101209513Simp# 1 = setting we are checking, 2 = list of valid values 102209513Simpcheck_value() 103209513Simp{ 104220059Sjpaetzel if [ -n "${1}" -a -n "${2}" ] 105209513Simp then 106228990Suqs # Get the first occurrence of the setting from the config, strip out whitespace 107234985Sjpaetzel VAL=`grep "^${1}" ${CFGF} | head -n 1 | cut -d '=' -f 2- | tr -d ' '` 108209513Simp VALID="1" 109209513Simp for i in ${2} 110209513Simp do 111209513Simp if [ "$VAL" = "${i}" ] 112209513Simp then 113209513Simp VALID="0" 114209513Simp fi 115209513Simp done 116209513Simp if [ "$VALID" = "1" ] 117209513Simp then 118209513Simp exit_err "Error: ${1} is set to unknown value $VAL" 119209513Simp fi 120209513Simp else 121209513Simp exit_err "Error: Did we forgot to supply a string to parse and setting to grab?" 122209513Simp fi 123209513Simp}; 124209513Simp 125228990Suqs# Checks for the presence of the supplied arguments in the config file 126209513Simp# 1 = values to confirm exist 127209513Simpfile_sanity_check() 128209513Simp{ 129220059Sjpaetzel if [ -n "$CFGF" -a -n "$1" ] 130209513Simp then 131209513Simp for i in $1 132209513Simp do 133220059Sjpaetzel grep -q "^${i}=" $CFGF 2>/dev/null 134220059Sjpaetzel if [ $? -eq 0 ] 135211730Simp then 136234985Sjpaetzel LN=`grep "^${i}=" ${CFGF} | head -n 1 | cut -d '=' -f 2- | tr -d ' '` 137211730Simp if [ -z "${LN}" ] 138211730Simp then 139211730Simp echo "Error: Config fails sanity test! ${i}= is empty" 140211730Simp exit 1 141211730Simp fi 142211730Simp else 143211730Simp echo "Error: Config fails sanity test! Missing ${i}=" 144211730Simp exit 1 145211730Simp fi 146209513Simp done 147209513Simp else 148209513Simp echo "Error: Missing config file, and / or values to sanity check for!" 149209513Simp exit 1 150209513Simp fi 151209513Simp}; 152209513Simp 153209513Simp 154209513Simp# Function which merges the contents of a new config into the specified old one 155209513Simp# Only works with <val>= type configurations 156209513Simpmerge_config() 157209513Simp{ 158209513Simp OLDCFG="${1}" 159209513Simp NEWCFG="${2}" 160209513Simp FINALCFG="${3}" 161209513Simp 162209513Simp # Copy our oldcfg to the new one, which will be used as basis 163209513Simp cp ${OLDCFG} ${FINALCFG} 164209513Simp 165209513Simp # Remove blank lines from new file 166209513Simp cat ${NEWCFG} | sed '/^$/d' > ${FINALCFG}.tmp 167209513Simp 168209513Simp # Set our marker if we've found any 169209513Simp FOUNDMERGE="NO" 170209513Simp 171209513Simp while read newline 172209513Simp do 173220059Sjpaetzel echo ${newline} | grep -q "^#" 2>/dev/null 174220059Sjpaetzel if [ $? -ne 0 ] ; then 175209513Simp VAL="`echo ${newline} | cut -d '=' -f 1`" 176220059Sjpaetzel cat ${OLDCFG} | grep -q ${VAL} 2>/dev/null 177220059Sjpaetzel if [ $? -ne 0 ] ; then 178209513Simp if [ "${FOUNDMERGE}" = "NO" ] ; then 179209513Simp echo "" >> ${FINALCFG} 180209513Simp echo "# Auto-merged values from newer ${NEWCFG}" >> ${FINALCFG} 181209513Simp FOUNDMERGE="YES" 182209513Simp fi 183209513Simp echo "${newline}" >> ${FINALCFG} 184209513Simp fi 185209513Simp fi 186209513Simp done < ${FINALCFG}.tmp 187209513Simp rm ${FINALCFG}.tmp 188209513Simp 189209513Simp}; 190209513Simp 191209513Simp# Loop to check for a specified mount-point in a list 192209513Simpcheck_for_mount() 193209513Simp{ 194209513Simp MNTS="${1}" 195209513Simp FINDMNT="${2}" 196209513Simp 197209513Simp # Check if we found a valid root partition 198209513Simp for CHECKMNT in `echo ${MNTS} | sed 's|,| |g'` 199209513Simp do 200209513Simp if [ "${CHECKMNT}" = "${FINDMNT}" ] ; then 201209513Simp return 0 202209513Simp fi 203209513Simp done 204209513Simp 205209513Simp return 1 206209513Simp}; 207209513Simp 208209513Simp# Function which returns the next line in the specified config file 209209513Simpget_next_cfg_line() 210209513Simp{ 211209513Simp CURFILE="$1" 212209513Simp CURLINE="$2" 213209513Simp 214209513Simp FOUND="1" 215209513Simp 216209513Simp while read line 217209513Simp do 218209513Simp if [ "$FOUND" = "0" ] ; then 219220059Sjpaetzel export VAL="$line" 220209513Simp return 221209513Simp fi 222209513Simp if [ "$line" = "${CURLINE}" ] ; then 223209513Simp FOUND="0" 224209513Simp fi 225209513Simp done <${CURFILE} 226209513Simp 227209513Simp # Got here, couldn't find this line or at end of file, set VAL to "" 228220059Sjpaetzel export VAL="" 229209513Simp}; 230