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