Home | History | Annotate | Line # | Download | only in sh
mknodes.sh revision 1.1.30.1
      1 #! /bin/sh
      2 #	$NetBSD: mknodes.sh,v 1.1.30.1 2008/05/18 12:28:44 yamt 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 #
     19 # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     20 # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     21 # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     22 # PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     23 # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     24 # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     25 # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     26 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     27 # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     28 # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     29 # POSSIBILITY OF SUCH DAMAGE.
     30 
     31 nodetypes=$1
     32 nodes_pat=$2
     33 objdir="$3"
     34 
     35 exec <$nodetypes
     36 exec >$objdir/nodes.h.tmp
     37 
     38 echo "/*"
     39 echo " * This file was generated by mknodes.sh"
     40 echo " */"
     41 echo
     42 
     43 tagno=0
     44 while IFS=; read -r line; do
     45 	line="${line%%#*}"
     46 	IFS=' 	'
     47 	set -- $line
     48 	IFS=
     49 	[ -z "$2" ] && continue
     50 	case "$line" in
     51 	[" 	"]* )
     52 		IFS=' '
     53 		[ $field = 0 ] && struct_list="$struct_list $struct"
     54 		eval field_${struct}_$field=\"\$*\"
     55 		eval numfld_$struct=\$field
     56 		field=$(($field + 1))
     57 		;;
     58 	* )
     59 		define=$1
     60 		struct=$2
     61 		echo "#define $define $tagno"
     62 		tagno=$(($tagno + 1))
     63 		eval define_$struct=\"\$define_$struct \$define\"
     64 		struct_define="$struct_define $struct"
     65 		field=0
     66 		;;
     67 	esac
     68 done
     69 
     70 echo
     71 
     72 IFS=' '
     73 for struct in $struct_list; do
     74 	echo
     75 	echo
     76 	echo "struct $struct {"
     77 	field=0
     78 	while
     79 		eval line=\"\$field_${struct}_$field\"
     80 		field=$(($field + 1))
     81 		[ -n "$line" ]
     82 	do
     83 		IFS=' '
     84 		set -- $line
     85 		name=$1
     86 		case $2 in
     87 		nodeptr ) type="union node *";;
     88 		nodelist ) type="struct nodelist *";;
     89 		string ) type="char *";;
     90 		int ) type="int ";;
     91 		* ) name=; shift 2; type="$*";;
     92 		esac
     93 		echo "      $type$name;"
     94 	done
     95 	echo "};"
     96 done
     97 
     98 echo
     99 echo
    100 echo "union node {"
    101 echo "      int type;"
    102 for struct in $struct_list; do
    103 	echo "      struct $struct $struct;"
    104 done
    105 echo "};"
    106 echo
    107 echo
    108 echo "struct nodelist {"
    109 echo "	struct nodelist *next;"
    110 echo "	union node *n;"
    111 echo "};"
    112 echo
    113 echo
    114 echo "union node *copyfunc(union node *);"
    115 echo "void freefunc(union node *);"
    116 
    117 mv $objdir/nodes.h.tmp $objdir/nodes.h || exit 1
    118 
    119 exec <$nodes_pat
    120 exec >$objdir/nodes.c.tmp
    121 
    122 echo "/*"
    123 echo " * This file was generated by mknodes.sh"
    124 echo " */"
    125 echo
    126 
    127 while IFS=; read -r line; do
    128 	IFS=' 	'
    129 	set -- $line
    130 	IFS=
    131 	case "$1" in
    132 	'%SIZES' )
    133 		echo "static const short nodesize[$tagno] = {"
    134 		IFS=' '
    135 		for struct in $struct_define; do
    136 			echo "      SHELL_ALIGN(sizeof (struct $struct)),"
    137 		done
    138 		echo "};"
    139 		;;
    140 	'%CALCSIZE' )
    141 		echo "      if (n == NULL)"
    142 		echo "	    return;"
    143 		echo "      funcblocksize += nodesize[n->type];"
    144 		echo "      switch (n->type) {"
    145 		IFS=' '
    146 		for struct in $struct_list; do
    147 			eval defines=\"\$define_$struct\"
    148 			for define in $defines; do
    149 				echo "      case $define:"
    150 			done
    151 			eval field=\$numfld_$struct
    152 			while
    153 				[ $field != 0 ]
    154 			do
    155 				eval line=\"\$field_${struct}_$field\"
    156 				field=$(($field - 1))
    157 				IFS=' '
    158 				set -- $line
    159 				name=$1
    160 				cl=")"
    161 				case $2 in
    162 				nodeptr ) fn=calcsize;;
    163 				nodelist ) fn=sizenodelist;;
    164 				string ) fn="funcstringsize += strlen"
    165 					cl=") + 1";;
    166 				* ) continue;;
    167 				esac
    168 				echo "	    ${fn}(n->$struct.$name${cl};"
    169 			done
    170 			echo "	    break;"
    171 		done
    172 		echo "      };"
    173 		;;
    174 	'%COPY' )
    175 		echo "      if (n == NULL)"
    176 		echo "	    return NULL;"
    177 		echo "      new = funcblock;"
    178 		echo "      funcblock = (char *) funcblock + nodesize[n->type];"
    179 		echo "      switch (n->type) {"
    180 		IFS=' '
    181 		for struct in $struct_list; do
    182 			eval defines=\"\$define_$struct\"
    183 			for define in $defines; do
    184 				echo "      case $define:"
    185 			done
    186 			eval field=\$numfld_$struct
    187 			while
    188 				[ $field != 0 ]
    189 			do
    190 				eval line=\"\$field_${struct}_$field\"
    191 				field=$(($field - 1))
    192 				IFS=' '
    193 				set -- $line
    194 				name=$1
    195 				case $2 in
    196 				nodeptr ) fn="copynode(";;
    197 				nodelist ) fn="copynodelist(";;
    198 				string ) fn="nodesavestr(";;
    199 				int ) fn=;;
    200 				* ) continue;;
    201 				esac
    202 				f="$struct.$name"
    203 				echo "	    new->$f = ${fn}n->$f${fn:+)};"
    204 			done
    205 			echo "	    break;"
    206 		done
    207 		echo "      };"
    208 		echo "      new->type = n->type;"
    209 		;;
    210 	* ) echo "$line";;
    211 	esac
    212 done
    213 
    214 mv $objdir/nodes.c.tmp $objdir/nodes.c || exit 1
    215