makesyscalls.sh revision 1.35.12.1 1 #! /bin/sh -
2 # $NetBSD: makesyscalls.sh,v 1.35.12.1 2001/05/01 08:53:00 he 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 # the config file sets the following variables:
45 # sysnames the syscall names file
46 # sysnumhdr the syscall numbers file
47 # syssw the syscall switch file
48 # sysarghdr the syscall argument struct definitions
49 # compatopts those syscall types that are for 'compat' syscalls
50 # switchname the name for the 'struct sysent' we define
51 # namesname the name for the 'char *[]' we define
52 # constprefix the prefix for the system call constants
53 # registertype the type for register_t
54 # sys_nosys [optional] name of function called for unsupported
55 # syscalls, if not sys_nosys()
56 #
57 # NOTE THAT THIS makesyscalls.sh DOES NOT SUPPORT 'LIBCOMPAT'.
58
59 # source the config file.
60 sys_nosys="sys_nosys" # default is sys_nosys(), if not specified otherwise
61 . ./$1
62
63 # tmp files:
64 sysdcl="sysent.dcl"
65 sysprotos="sys.protos"
66 syscompat_pref="sysent."
67 sysent="sysent.switch"
68 sysnamesbottom="sysnames.bottom"
69
70 trap "rm $sysdcl $sysprotos $sysent $sysnamesbottom" 0
71
72 # Awk program (must support nawk extensions)
73 # Use "awk" at Berkeley, "nawk" or "gawk" elsewhere.
74 awk=${AWK:-awk}
75
76 # Does this awk have a "toupper" function? (i.e. is it GNU awk)
77 isgawk=`$awk 'BEGIN { print toupper("true"); exit; }' 2>/dev/null`
78
79 # If this awk does not define "toupper" then define our own.
80 if [ "$isgawk" = TRUE ] ; then
81 # GNU awk provides it.
82 toupper=
83 else
84 # Provide our own toupper()
85 toupper='
86 function toupper(str) {
87 _toupper_cmd = "echo "str" |tr a-z A-Z"
88 _toupper_cmd | getline _toupper_str;
89 close(_toupper_cmd);
90 return _toupper_str;
91 }'
92 fi
93
94 # before handing it off to awk, make a few adjustments:
95 # (1) insert spaces around {, }, (, ), *, and commas.
96 # (2) get rid of any and all dollar signs (so that rcs id use safe)
97 #
98 # The awk script will deal with blank lines and lines that
99 # start with the comment character (';').
100
101 sed -e '
102 s/\$//g
103 :join
104 /\\$/{a\
105
106 N
107 s/\\\n//
108 b join
109 }
110 2,${
111 /^#/!s/\([{}()*,]\)/ \1 /g
112 }
113 ' < $2 | $awk "
114 $toupper
115 BEGIN {
116 # to allow nested #if/#else/#endif sets
117 savedepth = 0
118
119 sysnames = \"$sysnames\"
120 sysprotos = \"$sysprotos\"
121 sysnumhdr = \"$sysnumhdr\"
122 sysarghdr = \"$sysarghdr\"
123 switchname = \"$switchname\"
124 namesname = \"$namesname\"
125 constprefix = \"$constprefix\"
126 registertype = \"$registertype\"
127 if (!registertype) {
128 registertype = \"register_t\"
129 }
130
131 sysdcl = \"$sysdcl\"
132 syscompat_pref = \"$syscompat_pref\"
133 sysent = \"$sysent\"
134 sysnamesbottom = \"$sysnamesbottom\"
135 sys_nosys = \"$sys_nosys\"
136 infile = \"$2\"
137
138 compatopts = \"$compatopts\"
139 "'
140
141 printf "/*\t\$NetBSD\$\t*/\n\n" > sysdcl
142 printf "/*\n * System call switch table.\n *\n" > sysdcl
143 printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysdcl
144
145 ncompat = split(compatopts,compat)
146 for (i = 1; i <= ncompat; i++) {
147 compat_upper[i] = toupper(compat[i])
148
149 printf "\n#ifdef %s\n", compat_upper[i] > sysent
150 printf "#define %s(func) __CONCAT(%s_,func)\n", compat[i], \
151 compat[i] > sysent
152 printf "#else\n" > sysent
153 printf "#define %s(func) %s\n", compat[i], sys_nosys > sysent
154 printf "#endif\n" > sysent
155 }
156
157 printf "\n#define\ts(type)\tsizeof(type)\n\n" > sysent
158 printf "struct sysent %s[] = {\n",switchname > sysent
159
160 printf "/*\t\$NetBSD\$\t*/\n\n" > sysnames
161 printf "/*\n * System call names.\n *\n" > sysnames
162 printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysnames
163
164 printf "\n/*\n * System call prototypes.\n */\n\n" > sysprotos
165
166 printf "/*\t\$NetBSD\$\t*/\n\n" > sysnumhdr
167 printf "/*\n * System call numbers.\n *\n" > sysnumhdr
168 printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysnumhdr
169
170 printf "/*\t\$NetBSD\$\t*/\n\n" > sysarghdr
171 printf "/*\n * System call argument lists.\n *\n" > sysarghdr
172 printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysarghdr
173 }
174 NR == 1 {
175 printf " * created from%s\n */\n\n", $0 > sysdcl
176
177 printf " * created from%s\n */\n\n", $0 > sysnames
178
179 # System call names are included by userland (kdump(1)), so
180 # hide the include files from it.
181 printf "#if defined(_KERNEL) && !defined(_LKM)\n" > sysnames
182
183 printf "#endif /* _KERNEL && ! _LKM */\n\n" > sysnamesbottom
184 printf "char *%s[] = {\n",namesname > sysnamesbottom
185
186 printf " * created from%s\n */\n\n", $0 > sysnumhdr
187
188 printf " * created from%s\n */\n\n", $0 > sysarghdr
189 printf "#ifndef _" constprefix "_SYSCALLARGS_H_\n" > sysarghdr
190 printf "#define _" constprefix "_SYSCALLARGS_H_\n\n" > sysarghdr
191 printf "#ifdef\tsyscallarg\n" > sysarghdr
192 printf "#undef\tsyscallarg\n" > sysarghdr
193 printf "#endif\n\n" > sysarghdr
194 printf "#define\tsyscallarg(x)\t\t\t\t\t\t\t\\\n" > sysarghdr
195 printf "\tunion {\t\t\t\t\t\t\t\t\\\n" > sysarghdr
196 printf "\t\t%s pad;\t\t\t\t\t\t\\\n", registertype > sysarghdr
197 printf "\t\tstruct { x datum; } le;\t\t\t\t\t\\\n" > sysarghdr
198 printf "\t\tstruct {\t\t\t\t\t\t\\\n" > sysarghdr
199 printf "\t\t\tint8_t pad[ (sizeof (%s) < sizeof (x))\t\\\n", \
200 registertype > sysarghdr
201 printf "\t\t\t\t? 0\t\t\t\t\t\\\n" > sysarghdr
202 printf "\t\t\t\t: sizeof (%s) - sizeof (x)];\t\\\n", \
203 registertype > sysarghdr
204 printf "\t\t\tx datum;\t\t\t\t\t\\\n" > sysarghdr
205 printf "\t\t} be;\t\t\t\t\t\t\t\\\n" > sysarghdr
206 printf "\t}\n" > sysarghdr
207 next
208 }
209 NF == 0 || $1 ~ /^;/ {
210 next
211 }
212 $1 ~ /^#[ ]*include/ {
213 print > sysdcl
214 print > sysnames
215 next
216 }
217 $1 ~ /^#[ ]*if/ {
218 print > sysent
219 print > sysprotos
220 print > sysnamesbottom
221 savesyscall[++savedepth] = syscall
222 next
223 }
224 $1 ~ /^#[ ]*else/ {
225 print > sysent
226 print > sysprotos
227 print > sysnamesbottom
228 if (savedepth <= 0) {
229 printf "%s: line %d: unbalenced #else\n", \
230 infile, NR
231 exit 1
232 }
233 syscall = savesyscall[savedepth]
234 next
235 }
236 $1 ~ /^#/ {
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 = "__P((struct proc *, void *, register_t *))"
364 if (compatwrap == "")
365 printf("int\t%s\t%s;\n", funcname,
366 prototype) > sysprotos
367 else
368 printf("int\t%s_%s\t%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 %s },\t\t\t/* %d = %s (indir) */\n", \
375 sys_nosys, 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 %s },\t\t\t/* %d = %s */\n", \
474 sys_nosys, 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 printf("};\n\n") > sysent
496 printf("};\n") > sysnamesbottom
497 printf("#define\t%sMAXSYSCALL\t%d\n", constprefix, syscall) > sysnumhdr
498 } '
499
500 cat $sysprotos >> $sysarghdr
501 echo "#endif /* _${constprefix}_SYSCALLARGS_H_ */" >> $sysarghdr
502 cat $sysdcl $sysent > $syssw
503 cat $sysnamesbottom >> $sysnames
504
505 #chmod 444 $sysnames $sysnumhdr $syssw
506