makesyscalls.sh revision 1.12 1 #! /bin/sh -
2 #
3 # Copyright (c) 1994 Christopher G. Demetriou
4 # All rights reserved.
5 #
6 # Redistribution and use in source and binary forms, with or without
7 # modification, are permitted provided that the following conditions
8 # are met:
9 # 1. Redistributions of source code must retain the above copyright
10 # notice, this list of conditions and the following disclaimer.
11 # 2. Redistributions in binary form must reproduce the above copyright
12 # notice, this list of conditions and the following disclaimer in the
13 # documentation and/or other materials provided with the distribution.
14 # 3. All advertising materials mentioning features or use of this software
15 # must display the following acknowledgement:
16 # This product includes software developed for the NetBSD Project
17 # by Christopher G. Demetriou.
18 # 4. The name of the author may not be used to endorse or promote products
19 # derived from this software without specific prior written permission
20 #
21 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
26 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
30 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 #
32 # from: @(#)makesyscalls.sh 8.1 (Berkeley) 6/10/93
33 # $NetBSD: makesyscalls.sh,v 1.12 1994/10/20 04:37:09 cgd Exp $
34 #
35
36 set -e
37
38 case $# in
39 2) ;;
40 *) echo "Usage: $0 config-file input-file" 1>&2
41 exit 1
42 ;;
43 esac
44
45 # source the config file.
46 . $1
47
48 # the config file sets the following variables:
49 # sysnames the syscall names file
50 # sysnumhdr the syscall numbers file
51 # syssw the syscall switch file
52 # sysarghdr the syscall argument struct definitions
53 # compatopts those syscall types that are for 'compat' syscalls
54 # switchname the name for the 'struct sysent' we define
55 # namesname the name for the 'char *[]' we define
56 # constprefix the prefix for the system call constants
57 #
58 # NOTE THAT THIS makesyscalls.sh DOES NOT SUPPORT 'LIBCOMPAT'.
59
60 # tmp files:
61 sysdcl="sysent.dcl"
62 syscompat_pref="sysent."
63 sysent="sysent.switch"
64
65 syscompat_files=""
66 for file in $compatopts; do
67 syscompat_files="$syscompat_files $syscompat_pref$file"
68 done
69
70 trap "rm $sysdcl $syscompat_files $sysent" 0
71
72 # Awk program (must support nawk extensions)
73 # Use "awk" at Berkeley, "nawk" or "gawk" elsewhere.
74 awk=${AWK:-awk}
75
76 # Does this awk have a "toupper" function? (i.e. is it GNU awk)
77 isgawk=`$awk 'BEGIN { print toupper("true"); exit; }' 2>/dev/null`
78
79 # If this awk does not define "toupper" then define our own.
80 if [ "$isgawk" = TRUE ] ; then
81 # GNU awk provides it.
82 toupper=
83 else
84 # Provide our own toupper()
85 toupper='
86 function toupper(str) {
87 _toupper_cmd = "echo "str" |tr a-z A-Z"
88 _toupper_cmd | getline _toupper_str;
89 close(_toupper_cmd);
90 return _toupper_str;
91 }'
92 fi
93
94 # before handing it off to awk, make a few adjustments:
95 # (1) insert spaces around {, }, (, ), *, and commas.
96 # (2) get rid of any and all dollar signs (so that rcs id use safe)
97 #
98 # The awk script will deal with blank lines and lines that
99 # start with the comment character (';').
100
101 sed -e '
102 s/\$//g
103 :join
104 /\\$/{a\
105
106 N
107 s/\\\n//
108 b join
109 }
110 2,${
111 /^#/!s/\([{}()*,]\)/ \1 /g
112 }
113 ' < $2 | $awk "
114 $toupper
115 BEGIN {
116 sysnames = \"$sysnames\"
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 infile = \"$2\"
127
128 compatopts = \"$compatopts\"
129 "'
130
131 printf "/*\n * System call switch table.\n *\n" > sysdcl
132 printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysdcl
133
134 ncompat = split(compatopts,compat)
135 for (i = 1; i <= ncompat; i++) {
136 compat_upper[i] = toupper(compat[i])
137 compat_file[i] = sprintf("%s%s", syscompat_pref, compat[i])
138
139 printf "\n#ifdef %s\n", compat_upper[i] > compat_file[i]
140 printf "#define %s(func) __CONCAT(%s_,func)\n\n", \
141 compat[i], compat[i] > compat_file[i]
142 }
143
144 printf "/*\n * System call names.\n *\n" > sysnames
145 printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysnames
146
147 printf "/*\n * System call numbers.\n *\n" > sysnumhdr
148 printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysnumhdr
149
150 printf "/*\n * System call numbers.\n *\n" > sysarghdr
151 printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysarghdr
152 }
153 NR == 1 {
154 printf " * created from%s\n */\n\n", $0 > sysdcl
155
156 printf "#define\ts(type)\tsizeof(type)\n\n" > sysent
157 printf "struct sysent %s[] = {\n",switchname > sysent
158
159 printf " * created from%s\n */\n\n", $0 > sysnames
160 printf "char *%s[] = {\n",namesname > sysnames
161
162 printf " * created from%s\n */\n\n", $0 > sysnumhdr
163
164 printf " * created from%s\n */\n\n", $0 > sysarghdr
165 printf "#define\tsyscallarg(x)\tunion { x datum; register_t pad; }\n" \
166 > sysarghdr
167 next
168 }
169 NF == 0 || $1 ~ /^;/ {
170 next
171 }
172 $1 ~ /^#[ ]*include/ {
173 print > sysdcl
174 next
175 }
176 $1 ~ /^#[ ]*if/ {
177 print > sysent
178 print > sysdcl
179 for (i = 1; i <= ncompat; i++)
180 print > compat_file[i]
181 print > sysnames
182 savesyscall = syscall
183 next
184 }
185 $1 ~ /^#[ ]*else/ {
186 print > sysent
187 print > sysdcl
188 for (i = 1; i <= ncompat; i++)
189 print > compat_file[i]
190 print > sysnames
191 syscall = savesyscall
192 next
193 }
194 $1 ~ /^#/ {
195 print > sysent
196 print > sysdcl
197 for (i = 1; i <= ncompat; i++)
198 print > compat_file[i]
199 print > sysnames
200 next
201 }
202 syscall != $1 {
203 printf "%s: line %d: syscall number out of sync at %d\n", \
204 infile, NR, syscall
205 printf "line is:\n"
206 print
207 exit 1
208 }
209 function parserr(was, wanted) {
210 printf "%s: line %d: unexpected %s (expected %s)\n", \
211 infile, NR, was, wanted
212 exit 1
213 }
214 function parseline() {
215 f=3 # toss number and type
216 if ($NF != "}") {
217 funcalias=$NF
218 end=NF-1
219 } else {
220 funcalias=""
221 end=NF
222 }
223 if ($f != "{")
224 parserr($f, "{")
225 f++
226 if ($end != "}")
227 parserr($end, "}")
228 end--
229 if ($end != ";")
230 parserr($end, ";")
231 end--
232 if ($end != ")")
233 parserr($end, ")")
234 end--
235
236 f++ # toss return type
237
238 funcname=$f
239 if (funcalias == "")
240 funcalias=funcname
241 f++
242
243 if ($f != "(")
244 parserr($f, ")")
245 f++
246
247 argc= 0;
248 if (f == end) {
249 if ($f != "void")
250 parserr($f, "argument definition")
251 return
252 }
253
254 while (f <= end) {
255 argc++
256 argtype[argc]=""
257 while (f < end && $(f+1) != ",") {
258 if (argtype[argc] != "")
259 argtype[argc] = argtype[argc]" ";
260 argtype[argc] = argtype[argc]$f;
261 f++
262 }
263 if (argtype[argc] == "")
264 parserr($f, "argument definition")
265 argname[argc]=$f;
266 f += 2; # skip name, and any comma
267 }
268 }
269 function putent(nodefs, declfile, compatwrap) {
270 # output syscall declaration for switch table
271 if (compatwrap == "")
272 printf("int\t%s();\n", funcname) > declfile
273 else
274 printf("int\t%s(%s)();\n", compatwrap, funcname) > declfile
275
276 # output syscall switch entry
277 # printf("\t{ { %d", argc) > sysent
278 # for (i = 1; i <= argc; i++) {
279 # if (i == 5) # wrap the line
280 # printf(",\n\t ") > sysent
281 # else
282 # printf(", ") > sysent
283 # printf("s(%s)", argtypenospc[i]) > sysent
284 # }
285 printf("\t{ %d, ", argc) > sysent
286 if (argc == 0)
287 printf("0") > sysent
288 else if (compatwrap == "")
289 printf("s(struct %s_args)", funcname) > sysent
290 else
291 printf("s(struct %s_%s_args)", compatwrap, funcname) > sysent
292 if (compatwrap == "")
293 wfn = sprintf("%s", funcname);
294 else
295 wfn = sprintf("%s(%s)", compatwrap, funcname);
296 printf(",\n\t %s },", wfn) > sysent
297 for (i = 0; i < (33 - length(wfn)) / 8; i++)
298 printf("\t") > sysent
299 if (compatwrap == "")
300 printf("/* %d = %s */\n", syscall, funcalias) > sysent
301 else
302 printf("/* %d = %s %s */\n", syscall, compatwrap,
303 funcalias) > sysent
304
305 # output syscall name for names table
306 if (compatwrap == "")
307 printf("\t\"%s\",\t\t\t/* %d = %s */\n", funcalias, syscall,
308 funcalias) > sysnames
309 else
310 printf("\t\"%s_%s\",\t/* %d = %s %s */\n", compatwrap,
311 funcalias, syscall, compatwrap, funcalias) > sysnames
312
313 # output syscall number of header, if appropriate
314 if (nodefs == "" || nodefs == "NOARGS")
315 printf("#define\t%s%s\t%d\n", constprefix, funcalias,
316 syscall) > sysnumhdr
317 else if (nodefs != "NODEF")
318 printf("\t\t\t\t/* %d is %s %s */\n", syscall,
319 compatwrap, funcalias) > sysnumhdr
320
321 # output syscall argument structure, if it has arguments
322 if (argc != 0 && nodefs != "NOARGS") {
323 if (compatwrap == "")
324 printf("\nstruct %s_args {\n", funcname) > sysarghdr
325 else
326 printf("\nstruct %s_%s_args {\n", compatwrap,
327 funcname) > sysarghdr
328 for (i = 1; i <= argc; i++)
329 printf("\tsyscallarg(%s) %s;\n", argtype[i],
330 argname[i]) > sysarghdr
331 printf("};\n") > sysarghdr
332 }
333 }
334 $2 == "STD" {
335 parseline()
336 putent("", sysdcl, "")
337 syscall++
338 next
339 }
340 $2 == "NODEF" || $2 == "NOARGS" {
341 parseline()
342 putent($2, sysdcl, "")
343 syscall++
344 next
345 }
346 $2 == "OBSOL" || $2 == "UNIMPL" {
347 if ($2 == "OBSOL")
348 comment="obsolete"
349 else
350 comment="unimplemented"
351 for (i = 3; i <= NF; i++)
352 comment=comment " " $i
353
354 printf("\t{ 0, 0,\n\t nosys },\t\t\t\t/* %d = %s */\n", \
355 syscall, comment) > sysent
356 printf("\t\"#%d (%s)\",\t\t/* %d = %s */\n", \
357 syscall, comment, syscall, comment) > sysnames
358 if ($2 != "UNIMPL")
359 printf("\t\t\t\t/* %d is %s */\n", syscall, comment) > sysnumhdr
360 syscall++
361 next
362 }
363 {
364 for (i = 1; i <= ncompat; i++) {
365 if ($2 == compat_upper[i]) {
366 parseline();
367 putent("COMMENT", compat_file[i], compat[i])
368 syscall++
369 next
370 }
371 }
372 printf "%s: line %d: unrecognized keyword %s\n", infile, NR, $2
373 exit 1
374 }
375 END {
376 printf "\n#undef\tsyscallarg\n" > sysarghdr
377
378 for (i = 1; i <= ncompat; i++) {
379 printf("\n#else /* %s */\n", compat_upper[i]) > compat_file[i]
380 printf("#define %s(func) nosys\n", compat[i]) > \
381 compat_file[i]
382 printf("#endif /* %s */\n\n", compat_upper[i]) > compat_file[i]
383 }
384
385 printf("};\n\n") > sysent
386 printf("int\tn%s= sizeof(%s) / sizeof(%s[0]);\n", switchname,
387 switchname, switchname) > sysent
388
389 printf("};\n") > sysnames
390 } '
391
392 cat $sysdcl $syscompat_files $sysent > $syssw
393
394 #chmod 444 $sysnames $syshdr $syssw
395