1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000
1.2 +++ b/makesd-expand-def Thu May 23 00:49:30 2019 +0200
1.3 @@ -0,0 +1,157 @@
1.4 +#!/bin/sh
1.5 +
1.6 +# Search for a definition in the definitions file, expanding it to the recorded
1.7 +# value, recursively expanding any definition references.
1.8 +
1.9 +PROGNAME=`basename "$0"`
1.10 +THISDIR=`dirname "$0"`
1.11 +
1.12 +DEFSFILE="makesd-defs"
1.13 +DEFSPATH="$THISDIR/$DEFSFILE"
1.14 +
1.15 +
1.16 +
1.17 +# Test for options.
1.18 +
1.19 +if [ "$1" = '--original' ] ; then
1.20 + FLAT=$1
1.21 + shift 1
1.22 +else
1.23 + FLAT=
1.24 +fi
1.25 +
1.26 +# Obtain the requested definition name.
1.27 +
1.28 +DEF=$1
1.29 +
1.30 +if [ ! "$DEF" ] || [ "$DEF" = '--help' ] ; then
1.31 + cat 1>&2 <<EOF
1.32 +Usage: $PROGNAME <definition name>
1.33 +
1.34 +Search for a definition of the given name in the definitions file:
1.35 +
1.36 +$DEFSPATH
1.37 +
1.38 +If the definition can be found, the value of the definition is emitted and an
1.39 +exit value of 0 returned. Otherwise, no output is produced and an exit value of
1.40 +1 is returned.
1.41 +EOF
1.42 + exit 1
1.43 +fi
1.44 +
1.45 +
1.46 +
1.47 +# lookup <definition name>
1.48 +#
1.49 +# Search for a definition of the given name in the definitions file. Emit the
1.50 +# full definition, incorporating multiple lines if continuation characters are
1.51 +# present.
1.52 +
1.53 +lookup()
1.54 +{
1.55 + local LINENUM
1.56 +
1.57 + # Obtain the line number of the matching definition.
1.58 +
1.59 + LINENUM=`grep -h -n "^$1\s" "$DEFSPATH" | cut -d: -f1`
1.60 +
1.61 + if [ "$LINENUM" ] ; then
1.62 +
1.63 + # Read from the definition line. Line continuations are observed.
1.64 +
1.65 + tail -n "+$LINENUM" "$DEFSPATH" | if read LINE ; then
1.66 + echo "$LINE"
1.67 + break
1.68 + fi
1.69 + fi
1.70 +}
1.71 +
1.72 +# match <string> <pattern>
1.73 +#
1.74 +# Attempt to match pattern in string, emitting the string if successful.
1.75 +
1.76 +match()
1.77 +{
1.78 + if `echo "$1" | grep -q "$2"` ; then
1.79 + echo "$1"
1.80 + return 0
1.81 + else
1.82 + return 1
1.83 + fi
1.84 +}
1.85 +
1.86 +# defname <prefixed name>
1.87 +#
1.88 +# Return the actual definition name from the given prefixed name.
1.89 +
1.90 +defname()
1.91 +{
1.92 + echo "$1" | sed 's/^\$//'
1.93 +}
1.94 +
1.95 +# expand <definition name>
1.96 +#
1.97 +# Expand the given definition name to its value, recursively expanding any
1.98 +# definition names found in the value text.
1.99 +
1.100 +expand()
1.101 +{
1.102 + local FOUND VALUE
1.103 +
1.104 + FOUND=
1.105 + VALUE=
1.106 +
1.107 + for WORD in `lookup "$1"`; do
1.108 + if [ ! "$FOUND" ] ; then
1.109 + FOUND=$WORD
1.110 + continue
1.111 + fi
1.112 +
1.113 + # Prevent recursive expansion if flat mode is selected.
1.114 +
1.115 + if [ ! "$FLAT" ] ; then
1.116 +
1.117 + # Identify definition names by looking for a $ prefix.
1.118 +
1.119 + DEFNAME=`match "$WORD" '^\\$'`
1.120 +
1.121 + # Attempt to expand definition names.
1.122 +
1.123 + if [ "$DEFNAME" ] ; then
1.124 + DEFNAME=`defname "$DEFNAME"`
1.125 + EXPANDED=`expand "$DEFNAME"`
1.126 +
1.127 + if [ "$EXPANDED" ] ; then
1.128 + WORD=$EXPANDED
1.129 + fi
1.130 + fi
1.131 + fi
1.132 +
1.133 + # Incorporate expansions into the final value.
1.134 +
1.135 + if [ ! "$VALUE" ] ; then
1.136 + VALUE=$WORD
1.137 + else
1.138 + VALUE="$VALUE $WORD"
1.139 + fi
1.140 + done
1.141 +
1.142 + # Show the expanded definition value.
1.143 +
1.144 + echo "$VALUE"
1.145 +
1.146 + # Return a result value of 0 if successful, 1 otherwise.
1.147 +
1.148 + if [ "$VALUE" ] ; then
1.149 + return 0
1.150 + else
1.151 + return 1
1.152 + fi
1.153 +}
1.154 +
1.155 +
1.156 +
1.157 +# Expand the definition and return the result code.
1.158 +
1.159 +expand "$DEF"
1.160 +exit $?