1 #!/bin/sh 2 3 # Search for a definition in the definitions file, expanding it to the recorded 4 # value, recursively expanding any definition references. 5 6 PROGNAME=`basename "$0"` 7 THISDIR=`dirname "$0"` 8 9 DEFS="$THISDIR/makesd-defs" 10 11 12 13 # Test for options. 14 15 if [ "$1" = '--original' ] ; then 16 FLAT=$1 17 shift 1 18 else 19 FLAT= 20 fi 21 22 # Obtain the requested definition name. 23 24 DEF=$1 25 26 if [ ! "$DEF" ] || [ "$DEF" = '--help' ] ; then 27 cat 1>&2 <<EOF 28 Usage: $PROGNAME <definition name> 29 30 Search for a definition of the given name in the definitions file: 31 32 $DEFS 33 34 If the definition can be found, the value of the definition is emitted and an 35 exit value of 0 returned. Otherwise, no output is produced and an exit value of 36 1 is returned. 37 EOF 38 exit 1 39 fi 40 41 42 43 # lookup <definition name> 44 # 45 # Search for a definition of the given name in the definitions file. Emit the 46 # full definition, incorporating multiple lines if continuation characters are 47 # present. 48 49 lookup() 50 { 51 local LINENUM 52 53 # Obtain the line number of the matching definition. 54 55 LINENUM=`grep -h -n "^$1\s" "$DEFS" | cut -d: -f1` 56 57 if [ "$LINENUM" ] ; then 58 59 # Read from the definition line. Line continuations are observed. 60 61 tail -n "+$LINENUM" "$DEFS" | if read LINE ; then 62 echo "$LINE" 63 break 64 fi 65 fi 66 } 67 68 # match <string> <pattern> 69 # 70 # Attempt to match pattern in string, emitting the string if successful. 71 72 match() 73 { 74 if `echo "$1" | grep -q "$2"` ; then 75 echo "$1" 76 return 0 77 else 78 return 1 79 fi 80 } 81 82 # defname <prefixed name> 83 # 84 # Return the actual definition name from the given prefixed name. 85 86 defname() 87 { 88 echo "$1" | sed 's/^\$//' 89 } 90 91 # expand <definition name> 92 # 93 # Expand the given definition name to its value, recursively expanding any 94 # definition names found in the value text. 95 96 expand() 97 { 98 local FOUND VALUE 99 100 FOUND= 101 VALUE= 102 103 for WORD in `lookup "$1"`; do 104 if [ ! "$FOUND" ] ; then 105 FOUND=$WORD 106 continue 107 fi 108 109 # Prevent recursive expansion if flat mode is selected. 110 111 if [ ! "$FLAT" ] ; then 112 113 # Identify definition names by looking for a $ prefix. 114 115 DEFNAME=`match "$WORD" '^\\$'` 116 117 # Attempt to expand definition names. 118 119 if [ "$DEFNAME" ] ; then 120 DEFNAME=`defname "$DEFNAME"` 121 EXPANDED=`expand "$DEFNAME"` 122 123 if [ "$EXPANDED" ] ; then 124 WORD=$EXPANDED 125 fi 126 fi 127 fi 128 129 # Incorporate expansions into the final value. 130 131 if [ ! "$VALUE" ] ; then 132 VALUE=$WORD 133 else 134 VALUE="$VALUE $WORD" 135 fi 136 done 137 138 # Show the expanded definition value. 139 140 echo "$VALUE" 141 142 # Return a result value of 0 if successful, 1 otherwise. 143 144 if [ "$VALUE" ] ; then 145 return 0 146 else 147 return 1 148 fi 149 } 150 151 152 153 # Expand the definition and return the result code. 154 155 expand "$DEF" 156 exit $?