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