| 1 | #! /bin/sh
 | 
|---|
| 2 | #       $NetBSD: mknodes.sh,v 1.1 2004/01/16 23:24:38 dsl Exp $
 | 
|---|
| 3 | 
 | 
|---|
| 4 | # Copyright (c) 2003 The NetBSD Foundation, Inc.
 | 
|---|
| 5 | # All rights reserved.
 | 
|---|
| 6 | #
 | 
|---|
| 7 | # This code is derived from software contributed to The NetBSD Foundation
 | 
|---|
| 8 | # by David Laight.
 | 
|---|
| 9 | #
 | 
|---|
| 10 | # Redistribution and use in source and binary forms, with or without
 | 
|---|
| 11 | # modification, are permitted provided that the following conditions
 | 
|---|
| 12 | # are met:
 | 
|---|
| 13 | # 1. Redistributions of source code must retain the above copyright
 | 
|---|
| 14 | #    notice, this list of conditions and the following disclaimer.
 | 
|---|
| 15 | # 2. Redistributions in binary form must reproduce the above copyright
 | 
|---|
| 16 | #    notice, this list of conditions and the following disclaimer in the
 | 
|---|
| 17 | #    documentation and/or other materials provided with the distribution.
 | 
|---|
| 18 | # 3. Neither the name of The NetBSD Foundation nor the names of its
 | 
|---|
| 19 | #    contributors may be used to endorse or promote products derived
 | 
|---|
| 20 | #    from this software without specific prior written permission.
 | 
|---|
| 21 | #
 | 
|---|
| 22 | # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
 | 
|---|
| 23 | # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 | 
|---|
| 24 | # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 | 
|---|
| 25 | # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 | 
|---|
| 26 | # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 | 
|---|
| 27 | # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 | 
|---|
| 28 | # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 | 
|---|
| 29 | # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 | 
|---|
| 30 | # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 | 
|---|
| 31 | # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 | 
|---|
| 32 | # POSSIBILITY OF SUCH DAMAGE.
 | 
|---|
| 33 | 
 | 
|---|
| 34 | nodetypes=$1
 | 
|---|
| 35 | nodes_pat=$2
 | 
|---|
| 36 | objdir="$3"
 | 
|---|
| 37 | 
 | 
|---|
| 38 | exec <$nodetypes
 | 
|---|
| 39 | exec >$objdir/nodes.h.tmp
 | 
|---|
| 40 | 
 | 
|---|
| 41 | echo "/*"
 | 
|---|
| 42 | echo " * This file was generated by mknodes.sh"
 | 
|---|
| 43 | echo " */"
 | 
|---|
| 44 | echo
 | 
|---|
| 45 | 
 | 
|---|
| 46 | tagno=0
 | 
|---|
| 47 | while IFS=; read -r line; do
 | 
|---|
| 48 |         line="${line%%#*}"
 | 
|---|
| 49 |         IFS='   '
 | 
|---|
| 50 |         set -- $line
 | 
|---|
| 51 |         IFS=
 | 
|---|
| 52 |         [ -z "$2" ] && continue
 | 
|---|
| 53 |         case "$line" in
 | 
|---|
| 54 |         ["      "]* )
 | 
|---|
| 55 |                 IFS=' '
 | 
|---|
| 56 |                 [ $field = 0 ] && struct_list="$struct_list $struct"
 | 
|---|
| 57 |                 eval field_${struct}_$field=\"\$*\"
 | 
|---|
| 58 |                 eval numfld_$struct=\$field
 | 
|---|
| 59 |                 field=$(($field + 1))
 | 
|---|
| 60 |                 ;;
 | 
|---|
| 61 |         * )
 | 
|---|
| 62 |                 define=$1
 | 
|---|
| 63 |                 struct=$2
 | 
|---|
| 64 |                 echo "#define $define $tagno"
 | 
|---|
| 65 |                 tagno=$(($tagno + 1))
 | 
|---|
| 66 |                 eval define_$struct=\"\$define_$struct \$define\"
 | 
|---|
| 67 |                 struct_define="$struct_define $struct"
 | 
|---|
| 68 |                 field=0
 | 
|---|
| 69 |                 ;;
 | 
|---|
| 70 |         esac
 | 
|---|
| 71 | done
 | 
|---|
| 72 | 
 | 
|---|
| 73 | echo
 | 
