1#!/bin/sh 2# 3# $NetBSD: makerumpif.sh,v 1.6 2013/02/14 10:54:54 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 36usage () 37{ 38 39 echo "usage: $0 spec" 40 exit 1 41} 42 43boom () 44{ 45 46 echo $* 47 exit 1 48} 49 50unset TOPDIR 51if [ $# -eq 3 ]; then 52 if [ $1 = '-R' ]; then 53 TOPDIR=$2 54 else 55 usage 56 fi 57 shift; shift 58fi 59[ $# -ne 1 ] && usage 60 61MYDIR=`pwd` 62if [ -z "${TOPDIR}" ]; then 63 while [ ! -f Makefile.rump ]; do 64 [ `pwd` = '/' ] && boom Could not find rump topdir. 65 cd .. 66 done 67 TOPDIR="`pwd`" 68fi 69cd ${MYDIR} 70 71sed -e ' 72:again 73 /\\$/{ 74 N 75 s/[ ]*\\\n[ ]*/ / 76 b again 77 } 78' ${1} | awk -F\| -v topdir=${TOPDIR} ' 79function fileheaders(file, srcstr) 80{ 81 printf("/*\t$NetBSD: makerumpif.sh,v 1.6 2013/02/14 10:54:54 pooka Exp $\t*/\n\n") > file 82 printf("/*\n * Automatically generated. DO NOT EDIT.\n") > file 83 genstr = "$NetBSD: makerumpif.sh,v 1.6 2013/02/14 10:54:54 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 90function die(str) 91{ 92 93 print str 94 exit(1) 95} 96 97NR == 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 172 if (funtype == "void") 173 voidret = 1 174 else 175 voidret = 0 176 if (funargs == "void") 177 voidarg = 1 178 else 179 voidarg = 0 180 181 printf("\n%s\nrump_pub_%s(", funtype, funname) > gencalls 182 if (!voidarg) { 183 narg = split(funargs, argv, ",") 184 for (i = 1; i <= narg; i++) { 185 sub(" *", "", argv[i]) 186 if (match(argv[i], "\\*$") != 0) 187 printf("%sarg%d", argv[i], i) > gencalls 188 else 189 printf("%s arg%d", argv[i], i) > gencalls 190 if (i != narg) 191 printf(", ") > gencalls 192 } 193 } else { 194 narg = 0 195 printf("void") > gencalls 196 } 197 printf(")\n{\n") > gencalls 198 199 if (!voidret) { 200 printf("\t%s rv;\n", funtype) > gencalls 201 } 202 printf("\n\trump_schedule();\n\t") > gencalls 203 if (!voidret) 204 printf("rv = ") > gencalls 205 printf("rump_%s(", funname) > gencalls 206 for (i = 1; i <= narg; i++) { 207 printf("arg%i", i) > gencalls 208 if (i < narg) 209 printf(", ") > gencalls 210 } 211 printf(");\n\trump_unschedule();\n") > gencalls 212 if (!voidret) 213 printf("\n\treturn rv;\n") > gencalls 214 printf("}\n") > gencalls 215 if (isweak) 216 printf("__weak_alias(rump_%s,rump_%s_unavailable);\n", \ 217 funname, myname) > gencalls 218}' 219