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