makesyscalls.sh revision 1.31 1 #! /bin/sh -
2 # $NetBSD: makesyscalls.sh,v 1.31 1999/01/03 04:25:26 erh Exp $
3 #
4 # Copyright (c) 1994,1996 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 # registertype the type for register_t
57 #
58 # NOTE THAT THIS makesyscalls.sh DOES NOT SUPPORT 'LIBCOMPAT'.
59
60 # tmp files:
61 sysdcl="sysent.dcl"
62 sysprotos="sys.protos"
63 syscompat_pref="sysent."
64 sysent="sysent.switch"
65 sysnamesbottom="sysnames.bottom"
66
67 trap "rm $sysdcl $sysprotos $sysent $sysnamesbottom" 0
68
69 # Awk program (must support nawk extensions)
70 # Use "awk" at Berkeley, "nawk" or "gawk" elsewhere.
71 awk=${AWK:-awk}
72
73 # Does this awk have a "toupper" function? (i.e. is it GNU awk)
74 isgawk=`$awk 'BEGIN { print toupper("true"); exit; }' 2>/dev/null`
75
76 # If this awk does not define "toupper" then define our own.
77 if [ "$isgawk" = TRUE ] ; then
78 # GNU awk provides it.
79 toupper=
80 else
81 # Provide our own toupper()
82 toupper='
83 function toupper(str) {
84 _toupper_cmd = "echo "str" |tr a-z A-Z"
85 _toupper_cmd | getline _toupper_str;
86 close(_toupper_cmd);
87 return _toupper_str;
88 }'
89 fi
90
91 # before handing it off to awk, make a few adjustments:
92 # (1) insert spaces around {, }, (, ), *, and commas.
93 # (2) get rid of any and all dollar signs (so that rcs id use safe)
94 #
95 # The awk script will deal with blank lines and lines that
96 # start with the comment character (';').
97
98 sed -e '
99 s/\$//g
100 :join
101 /\\$/{a\
102
103 N
104 s/\\\n//
105 b join
106 }
107 2,${
108 /^#/!s/\([{}()*,]\)/ \1 /g
109 }
110 ' < $2 | $awk "
111 $toupper
112 BEGIN {
113 # to allow nested #if/#else/#endif sets
114 savedepth = 0
115
116 sysnames = \"$sysnames\"
117 sysprotos = \"$sysprotos\"
118 sysnumhdr = \"$sysnumhdr\"
119 sysarghdr = \"$sysarghdr\"
120 switchname = \"$switchname\"
121 namesname = \"$namesname\"
122 constprefix = \"$constprefix\"
123 registertype = \"$registertype\"
124 if (!registertype) {
125 registertype = \"register_t\"
126 }
127
128 sysdcl = \"$sysdcl\"
129 syscompat_pref = \"$syscompat_pref\"
130 sysent = \"$sysent\"
131 sysnamesbottom = \"$sysnamesbottom\"
132 infile = \"$2\"
133
134 compatopts = \"$compatopts\"
135 "'
136
137 printf "/*\t\$NetBSD\$\t*/\n\n" > sysdcl
138 printf "/*\n * System call switch table.\n *\n" > sysdcl
139 printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysdcl
140
141 ncompat = split(compatopts,compat)
142 for (i = 1; i <= ncompat; i++) {
143 compat_upper[i] = toupper(compat[i])
144
145 printf "\n#ifdef %s\n", compat_upper[i] > sysent
146 printf "#define %s(func) __CONCAT(%s_,func)\n", compat[i], \
147 compat[i] > sysent
148 printf "#else\n" > sysent
149 printf "#define %s(func) sys_nosys\n", compat[i] > sysent
150 printf "#endif\n" > sysent
151 }
152
153 printf "\n#define\ts(type)\tsizeof(type)\n\n" > sysent
154 printf "struct sysent %s[] = {\n",switchname > sysent
155
156 printf "/*\t\$NetBSD\$\t*/\n\n" > sysnames
157 printf "/*\n * System call names.\n *\n" > sysnames
158 printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysnames
159
160 printf "\n/*\n * System call prototypes.\n */\n\n" > sysprotos
161
162 printf "/*\t\$NetBSD\$\t*/\n\n" > sysnumhdr
163 printf "/*\n * System call numbers.\n *\n" > sysnumhdr
164 printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysnumhdr
165
166 printf "/*\t\$NetBSD\$\t*/\n\n" > sysarghdr
167 printf "/*\n * System call argument lists.\n *\n" > sysarghdr
168 printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysarghdr
169 }
170 NR == 1 {
171 printf " * created from%s\n */\n\n", $0 > sysdcl
172
173 printf " * created from%s\n */\n\n", $0 > sysnames
174
175 # System call names are included by userland (kdump(1)), so
176 # hide the include files from it.
177 printf "#if defined(_KERNEL) && !defined(_LKM)\n" > sysnames
178
179 printf "#endif /* _KERNEL && ! _LKM */\n\n" > sysnamesbottom
180 printf "char *%s[] = {\n",namesname > sysnamesbottom
181
182 printf " * created from%s\n */\n\n", $0 > sysnumhdr
183
184 printf " * created from%s\n */\n\n", $0 > sysarghdr
185 printf "#ifndef _" constprefix "_SYSCALLARGS_H_\n" > sysarghdr
186 printf "#define _" constprefix "_SYSCALLARGS_H_\n\n" > sysarghdr
187 printf "#ifdef\tsyscallarg\n" > sysarghdr
188 printf "#undef\tsyscallarg\n" > sysarghdr
189 printf "#endif\n\n" > sysarghdr
190 printf "#define\tsyscallarg(x)\t\t\t\t\t\t\t\t\\\n" > sysarghdr
191 printf "\t\tunion {\t\t\t\t\t\t\t\t\\\n" > sysarghdr
192 printf "\t\t\t%s pad;\t\t\t\t\t\t\\\n", registertype > sysarghdr
193 printf "\t\t\tstruct { x datum; } le;\t\t\t\t\t\\\n" > sysarghdr
194 printf "\t\t\tstruct {\t\t\t\t\t\t\\\n" > sysarghdr
195 printf "\t\t\t\tint8_t pad[ (sizeof (%s) < sizeof (x))\t\\\n", \
196 registertype > sysarghdr
197 printf "\t\t\t\t\t? 0\t\t\t\t\t\\\n" > sysarghdr
198 printf "\t\t\t\t\t: sizeof (%s) - sizeof (x)];\t\\\n", \
199 registertype > sysarghdr
200 printf "\t\t\t\tx datum;\t\t\t\t\t\\\n" > sysarghdr
201 printf "\t\t\t} be;\t\t\t\t\t\t\t\\\n" > sysarghdr
202 printf "\t\t}\n" > sysarghdr
203 next
204 }
205 NF == 0 || $1 ~ /^;/ {
206 next
207 }
208 $1 ~ /^#[ ]*include/ {
209 print > sysdcl
210 print > sysnames
211 next
212 }
213 $1 ~ /^#[ ]*if/ {
214 print > sysent
215 print > sysprotos
216 print > sysnamesbottom
217 savesyscall[++savedepth] = syscall
218 next
219 }
220 $1 ~ /^#[ ]*else/ {
221 print > sysent
222 print > sysprotos
223 print > sysnamesbottom
224 if (savedepth <= 0) {
225 printf "%s: line %d: unbalenced #else\n", \
226 infile, NR
227 exit 1
228 }
229 syscall = savesyscall[savedepth]
230 next
231 }
232 $1 ~ /^#/ {
233 if ($1 ~ /^#[ ]*endif/) {
234 if (savedepth <= 0) {
235 printf "%s: line %d: unbalenced #endif\n", \
236 infile, NR
237 exit 1
238 }
239 savedepth--;
240 }
241 print > sysent
242 print > sysprotos
243 print > sysnamesbottom
244 next
245 }
246 syscall != $1 {
247 printf "%s: line %d: syscall number out of sync at %d\n", \
248 infile, NR, syscall
249 printf "line is:\n"
250 print
251 exit 1
252 }
253 function parserr(was, wanted) {
254 printf "%s: line %d: unexpected %s (expected %s)\n", \
255 infile, NR, was, wanted
256 exit 1
257 }
258 function parseline() {
259 f=3 # toss number and type
260 if ($NF != "}") {
261 funcalias=$NF
262 end=NF-1
263 } else {
264 funcalias=""
265 end=NF
266 }
267 if ($f != "{")
268 parserr($f, "{")
269 f++
270 if ($end != "}")
271 parserr($end, "}")
272 end--
273 if ($end != ";")
274 parserr($end, ";")
275 end--
276 if ($end != ")")
277 parserr($end, ")")
278 end--
279
280 returntype = oldf = "";
281 do {
282 if (returntype != "" && oldf != "*")
283 returntype = returntype" ";
284 returntype = returntype$f;
285 oldf = $f;
286 f++
287 } while (f < (end - 1) && $(f+1) != "(");
288 if (f == (end - 1)) {
289 parserr($f, "function argument definition (maybe \"(\"?)");
290 }
291
292 funcname=$f
293 if (funcalias == "") {
294 funcalias=funcname
295 sub(/^([^_]+_)*sys_/, "", funcalias)
296 }
297 f++
298
299 if ($f != "(")
300 parserr($f, ")")
301 f++
302
303 argc=0;
304 if (f == end) {
305 if ($f != "void")
306 parserr($f, "argument definition")
307 isvarargs = 0;
308 varargc = 0;
309 return
310 }
311
312 # some system calls (open() and fcntl()) can accept a variable
313 # number of arguments. If syscalls accept a variable number of
314 # arguments, they must still have arguments specified for
315 # the remaining argument "positions," because of the way the
316 # kernel system call argument handling works.
317 #
318 # Indirect system calls, e.g. syscall(), are exceptions to this
319 # rule, since they are handled entirely by machine-dependent code
320 # and do not need argument structures built.
321
322 isvarargs = 0;
323 while (f <= end) {
324 if ($f == "...") {
325 f++;
326 isvarargs = 1;
327 varargc = argc;
328 continue;
329 }
330 argc++
331 argtype[argc]=""
332 oldf=""
333 while (f < end && $(f+1) != ",") {
334 if (argtype[argc] != "" && oldf != "*")
335 argtype[argc] = argtype[argc]" ";
336 argtype[argc] = argtype[argc]$f;
337 oldf = $f;
338 f++
339 }
340 if (argtype[argc] == "")
341 parserr($f, "argument definition")
342 argname[argc]=$f;
343 f += 2; # skip name, and any comma
344 }
345 # must see another argument after varargs notice.
346 if (isvarargs) {
347 if (argc == varargc && $2 != "INDIR")
348 parserr($f, "argument definition")
349 } else
350 varargc = argc;
351 }
352 function putent(nodefs, compatwrap) {
353 # output syscall declaration for switch table. INDIR functions
354 # get none, since they always have sys_nosys() for their table
355 # entries.
356 if (nodefs != "INDIR") {
357 prototype = "__P((struct proc *, void *, register_t *))"
358 if (compatwrap == "")
359 printf("int\t%s\t%s;\n", funcname,
360 prototype) > sysprotos
361 else
362 printf("int\t%s_%s\t%s;\n", compatwrap, funcname,
363 prototype) > sysprotos
364 }
365
366 # output syscall switch entry
367 if (nodefs == "INDIR") {
368 printf("\t{ 0, 0,\n\t sys_nosys },\t\t\t/* %d = %s (indir) */\n", \
369 syscall, funcalias) > sysent
370 } else {
371 # printf("\t{ { %d", argc) > sysent
372 # for (i = 1; i <= argc; i++) {
373 # if (i == 5) # wrap the line
374 # printf(",\n\t ") > sysent
375 # else
376 # printf(", ") > sysent
377 # printf("s(%s)", argtypenospc[i]) > sysent
378 # }
379 printf("\t{ %d, ", argc) > sysent
380 if (argc == 0)
381 printf("0") > sysent
382 else if (compatwrap == "")
383 printf("s(struct %s_args)", funcname) > sysent
384 else
385 printf("s(struct %s_%s_args)", compatwrap,
386 funcname) > sysent
387 if (compatwrap == "")
388 wfn = sprintf("%s", funcname);
389 else
390 wfn = sprintf("%s(%s)", compatwrap, funcname);
391 printf(",\n\t %s },", wfn) > sysent
392 for (i = 0; i < (33 - length(wfn)) / 8; i++)
393 printf("\t") > sysent
394 if (compatwrap == "")
395 printf("/* %d = %s */\n", syscall, funcalias) > sysent
396 else
397 printf("/* %d = %s %s */\n", syscall, compatwrap,
398 funcalias) > sysent
399 }
400
401 # output syscall name for names table
402 if (compatwrap == "")
403 printf("\t\"%s\",\t\t\t/* %d = %s */\n", funcalias, syscall,
404 funcalias) > sysnamesbottom
405 else
406 printf("\t\"%s_%s\",\t/* %d = %s %s */\n", compatwrap,
407 funcalias, syscall, compatwrap, funcalias) > sysnamesbottom
408
409 # output syscall number of header, if appropriate
410 if (nodefs == "" || nodefs == "NOARGS" || nodefs == "INDIR") {
411 # output a prototype, to be used to generate lint stubs in
412 # libc.
413 printf("/* syscall: \"%s\" ret: \"%s\" args:", funcalias,
414 returntype) > sysnumhdr
415 for (i = 1; i <= varargc; i++)
416 printf(" \"%s\"", argtype[i]) > sysnumhdr
417 if (isvarargs)
418 printf(" \"...\"") > sysnumhdr
419 printf(" */\n") > sysnumhdr
420
421 printf("#define\t%s%s\t%d\n\n", constprefix, funcalias,
422 syscall) > sysnumhdr
423 } else if (nodefs == "COMPAT") {
424 # Just define the syscall number with a comment. These
425 # may be used by compatibility stubs in libc.
426 printf("#define\t%s%s_%s\t%d\n\n",
427 constprefix, compatwrap, funcalias, syscall) > sysnumhdr
428 } else if (nodefs != "NODEF")
429 printf("\t\t\t\t/* %d is %s %s */\n\n", syscall,
430 compatwrap, funcalias) > sysnumhdr
431
432 # output syscall argument structure, if it has arguments
433 if (argc != 0 && nodefs != "NOARGS" && nodefs != "INDIR") {
434 if (compatwrap == "")
435 printf("\nstruct %s_args {\n", funcname) > sysarghdr
436 else
437 printf("\nstruct %s_%s_args {\n", compatwrap,
438 funcname) > sysarghdr
439 for (i = 1; i <= argc; i++)
440 printf("\tsyscallarg(%s) %s;\n", argtype[i],
441 argname[i]) > sysarghdr
442 printf("};\n") > sysarghdr
443 }
444 }
445 $2 == "STD" {
446 parseline()
447 putent("", "");
448 syscall++
449 next
450 }
451 $2 == "NODEF" || $2 == "NOARGS" || $2 == "INDIR" {
452 parseline()
453 putent($2, "")
454 syscall++
455 next
456 }
457 $2 == "OBSOL" || $2 == "UNIMPL" {
458 if ($2 == "OBSOL")
459 comment="obsolete"
460 else
461 comment="unimplemented"
462 for (i = 3; i <= NF; i++)
463 comment=comment " " $i
464
465 printf("\t{ 0, 0,\n\t sys_nosys },\t\t\t/* %d = %s */\n", \
466 syscall, comment) > sysent
467 printf("\t\"#%d (%s)\",\t\t/* %d = %s */\n", \
468 syscall, comment, syscall, comment) > sysnamesbottom
469 if ($2 != "UNIMPL")
470 printf("\t\t\t\t/* %d is %s */\n", syscall, comment) > sysnumhdr
471 syscall++
472 next
473 }
474 {
475 for (i = 1; i <= ncompat; i++) {
476 if ($2 == compat_upper[i]) {
477 parseline();
478 putent("COMPAT", compat[i])
479 syscall++
480 next
481 }
482 }
483 printf "%s: line %d: unrecognized keyword %s\n", infile, NR, $2
484 exit 1
485 }
486 END {
487 printf("};\n\n") > sysent
488 printf("};\n") > sysnamesbottom
489 printf("#define\t%sMAXSYSCALL\t%d\n", constprefix, syscall) > sysnumhdr
490 } '
491
492 cat $sysprotos >> $sysarghdr
493 echo "#endif /* _${constprefix}_SYSCALLARGS_H_ */" >> $sysarghdr
494 cat $sysdcl $sysent > $syssw
495 cat $sysnamesbottom >> $sysnames
496
497 #chmod 444 $sysnames $sysnumhdr $syssw
498