Home | History | Annotate | Line # | Download | only in librump
makerumpif.sh revision 1.2
      1 #!/bin/sh
      2 #
      3 #	$NetBSD: makerumpif.sh,v 1.2 2009/10/14 17:26:09 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 [ $# != 1 ] && usage
     51 
     52 MYDIR=`pwd`
     53 while [ ! -f Makefile.rump  ]; do
     54 	[ `pwd` = '/' ] && boom Could not find rump topdir.
     55 	cd ..
     56 done
     57 RUMPTOP="`pwd`"
     58 cd ${MYDIR}
     59 
     60 sed -e '
     61 :again
     62 	/\\$/{
     63 		N
     64 		s/[ 	]*\\\n[ 	]*/ /
     65 		b again
     66 	}
     67 ' ${1} | awk -F\| -v rumptop=${RUMPTOP} '
     68 function fileheaders(file, srcstr)
     69 {
     70 	printf("/*\t$NetBSD: makerumpif.sh,v 1.2 2009/10/14 17:26:09 pooka Exp $\t*/\n\n") > file
     71 	printf("/*\n * Automatically generated.  DO NOT EDIT.\n") > file
     72 	genstr = "$NetBSD: makerumpif.sh,v 1.2 2009/10/14 17:26:09 pooka Exp $"
     73 	gsub("\\$", "", genstr)
     74 	printf(" * from: %s\n", srcstr) > file
     75 	printf(" * by:   %s\n", genstr) > file
     76 	printf(" */\n") > file
     77 }
     78 
     79 function die(str)
     80 {
     81 
     82 	print str
     83 	exit(1)
     84 }
     85 
     86 NR == 1 {
     87 	sub(";[^\\$]*\\$", "")
     88 	sub("\\$", "")
     89 	fromstr = $0
     90 	next
     91 }
     92 
     93 $1 == "NAME"{myname = $2;next}
     94 $1 == "PUBHDR"{pubhdr = rumptop "/" $2;next}
     95 $1 == "PRIVHDR"{privhdr = rumptop "/" $2;next}
     96 $1 == "WRAPPERS"{gencalls = rumptop "/" $2;next}
     97 
     98 /^;/{next}
     99 /\\$/{sub("\\\n", "");getline nextline;$0 = $0 nextline}
    100 /^[ \t]*$/{next}
    101 {
    102 	if (NF != 3 && NF != 4) {
    103 		die("error: unexpected number of fields\n")
    104 	}
    105 	if (NF == 4) {
    106 		if ($4 == "WEAK")
    107 			isweak = 1
    108 		else
    109 			die("error: unexpected fourth field");
    110 	} else {
    111 		isweak = 0
    112 	}
    113 	if (!myname)
    114 		die("name not specified");
    115 	if (!pubhdr)
    116 		die("public header not specified");
    117 	if (!privhdr)
    118 		die("private header not specified");
    119 	if (!gencalls)
    120 		die("wrapper file not specified");
    121 
    122 	if (!once) {
    123 		fileheaders(pubhdr, fromstr)
    124 		fileheaders(privhdr, fromstr)
    125 		fileheaders(gencalls, fromstr)
    126 		once = 1
    127 
    128 		pubfile = pubhdr
    129 		sub(".*/", "", pubfile)
    130 
    131 		privfile = privhdr
    132 		sub(".*/", "", privfile)
    133 
    134 		printf("\n") > pubhdr
    135 		printf("\n") > privhdr
    136 
    137 		printf("\n#include <sys/cdefs.h>\n") > gencalls
    138 		printf("#include <sys/systm.h>\n") > gencalls
    139 		printf("\n#include <rump/rump.h>\n") > gencalls
    140 		printf("#include <rump/%s>\n\n", pubfile) > gencalls
    141 		printf("#include \"%s\"\n\n", privfile) > gencalls
    142 		printf("void __dead rump_%s_unavailable(void);\n",	\
    143 		    myname) > gencalls
    144 		printf("void __dead\nrump_%s_unavailable(void)\n{\n",	\
    145 		    myname) > gencalls
    146 		printf("\n\tpanic(\"%s interface unavailable\");\n}\n",	\
    147 		    myname) > gencalls
    148 	}
    149 
    150 	funtype = $1
    151 	sub("[ \t]*$", "", funtype)
    152 	funname = $2
    153 	sub("[ \t]*$", "", funname)
    154 	funargs = $3
    155 	sub("[ \t]*$", "", funargs)
    156 
    157 	printf("%s rump_%s(%s);\n", funtype, funname, funargs) > pubhdr
    158 	printf("%s rumppriv_%s(%s);\n", funtype, funname, funargs) > privhdr
    159 
    160 	if (funtype == "void")
    161 		voidret = 1
    162 	else
    163 		voidret = 0
    164 	if (funargs == "void")
    165 		voidarg = 1
    166 	else
    167 		voidarg = 0
    168 
    169 	printf("\n%s\nrump_%s(", funtype, funname) > gencalls
    170 	if (!voidarg) {
    171 		narg = split(funargs, argv, ",")
    172 		for (i = 1; i <= narg; i++) {
    173 			sub(" *", "", argv[i])
    174 			if (match(argv[i], "\\*$") != 0)
    175 				printf("%sarg%d", argv[i], i) > gencalls
    176 			else
    177 				printf("%s arg%d", argv[i], i) > gencalls
    178 			if (i != narg)
    179 				printf(", ") > gencalls
    180 		}
    181 	} else {
    182 		narg = 0
    183 		printf("void") > gencalls
    184 	}
    185 	printf(")\n{\n") > gencalls
    186 
    187 	if (!voidret) {
    188 		printf("\t%s rv;\n", funtype) > gencalls
    189 	}
    190 	printf("\n\t") > gencalls
    191 	if (!voidret)
    192 		printf("rv = ") > gencalls
    193 	printf("rumppriv_%s(", funname) > gencalls
    194 	for (i = 1; i <= narg; i++) {
    195 		printf("arg%i", i) > gencalls
    196 		if (i < narg)
    197 			printf(", ") > gencalls
    198 	}
    199 	printf(");\n") > gencalls
    200 	if (!voidret)
    201 		printf("\n\treturn rv;\n") > gencalls
    202 	printf("}\n") > gencalls
    203 	if (isweak)
    204 		printf("__weak_alias(rumppriv_%s,rump_%s_unavailable);\n", \
    205 		    funname, myname) > gencalls
    206 }'
    207