Home | History | Annotate | Line # | Download | only in librump
makerumpif.sh revision 1.7
      1 #!/bin/sh
      2 #
      3 #	$NetBSD: makerumpif.sh,v 1.7 2014/04/25 13:07:31 pooka Exp $
      4 #
      5 # Copyright (c) 2009 Antti Kantee.  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 #
     16 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17 # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18 # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19 # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20 # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21 # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22 # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23 # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25 # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26 #
     27 
     28 #
     29 # This reads a rump component interface description and creates:
     30 #  1: rump private prototypes for internal calls
     31 #  2: public prototypes for calls outside of rump
     32 #  3: public interface implementations which run the rump scheduler
     33 #     and call the private interface
     34 #
     35 
     36 usage ()
     37 {
     38 
     39 	echo "usage: $0 spec"
     40 	exit 1
     41 }
     42 
     43 boom ()
     44 {
     45 
     46 	echo $*
     47 	exit 1
     48 }
     49 
     50 unset TOPDIR
     51 if [ $# -eq 3 ]; then
     52 	if [ $1 = '-R' ]; then
     53 		TOPDIR=$2
     54 	else
     55 		usage
     56 	fi
     57 	shift; shift
     58 fi
     59 [ $# -ne 1 ] && usage
     60 
     61 MYDIR=`pwd`
     62 if [ -z "${TOPDIR}" ]; then
     63 	while [ ! -f Makefile.rump  ]; do
     64 		[ `pwd` = '/' ] && boom Could not find rump topdir.
     65 		cd ..
     66 	done
     67 	TOPDIR="`pwd`"
     68 fi
     69 cd ${MYDIR}
     70 
     71 sed -e '
     72 :again
     73 	/\\$/{
     74 		N
     75 		s/[ 	]*\\\n[ 	]*/ /
     76 		b again
     77 	}
     78 ' ${1} | awk -F\| -v topdir=${TOPDIR} '
     79 function fileheaders(file, srcstr)
     80 {
     81 	printf("/*\t$NetBSD: makerumpif.sh,v 1.7 2014/04/25 13:07:31 pooka Exp $\t*/\n\n") > file
     82 	printf("/*\n * Automatically generated.  DO NOT EDIT.\n") > file
     83 	genstr = "$NetBSD: makerumpif.sh,v 1.7 2014/04/25 13:07:31 pooka Exp $"
     84 	gsub("\\$", "", genstr)
     85 	printf(" * from: %s\n", srcstr) > file
     86 	printf(" * by:   %s\n", genstr) > file
     87 	printf(" */\n") > file
     88 }
     89 
     90 function die(str)
     91 {
     92 
     93 	print str
     94 	exit(1)
     95 }
     96 
     97 NR == 1 {
     98 	sub(";[^\\$]*\\$", "")
     99 	sub("\\$", "")
    100 	fromstr = $0
    101 	next
    102 }
    103 
    104 $1 == "NAME"{myname = $2;next}
    105 $1 == "PUBHDR"{pubhdr = topdir "/" $2;print pubhdr;next}
    106 $1 == "PRIVHDR"{privhdr = topdir "/" $2;print privhdr;next}
    107 $1 == "WRAPPERS"{gencalls = topdir "/" $2;print gencalls;next}
    108 
    109 /^;/{next}
    110 /\\$/{sub("\\\n", "");getline nextline;$0 = $0 nextline}
    111 /^[ \t]*$/{next}
    112 {
    113 	if (NF != 3 && NF != 4) {
    114 		die("error: unexpected number of fields\n")
    115 	}
    116 	if (NF == 4) {
    117 		if ($4 == "WEAK")
    118 			isweak = 1
    119 		else
    120 			die("error: unexpected fourth field");
    121 	} else {
    122 		isweak = 0
    123 	}
    124 	if (!myname)
    125 		die("name not specified");
    126 	if (!pubhdr)
    127 		die("public header not specified");
    128 	if (!privhdr)
    129 		die("private header not specified");
    130 	if (!gencalls)
    131 		die("wrapper file not specified");
    132 
    133 	if (!once) {
    134 		fileheaders(pubhdr, fromstr)
    135 		fileheaders(privhdr, fromstr)
    136 		fileheaders(gencalls, fromstr)
    137 		once = 1
    138 
    139 		pubfile = pubhdr
    140 		sub(".*/", "", pubfile)
    141 
    142 		privfile = privhdr
    143 		sub(".*/", "", privfile)
    144 
    145 		printf("\n") > pubhdr
    146 		printf("\n") > privhdr
    147 
    148 		printf("\n#include <sys/cdefs.h>\n") > gencalls
    149 		printf("#include <sys/systm.h>\n") > gencalls
    150 		printf("\n#include <rump/rump.h>\n") > gencalls
    151 		printf("#include <rump/%s>\n\n", pubfile) > gencalls
    152 		printf("#include \"rump_private.h\"\n", privfile) > gencalls
    153 		printf("#include \"%s\"\n\n", privfile) > gencalls
    154 		printf("void __dead rump_%s_unavailable(void);\n",	\
    155 		    myname) > gencalls
    156 		printf("void __dead\nrump_%s_unavailable(void)\n{\n",	\
    157 		    myname) > gencalls
    158 		printf("\n\tpanic(\"%s interface unavailable\");\n}\n",	\
    159 		    myname) > gencalls
    160 	}
    161 
    162 	funtype = $1
    163 	sub("[ \t]*$", "", funtype)
    164 	funname = $2
    165 	sub("[ \t]*$", "", funname)
    166 	funargs = $3
    167 	sub("[ \t]*$", "", funargs)
    168 
    169 	printf("%s rump_pub_%s(%s);\n", funtype, funname, funargs) > pubhdr
    170 	printf("%s rump_%s(%s);\n", funtype, funname, funargs) > privhdr
    171 	printf("typedef %s (*rump_%s_fn)(%s);\n",	\
    172 	    funtype, funname, funargs) > privhdr
    173 
    174 	if (funtype == "void")
    175 		voidret = 1
    176 	else
    177 		voidret = 0
    178 	if (funargs == "void")
    179 		voidarg = 1
    180 	else
    181 		voidarg = 0
    182 
    183 	printf("\n%s\nrump_pub_%s(", funtype, funname) > gencalls
    184 	if (!voidarg) {
    185 		narg = split(funargs, argv, ",")
    186 		for (i = 1; i <= narg; i++) {
    187 			sub(" *", "", argv[i])
    188 			if (match(argv[i], "\\*$") != 0)
    189 				printf("%sarg%d", argv[i], i) > gencalls
    190 			else
    191 				printf("%s arg%d", argv[i], i) > gencalls
    192 			if (i != narg)
    193 				printf(", ") > gencalls
    194 		}
    195 	} else {
    196 		narg = 0
    197 		printf("void") > gencalls
    198 	}
    199 	printf(")\n{\n") > gencalls
    200 
    201 	if (!voidret) {
    202 		printf("\t%s rv;\n", funtype) > gencalls
    203 	}
    204 	printf("\n\trump_schedule();\n\t") > gencalls
    205 	if (!voidret)
    206 		printf("rv = ") > gencalls
    207 	printf("rump_%s(", funname) > gencalls
    208 	for (i = 1; i <= narg; i++) {
    209 		printf("arg%i", i) > gencalls
    210 		if (i < narg)
    211 			printf(", ") > gencalls
    212 	}
    213 	printf(");\n\trump_unschedule();\n") > gencalls
    214 	if (!voidret)
    215 		printf("\n\treturn rv;\n") > gencalls
    216 	printf("}\n") > gencalls
    217 	if (isweak)
    218 		printf("__weak_alias(rump_%s,rump_%s_unavailable);\n", \
    219 		    funname, myname) > gencalls
    220 }'
    221