|---|
| 74 | 
 | 
|---|
| 75 | IFS=' '
 | 
|---|
| 76 | for struct in $struct_list; do
 | 
|---|
| 77 |         echo
 | 
|---|
| 78 |         echo
 | 
|---|
| 79 |         echo "struct $struct {"
 | 
|---|
| 80 |         field=0
 | 
|---|
| 81 |         while
 | 
|---|
| 82 |                 eval line=\"\$field_${struct}_$field\"
 | 
|---|
| 83 |                 field=$(($field + 1))
 | 
|---|
| 84 |                 [ -n "$line" ]
 | 
|---|
| 85 |         do
 | 
|---|
| 86 |                 IFS=' '
 | 
|---|
| 87 |                 set -- $line
 | 
|---|
| 88 |                 name=$1
 | 
|---|
| 89 |                 case $2 in
 | 
|---|
| 90 |                 nodeptr ) type="union node *";;
 | 
|---|
| 91 |                 nodelist ) type="struct nodelist *";;
 | 
|---|
| 92 |                 string ) type="char *";;
 | 
|---|
| 93 |                 int ) type="int ";;
 | 
|---|
| 94 |                 * ) name=; shift 2; type="$*";;
 | 
|---|
| 95 |                 esac
 | 
|---|
| 96 |                 echo "      $type$name;"
 | 
|---|
| 97 |         done
 | 
|---|
| 98 |         echo "};"
 | 
|---|
| 99 | done
 | 
|---|
| 100 | 
 | 
|---|
| 101 | echo
 | 
|---|
| 102 | echo
 | 
|---|
| 103 | echo "union node {"
 | 
|---|
| 104 | echo "      int type;"
 | 
|---|
| 105 | for struct in $struct_list; do
 | 
|---|
| 106 |         echo "      struct $struct $struct;"
 | 
|---|
| 107 | done
 | 
|---|
| 108 | echo "};"
 | 
|---|
| 109 | echo
 | 
|---|
| 110 | echo
 | 
|---|
| 111 | echo "struct nodelist {"
 | 
|---|
| 112 | echo "  struct nodelist *next;"
 | 
|---|
| 113 | echo "  union node *n;"
 | 
|---|
| 114 | echo "};"
 | 
|---|
| 115 | echo
 | 
|---|
| 116 | echo
 | 
|---|
| 117 | echo "union node *copyfunc(union node *);"
 | 
|---|
| 118 | echo "void freefunc(union node *);"
 | 
|---|
| 119 | 
 | 
|---|
| 120 | exec <$nodes_pat
 | 
|---|
| 121 | exec >$objdir/nodes.c.tmp
 | 
|---|
| 122 | mv -f $objdir/nodes.h.tmp $objdir/nodes.h || exit 1
 | 
|---|
| 123 | 
 | 
|---|
| 124 | echo "/*"
 | 
|---|
| 125 | echo " * This file was generated by mknodes.sh"
 | 
|---|
| 126 | echo " */"
 | 
|---|
| 127 | echo
 | 
|---|
| 128 | 
 | 
|---|
| 129 | while IFS=; read -r line; do
 | 
|---|
| 130 |         IFS='   '
 | 
|---|
| 131 |         set -- $line
 | 
|---|
| 132 |         IFS=
 | 
|---|
| 133 |         case "$1" in
 | 
|---|
| 134 |         '%SIZES' )
 | 
|---|
| 135 |                 echo "static const short nodesize[$tagno] = {"
 | 
|---|
| 136 |                 IFS=' '
 | 
|---|
| 137 |                 for struct in $struct_define; do
 | 
|---|
| 138 |                         echo "      SHELL_ALIGN(sizeof (struct $struct)),"
 | 
|---|
| 139 |                 done
 | 
|---|
| 140 |                 echo "};"
 | 
|---|
| 141 |                 ;;
 | 
|---|
| 142 |         '%CALCSIZE' )
 | 
|---|
| 143 |                 echo "      if (n == NULL)"
 | 
|---|
| 144 |                 echo "      return;"
 | 
|---|
| 145 |                 echo "      funcblocksize += nodesize[n->type];"
 | 
|---|
| 146 |                 echo "      switch (n->type) {"
 | 
|---|
| 147 |                 IFS=' '
 | 
|---|
| 148 |                 for struct in $struct_list; do
 | 
