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