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