makesyscalls.sh revision 1.17 1 #! /bin/sh -
2 # $NetBSD: makesyscalls.sh,v 1.17 1995/10/07 06:28:31 mycroft Exp $
3 #
4 # Copyright (c) 1994 Christopher G. Demetriou
5 # 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 # 3. All advertising materials mentioning features or use of this software
16 # must display the following acknowledgement:
17 # This product includes software developed for the NetBSD Project
18 # by Christopher G. Demetriou.
19 # 4. The name of the author may not be used to endorse or promote products
20 # derived from this software without specific prior written permission
21 #
22 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
23 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
24 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
25 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
26 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
27 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
31 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32
33 # @(#)makesyscalls.sh 8.1 (Berkeley) 6/10/93
34
35 set -e
36
37 case $# in
38 2) ;;
39 *) echo "Usage: $0 config-file input-file" 1>&2
40 exit 1
41 ;;
42 esac
43
44 # source the config file.
45 . $1
46
47 # the config file sets the following variables:
48 # sysnames the syscall names file
49 # sysnumhdr the syscall numbers file
50 # syssw the syscall switch file
51 # sysarghdr the syscall argument struct definitions
52 # compatopts those syscall types that are for 'compat' syscalls
53 # switchname the name for the 'struct sysent' we define
54 # namesname the name for the 'char *[]' we define
55 # constprefix the prefix for the system call constants
56 #
57 # NOTE THAT THIS makesyscalls.sh DOES NOT SUPPORT 'LIBCOMPAT'.
58
59 # tmp files:
60 sysdcl="sysent.dcl"
61 sysprotos="sys.protos"
62 syscompat_pref="sysent."
63 sysent="sysent.switch"
64
65 trap "rm $sysdcl $sysprotos $sysent" 0
66
67 # Awk program (must support nawk extensions)
68 # Use "awk" at Berkeley, "nawk" or "gawk" elsewhere.
69 awk=${AWK:-awk}
70
71 # Does this awk have a "toupper" function? (i.e. is it GNU awk)
72 isgawk=`$awk 'BEGIN { print toupper("true"); exit; }' 2>/dev/null`
73
74 # If this awk does not define "toupper" then define our own.
75 if [ "$isgawk" = TRUE ] ; then
76 # GNU awk provides it.
77 toupper=
78 else
79 # Provide our own toupper()
80 toupper='
81 function toupper(str) {
82 _toupper_cmd = "echo "str" |tr a-z A-Z"
83 _toupper_cmd | getline _toupper_str;
84 close(_toupper_cmd);
85 return _toupper_str;
86 }'
87 fi
88
89 # before handing it off to awk, make a few adjustments:
90 # (1) insert spaces around {, }, (, ), *, and commas.
91 # (2) get rid of any and all dollar signs (so that rcs id use safe)
92 #
93 # The awk script will deal with blank lines and lines that
94 # start with the comment character (';').
95
96 sed -e '
97 s/\$//g
98 :join
99 /\\$/{a\
100
101 N
102 s/\\\n//
103 b join
104 }
105 2,${
106 /^#/!s/\([{}()*,]\)/ \1 /g
107 }
108 ' < $2 | $awk "
109 $toupper
110 BEGIN {
111 sysnames = \"$sysnames\"
112 sysprotos = \"$sysprotos\"
113 sysnumhdr = \"$sysnumhdr\"
114 sysarghdr = \"$sysarghdr\"
115 switchname = \"$switchname\"
116 namesname = \"$namesname\"
117 constprefix = \"$constprefix\"
118
119 sysdcl = \"$sysdcl\"
120 syscompat_pref = \"$syscompat_pref\"
121 sysent = \"$sysent\"
122 infile = \"$2\"
123
124 compatopts = \"$compatopts\"
125 "'
126
127 printf "/*\n * System call switch table.\n *\n" > sysdcl
128 printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysdcl
129
130 ncompat = split(compatopts,compat)
131 for (i = 1; i <= ncompat; i++) {
132 compat_upper[i] = toupper(compat[i])
133
134 printf "\n#ifdef %s\n", compat_upper[i] > sysent
135 printf "#define %s(func) __CONCAT(%s_,func)\n", compat[i], \
136 compat[i] > sysent
137 printf "#else\n" > sysent
138 printf "#define %s(func) sys_nosys\n", compat[i] > sysent
139 printf "#endif\n" > sysent
140 }
141
142 printf "\n#define\ts(type)\tsizeof(type)\n\n" > sysent
143 printf "struct sysent %s[] = {\n",switchname > sysent
144
145 printf "/*\n * System call names.\n *\n" > sysnames
146 printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysnames
147
148 printf "\n/*\n * System call prototypes.\n */\n\n" > sysprotos
149
150 printf "/*\n * System call numbers.\n *\n" > sysnumhdr
151 printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysnumhdr
152
153 printf "/*\n * System call argument lists.\n *\n" > sysarghdr
154 printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysarghdr
155 }
156 NR == 1 {
157 printf " * created from%s\n */\n\n", $0 > sysdcl
158
159 printf " * created from%s\n */\n\n", $0 > sysnames
160 printf "char *%s[] = {\n",namesname > sysnames
161
162 printf " * created from%s\n */\n\n", $0 > sysnumhdr
163
164 printf " * created from%s\n */\n\n", $0 > sysarghdr
165 printf "#define\tsyscallarg(x)\tunion { x datum; register_t pad; }\n" \
166 > sysarghdr
167 next
168 }
169 NF == 0 || $1 ~ /^;/ {
170 next
171 }
172 $1 ~ /^#[ ]*include/ {
173 print > sysdcl
174 next
175 }
176 $1 ~ /^#[ ]*if/ {
177 print > sysent
178 print > sysprotos
179 print > sysnames
180 savesyscall = syscall
181 next
182 }
183 $1 ~ /^#[ ]*else/ {
184 print > sysent
185 print > sysprotos
186 print > sysnames
187 syscall = savesyscall
188 next
189 }
190 $1 ~ /^#/ {
191 print > sysent
192 print > sysprotos
193 print > sysnames
194 next
195 }
196 syscall != $1 {
197 printf "%s: line %d: syscall number out of sync at %d\n", \
198 infile, NR, syscall
199 printf "line is:\n"
200 print
201 exit 1
202 }
203 function parserr(was, wanted) {
204 printf "%s: line %d: unexpected %s (expected %s)\n", \
205 infile, NR, was, wanted
206 exit 1
207 }
208 function parseline() {
209 f=3 # toss number and type
210 if ($NF != "}") {
211 funcalias=$NF
212 end=NF-1
213 } else {
214 funcalias=""
215 end=NF
216 }
217 if ($f != "{")
218 parserr($f, "{")
219 f++
220 if ($end != "}")
221 parserr($end, "}")
222 end--
223 if ($end != ";")
224 parserr($end, ";")
225 end--
226 if ($end != ")")
227 parserr($end, ")")
228 end--
229
230 f++ # toss return type
231
232 funcname=$f
233 if (funcalias == "") {
234 funcalias=funcname
235 sub(/^([^_]+_)*sys_/, "", funcalias)
236 }
237 f++
238
239 if ($f != "(")
240 parserr($f, ")")
241 f++
242
243 argc= 0;
244 if (f == end) {
245 if ($f != "void")
246 parserr($f, "argument definition")
247 return
248 }
249
250 while (f <= end) {
251 argc++
252 argtype[argc]=""
253 oldf=""
254 while (f < end && $(f+1) != ",") {
255 if (argtype[argc] != "" && oldf != "*")
256 argtype[argc] = argtype[argc]" ";
257 argtype[argc] = argtype[argc]$f;
258 oldf = $f;
259 f++
260 }
261 if (argtype[argc] == "")
262 parserr($f, "argument definition")
263 argname[argc]=$f;
264 f += 2; # skip name, and any comma
265 }
266 }
267 function putent(nodefs, compatwrap) {
268 # output syscall declaration for switch table
269 prototype = "__P((struct proc *, void *, register_t *))"
270 if (compatwrap == "")
271 printf("int\t%s\t%s;\n", funcname, prototype) > sysprotos
272 else
273 printf("int\t%s_%s\t%s;\n", compatwrap, funcname, prototype) > sysprotos
274
275 # output syscall switch entry
276 # printf("\t{ { %d", argc) > sysent
277 # for (i = 1; i <= argc; i++) {
278 # if (i == 5) # wrap the line
279 # printf(",\n\t ") > sysent
280 # else
281 # printf(", ") > sysent
282 # printf("s(%s)", argtypenospc[i]) > sysent
283 # }
284 printf("\t{ %d, ", argc) > sysent
285 if (argc == 0)
286 printf("0") > sysent
287 else if (compatwrap == "")
288 printf("s(struct %s_args)", funcname) > sysent
289 else
290 printf("s(struct %s_%s_args)", compatwrap, funcname) > sysent
291 if (compatwrap == "")
292 wfn = sprintf("%s", funcname);
293 else
294 wfn = sprintf("%s(%s)", compatwrap, funcname);
295 printf(",\n\t %s },", wfn) > sysent
296 for (i = 0; i < (33 - length(wfn)) / 8; i++)
297 printf("\t") > sysent
298 if (compatwrap == "")
299 printf("/* %d = %s */\n", syscall, funcalias) > sysent
300 else
301 printf("/* %d = %s %s */\n", syscall, compatwrap,
302 funcalias) > sysent
303
304 # output syscall name for names table
305 if (compatwrap == "")
306 printf("\t\"%s\",\t\t\t/* %d = %s */\n", funcalias, syscall,
307 funcalias) > sysnames
308 else
309 printf("\t\"%s_%s\",\t/* %d = %s %s */\n", compatwrap,
310 funcalias, syscall, compatwrap, funcalias) > sysnames
311
312 # output syscall number of header, if appropriate
313 if (nodefs == "" || nodefs == "NOARGS")
314 printf("#define\t%s%s\t%d\n", constprefix, funcalias,
315 syscall) > sysnumhdr
316 else if (nodefs != "NODEF")
317 printf("\t\t\t\t/* %d is %s %s */\n", syscall,
318 compatwrap, funcalias) > sysnumhdr
319
320 # output syscall argument structure, if it has arguments
321 if (argc != 0 && nodefs != "NOARGS") {
322 if (compatwrap == "")
323 printf("\nstruct %s_args {\n", funcname) > sysarghdr
324 else
325 printf("\nstruct %s_%s_args {\n", compatwrap,
326 funcname) > sysarghdr
327 for (i = 1; i <= argc; i++)
328 printf("\tsyscallarg(%s) %s;\n", argtype[i],
329 argname[i]) > sysarghdr
330 printf("};\n") > sysarghdr
331 }
332 }
333 $2 == "STD" {
334 parseline()
335 putent("", "");
336 syscall++
337 next
338 }
339 $2 == "NODEF" || $2 == "NOARGS" {
340 parseline()
341 putent($2, "")
342 syscall++
343 next
344 }
345 $2 == "OBSOL" || $2 == "UNIMPL" {
346 if ($2 == "OBSOL")
347 comment="obsolete"
348 else
349 comment="unimplemented"
350 for (i = 3; i <= NF; i++)
351 comment=comment " " $i
352
353 printf("\t{ 0, 0,\n\t sys_nosys },\t\t\t/* %d = %s */\n", \
354 syscall, comment) > sysent
355 printf("\t\"#%d (%s)\",\t\t/* %d = %s */\n", \
356 syscall, comment, syscall, comment) > sysnames
357 if ($2 != "UNIMPL")
358 printf("\t\t\t\t/* %d is %s */\n", syscall, comment) > sysnumhdr
359 syscall++
360 next
361 }
362 {
363 for (i = 1; i <= ncompat; i++) {
364 if ($2 == compat_upper[i]) {
365 parseline();
366 putent("COMMENT", compat[i])
367 syscall++
368 next
369 }
370 }
371 printf "%s: line %d: unrecognized keyword %s\n", infile, NR, $2
372 exit 1
373 }
374 END {
375 printf("};\n\n") > sysent
376 printf("};\n") > sysnames
377 printf("#define\t%sMAXSYSCALL\t%d\n", constprefix, syscall) > sysnumhdr
378 } '
379
380 cat $sysprotos >> $sysarghdr
381 cat $sysdcl $sysent > $syssw
382
383 #chmod 444 $sysnames $sysnumhdr $syssw
384