convert_drvapi.sh revision 267870
1#!/bin/sh 2# 3# Copyright (c) 2014 Juniper Networks, Inc. 4# All rights reserved. 5# 6# Redistribution and use in source and binary forms, with or without 7# modification, are permitted provided that the following conditions 8# are met: 9# 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# $FreeBSD: head/tools/ifnet/convert_drvapi.sh 267870 2014-06-25 15:22:14Z marcel $ 28# 29 30# 31# Convert a NIC driver to use the procdural API. 32# It doesn't take care of all the # cases yet, 33# but still does about 95% of work. 34# 35# Author: Sreekanth Rupavatharam 36# 37 38if [ $# -lt 1 ] 39then 40 echo " $0 <driver source (e.g., if_em.c)>"; 41 exit 1; 42fi 43 44# XXX - This needs to change if the data structure uses different name 45__ifp__="ifp"; 46 47file=$1 48 49rotateCursor() { 50 case $toggle 51 in 52 1) 53 printf " \\ " 54 printf "\b\b" 55 toggle="2" 56 ;; 57 58 2) 59 printf " | " 60 printf "\b\b\b" 61 toggle="3" 62 ;; 63 64 3) 65 printf " / " 66 printf "\b\b\b" 67 toggle="4" 68 ;; 69 70 *) 71 printf " - " 72 printf "\b\b\b" 73 toggle="1" 74 ;; 75 esac 76} 77 78handle_set() { 79# Handle the case where $__ifp__->if_blah = XX; 80 line=$1 81 set=`echo $line| grep "$__ifp__->.* = "` 82 if [ ! -z "$set" ] 83 then 84 word=`echo $line | awk -F "if_" ' { print $2 }' | awk -F" =" '{ print $1 }'` 85 value=`echo $line | awk -F "=" '{ print $2 }' | sed -e 's/;//g'` 86 new=`echo if_set$word"\($__ifp__,"$value");"` 87 new=`echo $new | sed -e 's/&/\\\&/'` 88 old=`echo $line|sed -e 's/^[ ]*//'` 89 line=`echo $line| sed -e's/'$old'/'$new'/g'` 90 return 0 91 fi 92 return 1 93} 94 95handle_inc() { 96 line=$1 97 inc=`echo $line | grep "$__ifp__->.*++\|++$__ifp__->.*"` 98 if [ ! -z "$inc" ] 99 then 100 word=`echo $line | awk -F"if_" '{ print $2 }'|awk -F"\+" '{ print $1}'` 101 value=' 1'; 102 old=`echo $line|sed -e 's/^[ ]*//'` 103 new=`echo if_inc$word"\($__ifp__,"$value");"` 104 new=`echo $new | sed -e 's/&/\\\&/'` 105 line=`echo $line| sed -e's/'$old'/'$new'/g'` 106 return 0; 107 fi 108 return 1; 109} 110 111handle_add() { 112 line=$1 113 add=`echo $line|grep "$__ifp__->.*+= "` 114 if [ ! -z "$add" ] 115 then 116 word=`echo $line | awk -F"if_" '{ print $2 }'|awk '{ print $1}'` 117 value=`echo $line | awk -F"=" '{ print $2}' | sed -e 's/;//g'` 118 new=`echo if_inc$word"\($__ifp__,$value);"` 119 new=`echo $new | sed -e 's/&/\\\&/'` 120 old=`echo $line|sed -e 's/^[ ]*//'` 121 line=`echo $line| sed -e's/'$old'/'$new'/g'` 122 return 0 123 fi 124 return 1; 125 126} 127 128handle_or() { 129 line=$1 130 or=`echo $line|grep "$__ifp__->.*|= "` 131 if [ ! -z "$or" ] 132 then 133 word=`echo $line | awk -F"if_" '{ print $2 }'|awk '{ print $1}'` 134 value=`echo $line | awk -F"=" '{ print $2}' | sed -e 's/;//g'` 135 new=`echo if_set${word}bit"($__ifp__,$value, 0);"` 136 new=`echo $new | sed -e 's/&/\\\&/'` 137 #line=`echo $line|sed -e 's/&/\\&/'` 138 old=`echo $line|sed -e 's/^[ ]*//'` 139 line=`echo $line| sed -e's/'$old'/'$new'/g'` 140 return 0; 141 fi 142 return 1; 143 144} 145 146handle_and() { 147 line=$1 148 or=`echo $line|grep "$__ifp__->.*&= "` 149 if [ ! -z "$or" ] 150 then 151 word=`echo $line | awk -F"if_" '{ print $2 }'|awk '{ print $1}'` 152 value=`echo $line | awk -F"=" '{ print $2}' | sed -e 's/;//g'` 153 value=`echo $value | sed -e's/~//g'` 154 new=`echo if_set${word}bit"\($__ifp__, 0,$value);"` 155 new=`echo $new | sed -e 's/&/\\\&/'` 156 old=`echo $line|sed -e 's/^[ ]*//'` 157 line=`echo $line| sed -e's/'$old'/'$new'/g'` 158 return 0; 159 fi 160 return 1; 161 162} 163 164handle_toggle() { 165 line=$1 166 if [ ! -z `echo $line | grep "\^="` ] 167 then 168 line=`echo $line | sed -e 's/'"$__ifp__"'->if_\(.*\) ^=\(.*\);/if_toggle\1('"$__ifp__"',\2);/g'` 169 return 0; 170 fi 171 return 1 172 173} 174 175# XXX - this needs updating 176handle_misc() { 177 line=$1 178 get=`echo $line | grep "if_capabilities\|if_flags\|if_softc\|if_capenable\|if_mtu\|if_drv_flags"` 179 if [ ! -z "$get" ] 180 then 181 word=`echo $line |awk -F"$__ifp__->if_" '{ print $2 }' | \ 182 sed -e's/[^a-zA-Z0-9_]/\@/'|awk -F"\@" '{ print $1}'` 183 old=`echo "$__ifp__->if_"${word}` 184 new=`echo "if_get"${word}"($__ifp__)"` 185 new=`echo $new | sed -e 's/&/\\\&/'` 186 line=`echo $line| sed -e's/'$old'/'$new'/g'` 187 return 0; 188 fi 189 return 1; 190 191} 192 193replace_str () 194{ 195 line=$1 196 orig=$2 197 new=$3 198 line=`echo $line | sed -e 's/'"$orig"'\(.*\)/'"$new"'\1/g'` 199 return 0; 200} 201 202# Handle special cases which do not fall under regular patterns 203handle_special () 204{ 205 line=$1 206 replace_str $line "(\*$__ifp__->if_input)" "if_input" 207 replace_str $line "if_setinit" "if_setinitfn" 208 replace_str $line "if_setioctl" "if_setioctlfn" 209 replace_str $line "if_getdrv_flags" "if_getdrvflags" 210 replace_str $line "if_setdrv_flagsbit" "if_setdrvflagbits" 211 replace_str $line "if_setstart" "if_setstartfn" 212 replace_str $line "if_sethwassistbit" "if_sethwassistbits" 213 replace_str $line "ifmedia_init" "ifmedia_init_drv" 214 replace_str $line "IFQ_DRV_IS_EMPTY(&$__ifp__->if_snd)" "if_sendq_empty($__ifp__)" 215 replace_str $line "IFQ_DRV_PREPEND(&$__ifp__->if_snd" "if_sendq_prepend($__ifp__" 216 replace_str $line "IFQ_SET_READY(&ifp->if_snd)" "if_setsendqready($__ifp__)" 217 line=`echo $line | sed -e 's/IFQ_SET_MAXLEN(&'$__ifp__'->if_snd, \(.*\))/if_setsendqlen('$__ifp__', \1)/g'` 218 line=`echo $line | sed -e 's/IFQ_DRV_DEQUEUE(&'$__ifp__'->if_snd, \(.*\))/\1 = if_dequeue('$__ifp__')/g'` 219 return 0 220} 221 222if [ -e $file.tmp ] 223then 224 rm $file.tmp 225fi 226IFS= 227echo -n "Conversion for $file started, please wait: " 228FAIL_PAT="XXX - DRVAPI" 229count=0 230cat $1 | while read -r line 231do 232count=`expr $count + 1` 233rotateCursor 234pat=`echo $line | grep "$__ifp__->"` 235while [ "$pat" != "" ] 236do 237 pat=`echo $line | grep "$__ifp__->"` 238 if [ ! -z `echo $pat | grep "$FAIL_PAT"` ] 239 then 240 break; 241 fi 242 243 handle_set $line 244 245 if [ $? != 0 ] 246 then 247 handle_inc $line 248 fi 249 250 if [ $? != 0 ] 251 then 252 handle_add $line 253 fi 254 255 if [ $? != 0 ] 256 then 257 handle_or $line 258 fi 259 260 if [ $? != 0 ] 261 then 262 handle_and $line 263 fi 264 265 if [ $? != 0 ] 266 then 267 handle_toggle $line 268 fi 269 270 if [ $? != 0 ] 271 then 272 handle_misc $line 273 fi 274 275 if [ $? != 0 ] 276 then 277 handle_special $line 278 fi 279 280 if [ ! -z `echo $line | grep "$__ifp__->"` ] 281 then 282 line=`echo $line | sed -e 's:$: \/* '${FAIL_PAT}' *\/:g'` 283 fi 284done 285 line=`echo "$line" | sed -e 's:VLAN_CAPABILITIES('$__ifp__'):if_vlancap('$__ifp__'):g'` 286 # Replace the ifnet * with if_t 287 if [ ! -z `echo $line | grep "struct ifnet"` ] 288 then 289 line=`echo $line | sed -e 's/struct ifnet[ \t]*\*/if_t /g'` 290 fi 291 echo "$line" >> $file.tmp 292done 293echo "" 294count=`grep $FAIL_PAT $file.tmp | wc -l` 295if [ $count -gt 0 ] 296then 297 echo "$count lines could not be converted to DRVAPI" 298 echo "Look for /* $FAIL_PAT */ in the converted file" 299fi 300echo "original $file has been moved to $file.orig" 301mv $file $file.orig 302mv $file.tmp $file 303