xref: /minix3/bin/sh/mknodes.sh (revision d90bee97498b3043241050f61aed100786c59df4)
1#! /bin/sh
2#	$NetBSD: mknodes.sh,v 1.2 2008/04/29 06:53:00 martin 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
31nodetypes=$1
32nodes_pat=$2
33objdir="$3"
34
35exec <$nodetypes
36exec >$objdir/nodes.h.tmp
37
38echo "/*"
39echo " * This file was generated by mknodes.sh"
40echo " */"
41echo
42
43tagno=0
44while 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
68done
69
70echo
71
72IFS=' '
73for 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 "};"
96done
97
98echo
99echo
100echo "union node {"
101echo "      int type;"
102for struct in $struct_list; do
103	echo "      struct $struct $struct;"
104done
105echo "};"
106echo
107echo
108echo "struct nodelist {"
109echo "	struct nodelist *next;"
110echo "	union node *n;"
111echo "};"
112echo
113echo
114echo "union node *copyfunc(union node *);"
115echo "void freefunc(union node *);"
116
117mv $objdir/nodes.h.tmp $objdir/nodes.h || exit 1
118
119exec <$nodes_pat
120exec >$objdir/nodes.c.tmp
121
122echo "/*"
123echo " * This file was generated by mknodes.sh"
124echo " */"
125echo
126
127while 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
212done
213
214mv $objdir/nodes.c.tmp $objdir/nodes.c || exit 1
215