makerumpif.sh revision 1.1 1 #!/bin/sh
2 #
3 # $NetBSD: makerumpif.sh,v 1.1 2009/10/14 17:17:00 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
36 usage ()
37 {
38
39 echo "usage: $0 spec"
40 exit 1
41 }
42
43 boom ()
44 {
45
46 echo $*
47 exit 1
48 }
49
50 [ $# != 1 ] && usage
51
52 MYDIR=`pwd`
53 while [ ! -f Makefile.rump ]; do
54 [ `pwd` = '/' ] && boom Could not find rump topdir.
55 cd ..
56 done
57 RUMPTOP="`pwd`"
58 cd ${MYDIR}
59
60 sed -e '
61 :again
62 /\\$/{
63 N
64 s/[ ]*\\\n[ ]*/ /
65 b again
66 }
67 ' ${1} | awk -F\| -v rumptop=${RUMPTOP} '
68 function fileheaders(file, srcstr)
69 {
70 printf("/*\t$NetBSD: makerumpif.sh,v 1.1 2009/10/14 17:17:00 pooka Exp $\t*/\n\n") > file
71 printf("/*\n * Automatically generated. DO NOT EDIT.\n") > file
72 genstr = "$NetBSD: makerumpif.sh,v 1.1 2009/10/14 17:17:00 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
79 function die(str)
80 {
81
82 print str
83 exit(1)
84 }
85
86 NR == 1 {
87 sub(";[^\\$]*\\$", "")
88 sub("\\$", "")
89 fromstr = $0
90 next
91 }
92
93 $1 == "NAME"{myname = $2;next}
94 $1 == "PUBHDR"{pubhdr = rumptop "/" $2;next}
95 $1 == "PRIVHDR"{privhdr = rumptop "/" $2;next}
96 $1 == "WRAPPERS"{gencalls = rumptop "/" $2;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 \"%s\"\n\n", privfile) > gencalls
142 printf("void __dead\nrump_%s_unavailable(void);\n", \
143 myname) > gencalls
144 printf("void __dead\nrump_%s_unavailable(void)\n{\n", \
145 myname) > gencalls
146 printf("\n\tpanic(\"%s interface unavailable\");\n}\n", \
147 myname) > gencalls
148 }
149
150 funtype = $1
151 sub("[ \t]*$", "", funtype)
152 funname = $2
153 sub("[ \t]*$", "", funname)
154 funargs = $3
155 printf("%s rump_%s(%s);\n", funtype, funname, funargs) > pubhdr
156 printf("%s rumppriv_%s(%s);\n", funtype, funname, funargs) > privhdr
157
158 if (funtype == "void")
159 voidret = 1
160 else
161 voidret = 0
162 if (funargs == "void")
163 voidarg = 1
164 else
165 voidarg = 0
166
167 printf("\n%s\nrump_%s(", funtype, funname) > gencalls
168 if (!voidarg) {
169 narg = split(funargs, argv, ",")
170 for (i = 1; i <= narg; i++) {
171 sub(" *", "", argv[i])
172 if (match(argv[i], "\\*$") != 0)
173 printf("%sarg%d", argv[i], i) > gencalls
174 else
175 printf("%s arg%d", argv[i], i) > gencalls
176 if (i != narg)
177 printf(", ") > gencalls
178 }
179 } else {
180 narg = 0
181 printf("void") > gencalls
182 }
183 printf(")\n{\n") > gencalls
184
185 if (!voidret) {
186 printf("\t%s rv;\n", $1) > gencalls
187 }
188 printf("\n\t") > gencalls
189 if (!voidret)
190 printf("rv = ") > gencalls
191 printf("rumppriv_%s(", funname) > gencalls
192 for (i = 1; i <= narg; i++) {
193 printf("arg%i", i) > gencalls
194 if (i < narg)
195 printf(", ") > gencalls
196 }
197 printf(");\n") > gencalls
198 if (!voidret)
199 printf("\n\treturn rv;\n") > gencalls
200 printf("}\n") > gencalls
201 if (isweak)
202 printf("__weak_alias(rumppriv_%s,rump_%s_unavailable);\n", \
203 funname, myname) > gencalls
204 }'
205