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