remakesd

makesd-expand-def

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