1#!/bin/sh
2#
3#	$NetBSD: makerumpif.sh,v 1.4 2009/10/15 00:29:19 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
50[ $# != 1 ] && usage
51
52MYDIR=`pwd`
53while [ ! -f Makefile.rump  ]; do
54	[ `pwd` = '/' ] && boom Could not find rump topdir.
55	cd ..
56done
57RUMPTOP="`pwd`"
58cd ${MYDIR}
59
60sed -e '
61:again
62	/\\$/{
63		N
64		s/[ 	]*\\\n[ 	]*/ /
65		b again
66	}
67' ${1} | awk -F\| -v rumptop=${RUMPTOP} '
68function fileheaders(file, srcstr)
69{
70	printf("/*\t$NetBSD: makerumpif.sh,v 1.4 2009/10/15 00:29:19 pooka Exp $\t*/\n\n") > file
71	printf("/*\n * Automatically generated.  DO NOT EDIT.\n") > file
72	genstr = "$NetBSD: makerumpif.sh,v 1.4 2009/10/15 00:29:19 pooka Exp $"
73	gsub("\\$", "", genstr)
74	printf(" * from: %s\n", srcstr) > file
75	printf(" * by:   %s\n", genstr) > file
76	printf(" */\n") > file
77}
78
79function die(str)
80{
81
82	print str
83	exit(1)
84}
85
86NR == 1 {
87	sub(";[^\\$]*\\$", "")
88	sub("\\$", "")
89	fromstr = $0
90	next
91}
92
93$1 == "NAME"{myname = $2;next}
94$1 == "PUBHDR"{pubhdr = rumptop "/" $2;print pubhdr;next}
95$1 == "PRIVHDR"{privhdr = rumptop "/" $2;print privhdr;next}
96$1 == "WRAPPERS"{gencalls = rumptop "/" $2;print gencalls;next}
97
98/^;/{next}
99/\\$/{sub("\\\n", "");getline nextline;$0 = $0 nextline}
100/^[ \t]*$/{next}
101{
102	if (NF != 3 && NF != 4) {
103		die("error: unexpected number of fields\n")
104	}
105	if (NF == 4) {
106		if ($4 == "WEAK")
107			isweak = 1
108		else
109			die("error: unexpected fourth field");
110	} else {
111		isweak = 0
112	}
113	if (!myname)
114		die("name not specified");
115	if (!pubhdr)
116		die("public header not specified");
117	if (!privhdr)
118		die("private header not specified");
119	if (!gencalls)
120		die("wrapper file not specified");
121
122	if (!once) {
123		fileheaders(pubhdr, fromstr)
124		fileheaders(privhdr, fromstr)
125		fileheaders(gencalls, fromstr)
126		once = 1
127
128		pubfile = pubhdr
129		sub(".*/", "", pubfile)
130
131		privfile = privhdr
132		sub(".*/", "", privfile)
133
134		printf("\n") > pubhdr
135		printf("\n") > privhdr
136
137		printf("\n#include <sys/cdefs.h>\n") > gencalls
138		printf("#include <sys/systm.h>\n") > gencalls
139		printf("\n#include <rump/rump.h>\n") > gencalls
140		printf("#include <rump/%s>\n\n", pubfile) > gencalls
141		printf("#include \"rump_private.h\"\n", privfile) > gencalls
142		printf("#include \"%s\"\n\n", privfile) > gencalls
143		printf("void __dead rump_%s_unavailable(void);\n",	\
144		    myname) > gencalls
145		printf("void __dead\nrump_%s_unavailable(void)\n{\n",	\
146		    myname) > gencalls
147		printf("\n\tpanic(\"%s interface unavailable\");\n}\n",	\
148		    myname) > gencalls
149	}
150
151	funtype = $1
152	sub("[ \t]*$", "", funtype)
153	funname = $2
154	sub("[ \t]*$", "", funname)
155	funargs = $3
156	sub("[ \t]*$", "", funargs)
157
158	printf("%s rump_pub_%s(%s);\n", funtype, funname, funargs) > pubhdr
159	printf("%s rump_%s(%s);\n", funtype, funname, funargs) > privhdr
160
161	if (funtype == "void")
162		voidret = 1
163	else
164		voidret = 0
165	if (funargs == "void")
166		voidarg = 1
167	else
168		voidarg = 0
169
170	printf("\n%s\nrump_pub_%s(", funtype, funname) > gencalls
171	if (!voidarg) {
172		narg = split(funargs, argv, ",")
173		for (i = 1; i <= narg; i++) {
174			sub(" *", "", argv[i])
175			if (match(argv[i], "\\*$") != 0)
176				printf("%sarg%d", argv[i], i) > gencalls
177			else
178				printf("%s arg%d", argv[i], i) > gencalls
179			if (i != narg)
180				printf(", ") > gencalls
181		}
182	} else {
183		narg = 0
184		printf("void") > gencalls
185	}
186	printf(")\n{\n") > gencalls
187
188	if (!voidret) {
189		printf("\t%s rv;\n", funtype) > gencalls
190	}
191	printf("\n\trump_schedule();\n\t") > gencalls
192	if (!voidret)
193		printf("rv = ") > gencalls
194	printf("rump_%s(", funname) > gencalls
195	for (i = 1; i <= narg; i++) {
196		printf("arg%i", i) > gencalls
197		if (i < narg)
198			printf(", ") > gencalls
199	}
200	printf(");\n\trump_unschedule();\n") > gencalls
201	if (!voidret)
202		printf("\n\treturn rv;\n") > gencalls
203	printf("}\n") > gencalls
204	if (isweak)
205		printf("__weak_alias(rump_%s,rump_%s_unavailable);\n", \
206		    funname, myname) > gencalls
207}'
208