|---|
| 149 |                         eval defines=\"\$define_$struct\"
 | 
|---|
| 150 |                         for define in $defines; do
 | 
|---|
| 151 |                                 echo "      case $define:"
 | 
|---|
| 152 |                         done
 | 
|---|
| 153 |                         eval field=\$numfld_$struct
 | 
|---|
| 154 |                         while
 | 
|---|
| 155 |                                 [ $field != 0 ]
 | 
|---|
| 156 |                         do
 | 
|---|
| 157 |                                 eval line=\"\$field_${struct}_$field\"
 | 
|---|
| 158 |                                 field=$(($field - 1))
 | 
|---|
| 159 |                                 IFS=' '
 | 
|---|
| 160 |                                 set -- $line
 | 
|---|
| 161 |                                 name=$1
 | 
|---|
| 162 |                                 cl=")"
 | 
|---|
| 163 |                                 case $2 in
 | 
|---|
| 164 |                                 nodeptr ) fn=calcsize;;
 | 
|---|
| 165 |                                 nodelist ) fn=sizenodelist;;
 | 
|---|
| 166 |                                 string ) fn="funcstringsize += strlen"
 | 
|---|
| 167 |                                         cl=") + 1";;
 | 
|---|
| 168 |                                 * ) continue;;
 | 
|---|
| 169 |                                 esac
 | 
|---|
| 170 |                                 echo "      ${fn}(n->$struct.$name${cl};"
 | 
|---|
| 171 |                         done
 | 
|---|
| 172 |                         echo "      break;"
 | 
|---|
| 173 |                 done
 | 
|---|
| 174 |                 echo "      };"
 | 
|---|
| 175 |                 ;;
 | 
|---|
| 176 |         '%COPY' )
 | 
|---|
| 177 |                 echo "      if (n == NULL)"
 | 
|---|
| 178 |                 echo "      return NULL;"
 | 
|---|
| 179 |                 echo "      new = funcblock;"
 | 
|---|
| 180 |                 echo "      funcblock = (char *) funcblock + nodesize[n->type];"
 | 
|---|
| 181 |                 echo "      switch (n->type) {"
 | 
|---|
| 182 |                 IFS=' '
 | 
|---|
| 183 |                 for struct in $struct_list; do
 | 
|---|
| 184 |                         eval defines=\"\$define_$struct\"
 | 
|---|
| 185 |                         for define in $defines; do
 | 
|---|
| 186 |                                 echo "      case $define:"
 | 
|---|
| 187 |                         done
 | 
|---|
| 188 |                         eval field=\$numfld_$struct
 | 
|---|
| 189 |                         while
 | 
|---|
| 190 |                                 [ $field != 0 ]
 | 
|---|
| 191 |                         do
 | 
|---|
| 192 |                                 eval line=\"\$field_${struct}_$field\"
 | 
|---|
| 193 |                                 field=$(($field - 1))
 | 
|---|
| 194 |                                 IFS=' '
 | 
|---|
| 195 |                                 set -- $line
 | 
|---|
| 196 |                                 name=$1
 | 
|---|
| 197 |                                 case $2 in
 | 
|---|
| 198 |                                 nodeptr ) fn="copynode(";;
 | 
|---|
| 199 |                                 nodelist ) fn="copynodelist(";;
 | 
|---|
| 200 |                                 string ) fn="nodesavestr(";;
 | 
|---|
| 201 |                                 int ) fn=;;
 | 
|---|
| 202 |                                 * ) continue;;
 | 
|---|
| 203 |                                 esac
 | 
|---|
| 204 |                                 f="$struct.$name"
 | 
|---|
| 205 |                                 echo "      new->$f = ${fn}n->$f${fn:+)};"
 | 
|---|
| 206 |                         done
 | 
|---|
| 207 |                         echo "      break;"
 | 
|---|
| 208 |                 done
 | 
|---|
| 209 |                 echo "      };"
 | 
|---|
| 210 |                 echo "      new->type = n->type;"
 | 
|---|
| 211 |                 ;;
 | 
|---|
| 212 |         * ) echo "$line";;
 | 
|---|
| 213 |         esac
 | 
|---|
| 214 | done
 | 
|---|
| 215 | 
 | 
|---|
| 216 | exec >/dev/null
 | 
|---|
| 217 | mv -f $objdir/nodes.c.tmp $objdir/nodes.c || exit 1
 | 
|---|