Home | History | Annotate | Line # | Download | only in kern
makesyscalls.sh revision 1.16
      1 #! /bin/sh -
      2 #	$NetBSD: makesyscalls.sh,v 1.16 1995/09/19 21:28:56 thorpej 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 sysprotos="sys.protos"
     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 $sysprotos $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 	sysprotos = \"$sysprotos\"
    118 	sysnumhdr = \"$sysnumhdr\"
    119 	sysarghdr = \"$sysarghdr\"
    120 	switchname = \"$switchname\"
    121 	namesname = \"$namesname\"
    122 	constprefix = \"$constprefix\"
    123 
    124 	sysdcl = \"$sysdcl\"
    125 	syscompat_pref = \"$syscompat_pref\"
    126 	sysent = \"$sysent\"
    127 	infile = \"$2\"
    128 
    129 	compatopts = \"$compatopts\"
    130 	"'
    131 
    132 	printf "/*\n * System call switch table.\n *\n" > sysdcl
    133 	printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysdcl
    134 
    135 	ncompat = split(compatopts,compat)
    136 	for (i = 1; i <= ncompat; i++) {
    137 		compat_upper[i] = toupper(compat[i])
    138 		compat_file[i] = sprintf("%s%s", syscompat_pref, compat[i])
    139 
    140 		printf "\n#ifdef %s\n", compat_upper[i] > compat_file[i]
    141 		printf "#define %s(func) __CONCAT(%s_,func)\n\n", \
    142 		    compat[i], compat[i] > compat_file[i]
    143 	}
    144 
    145 	printf "/*\n * System call names.\n *\n" > sysnames
    146 	printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysnames
    147 
    148 	printf "\n/*\n * System call prototypes.\n */\n\n" > sysprotos
    149 
    150 	printf "/*\n * System call numbers.\n *\n" > sysnumhdr
    151 	printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysnumhdr
    152 
    153 	printf "/*\n * System call argument lists.\n *\n" > sysarghdr
    154 	printf " * DO NOT EDIT-- this file is automatically generated.\n" > sysarghdr
    155 }
    156 NR == 1 {
    157 	printf " * created from%s\n */\n\n", $0 > sysdcl
    158 
    159 	printf "#define\ts(type)\tsizeof(type)\n\n" > sysent
    160 	printf "struct sysent %s[] = {\n",switchname > sysent
    161 
    162 	printf " * created from%s\n */\n\n", $0 > sysnames
    163 	printf "char *%s[] = {\n",namesname > sysnames
    164 
    165 	printf " * created from%s\n */\n\n", $0 > sysnumhdr
    166 
    167 	printf " * created from%s\n */\n\n", $0 > sysarghdr
    168 	printf "#define\tsyscallarg(x)\tunion { x datum; register_t pad; }\n" \
    169 		> sysarghdr
    170 	next
    171 }
    172 NF == 0 || $1 ~ /^;/ {
    173 	next
    174 }
    175 $1 ~ /^#[ 	]*include/ {
    176 	print > sysdcl
    177 	next
    178 }
    179 $1 ~ /^#[ 	]*if/ {
    180 	print > sysent
    181 	print > sysprotos
    182 	for (i = 1; i <= ncompat; i++)
    183 		print > compat_file[i]
    184 	print > sysnames
    185 	savesyscall = syscall
    186 	next
    187 }
    188 $1 ~ /^#[ 	]*else/ {
    189 	print > sysent
    190 	print > sysprotos
    191 	for (i = 1; i <= ncompat; i++)
    192 		print > compat_file[i]
    193 	print > sysnames
    194 	syscall = savesyscall
    195 	next
    196 }
    197 $1 ~ /^#/ {
    198 	print > sysent
    199 	print > sysprotos
    200 	for (i = 1; i <= ncompat; i++)
    201 		print > compat_file[i]
    202 	print > sysnames
    203 	next
    204 }
    205 syscall != $1 {
    206 	printf "%s: line %d: syscall number out of sync at %d\n", \
    207 	   infile, NR, syscall
    208 	printf "line is:\n"
    209 	print
    210 	exit 1
    211 }
    212 function parserr(was, wanted) {
    213 	printf "%s: line %d: unexpected %s (expected %s)\n", \
    214 	    infile, NR, was, wanted
    215 	exit 1
    216 }
    217 function parseline() {
    218 	f=3			# toss number and type
    219 	if ($NF != "}") {
    220 		funcalias=$NF
    221 		end=NF-1
    222 	} else {
    223 		funcalias=""
    224 		end=NF
    225 	}
    226 	if ($f != "{")
    227 		parserr($f, "{")
    228 	f++
    229 	if ($end != "}")
    230 		parserr($end, "}")
    231 	end--
    232 	if ($end != ";")
    233 		parserr($end, ";")
    234 	end--
    235 	if ($end != ")")
    236 		parserr($end, ")")
    237 	end--
    238 
    239 	f++			# toss return type
    240 
    241 	funcname=$f
    242 	if (funcalias == "")
    243 		funcalias=funcname
    244 	f++
    245 
    246 	if ($f != "(")
    247 		parserr($f, ")")
    248 	f++
    249 
    250 	argc= 0;
    251 	if (f == end) {
    252 		if ($f != "void")
    253 			parserr($f, "argument definition")
    254 		return
    255 	}
    256 
    257 	while (f <= end) {
    258 		argc++
    259 		argtype[argc]=""
    260 		oldf=""
    261 		while (f < end && $(f+1) != ",") {
    262 			if (argtype[argc] != "" && oldf != "*")
    263 				argtype[argc] = argtype[argc]" ";
    264 			argtype[argc] = argtype[argc]$f;
    265 			oldf = $f;
    266 			f++
    267 		}
    268 		if (argtype[argc] == "")
    269 			parserr($f, "argument definition")
    270 		argname[argc]=$f;
    271 		f += 2;			# skip name, and any comma
    272 	}
    273 }
    274 function putent(nodefs, declfile, compatwrap) {
    275 	# output syscall declaration for switch table
    276 	prototype = "__P((struct proc *, void *, register_t *))"
    277 	if (compatwrap == "")
    278 		printf("int\t%s\t%s;\n", funcname, prototype) > declfile
    279 	else
    280 		printf("int\t%s(%s)\t%s;\n", compatwrap, funcname, prototype) > declfile
    281 
    282 	# output syscall switch entry
    283 #	printf("\t{ { %d", argc) > sysent
    284 #	for (i = 1; i <= argc; i++) {
    285 #		if (i == 5) 		# wrap the line
    286 #			printf(",\n\t    ") > sysent
    287 #		else
    288 #			printf(", ") > sysent
    289 #		printf("s(%s)", argtypenospc[i]) > sysent
    290 #	}
    291 	printf("\t{ %d, ", argc) > sysent
    292 	if (argc == 0)
    293 		printf("0") > sysent
    294 	else if (compatwrap == "")
    295 		printf("s(struct %s_args)", funcname) > sysent
    296 	else
    297 		printf("s(struct %s_%s_args)", compatwrap, funcname) > sysent
    298 	if (compatwrap == "")
    299 		wfn = sprintf("%s", funcname);
    300 	else
    301 		wfn = sprintf("%s(%s)", compatwrap, funcname);
    302 	printf(",\n\t    %s },", wfn) > sysent
    303 	for (i = 0; i < (33 - length(wfn)) / 8; i++)
    304 		printf("\t") > sysent
    305 	if (compatwrap == "")
    306 		printf("/* %d = %s */\n", syscall, funcalias) > sysent
    307 	else
    308 		printf("/* %d = %s %s */\n", syscall, compatwrap,
    309 		    funcalias) > sysent
    310 
    311 	# output syscall name for names table
    312 	if (compatwrap == "")
    313 		printf("\t\"%s\",\t\t\t/* %d = %s */\n", funcalias, syscall,
    314 		    funcalias) > sysnames
    315 	else
    316 		printf("\t\"%s_%s\",\t/* %d = %s %s */\n", compatwrap,
    317 		    funcalias, syscall, compatwrap, funcalias) > sysnames
    318 
    319 	# output syscall number of header, if appropriate
    320 	if (nodefs == "" || nodefs == "NOARGS")
    321 		printf("#define\t%s%s\t%d\n", constprefix, funcalias,
    322 		    syscall) > sysnumhdr
    323 	else if (nodefs != "NODEF")
    324 		printf("\t\t\t\t/* %d is %s %s */\n", syscall,
    325 		    compatwrap, funcalias) > sysnumhdr
    326 
    327 	# output syscall argument structure, if it has arguments
    328 	if (argc != 0 && nodefs != "NOARGS") {
    329 		if (compatwrap == "")
    330 			printf("\nstruct %s_args {\n", funcname) > sysarghdr
    331 		else
    332 			printf("\nstruct %s_%s_args {\n", compatwrap,
    333 			    funcname) > sysarghdr
    334 		for (i = 1; i <= argc; i++)
    335 			printf("\tsyscallarg(%s) %s;\n", argtype[i],
    336 			    argname[i]) > sysarghdr
    337 		printf("};\n") > sysarghdr
    338 	}
    339 }
    340 $2 == "STD" {
    341 	parseline()
    342 	putent("", sysprotos, "");
    343 	syscall++
    344 	next
    345 }
    346 $2 == "NODEF" || $2 == "NOARGS" {
    347 	parseline()
    348 	putent($2, sysprotos, "")
    349 	syscall++
    350 	next
    351 }
    352 $2 == "OBSOL" || $2 == "UNIMPL" {
    353 	if ($2 == "OBSOL")
    354 		comment="obsolete"
    355 	else
    356 		comment="unimplemented"
    357 	for (i = 3; i <= NF; i++)
    358 		comment=comment " " $i
    359 
    360 	printf("\t{ 0, 0,\n\t    nosys },\t\t\t\t/* %d = %s */\n", \
    361 	    syscall, comment) > sysent
    362 	printf("\t\"#%d (%s)\",\t\t/* %d = %s */\n", \
    363 	    syscall, comment, syscall, comment) > sysnames
    364 	if ($2 != "UNIMPL")
    365 		printf("\t\t\t\t/* %d is %s */\n", syscall, comment) > sysnumhdr
    366 	syscall++
    367 	next
    368 }
    369 {
    370 	for (i = 1; i <= ncompat; i++) {
    371 		if ($2 == compat_upper[i]) {
    372 			parseline();
    373 			putent("COMMENT", compat_file[i], compat[i])
    374 			syscall++
    375 			next
    376 		}
    377 	}
    378 	printf "%s: line %d: unrecognized keyword %s\n", infile, NR, $2
    379 	exit 1
    380 }
    381 END {
    382 	printf "\n#undef\tsyscallarg\n" > sysarghdr
    383 
    384         for (i = 1; i <= ncompat; i++) {
    385 		printf("\n#else /* %s */\n", compat_upper[i]) > compat_file[i]
    386 		printf("#define %s(func) nosys\n", compat[i]) > \
    387 		    compat_file[i]
    388 		printf("#endif /* %s */\n\n", compat_upper[i]) > compat_file[i]
    389         }
    390 
    391 	printf("};\n\n") > sysent
    392 	printf("};\n") > sysnames
    393 	printf("#define\t%sMAXSYSCALL\t%d\n", constprefix, syscall) > sysnumhdr
    394 } '
    395 
    396 cat $sysprotos $syscompat_files >> $sysarghdr
    397 cat $sysdcl $sysent > $syssw
    398 
    399 #chmod 444 $sysnames $sysnumhdr $syssw
    400