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