Home | History | Annotate | Line # | Download | only in kern
makesyscalls.sh revision 1.166
      1 #	$NetBSD: makesyscalls.sh,v 1.166 2017/01/13 06:11:27 christos Exp $
      2 #
      3 # Copyright (c) 1994, 1996, 2000 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 #	@(#)makesyscalls.sh	8.1 (Berkeley) 6/10/93
     33 
     34 set -e
     35 
     36 case $# in
     37     2)	;;
     38     *)	echo "Usage: $0 config-file input-file" 1>&2
     39 	exit 1
     40 	;;
     41 esac
     42 
     43 # the config file sets the following variables:
     44 #	sysalign	check for alignment of off_t/dev_t/time_t
     45 #	sysnames	the syscall names file
     46 #	sysnumhdr	the syscall numbers file
     47 #	syssw		the syscall switch file
     48 #	sysautoload	the syscall autoload definitions file
     49 #	sysarghdr	the syscall argument struct definitions
     50 #	compatopts	those syscall types that are for 'compat' syscalls
     51 #	switchname	the name for the 'struct sysent' we define
     52 #	namesname	the name for the 'const char *[]' we define
     53 #	constprefix	the prefix for the system call constants
     54 #	emulname	the emulation name
     55 #	registertype	the type for register_t
     56 #	nsysent		the size of the sysent table
     57 #	sys_nosys	[optional] name of function called for unsupported
     58 #			syscalls, if not sys_nosys()
     59 #       maxsysargs	[optiona] the maximum number or arguments
     60 #
     61 # NOTE THAT THIS makesyscalls.sh DOES NOT SUPPORT 'SYSLIBCOMPAT'.
     62 
     63 # source the config file.
     64 sys_nosys="sys_nosys"	# default is sys_nosys(), if not specified otherwise
     65 maxsysargs=8		# default limit is 8 (32bit) arguments
     66 systrace="/dev/null"
     67 sysautoload="/dev/null"
     68 rumpcalls="/dev/null"
     69 rumpcallshdr="/dev/null"
     70 rumpsysmap="/dev/null"
     71 rumpsysent="rumpsysent.tmp"
     72 rumpnoflags="\n\t\t.sy_flags = SYCALL_NOSYS,"
     73 rumpnosys="(sy_call_t *)rumpns_enosys"
     74 rumpnomodule="(sy_call_t *)rumpns_sys_nomodule"
     75 
     76 case $1 in
     77 /*)	. $1;;
     78 *)	. ./$1;;
     79 esac
     80 
     81 # tmp files:
     82 sysdcl="sysent.dcl"
     83 sysprotos="sys.protos"
     84 syscompat_pref="sysent."
     85 sysent="sysent.switch"
     86 sysnamesbottom="$sysnames.bottom"
     87 sysnamesfriendly="$sysnames.friendly"
     88 rumptypes="rumphdr.types"
     89 rumpprotos="rumphdr.protos"
     90 systracetmp="systrace.$$"
     91 systraceret="systraceret.$$"
     92 
     93 cleanup() {
     94     rm $sysdcl $sysprotos $sysent $sysnamesbottom $sysnamesfriendly $rumpsysent $rumptypes $rumpprotos $systracetmp $systraceret
     95 }
     96 trap "cleanup" 0
     97 
     98 # Awk program (must support nawk extensions)
     99 # Use "awk" at Berkeley, "nawk" or "gawk" elsewhere.
    100 awk=${AWK:-awk}
    101 
    102 # Does this awk have a "toupper" function?
    103 have_toupper="$($awk 'BEGIN { print toupper("true"); exit; }' 2>/dev/null)"
    104 
    105 # If this awk does not define "toupper" then define our own.
    106 if [ "$have_toupper" = TRUE ] ; then
    107 	# Used awk (GNU awk or nawk) provides it
    108 	toupper=
    109 else
    110 	# Provide our own toupper()
    111 	toupper='
    112 function toupper(str) {
    113 	_toupper_cmd = "echo "str" |tr a-z A-Z"
    114 	_toupper_cmd | getline _toupper_str;
    115 	close(_toupper_cmd);
    116 	return _toupper_str;
    117 }'
    118 fi
    119 
    120 # before handing it off to awk, make a few adjustments:
    121 #	(1) insert spaces around {, }, (, ), *, and commas.
    122 #	(2) get rid of any and all dollar signs (so that rcs id use safe)
    123 #
    124 # The awk script will deal with blank lines and lines that
    125 # start with the comment character (';').
    126 
    127 sed -e '
    128 s/\$//g
    129 :join
    130 	/\\$/{a\
    131 
    132 	N
    133 	s/\\\n//
    134 	b join
    135 	}
    136 2,${
    137 	/^#/!s/\([{}()*,|]\)/ \1 /g
    138 }
    139 ' < $2 | $awk "
    140 $toupper
    141 BEGIN {
    142 	# Create a NetBSD tag that does not get expanded when checking
    143 	# this script out of CVS.  (This part of the awk script is in a
    144 	# shell double-quoted string, so the backslashes are eaten by
    145 	# the shell.)
    146 	tag = \"\$\" \"NetBSD\" \"\$\"
    147 
    148 	# to allow nested #if/#else/#endif sets
    149 	savedepth = 0
    150 	auto_skip = 0
    151 	# to track already processed syscalls
    152 
    153 	sysnames = \"$sysnames\"
    154 	sysprotos = \"$sysprotos\"
    155 	sysnumhdr = \"$sysnumhdr\"
    156 	sysarghdr = \"$sysarghdr\"
    157 	sysarghdrextra = \"$sysarghdrextra\"
    158 	systrace = \"$systrace\"
    159 	systracetmp = \"$systracetmp\"
    160 	systraceret = \"$systraceret\"
    161 	sysautoload = \"$sysautoload\"
    162 	rumpcalls = \"$rumpcalls\"
    163 	rumpcallshdr = \"$rumpcallshdr\"
    164 	rumpsysent = \"$rumpsysent\"
    165 	rumpsysmap = \"$rumpsysmap\"
    166 	switchname = \"$switchname\"
    167 	namesname = \"$namesname\"
    168 	constprefix = \"$constprefix\"
    169 	emulname = \"$emulname\"
    170 	registertype = \"$registertype\"
    171 	sysalign=\"$sysalign\"
    172 	if (!registertype) {
    173 	    registertype = \"register_t\"
    174 	}
    175 	nsysent = \"$nsysent\"
    176 
    177 	sysdcl = \"$sysdcl\"
    178 	syscompat_pref = \"$syscompat_pref\"
    179 	sysent = \"$sysent\"
    180 	sysnamesbottom = \"${sysnames}.bottom\"
    181 	sysnamesfriendly = \"${sysnames}.friendly\"
    182 	rumpprotos = \"$rumpprotos\"
    183 	rumptypes = \"$rumptypes\"
    184 	sys_nosys = \"$sys_nosys\"
    185 	maxsysargs = \"$maxsysargs\"
    186 	rumpnoflags=\"$rumpnoflags\"
    187 	rumpnosys=\"$rumpnosys\"
    188 	rumpnomodule=\"$rumpnomodule\"
    189 	infile = \"$2\"
    190 
    191 	compatopts = \"$compatopts\"
    192 	"'
    193 
    194 	if (rumpcalls != "/dev/null")
    195 		haverumpcalls = 1
    196 
    197 	printf "/* %s */\n\n", tag > sysdcl
    198 	printf "/*\n * System call switch table.\n *\n" > sysdcl
    199 	printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysdcl
    200 
    201 	ncompat = split(compatopts,compat)
    202 	for (i = 1; i <= ncompat; i++) {
    203 		compat_upper[i] = toupper(compat[i])
    204 
    205 		printf "\n#ifdef %s\n", compat_upper[i] > sysent
    206 		printf "#define	%s(func) __CONCAT(%s_,func)\n", compat[i], \
    207 		    compat[i] > sysent
    208 		printf "#else\n" > sysent
    209 		printf "#define	%s(func) %s\n", compat[i], sys_nosys > sysent
    210 		printf "#endif\n" > sysent
    211 	}
    212 
    213 	printf "\n#define\ts(type)\tsizeof(type)\n" > sysent
    214 	printf "#define\tn(type)\t(sizeof(type)/sizeof (%s))\n", registertype > sysent
    215 	printf "#define\tns(type)\t.sy_narg = n(type), .sy_argsize = s(type)\n\n", registertype > sysent
    216 	printf "struct sysent %s[] = {\n",switchname > sysent
    217 
    218 	printf "/* %s */\n\n", tag > sysnames
    219 	printf "/*\n * System call names.\n *\n" > sysnames
    220 	printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysnames
    221 
    222 	printf "\n/*\n * System call prototypes.\n */\n\n" > sysprotos
    223 	if (haverumpcalls)
    224 		printf("#ifndef RUMP_CLIENT\n") > sysprotos
    225 
    226 	printf "/* %s */\n\n", tag > sysnumhdr
    227 	printf "/*\n * System call numbers.\n *\n" > sysnumhdr
    228 	printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysnumhdr
    229 
    230 	printf "/* %s */\n\n", tag > sysarghdr
    231 	printf "/*\n * System call argument lists.\n *\n" > sysarghdr
    232 	printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysarghdr
    233 
    234 	printf "/* %s */\n\n", tag > sysautoload
    235 	printf "/*\n * System call autoload table.\n *\n" > sysautoload
    236 	printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysautoload
    237 
    238 	printf "/* %s */\n\n", tag > rumpcalls
    239 	printf "/*\n * System call vector and marshalling for rump.\n *\n" > rumpcalls
    240 	printf " * DO NOT EDIT-- this file is automatically generated.\n" > rumpcalls
    241 
    242 	printf "/* %s */\n\n", tag > rumpcallshdr
    243 	printf "/*\n * System call protos in rump namespace.\n *\n" > rumpcallshdr
    244 	printf " * DO NOT EDIT-- this file is automatically generated.\n" > rumpcallshdr
    245 
    246 	printf "/* %s */\n\n", tag > systrace
    247 	printf "/*\n * System call argument to DTrace register array converstion.\n *\n" > systrace
    248 	printf " * DO NOT EDIT-- this file is automatically generated.\n" > systrace
    249 }
    250 NR == 1 {
    251 	sub(/ $/, "")
    252 	printf " * created from%s\n */\n\n", $0 > sysdcl
    253 	printf "#include <sys/cdefs.h>\n__KERNEL_RCSID(0, \"%s\");\n\n", tag > sysdcl
    254 
    255 	printf " * created from%s\n */\n\n", $0 > sysnames
    256 	printf "#include <sys/cdefs.h>\n__KERNEL_RCSID(0, \"%s\");\n\n", tag > sysnames
    257 
    258 	printf " * created from%s\n */\n\n", $0 > sysautoload
    259 	printf "#include <sys/cdefs.h>\n__KERNEL_RCSID(0, \"%s\");\n\n", tag > sysautoload
    260 	printf("#include <sys/proc.h>\n")		> sysautoload
    261 	printf("static struct sc_autoload " emulname \
    262 		"_syscalls_autoload[] = {\n")		> sysautoload
    263 
    264 	printf " * created from%s\n */\n\n", $0 > rumpcalls
    265 	printf "#ifdef RUMP_CLIENT\n" > rumpcalls
    266 	printf "#include <rump/rumpuser_port.h>\n" > rumpcalls
    267 	printf "#endif /* RUMP_CLIENT */\n\n" > rumpcalls
    268 	printf "#include <sys/param.h>\n\n" > rumpcalls
    269 	printf "#ifdef __NetBSD__\n" > rumpcalls
    270 	printf "#include <sys/cdefs.h>\n__KERNEL_RCSID(0, \"%s\");\n\n", tag > rumpcalls
    271 
    272 	printf "#include <sys/fstypes.h>\n" > rumpcalls
    273 	printf "#include <sys/proc.h>\n" > rumpcalls
    274 	printf "#endif /* __NetBSD__ */\n\n" > rumpcalls
    275 	printf "#ifdef RUMP_CLIENT\n" > rumpcalls
    276 	printf "#include <errno.h>\n" > rumpcalls
    277 	printf "#include <stdint.h>\n" > rumpcalls
    278 	printf "#include <stdlib.h>\n" > rumpcalls
    279 	printf "#include <string.h>\n\n" > rumpcalls
    280 	printf "#include <srcsys/syscall.h>\n" > rumpcalls
    281 	printf "#include <srcsys/syscallargs.h>\n\n" > rumpcalls
    282 	printf "#include <rump/rumpclient.h>\n\n" > rumpcalls
    283 	printf "#define rsys_syscall(num, data, dlen, retval)\t\\\n" > rumpcalls
    284 	printf "    rumpclient_syscall(num, data, dlen, retval)\n" > rumpcalls
    285 	printf "#define rsys_seterrno(error) errno = error\n" > rumpcalls
    286 	printf "#else\n" > rumpcalls
    287 	printf "#include <sys/syscall.h>\n" > rumpcalls
    288 	printf "#include <sys/syscallargs.h>\n\n" > rumpcalls
    289 	printf "#include <sys/syscallvar.h>\n\n" > rumpcalls
    290 	printf "#include <rump-sys/kern.h>\n\n" > rumpcalls
    291 	printf "#include <rump/rumpuser.h>\n" > rumpcalls
    292 	printf "#define rsys_syscall(num, data, dlen, retval)\t\\\n" > rumpcalls
    293 	printf "    rump_syscall(num, data, dlen, retval)\n\n" > rumpcalls
    294 	printf "#define rsys_seterrno(error) rumpuser_seterrno(error)\n" \
    295 	    > rumpcalls
    296 	printf "#endif\n\n" > rumpcalls
    297 
    298 	printf "#ifndef RUMP_KERNEL_IS_LIBC\n" > rumpcalls
    299 	printf "#define RUMP_SYS_COMPAT\n" > rumpcalls
    300 	printf "#endif\n\n" > rumpcalls
    301 
    302 	printf "#if\tBYTE_ORDER == BIG_ENDIAN\n" > rumpcalls
    303 	printf "#define SPARG(p,k)\t((p)->k.be.datum)\n" > rumpcalls
    304 	printf "#else /* LITTLE_ENDIAN, I hope dearly */\n" > rumpcalls
    305 	printf "#define SPARG(p,k)\t((p)->k.le.datum)\n" > rumpcalls
    306 	printf "#endif\n\n" > rumpcalls
    307 	printf "\nvoid rumpns_sys_nomodule(void);\n" > rumpcalls
    308 
    309 	printf "\n#ifndef RUMP_CLIENT\n" > rumpsysent
    310 	printf "int rumpns_enosys(void);\n" > rumpsysent
    311 	printf "#define\ts(type)\tsizeof(type)\n" > rumpsysent
    312 	printf "#define\tn(type)\t(sizeof(type)/sizeof (%s))\n", registertype > rumpsysent
    313 	printf "#define\tns(type)\tn(type), s(type)\n\n", registertype > rumpsysent
    314 	printf "struct sysent rump_sysent[] = {\n" > rumpsysent
    315 
    316 	# System call names are included by userland (kdump(1)), so
    317 	# hide the include files from it.
    318 	printf "#if defined(_KERNEL_OPT)\n" > sysnames
    319 
    320 	printf "#else /* _KERNEL_OPT */\n" > sysnamesbottom
    321 	printf "#include <sys/null.h>\n" > sysnamesbottom
    322 	printf "#endif /* _KERNEL_OPT */\n\n" > sysnamesbottom
    323 	printf "const char *const %s[] = {\n",namesname > sysnamesbottom
    324 	printf "\n\n/* libc style syscall names */\n" > sysnamesfriendly
    325 	printf "const char *const alt%s[] = {\n", namesname > sysnamesfriendly
    326 
    327 	printf " * created from%s\n */\n\n", $0 > sysnumhdr
    328 	printf "#ifndef _" constprefix "SYSCALL_H_\n" > sysnumhdr
    329 	printf "#define	_" constprefix "SYSCALL_H_\n\n" > sysnumhdr
    330 
    331 	printf " * created from%s\n */\n\n", $0 > sysarghdr
    332 	printf "#ifndef _" constprefix "SYSCALLARGS_H_\n" > sysarghdr
    333 	printf "#define	_" constprefix "SYSCALLARGS_H_\n\n" > sysarghdr
    334 
    335 	printf " * created from%s\n */\n\n", $0 > rumpcallshdr
    336 	printf "#ifndef _RUMP_RUMP_SYSCALLS_H_\n" > rumpcallshdr
    337 	printf "#define _RUMP_RUMP_SYSCALLS_H_\n\n" > rumpcallshdr
    338 	printf "#ifdef _KERNEL\n" > rumpcallshdr
    339 	printf "#error Interface not supported inside kernel\n" > rumpcallshdr
    340 	printf "#endif /* _KERNEL */\n\n" > rumpcallshdr
    341 	printf "#include <rump/rump_syscalls_compat.h>\n\n" > rumpcallshdr
    342 
    343 	printf "%s", sysarghdrextra > sysarghdr
    344 	# Write max number of system call arguments to both headers
    345 	printf("#define\t%sMAXSYSARGS\t%d\n\n", constprefix, maxsysargs) \
    346 		> sysnumhdr
    347 	printf("#define\t%sMAXSYSARGS\t%d\n\n", constprefix, maxsysargs) \
    348 		> sysarghdr
    349 	printf "#undef\tsyscallarg\n" > sysarghdr
    350 	printf "#define\tsyscallarg(x)\t\t\t\t\t\t\t\\\n" > sysarghdr
    351 	printf "\tunion {\t\t\t\t\t\t\t\t\\\n" > sysarghdr
    352 	printf "\t\t%s pad;\t\t\t\t\t\t\\\n", registertype > sysarghdr
    353 	printf "\t\tstruct { x datum; } le;\t\t\t\t\t\\\n" > sysarghdr
    354 	printf "\t\tstruct { /* LINTED zero array dimension */\t\t\\\n" \
    355 		> sysarghdr
    356 	printf "\t\t\tint8_t pad[  /* CONSTCOND */\t\t\t\\\n" > sysarghdr
    357 	printf "\t\t\t\t(sizeof (%s) < sizeof (x))\t\\\n", \
    358 		registertype > sysarghdr
    359 	printf "\t\t\t\t? 0\t\t\t\t\t\\\n" > sysarghdr
    360 	printf "\t\t\t\t: sizeof (%s) - sizeof (x)];\t\\\n", \
    361 		registertype > sysarghdr
    362 	printf "\t\t\tx datum;\t\t\t\t\t\\\n" > sysarghdr
    363 	printf "\t\t} be;\t\t\t\t\t\t\t\\\n" > sysarghdr
    364 	printf "\t}\n" > sysarghdr
    365 	printf("\n#undef check_syscall_args\n") >sysarghdr
    366 	printf("#define check_syscall_args(call) /*LINTED*/ \\\n" \
    367 		"\ttypedef char call##_check_args" \
    368 		    "[sizeof (struct call##_args) \\\n" \
    369 		"\t\t<= %sMAXSYSARGS * sizeof (%s) ? 1 : -1];\n", \
    370 		constprefix, registertype) >sysarghdr
    371 
    372 	printf " * This file is part of the DTrace syscall provider.\n */\n\n" > systrace
    373 	printf "static void\nsystrace_args(register_t sysnum, const void *params, uintptr_t *uarg, size_t *n_args)\n{\n" > systrace
    374 	printf "\tintptr_t *iarg  = (intptr_t *)uarg;\n" > systrace
    375 	printf "\tswitch (sysnum) {\n" > systrace
    376 
    377 	printf "static void\nsystrace_entry_setargdesc(int sysnum, int ndx, char *desc, size_t descsz)\n{\n\tconst char *p = NULL;\n" > systracetmp
    378 	printf "\tswitch (sysnum) {\n" > systracetmp
    379 
    380 	printf "static void\nsystrace_return_setargdesc(int sysnum, int ndx, char *desc, size_t descsz)\n{\n\tconst char *p = NULL;\n" > systraceret
    381 	printf "\tswitch (sysnum) {\n" > systraceret
    382 
    383 	# compat types from syscalls.master.  this is slightly ugly,
    384 	# but given that we have so few compats from over 17 years,
    385 	# a more complicated solution is not currently warranted.
    386 	uncompattypes["struct timeval50"] = "struct timeval";
    387 	uncompattypes["struct timespec50"] = "struct timespec";
    388 	uncompattypes["struct stat30"] = "struct stat";
    389 
    390 	next
    391 }
    392 NF == 0 || $1 ~ /^;/ {
    393 	next
    394 }
    395 $0 ~ /^%%$/ {
    396 	intable = 1
    397 	next
    398 }
    399 $1 ~ /^#[ 	]*include/ {
    400 	print > sysdcl
    401 	print > sysnames
    402 	next
    403 }
    404 $1 ~ /^#/ && !intable {
    405 	print > sysdcl
    406 	print > sysnames
    407 	next
    408 }
    409 $1 ~ /^#/ && intable {
    410 	if ($1 ~ /^#[ 	]*if/) {
    411 		savedepth++
    412 		savesyscall[savedepth] = syscall
    413 		skip_auto[savedepth] = auto_skip
    414 		auto_skip = 0
    415 
    416 		# Special handling for sysautoload conditionals
    417 		#
    418 		# We ignore all conditions other than those for
    419 		# !defined(_LP64) which are used for SYSV* syscalls
    420 		# only
    421 
    422 		if ($0 ~ /!defined\(_LP64\)/) {
    423 			printf("#if !defined(_LP64)\n") > sysautoload
    424 			auto_skip = savedepth
    425 		}
    426 	}
    427 	if ($1 ~ /^#[ 	]*else/) {
    428 		if (savedepth <= 0) {
    429 			printf("%s: line %d: unbalanced #else\n", \
    430 			    infile, NR)
    431 			exit 1
    432 		}
    433 		if (auto_skip == savedepth) {
    434 			print > sysautoload
    435 		}
    436 		syscall = savesyscall[savedepth]
    437 	}
    438 	if ($1 ~ /^#[       ]*endif/) {
    439 		if (savedepth <= 0) {
    440 			printf("%s: line %d: unbalanced #endif\n", \
    441 			    infile, NR)
    442 			exit 1
    443 		}
    444 		if (auto_skip == savedepth) {
    445 			print > sysautoload
    446 		}
    447 		auto_skip = skip_auto[savedepth];
    448 		savedepth--
    449 	}
    450 	print > sysent
    451 	print > sysarghdr
    452 	print > sysnumhdr
    453 	print > sysprotos
    454 	print > sysnamesbottom
    455 	print > sysnamesfriendly
    456 	print > systrace
    457 	print > systracetmp
    458 	print > systraceret
    459 
    460 	# XXX: technically we do not want to have conditionals in rump,
    461 	# but it is easier to just let the cpp handle them than try to
    462 	# figure out what we want here in this script
    463 	print > rumpsysent
    464 	next
    465 }
    466 syscall != $1 {
    467 	printf "%s: line %d: syscall number out of sync at %d\n", \
    468 	   infile, NR, syscall
    469 	printf "line is:\n"
    470 	print
    471 	exit 1
    472 }
    473 function isarg64(type) {
    474 	gsub("netbsd32_", "", type);
    475 	return type == "quad_t" || type == "off_t" \
    476 	    || type == "dev_t" ||  type == "time_t";
    477 }
    478 function parserr(was, wanted) {
    479 	printf "%s: line %d: unexpected %s (expected <%s>)\n", \
    480 	    infile, NR, was, wanted
    481 	printf "line is:\n"
    482 	print
    483 	exit 1
    484 }
    485 function fillerpsysent(syscall, flags, name, comment) {
    486 	return sprintf("\t{%s\n\t\t.sy_call = %s,\n\t},\t\t/* %d = filler */",\
    487 	    flags, name, syscall, comment);
    488 }
    489 function parseline() {
    490 	f=3			# toss number and type
    491 	if ($2 == "INDIR")
    492 		sycall_flags="SYCALL_INDIRECT"
    493 	else
    494 		sycall_flags="0"
    495 	if ($NF != "}") {
    496 		funcalias=$NF
    497 		end=NF-1
    498 	} else {
    499 		funcalias=""
    500 		end=NF
    501 	}
    502 	if ($f == "INDIR") {		# allow for "NOARG INDIR"
    503 		sycall_flags = "SYCALL_INDIRECT | " sycall_flags
    504 		f++
    505 	}
    506 	if ($f == "MODULAR") {		# registered at runtime
    507 		modular = 1
    508 		f++
    509 		modname = $f
    510 		f++
    511 	} else {
    512 		modular =  0;
    513 	}
    514 	if ($f == "RUMP") {
    515 		rumpable = 1
    516 		f++
    517 	} else {
    518 		rumpable = 0
    519 	}
    520 	if ($f ~ /^[a-z0-9_]*$/) {	# allow syscall alias
    521 		funcalias=$f
    522 		f++
    523 	}
    524 	if ($f != "{")
    525 		parserr($f, "{")
    526 	f++
    527 	if ($end != "}")
    528 		parserr($end, "}")
    529 	end--
    530 	if ($end != ";")
    531 		parserr($end, ";")
    532 	end--
    533 	if ($end != ")")
    534 		parserr($end, ")")
    535 	end--
    536 
    537 	returntype = oldf = "";
    538 	do {
    539 		if (returntype != "" && oldf != "*")
    540 			returntype = returntype" ";
    541 		returntype = returntype$f;
    542 		oldf = $f;
    543 		f++
    544 	} while ($f != "|" && f < (end-1))
    545 	if (f == (end - 1)) {
    546 		parserr($f, "function argument definition (maybe \"|\"?)");
    547 	}
    548 	f++
    549 
    550 	fprefix=$f
    551 	f++
    552 	if ($f != "|") {
    553 		parserr($f, "function compat delimiter (maybe \"|\"?)");
    554 	}
    555 	f++
    556 
    557 	fcompat=""
    558 	if ($f != "|") {
    559 		fcompat=$f
    560 		f++
    561 	}
    562 
    563 	if ($f != "|") {
    564 		parserr($f, "function name delimiter (maybe \"|\"?)");
    565 	}
    566 	f++
    567 	fbase=$f
    568 
    569 	# pipe is special in how to returns its values.
    570 	# So just generate it manually if present.
    571 	if (rumpable == 1 && fbase == "pipe") {
    572 		rumpable = 0;
    573 		rumphaspipe = 1;
    574 	}
    575 
    576 	if (fcompat != "") {
    577 		funcname=fprefix "___" fbase "" fcompat
    578 	} else {
    579 		funcname=fprefix "_" fbase
    580 	}
    581 	if (isarg64(returntype)) {
    582 		if (sycall_flags == "0")
    583 			sycall_flags = "SYCALL_RET_64";
    584 		else
    585 			sycall_flags = "SYCALL_RET_64 | " sycall_flags;
    586 	}
    587 
    588 	if (funcalias == "") {
    589 		funcalias=funcname
    590 		sub(/^([^_]+_)*sys_/, "", funcalias)
    591 		realname=fbase
    592 	} else {
    593 		realname=funcalias
    594 	}
    595 	rumpfname=realname "" fcompat
    596 	f++
    597 
    598 	if ($f != "(")
    599 		parserr($f, "(")
    600 	f++
    601 
    602 	argc=0;
    603 	argalign=0;
    604 	if (f == end) {
    605 		if ($f != "void")
    606 			parserr($f, "argument definition")
    607 		isvarargs = 0;
    608 		varargc = 0;
    609 		argtype[0]="void";
    610 		return
    611 	}
    612 
    613 	# some system calls (open() and fcntl()) can accept a variable
    614 	# number of arguments.  If syscalls accept a variable number of
    615 	# arguments, they must still have arguments specified for
    616 	# the remaining argument "positions," because of the way the
    617 	# kernel system call argument handling works.
    618 	#
    619 	# Indirect system calls, e.g. syscall(), are exceptions to this
    620 	# rule, since they are handled entirely by machine-dependent code
    621 	# and do not need argument structures built.
    622 
    623 	isvarargs = 0;
    624 	args64 = 0;
    625 	ptr = 0;
    626 	while (f <= end) {
    627 		if ($f == "...") {
    628 			f++;
    629 			isvarargs = 1;
    630 			varargc = argc;
    631 			continue;
    632 		}
    633 		argc++
    634 		argtype[argc]=""
    635 		oldf=""
    636 		while (f < end && $(f+1) != ",") {
    637 			if (argtype[argc] != "" && oldf != "*")
    638 				argtype[argc] = argtype[argc]" ";
    639 			argtype[argc] = argtype[argc]$f;
    640 			oldf = $f;
    641 			f++
    642 		}
    643 		if (argtype[argc] == "")
    644 			parserr($f, "argument definition")
    645 		if (argtype[argc] == "off_t"  \
    646 		  || argtype[argc] == "dev_t" \
    647 		  || argtype[argc] == "time_t") {
    648 			if ((argalign % 2) != 0 && sysalign &&
    649 			    funcname != "sys_posix_fadvise") # XXX for now
    650 				parserr($f, "a padding argument")
    651 		} else {
    652 			argalign++;
    653 		}
    654 		if (isarg64(argtype[argc])) {
    655 			if (sycall_flags == "0")
    656 				sycall_flags = "SYCALL_ARG"argc-1"_64";
    657 			else
    658 				sycall_flags = "SYCALL_ARG"argc-1"_64 | " sycall_flags;
    659 			args64++;
    660 		}
    661 		if (index(argtype[argc], "*") != 0 && ptr == 0) {
    662 			if (sycall_flags == "0")
    663 				sycall_flags = "SYCALL_ARG_PTR";
    664 			else
    665 				sycall_flags = "SYCALL_ARG_PTR | " sycall_flags;
    666 			ptr = 1;
    667 		}
    668 		argname[argc]=$f;
    669 		f += 2;			# skip name, and any comma
    670 	}
    671 	if (args64 > 0)
    672 		sycall_flags = "SYCALL_NARGS64_VAL("args64") | " sycall_flags;
    673 	# must see another argument after varargs notice.
    674 	if (isvarargs) {
    675 		if (argc == varargc)
    676 			parserr($f, "argument definition")
    677 	} else
    678 		varargc = argc;
    679 }
    680 
    681 function printproto(wrap) {
    682 	printf("/* syscall: \"%s%s\" ret: \"%s\" args:", wrap, funcalias,
    683 	    returntype) > sysnumhdr
    684 	for (i = 1; i <= varargc; i++)
    685 		printf(" \"%s\"", argtype[i]) > sysnumhdr
    686 	if (isvarargs)
    687 		printf(" \"...\"") > sysnumhdr
    688 	printf(" */\n") > sysnumhdr
    689 	printf("#define\t%s%s%s\t%d\n\n", constprefix, wrap, funcalias,
    690 	    syscall) > sysnumhdr
    691 
    692 	# output entry for syscall autoload table, if modular
    693 	if (modular ) {
    694 		printf("\t    { %s%s%s, \"%s\" },\n", constprefix, wrap,
    695 		    funcalias, modname) > sysautoload
    696 	}
    697 
    698 
    699 	# rumpalooza
    700 	if (!rumpable)
    701 		return
    702 
    703 	# accumulate fbases we have seen.  we want the last
    704 	# occurence for the default __RENAME()
    705 	seen = funcseen[fbase]
    706 	funcseen[fbase] = rumpfname
    707 	# special case for mknod as type of last argument changed from
    708 	# uint32_t to dev_t
    709 	if ((seen && fbase != "mknod") || (!seen && fbase == "mknod"))
    710 		return
    711 
    712 	printf("%s rump_sys_%s(", returntype, realname) > rumpprotos
    713 
    714 	for (i = 1; i < varargc; i++)
    715 		if (argname[i] != "PAD")
    716 			printf("%s, ", uncompattype(argtype[i])) > rumpprotos
    717 
    718 	if (isvarargs)
    719 		printf("%s, ...)", uncompattype(argtype[varargc]))>rumpprotos
    720 	else
    721 		printf("%s)", uncompattype(argtype[argc])) > rumpprotos
    722 
    723 	printf(" __RENAME(RUMP_SYS_RENAME_%s)", toupper(fbase))> rumpprotos
    724 	printf(";\n") > rumpprotos
    725 
    726 	# generate forward-declares for types, apart from the
    727 	# braindead typedef jungle we cannot easily handle here
    728 	for (i = 1; i <= varargc; i++) {
    729 		type=uncompattype(argtype[i])
    730 		sub("const ", "", type)
    731 		ntype=type
    732 		sub(" *\\*.*", "", ntype);
    733 		if (!typeseen[ntype] && \
    734 		    match(type, "struct") && match(type, "\\*")) {
    735 			typeseen[ntype] = 1
    736 			printf("%s;\n", ntype) > rumptypes
    737 		}
    738 	}
    739 }
    740 
    741 function printrumpsysent(insysent, compatwrap) {
    742 	if (modular) {
    743 		fn = rumpnomodule
    744 		flags = rumpnoflags
    745 	} else {
    746 		fn = rumpnosys
    747 		flags = ""
    748 	}
    749 	if (!insysent) {
    750 		printf("\t{%s\n\t\t.sy_call = %s,\n},\t\t/* %d = %s */\n", \
    751 		    flags, fn, syscall, funcalias) > rumpsysent
    752 		return
    753 	}
    754 
    755 	printf("\t{") > rumpsysent
    756 	if (argc != 0) {
    757 		printf("\n\t\tns(struct %ssys_%s_args),", compatwrap_, funcalias) > rumpsysent
    758 	}
    759 
    760 	printf("\n\t\t.sy_call = %s,\n\t},", fn) > rumpsysent
    761 	printf("\t\t/* %d = %s%s */\n", syscall, compatwrap_, funcalias) > rumpsysent
    762 }
    763 
    764 function iscompattype(type) {
    765 	for (var in uncompattypes) {
    766 		if (match(type, var)) {
    767 			return 1
    768 		}
    769 	}
    770 
    771 	return 0
    772 }
    773 
    774 function uncompattype(type) {
    775 	for (var in uncompattypes) {
    776 		if (match(type, var)) {
    777 			sub(var, uncompattypes[var], type)
    778 			return type
    779 		}
    780 	}
    781 
    782 	return type
    783 }
    784 
    785 function printrumpsysmap(syscall, wfn, funcalias, rumpentry) {
    786 	printf("%-4d %-22s %-18s %s\n",
    787 	    syscall, wfn, funcalias, rumpentry) > rumpsysmap
    788 }
    789 
    790 function putsystrace(type, compatwrap_) {
    791 	printf("\t/* %s */\n\tcase %d: {\n", funcname, syscall) > systrace
    792 	printf("\t/* %s */\n\tcase %d:\n", funcname, syscall) > systracetmp
    793 	printf("\t/* %s */\n\tcase %d:\n", funcname, syscall) > systraceret
    794 	if (argc > 0) {
    795 		printf("\t\tswitch(ndx) {\n") > systracetmp
    796 		printf("\t\tconst struct %s%s_args *p = params;\n", compatwrap_, funcname) > systrace
    797 		for (i = 1; i <= argc; i++) {
    798 			arg = argtype[i]
    799 			sub("__restrict$", "", arg)
    800 			printf("\t\tcase %d:\n\t\t\tp = \"%s\";\n\t\t\tbreak;\n", i - 1, arg) > systracetmp
    801 			if (arg ~ /.*p_t$/ || arg ~ /.*p$/ || arg ~ /.*_t_p$/ ||
    802 			    arg ~ /.*_pointer_t$/)
    803 				printf("\t\tuarg[%d] = (intptr_t) SCARG(p, %s).i32; /* %s */\n", \
    804 				     i - 1, \
    805 				     argname[i], arg) > systrace
    806 			else if (index(arg, "*") > 0 || arg == "caddr_t")
    807 				printf("\t\tuarg[%d] = (intptr_t) SCARG(p, %s); /* %s */\n", \
    808 				     i - 1, \
    809 				     argname[i], arg) > systrace
    810 			else if (substr(arg, 1, 1) == "u" || arg == "size_t")
    811 				printf("\t\tuarg[%d] = SCARG(p, %s); /* %s */\n", \
    812 				     i - 1, \
    813 				     argname[i], arg) > systrace
    814 			else
    815 				printf("\t\tiarg[%d] = SCARG(p, %s); /* %s */\n", \
    816 				     i - 1, \
    817 				     argname[i], arg) > systrace
    818 		}
    819 		printf("\t\tdefault:\n\t\t\tbreak;\n\t\t};\n") > systracetmp
    820 
    821 		printf("\t\tif (ndx == 0 || ndx == 1)\n") > systraceret
    822 		printf("\t\t\tp = \"%s\";\n", returntype) > systraceret
    823 		printf("\t\tbreak;\n") > systraceret
    824 	}
    825 	printf("\t\t*n_args = %d;\n\t\tbreak;\n\t}\n", argc) > systrace
    826 	printf("\t\tbreak;\n") > systracetmp
    827 }
    828 
    829 function putent(type, compatwrap) {
    830 	# output syscall declaration for switch table.
    831 	if (compatwrap == "")
    832 		compatwrap_ = ""
    833 	else
    834 		compatwrap_ = compatwrap "_"
    835 	if (argc == 0)
    836 		arg_type = "void";
    837 	else {
    838 		arg_type = "struct " compatwrap_ funcname "_args";
    839 	}
    840 	putsystrace(type, compatwrap_)
    841 	proto = "int\t" compatwrap_ funcname "(struct lwp *, const " \
    842 	    arg_type " *, register_t *);\n"
    843 	if (sysmap[proto] != 1) {
    844 		sysmap[proto] = 1;
    845 		print proto > sysprotos;
    846 	}
    847 
    848 	# output syscall switch entry
    849 	printf("\t{") > sysent
    850 	if (argc != 0) {
    851 		printf("\n\t\tns(struct %s%s_args),", compatwrap_, funcname) > sysent
    852 	}
    853 	if (modular) 
    854 		wfn = "sys_nomodule";
    855 	else if (compatwrap == "")
    856 		wfn = funcname;
    857 	else
    858 		wfn = compatwrap "(" funcname ")";
    859 	wfn_cast="(sy_call_t *)" wfn
    860 	if (sycall_flags != "0")
    861 		flags = "\n\t\t.sy_flags = " sycall_flags ","
    862 	else
    863 		flags = ""
    864 	printf("%s\n\t\t.sy_call = %s\n\t},", flags, wfn_cast) > sysent
    865 	printf("\t\t/* %d = %s%s */\n", syscall, compatwrap_, funcalias) > sysent
    866 
    867 	# output syscall name for names table
    868 	printf("\t/* %3d */\t\"%s%s\",\n", syscall, compatwrap_, funcalias) \
    869 	    > sysnamesbottom
    870 	if (compatwrap_ != "" || fbase == funcalias)
    871 		printf("\t/* %3d */\tNULL, /* %s%s */\n", syscall, \
    872 		    compatwrap_, funcalias) > sysnamesfriendly
    873 	else
    874 		printf("\t/* %3d */\t\"%s%s\",\n", syscall, compatwrap_, \
    875 		    fbase) > sysnamesfriendly
    876 
    877 	# output syscall number of header, if appropriate
    878 	if (type == "STD" || type == "NOARGS" || type == "INDIR" || \
    879 	    type == "NOERR") {
    880 		# output a prototype, to be used to generate lint stubs in
    881 		# libc.
    882 		printproto("")
    883 	} else if (type == "COMPAT" || type == "EXTERN") {
    884 		# Just define the syscall number with a comment.  These
    885 		# may be used by compatibility stubs in libc.
    886 		printproto(compatwrap_)
    887 	}
    888 
    889 	# output syscall argument structure, if it has arguments
    890 	if (argc != 0) {
    891 		printf("\n") > sysarghdr
    892 		if (haverumpcalls && !rumpable)
    893 			printf("#ifndef RUMP_CLIENT\n") > sysarghdr
    894 		printf("struct %s%s_args", compatwrap_, funcname) > sysarghdr
    895 		if (type != "NOARGS") {
    896 			print " {" >sysarghdr;
    897 			for (i = 1; i <= argc; i++)
    898 				printf("\tsyscallarg(%s) %s;\n", argtype[i],
    899 				    argname[i]) > sysarghdr
    900 			printf "}" >sysarghdr;
    901 		}
    902 		printf(";\n") > sysarghdr
    903 		if (type != "NOARGS" && type != "INDIR") {
    904 			printf("check_syscall_args(%s%s)\n", compatwrap_,
    905 			    funcname) >sysarghdr
    906 		}
    907 		if (haverumpcalls && !rumpable)
    908 			printf("#endif /* !RUMP_CLIENT */\n") > sysarghdr
    909 	}
    910 
    911 	if (!rumpable) {
    912 		if (funcname == "sys_pipe" && rumphaspipe == 1) {
    913 			insysent = 1
    914 			printrumpsysmap(syscall,
    915 			    funcname, funcalias, "rump_sys_pipe")
    916 		} else {
    917 			insysent = 0
    918 		}
    919 	} else {
    920 		insysent = 1
    921 	}
    922 	printrumpsysent(insysent, compatwrap)
    923 
    924 	# output rump marshalling code if necessary
    925 	if (!rumpable) {
    926 		return
    927 	}
    928 
    929 	printrumpsysmap(syscall, wfn, funcalias, "rump___sysimpl_" rumpfname)
    930 
    931 	printf("\n") > rumpcalls
    932 
    933 	if (compatwrap)
    934 		printf("#ifdef RUMP_SYS_COMPAT\n") > rumpcalls
    935 
    936 	# need a local prototype, we export the re-re-named one in .h
    937 	printf("%s rump___sysimpl_%s(", returntype, rumpfname) \
    938 	    > rumpcalls
    939 	for (i = 1; i < argc; i++) {
    940 		if (argname[i] != "PAD")
    941 			printf("%s, ", uncompattype(argtype[i])) > rumpcalls
    942 	}
    943 	printf("%s);", uncompattype(argtype[argc])) > rumpcalls
    944 
    945 	printf("\n%s\nrump___sysimpl_%s(", returntype, rumpfname) > rumpcalls
    946 	for (i = 1; i < argc; i++) {
    947 		if (argname[i] != "PAD")
    948 			printf("%s %s, ", uncompattype(argtype[i]), \
    949 			    argname[i]) > rumpcalls
    950 	}
    951 	printf("%s %s)\n", uncompattype(argtype[argc]), argname[argc]) \
    952 	    > rumpcalls
    953 	printf("{\n\tregister_t retval[2];\n") > rumpcalls
    954 	if (returntype != "void") {
    955 		if (type != "NOERR") {
    956 			printf("\tint error = 0;\n") > rumpcalls
    957 		}
    958 		# assume rumpcalls return only integral types
    959 		printf("\t%s rv = -1;\n", returntype) > rumpcalls
    960 	}
    961 
    962 	argarg = "NULL"
    963 	argsize = 0;
    964 	if (argc) {
    965 		argarg = "&callarg"
    966 		argsize = "sizeof(callarg)"
    967 		printf("\tstruct %s%s_args callarg;\n\n",compatwrap_,funcname) \
    968 		    > rumpcalls
    969 		printf "\tmemset(&callarg, 0, sizeof(callarg));\n" > rumpcalls
    970 		for (i = 1; i <= argc; i++) {
    971 			if (argname[i] == "PAD") {
    972 				printf("\tSPARG(&callarg, %s) = 0;\n", \
    973 				    argname[i]) > rumpcalls
    974 			} else {
    975 				if (iscompattype(argtype[i])) {
    976 					printf("\tSPARG(&callarg, %s) = "    \
    977 					"(%s)%s;\n", argname[i], argtype[i], \
    978 					argname[i]) > rumpcalls
    979 				} else {
    980 					printf("\tSPARG(&callarg, %s) = %s;\n",\
    981 					    argname[i], argname[i]) > rumpcalls
    982 				}
    983 			}
    984 		}
    985 		printf("\n") > rumpcalls
    986 	} else {
    987 		printf("\n") > rumpcalls
    988 	}
    989 	printf("\t") > rumpcalls
    990 	if (returntype != "void" && type != "NOERR")
    991 		printf("error = ") > rumpcalls
    992 	else if (returntype != "void")
    993 		printf("(void)") > rumpcalls
    994 	printf("rsys_syscall(%s%s%s, " \
    995 	    "%s, %s, retval);\n", constprefix, compatwrap_, funcalias, \
    996 	    argarg, argsize) > rumpcalls
    997 	if (type != "NOERR") {
    998 		printf("\trsys_seterrno(error);\n") > rumpcalls
    999 		printf("\tif (error == 0) {\n") > rumpcalls
   1000 		indent="\t\t"
   1001 		ending="\t}\n"
   1002 	} else {
   1003 		indent="\t"
   1004 		ending=""
   1005 	}
   1006 	if (returntype != "void") {
   1007 		printf("%sif (sizeof(%s) > sizeof(register_t))\n", \
   1008 		    indent, returntype) > rumpcalls
   1009 		printf("%s\trv = *(%s *)retval;\n", \
   1010 		    indent, returntype) > rumpcalls
   1011 		printf("%selse\n", indent, indent) > rumpcalls
   1012 		printf("%s\trv = *retval;\n", indent, returntype) > rumpcalls
   1013 		printf("%s", ending) > rumpcalls
   1014 		printf("\treturn rv;\n") > rumpcalls
   1015 	}
   1016 	printf("}\n") > rumpcalls
   1017 
   1018 	printf("#ifdef RUMP_KERNEL_IS_LIBC\n") > rumpcalls
   1019 
   1020 	# create the bog-standard, non-renamed public name.
   1021 	# this way we get e.g. select instead of just __select50
   1022 	if (fcompat)
   1023 		printf("__weak_alias(%s,rump___sysimpl_%s);\n", \
   1024 		    fbase, rumpfname) > rumpcalls
   1025 
   1026 	printf("__weak_alias(%s,rump___sysimpl_%s);\n", \
   1027 	    funcalias, rumpfname) > rumpcalls
   1028 	printf("__weak_alias(_%s,rump___sysimpl_%s);\n", \
   1029 	    funcalias, rumpfname) > rumpcalls
   1030 	printf("__strong_alias(_sys_%s,rump___sysimpl_%s);\n", \
   1031 	    funcalias, rumpfname) >rumpcalls
   1032 
   1033 	printf("#endif /* RUMP_KERNEL_IS_LIBC */\n") > rumpcalls
   1034 
   1035 	if (compatwrap)
   1036 		printf("#endif /* RUMP_SYS_COMPAT */\n") > rumpcalls
   1037 
   1038 }
   1039 $2 == "STD" || $2 == "NODEF" || $2 == "NOARGS" || $2 == "INDIR" \
   1040     || $2 == "NOERR" {
   1041 	parseline()
   1042 	putent($2, "")
   1043 	syscall++
   1044 	next
   1045 }
   1046 $2 == "OBSOL" || $2 == "UNIMPL" || $2 == "EXCL" || $2 == "IGNORED" {
   1047 	if ($2 == "OBSOL")
   1048 		comment="obsolete"
   1049 	else if ($2 == "EXCL")
   1050 		comment="excluded"
   1051 	else if ($2 == "IGNORED")
   1052 		comment="ignored"
   1053 	else
   1054 		comment="unimplemented"
   1055 	for (i = 3; i <= NF; i++)
   1056 		comment=comment " " $i
   1057 
   1058 	if ($2 == "IGNORED")
   1059 		sys_stub = "(sy_call_t *)nullop";
   1060 	else
   1061 		sys_stub = sys_nosys;
   1062 
   1063 	print fillerpsysent(syscall, "", sys_stub, comment) > sysent
   1064 	print fillerpsysent(syscall, rumpnoflags, rumpnosys, comment) > rumpsysent
   1065 	printf("\t/* %3d */\t\"#%d (%s)\",\n", syscall, syscall, comment) \
   1066 	    > sysnamesbottom
   1067 	printf("\t/* %3d */\tNULL, /* %s */\n", syscall, comment) \
   1068 	    > sysnamesfriendly
   1069 	if ($2 != "UNIMPL")
   1070 		printf("\t\t\t\t/* %d is %s */\n", syscall, comment) > sysnumhdr
   1071 	syscall++
   1072 	next
   1073 }
   1074 $2 == "EXTERN" {
   1075 	parseline()
   1076 	putent("EXTERN", "")
   1077 	syscall++
   1078 	next
   1079 }
   1080 {
   1081 	for (i = 1; i <= ncompat; i++) {
   1082 		if ($2 == compat_upper[i]) {
   1083 			parseline();
   1084 			putent("COMPAT", compat[i])
   1085 			syscall++
   1086 			next
   1087 		}
   1088 	}
   1089 	printf("%s: line %d: unrecognized keyword %s\n", infile, NR, $2)
   1090 	exit 1
   1091 }
   1092 END {
   1093 	# output pipe() syscall with its special retval[2] handling
   1094 	if (rumphaspipe) {
   1095 		printf("int rump_sys_pipe(int *);\n") > rumpprotos
   1096 		printf("\nint rump_sys_pipe(int *);\n") > rumpcalls
   1097 		printf("int\nrump_sys_pipe(int *fd)\n{\n") > rumpcalls
   1098 		printf("\tregister_t retval[2];\n") > rumpcalls
   1099 		printf("\tint error = 0;\n") > rumpcalls
   1100 		printf("\n\terror = rsys_syscall(SYS_pipe, ") > rumpcalls
   1101 		printf("NULL, 0, retval);\n") > rumpcalls
   1102 		printf("\tif (error) {\n") > rumpcalls
   1103 		printf("\t\trsys_seterrno(error);\n") > rumpcalls
   1104 		printf("\t} else {\n\t\tfd[0] = retval[0];\n") > rumpcalls
   1105 		printf("\t\tfd[1] = retval[1];\n\t}\n") > rumpcalls
   1106 		printf("\treturn error ? -1 : 0;\n}\n") > rumpcalls
   1107 		printf("#ifdef RUMP_KERNEL_IS_LIBC\n") > rumpcalls
   1108 		printf("__weak_alias(pipe,rump_sys_pipe);\n") > rumpcalls
   1109 		printf("__weak_alias(_pipe,rump_sys_pipe);\n") > rumpcalls
   1110 		printf("__strong_alias(_sys_pipe,rump_sys_pipe);\n") > rumpcalls
   1111 		printf("#endif\n") > rumpcalls
   1112 	}
   1113 
   1114 	# print default rump syscall interfaces
   1115 	for (var in funcseen) {
   1116 		printf("#ifndef RUMP_SYS_RENAME_%s\n", \
   1117 		    toupper(var)) > rumpcallshdr
   1118 		printf("#define RUMP_SYS_RENAME_%s rump___sysimpl_%s\n", \
   1119 		    toupper(var), funcseen[var]) > rumpcallshdr
   1120 		printf("#endif\n\n") > rumpcallshdr
   1121 	}
   1122 
   1123 	maxsyscall = syscall
   1124 	if (nsysent) {
   1125 		if (syscall > nsysent) {
   1126 			printf("%s: line %d: too many syscalls [%d > %d]\n", infile, NR, syscall, nsysent)
   1127 			exit 1
   1128 		}
   1129 		while (syscall < nsysent) {
   1130 			print fillerpsysent(syscall, "", sys_nosys, "filler") > sysent
   1131 			print fillerpsysent(syscall, rumpnoflags, rumpnosys, "filler") > rumpsysent
   1132 			printf("\t/* %3d */\t\"# filler\",\n", syscall) \
   1133 			    > sysnamesbottom
   1134 			printf("\t/* %3d */\tNULL, /* filler */\n", syscall) \
   1135 			    > sysnamesfriendly
   1136 			syscall++
   1137 		}
   1138 	}
   1139 	printf("};\n") > sysent
   1140 	printf("};\n") > rumpsysent
   1141 	printf("CTASSERT(__arraycount(rump_sysent) == SYS_NSYSENT);\n") > rumpsysent
   1142 	printf("__strong_alias(rumpns_sysent,rump_sysent);\n") > rumpsysent
   1143 	printf("#endif /* RUMP_CLIENT */\n") > rumpsysent
   1144 	if (haverumpcalls)
   1145 		printf("#endif /* !RUMP_CLIENT */\n") > sysprotos
   1146 	printf("};\n") > sysnamesbottom
   1147 	printf("};\n") > sysnamesfriendly
   1148 	printf("#define\t%sMAXSYSCALL\t%d\n", constprefix, maxsyscall) > sysnumhdr
   1149 	if (nsysent)
   1150 		printf("#define\t%sNSYSENT\t%d\n", constprefix, nsysent) > sysnumhdr
   1151 	printf "\tdefault:\n\t\t*n_args = 0;\n\t\tbreak;\n\t};\n}\n" > systrace
   1152 	printf "\tdefault:\n\t\tbreak;\n\t};\n\tif (p != NULL)\n\t\tstrlcpy(desc, p, descsz);\n}\n" > systracetmp
   1153 	printf "\tdefault:\n\t\tbreak;\n\t};\n\tif (p != NULL)\n\t\tstrlcpy(desc, p, descsz);\n}\n" > systraceret
   1154 } '
   1155 
   1156 cat $sysprotos >> $sysarghdr
   1157 echo "#endif /* _${constprefix}SYSCALL_H_ */" >> $sysnumhdr
   1158 echo "#endif /* _${constprefix}SYSCALLARGS_H_ */" >> $sysarghdr
   1159 printf "\t    { 0, NULL }\n" >> $sysautoload
   1160 echo "};" >> $sysautoload
   1161 printf "\n#endif /* _RUMP_RUMP_SYSCALLS_H_ */\n" >> $rumpprotos
   1162 cat $sysdcl $sysent > $syssw
   1163 cat $sysnamesbottom >> $sysnames
   1164 cat $sysnamesfriendly >> $sysnames
   1165 cat $rumpsysent >> $rumpcalls
   1166 
   1167 touch $rumptypes
   1168 cat $rumptypes >> $rumpcallshdr
   1169 echo >> $rumpcallshdr
   1170 cat $rumpprotos >> $rumpcallshdr
   1171 
   1172 #chmod 444 $sysnames $sysnumhdr $syssw
   1173 
   1174 cat $systracetmp >> $systrace
   1175 cat $systraceret >> $systrace
   1176 
   1177 echo Generated following files:
   1178 echo $sysarghdr $sysnumhdr $syssw $sysnames $sysautoload $systrace $rumpcalls $rumpcallshdr $rumpsysmap
   1179