xref: /netbsd-src/sys/rump/librump/makerumpif.sh (revision b7b7574d3bf8eeb51a1fa3977b59142ec6434a55)
1#!/bin/sh
2#
3#	$NetBSD: makerumpif.sh,v 1.8 2014/04/25 17:50:01 pooka Exp $
4#
5# Copyright (c) 2009 Antti Kantee.  All rights reserved.
6#
7# Redistribution and use in source and binary forms, with or without
8# modification, are permitted provided that the following conditions
9# are met:
10# 1. Redistributions of source code must retain the above copyright
11#    notice, this list of conditions and the following disclaimer.
12# 2. Redistributions in binary form must reproduce the above copyright
13#    notice, this list of conditions and the following disclaimer in the
14#    documentation and/or other materials provided with the distribution.
15#
16# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26#
27
28#
29# This reads a rump component interface description and creates:
30#  1: rump private prototypes for internal calls
31#  2: public prototypes for calls outside of rump
32#  3: public interface implementations which run the rump scheduler
33#     and call the private interface
34#
35
36usage ()
37{
38
39	echo "usage: $0 spec"
40	exit 1
41}
42
43boom ()
44{
45
46	echo $*
47	exit 1
48}
49
50unset TOPDIR
51if [ $# -eq 3 ]; then
52	if [ $1 = '-R' ]; then
53		TOPDIR=$2
54	else
55		usage
56	fi
57	shift; shift
58fi
59[ $# -ne 1 ] && usage
60
61MYDIR=`pwd`
62if [ -z "${TOPDIR}" ]; then
63	while [ ! -f Makefile.rump  ]; do
64		[ `pwd` = '/' ] && boom Could not find rump topdir.
65		cd ..
66	done
67	TOPDIR="`pwd`"
68fi
69cd ${MYDIR}
70
71sed -e '
72:again
73	/\\$/{
74		N
75		s/[ 	]*\\\n[ 	]*/ /
76		b again
77	}
78' ${1} | awk -F\| -v topdir=${TOPDIR} '
79function fileheaders(file, srcstr)
80{
81	printf("/*\t$NetBSD: makerumpif.sh,v 1.8 2014/04/25 17:50:01 pooka Exp $\t*/\n\n") > file
82	printf("/*\n * Automatically generated.  DO NOT EDIT.\n") > file
83	genstr = "$NetBSD: makerumpif.sh,v 1.8 2014/04/25 17:50:01 pooka Exp $"
84	gsub("\\$", "", genstr)
85	printf(" * from: %s\n", srcstr) > file
86	printf(" * by:   %s\n", genstr) > file
87	printf(" */\n") > file
88}
89
90function die(str)
91{
92
93	print str
94	exit(1)
95}
96
97NR == 1 {
98	sub(";[^\\$]*\\$", "")
99	sub("\\$", "")
100	fromstr = $0
101	next
102}
103
104$1 == "NAME"{myname = $2;next}
105$1 == "PUBHDR"{pubhdr = topdir "/" $2;print pubhdr;next}
106$1 == "PRIVHDR"{privhdr = topdir "/" $2;print privhdr;next}
107$1 == "WRAPPERS"{gencalls = topdir "/" $2;print gencalls;next}
108
109/^;/{next}
110/\\$/{sub("\\\n", "");getline nextline;$0 = $0 nextline}
111/^[ \t]*$/{next}
112{
113	if (NF != 3 && NF != 4) {
114		die("error: unexpected number of fields\n")
115	}
116	if (NF == 4) {
117		if ($4 == "WEAK")
118			isweak = 1
119		else
120			die("error: unexpected fourth field");
121	} else {
122		isweak = 0
123	}
124	if (!myname)
125		die("name not specified");
126	if (!pubhdr)
127		die("public header not specified");
128	if (!privhdr)
129		die("private header not specified");
130	if (!gencalls)
131		die("wrapper file not specified");
132
133	if (!once) {
134		fileheaders(pubhdr, fromstr)
135		fileheaders(privhdr, fromstr)
136		fileheaders(gencalls, fromstr)
137		once = 1
138
139		pubfile = pubhdr
140		sub(".*/", "", pubfile)
141
142		privfile = privhdr
143		sub(".*/", "", privfile)
144
145		printf("\n") > pubhdr
146		printf("\n") > privhdr
147
148		printf("#ifndef _RUMP_PRIF_%s_H_\n", toupper(myname)) > privhdr
149		printf("#define _RUMP_PRIF_%s_H_\n", toupper(myname)) > privhdr
150		printf("\n") > privhdr
151
152		printf("\n#include <sys/cdefs.h>\n") > gencalls
153		printf("#include <sys/systm.h>\n") > gencalls
154		printf("\n#include <rump/rump.h>\n") > gencalls
155		printf("#include <rump/%s>\n\n", pubfile) > gencalls
156		printf("#include \"rump_private.h\"\n", privfile) > gencalls
157		printf("#include \"%s\"\n\n", privfile) > gencalls
158		printf("void __dead rump_%s_unavailable(void);\n",	\
159		    myname) > gencalls
160		printf("void __dead\nrump_%s_unavailable(void)\n{\n",	\
161		    myname) > gencalls
162		printf("\n\tpanic(\"%s interface unavailable\");\n}\n",	\
163		    myname) > gencalls
164	}
165
166	funtype = $1
167	sub("[ \t]*$", "", funtype)
168	funname = $2
169	sub("[ \t]*$", "", funname)
170	funargs = $3
171	sub("[ \t]*$", "", funargs)
172
173	printf("%s rump_pub_%s(%s);\n", funtype, funname, funargs) > pubhdr
174	printf("%s rump_%s(%s);\n", funtype, funname, funargs) > privhdr
175	printf("typedef %s (*rump_%s_fn)(%s);\n",	\
176	    funtype, funname, funargs) > privhdr
177
178	if (funtype == "void")
179		voidret = 1
180	else
181		voidret = 0
182	if (funargs == "void")
183		voidarg = 1
184	else
185		voidarg = 0
186
187	printf("\n%s\nrump_pub_%s(", funtype, funname) > gencalls
188	if (!voidarg) {
189		narg = split(funargs, argv, ",")
190		for (i = 1; i <= narg; i++) {
191			sub(" *", "", argv[i])
192			if (match(argv[i], "\\*$") != 0)
193				printf("%sarg%d", argv[i], i) > gencalls
194			else
195				printf("%s arg%d", argv[i], i) > gencalls
196			if (i != narg)
197				printf(", ") > gencalls
198		}
199	} else {
200		narg = 0
201		printf("void") > gencalls
202	}
203	printf(")\n{\n") > gencalls
204
205	if (!voidret) {
206		printf("\t%s rv;\n", funtype) > gencalls
207	}
208	printf("\n\trump_schedule();\n\t") > gencalls
209	if (!voidret)
210		printf("rv = ") > gencalls
211	printf("rump_%s(", funname) > gencalls
212	for (i = 1; i <= narg; i++) {
213		printf("arg%i", i) > gencalls
214		if (i < narg)
215			printf(", ") > gencalls
216	}
217	printf(");\n\trump_unschedule();\n") > gencalls
218	if (!voidret)
219		printf("\n\treturn rv;\n") > gencalls
220	printf("}\n") > gencalls
221	if (isweak)
222		printf("__weak_alias(rump_%s,rump_%s_unavailable);\n", \
223		    funname, myname) > gencalls
224}
225
226END { printf("\n#endif /* _RUMP_PRIF_%s_H_ */\n", toupper(myname)) > privhdr }'
227