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