1 #!/bin/sh 2 3 # Search for a definition in the definitions file, expanding it recursively to 4 # provide a complete description. 5 # 6 # Copyright (C) 2019 Paul Boddie <paul@boddie.org.uk> 7 # 8 # This program is free software; you can redistribute it and/or modify it under 9 # the terms of the GNU General Public License as published by the Free Software 10 # Foundation; either version 3 of the License, or (at your option) any later 11 # version. 12 # 13 # This program is distributed in the hope that it will be useful, but WITHOUT 14 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS 15 # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more 16 # details. 17 # 18 # You should have received a copy of the GNU General Public License along with 19 # this program. If not, see <http://www.gnu.org/licenses/>. 20 21 22 PROGNAME=`basename "$0"` 23 THISDIR=`dirname "$0"` 24 25 COMMON="$THISDIR/makesd-common" 26 DEFS="$THISDIR/makesd-defs" 27 28 . "$COMMON" 29 30 31 32 # lookup <definition> 33 # 34 # Search for the given definition in the definitions file. Emit the full 35 # definition. 36 37 lookup() 38 { 39 local LINENUM 40 41 # Obtain the line number of the matching definition. 42 43 LINENUM=`search "^$1$" "$DEFS"` 44 45 if [ "$LINENUM" ] ; then 46 47 # Read from the definition until a blank line or the end of file is 48 # encountered. Line continuations are observed. 49 50 tail -n "+$(($LINENUM + 1))" "$DEFS" | while read LINE ; do 51 52 # Detect end of definition. 53 54 if [ ! "$LINE" ] ; then 55 break 56 fi 57 58 # Show definition lines. 59 60 echo "$LINE" 61 done 62 fi 63 } 64 65 # consolidate 66 # 67 # Eliminate duplicate properties, preserving only the final definition. 68 69 consolidate() 70 { 71 sort -k1,1 -t: -s | show_last 72 } 73 74 show_last() 75 { 76 local FIELD LAST LASTLINE 77 78 LAST= 79 LASTLINE= 80 81 while read LINE ; do 82 83 # Inspect the property details. 84 85 FIELD=`get_field "$LINE"` 86 87 # With a differing property from any previous one, emit the previous 88 # one. 89 90 if [ "$LAST" ] && [ "$FIELD" != "$LAST" ] ; then 91 echo "$LASTLINE" 92 fi 93 94 # Retain this property definition for possible future emission. 95 96 LAST=$FIELD 97 LASTLINE=$LINE 98 done 99 100 # Emit any remaining line, also indicating whether any lines were processed. 101 102 if [ "$LASTLINE" ] ; then 103 echo "$LASTLINE" 104 return 0 105 else 106 return 1 107 fi 108 } 109 110 # expand <definition> 111 # 112 # Expand the given definition name to its value, recursively expanding any 113 # definition names found in the value text. 114 115 expand() 116 { 117 local TYPE 118 119 TYPE=`echo "$1" | cut -d' ' -f1` 120 121 # Look up the definition and read each line. 122 123 lookup "$1" | expand_def "$TYPE" 124 } 125 126 expand_def() 127 { 128 local FIELD LINE RESULT TYPE VALUE 129 130 RESULT=1 131 132 while read LINE ; do 133 RESULT=0 134 135 # Inspect the line and find any references to other definitions. 136 137 FIELD=`get_field "$LINE"` 138 VALUE=`get_value "$LINE"` 139 140 # Merge definitions that are adapted. 141 142 if [ "$FIELD" = 'adapts' ] ; then 143 expand "$TYPE $VALUE" 144 145 # Aggregate system components such as partitions and regions. 146 147 elif [ "$TYPE" = 'system' ] && ( 148 [ "$FIELD" = 'partition' ] || [ "$FIELD" = 'region' ] ) ; then 149 150 # Declare regions and partitions in the output. 151 152 echo "$FIELD" 153 expand "$FIELD $VALUE" | consolidate 154 echo 155 156 # Otherwise, emit properties of the definition. 157 158 else 159 echo "$LINE" 160 fi 161 done 162 163 return $RESULT 164 } 165 166 167 168 # Obtain the requested system name. 169 170 if [ ! "$1" ] || [ "$1" = '--help' ] ; then 171 cat 1>&2 <<EOF 172 Usage: $PROGNAME <system> 173 $PROGNAME -a | --all | --defs 174 175 Search for a definition of the given system in the definitions file: 176 177 $DEFS 178 179 If the definition can be found, the value of the definition is emitted and an 180 exit value of 0 returned. Otherwise, no output is produced and an exit value of 181 1 is returned. 182 183 $(show_definitions_description) 184 EOF 185 exit 1 186 fi 187 188 # Show definitions if requested. 189 190 if [ "$1" = '-a' ] || [ "$1" = '--all' ] || [ "$1" = '--defs' ] ; then 191 grep '^system' "$DEFS" | sed 's/^system //' | sort 192 exit 0 193 fi 194 195 # Expand the definition and return the result code. 196 197 DEF=$1 198 199 expand "system $DEF" 200 exit $?