makesyscalls.sh revision 1.39 1 #! /bin/sh -
2 # $NetBSD: makesyscalls.sh,v 1.39 2000/11/29 22:09:05 jdolecek Exp $
3 #
4 # Copyright (c) 1994, 1996, 2000 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 "/* \$NetBSD\$ */\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 "/* \$NetBSD\$ */\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 "/* \$NetBSD\$ */\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 "/* \$NetBSD\$ */\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 "const char * const %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\\\n" > sysarghdr
191 printf "\tunion {\t\t\t\t\t\t\t\t\\\n" > sysarghdr
192 printf "\t\t%s pad;\t\t\t\t\t\t\\\n", registertype > sysarghdr
193 printf "\t\tstruct { x datum; } le;\t\t\t\t\t\\\n" > sysarghdr
194 printf "\t\tstruct {\t\t\t\t\t\t\\\n" > sysarghdr
195 printf "\t\t\tint8_t pad[ (sizeof (%s) < sizeof (x))\t\\\n", \
196 registertype > sysarghdr
197 printf "\t\t\t\t? 0\t\t\t\t\t\\\n" > sysarghdr
198 printf "\t\t\t\t: sizeof (%s) - sizeof (x)];\t\\\n", \
199 registertype > sysarghdr
200 printf "\t\t\tx datum;\t\t\t\t\t\\\n" > sysarghdr
201 printf "\t\t} be;\t\t\t\t\t\t\t\\\n" > sysarghdr
202 printf "\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: unbalanced #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 $1 ~ /^if/ {
247 print "#"$0 > sysdcl
248 next
249 }
250 $1 ~ /^endif/ {
251 print "#"$0 > sysdcl
252 next
253 }
254
255 syscall != $1 {
256 printf "%s: line %d: syscall number out of sync at %d\n", \
257 infile, NR, syscall
258 printf "line is:\n"
259 print
260 exit 1
261 }
262 function parserr(was, wanted) {
263 printf "%s: line %d: unexpected %s (expected %s)\n", \
264 infile, NR, was, wanted
265 printf "line is:\n"
266 print
267 exit 1
268 }
269 function parseline() {
270 f=3 # toss number and type
271 if ($NF != "}") {
272 funcalias=$NF
273 end=NF-1
274 } else {
275 funcalias=""
276 end=NF
277 }
278 if ($f != "{")
279 parserr($f, "{")
280 f++
281 if ($end != "}")
282 parserr($end, "}")
283 end--
284 if ($end != ";")
285 parserr($end, ";")
286 end--
287 if ($end != ")")
288 parserr($end, ")")
289 end--
290
291 returntype = oldf = "";
292 do {
293 if (returntype != "" && oldf != "*")
294 returntype = returntype" ";
295 returntype = returntype$f;
296 oldf = $f;
297 f++
298 } while (f < (end - 1) && $(f+1) != "(");
299 if (f == (end - 1)) {
300 parserr($f, "function argument definition (maybe \"(\"?)");
301 }
302
303 funcname=$f
304 if (funcalias == "") {
305 funcalias=funcname
306 sub(/^([^_]+_)*sys_/, "", funcalias)
307 }
308 f++
309
310 if ($f != "(")
311 parserr($f, ")")
312 f++
313
314 argc=0;
315 if (f == end) {
316 if ($f != "void")
317 parserr($f, "argument definition")
318 isvarargs = 0;
319 varargc = 0;
320 return
321 }
322
323 # some system calls (open() and fcntl()) can accept a variable
324 # number of arguments. If syscalls accept a variable number of
325 # arguments, they must still have arguments specified for
326 # the remaining argument "positions," because of the way the
327 # kernel system call argument handling works.
328 #
329 # Indirect system calls, e.g. syscall(), are exceptions to this
330 # rule, since they are handled entirely by machine-dependent code
331 # and do not need argument structures built.
332
333 isvarargs = 0;
334 while (f <= end) {
335 if ($f == "...") {
336 f++;
337 isvarargs = 1;
338 varargc = argc;
339 continue;
340 }
341 argc++
342 argtype[argc]=""
343 oldf=""
344 while (f < end && $(f+1) != ",") {
345 if (argtype[argc] != "" && oldf != "*")
346 argtype[argc] = argtype[argc]" ";
347 argtype[argc] = argtype[argc]$f;
348 oldf = $f;
349 f++
350 }
351 if (argtype[argc] == "")
352 parserr($f, "argument definition")
353 argname[argc]=$f;
354 f += 2; # skip name, and any comma
355 }
356 # must see another argument after varargs notice.
357 if (isvarargs) {
358 if (argc == varargc && $2 != "INDIR")
359 parserr($f, "argument definition")
360 } else
361 varargc = argc;
362 }
363 function putent(nodefs, compatwrap) {
364 # output syscall declaration for switch table. INDIR functions
365 # get none, since they always have sys_nosys() for their table
366 # entries.
367 if (nodefs != "INDIR") {
368 prototype = "(struct proc *, void *, register_t *)"
369 if (compatwrap == "")
370 printf("int\t%s%s;\n", funcname,
371 prototype) > sysprotos
372 else
373 printf("int\t%s_%s%s;\n", compatwrap, funcname,
374 prototype) > sysprotos
375 }
376
377 # output syscall switch entry
378 if (nodefs == "INDIR") {
379 printf("\t{ 0, 0,\n\t sys_nosys },\t\t\t/* %d = %s (indir) */\n", \
380 syscall, funcalias) > sysent
381 } else {
382 # printf("\t{ { %d", argc) > sysent
383 # for (i = 1; i <= argc; i++) {
384 # if (i == 5) # wrap the line
385 # printf(",\n\t ") > sysent
386 # else
387 # printf(", ") > sysent
388 # printf("s(%s)", argtypenospc[i]) > sysent
389 # }
390 printf("\t{ %d, ", argc) > sysent
391 if (argc == 0)
392 printf("0") > sysent
393 else if (compatwrap == "")
394 printf("s(struct %s_args)", funcname) > sysent
395 else
396 printf("s(struct %s_%s_args)", compatwrap,
397 funcname) > sysent
398 if (compatwrap == "")
399 wfn = sprintf("%s", funcname);
400 else
401 wfn = sprintf("%s(%s)", compatwrap, funcname);
402 printf(",\n\t %s },", wfn) > sysent
403 for (i = 0; i < (33 - length(wfn)) / 8; i++)
404 printf("\t") > sysent
405 if (compatwrap == "")
406 printf("/* %d = %s */\n", syscall, funcalias) > sysent
407 else
408 printf("/* %d = %s %s */\n", syscall, compatwrap,
409 funcalias) > sysent
410 }
411
412 # output syscall name for names table
413 if (compatwrap == "")
414 printf("\t\"%s\",\t\t\t/* %d = %s */\n", funcalias, syscall,
415 funcalias) > sysnamesbottom
416 else
417 printf("\t\"%s_%s\",\t/* %d = %s %s */\n", compatwrap,
418 funcalias, syscall, compatwrap, funcalias) > sysnamesbottom
419
420 # output syscall number of header, if appropriate
421 if (nodefs == "" || nodefs == "NOARGS" || nodefs == "INDIR") {
422 # output a prototype, to be used to generate lint stubs in
423 # libc.
424 printf("/* syscall: \"%s\" ret: \"%s\" args:", funcalias,
425 returntype) > sysnumhdr
426 for (i = 1; i <= varargc; i++)
427 printf(" \"%s\"", argtype[i]) > sysnumhdr
428 if (isvarargs)
429 printf(" \"...\"") > sysnumhdr
430 printf(" */\n") > sysnumhdr
431
432 printf("#define\t%s%s\t%d\n\n", constprefix, funcalias,
433 syscall) > sysnumhdr
434 } else if (nodefs == "COMPAT") {
435 # Just define the syscall number with a comment. These
436 # may be used by compatibility stubs in libc.
437 printf("#define\t%s%s_%s\t%d\n\n",
438 constprefix, compatwrap, funcalias, syscall) > sysnumhdr
439 } else if (nodefs != "NODEF")
440 printf("\t\t\t\t/* %d is %s %s */\n\n", syscall,
441 compatwrap, funcalias) > sysnumhdr
442
443 # output syscall argument structure, if it has arguments
444 if (argc != 0 && nodefs != "NOARGS" && nodefs != "INDIR") {
445 if (compatwrap == "")
446 printf("\nstruct %s_args {\n", funcname) > sysarghdr
447 else
448 printf("\nstruct %s_%s_args {\n", compatwrap,
449 funcname) > sysarghdr
450 for (i = 1; i <= argc; i++)
451 printf("\tsyscallarg(%s) %s;\n", argtype[i],
452 argname[i]) > sysarghdr
453 printf("};\n") > sysarghdr
454 }
455 }
456 $2 == "STD" {
457 parseline()
458 putent("", "");
459 syscall++
460 next
461 }
462 $2 == "NODEF" || $2 == "NOARGS" || $2 == "INDIR" {
463 parseline()
464 putent($2, "")
465 syscall++
466 next
467 }
468 $2 == "OBSOL" || $2 == "UNIMPL" || $2 == "EXCL" {
469 if ($2 == "OBSOL")
470 comment="obsolete"
471 else if ($2 == "EXCL")
472 comment="excluded"
473 else
474 comment="unimplemented"
475 for (i = 3; i <= NF; i++)
476 comment=comment " " $i
477
478 printf("\t{ 0, 0,\n\t sys_nosys },\t\t\t/* %d = %s */\n", \
479 syscall, comment) > sysent
480 printf("\t\"#%d (%s)\",\t\t/* %d = %s */\n", \
481 syscall, comment, syscall, comment) > sysnamesbottom
482 if ($2 != "UNIMPL")
483 printf("\t\t\t\t/* %d is %s */\n", syscall, comment) > sysnumhdr
484 syscall++
485 next
486 }
487 {
488 for (i = 1; i <= ncompat; i++) {
489 if ($2 == compat_upper[i]) {
490 parseline();
491 putent("COMPAT", compat[i])
492 syscall++
493 next
494 }
495 }
496 printf "%s: line %d: unrecognized keyword %s\n", infile, NR, $2
497 exit 1
498 }
499 END {
500 printf("};\n\n") > sysent
501 printf("};\n") > sysnamesbottom
502 printf("#define\t%sMAXSYSCALL\t%d\n", constprefix, syscall) > sysnumhdr
503 } '
504
505 cat $sysprotos >> $sysarghdr
506 echo "#endif /* _${constprefix}_SYSCALLARGS_H_ */" >> $sysarghdr
507 cat $sysdcl $sysent > $syssw
508 cat $sysnamesbottom >> $sysnames
509
510 #chmod 444 $sysnames $sysnumhdr $syssw
511