makesyscalls.sh revision 1.15 1 #! /bin/sh -
2 # $NetBSD: makesyscalls.sh,v 1.15 1995/04/22 19:43:02 christos 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 syscompat_pref="sysent."
62 sysent="sysent.switch"
63
64 syscompat_files=""
65 for file in $compatopts; do
66 syscompat_files="$syscompat_files $syscompat_pref$file"
67 done
68
69 trap "rm $sysdcl $syscompat_files $sysent" 0
70
71 # Awk program (must support nawk extensions)
72 # Use "awk" at Berkeley, "nawk" or "gawk" elsewhere.
73 awk=${AWK:-awk}
74
75 # Does this awk have a "toupper" function? (i.e. is it GNU awk)
76 isgawk=`$awk 'BEGIN { print toupper("true"); exit; }' 2>/dev/null`
77
78 # If this awk does not define "toupper" then define our own.
79 if [ "$isgawk" = TRUE ] ; then
80 # GNU awk provides it.
81 toupper=
82 else
83 # Provide our own toupper()
84 toupper='
85 function toupper(str) {
86 _toupper_cmd = "echo "str" |tr a-z A-Z"
87 _toupper_cmd | getline _toupper_str;
88 close(_toupper_cmd);
89 return _toupper_str;
90 }'
91 fi
92
93 # before handing it off to awk, make a few adjustments:
94 # (1) insert spaces around {, }, (, ), *, and commas.
95 # (2) get rid of any and all dollar signs (so that rcs id use safe)
96 #
97 # The awk script will deal with blank lines and lines that
98 # start with the comment character (';').
99
100 sed -e '
101 s/\$//g
102 :join
103 /\\$/{a\
104
105 N
106 s/\\\n//
107 b join
108 }
109 2,${
110 /^#/!s/\([{}()*,]\)/ \1 /g
111 }
112 ' < $2 | $awk "
113 $toupper
114 BEGIN {
115 sysnames = \"$sysnames\"
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 compat_file[i] = sprintf("%s%s", syscompat_pref, compat[i])
137
138 printf "\n#ifdef %s\n", compat_upper[i] > compat_file[i]
139 printf "#define %s(func) __CONCAT(%s_,func)\n\n", \
140 compat[i], compat[i] > compat_file[i]
141 }
142
143 printf "/*\n * System call names.\n *\n" > sysnames
144 printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysnames
145
146 printf "/*\n * System call numbers.\n *\n" > sysnumhdr
147 printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysnumhdr
148
149 printf "/*\n * System call argument lists.\n *\n" > sysarghdr
150 printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysarghdr
151 }
152 NR == 1 {
153 printf " * created from%s\n */\n\n", $0 > sysdcl
154
155 printf "#define\ts(type)\tsizeof(type)\n\n" > sysent
156 printf "struct sysent %s[] = {\n",switchname > sysent
157
158 printf " * created from%s\n */\n\n", $0 > sysnames
159 printf "char *%s[] = {\n",namesname > sysnames
160
161 printf " * created from%s\n */\n\n", $0 > sysnumhdr
162
163 printf " * created from%s\n */\n\n", $0 > sysarghdr
164 printf "#define\tsyscallarg(x)\tunion { x datum; register_t pad; }\n" \
165 > sysarghdr
166 next
167 }
168 NF == 0 || $1 ~ /^;/ {
169 next
170 }
171 $1 ~ /^#[ ]*include/ {
172 print > sysdcl
173 next
174 }
175 $1 ~ /^#[ ]*if/ {
176 print > sysent
177 print > sysdcl
178 for (i = 1; i <= ncompat; i++)
179 print > compat_file[i]
180 print > sysnames
181 savesyscall = syscall
182 next
183 }
184 $1 ~ /^#[ ]*else/ {
185 print > sysent
186 print > sysdcl
187 for (i = 1; i <= ncompat; i++)
188 print > compat_file[i]
189 print > sysnames
190 syscall = savesyscall
191 next
192 }
193 $1 ~ /^#/ {
194 print > sysent
195 print > sysdcl
196 for (i = 1; i <= ncompat; i++)
197 print > compat_file[i]
198 print > sysnames
199 next
200 }
201 syscall != $1 {
202 printf "%s: line %d: syscall number out of sync at %d\n", \
203 infile, NR, syscall
204 printf "line is:\n"
205 print
206 exit 1
207 }
208 function parserr(was, wanted) {
209 printf "%s: line %d: unexpected %s (expected %s)\n", \
210 infile, NR, was, wanted
211 exit 1
212 }
213 function parseline() {
214 f=3 # toss number and type
215 if ($NF != "}") {
216 funcalias=$NF
217 end=NF-1
218 } else {
219 funcalias=""
220 end=NF
221 }
222 if ($f != "{")
223 parserr($f, "{")
224 f++
225 if ($end != "}")
226 parserr($end, "}")
227 end--
228 if ($end != ";")
229 parserr($end, ";")
230 end--
231 if ($end != ")")
232 parserr($end, ")")
233 end--
234
235 f++ # toss return type
236
237 funcname=$f
238 if (funcalias == "")
239 funcalias=funcname
240 f++
241
242 if ($f != "(")
243 parserr($f, ")")
244 f++
245
246 argc= 0;
247 if (f == end) {
248 if ($f != "void")
249 parserr($f, "argument definition")
250 return
251 }
252
253 while (f <= end) {
254 argc++
255 argtype[argc]=""
256 oldf=""
257 while (f < end && $(f+1) != ",") {
258 if (argtype[argc] != "" && oldf != "*")
259 argtype[argc] = argtype[argc]" ";
260 argtype[argc] = argtype[argc]$f;
261 oldf = $f;
262 f++
263 }
264 if (argtype[argc] == "")
265 parserr($f, "argument definition")
266 argname[argc]=$f;
267 f += 2; # skip name, and any comma
268 }
269 }
270 function putent(nodefs, declfile, compatwrap) {
271 # output syscall declaration for switch table
272 if (compatwrap == "")
273 printf("int\t%s();\n", funcname) > declfile
274 else
275 printf("int\t%s(%s)();\n", compatwrap, funcname) > declfile
276
277 # output syscall switch entry
278 # printf("\t{ { %d", argc) > sysent
279 # for (i = 1; i <= argc; i++) {
280 # if (i == 5) # wrap the line
281 # printf(",\n\t ") > sysent
282 # else
283 # printf(", ") > sysent
284 # printf("s(%s)", argtypenospc[i]) > sysent
285 # }
286 printf("\t{ %d, ", argc) > sysent
287 if (argc == 0)
288 printf("0") > sysent
289 else if (compatwrap == "")
290 printf("s(struct %s_args)", funcname) > sysent
291 else
292 printf("s(struct %s_%s_args)", compatwrap, funcname) > sysent
293 if (compatwrap == "")
294 wfn = sprintf("%s", funcname);
295 else
296 wfn = sprintf("%s(%s)", compatwrap, funcname);
297 printf(",\n\t %s },", wfn) > sysent
298 for (i = 0; i < (33 - length(wfn)) / 8; i++)
299 printf("\t") > sysent
300 if (compatwrap == "")
301 printf("/* %d = %s */\n", syscall, funcalias) > sysent
302 else
303 printf("/* %d = %s %s */\n", syscall, compatwrap,
304 funcalias) > sysent
305
306 # output syscall name for names table
307 if (compatwrap == "")
308 printf("\t\"%s\",\t\t\t/* %d = %s */\n", funcalias, syscall,
309 funcalias) > sysnames
310 else
311 printf("\t\"%s_%s\",\t/* %d = %s %s */\n", compatwrap,
312 funcalias, syscall, compatwrap, funcalias) > sysnames
313
314 # output syscall number of header, if appropriate
315 if (nodefs == "" || nodefs == "NOARGS")
316 printf("#define\t%s%s\t%d\n", constprefix, funcalias,
317 syscall) > sysnumhdr
318 else if (nodefs != "NODEF")
319 printf("\t\t\t\t/* %d is %s %s */\n", syscall,
320 compatwrap, funcalias) > sysnumhdr
321
322 # output syscall argument structure, if it has arguments
323 if (argc != 0 && nodefs != "NOARGS") {
324 if (compatwrap == "")
325 printf("\nstruct %s_args {\n", funcname) > sysarghdr
326 else
327 printf("\nstruct %s_%s_args {\n", compatwrap,
328 funcname) > sysarghdr
329 for (i = 1; i <= argc; i++)
330 printf("\tsyscallarg(%s) %s;\n", argtype[i],
331 argname[i]) > sysarghdr
332 printf("};\n") > sysarghdr
333 }
334 }
335 $2 == "STD" {
336 parseline()
337 putent("", sysdcl, "")
338 syscall++
339 next
340 }
341 $2 == "NODEF" || $2 == "NOARGS" {
342 parseline()
343 putent($2, sysdcl, "")
344 syscall++
345 next
346 }
347 $2 == "OBSOL" || $2 == "UNIMPL" {
348 if ($2 == "OBSOL")
349 comment="obsolete"
350 else
351 comment="unimplemented"
352 for (i = 3; i <= NF; i++)
353 comment=comment " " $i
354
355 printf("\t{ 0, 0,\n\t nosys },\t\t\t\t/* %d = %s */\n", \
356 syscall, comment) > sysent
357 printf("\t\"#%d (%s)\",\t\t/* %d = %s */\n", \
358 syscall, comment, syscall, comment) > sysnames
359 if ($2 != "UNIMPL")
360 printf("\t\t\t\t/* %d is %s */\n", syscall, comment) > sysnumhdr
361 syscall++
362 next
363 }
364 {
365 for (i = 1; i <= ncompat; i++) {
366 if ($2 == compat_upper[i]) {
367 parseline();
368 putent("COMMENT", compat_file[i], compat[i])
369 syscall++
370 next
371 }
372 }
373 printf "%s: line %d: unrecognized keyword %s\n", infile, NR, $2
374 exit 1
375 }
376 END {
377 printf "\n#undef\tsyscallarg\n" > sysarghdr
378
379 for (i = 1; i <= ncompat; i++) {
380 printf("\n#else /* %s */\n", compat_upper[i]) > compat_file[i]
381 printf("#define %s(func) nosys\n", compat[i]) > \
382 compat_file[i]
383 printf("#endif /* %s */\n\n", compat_upper[i]) > compat_file[i]
384 }
385
386 printf("};\n\n") > sysent
387 printf("};\n") > sysnames
388 printf("#define\t%sMAXSYSCALL\t%d\n", constprefix, syscall) > sysnumhdr
389 } '
390
391 cat $sysdcl $syscompat_files $sysent > $syssw
392
393 #chmod 444 $sysnames $sysnumhdr $syssw
394