paul@0 | 1 | #!/bin/sh |
paul@0 | 2 | |
paul@0 | 3 | # Search for a definition in the definitions file, expanding it to the recorded |
paul@0 | 4 | # value, recursively expanding any definition references. |
paul@15 | 5 | # |
paul@15 | 6 | # Copyright (C) 2019 Paul Boddie <paul@boddie.org.uk> |
paul@15 | 7 | # |
paul@15 | 8 | # This program is free software; you can redistribute it and/or modify it under |
paul@15 | 9 | # the terms of the GNU General Public License as published by the Free Software |
paul@15 | 10 | # Foundation; either version 3 of the License, or (at your option) any later |
paul@15 | 11 | # version. |
paul@15 | 12 | # |
paul@15 | 13 | # This program is distributed in the hope that it will be useful, but WITHOUT |
paul@15 | 14 | # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS |
paul@15 | 15 | # FOR A PARTICULAR PURPOSE. See the GNU General Public License for more |
paul@15 | 16 | # details. |
paul@15 | 17 | # |
paul@15 | 18 | # You should have received a copy of the GNU General Public License along with |
paul@15 | 19 | # this program. If not, see <http://www.gnu.org/licenses/>. |
paul@15 | 20 | |
paul@0 | 21 | |
paul@0 | 22 | PROGNAME=`basename "$0"` |
paul@0 | 23 | THISDIR=`dirname "$0"` |
paul@0 | 24 | |
paul@14 | 25 | DEFS="$THISDIR/makesd-defs" |
paul@0 | 26 | |
paul@0 | 27 | |
paul@0 | 28 | |
paul@0 | 29 | # Test for options. |
paul@0 | 30 | |
paul@0 | 31 | if [ "$1" = '--original' ] ; then |
paul@0 | 32 | FLAT=$1 |
paul@0 | 33 | shift 1 |
paul@0 | 34 | else |
paul@0 | 35 | FLAT= |
paul@0 | 36 | fi |
paul@0 | 37 | |
paul@0 | 38 | # Obtain the requested definition name. |
paul@0 | 39 | |
paul@0 | 40 | DEF=$1 |
paul@0 | 41 | |
paul@0 | 42 | if [ ! "$DEF" ] || [ "$DEF" = '--help' ] ; then |
paul@0 | 43 | cat 1>&2 <<EOF |
paul@0 | 44 | Usage: $PROGNAME <definition name> |
paul@0 | 45 | |
paul@0 | 46 | Search for a definition of the given name in the definitions file: |
paul@0 | 47 | |
paul@14 | 48 | $DEFS |
paul@0 | 49 | |
paul@0 | 50 | If the definition can be found, the value of the definition is emitted and an |
paul@0 | 51 | exit value of 0 returned. Otherwise, no output is produced and an exit value of |
paul@0 | 52 | 1 is returned. |
paul@0 | 53 | EOF |
paul@0 | 54 | exit 1 |
paul@0 | 55 | fi |
paul@0 | 56 | |
paul@0 | 57 | |
paul@0 | 58 | |
paul@0 | 59 | # lookup <definition name> |
paul@0 | 60 | # |
paul@0 | 61 | # Search for a definition of the given name in the definitions file. Emit the |
paul@0 | 62 | # full definition, incorporating multiple lines if continuation characters are |
paul@0 | 63 | # present. |
paul@0 | 64 | |
paul@0 | 65 | lookup() |
paul@0 | 66 | { |
paul@0 | 67 | local LINENUM |
paul@0 | 68 | |
paul@0 | 69 | # Obtain the line number of the matching definition. |
paul@0 | 70 | |
paul@14 | 71 | LINENUM=`grep -h -n "^$1\s" "$DEFS" | cut -d: -f1` |
paul@0 | 72 | |
paul@0 | 73 | if [ "$LINENUM" ] ; then |
paul@0 | 74 | |
paul@0 | 75 | # Read from the definition line. Line continuations are observed. |
paul@0 | 76 | |
paul@14 | 77 | tail -n "+$LINENUM" "$DEFS" | if read LINE ; then |
paul@0 | 78 | echo "$LINE" |
paul@0 | 79 | break |
paul@0 | 80 | fi |
paul@0 | 81 | fi |
paul@0 | 82 | } |
paul@0 | 83 | |
paul@0 | 84 | # match <string> <pattern> |
paul@0 | 85 | # |
paul@0 | 86 | # Attempt to match pattern in string, emitting the string if successful. |
paul@0 | 87 | |
paul@0 | 88 | match() |
paul@0 | 89 | { |
paul@0 | 90 | if `echo "$1" | grep -q "$2"` ; then |
paul@0 | 91 | echo "$1" |
paul@0 | 92 | return 0 |
paul@0 | 93 | else |
paul@0 | 94 | return 1 |
paul@0 | 95 | fi |
paul@0 | 96 | } |
paul@0 | 97 | |
paul@0 | 98 | # defname <prefixed name> |
paul@0 | 99 | # |
paul@0 | 100 | # Return the actual definition name from the given prefixed name. |
paul@0 | 101 | |
paul@0 | 102 | defname() |
paul@0 | 103 | { |
paul@0 | 104 | echo "$1" | sed 's/^\$//' |
paul@0 | 105 | } |
paul@0 | 106 | |
paul@0 | 107 | # expand <definition name> |
paul@0 | 108 | # |
paul@0 | 109 | # Expand the given definition name to its value, recursively expanding any |
paul@0 | 110 | # definition names found in the value text. |
paul@0 | 111 | |
paul@0 | 112 | expand() |
paul@0 | 113 | { |
paul@0 | 114 | local FOUND VALUE |
paul@0 | 115 | |
paul@0 | 116 | FOUND= |
paul@0 | 117 | VALUE= |
paul@0 | 118 | |
paul@0 | 119 | for WORD in `lookup "$1"`; do |
paul@0 | 120 | if [ ! "$FOUND" ] ; then |
paul@0 | 121 | FOUND=$WORD |
paul@0 | 122 | continue |
paul@0 | 123 | fi |
paul@0 | 124 | |
paul@0 | 125 | # Prevent recursive expansion if flat mode is selected. |
paul@0 | 126 | |
paul@0 | 127 | if [ ! "$FLAT" ] ; then |
paul@0 | 128 | |
paul@0 | 129 | # Identify definition names by looking for a $ prefix. |
paul@0 | 130 | |
paul@0 | 131 | DEFNAME=`match "$WORD" '^\\$'` |
paul@0 | 132 | |
paul@0 | 133 | # Attempt to expand definition names. |
paul@0 | 134 | |
paul@0 | 135 | if [ "$DEFNAME" ] ; then |
paul@0 | 136 | DEFNAME=`defname "$DEFNAME"` |
paul@0 | 137 | EXPANDED=`expand "$DEFNAME"` |
paul@0 | 138 | |
paul@0 | 139 | if [ "$EXPANDED" ] ; then |
paul@0 | 140 | WORD=$EXPANDED |
paul@0 | 141 | fi |
paul@0 | 142 | fi |
paul@0 | 143 | fi |
paul@0 | 144 | |
paul@0 | 145 | # Incorporate expansions into the final value. |
paul@0 | 146 | |
paul@0 | 147 | if [ ! "$VALUE" ] ; then |
paul@0 | 148 | VALUE=$WORD |
paul@0 | 149 | else |
paul@0 | 150 | VALUE="$VALUE $WORD" |
paul@0 | 151 | fi |
paul@0 | 152 | done |
paul@0 | 153 | |
paul@0 | 154 | # Show the expanded definition value. |
paul@0 | 155 | |
paul@0 | 156 | echo "$VALUE" |
paul@0 | 157 | |
paul@0 | 158 | # Return a result value of 0 if successful, 1 otherwise. |
paul@0 | 159 | |
paul@0 | 160 | if [ "$VALUE" ] ; then |
paul@0 | 161 | return 0 |
paul@0 | 162 | else |
paul@0 | 163 | return 1 |
paul@0 | 164 | fi |
paul@0 | 165 | } |
paul@0 | 166 | |
paul@0 | 167 | |
paul@0 | 168 | |
paul@0 | 169 | # Expand the definition and return the result code. |
paul@0 | 170 | |
paul@0 | 171 | expand "$DEF" |
paul@0 | 172 | exit $? |